]> git.saurik.com Git - apple/objc4.git/blob - test/copyPropertyList.m
58210165f48da7c6122734859d15e52c0cc16536
[apple/objc4.git] / test / copyPropertyList.m
1 #include "test.h"
2 #include <string.h>
3 #include <malloc/malloc.h>
4 #include <objc/objc-runtime.h>
5
6 @interface SuperProps { id isa; int prop1; int prop2; }
7 @property int prop1;
8 @property int prop2;
9 @end
10 @implementation SuperProps
11 @synthesize prop1;
12 @synthesize prop2;
13 @end
14
15 @interface SubProps : SuperProps { int prop3; int prop4; }
16 @property int prop3;
17 @property int prop4;
18 @end
19 @implementation SubProps
20 @synthesize prop3;
21 @synthesize prop4;
22 @end
23
24
25 @interface FourProps { int prop1; int prop2; int prop3; int prop4; }
26 @property int prop1;
27 @property int prop2;
28 @property int prop3;
29 @property int prop4;
30 @end
31 @implementation FourProps
32 @synthesize prop1;
33 @synthesize prop2;
34 @synthesize prop3;
35 @synthesize prop4;
36 @end
37
38 @interface NoProps @end
39 @implementation NoProps @end
40
41 static int isNamed(objc_property_t p, const char *name)
42 {
43 return (0 == strcmp(name, property_getName(p)));
44 }
45
46 int main()
47 {
48 objc_property_t *props;
49 unsigned int count;
50 Class cls;
51
52 cls = objc_getClass("SubProps");
53 testassert(cls);
54
55 count = 100;
56 props = class_copyPropertyList(cls, &count);
57 testassert(props);
58 testassert(count == 2);
59 testassert((isNamed(props[0], "prop3") && isNamed(props[1], "prop4")) ||
60 (isNamed(props[1], "prop3") && isNamed(props[0], "prop4")));
61 // props[] should be null-terminated
62 testassert(props[2] == NULL);
63 free(props);
64
65 cls = objc_getClass("SuperProps");
66 testassert(cls);
67
68 count = 100;
69 props = class_copyPropertyList(cls, &count);
70 testassert(props);
71 testassert(count == 2);
72 testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) ||
73 (isNamed(props[1], "prop1") && isNamed(props[0], "prop2")));
74 // props[] should be null-terminated
75 testassert(props[2] == NULL);
76 free(props);
77
78 // Check null-termination - this property list block would be 16 bytes
79 // if it weren't for the terminator
80 cls = objc_getClass("FourProps");
81 testassert(cls);
82
83 count = 100;
84 props = class_copyPropertyList(cls, &count);
85 testassert(props);
86 testassert(count == 4);
87 testassert(malloc_size(props) >= 5 * sizeof(objc_property_t));
88 testassert(props[3] != NULL);
89 testassert(props[4] == NULL);
90 free(props);
91
92 // Check NULL count parameter
93 props = class_copyPropertyList(cls, NULL);
94 testassert(props);
95 testassert(props[4] == NULL);
96 testassert(props[3] != NULL);
97 free(props);
98
99 // Check NULL class parameter
100 count = 100;
101 props = class_copyPropertyList(NULL, &count);
102 testassert(!props);
103 testassert(count == 0);
104
105 // Check NULL class and count
106 props = class_copyPropertyList(NULL, NULL);
107 testassert(!props);
108
109 // Check class with no properties
110 cls = objc_getClass("NoProps");
111 testassert(cls);
112
113 count = 100;
114 props = class_copyPropertyList(cls, &count);
115 testassert(!props);
116 testassert(count == 0);
117
118 succeed(__FILE__);
119 return 0;
120 }