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