[PATCH] RISC-V: Add missing insn types for XiangShan Nanhu scheduler model
[official-gcc.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-method.mm
blob07c3a22ec32fc5adba1ebc56de8db15658035b44
1 /* Test the Modern GNU Objective-C Runtime API.
3   This is test 'method', covering all functions starting with 'method'.  */
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 - (id) constant;
43 @end
45 @implementation MySubClass
46 - (void) setVariable: (id)value { variable_ivar = value; }
47 - (id) variable { return variable_ivar; }
48 - (id) constant { return nil; }
49 @end
52 int main ()
54   /* Functions are tested in alphabetical order.  */
56   std::cout <<"Testing method_copyArgumentType () ...\n";
57   {
58     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
59                                              @selector (setVariable:));
60     char *type = method_copyArgumentType (method, 2);
62     if (type == NULL  ||  type[0] != '@')
63       abort ();
64   }
66   std::cout << "Testing method_copyReturnType () ...\n";
67   {
68     Method method = class_getClassMethod (objc_getClass ("MyRootClass"),
69                                           @selector (alloc));
70     char *type = method_copyReturnType (method);
72     /* Check that it returns an object.  */
73     if (type == NULL  ||  type[0] != '@')
74       abort ();
75   }
77   std::cout << "Testing method_exchangeImplementations () ...\n";
78   {
79     Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"),
80                                                @selector (variable));
81     Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"),
82                                                @selector (constant));
83     MySubClass *object = [[MySubClass alloc] init];
85     /* Check that things work as expected before the swap.  */
86     [object setVariable: object];
88     if ([object variable] != object  ||  [object constant] != nil)
89       abort ();
91     /* Swap the methods.  */
92     method_exchangeImplementations (method_a, method_b);
94     /* Check that behavior has changed.  */
95     if ([object variable] != nil  ||  [object constant] != object)
96       abort ();
98     /* Swap the methods again.  */
99     method_exchangeImplementations (method_a, method_b);
100     
101     /* Check that behavior is back to normal.  */
102     if ([object variable] != object  ||  [object constant] != nil)
103       abort ();
104   }
106   std::cout << "Testing method_getArgumentType () ...\n";
107   {
108     Method method = class_getInstanceMethod (objc_getClass ("MyRootClass"),
109                                              @selector (init));
110     char type[16];
111     
112     method_getArgumentType (method, 1, type, 16);
114     /* Check the second argument (_cmd), which should be a SEL.  */
115     if (type[0] != ':')
116       abort ();
117   }
119   std::cout << "Testing method_getDescription () ...\n";
120   {
121     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
122                                              @selector (variable));
123     struct objc_method_description *description = method_getDescription (method);
125     if (std::strcmp (sel_getName (description->name), "variable") != 0)
126       abort ();
128     if (method_getDescription (NULL) != NULL)
129       abort ();
130   }
132   std::cout << "Testing method_getImplementation () ...\n";
133   {
134     typedef void (*set_variable_function) (id receiver, SEL _cmd, id variable);
135     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
136                                              @selector (setVariable:));
137     set_variable_function imp;
138     MySubClass *object = [[MySubClass alloc] init];
140     imp = (set_variable_function)(method_getImplementation (method));
141     
142     (*imp)(object, @selector (setVariable:), object);
144     if ([object variable] != object)
145       abort ();
146   }
148   std::cout << "Testing method_getName () ...\n";
149   {
150     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
151                                              @selector (setVariable:));
152     if (std::strcmp (sel_getName (method_getName (method)), "setVariable:") != 0)
153       abort ();
154   }
156   std::cout << "Testing method_getNumberOfArguments () ...\n";
157   {
158     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
159                                              @selector (setVariable:));
160     if (method_getNumberOfArguments (method) != 3)
161       abort ();
163     method = class_getInstanceMethod (objc_getClass ("MySubClass"),
164                                       @selector (variable));
165     if (method_getNumberOfArguments (method) != 2)
166       abort ();
167   }
169   std::cout << "Testing method_getTypeEncoding () ...\n";
170   {
171     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
172                                              @selector (setVariable:));
173     const char *types = method_getTypeEncoding (method);
175     /* Check that method type string starts with 'v' (void)  */
176     if (types == NULL || types[0] != 'v')
177       abort ();    
178   }
180   std::cout << "Testing method_getReturnType () ...\n";
181   {
182     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
183                                              @selector (setVariable:));
184     char type[16];
185     
186     method_getReturnType (method, type, 16);
188     if (type[0] != 'v')
189       abort ();
191     method_getReturnType (NULL, type, 16);
193     if (type[0] != 0)
194       abort ();
195   }
197   std::cout << "Testing method_setImplementation () ...\n";
198   {
199     Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"),
200                                                @selector (variable));
201     Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"),
202                                                @selector (constant));
203     IMP original_imp_a = method_getImplementation (method_a);
204     IMP original_imp_b = method_getImplementation (method_b);
205     MySubClass *object = [[MySubClass alloc] init];
207     /* Check that things work as expected before the swap.  */
208     [object setVariable: object];
209     
210     if ([object variable] != object  ||  [object constant] != nil)
211       abort ();
212     
213     /* Have 'variable' use the same implementation as 'constant'.  */
214     if (method_setImplementation (method_a, original_imp_b) != original_imp_a)
215       abort ();
217     /* Check that behavior has changed.  */
218     if ([object variable] != nil  ||  [object constant] != nil)
219       abort ();
221     /* Put the original method back.  */
222     if (method_setImplementation (method_a, original_imp_a) != original_imp_b)
223       abort ();
224     
225     /* Check that behavior is back to normal.  */
226     if ([object variable] != object  ||  [object constant] != nil)
227       abort ();
228   }
230   return (0);