]> git.saurik.com Git - apple/objc4.git/blob - test/weak.m
objc4-493.9.tar.gz
[apple/objc4.git] / test / weak.m
1 // See instructions in weak.h
2
3 #include "test.h"
4 #include "weak.h"
5
6 // Subclass of superclass that isn't there
7 @interface MyMissingSuper : MissingSuper
8 +(int) method;
9 @end
10 @implementation MyMissingSuper
11 +(int) method { return 1+[super method]; }
12 +(void) load { state++; }
13 @end
14
15 // Subclass of subclass of superclass that isn't there
16 @interface MyMissingSub : MyMissingSuper
17 +(int) method;
18 @end
19 @implementation MyMissingSub
20 +(int) method { return 1+[super method]; }
21 +(void) load { state++; }
22 @end
23
24 // Subclass of real superclass
25 @interface MyNotMissingSuper : NotMissingSuper
26 +(int) method;
27 @end
28 @implementation MyNotMissingSuper
29 +(int) method { return 1+[super method]; }
30 +(void) load { state++; }
31 @end
32
33 // Subclass of subclass of superclass that isn't there
34 @interface MyNotMissingSub : MyNotMissingSuper
35 +(int) method;
36 @end
37 @implementation MyNotMissingSub
38 +(int) method { return 1+[super method]; }
39 +(void) load { state++; }
40 @end
41
42 // Categories on all of the above
43 @interface MissingRoot (MissingRootExtras)
44 +(void)load;
45 +(int) cat_method;
46 @end
47 @implementation MissingRoot (MissingRootExtras)
48 +(void)load { state++; }
49 +(int) cat_method { return 40; }
50 @end
51
52 @interface MissingSuper (MissingSuperExtras)
53 +(void)load;
54 +(int) cat_method;
55 @end
56 @implementation MissingSuper (MissingSuperExtras)
57 +(void)load { state++; }
58 +(int) cat_method { return 1+[super cat_method]; }
59 @end
60
61 @interface MyMissingSuper (MyMissingSuperExtras)
62 +(void)load;
63 +(int) cat_method;
64 @end
65 @implementation MyMissingSuper (MyMissingSuperExtras)
66 +(void)load { state++; }
67 +(int) cat_method { return 1+[super cat_method]; }
68 @end
69
70 @interface MyMissingSub (MyMissingSubExtras)
71 +(void)load;
72 +(int) cat_method;
73 @end
74 @implementation MyMissingSub (MyMissingSubExtras)
75 +(void)load { state++; }
76 +(int) cat_method { return 1+[super cat_method]; }
77 @end
78
79
80 @interface NotMissingRoot (NotMissingRootExtras)
81 +(void)load;
82 +(int) cat_method;
83 @end
84 @implementation NotMissingRoot (NotMissingRootExtras)
85 +(void)load { state++; }
86 +(int) cat_method { return 30; }
87 @end
88
89 @interface NotMissingSuper (NotMissingSuperExtras)
90 +(void)load;
91 +(int) cat_method;
92 @end
93 @implementation NotMissingSuper (NotMissingSuperExtras)
94 +(void)load { state++; }
95 +(int) cat_method { return 1+[super cat_method]; }
96 @end
97
98 @interface MyNotMissingSuper (MyNotMissingSuperExtras)
99 +(void)load;
100 +(int) cat_method;
101 @end
102 @implementation MyNotMissingSuper (MyNotMissingSuperExtras)
103 +(void)load { state++; }
104 +(int) cat_method { return 1+[super cat_method]; }
105 @end
106
107 @interface MyNotMissingSub (MyNotMissingSubExtras)
108 +(void)load;
109 +(int) cat_method;
110 @end
111 @implementation MyNotMissingSub (MyNotMissingSubExtras)
112 +(void)load { state++; }
113 +(int) cat_method { return 1+[super cat_method]; }
114 @end
115
116
117 #if WEAK_FRAMEWORK
118 # define TESTIVAR(cond) testassert(cond)
119 #else
120 # define TESTIVAR(cond) /* rdar */
121 #endif
122
123 static BOOL classInList(Class *classes, const char *name)
124 {
125 Class *cp;
126 for (cp = classes; *cp; cp++) {
127 if (0 == strcmp(class_getName(*cp), name)) return YES;
128 }
129 return NO;
130 }
131
132 static BOOL classInNameList(const char **names, const char *name)
133 {
134 const char **cp;
135 for (cp = names; *cp; cp++) {
136 if (0 == strcmp(*cp, name)) return YES;
137 }
138 return NO;
139 }
140
141 int main(int argc __unused, char **argv)
142 {
143 BOOL weakMissing;
144 if (strstr(argv[0], "-not-missing.out")) {
145 weakMissing = NO;
146 } else if (strstr(argv[0], "-missing.out")) {
147 weakMissing = YES;
148 } else {
149 fail("executable name must be weak*-missing.out or weak*-not-missing.out");
150 }
151
152 // class and category +load methods
153 if (weakMissing) testassert(state == 8);
154 else testassert(state == 16);
155 state = 0;
156
157 // classes
158 testassert([NotMissingRoot class]);
159 testassert([NotMissingSuper class]);
160 testassert([MyNotMissingSuper class]);
161 testassert([MyNotMissingSub class]);
162 if (weakMissing) {
163 testassert(! [MissingRoot class]);
164 testassert(! [MissingSuper class]);
165 testassert(! [MyMissingSuper class]);
166 testassert(! [MyMissingSub class]);
167 } else {
168 testassert([MissingRoot class]);
169 testassert([MissingSuper class]);
170 testassert([MyMissingSuper class]);
171 testassert([MyMissingSub class]);
172 }
173
174 // objc_getClass
175 testassert(objc_getClass("NotMissingRoot"));
176 testassert(objc_getClass("NotMissingSuper"));
177 testassert(objc_getClass("MyNotMissingSuper"));
178 testassert(objc_getClass("MyNotMissingSub"));
179 if (weakMissing) {
180 testassert(! objc_getClass("MissingRoot"));
181 testassert(! objc_getClass("MissingSuper"));
182 testassert(! objc_getClass("MyMissingSuper"));
183 testassert(! objc_getClass("MyMissingSub"));
184 } else {
185 testassert(objc_getClass("MissingRoot"));
186 testassert(objc_getClass("MissingSuper"));
187 testassert(objc_getClass("MyMissingSuper"));
188 testassert(objc_getClass("MyMissingSub"));
189 }
190
191 // class list
192 Class *classes = objc_copyClassList(NULL);
193 testassert(classInList(classes, "NotMissingRoot"));
194 testassert(classInList(classes, "NotMissingSuper"));
195 testassert(classInList(classes, "MyNotMissingSuper"));
196 testassert(classInList(classes, "MyNotMissingSub"));
197 if (weakMissing) {
198 testassert(! classInList(classes, "MissingRoot"));
199 testassert(! classInList(classes, "MissingSuper"));
200 testassert(! classInList(classes, "MyMissingSuper"));
201 testassert(! classInList(classes, "MyMissingSub"));
202 } else {
203 testassert(classInList(classes, "MissingRoot"));
204 testassert(classInList(classes, "MissingSuper"));
205 testassert(classInList(classes, "MyMissingSuper"));
206 testassert(classInList(classes, "MyMissingSub"));
207 }
208 free(classes);
209
210 // class name list
211 const char *image = class_getImageName(objc_getClass("NotMissingRoot"));
212 testassert(image);
213 const char **names = objc_copyClassNamesForImage(image, NULL);
214 testassert(names);
215 testassert(classInNameList(names, "NotMissingRoot"));
216 testassert(classInNameList(names, "NotMissingSuper"));
217 if (weakMissing) {
218 testassert(! classInNameList(names, "MissingRoot"));
219 testassert(! classInNameList(names, "MissingSuper"));
220 } else {
221 testassert(classInNameList(names, "MissingRoot"));
222 testassert(classInNameList(names, "MissingSuper"));
223 }
224 free(names);
225
226 image = class_getImageName(objc_getClass("MyNotMissingSub"));
227 testassert(image);
228 names = objc_copyClassNamesForImage(image, NULL);
229 testassert(names);
230 testassert(classInNameList(names, "MyNotMissingSuper"));
231 testassert(classInNameList(names, "MyNotMissingSub"));
232 if (weakMissing) {
233 testassert(! classInNameList(names, "MyMissingSuper"));
234 testassert(! classInNameList(names, "MyMissingSub"));
235 } else {
236 testassert(classInNameList(names, "MyMissingSuper"));
237 testassert(classInNameList(names, "MyMissingSub"));
238 }
239 free(names);
240
241 // methods
242 testassert(20 == [NotMissingRoot method]);
243 testassert(21 == [NotMissingSuper method]);
244 testassert(22 == [MyNotMissingSuper method]);
245 testassert(23 == [MyNotMissingSub method]);
246 if (weakMissing) {
247 testassert(0 == [MissingRoot method]);
248 testassert(0 == [MissingSuper method]);
249 testassert(0 == [MyMissingSuper method]);
250 testassert(0 == [MyMissingSub method]);
251 } else {
252 testassert(10 == [MissingRoot method]);
253 testassert(11 == [MissingSuper method]);
254 testassert(12 == [MyMissingSuper method]);
255 testassert(13 == [MyMissingSub method]);
256 }
257
258 // category methods
259 testassert(30 == [NotMissingRoot cat_method]);
260 testassert(31 == [NotMissingSuper cat_method]);
261 testassert(32 == [MyNotMissingSuper cat_method]);
262 testassert(33 == [MyNotMissingSub cat_method]);
263 if (weakMissing) {
264 testassert(0 == [MissingRoot cat_method]);
265 testassert(0 == [MissingSuper cat_method]);
266 testassert(0 == [MyMissingSuper cat_method]);
267 testassert(0 == [MyMissingSub cat_method]);
268 } else {
269 testassert(40 == [MissingRoot cat_method]);
270 testassert(41 == [MissingSuper cat_method]);
271 testassert(42 == [MyMissingSuper cat_method]);
272 testassert(43 == [MyMissingSub cat_method]);
273 }
274
275 // allocations and ivars
276 id obj;
277 NotMissingSuper *obj2;
278 MissingSuper *obj3;
279 testassert((obj = [[NotMissingRoot alloc] init]));
280 [obj dealloc];
281 testassert((obj2 = [[NotMissingSuper alloc] init]));
282 TESTIVAR(obj2->ivar == 200);
283 [obj2 dealloc];
284 testassert((obj2 = [[MyNotMissingSuper alloc] init]));
285 TESTIVAR(obj2->ivar == 200);
286 [obj2 dealloc];
287 testassert((obj2 = [[MyNotMissingSub alloc] init]));
288 TESTIVAR(obj2->ivar == 200);
289 [obj2 dealloc];
290 if (weakMissing) {
291 testassert(! [[MissingRoot alloc] init]);
292 testassert(! [[MissingSuper alloc] init]);
293 testassert(! [[MyMissingSuper alloc] init]);
294 testassert(! [[MyMissingSub alloc] init]);
295 } else {
296 testassert((obj = [[MissingRoot alloc] init]));
297 [obj dealloc];
298 testassert((obj3 = [[MissingSuper alloc] init]));
299 TESTIVAR(obj3->ivar == 100);
300 [obj3 dealloc];
301 testassert((obj3 = [[MyMissingSuper alloc] init]));
302 TESTIVAR(obj3->ivar == 100);
303 [obj3 dealloc];
304 testassert((obj3 = [[MyMissingSub alloc] init]));
305 TESTIVAR(obj3->ivar == 100);
306 [obj3 dealloc];
307 }
308
309 *strrchr(argv[0], '.') = 0;
310 succeed(basename(argv[0]));
311 return 0;
312 }
313