1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'object', covering all functions starting with 'object'. */
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 MySubSubClass : MySubClass
52 @implementation MySubSubClass
53 - (id) test { return self; }
60 /* Functions are tested in alphabetical order. */
62 std::cout << "Testing object_copy () ...\n";
64 MySubClass *object_a = [[MySubClass alloc] init];
65 MySubClass *object_b = object_copy (object_a, 0);
67 [object_b setVariable: object_a];
68 if ([object_b variable] != object_a)
72 std::cout << "Testing object_dispose () ...\n";
74 MySubClass *object = [[MySubClass alloc] init];
76 object_dispose (object);
79 std::cout << "Testing object_getClass () ...\n";
81 MyRootClass *o = [[MySubClass alloc] init];
83 if (object_getClass (o) != objc_getClass ("MySubClass"))
87 std::cout << "Testing object_getClassName () ...\n";
89 MyRootClass *o = [[MyRootClass alloc] init];
91 if (std::strcmp (object_getClassName (o), "MyRootClass") != 0)
95 std::cout << "Testing object_getIndexedIvars () ...\n";
97 if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
101 std::cout << "Testing object_getInstanceVariable () ...\n";
103 MySubClass *o = [[MySubClass alloc] init];
108 if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
115 std::cout << "Testing object_getIvar () ...\n";
117 MySubClass *o = [[MySubClass alloc] init];
118 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
122 if (object_getIvar (o, ivar) != o)
126 std::cout << "Testing object_setClass () ...\n";
128 MySubClass *o = [[MySubClass alloc] init];
130 object_setClass (o, objc_getClass ("MySubSubClass"));
132 if ([(MySubSubClass *)o test] != o)
136 std::cout << "Testing object_setInstanceVariable () ...\n";
138 MySubClass *o = [[MySubClass alloc] init];
140 [o setVariable: nil];
142 if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
145 if ([o variable] != o)
149 std::cout << "Testing object_setIvar () ...\n";
151 MySubClass *o = [[MySubClass alloc] init];
152 MySubClass *value = [[MySubClass alloc] init];
153 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
157 object_setIvar (o, ivar, value);
159 if ([o variable] != value)