1 // TEST_CFLAGS -framework Foundation
2 // need Foundation to get NSObject compatibility additions for class Protocol
3 // because ARC calls [protocol retain]
7 #include <malloc/malloc.h>
8 #include <objc/objc-runtime.h>
10 @protocol SuperMethods
11 +(void)SuperMethodClass;
12 +(void)SuperMethodClass2;
13 -(void)SuperMethodInstance;
14 -(void)SuperMethodInstance2;
18 +(void)SubMethodClass;
19 +(void)SubMethodClass2;
20 -(void)SubMethodInstance;
21 -(void)SubMethodInstance2;
24 @protocol SuperOptionalMethods
26 +(void)SuperOptMethodClass;
27 +(void)SuperOptMethodClass2;
28 -(void)SuperOptMethodInstance;
29 -(void)SuperOptMethodInstance2;
32 @protocol SubOptionalMethods <SuperOptionalMethods>
34 +(void)SubOptMethodClass;
35 +(void)SubOptMethodClass2;
36 -(void)SubOptMethodInstance;
37 -(void)SubOptMethodInstance2;
40 @protocol NoMethods @end
42 static int isNamed(struct objc_method_description m, const char *name)
44 return (m.name == sel_registerName(name));
49 struct objc_method_description *methods;
53 proto = @protocol(SubMethods);
56 // Check required methods
58 methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
60 testassert(count == 2);
61 testassert((isNamed(methods[0], "SubMethodInstance") &&
62 isNamed(methods[1], "SubMethodInstance2"))
64 (isNamed(methods[1], "SubMethodInstance") &&
65 isNamed(methods[0], "SubMethodInstance2")));
69 methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
71 testassert(count == 2);
72 testassert((isNamed(methods[0], "SubMethodClass") &&
73 isNamed(methods[1], "SubMethodClass2"))
75 (isNamed(methods[1], "SubMethodClass") &&
76 isNamed(methods[0], "SubMethodClass2")));
79 // Check lack of optional methods
81 methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
83 testassert(count == 0);
85 methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
87 testassert(count == 0);
90 proto = @protocol(SubOptionalMethods);
93 // Check optional methods
95 methods = protocol_copyMethodDescriptionList(proto, NO, YES, &count);
97 testassert(count == 2);
98 testassert((isNamed(methods[0], "SubOptMethodInstance") &&
99 isNamed(methods[1], "SubOptMethodInstance2"))
101 (isNamed(methods[1], "SubOptMethodInstance") &&
102 isNamed(methods[0], "SubOptMethodInstance2")));
106 methods = protocol_copyMethodDescriptionList(proto, NO, NO, &count);
108 testassert(count == 2);
109 testassert((isNamed(methods[0], "SubOptMethodClass") &&
110 isNamed(methods[1], "SubOptMethodClass2"))
112 (isNamed(methods[1], "SubOptMethodClass") &&
113 isNamed(methods[0], "SubOptMethodClass2")));
116 // Check lack of required methods
118 methods = protocol_copyMethodDescriptionList(proto, YES, YES, &count);
119 testassert(!methods);
120 testassert(count == 0);
122 methods = protocol_copyMethodDescriptionList(proto, YES, NO, &count);
123 testassert(!methods);
124 testassert(count == 0);
127 // Check NULL protocol parameter
129 methods = protocol_copyMethodDescriptionList(NULL, YES, YES, &count);
130 testassert(!methods);
131 testassert(count == 0);
133 methods = protocol_copyMethodDescriptionList(NULL, YES, NO, &count);
134 testassert(!methods);
135 testassert(count == 0);
137 methods = protocol_copyMethodDescriptionList(NULL, NO, YES, &count);
138 testassert(!methods);
139 testassert(count == 0);
141 methods = protocol_copyMethodDescriptionList(NULL, NO, NO, &count);
142 testassert(!methods);
143 testassert(count == 0);