Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-class.mm
blobfa04fc628038647b08005c54707d90462b85e15b
1 /* Test the Modern GNU Objective-C Runtime API.
3   This is test 'class', covering all functions starting with 'class'.  */
5 /* { dg-do run } */
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
9    objc/runtime.h.  */
10 #include <objc/runtime.h>
11 #include <stdlib.h>
12 #include <iostream>
13 #include <cstring>
15 @interface MyRootClass
16 { Class isa; }
17 + alloc;
18 - init;
19 + initialize;
20 @end
22 @implementation MyRootClass
23 + alloc { return class_createInstance (self, 0); }
24 - init  { return self; }
25 + initialize { return self; }
26 @end
28 @protocol MyProtocol
29 - (id) variable;
30 @end
32 @protocol MySecondProtocol
33 - (id) setVariable: (id)value;
34 @end
36 @interface MySubClass : MyRootClass <MyProtocol>
37 { id variable_ivar; }
38 - (void) setVariable: (id)value;
39 - (id) variable;
40 @end
42 @implementation MySubClass
43 - (void) setVariable: (id)value { variable_ivar = value; }
44 - (id) variable { return variable_ivar; }
45 @end
47 @interface MyOtherSubClass : MySubClass
48 @end
50 @implementation MyOtherSubClass
51 @end
53 @interface DifferentClass : MyRootClass
54 - (id) myClass;
55 - (id) self;
56 @end
58 @implementation DifferentClass
59 - (id) myClass { return object_getClass (self); }
60 - (id) self { return self; }
61 @end
63 @interface MySubClass (MySelf)
64 - (id) mySelf;
65 @end
67 /* Hack to calculate the log2 of a byte alignment.  */
68 unsigned char
69 log_2_of (unsigned int x)
71   unsigned char result = 0;
73   /* We count how many times we need to divide by 2 before we reach 1.
74      This algorithm is good enough for the small numbers (such as 8,
75      16 or 64) that we have to deal with.  */
76   while (x > 1)
77     {
78       x = x / 2;
79       result++;
80     }
82   return result;
85 int main ()
87   /* Functions are tested in alphabetical order.  */
89   std::cout << "Testing class_addIvar ()...\n";
90   {
91     Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
93     if (new_class == Nil)
94       abort ();
95     
96     if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
97                          log_2_of (__alignof__ (id)), @encode (id)))
98       abort ();
100     if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
101                          log_2_of (__alignof__ (unsigned char)), @encode (unsigned char)))
102       abort ();
104     if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
105                          log_2_of (__alignof__ (unsigned long)), @encode (unsigned long)))
106       abort ();
108     objc_registerClassPair (new_class);    
110     {
111       MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
112       Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
113       Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
114       Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
116       if (variable2 == NULL  || variable3 == NULL  ||  variable4 == NULL)
117         abort ();
118       
119       if (std::strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
120         abort ();
122       if (std::strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
123         abort ();
125       if (std::strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
126         abort ();
128       {
129         unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
130         unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
132         object_setIvar (o, variable2, new_class);
133         *var3 = 230;
134         *var4 = 89000L;
136         if (object_getIvar (o, variable2) != new_class)
137           abort ();
139         if (*var3 != 230)
140           abort ();
142         if (*var4 != 89000L)
143           abort ();
144       }
145     }
146   }
148   std::cout << "Testing class_addMethod ()...\n";
149   {
150     Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
151     Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
152     Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
154     if (new_class == Nil)
155       abort ();
156     
157     if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
158                          log_2_of (__alignof__ (id)), @encode (id)))
159       abort ();
161     if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
162                            method_getTypeEncoding (method1)))
163       abort ();
165     if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
166                            method_getTypeEncoding (method2)))
167       abort ();
169     /* Test that if the method already exists in the class,
170        class_addMethod() returns NO.  */
171     if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
172                          method_getTypeEncoding (method2)))
173       abort ();
175     objc_registerClassPair (new_class);    
177     /* Now, MySubClass2 is basically the same as MySubClass!  We'll
178        use the variable and setVariable: methods on it.  */
179     {
180       MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
182       [o setVariable: o];
184       if ([o variable] != o)
185         abort ();
186     }
188     /* Now, try that if you take an existing class and try to add an
189        already existing method, class_addMethod returns NO.  This is
190        subtly different from before, when 'new_class' was still in
191        construction.  Now it's a real class and the libobjc internals
192        differ between the two cases.  */
193     if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
194                          method_getTypeEncoding (method2)))
195       abort ();
196   }
198   std::cout << "Testing class_addProtocol ()...\n";
199   {
200     if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
201       abort ();
202     
203     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
204       abort ();
206     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
207       abort ();
208   }
210   std::cout << "Testing class_conformsToProtocol ()...\n";
211   {
212     if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
213       abort ();
215     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
216       abort ();
218     /* Test that class_conformsToProtocol checks the class, but not
219        superclasses.  */
220     if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol)))
221       abort ();
222   }
224   std::cout << "Testing class_copyIvarList ()...\n";
225   {
226     unsigned int count;
227     Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
229     if (count != 1)
230       abort ();
232     if (std::strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
233       abort ();
234     
235     if (list[1] != NULL)
236       abort ();
237   }
239   std::cout << "Testing class_copyMethodList ()...\n";
240   {
241     unsigned int count;
242     Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
244     if (count != 2)
245       abort ();
246     
247     if (! ((std::strcmp (sel_getName (method_getName (list[0])), "variable") == 0
248             && std::strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
249            || (std::strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
250                && std::strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
251       abort ();
252     
253     if (list[2] != NULL)
254       abort ();
255   }
257   /* TODO: Test new ABI (when available).  */
258   std::cout << "Testing class_copyPropertyList ()...\n";
259   {
260     unsigned int count;
261     objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
263     if (count != 0  ||  list != NULL)
264       abort ();
265   }
267   std::cout << "Testing class_copyProtocolList ()...\n";
268   {
269     unsigned int count;
270     Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
272     /* Remember that we added MySecondProtocol in the test above.  */
273     if (count != 2)
274       abort ();
276     if (! ((std::strcmp (protocol_getName (list[0]), "MyProtocol") == 0
277             && std::strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
278            || (std::strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
279                && std::strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
280       abort ();
281     
282     if (list[2] != NULL)
283       abort ();
284   }
286   std::cout << "Testing class_createInstance ()...\n";
287   {
288     MySubClass *object = [[MySubClass alloc] init];
290     [object setVariable: object];
291     if ([object variable] != object)
292       abort ();
293   }
295   std::cout << "Testing class_getClassMethod ()...\n";
296   {
297     Method method = class_getClassMethod (objc_getClass ("MySubClass"),
298                                           @selector(alloc));
300     if (method == NULL)
301       abort ();
303     if (std::strcmp (sel_getName (method_getName (method)), "alloc") != 0)
304       abort ();
306     if (class_getClassMethod (objc_getClass ("MySubClass"), 
307                               @selector(variable)))
308       abort ();
309   }
311   std::cout << "Testing class_getClassVariable ()...\n";
312   {
313     if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
314       abort ();
315   }
317   std::cout << "Testing class_getInstanceMethod ()...\n";
318   {
319     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), 
320                                              @selector(variable));
322     if (method == NULL)
323       abort ();
325     if (std::strcmp (sel_getName (method_getName (method)), "variable") != 0)
326       abort ();
328     if (class_getInstanceMethod (objc_getClass ("MySubClass"), 
329                                  @selector(alloc)))
330       abort ();
331   }
333   std::cout << "Testing class_getInstanceSize ()...\n";
334   {
335     if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
336       abort ();
337   }
339   std::cout << "Testing class_getInstanceVariable ()...\n";
340   {
341     Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
343     if (variable == NULL)
344       abort ();
346     if (std::strcmp (ivar_getName (variable), "variable_ivar") != 0)
347       abort ();
349     if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
350       abort ();
351   }
353   std::cout << "Testing class_getIvarLayout ()...\n";
354   {
355     if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
356       abort ();
357   }
359   std::cout << "Testing class_getMethodImplementation ()...\n";
360   {
361     MySubClass *object = [[MySubClass alloc] init];
362     IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"), 
363                                              @selector(variable));
365     if (imp == NULL)
366       abort ();
368     [object setVariable: object];
370     if ((*imp)(object, @selector(variable)) != object)
371       abort ();
372   }
374   /* This function does not exist with the GNU runtime.  */
375   /* std::cout << "Testing class_getMethodImplementation_stret ()...\n"; */
377   std::cout << "Testing class_getName ()...\n";
378   {
379     if (std::strcmp (class_getName (objc_getClass ("MyRootClass")),
380                 "MyRootClass") != 0)
381       abort ();
382   }
384   /* TODO: Test new ABI (when available).  */
385   std::cout << "Testing class_getProperty ()...\n";
386   {
387     if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
388       abort ();
389   }
391   std::cout << "Testing class_getSuperclass ()...\n";
392   {
393     MySubClass *object = [[MySubClass alloc] init];
394     if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
395       abort ();
396   }
398   std::cout << "Testing class_getVersion ()...\n";
399   {
400     if (class_getVersion (objc_getClass ("MySubClass")) != 0)
401       abort ();
402   }
404    std::cout << "Testing class_getWeakIvarLayout ()...\n";
405   {
406     if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
407       abort ();
408   }
410   std::cout << "Testing class_isMetaClass ()...\n";
411   {
412     MySubClass *object = [[MySubClass alloc] init];
413     if (class_isMetaClass (object_getClass (object)) 
414         || ! class_isMetaClass (object_getClass (object_getClass (object))))
415       abort ();
416   }
418   std::cout << "Testing class_replaceMethod ()...\n";
419   {
420     Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
421                                                  @selector (myClass));
422     Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
423                                                  @selector (variable));
424     const char *new_types = method_getTypeEncoding (new_method);
425     IMP new_imp = method_getImplementation (new_method);
426     const char *old_types = method_getTypeEncoding (old_method);
427     IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
428                                        method_getImplementation (new_method),
429                                        method_getTypeEncoding (new_method));
430     MySubClass *o = [[MySubClass alloc] init];
432     [o setVariable: o];
434     /* Try the new method implementation.  */
435     if ([o variable] != objc_getClass ("MySubClass"))
436       abort ();
438     /* Put the original method back.  */
439     class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
440                          old_imp, old_types);
442     /* Test it's back to what it was.  */
443     if ([o variable] != o)
444       abort ();    
446     {
447       DifferentClass *o = [[DifferentClass alloc] init];
449       /* Finally, try adding a new method.  */
450       class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
451                            new_imp, new_types);
452       
453       if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
454         abort ();
455     }
456   }
458   std::cout << "Testing class_respondsToSelector ()...\n";
459   {
460     if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
461       abort ();
463     if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
464       abort ();
465   }
467   /* This is not really implemented with the GNU runtime.  */
468   /* std::cout << "Testing class_setIvarLayout ()...\n"; */
470   std::cout << "Testing class_setVersion ()...\n";
471   {
472     class_setVersion (objc_getClass ("MySubClass"), 45);
473     
474     if (class_getVersion (objc_getClass ("MySubClass")) != 45)
475       abort ();
477     class_setVersion (objc_getClass ("MySubClass"), 46);
479     if (class_getVersion (objc_getClass ("MySubClass")) != 46)
480       abort ();
481   }
483   /* This is not really implemented with the GNU runtime.  */
484   /* std::cout << "Testing class_setWeakIvarLayout ()...\n"; */
486   return (0);