1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'object', covering all functions starting with 'object'. */
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 MySubSubClass : MySubClass
51 @implementation MySubSubClass
52 - (id) test { return self; }
57 int main(int argc, void **args)
59 /* Functions are tested in alphabetical order. */
61 printf ("Testing object_copy () ...\n");
63 MySubClass *object_a = [[MySubClass alloc] init];
64 MySubClass *object_b = object_copy (object_a, 0);
66 [object_b setVariable: object_a];
67 if ([object_b variable] != object_a)
71 printf ("Testing object_dispose () ...\n");
73 MySubClass *object = [[MySubClass alloc] init];
75 object_dispose (object);
78 printf ("Testing object_getClass () ...\n");
80 MyRootClass *o = [[MySubClass alloc] init];
82 if (object_getClass (o) != objc_getClass ("MySubClass"))
86 printf ("Testing object_getClassName () ...\n");
88 MyRootClass *o = [[MyRootClass alloc] init];
90 if (strcmp (object_getClassName (o), "MyRootClass") != 0)
94 printf ("Testing object_getIndexedIvars () ...\n");
96 if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
100 printf ("Testing object_getInstanceVariable () ...\n");
102 MySubClass *o = [[MySubClass alloc] init];
107 if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
114 printf ("Testing object_getIvar () ...\n");
116 MySubClass *o = [[MySubClass alloc] init];
117 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
121 if (object_getIvar (o, ivar) != o)
125 printf ("Testing object_setClass () ...\n");
127 MySubClass *o = [[MySubClass alloc] init];
129 object_setClass (o, objc_getClass ("MySubSubClass"));
131 if ([(MySubSubClass *)o test] != o)
135 printf ("Testing object_setInstanceVariable () ...\n");
137 MySubClass *o = [[MySubClass alloc] init];
139 [o setVariable: nil];
141 if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
144 if ([o variable] != o)
148 printf ("Testing object_setIvar () ...\n");
150 MySubClass *o = [[MySubClass alloc] init];
151 MySubClass *value = [[MySubClass alloc] init];
152 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
156 object_setIvar (o, ivar, value);
158 if ([o variable] != value)