| 1 |
/* Test the Modern GNU Objective-C Runtime API. |
| 2 |
|
| 3 |
This is test 'objc', covering all functions starting with 'objc'. */ |
| 4 |
|
| 5 |
/* { dg-do run } */ |
| 6 |
/* Although this works with the NeXT runtime in a sub-set of cases, some |
| 7 |
versions of the runtime header pulls in a number of system protocols, |
| 8 |
which causes the objc_copyProtocolList test to fail (in addition to those |
| 9 |
systems that don't have the V2 APis). XFAILing the run is not useful |
| 10 |
since it will XPASS on the sub-set that works. */ |
| 11 |
/* { dg-skip-if "Incompatible" { *-*-darwin* } { "-fnext-runtime" } { "" } } */ |
| 12 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 13 |
|
| 14 |
/* To get the modern GNU Objective-C Runtime API, you include |
| 15 |
objc/runtime.h. */ |
| 16 |
#include <objc/runtime.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
@interface MyRootClass |
| 22 |
{ Class isa; } |
| 23 |
+ alloc; |
| 24 |
- init; |
| 25 |
+ initialize; |
| 26 |
@end |
| 27 |
|
| 28 |
@implementation MyRootClass |
| 29 |
+ alloc { return class_createInstance (self, 0); } |
| 30 |
- init { return self; } |
| 31 |
+ initialize { return self; } |
| 32 |
@end |
| 33 |
|
| 34 |
@protocol MyProtocol |
| 35 |
- (id) variable; |
| 36 |
@end |
| 37 |
|
| 38 |
@protocol MySecondProtocol |
| 39 |
- (id) setVariable: (id)value; |
| 40 |
@end |
| 41 |
|
| 42 |
@interface MySubClass : MyRootClass <MyProtocol> |
| 43 |
{ id variable_ivar; } |
| 44 |
- (void) setVariable: (id)value; |
| 45 |
- (id) variable; |
| 46 |
@end |
| 47 |
|
| 48 |
@implementation MySubClass |
| 49 |
- (void) setVariable: (id)value { variable_ivar = value; } |
| 50 |
- (id) variable { return variable_ivar; } |
| 51 |
@end |
| 52 |
|
| 53 |
/* Hack to calculate the log2 of a byte alignment. */ |
| 54 |
unsigned char |
| 55 |
log_2_of (unsigned int x) |
| 56 |
{ |
| 57 |
unsigned char result = 0; |
| 58 |
|
| 59 |
/* We count how many times we need to divide by 2 before we reach 1. |
| 60 |
This algorithm is good enough for the small numbers (such as 8, |
| 61 |
16 or 64) that we have to deal with. */ |
| 62 |
while (x > 1) |
| 63 |
{ |
| 64 |
x = x / 2; |
| 65 |
result++; |
| 66 |
} |
| 67 |
|
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
int main(int argc, void **args) |
| 72 |
{ |
| 73 |
/* Functions are tested in alphabetical order. */ |
| 74 |
|
| 75 |
printf ("Testing objc_allocateClassPair ()...\n"); |
| 76 |
{ |
| 77 |
Class new_root_class = objc_allocateClassPair (Nil, "MyNewRootClass", 0); |
| 78 |
Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass", 0); |
| 79 |
|
| 80 |
/* A new root class would obviously need at least an 'isa' |
| 81 |
instance variable. */ |
| 82 |
class_addIvar (new_root_class, "isa", sizeof (Class), log_2_of (__alignof__ (Class)), |
| 83 |
@encode (Class)); |
| 84 |
|
| 85 |
objc_registerClassPair (new_root_class); |
| 86 |
objc_registerClassPair (new_class); |
| 87 |
|
| 88 |
if (strcmp (class_getName (new_class), "MyNewSubClass") != 0) |
| 89 |
abort (); |
| 90 |
|
| 91 |
if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass")) |
| 92 |
abort (); |
| 93 |
|
| 94 |
if (strcmp (class_getName (new_root_class), "MyNewRootClass") != 0) |
| 95 |
abort (); |
| 96 |
|
| 97 |
if (class_getSuperclass (new_root_class) != Nil) |
| 98 |
abort (); |
| 99 |
|
| 100 |
{ |
| 101 |
MySubClass *o = [[(Class)objc_getClass ("MyNewSubClass") alloc] init]; |
| 102 |
|
| 103 |
if (object_getClass (o) != objc_getClass ("MyNewSubClass")) |
| 104 |
abort (); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
printf ("Testing objc_copyProtocolList ()...\n"); |
| 109 |
{ |
| 110 |
/* Make sure both our two protocols are known to the runtime. */ |
| 111 |
id my_protocol = @protocol (MyProtocol); |
| 112 |
id my_second_protocol = @protocol (MySecondProtocol); |
| 113 |
unsigned int count; |
| 114 |
Protocol ** list = objc_copyProtocolList (&count); |
| 115 |
|
| 116 |
if (count != 2) |
| 117 |
abort (); |
| 118 |
|
| 119 |
if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0 |
| 120 |
&& strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0) |
| 121 |
|| (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0 |
| 122 |
&& strcmp (protocol_getName (list[1]), "MyProtocol") == 0))) |
| 123 |
abort (); |
| 124 |
|
| 125 |
if (list[2] != NULL) |
| 126 |
abort (); |
| 127 |
} |
| 128 |
|
| 129 |
printf ("Testing objc_disposeClassPair ()...\n"); |
| 130 |
{ |
| 131 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:)); |
| 132 |
Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MyNewSubClass2", 0); |
| 133 |
|
| 134 |
if (new_class == Nil) |
| 135 |
abort (); |
| 136 |
|
| 137 |
/* Add a bit of everything to the class to exercise undoing all these changes. */ |
| 138 |
|
| 139 |
/* Instance variable. */ |
| 140 |
class_addIvar (new_class, "my_variable", sizeof (float), log_2_of (__alignof__ (float)), @encode (float)); |
| 141 |
|
| 142 |
/* Instance method. */ |
| 143 |
class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method), |
| 144 |
method_getTypeEncoding (method)); |
| 145 |
|
| 146 |
/* Class method. */ |
| 147 |
class_addMethod (object_getClass (new_class), @selector (setVariable:), method_getImplementation (method), |
| 148 |
method_getTypeEncoding (method)); |
| 149 |
|
| 150 |
/* Protocol. */ |
| 151 |
class_addProtocol (new_class, @protocol (MyProtocol)); |
| 152 |
|
| 153 |
objc_disposeClassPair (new_class); |
| 154 |
} |
| 155 |
|
| 156 |
/* This function currently does not exist with the GNU runtime. */ |
| 157 |
/* printf ("Testing objc_duplicateClass ()...\n"); */ |
| 158 |
|
| 159 |
/* TODO - Test it when implemented in the GNU Runtime */ |
| 160 |
/* printf ("Testing objc_getAssociatedObject ()...\n"); */ |
| 161 |
|
| 162 |
printf ("Testing objc_getClass ()...\n"); |
| 163 |
{ |
| 164 |
if (strcmp (class_getName (objc_getClass ("MySubClass")), |
| 165 |
"MySubClass") != 0) |
| 166 |
abort (); |
| 167 |
} |
| 168 |
|
| 169 |
printf ("Testing objc_getClassList ()...\n"); |
| 170 |
{ |
| 171 |
Class *list; |
| 172 |
int i, count, other_count; |
| 173 |
count = objc_getClassList (NULL, 0); |
| 174 |
|
| 175 |
/* count most likely will be 5, (MyRootClass, MySubClass, |
| 176 |
Protocol, Object, NXConstantString). */ |
| 177 |
if (count < 3) |
| 178 |
abort (); |
| 179 |
|
| 180 |
list = malloc (sizeof (Class) * count); |
| 181 |
other_count = objc_getClassList (list, count); |
| 182 |
|
| 183 |
if (other_count != count) |
| 184 |
abort (); |
| 185 |
|
| 186 |
/* Spot-check: search for class 'MyRootClass' in the list. */ |
| 187 |
for (i = 0; i < count; i++) |
| 188 |
{ |
| 189 |
if (strcmp (class_getName (list[i]), "MyRootClass") == 0) |
| 190 |
break; |
| 191 |
} |
| 192 |
if (i == count) |
| 193 |
abort (); |
| 194 |
|
| 195 |
/* Spot-check: search for class 'MySubClass' in the list. */ |
| 196 |
for (i = 0; i < count; i++) |
| 197 |
{ |
| 198 |
if (strcmp (class_getName (list[i]), "MySubClass") == 0) |
| 199 |
break; |
| 200 |
} |
| 201 |
if (i == count) |
| 202 |
abort (); |
| 203 |
|
| 204 |
/* Spot-check: search for class 'Protocol' in the list. */ |
| 205 |
for (i = 0; i < count; i++) |
| 206 |
{ |
| 207 |
if (strcmp (class_getName (list[i]), "Protocol") == 0) |
| 208 |
break; |
| 209 |
} |
| 210 |
if (i == count) |
| 211 |
abort (); |
| 212 |
} |
| 213 |
|
| 214 |
/* This function does not exist with the GNU runtime. */ |
| 215 |
/* printf ("Testing objc_getFutureClass ()...\n"); */ |
| 216 |
|
| 217 |
printf ("Testing objc_getMetaClass ()...\n"); |
| 218 |
{ |
| 219 |
if (! class_isMetaClass (objc_getMetaClass ("MyRootClass"))) |
| 220 |
abort (); |
| 221 |
} |
| 222 |
|
| 223 |
printf ("Testing objc_getProtocol ()...\n"); |
| 224 |
{ |
| 225 |
if (! protocol_isEqual (objc_getProtocol ("MyProtocol"), @protocol (MyProtocol))) |
| 226 |
abort (); |
| 227 |
} |
| 228 |
|
| 229 |
printf ("Testing objc_getRequiredClass ()...\n"); |
| 230 |
{ |
| 231 |
if (strcmp (class_getName (objc_getRequiredClass ("MyRootClass")), |
| 232 |
"MyRootClass") != 0) |
| 233 |
abort (); |
| 234 |
} |
| 235 |
|
| 236 |
printf ("Testing objc_lookUpClass ()...\n"); |
| 237 |
{ |
| 238 |
if (strcmp (class_getName (objc_lookUpClass ("MyRootClass")), |
| 239 |
"MyRootClass") != 0) |
| 240 |
abort (); |
| 241 |
} |
| 242 |
|
| 243 |
/* This function does not exist with the GNU runtime. */ |
| 244 |
/* printf ("Testing objc_setFutureClass ()...\n"); */ |
| 245 |
|
| 246 |
printf ("Testing objc_registerClassPair ()...\n"); |
| 247 |
{ |
| 248 |
Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0); |
| 249 |
|
| 250 |
class_addProtocol (new_class, @protocol (MySecondProtocol)); |
| 251 |
|
| 252 |
objc_registerClassPair (new_class); |
| 253 |
|
| 254 |
if (strcmp (class_getName (new_class), "MySubSubClass") != 0) |
| 255 |
abort (); |
| 256 |
|
| 257 |
if (class_getSuperclass (new_class) != objc_getClass ("MySubClass")) |
| 258 |
abort (); |
| 259 |
|
| 260 |
if (! class_conformsToProtocol (new_class, @protocol (MySecondProtocol))) |
| 261 |
abort (); |
| 262 |
} |
| 263 |
|
| 264 |
/* TODO - Test it when implemented in the GNU Runtime */ |
| 265 |
/* printf ("Testing objc_removeAssociatedObjects ()...\n"); */ |
| 266 |
|
| 267 |
/* TODO - Test it when implemented in the GNU Runtime */ |
| 268 |
/* printf ("Testing objc_setAssociatedObject ()...\n"); */ |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |