1 // TEST_CFLAGS -Wno-deprecated-declarations
6 #include <objc/objc-runtime.h>
9 #include <objc/Protocol.h>
14 -proto1InstanceMethod;
19 -proto2InstanceMethod;
22 @protocol Proto3 <Proto2>
24 -proto3InstanceMethod;
34 @interface Super <Proto1> { id isa; } @end
36 +class { return self; }
38 +proto1ClassMethod { return self; }
39 -proto1InstanceMethod { return self; }
42 @interface SubNoProtocols : Super { } @end
43 @implementation SubNoProtocols @end
45 @interface SuperNoProtocols { id isa; } @end
46 @implementation SuperNoProtocols
47 +class { return self; }
51 @interface SubProp : Super <Proto4> { int i; } @end
52 @implementation SubProp
60 Protocol * const *list;
61 Protocol *protocol, *empty;
63 struct objc_method_description *desc;
65 struct objc_method_description desc2;
66 objc_property_t *proplist;
69 protocol = @protocol(Proto3);
70 empty = @protocol(ProtoEmpty);
75 testassert([protocol isKindOf:[Protocol class]]);
76 testassert([empty isKindOf:[Protocol class]]);
77 testassert(0 == strcmp([protocol name], "Proto3"));
78 testassert(0 == strcmp([empty name], "ProtoEmpty"));
80 testassert(0 == strcmp(protocol_getName(protocol), "Proto3"));
81 testassert(0 == strcmp(protocol_getName(empty), "ProtoEmpty"));
83 testassert(class_conformsToProtocol([Super class], @protocol(Proto1)));
84 testassert(!class_conformsToProtocol([SubProp class], @protocol(Proto1)));
85 testassert(class_conformsToProtocol([SubProp class], @protocol(Proto4)));
86 testassert(!class_conformsToProtocol([SubProp class], @protocol(Proto3)));
87 testassert(!class_conformsToProtocol([Super class], @protocol(Proto3)));
89 testassert(!protocol_conformsToProtocol(@protocol(Proto1), @protocol(Proto2)));
90 testassert(protocol_conformsToProtocol(@protocol(Proto3), @protocol(Proto2)));
91 testassert(!protocol_conformsToProtocol(@protocol(Proto2), @protocol(Proto3)));
94 testassert([@protocol(Proto1) isEqual:@protocol(Proto1)]);
95 testassert(! [@protocol(Proto1) isEqual:@protocol(Proto2)]);
97 testassert(protocol_isEqual(@protocol(Proto1), @protocol(Proto1)));
98 testassert(! protocol_isEqual(@protocol(Proto1), @protocol(Proto2)));
101 desc = [protocol descriptionForInstanceMethod:@selector(proto3InstanceMethod)];
103 testassert(desc->name == @selector(proto3InstanceMethod));
104 desc = [protocol descriptionForClassMethod:@selector(proto3ClassMethod)];
106 testassert(desc->name == @selector(proto3ClassMethod));
107 desc = [protocol descriptionForInstanceMethod:@selector(proto3ClassMethod)];
109 desc = [protocol descriptionForClassMethod:@selector(proto3InstanceMethod)];
111 desc = [empty descriptionForInstanceMethod:@selector(proto3ClassMethod)];
113 desc = [empty descriptionForClassMethod:@selector(proto3InstanceMethod)];
116 desc2 = protocol_getMethodDescription(protocol, @selector(proto3InstanceMethod), YES, YES);
117 testassert(desc2.name && desc2.types);
118 testassert(desc2.name == @selector(proto3InstanceMethod));
119 desc2 = protocol_getMethodDescription(protocol, @selector(proto3ClassMethod), YES, NO);
120 testassert(desc2.name && desc2.types);
121 testassert(desc2.name == @selector(proto3ClassMethod));
123 desc2 = protocol_getMethodDescription(protocol, @selector(proto3ClassMethod), YES, YES);
124 testassert(!desc2.name && !desc2.types);
125 desc2 = protocol_getMethodDescription(protocol, @selector(proto3InstanceMethod), YES, NO);
126 testassert(!desc2.name && !desc2.types);
127 desc2 = protocol_getMethodDescription(empty, @selector(proto3ClassMethod), YES, YES);
128 testassert(!desc2.name && !desc2.types);
129 desc2 = protocol_getMethodDescription(empty, @selector(proto3InstanceMethod), YES, NO);
130 testassert(!desc2.name && !desc2.types);
133 list = protocol_copyProtocolList(@protocol(Proto2), &count);
135 testassert(count == 0);
137 list = protocol_copyProtocolList(@protocol(Proto3), &count);
139 testassert(count == 1);
140 testassert(protocol_isEqual(list[0], @protocol(Proto2)));
141 testassert(!list[1]);
145 cls = objc_getClass("Super");
147 list = class_copyProtocolList(cls, &count);
149 testassert(list[count] == NULL);
150 testassert(count == 1);
151 testassert(0 == strcmp(protocol_getName(list[0]), "Proto1"));
155 cls = objc_getClass("SuperNoProtocols");
157 list = class_copyProtocolList(cls, &count);
159 testassert(count == 0);
162 cls = objc_getClass("SubNoProtocols");
164 list = class_copyProtocolList(cls, &count);
166 testassert(count == 0);
169 cls = objc_getClass("SuperNoProtocols");
171 list = class_copyProtocolList(cls, NULL);
174 cls = objc_getClass("Super");
176 list = class_copyProtocolList(cls, NULL);
181 list = class_copyProtocolList(NULL, &count);
183 testassert(count == 0);
186 // Check property added by protocol
187 cls = objc_getClass("SubProp");
191 list = class_copyProtocolList(cls, &count);
193 testassert(count == 1);
194 testassert(0 == strcmp(protocol_getName(list[0]), "Proto4"));
195 testassert(list[1] == NULL);
199 proplist = class_copyPropertyList(cls, &count);
200 testassert(proplist);
201 testassert(count == 1);
202 testassert(0 == strcmp(property_getName(proplist[0]), "i"));
203 testassert(proplist[1] == NULL);