jit: Fix Darwin bootstrap after r15-1699.
[official-gcc.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-object.mm
blob4d99053103cc1560a022c7e53cdefdbea72ceb33
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-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" } { "" } } */
8 // { dg-additional-options "-Wno-objc-root-class" }
10 /* To get the modern GNU Objective-C Runtime API, you include
11    objc/runtime.h.  */
12 #include <objc/runtime.h>
13 #include <stdlib.h>
14 #include <iostream>
15 #include <cstring>
17 @interface MyRootClass
18 { Class isa; }
19 + alloc;
20 - init;
21 + initialize;
22 @end
24 @implementation MyRootClass
25 + alloc { return class_createInstance (self, 0); }
26 - init  { return self; }
27 + initialize { return self; }
28 @end
30 @protocol MyProtocol
31 - (id) variable;
32 @end
34 @protocol MySecondProtocol
35 - (id) setVariable: (id)value;
36 @end
38 @interface MySubClass : MyRootClass <MyProtocol>
39 { id variable_ivar; }
40 - (void) setVariable: (id)value;
41 - (id) variable;
42 @end
44 @implementation MySubClass
45 - (void) setVariable: (id)value { variable_ivar = value; }
46 - (id) variable { return variable_ivar; }
47 @end
49 @interface MySubSubClass : MySubClass
50 - (id) test;
51 @end
53 @implementation MySubSubClass
54 - (id) test { return self; }
55 @end
59 int main ()
61   /* Functions are tested in alphabetical order.  */
62   
63   std::cout << "Testing object_copy () ...\n";
64   {
65     MySubClass *object_a = [[MySubClass alloc] init];
66     MySubClass *object_b = object_copy (object_a, 0);
68     [object_b setVariable: object_a];
69     if ([object_b variable] != object_a)
70       abort ();
71   }
73   std::cout << "Testing object_dispose () ...\n";
74   {
75     MySubClass *object = [[MySubClass alloc] init];
77     object_dispose (object);
78   }
80   std::cout << "Testing object_getClass () ...\n";
81   {
82     MyRootClass *o = [[MySubClass alloc] init];
84     if (object_getClass (o) != objc_getClass ("MySubClass"))
85       abort ();
86   }
88   std::cout << "Testing object_getClassName () ...\n";
89   {
90     MyRootClass *o = [[MyRootClass alloc] init];
92     if (std::strcmp (object_getClassName (o), "MyRootClass") != 0)
93       abort ();
94   }
96   std::cout << "Testing object_getIndexedIvars () ...\n";
97   {
98     if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
99       abort ();
100   }
101   
102   std::cout << "Testing object_getInstanceVariable () ...\n";
103   {
104     MySubClass *o = [[MySubClass alloc] init];
105     id value;
107     [o setVariable: o];
109     if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
110       abort ();
112     if (value != o)
113       abort ();
114   }
116   std::cout << "Testing object_getIvar () ...\n";
117   {
118     MySubClass *o = [[MySubClass alloc] init];
119     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
121     [o setVariable: o];
123     if (object_getIvar (o, ivar) != o)
124       abort ();
125   }
127   std::cout << "Testing object_setClass () ...\n";
128   {
129     MySubClass *o = [[MySubClass alloc] init];
131     object_setClass (o, objc_getClass ("MySubSubClass"));
133     if ([(MySubSubClass *)o test] != o)
134       abort ();
135   }
137   std::cout << "Testing object_setInstanceVariable () ...\n";
138   {
139     MySubClass *o = [[MySubClass alloc] init];
140     
141     [o setVariable: nil];
143     if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
144       abort ();
146     if ([o variable] != o)
147       abort ();
148   }
150   std::cout << "Testing object_setIvar () ...\n";
151   {
152     MySubClass *o = [[MySubClass alloc] init];
153     MySubClass *value = [[MySubClass alloc] init];
154     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
155     
156     [o setVariable: o];
158     object_setIvar (o, ivar, value);
160     if ([o variable] != value)
161       abort ();
162   }  
164   return (0);