OSDN Git Service

In gcc/objc/:
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / objc.dg / gnu-api-2-object.m
1 /* Test the Modern GNU Objective-C Runtime API.
2
3   This is test 'object', covering all functions starting with 'object'.  */
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 MySubSubClass : MySubClass
46 - (id) test;
47 @end
48
49 @implementation MySubSubClass
50 - (id) test { return self; }
51 @end
52
53
54
55 int main(int argc, void **args)
56 {
57   /* Functions are tested in alphabetical order.  */
58   
59   printf ("Testing object_copy () ...\n");
60   {
61     MySubClass *object_a = [[MySubClass alloc] init];
62     MySubClass *object_b = object_copy (object_a, 0);
63
64     [object_b setVariable: object_a];
65     if ([object_b variable] != object_a)
66       abort ();
67   }
68
69   printf ("Testing object_dispose () ...\n");
70   {
71     MySubClass *object = [[MySubClass alloc] init];
72
73     object_dispose (object);
74   }
75
76   printf ("Testing object_getClass () ...\n");
77   {
78     MyRootClass *o = [[MySubClass alloc] init];
79
80     if (object_getClass (o) != objc_getClass ("MySubClass"))
81       abort ();
82   }
83
84   printf ("Testing object_getClassName () ...\n");
85   {
86     MyRootClass *o = [[MyRootClass alloc] init];
87
88     if (strcmp (object_getClassName (o), "MyRootClass") != 0)
89       abort ();
90   }
91
92   printf ("Testing object_getIndexedIvars () ...\n");
93   {
94     if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
95       abort ();
96   }
97   
98   printf ("Testing object_getInstanceVariable () ...\n");
99   {
100     MySubClass *o = [[MySubClass alloc] init];
101     id value;
102
103     [o setVariable: o];
104
105     if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
106       abort ();
107
108     if (value != o)
109       abort ();
110   }
111
112   printf ("Testing object_getIvar () ...\n");
113   {
114     MySubClass *o = [[MySubClass alloc] init];
115     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
116
117     [o setVariable: o];
118
119     if (object_getIvar (o, ivar) != o)
120       abort ();
121   }
122
123   printf ("Testing object_setClass () ...\n");
124   {
125     MySubClass *o = [[MySubClass alloc] init];
126
127     object_setClass (o, objc_getClass ("MySubSubClass"));
128
129     if ([(MySubSubClass *)o test] != o)
130       abort ();
131   }
132
133   printf ("Testing object_setInstanceVariable () ...\n");
134   {
135     MySubClass *o = [[MySubClass alloc] init];
136     
137     [o setVariable: nil];
138
139     if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
140       abort ();
141
142     if ([o variable] != o)
143       abort ();
144   }
145
146   printf ("Testing object_setIvar () ...\n");
147   {
148     MySubClass *o = [[MySubClass alloc] init];
149     MySubClass *value = [[MySubClass alloc] init];
150     Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
151     
152     [o setVariable: o];
153
154     object_setIvar (o, ivar, value);
155
156     if ([o variable] != value)
157       abort ();
158   }  
159
160   return 0;
161 }