]> git.saurik.com Git - apple/objc4.git/blob - test/copyPropertyList.m
objc4-779.1.tar.gz
[apple/objc4.git] / test / copyPropertyList.m
1 // TEST_CONFIG
2
3 #include "test.h"
4 #include <string.h>
5 #include <malloc/malloc.h>
6 #include <objc/objc-runtime.h>
7
8 OBJC_ROOT_CLASS
9 @interface SuperProps { id isa; int prop1; int prop2; }
10 @property int prop1;
11 @property int prop2;
12 @end
13 @implementation SuperProps
14 @synthesize prop1;
15 @synthesize prop2;
16 @end
17
18 @interface SubProps : SuperProps { int prop3; int prop4; }
19 @property int prop3;
20 @property int prop4;
21 @end
22 @implementation SubProps
23 @synthesize prop3;
24 @synthesize prop4;
25 @end
26
27 OBJC_ROOT_CLASS
28 @interface FourProps { int prop1; int prop2; int prop3; int prop4; }
29 @property int prop1;
30 @property int prop2;
31 @property int prop3;
32 @property int prop4;
33 @end
34 @implementation FourProps
35 @synthesize prop1;
36 @synthesize prop2;
37 @synthesize prop3;
38 @synthesize prop4;
39 @end
40
41 OBJC_ROOT_CLASS
42 @interface NoProps @end
43 @implementation NoProps @end
44
45 OBJC_ROOT_CLASS
46 @interface ClassProps
47 @property(readonly,class) int prop1;
48 @property(readonly,class) int prop2;
49 @property(readonly) int prop2;
50 @property(readonly) int prop3;
51 @end
52 @implementation ClassProps
53 +(int)prop1 { return 0; }
54 +(int)prop2 { return 0; }
55 -(int)prop2 { return 0; }
56 -(int)prop3 { return 0; }
57 @end
58
59 static int isNamed(objc_property_t p, const char *name)
60 {
61 return (0 == strcmp(name, property_getName(p)));
62 }
63
64 int main()
65 {
66 objc_property_t *props;
67 unsigned int count;
68 Class cls;
69
70 cls = objc_getClass("SubProps");
71 testassert(cls);
72
73 count = 100;
74 props = class_copyPropertyList(cls, &count);
75 testassert(props);
76 testassert(count == 2);
77 testassert((isNamed(props[0], "prop3") && isNamed(props[1], "prop4")) ||
78 (isNamed(props[1], "prop3") && isNamed(props[0], "prop4")));
79 // props[] should be null-terminated
80 testassert(props[2] == NULL);
81 free(props);
82
83 cls = objc_getClass("SuperProps");
84 testassert(cls);
85
86 count = 100;
87 props = class_copyPropertyList(cls, &count);
88 testassert(props);
89 testassert(count == 2);
90 testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) ||
91 (isNamed(props[1], "prop1") && isNamed(props[0], "prop2")));
92 // props[] should be null-terminated
93 testassert(props[2] == NULL);
94 free(props);
95
96 // Check null-termination - this property list block would be 16 bytes
97 // if it weren't for the terminator
98 cls = objc_getClass("FourProps");
99 testassert(cls);
100
101 count = 100;
102 props = class_copyPropertyList(cls, &count);
103 testassert(props);
104 testassert(count == 4);
105 testassert(malloc_size(props) >= 5 * sizeof(objc_property_t));
106 testassert(props[3] != NULL);
107 testassert(props[4] == NULL);
108 free(props);
109
110 // Check NULL count parameter
111 props = class_copyPropertyList(cls, NULL);
112 testassert(props);
113 testassert(props[4] == NULL);
114 testassert(props[3] != NULL);
115 free(props);
116
117 // Check NULL class parameter
118 count = 100;
119 props = class_copyPropertyList(NULL, &count);
120 testassert(!props);
121 testassert(count == 0);
122
123 // Check NULL class and count
124 props = class_copyPropertyList(NULL, NULL);
125 testassert(!props);
126
127 // Check class with no properties
128 cls = objc_getClass("NoProps");
129 testassert(cls);
130
131 count = 100;
132 props = class_copyPropertyList(cls, &count);
133 testassert(!props);
134 testassert(count == 0);
135
136 // Check class properties
137
138 cls = objc_getClass("ClassProps");
139 testassert(cls);
140
141 count = 100;
142 props = class_copyPropertyList(cls, &count);
143 testassert(props);
144 testassert(count == 2);
145 testassert((isNamed(props[0], "prop2") && isNamed(props[1], "prop3")) ||
146 (isNamed(props[1], "prop2") && isNamed(props[0], "prop3")));
147 // props[] should be null-terminated
148 testassert(props[2] == NULL);
149 free(props);
150
151 cls = object_getClass(objc_getClass("ClassProps"));
152 testassert(cls);
153
154 count = 100;
155 props = class_copyPropertyList(cls, &count);
156 testassert(props);
157 testassert(count == 2);
158 testassert((isNamed(props[0], "prop1") && isNamed(props[1], "prop2")) ||
159 (isNamed(props[1], "prop1") && isNamed(props[0], "prop2")));
160 // props[] should be null-terminated
161 testassert(props[2] == NULL);
162 free(props);
163
164
165 succeed(__FILE__);
166 return 0;
167 }