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
|
/*
copyright 2004 Alexander Malmberg <alexander@malmberg.org>
*/
#include "Testing.h"
#include <stdio.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSAttributedString.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSByteOrder.h>
#include <Foundation/NSCharacterSet.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSDateFormatter.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include <Foundation/NSFormatter.h>
#include <Foundation/NSGeometry.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSNull.h>
#include <Foundation/NSObject.h>
#include <Foundation/NSSet.h>
#include <Foundation/NSString.h>
#include <Foundation/NSURL.h>
#include <Foundation/NSValue.h>
@interface NSObject (Equality)
- (BOOL) testEquality: (id)other;
@end
@interface NSObject (DecodingTests)
+ (NSObject*) createTestInstance;
+ (BOOL) verifyTestInstance: (NSObject *)instance
ofVersion: (int)version;
- (BOOL) testEquality;
@end
@implementation NSObject (DecodingTests)
+ (NSObject *) createTestInstance
{
if (self == [NSException class])
{
return AUTORELEASE([[NSException alloc] initWithName: @"Test"
reason: @"Testing"
userInfo: nil]);
}
else
{
return AUTORELEASE([[self alloc] init]);
}
}
+ (BOOL) verifyTestInstance: (NSObject *)instance
ofVersion: (int)version
{
id o;
if (instance == nil) return NO;
o = [self createTestInstance];
if (YES == [o respondsToSelector: @selector(testEquality:)])
return [o testEquality: instance];
if (NO == [instance testEquality]) return YES;
return [o isEqual: instance];
}
- (BOOL) testEquality
{
static IMP impNSObject = 0;
/* By default, assume that every class that overrides NSObject's
isEqual: implementation can compare archived instances.
subclasses for which this doesn't hold can simply override this
method in a category and return a constant YES/NO. */
if (!impNSObject)
{
impNSObject = [NSObject instanceMethodForSelector:@selector(isEqual:)];
}
return [self methodForSelector:@selector(isEqual:)] == impNSObject
? NO : YES;
}
@end
@implementation NSCharacterSet (DecodingTests)
+ (NSObject *) createTestInstance
{
return [self characterSetWithCharactersInString: @"qwertzuiop"];
}
@end
@implementation NSValue (DecodingTests)
+ (NSObject *) createTestInstance
{
return [self valueWithSize: NSMakeSize(1.1, 1.2)];
}
- (BOOL) testEquality: (id)other
{
if (strcmp([self objCType], @encode(NSSize)) == 0)
{
NSSize mSize = [self sizeValue];
NSSize oSize = [other sizeValue];
if (EQ(mSize.height, oSize.height) && EQ(mSize.width, oSize.width))
return YES;
return NO;
}
return [self isEqual: other];
}
@end
@implementation NSNumber (DecodingTests)
+ (NSObject *) createTestInstance
{
return [self numberWithInt: 1];
}
@end
@implementation NSData (DecodingTests)
+ (NSObject *) createTestInstance
{
NSString *source = @"We need constant data";
NSData *data = [source dataUsingEncoding: NSUnicodeStringEncoding];
if (NSHostByteOrder() == NS_BigEndian)
{
NSMutableData *m = AUTORELEASE([data mutableCopy]);
uint8_t *p = (uint8_t*)[m mutableBytes];
uint8_t *e = p + [m length];
while (p < e)
{
uint8_t tmp = p[0];
p[0] = p[1];
p[1] = tmp;
p += 2;
}
return m;
}
else
{
return data;
}
}
@end
@implementation NSDate (DecodingTests)
+ (NSObject *) createTestInstance
{
return [NSDate dateWithTimeIntervalSince1970: 4294967296.0];
}
@end
@implementation NSURL (DecodingTests)
+ (NSObject *) createTestInstance
{
return AUTORELEASE([[self alloc] initWithString: @"http://www.gnustep.org/"]);
}
@end
/*
If set, we write out new .data files for the current versions for classes
that don't have them.
*/
BOOL update;
void test(Class class)
{
NS_DURING
{
/*
In order to catch decoders that don't consume all the data that they
should, we decode/encode an array that includes the object and a string.
We verify that the string was correctly decoded, although any errors will
likely be caught by crashes in the unarchiver.
*/
NSString *sentinel = @"quux!";
int v = [class version];
NSObject *instance;
NSArray *decodedInstance;
NSData *d;
NSString *filename;
instance = [class createTestInstance];
d = [NSArchiver archivedDataWithRootObject:
[NSArray arrayWithObjects: instance, sentinel, nil]];
decodedInstance = [NSUnarchiver unarchiveObjectWithData: d];
NSCAssert([sentinel isEqual: [decodedInstance objectAtIndex: 1]],
NSInternalInconsistencyException);
PASS([class verifyTestInstance: [decodedInstance objectAtIndex: 0]
ofVersion: v], "decoding current version of class %s", POBJECT(class));
for (; v >= 0; v--)
{
int w;
for (w = 0; w < 2; w++)
{
const char *width;
if (0 == w)
{
if (YES == update && 4 != sizeof(void*))
{
continue; // Can't write a 32bit update.
}
width = "32bit";
}
else
{
if (YES == update && 8 != sizeof(void*))
{
continue; // Can't write a 64bit update.
}
width = "64bit";
}
filename = [NSString stringWithFormat: @"%@.%i.%s",
class, v, width];
d = [NSData dataWithContentsOfFile: filename];
if (!d)
{
if (v == [class version])
{
if (!update)
PASS(0, "%s has %s reference data for current version",
POBJECT(class), width)
else
[NSArchiver archiveRootObject:
[NSArray arrayWithObjects: instance, sentinel, nil]
toFile: filename];
}
continue;
}
decodedInstance = [NSUnarchiver unarchiveObjectWithData: d];
NSCAssert([sentinel isEqual: [decodedInstance objectAtIndex: 1]],
NSInternalInconsistencyException);
PASS([class verifyTestInstance: [decodedInstance objectAtIndex: 0]
ofVersion: v], "decoding %s version %i of class %s",
width, v, POBJECT(class));
}
}
}
NS_HANDLER
{
PASS(0, "decoding class %s: %s",
POBJECT(class), POBJECT(localException));
}
NS_ENDHANDLER
}
int main(int argc, char **argv)
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
update = argc == 2 && !strcmp(argv[1], "--update");
#define T(c) test([c class]);
T(NSArray)
T(NSAttributedString)
T(NSCharacterSet)
T(NSData)
T(NSMutableData)
T(NSDate)
T(NSDateFormatter)
T(NSDictionary)
T(NSException)
T(NSNotification)
T(NSNull)
T(NSSet)
T(NSString)
T(NSURL)
T(NSValue)
T(NSNumber)
[arp release]; arp = nil;
return 0;
}
|