]> git.saurik.com Git - apple/objc4.git/blob - test/protocol.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / protocol.m
1 // TEST_CFLAGS -Wno-deprecated-declarations
2
3 #include "test.h"
4
5 #include <string.h>
6 #include <objc/objc-runtime.h>
7
8 #if !__OBJC2__
9 #include <objc/Protocol.h>
10 #endif
11
12 @protocol Proto1
13 +proto1ClassMethod;
14 -proto1InstanceMethod;
15 @end
16
17 @protocol Proto2
18 +proto2ClassMethod;
19 -proto2InstanceMethod;
20 @end
21
22 @protocol Proto3 <Proto2>
23 +proto3ClassMethod;
24 -proto3InstanceMethod;
25 @end
26
27 @protocol Proto4
28 @property int i;
29 @end
30
31 @protocol ProtoEmpty
32 @end
33
34 @interface Super <Proto1> { id isa; } @end
35 @implementation Super
36 +class { return self; }
37 +(void)initialize { }
38 +proto1ClassMethod { return self; }
39 -proto1InstanceMethod { return self; }
40 @end
41
42 @interface SubNoProtocols : Super { } @end
43 @implementation SubNoProtocols @end
44
45 @interface SuperNoProtocols { id isa; } @end
46 @implementation SuperNoProtocols
47 +class { return self; }
48 +(void)initialize { }
49 @end
50
51 @interface SubProp : Super <Proto4> { int i; } @end
52 @implementation SubProp
53 @synthesize i;
54 @end
55
56
57 int main()
58 {
59 Class cls;
60 Protocol * const *list;
61 Protocol *protocol, *empty;
62 #if !__OBJC2__
63 struct objc_method_description *desc;
64 #endif
65 struct objc_method_description desc2;
66 objc_property_t *proplist;
67 unsigned int count;
68
69 protocol = @protocol(Proto3);
70 empty = @protocol(ProtoEmpty);
71 testassert(protocol);
72 testassert(empty);
73
74 #if !__OBJC2__
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"));
79 #endif
80 testassert(0 == strcmp(protocol_getName(protocol), "Proto3"));
81 testassert(0 == strcmp(protocol_getName(empty), "ProtoEmpty"));
82
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)));
88
89 testassert(!protocol_conformsToProtocol(@protocol(Proto1), @protocol(Proto2)));
90 testassert(protocol_conformsToProtocol(@protocol(Proto3), @protocol(Proto2)));
91 testassert(!protocol_conformsToProtocol(@protocol(Proto2), @protocol(Proto3)));
92
93 #if !__OBJC2__
94 testassert([@protocol(Proto1) isEqual:@protocol(Proto1)]);
95 testassert(! [@protocol(Proto1) isEqual:@protocol(Proto2)]);
96 #endif
97 testassert(protocol_isEqual(@protocol(Proto1), @protocol(Proto1)));
98 testassert(! protocol_isEqual(@protocol(Proto1), @protocol(Proto2)));
99
100 #if !__OBJC2__
101 desc = [protocol descriptionForInstanceMethod:@selector(proto3InstanceMethod)];
102 testassert(desc);
103 testassert(desc->name == @selector(proto3InstanceMethod));
104 desc = [protocol descriptionForClassMethod:@selector(proto3ClassMethod)];
105 testassert(desc);
106 testassert(desc->name == @selector(proto3ClassMethod));
107 desc = [protocol descriptionForInstanceMethod:@selector(proto3ClassMethod)];
108 testassert(!desc);
109 desc = [protocol descriptionForClassMethod:@selector(proto3InstanceMethod)];
110 testassert(!desc);
111 desc = [empty descriptionForInstanceMethod:@selector(proto3ClassMethod)];
112 testassert(!desc);
113 desc = [empty descriptionForClassMethod:@selector(proto3InstanceMethod)];
114 testassert(!desc);
115 #endif
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));
122
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);
131
132 count = 100;
133 list = protocol_copyProtocolList(@protocol(Proto2), &count);
134 testassert(!list);
135 testassert(count == 0);
136 count = 100;
137 list = protocol_copyProtocolList(@protocol(Proto3), &count);
138 testassert(list);
139 testassert(count == 1);
140 testassert(protocol_isEqual(list[0], @protocol(Proto2)));
141 testassert(!list[1]);
142 free((void*)list);
143
144 count = 100;
145 cls = objc_getClass("Super");
146 testassert(cls);
147 list = class_copyProtocolList(cls, &count);
148 testassert(list);
149 testassert(list[count] == NULL);
150 testassert(count == 1);
151 testassert(0 == strcmp(protocol_getName(list[0]), "Proto1"));
152 free((void*)list);
153
154 count = 100;
155 cls = objc_getClass("SuperNoProtocols");
156 testassert(cls);
157 list = class_copyProtocolList(cls, &count);
158 testassert(!list);
159 testassert(count == 0);
160
161 count = 100;
162 cls = objc_getClass("SubNoProtocols");
163 testassert(cls);
164 list = class_copyProtocolList(cls, &count);
165 testassert(!list);
166 testassert(count == 0);
167
168
169 cls = objc_getClass("SuperNoProtocols");
170 testassert(cls);
171 list = class_copyProtocolList(cls, NULL);
172 testassert(!list);
173
174 cls = objc_getClass("Super");
175 testassert(cls);
176 list = class_copyProtocolList(cls, NULL);
177 testassert(list);
178 free((void*)list);
179
180 count = 100;
181 list = class_copyProtocolList(NULL, &count);
182 testassert(!list);
183 testassert(count == 0);
184
185
186 // Check property added by protocol
187 cls = objc_getClass("SubProp");
188 testassert(cls);
189
190 count = 100;
191 list = class_copyProtocolList(cls, &count);
192 testassert(list);
193 testassert(count == 1);
194 testassert(0 == strcmp(protocol_getName(list[0]), "Proto4"));
195 testassert(list[1] == NULL);
196 free((void*)list);
197
198 count = 100;
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);
204 free(proplist);
205
206 succeed(__FILE__);
207 }