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