1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'class', covering all functions starting with 'class'. */
6 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
8 /* To get the modern GNU Objective-C Runtime API, you include
10 #include <objc/runtime.h>
15 @interface MyRootClass
22 @implementation MyRootClass
23 + alloc { return class_createInstance (self, 0); }
24 - init { return self; }
25 + initialize { return self; }
32 @protocol MySecondProtocol
33 - (id) setVariable: (id)value;
36 @interface MySubClass : MyRootClass <MyProtocol>
38 - (void) setVariable: (id)value;
42 @implementation MySubClass
43 - (void) setVariable: (id)value { variable_ivar = value; }
44 - (id) variable { return variable_ivar; }
47 @interface MyOtherSubClass : MySubClass
50 @implementation MyOtherSubClass
53 @interface DifferentClass : MyRootClass
58 @implementation DifferentClass
59 - (id) myClass { return object_getClass (self); }
60 - (id) self { return self; }
63 @interface MySubClass (MySelf)
67 /* Hack to calculate the log2 of a byte alignment. */
69 log_2_of (unsigned int x)
71 unsigned char result = 0;
73 /* We count how many times we need to divide by 2 before we reach 1.
74 This algorithm is good enough for the small numbers (such as 8,
75 16 or 64) that we have to deal with. */
87 /* Functions are tested in alphabetical order. */
89 std::cout << "Testing class_addIvar ()...\n";
91 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
96 if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
97 log_2_of (__alignof__ (id)), @encode (id)))
100 if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
101 log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
104 if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
105 log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
108 objc_registerClassPair (new_class);
111 MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
112 Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
113 Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
114 Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
116 if (variable2 == NULL || variable3 == NULL || variable4 == NULL)
119 if (std::strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
122 if (std::strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
125 if (std::strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
129 unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
130 unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
132 object_setIvar (o, variable2, new_class);
136 if (object_getIvar (o, variable2) != new_class)
148 std::cout << "Testing class_addMethod ()...\n";
150 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
151 Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
152 Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
154 if (new_class == Nil)
157 if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
158 log_2_of (__alignof__ (id)), @encode (id)))
161 if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
162 method_getTypeEncoding (method1)))
165 if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
166 method_getTypeEncoding (method2)))
169 /* Test that if the method already exists in the class,
170 class_addMethod() returns NO. */
171 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
172 method_getTypeEncoding (method2)))
175 objc_registerClassPair (new_class);
177 /* Now, MySubClass2 is basically the same as MySubClass! We'll
178 use the variable and setVariable: methods on it. */
180 MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
184 if ([o variable] != o)
188 /* Now, try that if you take an existing class and try to add an
189 already existing method, class_addMethod returns NO. This is
190 subtly different from before, when 'new_class' was still in
191 construction. Now it's a real class and the libobjc internals
192 differ between the two cases. */
193 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
194 method_getTypeEncoding (method2)))
198 std::cout << "Testing class_addProtocol ()...\n";
200 if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
203 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
206 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
210 std::cout << "Testing class_conformsToProtocol ()...\n";
212 if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
215 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
218 /* Test that class_conformsToProtocol checks the class, but not
220 if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol)))
224 std::cout << "Testing class_copyIvarList ()...\n";
227 Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
232 if (std::strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
239 std::cout << "Testing class_copyMethodList ()...\n";
242 Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
247 if (! ((std::strcmp (sel_getName (method_getName (list[0])), "variable") == 0
248 && std::strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
249 || (std::strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
250 && std::strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
257 /* TODO: Test new ABI (when available). */
258 std::cout << "Testing class_copyPropertyList ()...\n";
261 objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
263 if (count != 0 || list != NULL)
267 std::cout << "Testing class_copyProtocolList ()...\n";
270 Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
272 /* Remember that we added MySecondProtocol in the test above. */
276 if (! ((std::strcmp (protocol_getName (list[0]), "MyProtocol") == 0
277 && std::strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
278 || (std::strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
279 && std::strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
286 std::cout << "Testing class_createInstance ()...\n";
288 MySubClass *object = [[MySubClass alloc] init];
290 [object setVariable: object];
291 if ([object variable] != object)
295 std::cout << "Testing class_getClassMethod ()...\n";
297 Method method = class_getClassMethod (objc_getClass ("MySubClass"),
303 if (std::strcmp (sel_getName (method_getName (method)), "alloc") != 0)
306 if (class_getClassMethod (objc_getClass ("MySubClass"),
307 @selector(variable)))
311 std::cout << "Testing class_getClassVariable ()...\n";
313 if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
317 std::cout << "Testing class_getInstanceMethod ()...\n";
319 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
320 @selector(variable));
325 if (std::strcmp (sel_getName (method_getName (method)), "variable") != 0)
328 if (class_getInstanceMethod (objc_getClass ("MySubClass"),
333 std::cout << "Testing class_getInstanceSize ()...\n";
335 if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
339 std::cout << "Testing class_getInstanceVariable ()...\n";
341 Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
343 if (variable == NULL)
346 if (std::strcmp (ivar_getName (variable), "variable_ivar") != 0)
349 if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
353 std::cout << "Testing class_getIvarLayout ()...\n";
355 if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
359 std::cout << "Testing class_getMethodImplementation ()...\n";
361 MySubClass *object = [[MySubClass alloc] init];
362 IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"),
363 @selector(variable));
368 [object setVariable: object];
370 if ((*imp)(object, @selector(variable)) != object)
374 /* This function does not exist with the GNU runtime. */
375 /* std::cout << "Testing class_getMethodImplementation_stret ()...\n"; */
377 std::cout << "Testing class_getName ()...\n";
379 if (std::strcmp (class_getName (objc_getClass ("MyRootClass")),
384 /* TODO: Test new ABI (when available). */
385 std::cout << "Testing class_getProperty ()...\n";
387 if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
391 std::cout << "Testing class_getSuperclass ()...\n";
393 MySubClass *object = [[MySubClass alloc] init];
394 if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
398 std::cout << "Testing class_getVersion ()...\n";
400 if (class_getVersion (objc_getClass ("MySubClass")) != 0)
404 std::cout << "Testing class_getWeakIvarLayout ()...\n";
406 if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
410 std::cout << "Testing class_isMetaClass ()...\n";
412 MySubClass *object = [[MySubClass alloc] init];
413 if (class_isMetaClass (object_getClass (object))
414 || ! class_isMetaClass (object_getClass (object_getClass (object))))
418 std::cout << "Testing class_replaceMethod ()...\n";
420 Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
421 @selector (myClass));
422 Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
423 @selector (variable));
424 const char *new_types = method_getTypeEncoding (new_method);
425 IMP new_imp = method_getImplementation (new_method);
426 const char *old_types = method_getTypeEncoding (old_method);
427 IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
428 method_getImplementation (new_method),
429 method_getTypeEncoding (new_method));
430 MySubClass *o = [[MySubClass alloc] init];
434 /* Try the new method implementation. */
435 if ([o variable] != objc_getClass ("MySubClass"))
438 /* Put the original method back. */
439 class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
442 /* Test it's back to what it was. */
443 if ([o variable] != o)
447 DifferentClass *o = [[DifferentClass alloc] init];
449 /* Finally, try adding a new method. */
450 class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
453 if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
458 std::cout << "Testing class_respondsToSelector ()...\n";
460 if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
463 if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
467 /* This is not really implemented with the GNU runtime. */
468 /* std::cout << "Testing class_setIvarLayout ()...\n"; */
470 std::cout << "Testing class_setVersion ()...\n";
472 class_setVersion (objc_getClass ("MySubClass"), 45);
474 if (class_getVersion (objc_getClass ("MySubClass")) != 45)
477 class_setVersion (objc_getClass ("MySubClass"), 46);
479 if (class_getVersion (objc_getClass ("MySubClass")) != 46)
483 /* This is not really implemented with the GNU runtime. */
484 /* std::cout << "Testing class_setWeakIvarLayout ()...\n"; */