1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'object', covering all functions starting with 'object'. */
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 MySubSubClass : MySubClass
49 @implementation MySubSubClass
50 - (id) test { return self; }
55 int main(int argc, void **args)
57 /* Functions are tested in alphabetical order. */
59 printf ("Testing object_copy () ...\n");
61 MySubClass *object_a = [[MySubClass alloc] init];
62 MySubClass *object_b = object_copy (object_a, 0);
64 [object_b setVariable: object_a];
65 if ([object_b variable] != object_a)
69 printf ("Testing object_dispose () ...\n");
71 MySubClass *object = [[MySubClass alloc] init];
73 object_dispose (object);
76 printf ("Testing object_getClass () ...\n");
78 MyRootClass *o = [[MySubClass alloc] init];
80 if (object_getClass (o) != objc_getClass ("MySubClass"))
84 printf ("Testing object_getClassName () ...\n");
86 MyRootClass *o = [[MyRootClass alloc] init];
88 if (strcmp (object_getClassName (o), "MyRootClass") != 0)
92 printf ("Testing object_getIndexedIvars () ...\n");
94 if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
98 printf ("Testing object_getInstanceVariable () ...\n");
100 MySubClass *o = [[MySubClass alloc] init];
105 if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
112 printf ("Testing object_getIvar () ...\n");
114 MySubClass *o = [[MySubClass alloc] init];
115 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
119 if (object_getIvar (o, ivar) != o)
123 printf ("Testing object_setClass () ...\n");
125 MySubClass *o = [[MySubClass alloc] init];
127 object_setClass (o, objc_getClass ("MySubSubClass"));
129 if ([(MySubSubClass *)o test] != o)
133 printf ("Testing object_setInstanceVariable () ...\n");
135 MySubClass *o = [[MySubClass alloc] init];
137 [o setVariable: nil];
139 if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
142 if ([o variable] != o)
146 printf ("Testing object_setIvar () ...\n");
148 MySubClass *o = [[MySubClass alloc] init];
149 MySubClass *value = [[MySubClass alloc] init];
150 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
154 object_setIvar (o, ivar, value);
156 if ([o variable] != value)