Debian Codegraph @

gcc/testsuite/objc.dg/gnu-api-2-object.m

raw
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 "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" } */
9
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 <stdio.h>
15 #include <string.h>
16
17 @interface MyRootClass
18 { Class isa; }
19 + alloc;
20 - init;
21 + initialize;
22 @end
23
24 @implementation MyRootClass
25 + alloc { return class_createInstance (self, 0); }
26 - init { return self; }
27 + initialize { return self; }
28 @end
29
30 @protocol MyProtocol
31 - (id) variable;
32 @end
33
34 @protocol MySecondProtocol
35 - (id) setVariable: (id)value;
36 @end
37
38 @interface MySubClass : MyRootClass <MyProtocol>
39 { id variable_ivar; }
40 - (void) setVariable: (id)value;
41 - (id) variable;
42 @end
43
44 @implementation MySubClass
45 - (void) setVariable: (id)value { variable_ivar = value; }
46 - (id) variable { return variable_ivar; }
47 @end
48
49 @interface MySubSubClass : MySubClass
50 - (id) test;
51 @end
52
53 @implementation MySubSubClass
54 - (id) test { return self; }
55 @end
56
57
58
59 int main(int argc, void **args)
60 {
61 /* Functions are tested in alphabetical order. */
62
63 printf ("Testing object_copy () ...\n");
64 {
65 MySubClass *object_a = [[MySubClass alloc] init];
66 MySubClass *object_b = object_copy (object_a, 0);
67
68 [object_b setVariable: object_a];
69 if ([object_b variable] != object_a)
70 abort ();
71 }
72
73 printf ("Testing object_dispose () ...\n");
74 {
75 MySubClass *object = [[MySubClass alloc] init];
76
77 object_dispose (object);
78 }
79
80 printf ("Testing object_getClass () ...\n");
81 {
82 MyRootClass *o = [[MySubClass alloc] init];
83
84 if (object_getClass (o) != objc_getClass ("MySubClass"))
85 abort ();
86 }
87
88 printf ("Testing object_getClassName () ...\n");
89 {
90 MyRootClass *o = [[MyRootClass alloc] init];
91
92 if (strcmp (object_getClassName (o), "MyRootClass") != 0)
93 abort ();
94 }
95
96 printf ("Testing object_getIndexedIvars () ...\n");
97 {
98 if (object_getIndexedIvars ([[MyRootClass alloc] init]) == NULL)
99 abort ();
100 }
101
102 printf ("Testing object_getInstanceVariable () ...\n");
103 {
104 MySubClass *o = [[MySubClass alloc] init];
105 id value;
106
107 [o setVariable: o];
108
109 if (object_getInstanceVariable (o, "variable_ivar", (void **)&value) == NULL)
110 abort ();
111
112 if (value != o)
113 abort ();
114 }
115
116 printf ("Testing object_getIvar () ...\n");
117 {
118 MySubClass *o = [[MySubClass alloc] init];
119 Ivar ivar = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar");
120
121 [o setVariable: o];
122
123 if (object_getIvar (o, ivar) != o)
124 abort ();
125 }
126
127 printf ("Testing object_setClass () ...\n");
128 {
129 MySubClass *o = [[MySubClass alloc] init];
130
131 object_setClass (o, objc_getClass ("MySubSubClass"));
132
133 if ([(MySubSubClass *)o test] != o)
134 abort ();
135 }
136
137 printf ("Testing object_setInstanceVariable () ...\n");
138 {
139 MySubClass *o = [[MySubClass alloc] init];
140
141 [o setVariable: nil];
142
143 if (object_setInstanceVariable (o, "variable_ivar", (void *)o) == NULL)
144 abort ();
145
146 if ([o variable] != o)
147 abort ();
148 }
149
150 printf ("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];
157
158 object_setIvar (o, ivar, value);
159
160 if ([o variable] != value)
161 abort ();
162 }
163
164 return 0;
165 }