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