1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'objc', covering all functions starting with 'objc'. */
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 /* Hack to calculate the log2 of a byte alignment. */
50 log_2_of (unsigned int x)
52 unsigned char result = 0;
54 /* We count how many times we need to divide by 2 before we reach 1.
55 This algorithm is good enough for the small numbers (such as 8,
56 16 or 64) that we have to deal with. */
66 int main(int argc, void **args)
68 /* Functions are tested in alphabetical order. */
70 printf ("Testing objc_allocateClassPair ()...\n");
72 Class new_root_class = objc_allocateClassPair (Nil, "MyNewRootClass", 0);
73 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass", 0);
75 /* A new root class would obviously need at least an 'isa'
77 class_addIvar (new_root_class, "isa", sizeof (Class), log_2_of (__alignof__ (Class)),
80 objc_registerClassPair (new_root_class);
81 objc_registerClassPair (new_class);
83 if (strcmp (class_getName (new_class), "MyNewSubClass") != 0)
86 if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass"))
89 if (strcmp (class_getName (new_root_class), "MyNewRootClass") != 0)
92 if (class_getSuperclass (new_root_class) != Nil)
96 MySubClass *o = [[(Class)objc_getClass ("MyNewSubClass") alloc] init];
98 if (object_getClass (o) != objc_getClass ("MyNewSubClass"))
103 printf ("Testing objc_copyProtocolList ()...\n");
105 /* Make sure both our two protocols are known to the runtime. */
106 id my_protocol = @protocol (MyProtocol);
107 id my_second_protocol = @protocol (MySecondProtocol);
109 Protocol ** list = objc_copyProtocolList (&count);
114 if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
115 && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
116 || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
117 && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
124 printf ("Testing objc_disposeClassPair ()...\n");
126 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
127 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass2", 0);
129 if (new_class == Nil)
132 /* Add a bit of everything to the class to exercise undoing all these changes. */
134 /* Instance variable. */
135 class_addIvar (new_class, "my_variable", sizeof (float), log_2_of (__alignof__ (float)), @encode (float));
137 /* Instance method. */
138 class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method),
139 method_getTypeEncoding (method));
142 class_addMethod (object_getClass (new_class), @selector (setVariable:), method_getImplementation (method),
143 method_getTypeEncoding (method));
146 class_addProtocol (new_class, @protocol (MyProtocol));
148 objc_disposeClassPair (new_class);
151 /* This function currently does not exist with the GNU runtime. */
152 /* printf ("Testing objc_duplicateClass ()...\n"); */
154 /* TODO - Test it when implemented in the GNU Runtime */
155 /* printf ("Testing objc_getAssociatedObject ()...\n"); */
157 printf ("Testing objc_getClass ()...\n");
159 if (strcmp (class_getName (objc_getClass ("MySubClass")),
164 printf ("Testing objc_getClassList ()...\n");
167 int i, count, other_count;
168 count = objc_getClassList (NULL, 0);
170 /* count most likely will be 5, (MyRootClass, MySubClass,
171 Protocol, Object, NXConstantString). */
175 list = malloc (sizeof (Class) * count);
176 other_count = objc_getClassList (list, count);
178 if (other_count != count)
181 /* Spot-check: search for class 'MyRootClass' in the list. */
182 for (i = 0; i < count; i++)
184 if (strcmp (class_getName (list[i]), "MyRootClass") == 0)
190 /* Spot-check: search for class 'MySubClass' in the list. */
191 for (i = 0; i < count; i++)
193 if (strcmp (class_getName (list[i]), "MySubClass") == 0)
199 /* Spot-check: search for class 'Protocol' in the list. */
200 for (i = 0; i < count; i++)
202 if (strcmp (class_getName (list[i]), "Protocol") == 0)
209 /* This function does not exist with the GNU runtime. */
210 /* printf ("Testing objc_getFutureClass ()...\n"); */
212 printf ("Testing objc_getMetaClass ()...\n");
214 if (! class_isMetaClass (objc_getMetaClass ("MyRootClass")))
218 printf ("Testing objc_getProtocol ()...\n");
220 if (! protocol_isEqual (objc_getProtocol ("MyProtocol"), @protocol (MyProtocol)))
224 printf ("Testing objc_getRequiredClass ()...\n");
226 if (strcmp (class_getName (objc_getRequiredClass ("MyRootClass")),
231 printf ("Testing objc_lookUpClass ()...\n");
233 if (strcmp (class_getName (objc_lookUpClass ("MyRootClass")),
238 /* This function does not exist with the GNU runtime. */
239 /* printf ("Testing objc_setFutureClass ()...\n"); */
241 printf ("Testing objc_registerClassPair ()...\n");
243 Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
245 class_addProtocol (new_class, @protocol (MySecondProtocol));
247 objc_registerClassPair (new_class);
249 if (strcmp (class_getName (new_class), "MySubSubClass") != 0)
252 if (class_getSuperclass (new_class) != objc_getClass ("MySubClass"))
255 if (! class_conformsToProtocol (new_class, @protocol (MySecondProtocol)))
259 /* TODO - Test it when implemented in the GNU Runtime */
260 /* printf ("Testing objc_removeAssociatedObjects ()...\n"); */
262 /* TODO - Test it when implemented in the GNU Runtime */
263 /* printf ("Testing objc_setAssociatedObject ()...\n"); */