1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'sel', covering all functions starting with 'sel'. */
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
11 #include <objc/runtime.h>
16 @interface MyRootClass
23 @implementation MyRootClass
24 + alloc { return class_createInstance (self, 0); }
25 - init { return self; }
26 + initialize { return self; }
33 @protocol MySecondProtocol
34 - (id) setVariable: (id)value;
37 @interface MySubClass : MyRootClass <MyProtocol>
39 - (void) setVariable: (id)value;
44 @implementation MySubClass
45 - (void) setVariable: (id)value { variable_ivar = value; }
46 - (id) variable { return variable_ivar; }
47 - (void) method { return; }
50 @interface ClassA : MyRootClass
51 - (id) conflictingSelectorMethod;
54 @implementation ClassA
55 - (id) conflictingSelectorMethod { return nil; }
58 @interface ClassB : MyRootClass
59 - (void) conflictingSelectorMethod;
62 @implementation ClassB
63 - (void) conflictingSelectorMethod { return; }
66 int main(int argc, void **args)
68 /* Functions are tested in alphabetical order. */
70 #ifdef __GNU_LIBOBJC__
71 printf ("Testing sel_copyTypedSelectorList ()...\n");
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). */
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
85 if (strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0)
88 if (strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0)
96 printf ("Testing sel_getName () ...\n");
98 if (strcmp (sel_getName (@selector (variable)), "variable") != 0)
101 if (strcmp (sel_getName (NULL), "<null selector>") != 0)
105 #ifdef __GNU_LIBOBJC__
106 printf ("Testing sel_getTypeEncoding () ...\n");
108 /* Get a selector from a real class, so it has interesting
110 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
111 @selector (variable));
113 if (strcmp (sel_getTypeEncoding (method_getName (method)),
114 method_getTypeEncoding (method)) != 0)
117 if (sel_getTypeEncoding (NULL) != NULL)
122 #ifdef __GNU_LIBOBJC__
123 printf ("Testing sel_getTypedSelector () ...\n");
125 /* First try with a selector where we know that a typed one has
127 SEL selector = sel_getTypedSelector ("variable");
129 if (selector == NULL)
132 if (sel_getTypeEncoding (selector) == NULL)
135 /* Now try a selector which was never registered. */
136 selector = sel_getTypedSelector ("not_registered");
138 if (selector != NULL)
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)
147 /* Try getting it. Nothing should be returned because it is
149 selector = sel_getTypedSelector ("registered_with_no_types");
151 if (selector != NULL)
154 /* Now try a selector with multiple, conflicting types. NULL
155 should be returned. */
156 selector = sel_getTypedSelector ("conflictingSelectorMethod");
158 if (selector != NULL)
163 printf ("Testing sel_getUid () ...\n");
165 if (strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
168 if (sel_getUid (NULL) != NULL)
172 printf ("Testing sel_isEqual () ...\n");
174 if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
178 printf ("Testing sel_registerName () ...\n");
180 if (strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
183 if (sel_registerName (NULL) != NULL)
187 #ifdef __GNU_LIBOBJC__
188 printf ("Testing set_registerTypedName () ...\n");
190 const char *types = method_getTypeEncoding (class_getInstanceMethod
191 (objc_getClass ("MySubClass"),
192 @selector (variable)));
193 SEL selector = sel_registerTypedName ("aMethod", types);
195 if (strcmp (sel_getName (selector), "aMethod") != 0)
198 if (strcmp (sel_getTypeEncoding (selector), types) != 0)
201 if (sel_registerTypedName (NULL, NULL) != NULL)
204 if (sel_registerTypedName (NULL, types) != NULL)