| 1 |
/* Test the Modern GNU Objective-C Runtime API. |
| 2 |
|
| 3 |
This is test 'sel', covering all functions starting with 'sel'. */ |
| 4 |
|
| 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" } */ |
| 8 |
|
| 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 <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
@interface MyRootClass |
| 17 |
{ Class isa; } |
| 18 |
+ alloc; |
| 19 |
- init; |
| 20 |
+ initialize; |
| 21 |
@end |
| 22 |
|
| 23 |
@implementation MyRootClass |
| 24 |
+ alloc { return class_createInstance (self, 0); } |
| 25 |
- init { return self; } |
| 26 |
+ initialize { return self; } |
| 27 |
@end |
| 28 |
|
| 29 |
@protocol MyProtocol |
| 30 |
- (id) variable; |
| 31 |
@end |
| 32 |
|
| 33 |
@protocol MySecondProtocol |
| 34 |
- (id) setVariable: (id)value; |
| 35 |
@end |
| 36 |
|
| 37 |
@interface MySubClass : MyRootClass <MyProtocol> |
| 38 |
{ id variable_ivar; } |
| 39 |
- (void) setVariable: (id)value; |
| 40 |
- (id) variable; |
| 41 |
- (void) method; |
| 42 |
@end |
| 43 |
|
| 44 |
@implementation MySubClass |
| 45 |
- (void) setVariable: (id)value { variable_ivar = value; } |
| 46 |
- (id) variable { return variable_ivar; } |
| 47 |
- (void) method { return; } |
| 48 |
@end |
| 49 |
|
| 50 |
@interface ClassA : MyRootClass |
| 51 |
- (id) conflictingSelectorMethod; |
| 52 |
@end |
| 53 |
|
| 54 |
@implementation ClassA |
| 55 |
- (id) conflictingSelectorMethod { return nil; } |
| 56 |
@end |
| 57 |
|
| 58 |
@interface ClassB : MyRootClass |
| 59 |
- (void) conflictingSelectorMethod; |
| 60 |
@end |
| 61 |
|
| 62 |
@implementation ClassB |
| 63 |
- (void) conflictingSelectorMethod { return; } |
| 64 |
@end |
| 65 |
|
| 66 |
int main(int argc, void **args) |
| 67 |
{ |
| 68 |
/* Functions are tested in alphabetical order. */ |
| 69 |
|
| 70 |
#ifdef __GNU_LIBOBJC__ |
| 71 |
printf ("Testing sel_copyTypedSelectorList ()...\n"); |
| 72 |
{ |
| 73 |
unsigned int count; |
| 74 |
SEL * list = sel_copyTypedSelectorList ("method", &count); |
| 75 |
|
| 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 (); |
| 80 |
|
| 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 (strcmp (sel_getName (list[0]), sel_getName (@selector (method))) != 0) |
| 86 |
abort (); |
| 87 |
|
| 88 |
if (strcmp (sel_getName (list[1]), sel_getName (@selector (method))) != 0) |
| 89 |
abort (); |
| 90 |
|
| 91 |
if (list[2] != NULL) |
| 92 |
abort (); |
| 93 |
} |
| 94 |
#endif |
| 95 |
|
| 96 |
printf ("Testing sel_getName () ...\n"); |
| 97 |
{ |
| 98 |
if (strcmp (sel_getName (@selector (variable)), "variable") != 0) |
| 99 |
abort (); |
| 100 |
|
| 101 |
if (strcmp (sel_getName (NULL), "<null selector>") != 0) |
| 102 |
abort (); |
| 103 |
} |
| 104 |
|
| 105 |
#ifdef __GNU_LIBOBJC__ |
| 106 |
printf ("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 (strcmp (sel_getTypeEncoding (method_getName (method)), |
| 114 |
method_getTypeEncoding (method)) != 0) |
| 115 |
abort (); |
| 116 |
|
| 117 |
if (sel_getTypeEncoding (NULL) != NULL) |
| 118 |
abort (); |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
#ifdef __GNU_LIBOBJC__ |
| 123 |
printf ("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"); |
| 128 |
|
| 129 |
if (selector == NULL) |
| 130 |
abort (); |
| 131 |
|
| 132 |
if (sel_getTypeEncoding (selector) == NULL) |
| 133 |
abort (); |
| 134 |
|
| 135 |
/* Now try a selector which was never registered. */ |
| 136 |
selector = sel_getTypedSelector ("not_registered"); |
| 137 |
|
| 138 |
if (selector != NULL) |
| 139 |
abort (); |
| 140 |
|
| 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 (); |
| 146 |
|
| 147 |
/* Try getting it. Nothing should be returned because it is |
| 148 |
untyped. */ |
| 149 |
selector = sel_getTypedSelector ("registered_with_no_types"); |
| 150 |
|
| 151 |
if (selector != NULL) |
| 152 |
abort (); |
| 153 |
|
| 154 |
/* Now try a selector with multiple, conflicting types. NULL |
| 155 |
should be returned. */ |
| 156 |
selector = sel_getTypedSelector ("conflictingSelectorMethod"); |
| 157 |
|
| 158 |
if (selector != NULL) |
| 159 |
abort (); |
| 160 |
} |
| 161 |
#endif |
| 162 |
|
| 163 |
printf ("Testing sel_getUid () ...\n"); |
| 164 |
{ |
| 165 |
if (strcmp (sel_getName (sel_getUid ("myMethod")), "myMethod") != 0) |
| 166 |
abort (); |
| 167 |
|
| 168 |
if (sel_getUid (NULL) != NULL) |
| 169 |
abort (); |
| 170 |
} |
| 171 |
|
| 172 |
printf ("Testing sel_isEqual () ...\n"); |
| 173 |
{ |
| 174 |
if (! sel_isEqual (@selector (setVariable:), @selector (setVariable:))) |
| 175 |
abort (); |
| 176 |
} |
| 177 |
|
| 178 |
printf ("Testing sel_registerName () ...\n"); |
| 179 |
{ |
| 180 |
if (strcmp (sel_getName (sel_registerName ("myMethod")), "myMethod") != 0) |
| 181 |
abort (); |
| 182 |
|
| 183 |
if (sel_registerName (NULL) != NULL) |
| 184 |
abort (); |
| 185 |
} |
| 186 |
|
| 187 |
#ifdef __GNU_LIBOBJC__ |
| 188 |
printf ("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 (strcmp (sel_getName (selector), "aMethod") != 0) |
| 196 |
abort (); |
| 197 |
|
| 198 |
if (strcmp (sel_getTypeEncoding (selector), types) != 0) |
| 199 |
abort (); |
| 200 |
|
| 201 |
if (sel_registerTypedName (NULL, NULL) != NULL) |
| 202 |
abort (); |
| 203 |
|
| 204 |
if (sel_registerTypedName (NULL, types) != NULL) |
| 205 |
abort (); |
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |