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" } { "" } } */
8 /* To get the modern GNU Objective-C Runtime API, you include
10 #include <objc/runtime.h>
15 @interface MyRootClass
22 @implementation MyRootClass
23 + alloc { return class_createInstance (self, 0); }
24 - init { return self; }
25 + initialize { return self; }
32 @protocol MySecondProtocol
33 - (id) setVariable: (id)value;
36 @interface MySubClass : MyRootClass <MyProtocol>
38 - (void) setVariable: (id)value;
43 @implementation MySubClass
44 - (void) setVariable: (id)value { variable_ivar = value; }
45 - (id) variable { return variable_ivar; }
46 - (void) method { return; }
49 @interface ClassA : MyRootClass
50 - (id) conflictingSelectorMethod;
53 @implementation ClassA
54 - (id) conflictingSelectorMethod { return nil; }
57 @interface ClassB : MyRootClass
58 - (void) conflictingSelectorMethod;
61 @implementation ClassB
62 - (void) conflictingSelectorMethod { return; }
67 /* Functions are tested in alphabetical order. */
69 #ifdef __GNU_LIBOBJC__
70 std::cout << "Testing sel_copyTypedSelectorList ()...\n";
73 SEL * list = sel_copyTypedSelectorList ("method", &count);
75 /* There should only be two, since 'method' is referenced twice,
76 once with types and once without (in this very test). */
80 /* Check that both selectors are not-NULL, and have the correct
81 name. We use @selector() here, which wouldn't really be
82 needed, just to register a second, untyped selector with name
84 if (std::strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0)
87 if (std::strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0)
95 std::cout << "Testing sel_getName () ...\n";
97 if (std::strcmp (sel_getName (@selector (variable)), "variable") != 0)
100 if (std::strcmp (sel_getName (NULL), "<null selector>") != 0)
104 #ifdef __GNU_LIBOBJC__
105 std::cout << "Testing sel_getTypeEncoding () ...\n";
107 /* Get a selector from a real class, so it has interesting
109 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
110 @selector (variable));
112 if (std::strcmp (sel_getTypeEncoding (method_getName (method)),
113 method_getTypeEncoding (method)) != 0)
116 if (sel_getTypeEncoding (NULL) != NULL)
121 #ifdef __GNU_LIBOBJC__
122 std::cout << "Testing sel_getTypedSelector () ...\n";
124 /* First try with a selector where we know that a typed one has
126 SEL selector = sel_getTypedSelector ("variable");
128 if (selector == NULL)
131 if (sel_getTypeEncoding (selector) == NULL)
134 /* Now try a selector which was never registered. */
135 selector = sel_getTypedSelector ("not_registered");
137 if (selector != NULL)
140 /* Now try registering a selector with no types. The following
141 line is just a way to have an unused '@selector()' expression
142 without the compiler complaining. */
143 if (@selector (registered_with_no_types) == NULL)
146 /* Try getting it. Nothing should be returned because it is
148 selector = sel_getTypedSelector ("registered_with_no_types");
150 if (selector != NULL)
153 /* Now try a selector with multiple, conflicting types. NULL
154 should be returned. */
155 selector = sel_getTypedSelector ("conflictingSelectorMethod");
157 if (selector != NULL)
162 std::cout << "Testing sel_getUid () ...\n";
164 if (std::strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
167 if (sel_getUid (NULL) != NULL)
171 std::cout << "Testing sel_isEqual () ...\n";
173 if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
177 std::cout << "Testing sel_registerName () ...\n";
179 if (std::strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
182 if (sel_registerName (NULL) != NULL)
186 #ifdef __GNU_LIBOBJC__
187 std::cout << "Testing set_registerTypedName () ...\n";
189 const char *types = method_getTypeEncoding (class_getInstanceMethod
190 (objc_getClass ("MySubClass"),
191 @selector (variable)));
192 SEL selector = sel_registerTypedName ("aMethod", types);
194 if (std::strcmp (sel_getName (selector), "aMethod") != 0)
197 if (std::strcmp (sel_getTypeEncoding (selector), types) != 0)
200 if (sel_registerTypedName (NULL, NULL) != NULL)
203 if (sel_registerTypedName (NULL, types) != NULL)