OSDN Git Service

2010-11-05 Steve Ellcey <sje@cup.hp.com>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / gnu-api-2-class.m
1 /* Test the Modern GNU Objective-C Runtime API.
2
3   This is test 'class', covering all functions starting with 'class'.  */
4
5 /* { dg-do run } */
6 /* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
7
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 <stdio.h>
13 #include <string.h>
14
15 @interface MyRootClass
16 { Class isa; }
17 + alloc;
18 - init;
19 @end
20
21 @implementation MyRootClass
22 + alloc { return class_createInstance (self, 0); }
23 - init  { return self; }
24 @end
25
26 @protocol MyProtocol
27 - (id) variable;
28 @end
29
30 @protocol MySecondProtocol
31 - (id) setVariable: (id)value;
32 @end
33
34 @interface MySubClass : MyRootClass <MyProtocol>
35 { id variable_ivar; }
36 - (void) setVariable: (id)value;
37 - (id) variable;
38 @end
39
40 @implementation MySubClass
41 - (void) setVariable: (id)value { variable_ivar = value; }
42 - (id) variable { return variable_ivar; }
43 @end
44
45 @interface DifferentClass : MyRootClass
46 - (id) myClass;
47 - (id) self;
48 @end
49
50 @implementation DifferentClass
51 - (id) myClass { return object_getClass (self); }
52 - (id) self { return self; }
53 @end
54
55 @interface MySubClass (MySelf)
56 - (id) mySelf;
57 @end
58
59 int main(int argc, void **args)
60 {
61   /* Functions are tested in alphabetical order.  */
62
63   printf ("Testing class_addIvar ()...\n");
64   {
65     Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0);
66
67     if (new_class == Nil)
68       abort ();
69     
70     if (! class_addIvar (new_class, "variable2_ivar", sizeof (id),
71                          __alignof__ (id), @encode (id)))
72       abort ();
73
74     if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char),
75                          __alignof__ (unsigned char), @encode (unsigned char)))
76       abort ();
77
78     if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long),
79                          __alignof__ (unsigned long), @encode (unsigned long)))
80       abort ();
81
82     objc_registerClassPair (new_class);    
83
84     {
85       MySubClass *o = [[objc_getClass ("MySubSubClass") alloc] init];
86       Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar");
87       Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar");
88       Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar");
89
90       if (variable2 == NULL  || variable3 == NULL  ||  variable4 == NULL)
91         abort ();
92       
93       if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0)
94         abort ();
95
96       if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0)
97         abort ();
98
99       if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0)
100         abort ();
101
102       {
103         unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3));
104         unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4));
105
106         object_setIvar (o, variable2, new_class);
107         *var3 = 230;
108         *var4 = 89000L;
109
110         if (object_getIvar (o, variable2) != new_class)
111           abort ();
112
113         if (*var3 != 230)
114           abort ();
115
116         if (*var4 != 89000L)
117           abort ();
118       }
119     }
120   }
121
122   printf ("Testing class_addMethod ()...\n");
123   {
124     Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
125     Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:));
126     Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable));
127
128     if (new_class == Nil)
129       abort ();
130     
131     if (! class_addIvar (new_class, "variable_ivar", sizeof (id),
132                          __alignof__ (id), @encode (id)))
133       abort ();
134
135     if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1),
136                            method_getTypeEncoding (method1)))
137       abort ();
138
139     if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2),
140                            method_getTypeEncoding (method2)))
141       abort ();
142
143     objc_registerClassPair (new_class);    
144
145     /* Now, MySubClass2 is basically the same as MySubClass!  We'll
146        use the variable and setVariable: methods on it.  */
147     {
148       MySubClass *o = (MySubClass *)[[objc_getClass ("MySubClass2") alloc] init];
149
150       [o setVariable: o];
151
152       if ([o variable] != o)
153         abort ();
154     }
155   }
156
157   printf ("Testing class_addProtocol ()...\n");
158   {
159     if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
160       abort ();
161     
162     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
163       abort ();
164
165     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol)))
166       abort ();
167   }
168
169   printf ("Testing class_conformsToProtocol ()...\n");
170   {
171     if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol)))
172       abort ();
173
174     if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol)))
175       abort ();
176   }
177
178   printf ("Testing class_copyIvarList ()...\n");
179   {
180     unsigned int count;
181     Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count);
182
183     if (count != 1)
184       abort ();
185
186     if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0)
187       abort ();
188     
189     if (list[1] != NULL)
190       abort ();
191   }
192
193   printf ("Testing class_copyMethodList ()...\n");
194   {
195     unsigned int count;
196     Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count);
197
198     if (count != 2)
199       abort ();
200     
201     if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
202             && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
203            || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
204                && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
205       abort ();
206     
207     if (list[2] != NULL)
208       abort ();
209   }
210
211   /* TODO: Test new ABI (when available).  */
212   printf ("Testing class_copyPropertyList ()...\n");
213   {
214     unsigned int count;
215     Property * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count);
216
217     if (count != 0  ||  list != NULL)
218       abort ();
219   }
220
221   printf ("Testing class_copyProtocolList ()...\n");
222   {
223     unsigned int count;
224     Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count);
225
226     /* Remember that we added MySecondProtocol in the test above.  */
227     if (count != 2)
228       abort ();
229
230     if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0
231             && strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0)
232            || (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0
233                && strcmp (protocol_getName (list[1]), "MyProtocol") == 0)))
234       abort ();
235     
236     if (list[2] != NULL)
237       abort ();
238   }
239
240   printf ("Testing class_createInstance ()...\n");
241   {
242     MySubClass *object = [[MySubClass alloc] init];
243
244     [object setVariable: object];
245     if ([object variable] != object)
246       abort ();
247   }
248
249   printf ("Testing class_getClassMethod ()...\n");
250   {
251     Method method = class_getClassMethod (objc_getClass ("MySubClass"),
252                                           @selector(alloc));
253
254     if (method == NULL)
255       abort ();
256
257     if (strcmp (sel_getName (method_getName (method)), "alloc") != 0)
258       abort ();
259
260     if (class_getClassMethod (objc_getClass ("MySubClass"), 
261                               @selector(variable)))
262       abort ();
263   }
264
265   printf ("Testing class_getClassVariable ()...\n");
266   {
267     if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar"))
268       abort ();
269   }
270
271   printf ("Testing class_getInstanceMethod ()...\n");
272   {
273     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), 
274                                              @selector(variable));
275
276     if (method == NULL)
277       abort ();
278
279     if (strcmp (sel_getName (method_getName (method)), "variable") != 0)
280       abort ();
281
282     if (class_getInstanceMethod (objc_getClass ("MySubClass"), 
283                                  @selector(alloc)))
284       abort ();
285   }
286
287   printf ("Testing class_getInstanceSize ()...\n");
288   {
289     if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object))
290       abort ();
291   }
292
293   printf ("Testing class_getInstanceVariable ()...\n");
294   {
295     Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
296
297     if (variable == NULL)
298       abort ();
299
300     if (strcmp (ivar_getName (variable), "variable_ivar") != 0)
301       abort ();
302
303     if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no"))
304       abort ();
305   }
306
307   printf ("Testing class_getIvarLayout ()...\n");
308   {
309     if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL)
310       abort ();
311   }
312
313   printf ("Testing class_getMethodImplementation ()...\n");
314   {
315     MySubClass *object = [[MySubClass alloc] init];
316     IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"), 
317                                              @selector(variable));
318
319     if (imp == NULL)
320       abort ();
321
322     [object setVariable: object];
323
324     if ((*imp)(object, @selector(variable)) != object)
325       abort ();
326   }
327
328   /* This function does not exist with the GNU runtime.  */
329   /* printf ("Testing class_getMethodImplementation_stret ()...\n"); */
330
331   printf ("Testing class_getName ()...\n");
332   {
333     if (strcmp (class_getName (objc_getClass ("MyRootClass")),
334                 "MyRootClass") != 0)
335       abort ();
336   }
337
338   /* TODO: Test new ABI (when available).  */
339   printf ("Testing class_getProperty ()...\n");
340   {
341     if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL)
342       abort ();
343   }
344
345   printf ("Testing class_getSuperclass ()...\n");
346   {
347     MySubClass *object = [[MySubClass alloc] init];
348     if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass"))
349       abort ();
350   }
351
352   printf ("Testing class_getVersion ()...\n");
353   {
354     if (class_getVersion (objc_getClass ("MySubClass")) != 0)
355       abort ();
356   }
357
358    printf ("Testing class_getWeakIvarLayout ()...\n");
359   {
360     if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL)
361       abort ();
362   }
363
364   printf ("Testing class_isMetaClass ()...\n");
365   {
366     MySubClass *object = [[MySubClass alloc] init];
367     if (class_isMetaClass (object_getClass (object)) 
368         || ! class_isMetaClass (object_getClass (object_getClass (object))))
369       abort ();
370   }
371
372   printf ("Testing class_replaceMethod ()...\n");
373   {
374     Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"),
375                                                  @selector (myClass));
376     Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"),
377                                                  @selector (variable));
378     const char *new_types = method_getTypeEncoding (new_method);
379     IMP new_imp = method_getImplementation (new_method);
380     const char *old_types = method_getTypeEncoding (old_method);
381     IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
382                                        method_getImplementation (new_method),
383                                        method_getTypeEncoding (new_method));
384     MySubClass *o = [[MySubClass alloc] init];
385
386     [o setVariable: o];
387
388     /* Try the new method implementation.  */
389     if ([o variable] != objc_getClass ("MySubClass"))
390       abort ();
391
392     /* Put the original method back.  */
393     class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable),
394                          old_imp, old_types);
395
396     /* Test it's back to what it was.  */
397     if ([o variable] != o)
398       abort ();    
399
400     {
401       DifferentClass *o = [[DifferentClass alloc] init];
402
403       /* Finally, try adding a new method.  */
404       class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf),
405                            new_imp, new_types);
406       
407       if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass"))
408         abort ();
409     }
410   }
411
412   printf ("Testing class_respondsToSelector ()...\n");
413   {
414     if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:)))
415       abort ();
416
417     if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:)))
418       abort ();
419   }
420
421   /* This is not really implemented with the GNU runtime.  */
422   /* printf ("Testing class_setIvarLayout ()...\n"); */
423
424   printf ("Testing class_setVersion ()...\n");
425   {
426     class_setVersion (objc_getClass ("MySubClass"), 45);
427     
428     if (class_getVersion (objc_getClass ("MySubClass")) != 45)
429       abort ();
430
431     class_setVersion (objc_getClass ("MySubClass"), 46);
432
433     if (class_getVersion (objc_getClass ("MySubClass")) != 46)
434       abort ();
435   }
436
437   /* This is not really implemented with the GNU runtime.  */
438   /* printf ("Testing class_setWeakIvarLayout ()...\n"); */
439
440   return 0;
441 }