1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'sel', covering all functions starting with 'sel'. */
6 /* { dg-skip-if "" { *-*-* } { "-fnext-runtime" } { "" } } */
8 /* To get the modern GNU Objective-C Runtime API, you include
10 #include <objc/runtime.h>
15 @interface MyRootClass
21 @implementation MyRootClass
22 + alloc { return class_createInstance (self, 0); }
23 - init { return self; }
30 @protocol MySecondProtocol
31 - (id) setVariable: (id)value;
34 @interface MySubClass : MyRootClass <MyProtocol>
36 - (void) setVariable: (id)value;
41 @implementation MySubClass
42 - (void) setVariable: (id)value { variable_ivar = value; }
43 - (id) variable { return variable_ivar; }
44 - (void) method { return; }
50 /* Functions are tested in alphabetical order. */
52 std::cout << "Testing sel_copyTypedSelectorList ()...\n";
55 SEL * list = sel_copyTypedSelectorList ("method", &count);
57 /* There should only be two, since 'method' is referenced twice,
58 once with types and once without (in this very test). */
62 /* Check that both selectors are not-NULL, and have the correct
63 name. We use @selector() here, which wouldn't really be
64 needed, just to register a second, untyped selector with name
66 if (std::strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0)
69 if (std::strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0)
76 std::cout << "Testing sel_getName () ...\n";
78 if (std::strcmp (sel_getName (@selector (variable)), "variable") != 0)
81 if (std::strcmp (sel_getName (NULL), "<null selector>") != 0)
85 std::cout << "Testing sel_getTypeEncoding () ...\n";
87 /* Get a selector from a real class, so it has interesting
89 Method method = class_getInstanceMethod (objc_getClass ("MySubClass"),
90 @selector (variable));
92 if (std::strcmp (sel_getTypeEncoding (method_getName (method)),
93 method_getTypeEncoding (method)) != 0)
96 if (sel_getTypeEncoding (NULL) != NULL)
100 std::cout << "Testing sel_getTypedSelector () ...\n";
102 /* First try with a selector where we know that a typed one has
104 SEL selector = sel_getTypedSelector ("variable");
106 if (selector == NULL)
109 if (sel_getTypeEncoding (selector) == NULL)
112 /* Now try a selector which was never registered. */
113 selector = sel_getTypedSelector ("not_registered");
115 if (selector != NULL)
118 /* Now try registering a selector with no types. The following
119 line is just a way to have an unused '@selector()' expression
120 without the compiler complaining. */
121 if (@selector (registered_with_no_types) == NULL)
124 /* Try getting it. Nothing should be returned because it is
126 selector = sel_getTypedSelector ("registered_with_no_types");
128 if (selector != NULL)
132 std::cout << "Testing sel_getUid () ...\n";
134 if (std::strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0)
138 std::cout << "Testing sel_isEqual () ...\n";
140 if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:)))
144 std::cout << "Testing sel_registerName () ...\n";
146 if (std::strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0)
150 std::cout << "Testing set_registerTypedName () ...\n";
152 const char *types = method_getTypeEncoding (class_getInstanceMethod
153 (objc_getClass ("MySubClass"),
154 @selector (variable)));
155 SEL selector = sel_registerTypedName ("aMethod", types);
157 if (std::strcmp (sel_getName (selector), "aMethod") != 0)
160 if (std::strcmp (sel_getTypeEncoding (selector), types) != 0)