1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#import "Testing.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSException.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSObjCRuntime.h>
#include <string.h>
#if defined(GNUSTEP)
#import <GNUstepBase/GSObjCRuntime.h>
#else
#include <objc/runtime.h>
#endif
static int c1count = 0;
static int c1initialize = 0;
static int c1load = 0;
@interface Class1 : NSObject
{
int ivar1;
Class1 *ivar1obj;
}
- (const char *) sel1;
@end
@implementation Class1
+ (void) initialize
{
if (self == [Class1 class])
c1initialize = ++c1count;
}
+ (void) load
{
c1load = ++c1count;
}
- (const char *) sel1
{
return "";
}
@end
@protocol SubProto
- (const char *) sel2;
@end
@interface SubClass1 : Class1 <SubProto>
{
int ivar2;
}
- (const char *) sel2;
@end
@implementation SubClass1
- (const char *) sel2
{
return "";
}
@end
@interface SubClass1 (Cat1)
- (BOOL) catMethod;
- (const char *) sel2;
@end
@implementation SubClass1 (Cat1)
- (BOOL) catMethod
{
return YES;
}
- (const char *) sel2
{
return "category sel2";
}
@end
int
main(int argc, char *argv[])
{
ENTER_POOL
Class cls;
Class meta;
SEL sel;
Ivar ivar;
Ivar *ivars;
unsigned int count;
Method method;
Method *methods;
Protocol **protocols;
NSUInteger s;
NSUInteger a;
const char *t0;
const char *t1;
const char *n;
unsigned u;
int i;
t0 = "1@1:@";
t1 = NSGetSizeAndAlignment(t0, &s, &a);
PASS(t1 == &t0[2], "NSGetSizeAndAlignment() steps through id");
t1 = NSGetSizeAndAlignment(t1, &s, &a);
PASS(t1 == &t0[4], "NSGetSizeAndAlignment() steps through sel");
PASS(NO == class_isMetaClass(Nil),
"class_isMetaClass() returns NO for Nil");
PASS(Nil == class_getSuperclass(Nil),
"class_getSuperclass() returns NO for Nil");
/* NB. the OSX documentation says that the function returns an empty string
* when given a Nil argument, but the actual behavior on OSX 10.6 is to
* return the string "nil"
*/
PASS_RUNS(n = class_getName(Nil), "class_getName() for Nil does not crash")
PASS(n != 0 && strcmp(n, "nil") == 0, "class_getName() for Nil is nil");
PASS(0 == class_getInstanceVariable(Nil, 0),
"class_getInstanceVariables() for Nil,0 is 0");
PASS(0 == class_getVersion(Nil),
"class_getVersion() for Nil is 0");
cls = [SubClass1 class];
PASS(c1initialize != 0, "+initialize was called");
PASS(c1load != 0, "+load was called");
PASS(c1initialize > c1load, "+load occurs before +initialize");
PASS(strcmp(class_getName(cls), "SubClass1") == 0, "class name works");
#ifdef _WIN32
testHopeful = YES; // apparently this is not supported on MinGW/clang
#endif
PASS(YES == class_respondsToSelector(cls, @selector(sel2)),
"class_respondsToSelector() works for class method");
PASS(YES == class_respondsToSelector(cls, @selector(sel1)),
"class_respondsToSelector() works for superclass method");
#ifdef _WIN32
testHopeful = NO;
#endif
PASS(NO == class_respondsToSelector(cls, @selector(rangeOfString:)),
"class_respondsToSelector() returns NO for unknown method");
PASS(NO == class_respondsToSelector(cls, 0),
"class_respondsToSelector() returns NO for nul selector");
PASS(NO == class_respondsToSelector(0, @selector(sel1)),
"class_respondsToSelector() returns NO for nul class");
meta = object_getClass(cls);
PASS(class_isMetaClass(meta), "object_getClass() retrieves meta class");
PASS(strcmp(class_getName(meta), "SubClass1") == 0, "metaclass name works");
ivar = class_getInstanceVariable(cls, 0);
PASS(ivar == 0, "class_getInstanceVariable() returns 0 for null name");
ivar = class_getInstanceVariable(cls, "bad name");
PASS(ivar == 0, "class_getInstanceVariable() returns 0 for non-existent");
ivar = class_getInstanceVariable(0, "ivar2");
PASS(ivar == 0, "class_getInstanceVariable() returns 0 for Nil class");
ivar = class_getInstanceVariable(cls, "ivar2");
PASS(ivar != 0, "class_getInstanceVariable() works");
ivar = class_getInstanceVariable(cls, "ivar1");
PASS(ivar != 0, "class_getInstanceVariable() works for superclass ivar");
ivar = class_getInstanceVariable(cls, "ivar1obj");
PASS(ivar != 0, "class_getInstanceVariable() works for superclass obj ivar");
i = objc_getClassList(NULL, 0);
PASS(i > 2, "class list contains a reasonable number of classes");
if (i > 2)
{
int classCount = i;
Class buf[classCount];
BOOL foundClass = NO;
BOOL foundSubClass = NO;
i = objc_getClassList(buf, classCount);
PASS(i == classCount, "retrieved all classes")
for (i = 0; i < classCount; i++)
{
n = class_getName(buf[i]);
if (n)
{
if (strcmp(n, "Class1") == 0)
{
foundClass = YES;
}
else if (strcmp(n, "SubClass1") == 0)
{
foundSubClass = YES;
}
}
}
PASS(foundClass && foundSubClass, "found classes in list")
}
u = 0;
protocols = objc_copyProtocolList(&u);
PASS(protocols && u, "we copied some protocols")
if (protocols)
{
BOOL found = NO;
for (i = 0; i < u; i++)
{
n = protocol_getName(protocols[i]);
if (strcmp(n, "SubProto") == 0)
{
found = YES;
}
}
free(protocols);
PASS(found, "we found our protocol in list")
}
methods = class_copyMethodList(cls, &count);
PASS(count == 3, "SubClass1 has three methods");
PASS(methods[count] == 0, "method list is terminated");
method = methods[2];
sel = method_getName(method);
PASS(sel_isEqual(sel, sel_getUid("sel2")),
"last method is sel2");
PASS(method_getImplementation(method) != [cls instanceMethodForSelector: sel],
"method 2 is the original, overridden by the category");
method = methods[0];
sel = method_getName(method);
PASS(sel_isEqual(sel, sel_getUid("catMethod"))
|| sel_isEqual(sel, sel_getUid("sel2")),
"method 0 has expected name");
if (sel_isEqual(sel, sel_getUid("catMethod")))
{
method = methods[1];
sel = method_getName(method);
PASS(sel_isEqual(sel, sel_getUid("sel2")),
"method 1 has expected name");
PASS(method_getImplementation(method)
== [cls instanceMethodForSelector: sel],
"method 1 is the category method overriding original");
}
else
{
PASS(method_getImplementation(method)
== [cls instanceMethodForSelector: sel],
"method 0 is the category method overriding original");
method = methods[1];
sel = method_getName(method);
PASS(sel_isEqual(sel, sel_getUid("catMethod")),
"method 1 has expected name");
}
free(methods);
ivars = class_copyIvarList(cls, &count);
PASS(count == 1, "SubClass1 has one ivar");
PASS(ivars[count] == 0, "ivar list is terminated");
PASS(strcmp(ivar_getName(ivars[0]), "ivar2") == 0,
"ivar has correct name");
PASS(strcmp(ivar_getTypeEncoding(ivars[0]), @encode(int)) == 0,
"ivar has correct type");
free(ivars);
protocols = class_copyProtocolList(cls, &count);
PASS(count == 1, "SubClass1 has one protocol");
PASS(protocols[count] == 0, "protocol list is terminated");
PASS(strcmp(protocol_getName(protocols[0]), "SubProto") == 0,
"protocol has correct name");
free(protocols);
cls = objc_allocateClassPair([NSString class], "runtime generated", 0);
PASS(cls != Nil, "can allocate a class pair");
PASS(class_addIvar(cls, "iv1", 1, 6, "c") == YES,
"able to add iVar 'iv1'");
PASS(class_addIvar(cls, "iv2", 1, 5, "c") == YES,
"able to add iVar 'iv2'");
PASS(class_addIvar(cls, "iv3", 1, 4, "c") == YES,
"able to add iVar 'iv3'");
PASS(class_addIvar(cls, "iv4", 1, 3, "c") == YES,
"able to add iVar 'iv4'");
objc_registerClassPair(cls);
ivar = class_getInstanceVariable(cls, "iv1");
PASS(ivar != 0, "iv1 exists");
PASS(ivar_getOffset(ivar) == 64, "iv1 offset is 64");
ivar = class_getInstanceVariable(cls, "iv2");
PASS(ivar != 0, "iv2 exists");
PASS(ivar_getOffset(ivar) == 96, "iv2 offset is 96");
ivar = class_getInstanceVariable(cls, "iv3");
PASS(ivar != 0, "iv3 exists");
PASS(ivar_getOffset(ivar) == 112, "iv3 offset is 112");
ivar = class_getInstanceVariable(cls, "iv4");
PASS(ivar != 0, "iv4 exists");
PASS(ivar_getOffset(ivar) == 120, "iv4 offset is 120");
/* NSObjCRuntime function tests.
*/
sel = NSSelectorFromString(nil);
PASS(sel == 0,
"NSSelectorFromString() returns 0 for nil string");
PASS(NSStringFromSelector(0) == nil,
"NSStringFromSelector() returns nil for null selector");
sel = NSSelectorFromString(@"xxxyyy_odd_name_xxxyyy");
PASS(sel != 0,
"NSSelectorFromString() creates for non-existent selector");
PASS([NSStringFromSelector(sel) isEqual: @"xxxyyy_odd_name_xxxyyy"],
"NSStringFromSelector() works for existing selector");
LEAVE_POOL
START_SET("weakref")
Class c = [NSObject class];
id obj;
id got;
id ref;
int rc;
ref = nil;
objc_storeWeak(&ref, nil);
PASS(ref == nil, "nil is stored unchanged")
objc_storeWeak(&ref, @"hello");
PASS(ref == @"hello", "literal string is stored unchanged")
objc_storeWeak(&ref, (id)c);
PASS(ref == (id)c, "a class is stored unchanged")
obj = [NSObject new];
objc_storeWeak(&ref, obj);
PASS(ref != obj, "object is stored as weak reference")
rc = [obj retainCount];
got = objc_loadWeakRetained(&ref);
PASS(got == obj && [obj retainCount] == rc + 1,
"objc_loadWeakRetained() returns original retained")
RELEASE(got);
RELEASE(obj);
got = objc_loadWeakRetained(&ref);
PASS(got == nil, "load of deallocated object returns nil")
END_SET("weakref")
return 0;
}
|