1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'class', covering all functions starting with 'class'. */
6 /* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
7 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
9 /* To get the modern GNU Objective-C Runtime API, you include
11 #include <objc/runtime.h>
16 @interface MyRootClass
23 @implementation MyRootClass
24 + alloc { return class_createInstance (self, 0); }
25 - init { return self; }
26 + initialize { return self; }
33 @protocol MySecondProtocol
34 - (id) setVariable: (id)value;
37 @interface MySubClass : MyRootClass <MyProtocol>
39 - (void) setVariable: (id)value;
43 @implementation MySubClass
44 - (void) setVariable: (id)value { variable_ivar = value; }
45 - (id) variable { return variable_ivar; }
48 @interface MyOtherSubClass : MySubClass
51 @implementation MyOtherSubClass
54 @interface DifferentClass : MyRootClass
59 @implementation DifferentClass
60 - (id) myClass { return object_getClass (self); }
61 - (id) self { return self; }
64 @interface MySubClass (MySelf)
68 /* Hack to calculate the log2 of a byte alignment. */
70 log_2_of (unsigned int x)
72 unsigned char result = 0;
74 /* We count how many times we need to divide by 2 before we reach 1.
75 This algorithm is good enough for the small numbers (such as 8,
76 16 or 64) that we have to deal with. */
86 int main(int argc, void **args)
88 /* Functions are tested in alphabetical order. */
90 printf ("Testing class_addIvar ()...\n");
92 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
97 if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
98 log_2_of (__alignof__ (id)), @encode (id)))
101 if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
102 log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
105 if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
106 log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
109 objc_registerClassPair (new_class);
112 MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
113 Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
114 Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
115 Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
117 if (variable2 == NULL || variable3 == NULL || variable4 == NULL)
120 if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
123 if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
126 if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
130 unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
131 unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
133 object_setIvar (o, variable2, new_class);
137 if (object_getIvar (o, variable2) != new_class)
149 printf ("Testing class_addMethod ()...\n");
151 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
152 Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
153 Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
155 if (new_class == Nil)
158 if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
159 log_2_of (__alignof__ (id)), @encode (id)))
162 if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
163 method_getTypeEncoding (method1)))
166 if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
167 method_getTypeEncoding (method2)))
170 /* Test that if the method already exists in the class,
171 class_addMethod() returns NO. */
172 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
173 method_getTypeEncoding (method2)))
176 objc_registerClassPair (new_class);
178 /* Now, MySubClass2 is basically the same as MySubClass! We'll
179 use the variable and setVariable: methods on it. */
181 MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
185 if ([o variable] != o)
189 /* Now, try that if you take an existing class and try to add an
190 already existing method, class_addMethod returns NO. This is
191 subtly different from before, when 'new_class' was still in
192 construction. Now it's a real class and the libobjc internals
193 differ between the two cases. */
194 if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
195 method_getTypeEncoding (method2)))
199 printf ("Testing class_addProtocol ()...\n");
201 if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
204 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
207 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
211 printf ("Testing class_conformsToProtocol ()...\n");
213 if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
216 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
219 /* Test that class_conformsToProtocol checks the class, but not
221 if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol)))
225 printf ("Testing class_copyIvarList ()...\n");
228 Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
233 if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
240 printf ("Testing class_copyMethodList ()...\n");
243 Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
248 if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
249 && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
250 || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
251 && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
258 /* TODO: Test new ABI (when available). */
259 printf ("Testing class_copyPropertyList ()...\n");
262 objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
264 if (count != 0 || list != NULL)
268 printf ("Testing class_copyProtocolList ()...\n");
271 Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
273 /* Remember that we added MySecondProtocol in the test above. */
277 if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
278 && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
279 || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
280 && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
287 printf ("Testing class_createInstance ()...\n");
289 MySubClass *object = [[MySubClass alloc] init];
291 [object setVariable: object];
292 if ([object variable] != object)
296 printf ("Testing class_getClassMethod ()...\n");
298 Method method = class_getClassMethod (objc_getClass ("MySubClass"),
304 if (strcmp (sel_getName (method_getName (method)), "alloc") != 0)
307 if (class_getClassMethod (objc_getClass ("MySubClass"),
308 @selector(variable)))
312 printf ("Testing class_getClassVariable ()...\n");
314 if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
318 printf ("Testing class_getInstanceMethod ()...\n");
320 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
321 @selector(variable));
326 if (strcmp (sel_getName (method_getName (method)), "variable") != 0)
329 if (class_getInstanceMethod (objc_getClass ("MySubClass"),
334 printf ("Testing class_getInstanceSize ()...\n");
336 if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
340 printf ("Testing class_getInstanceVariable ()...\n");
342 Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
344 if (variable == NULL)
347 if (strcmp (ivar_getName (variable), "variable_ivar") != 0)
350 if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
354 printf ("Testing class_getIvarLayout ()...\n");
356 if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
360 printf ("Testing class_getMethodImplementation ()...\n");
362 MySubClass *object = [[MySubClass alloc] init];
363 IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"),
364 @selector(variable));
369 [object setVariable: object];
371 if ((*imp)(object, @selector(variable)) != object)
375 /* This function does not exist with the GNU runtime. */
376 /* printf ("Testing class_getMethodImplementation_stret ()...\n"); */
378 printf ("Testing class_getName ()...\n");
380 if (strcmp (class_getName (objc_getClass ("MyRootClass")),
385 /* TODO: Test new ABI (when available). */
386 printf ("Testing class_getProperty ()...\n");
388 if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
392 printf ("Testing class_getSuperclass ()...\n");
394 MySubClass *object = [[MySubClass alloc] init];
395 if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
399 printf ("Testing class_getVersion ()...\n");
401 if (class_getVersion (objc_getClass ("MySubClass")) != 0)
405 printf ("Testing class_getWeakIvarLayout ()...\n");
407 if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
411 printf ("Testing class_isMetaClass ()...\n");
413 MySubClass *object = [[MySubClass alloc] init];
414 if (class_isMetaClass (object_getClass (object))
415 || ! class_isMetaClass (object_getClass (object_getClass (object))))
419 printf ("Testing class_replaceMethod ()...\n");
421 Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
422 @selector (myClass));
423 Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
424 @selector (variable));
425 const char *new_types = method_getTypeEncoding (new_method);
426 IMP new_imp = method_getImplementation (new_method);
427 const char *old_types = method_getTypeEncoding (old_method);
428 IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
429 method_getImplementation (new_method),
430 method_getTypeEncoding (new_method));
431 MySubClass *o = [[MySubClass alloc] init];
435 /* Try the new method implementation. */
436 if ([o variable] != objc_getClass ("MySubClass"))
439 /* Put the original method back. */
440 class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
443 /* Test it's back to what it was. */
444 if ([o variable] != o)
448 DifferentClass *o = [[DifferentClass alloc] init];
450 /* Finally, try adding a new method. */
451 class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
454 if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
459 printf ("Testing class_respondsToSelector ()...\n");
461 if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
464 if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
468 /* This is not really implemented with the GNU runtime. */
469 /* printf ("Testing class_setIvarLayout ()...\n"); */
471 printf ("Testing class_setVersion ()...\n");
473 class_setVersion (objc_getClass ("MySubClass"), 45);
475 if (class_getVersion (objc_getClass ("MySubClass")) != 45)
478 class_setVersion (objc_getClass ("MySubClass"), 46);
480 if (class_getVersion (objc_getClass ("MySubClass")) != 46)
484 /* This is not really implemented with the GNU runtime. */
485 /* printf ("Testing class_setWeakIvarLayout ()...\n"); */