1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'class', covering all functions starting with 'class'. */
6 /* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
8 /* To get the modern GNU Objective-C Runtime API, you include
10 #include <objc/runtime.h>
15 @interface MyRootClass
21 @implementation MyRootClass
22 + alloc { return class_createInstance (self, 0); }
23 - init { return self; }
30 @protocol MySecondProtocol
31 - (id) setVariable: (id)value;
34 @interface MySubClass : MyRootClass <MyProtocol>
36 - (void) setVariable: (id)value;
40 @implementation MySubClass
41 - (void) setVariable: (id)value { variable_ivar = value; }
42 - (id) variable { return variable_ivar; }
45 @interface DifferentClass : MyRootClass
50 @implementation DifferentClass
51 - (id) myClass { return object_getClass (self); }
52 - (id) self { return self; }
55 @interface MySubClass (MySelf)
59 int main(int argc, void **args)
61 /* Functions are tested in alphabetical order. */
63 printf ("Testing class_addIvar ()...\n");
65 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
70 if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
71 __alignof__ (id), @encode (id)))
74 if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
75 __alignof__ (unsigned char), @encode (unsigned char)))
78 if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
79 __alignof__ (unsigned long), @encode (unsigned long)))
82 objc_registerClassPair (new_class);
85 MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
86 Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
87 Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
88 Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
90 if (variable2 == NULL || variable3 == NULL || variable4 == NULL)
93 if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
96 if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
99 if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
103 unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
104 unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
106 object_setIvar (o, variable2, new_class);
110 if (object_getIvar (o, variable2) != new_class)
122 printf ("Testing class_addMethod ()...\n");
124 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
125 Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
126 Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
128 if (new_class == Nil)
131 if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
132 __alignof__ (id), @encode (id)))
135 if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
136 method_getTypeEncoding (method1)))
139 if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
140 method_getTypeEncoding (method2)))
143 objc_registerClassPair (new_class);
145 /* Now, MySubClass2 is basically the same as MySubClass! We'll
146 use the variable and setVariable: methods on it. */
148 MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
152 if ([o variable] != o)
157 printf ("Testing class_addProtocol ()...\n");
159 if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
162 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
165 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
169 printf ("Testing class_conformsToProtocol ()...\n");
171 if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
174 if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
178 printf ("Testing class_copyIvarList ()...\n");
181 Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
186 if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
193 printf ("Testing class_copyMethodList ()...\n");
196 Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
201 if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
202 && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
203 || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
204 && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
211 /* TODO: Test new ABI (when available). */
212 printf ("Testing class_copyPropertyList ()...\n");
215 Property * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
217 if (count != 0 || list != NULL)
221 printf ("Testing class_copyProtocolList ()...\n");
224 Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
226 /* Remember that we added MySecondProtocol in the test above. */
230 if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
231 && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
232 || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
233 && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
240 printf ("Testing class_createInstance ()...\n");
242 MySubClass *object = [[MySubClass alloc] init];
244 [object setVariable: object];
245 if ([object variable] != object)
249 printf ("Testing class_getClassMethod ()...\n");
251 Method method = class_getClassMethod (objc_getClass ("MySubClass"),
257 if (strcmp (sel_getName (method_getName (method)), "alloc") != 0)
260 if (class_getClassMethod (objc_getClass ("MySubClass"),
261 @selector(variable)))
265 printf ("Testing class_getClassVariable ()...\n");
267 if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
271 printf ("Testing class_getInstanceMethod ()...\n");
273 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
274 @selector(variable));
279 if (strcmp (sel_getName (method_getName (method)), "variable") != 0)
282 if (class_getInstanceMethod (objc_getClass ("MySubClass"),
287 printf ("Testing class_getInstanceSize ()...\n");
289 if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
293 printf ("Testing class_getInstanceVariable ()...\n");
295 Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
297 if (variable == NULL)
300 if (strcmp (ivar_getName (variable), "variable_ivar") != 0)
303 if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
307 printf ("Testing class_getIvarLayout ()...\n");
309 if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
313 printf ("Testing class_getMethodImplementation ()...\n");
315 MySubClass *object = [[MySubClass alloc] init];
316 IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"),
317 @selector(variable));
322 [object setVariable: object];
324 if ((*imp)(object, @selector(variable)) != object)
328 /* This function does not exist with the GNU runtime. */
329 /* printf ("Testing class_getMethodImplementation_stret ()...\n"); */
331 printf ("Testing class_getName ()...\n");
333 if (strcmp (class_getName (objc_getClass ("MyRootClass")),
338 /* TODO: Test new ABI (when available). */
339 printf ("Testing class_getProperty ()...\n");
341 if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
345 printf ("Testing class_getSuperclass ()...\n");
347 MySubClass *object = [[MySubClass alloc] init];
348 if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
352 printf ("Testing class_getVersion ()...\n");
354 if (class_getVersion (objc_getClass ("MySubClass")) != 0)
358 printf ("Testing class_getWeakIvarLayout ()...\n");
360 if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
364 printf ("Testing class_isMetaClass ()...\n");
366 MySubClass *object = [[MySubClass alloc] init];
367 if (class_isMetaClass (object_getClass (object))
368 || ! class_isMetaClass (object_getClass (object_getClass (object))))
372 printf ("Testing class_replaceMethod ()...\n");
374 Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
375 @selector (myClass));
376 Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
377 @selector (variable));
378 const char *new_types = method_getTypeEncoding (new_method);
379 IMP new_imp = method_getImplementation (new_method);
380 const char *old_types = method_getTypeEncoding (old_method);
381 IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
382 method_getImplementation (new_method),
383 method_getTypeEncoding (new_method));
384 MySubClass *o = [[MySubClass alloc] init];
388 /* Try the new method implementation. */
389 if ([o variable] != objc_getClass ("MySubClass"))
392 /* Put the original method back. */
393 class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
396 /* Test it's back to what it was. */
397 if ([o variable] != o)
401 DifferentClass *o = [[DifferentClass alloc] init];
403 /* Finally, try adding a new method. */
404 class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
407 if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
412 printf ("Testing class_respondsToSelector ()...\n");
414 if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
417 if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
421 /* This is not really implemented with the GNU runtime. */
422 /* printf ("Testing class_setIvarLayout ()...\n"); */
424 printf ("Testing class_setVersion ()...\n");
426 class_setVersion (objc_getClass ("MySubClass"), 45);
428 if (class_getVersion (objc_getClass ("MySubClass")) != 45)
431 class_setVersion (objc_getClass ("MySubClass"), 46);
433 if (class_getVersion (objc_getClass ("MySubClass")) != 46)
437 /* This is not really implemented with the GNU runtime. */
438 /* printf ("Testing class_setWeakIvarLayout ()...\n"); */