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