Debian Codegraph @

gcc/testsuite/obj-c++.dg/gnu-api-2-class.mm

raw
1 /* Test the Modern GNU Objective-C Runtime API.
2
3 This is test 'class', covering all functions starting with 'class'.
4 Tests calling the functions with a meta class as argument are covered
5 in the separate file, gnu-api-2-class-meta.mm. */
6
7 /* { dg-do run } */
8 /* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
9 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
10 /* { dg-additional-options "-DOBJC_OLD_DISPATCH_PROTOTYPES" { target { *-*-darwin* } } } */
11 // { dg-additional-options "-Wno-objc-root-class" }
12
13 /* To get the modern GNU Objective-C Runtime API, you include
14 objc/runtime.h. */
15 #include <objc/runtime.h>
16 #include <stdlib.h>
17 #include <iostream>
18 #include <cstring>
19
20 @interface MyRootClass
21 { Class isa; }
22 + alloc;
23 - init;
24 + initialize;
25 @end
26
27 @implementation MyRootClass
28 + alloc { return class_createInstance (self, 0); }
29 - init { return self; }
30 + initialize { return self; }
31 @end
32
33 @protocol MyProtocol
34 - (id) variable;
35 @end
36
37 @protocol MySecondProtocol
38 - (id) setVariable: (id)value;
39 @end
40
41 @interface MySubClass : MyRootClass <MyProtocol>
42 { id variable_ivar; }
43 - (void) setVariable: (id)value;
44 - (id) variable;
45 @end
46
47 @implementation MySubClass
48 - (void) setVariable: (id)value { variable_ivar = value; }
49 - (id) variable { return variable_ivar; }
50 @end
51
52 @interface MyOtherSubClass : MySubClass
53 @end
54
55 @implementation MyOtherSubClass
56 @end
57
58 @interface DifferentClass : MyRootClass
59 - (id) myClass;
60 - (id) self;
61 @end
62
63 @implementation DifferentClass
64 - (id) myClass { return object_getClass (self); }
65 - (id) self { return self; }
66 @end
67
68 @interface MySubClass (MySelf)
69 - (id) mySelf;
70 @end
71
72 /* Hack to calculate the log2 of a byte alignment. */
73 unsigned char
74 log_2_of (unsigned int x)
75 {
76 unsigned char result = 0;
77
78 /* We count how many times we need to divide by 2 before we reach 1.
79 This algorithm is good enough for the small numbers (such as 8,
80 16 or 64) that we have to deal with. */
81 while (x > 1)
82 {
83 x = x / 2;
84 result++;
85 }
86
87 return result;
88 }
89
90 int main ()
91 {
92 /* Functions are tested in alphabetical order. */
93
94 std::cout << "Testing class_addIvar ()...\n";
95 {
96 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
97
98 if (new_class == Nil)
99 abort ();
100
101 if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
102 log_2_of (__alignof__ (id)), @encode (id)))
103 abort ();
104
105 if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
106 log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
107 abort ();
108
109 if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
110 log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
111 abort ();
112
113 objc_registerClassPair (new_class);
114
115 {
116 MySubClass *o = [[(Class)objc_getClass ("MySubSubClass") alloc] init];
117 Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
118 Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
119 Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
120
121 if (variable2 == NULL || variable3 == NULL || variable4 == NULL)
122 abort ();
123
124 if (std::strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
125 abort ();
126
127 if (std::strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
128 abort ();
129
130 if (std::strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
131 abort ();
132
133 {
134 unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
135 unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
136
137 object_setIvar (o, variable2, new_class);
138 *var3 = 230;
139 *var4 = 89000L;
140
141 if (object_getIvar (o, variable2) != new_class)
142 abort ();
143
144 if (*var3 != 230)
145 abort ();
146
147 if (*var4 != 89000L)
148 abort ();
149 }
150 }
151 }
152
153 std::cout << "Testing class_addMethod ()...\n";
154 {
155 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
156 Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
157 Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
158
159 if (new_class == Nil)
160 abort ();
161
162 if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
163 log_2_of (__alignof__ (id)), @encode (id)))
164 abort ();
165
166 if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
167 method_getTypeEncoding (method1)))
168 abort ();
169
170 if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
171 method_getTypeEncoding (method2)))
172 abort ();
173
174 /* Test that if the method already exists in the class,
175 class_addMethod() returns NO. */
176 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
177 method_getTypeEncoding (method2)))
178 abort ();
179
180 objc_registerClassPair (new_class);
181
182 /* Now, MySubClass2 is basically the same as MySubClass! We'll
183 use the variable and setVariable: methods on it. */
184 {
185 MySubClass *o = (MySubClass *)[[(Class)objc_getClass ("MySubClass2") alloc] init];
186
187 [o setVariable: o];
188
189 if ([o variable] != o)
190 abort ();
191 }
192
193 /* Now, try that if you take an existing class and try to add an
194 already existing method, class_addMethod returns NO. This is
195 subtly different from before, when 'new_class' was still in
196 construction. Now it's a real class and the libobjc internals
197 differ between the two cases. */
198 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
199 method_getTypeEncoding (method2)))
200 abort ();
201 }
202
203 std::cout << "Testing class_addProtocol ()...\n";
204 {
205 if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
206 abort ();
207
208 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
209 abort ();
210
211 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
212 abort ();
213 }
214
215 std::cout << "Testing class_conformsToProtocol ()...\n";
216 {
217 if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
218 abort ();
219
220 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
221 abort ();
222
223 /* Test that class_conformsToProtocol checks the class, but not
224 superclasses. */
225 if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol)))
226 abort ();
227 }
228
229 std::cout << "Testing class_copyIvarList ()...\n";
230 {
231 unsigned int count;
232 Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
233
234 if (count != 1)
235 abort ();
236
237 if (std::strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
238 abort ();
239
240 if (list[1] != NULL)
241 abort ();
242 }
243
244 std::cout << "Testing class_copyMethodList ()...\n";
245 {
246 unsigned int count;
247 Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
248
249 if (count != 2)
250 abort ();
251
252 if (! ((std::strcmp (sel_getName (method_getName (list[0])), "variable") == 0
253 && std::strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
254 || (std::strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
255 && std::strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
256 abort ();
257
258 if (list[2] != NULL)
259 abort ();
260 }
261
262 /* TODO: Test new ABI (when available). */
263 std::cout << "Testing class_copyPropertyList ()...\n";
264 {
265 unsigned int count;
266 objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
267
268 if (count != 0 || list != NULL)
269 abort ();
270 }
271
272 std::cout << "Testing class_copyProtocolList ()...\n";
273 {
274 unsigned int count;
275 Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
276
277 /* Remember that we added MySecondProtocol in the test above. */
278 if (count != 2)
279 abort ();
280
281 if (! ((std::strcmp (protocol_getName (list[0]), "MyProtocol") == 0
282 && std::strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
283 || (std::strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
284 && std::strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
285 abort ();
286
287 if (list[2] != NULL)
288 abort ();
289 }
290
291 std::cout << "Testing class_createInstance ()...\n";
292 {
293 MySubClass *object = [[MySubClass alloc] init];
294
295 [object setVariable: object];
296 if ([object variable] != object)
297 abort ();
298 }
299
300 std::cout << "Testing class_getClassMethod ()...\n";
301 {
302 Method method = class_getClassMethod (objc_getClass ("MySubClass"),
303 @selector(alloc));
304
305 if (method == NULL)
306 abort ();
307
308 if (std::strcmp (sel_getName (method_getName (method)), "alloc") != 0)
309 abort ();
310
311 if (class_getClassMethod (objc_getClass ("MySubClass"),
312 @selector(variable)))
313 abort ();
314 }
315
316 std::cout << "Testing class_getClassVariable ()...\n";
317 {
318 if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
319 abort ();
320 }
321
322 std::cout << "Testing class_getInstanceMethod ()...\n";
323 {
324 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
325 @selector(variable));
326
327 if (method == NULL)
328 abort ();
329
330 if (std::strcmp (sel_getName (method_getName (method)), "variable") != 0)
331 abort ();
332
333 if (class_getInstanceMethod (objc_getClass ("MySubClass"),
334 @selector(alloc)))
335 abort ();
336 }
337
338 std::cout << "Testing class_getInstanceSize ()...\n";
339 {
340 if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
341 abort ();
342 }
343
344 std::cout << "Testing class_getInstanceVariable ()...\n";
345 {
346 Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
347
348 if (variable == NULL)
349 abort ();
350
351 if (std::strcmp (ivar_getName (variable), "variable_ivar") != 0)
352 abort ();
353
354 if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
355 abort ();
356 }
357
358 std::cout << "Testing class_getIvarLayout ()...\n";
359 {
360 if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
361 abort ();
362 }
363
364 std::cout << "Testing class_getMethodImplementation ()...\n";
365 {
366 MySubClass *object = [[MySubClass alloc] init];
367 IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"),
368 @selector(variable));
369
370 if (imp == NULL)
371 abort ();
372
373 [object setVariable: object];
374
375 if ((*imp)(object, @selector(variable)) != object)
376 abort ();
377 }
378
379 /* This function does not exist with the GNU runtime. */
380 /* std::cout << "Testing class_getMethodImplementation_stret ()...\n"; */
381
382 std::cout << "Testing class_getName ()...\n";
383 {
384 if (std::strcmp (class_getName (objc_getClass ("MyRootClass")),
385 "MyRootClass") != 0)
386 abort ();
387 }
388
389 /* TODO: Test new ABI (when available). */
390 std::cout << "Testing class_getProperty ()...\n";
391 {
392 if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
393 abort ();
394 }
395
396 std::cout << "Testing class_getSuperclass ()...\n";
397 {
398 MySubClass *object = [[MySubClass alloc] init];
399 if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
400 abort ();
401
402 /* Test that it works on a newly created, but not registered, class. */
403 {
404 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass3", 0);
405
406 if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass"))
407 abort ();
408 }
409 }
410
411 std::cout << "Testing class_getVersion ()...\n";
412 {
413 if (class_getVersion (objc_getClass ("MySubClass")) != 0)
414 abort ();
415 }
416
417 std::cout << "Testing class_getWeakIvarLayout ()...\n";
418 {
419 if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
420 abort ();
421 }
422
423 std::cout << "Testing class_isMetaClass ()...\n";
424 {
425 MySubClass *object = [[MySubClass alloc] init];
426 if (class_isMetaClass (object_getClass (object))
427 || ! class_isMetaClass (object_getClass (object_getClass (object))))
428 abort ();
429 }
430
431 std::cout << "Testing class_replaceMethod ()...\n";
432 {
433 Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
434 @selector (myClass));
435 Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
436 @selector (variable));
437 const char *new_types = method_getTypeEncoding (new_method);
438 IMP new_imp = method_getImplementation (new_method);
439 const char *old_types = method_getTypeEncoding (old_method);
440 IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
441 method_getImplementation (new_method),
442 method_getTypeEncoding (new_method));
443 MySubClass *o = [[MySubClass alloc] init];
444
445 [o setVariable: o];
446
447 /* Try the new method implementation. */
448 if ([o variable] != objc_getClass ("MySubClass"))
449 abort ();
450
451 /* Put the original method back. */
452 class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
453 old_imp, old_types);
454
455 /* Test it's back to what it was. */
456 if ([o variable] != o)
457 abort ();
458
459 {
460 DifferentClass *o = [[DifferentClass alloc] init];
461
462 /* Finally, try adding a new method. */
463 class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
464 new_imp, new_types);
465
466 if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
467 abort ();
468 }
469 }
470
471 std::cout << "Testing class_respondsToSelector ()...\n";
472 {
473 if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
474 abort ();
475
476 if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
477 abort ();
478 }
479
480 /* This is not really implemented with the GNU runtime. */
481 /* std::cout << "Testing class_setIvarLayout ()...\n"; */
482
483 std::cout << "Testing class_setVersion ()...\n";
484 {
485 class_setVersion (objc_getClass ("MySubClass"), 45);
486
487 if (class_getVersion (objc_getClass ("MySubClass")) != 45)
488 abort ();
489
490 class_setVersion (objc_getClass ("MySubClass"), 46);
491
492 if (class_getVersion (objc_getClass ("MySubClass")) != 46)
493 abort ();
494 }
495
496 /* This is not really implemented with the GNU runtime. */
497 /* std::cout << "Testing class_setWeakIvarLayout ()...\n"; */
498
499 return (0);
500 }