Build: Set gcc_cv_as_mips_explicit_relocs if gcc_cv_as_mips_explicit_relocs_pcrel
[official-gcc.git] / gcc / testsuite / obj-c++.dg / gnu-api-2-sel.mm
blobdd8ff3eeefe2ddbbb0d219bab5ae5e233ac51798
1 /* Test the Modern GNU Objective-C Runtime API.
3   This is test 'sel', covering all functions starting with 'sel'.  */
5 /* { dg-do run } */
6 /* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
7 // { dg-additional-options "-Wno-objc-root-class" }
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 - (void) method;
42 @end
44 @implementation MySubClass
45 - (void) setVariable: (id)value { variable_ivar = value; }
46 - (id) variable { return variable_ivar; }
47 - (void) method { return; }
48 @end
50 @interface ClassA : MyRootClass
51 - (id) conflictingSelectorMethod;
52 @end
54 @implementation ClassA
55 - (id) conflictingSelectorMethod { return nil; }
56 @end
58 @interface ClassB : MyRootClass
59 - (void) conflictingSelectorMethod;
60 @end
62 @implementation ClassB
63 - (void) conflictingSelectorMethod { return; }
64 @end
66 int main ()
68   /* Functions are tested in alphabetical order.  */
70 #ifdef __GNU_LIBOBJC__
71   std::cout << "Testing sel_copyTypedSelectorList ()...\n";
72   {
73     unsigned int count;
74     SEL * list = sel_copyTypedSelectorList ("method", &count);
76     /* There should only be two, since 'method' is referenced twice,
77        once with types and once without (in this very test).  */
78     if (count != 2)
79       abort ();
81     /* Check that both selectors are not-NULL, and have the correct
82        name.  We use @selector() here, which wouldn't really be
83        needed, just to register a second, untyped selector with name
84        'method'.  */
85     if (std::strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0)
86       abort ();
88     if (std::strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0)
89       abort ();
90     
91     if (list[2] != NULL)
92       abort ();
93   }
94 #endif
96   std::cout << "Testing sel_getName () ...\n";
97   {
98     if (std::strcmp (sel_getName (@selector (variable)), "variable") != 0)
99       abort ();
101     if (std::strcmp (sel_getName (NULL), "<null selector>") != 0)
102       abort ();
103   }
105 #ifdef __GNU_LIBOBJC__
106   std::cout << "Testing sel_getTypeEncoding () ...\n";
107   {
108     /* Get a selector from a real class, so it has interesting
109        types.  */
110     Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
111                                              @selector (variable));
112     
113     if (std::strcmp (sel_getTypeEncoding (method_getName (method)),
114                 method_getTypeEncoding (method)) != 0)
115       abort ();
117     if (sel_getTypeEncoding (NULL) != NULL)
118       abort ();
119   }
120 #endif
122 #ifdef __GNU_LIBOBJC__
123   std::cout << "Testing sel_getTypedSelector () ...\n";
124   {
125     /* First try with a selector where we know that a typed one has
126        been registered.  */
127     SEL selector = sel_getTypedSelector ("variable");
129     if (selector == NULL)
130       abort ();
132     if (sel_getTypeEncoding (selector) == NULL)
133       abort ();
135     /* Now try a selector which was never registered.  */
136     selector = sel_getTypedSelector ("not_registered");
138     if (selector != NULL)
139       abort ();
141     /* Now try registering a selector with no types.  The following
142        line is just a way to have an unused '@selector()' expression
143        without the compiler complaining.  */
144     if (@selector (registered_with_no_types) == NULL)
145       abort ();
147     /* Try getting it.  Nothing should be returned because it is
148        untyped.  */
149     selector = sel_getTypedSelector ("registered_with_no_types");
151     if (selector != NULL)
152       abort ();
154     /* Now try a selector with multiple, conflicting types.  NULL
155        should be returned.  */
156     selector = sel_getTypedSelector ("conflictingSelectorMethod");
158     if (selector != NULL)
159       abort ();
160   }
161 #endif
163   std::cout << "Testing sel_getUid () ...\n";
164   {
165     if (std::strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
166       abort ();
168     if (sel_getUid (NULL) != NULL)
169       abort ();
170   }
172   std::cout << "Testing sel_isEqual () ...\n";
173   {
174     if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
175       abort ();
176   }
177   
178   std::cout << "Testing sel_registerName () ...\n";
179   {
180     if (std::strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
181       abort ();
183     if (sel_registerName (NULL) != NULL)
184       abort ();
185   }
187 #ifdef __GNU_LIBOBJC__
188   std::cout << "Testing set_registerTypedName () ...\n";
189   {
190     const char *types = method_getTypeEncoding (class_getInstanceMethod 
191                                                 (objc_getClass ("MySubClass"),
192                                                  @selector (variable)));
193     SEL selector = sel_registerTypedName ("aMethod", types);
194     
195     if (std::strcmp (sel_getName (selector), "aMethod") != 0)
196       abort ();
198     if (std::strcmp (sel_getTypeEncoding (selector), types) != 0)
199       abort ();
201     if (sel_registerTypedName (NULL, NULL) != NULL)
202       abort ();
204     if (sel_registerTypedName (NULL, types) != NULL)
205       abort ();
206   }
207 #endif
209   return (0);