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