Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / testsuite / objc.dg / gnu-api-2-object.m
blobda010c43db81e9bf34780bb5068142c6dc9c8e3a
1 /* Test the Modern GNU Objective-C Runtime API.
3   This is test 'object', covering all functions starting with 'object'.  */
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 <stdio.h>
13 #include <string.h>
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 MySubSubClass : MySubClass
48 - (id) test;
49 @end
51 @implementation MySubSubClass
52 - (id) test { return self; }
53 @end
57 int main(int argc, void **args)
59   /* Functions are tested in alphabetical order.  */
60   
61   printf ("Testing object_copy () ...\n");
62   {
63     MySubClass *object_a = [[MySubClass alloc] init];
64     MySubClass *object_b = object_copy (object_a, 0);
66     [object_b setVariable: object_a];
67     if ([object_b variable] != object_a)
68       abort ();
69   }
71   printf ("Testing object_dispose () ...\n");
72   {
73     MySubClass *object = [[MySubClass alloc] init];
75     object_dispose (object);
76   }
78   printf ("Testing object_getClass () ...\n");
79   {
80     MyRootClass *o = [[MySubClass alloc] init];
82     if (object_getClass (o) != objc_getClass ("MySubClass"))
83       abort ();
84   }
86   printf ("Testing object_getClassName () ...\n");
87   {
88     MyRootClass *o = [[MyRootClass alloc] init];
90     if (strcmp (object_getClassName (o), "MyRootClass") != 0)
91       abort ();
92   }
94   printf ("Testing object_getIndexedIvars () ...\n");
95   {
96     if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
97       abort ();
98   }
99   
100   printf ("Testing object_getInstanceVariable () ...\n");
101   {
102     MySubClass *o = [[MySubClass alloc] init];
103     id value;
105     [o setVariable: o];
107     if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
108       abort ();
110     if (value != o)
111       abort ();
112   }
114   printf ("Testing object_getIvar () ...\n");
115   {
116     MySubClass *o = [[MySubClass alloc] init];
117     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
119     [o setVariable: o];
121     if (object_getIvar (o, ivar) != o)
122       abort ();
123   }
125   printf ("Testing object_setClass () ...\n");
126   {
127     MySubClass *o = [[MySubClass alloc] init];
129     object_setClass (o, objc_getClass ("MySubSubClass"));
131     if ([(MySubSubClass *)o test] != o)
132       abort ();
133   }
135   printf ("Testing object_setInstanceVariable () ...\n");
136   {
137     MySubClass *o = [[MySubClass alloc] init];
138     
139     [o setVariable: nil];
141     if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
142       abort ();
144     if ([o variable] != o)
145       abort ();
146   }
148   printf ("Testing object_setIvar () ...\n");
149   {
150     MySubClass *o = [[MySubClass alloc] init];
151     MySubClass *value = [[MySubClass alloc] init];
152     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
153     
154     [o setVariable: o];
156     object_setIvar (o, ivar, value);
158     if ([o variable] != value)
159       abort ();
160   }  
162   return 0;