* auto-profile.c (afdo_annotate_cfg): Use update_max_bb_count.
[official-gcc.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-object.mm
bloba2702d64707c8717bd45a153af6e63b0c40a87d9
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" } { "" } } */
9 /* To get the modern GNU Objective-C Runtime API, you include
10    objc/runtime.h.  */
11 #include <objc/runtime.h>
12 #include <stdlib.h>
13 #include <iostream>
14 #include <cstring>
16 @interface MyRootClass
17 { Class isa; }
18 + alloc;
19 - init;
20 + initialize;
21 @end
23 @implementation MyRootClass
24 + alloc { return class_createInstance (self, 0); }
25 - init  { return self; }
26 + initialize { return self; }
27 @end
29 @protocol MyProtocol
30 - (id) variable;
31 @end
33 @protocol MySecondProtocol
34 - (id) setVariable: (id)value;
35 @end
37 @interface MySubClass : MyRootClass <MyProtocol>
38 { id variable_ivar; }
39 - (void) setVariable: (id)value;
40 - (id) variable;
41 @end
43 @implementation MySubClass
44 - (void) setVariable: (id)value { variable_ivar = value; }
45 - (id) variable { return variable_ivar; }
46 @end
48 @interface MySubSubClass : MySubClass
49 - (id) test;
50 @end
52 @implementation MySubSubClass
53 - (id) test { return self; }
54 @end
58 int main ()
60   /* Functions are tested in alphabetical order.  */
61   
62   std::cout << "Testing object_copy () ...\n";
63   {
64     MySubClass *object_a = [[MySubClass alloc] init];
65     MySubClass *object_b = object_copy (object_a, 0);
67     [object_b setVariable: object_a];
68     if ([object_b variable] != object_a)
69       abort ();
70   }
72   std::cout << "Testing object_dispose () ...\n";
73   {
74     MySubClass *object = [[MySubClass alloc] init];
76     object_dispose (object);
77   }
79   std::cout << "Testing object_getClass () ...\n";
80   {
81     MyRootClass *o = [[MySubClass alloc] init];
83     if (object_getClass (o) != objc_getClass ("MySubClass"))
84       abort ();
85   }
87   std::cout << "Testing object_getClassName () ...\n";
88   {
89     MyRootClass *o = [[MyRootClass alloc] init];
91     if (std::strcmp (object_getClassName (o), "MyRootClass") != 0)
92       abort ();
93   }
95   std::cout << "Testing object_getIndexedIvars () ...\n";
96   {
97     if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
98       abort ();
99   }
100   
101   std::cout << "Testing object_getInstanceVariable () ...\n";
102   {
103     MySubClass *o = [[MySubClass alloc] init];
104     id value;
106     [o setVariable: o];
108     if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
109       abort ();
111     if (value != o)
112       abort ();
113   }
115   std::cout << "Testing object_getIvar () ...\n";
116   {
117     MySubClass *o = [[MySubClass alloc] init];
118     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
120     [o setVariable: o];
122     if (object_getIvar (o, ivar) != o)
123       abort ();
124   }
126   std::cout << "Testing object_setClass () ...\n";
127   {
128     MySubClass *o = [[MySubClass alloc] init];
130     object_setClass (o, objc_getClass ("MySubSubClass"));
132     if ([(MySubSubClass *)o test] != o)
133       abort ();
134   }
136   std::cout << "Testing object_setInstanceVariable () ...\n";
137   {
138     MySubClass *o = [[MySubClass alloc] init];
139     
140     [o setVariable: nil];
142     if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
143       abort ();
145     if ([o variable] != o)
146       abort ();
147   }
149   std::cout << "Testing object_setIvar () ...\n";
150   {
151     MySubClass *o = [[MySubClass alloc] init];
152     MySubClass *value = [[MySubClass alloc] init];
153     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
154     
155     [o setVariable: o];
157     object_setIvar (o, ivar, value);
159     if ([o variable] != value)
160       abort ();
161   }  
163   return (0);