]> git.saurik.com Git - apple/objc4.git/blob - test/protocol_copyMethodList.m
objc4-680.tar.gz
[apple/objc4.git] / test / protocol_copyMethodList.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
6 #include "test.h"
7 #include <malloc/malloc.h>
8 #include <objc/objc-runtime.h>
9
10 @protocol SuperMethods
11 +(void)SuperMethodClass;
12 +(void)SuperMethodClass2;
13 -(void)SuperMethodInstance;
14 -(void)SuperMethodInstance2;
15 @end
16
17 @protocol SubMethods
18 +(void)SubMethodClass;
19 +(void)SubMethodClass2;
20 -(void)SubMethodInstance;
21 -(void)SubMethodInstance2;
22 @end
23
24 @protocol SuperOptionalMethods
25 @optional
26 +(void)SuperOptMethodClass;
27 +(void)SuperOptMethodClass2;
28 -(void)SuperOptMethodInstance;
29 -(void)SuperOptMethodInstance2;
30 @end
31
32 @protocol SubOptionalMethods <SuperOptionalMethods>
33 @optional
34 +(void)SubOptMethodClass;
35 +(void)SubOptMethodClass2;
36 -(void)SubOptMethodInstance;
37 -(void)SubOptMethodInstance2;
38 @end
39
40 @protocol NoMethods @end
41
42 static int isNamed(struct objc_method_description m, const char *name)
43 {
44 return (m.name == sel_registerName(name));
45 }
46
47 int main()
48 {
49 struct objc_method_description *methods;
50 unsigned int count;
51 Protocol *proto;
52
53 proto = @protocol(SubMethods);
54 testassert(proto);
55
56 // Check required methods
57 count = 999;
58 methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
59 testassert(methods);
60 testassert(count == 2);
61 testassert((isNamed(methods[0], "SubMethodInstance") &&
62 isNamed(methods[1], "SubMethodInstance2"))
63 ||
64 (isNamed(methods[1], "SubMethodInstance") &&
65 isNamed(methods[0], "SubMethodInstance2")));
66 free(methods);
67
68 count = 999;
69 methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
70 testassert(methods);
71 testassert(count == 2);
72 testassert((isNamed(methods[0], "SubMethodClass") &&
73 isNamed(methods[1], "SubMethodClass2"))
74 ||
75 (isNamed(methods[1], "SubMethodClass") &&
76 isNamed(methods[0], "SubMethodClass2")));
77 free(methods);
78
79 // Check lack of optional methods
80 count = 999;
81 methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
82 testassert(!methods);
83 testassert(count == 0);
84 count = 999;
85 methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
86 testassert(!methods);
87 testassert(count == 0);
88
89
90 proto = @protocol(SubOptionalMethods);
91 testassert(proto);
92
93 // Check optional methods
94 count = 999;
95 methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
96 testassert(methods);
97 testassert(count == 2);
98 testassert((isNamed(methods[0], "SubOptMethodInstance") &&
99 isNamed(methods[1], "SubOptMethodInstance2"))
100 ||
101 (isNamed(methods[1], "SubOptMethodInstance") &&
102 isNamed(methods[0], "SubOptMethodInstance2")));
103 free(methods);
104
105 count = 999;
106 methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
107 testassert(methods);
108 testassert(count == 2);
109 testassert((isNamed(methods[0], "SubOptMethodClass") &&
110 isNamed(methods[1], "SubOptMethodClass2"))
111 ||
112 (isNamed(methods[1], "SubOptMethodClass") &&
113 isNamed(methods[0], "SubOptMethodClass2")));
114 free(methods);
115
116 // Check lack of required methods
117 count = 999;
118 methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
119 testassert(!methods);
120 testassert(count == 0);
121 count = 999;
122 methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
123 testassert(!methods);
124 testassert(count == 0);
125
126
127 // Check NULL protocol parameter
128 count = 999;
129 methods = protocol_copyMethodDescriptionList(NULL, YES, YES, &count);
130 testassert(!methods);
131 testassert(count == 0);
132 count = 999;
133 methods = protocol_copyMethodDescriptionList(NULL, YES, NO, &count);
134 testassert(!methods);
135 testassert(count == 0);
136 count = 999;
137 methods = protocol_copyMethodDescriptionList(NULL, NO, YES, &count);
138 testassert(!methods);
139 testassert(count == 0);
140 count = 999;
141 methods = protocol_copyMethodDescriptionList(NULL, NO, NO, &count);
142 testassert(!methods);
143 testassert(count == 0);
144
145 succeed(__FILE__);
146 }