1 /* Test the Modern GNU Objective-C Runtime API.
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.m. */
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 "-Wno-objc-root-class" } */
11 /* { dg-additional-options "-DOBJC_OLD_DISPATCH_PROTOTYPES" { target { *-*-darwin* } } } */
13 /* To get the modern GNU Objective-C Runtime API, you include
15 #include <objc/runtime.h>
20 @interface MyRootClass
27 @implementation MyRootClass
28 + alloc { return class_createInstance (self, 0); }
29 - init { return self; }
30 + initialize { return self; }
37 @protocol MySecondProtocol
38 - (id) setVariable: (id)value;
41 @interface MySubClass : MyRootClass <MyProtocol>
43 - (void) setVariable: (id)value;
47 @implementation MySubClass
48 - (void) setVariable: (id)value { variable_ivar = value; }
49 - (id) variable { return variable_ivar; }
52 @interface MyOtherSubClass : MySubClass
55 @implementation MyOtherSubClass
58 @interface DifferentClass : MyRootClass
63 @implementation DifferentClass
64 - (id) myClass { return object_getClass (self); }
65 - (id) self { return self; }
68 @interface MySubClass (MySelf)
72 /* Hack to calculate the log2 of a byte alignment. */
74 log_2_of (unsigned int x)
76 unsigned char result = 0;
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. */
90 int main(int argc, void **args)
92 /* Functions are tested in alphabetical order. */
94 printf ("Testing class_addIvar ()...\n");
96 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
101 if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
102 log_2_of (__alignof__ (id)), @encode (id)))
105 if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
106 log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
109 if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
110 log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
113 objc_registerClassPair (new_class);
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");
121 if (variable2 == NULL || variable3 == NULL || variable4 == NULL)
124 if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
127 if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
130 if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
134 unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
135 unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
137 object_setIvar (o, variable2, new_class);
141 if (object_getIvar (o, variable2) != new_class)
153 printf ("Testing class_addMethod ()...\n");
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));
159 if (new_class == Nil)
162 if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
163 log_2_of (__alignof__ (id)), @encode (id)))
166 if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
167 method_getTypeEncoding (method1)))
170 if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
171 method_getTypeEncoding (method2)))
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)))
180 objc_registerClassPair (new_class);
182 /* Now, MySubClass2 is basically the same as MySubClass! We'll
183 use the variable and setVariable: methods on it. */
185 MySubClass *o = (MySubClass *)[[(Class)objc_getClass ("MySubClass2") alloc] init];
189 if ([o variable] != o)
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)))
203 printf ("Testing class_addProtocol ()...\n");
205 if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
208 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
211 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
215 printf ("Testing class_conformsToProtocol ()...\n");
217 if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
220 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
223 /* Test that class_conformsToProtocol checks the class, but not
225 if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol)))
229 printf ("Testing class_copyIvarList ()...\n");
232 Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
237 if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
244 printf ("Testing class_copyIvarList () on class with no instance variables...\n");
247 Ivar * list = class_copyIvarList (objc_getClass ("MyOtherSubClass"),
257 printf ("Testing class_copyMethodList ()...\n");
260 Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
265 if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
266 && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
267 || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
268 && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
275 /* TODO: Test new ABI (when available). */
276 printf ("Testing class_copyPropertyList ()...\n");
279 objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
281 if (count != 0 || list != NULL)
285 printf ("Testing class_copyProtocolList ()...\n");
288 Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
290 /* Remember that we added MySecondProtocol in the test above. */
294 if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
295 && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
296 || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
297 && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
304 printf ("Testing class_createInstance ()...\n");
306 MySubClass *object = [[MySubClass alloc] init];
308 [object setVariable: object];
309 if ([object variable] != object)
313 printf ("Testing class_getClassMethod ()...\n");
315 Method method = class_getClassMethod (objc_getClass ("MySubClass"),
321 if (strcmp (sel_getName (method_getName (method)), "alloc") != 0)
324 if (class_getClassMethod (objc_getClass ("MySubClass"),
325 @selector(variable)))
329 printf ("Testing class_getClassVariable ()...\n");
331 if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
335 printf ("Testing class_getInstanceMethod ()...\n");
337 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
338 @selector(variable));
343 if (strcmp (sel_getName (method_getName (method)), "variable") != 0)
346 if (class_getInstanceMethod (objc_getClass ("MySubClass"),
351 printf ("Testing class_getInstanceSize ()...\n");
353 if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
357 printf ("Testing class_getInstanceVariable ()...\n");
359 Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
361 if (variable == NULL)
364 if (strcmp (ivar_getName (variable), "variable_ivar") != 0)
367 if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
371 printf ("Testing class_getIvarLayout ()...\n");
373 if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
377 printf ("Testing class_getMethodImplementation ()...\n");
379 MySubClass *object = [[MySubClass alloc] init];
380 IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"),
381 @selector(variable));
386 [object setVariable: object];
388 if ((*imp)(object, @selector(variable)) != object)
392 /* This function does not exist with the GNU runtime. */
393 /* printf ("Testing class_getMethodImplementation_stret ()...\n"); */
395 printf ("Testing class_getName ()...\n");
397 if (strcmp (class_getName (objc_getClass ("MyRootClass")),
402 /* TODO: Test new ABI (when available). */
403 printf ("Testing class_getProperty ()...\n");
405 if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
409 printf ("Testing class_getSuperclass ()...\n");
411 MySubClass *object = [[MySubClass alloc] init];
412 if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
415 /* Test that it works on a newly created, but not registered, class. */
417 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass3", 0);
419 if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass"))
424 printf ("Testing class_getVersion ()...\n");
426 if (class_getVersion (objc_getClass ("MySubClass")) != 0)
430 printf ("Testing class_getWeakIvarLayout ()...\n");
432 if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
436 printf ("Testing class_isMetaClass ()...\n");
438 MySubClass *object = [[MySubClass alloc] init];
439 if (class_isMetaClass (object_getClass (object))
440 || ! class_isMetaClass (object_getClass (object_getClass (object))))
444 printf ("Testing class_replaceMethod ()...\n");
446 Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
447 @selector (myClass));
448 Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
449 @selector (variable));
450 const char *new_types = method_getTypeEncoding (new_method);
451 IMP new_imp = method_getImplementation (new_method);
452 const char *old_types = method_getTypeEncoding (old_method);
453 IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
454 method_getImplementation (new_method),
455 method_getTypeEncoding (new_method));
456 MySubClass *o = [[MySubClass alloc] init];
460 /* Try the new method implementation. */
461 if ([o variable] != objc_getClass ("MySubClass"))
464 /* Put the original method back. */
465 class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
468 /* Test it's back to what it was. */
469 if ([o variable] != o)
473 DifferentClass *o = [[DifferentClass alloc] init];
475 /* Finally, try adding a new method. */
476 class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
479 if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
484 printf ("Testing class_respondsToSelector ()...\n");
486 if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
489 if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
493 /* This is not really implemented with the GNU runtime. */
494 /* printf ("Testing class_setIvarLayout ()...\n"); */
496 printf ("Testing class_setVersion ()...\n");
498 class_setVersion (objc_getClass ("MySubClass"), 45);
500 if (class_getVersion (objc_getClass ("MySubClass")) != 45)
503 class_setVersion (objc_getClass ("MySubClass"), 46);
505 if (class_getVersion (objc_getClass ("MySubClass")) != 46)
509 /* This is not really implemented with the GNU runtime. */
510 /* printf ("Testing class_setWeakIvarLayout ()...\n"); */