jens@6: // jens@6: // MYOttoman_test.m jens@6: // Ottoman jens@6: // jens@6: // Created by Jens Alfke on 9/24/09. jens@6: // Copyright 2009 Jens Alfke. All rights reserved. jens@6: // jens@6: jens@6: #import "MYOttoman.h" jens@6: #import "MYVersionDictionary.h" jens@6: jens@6: #import "Test.h" jens@6: jens@6: static NSData* dataOf (NSString *str) { jens@6: return [str dataUsingEncoding: NSUTF8StringEncoding]; jens@6: } jens@6: jens@6: /* jens@6: static NSString* stringOf (id data) { jens@6: if (!data) jens@6: return nil; jens@6: CAssert([data isKindOfClass: [NSData class]], @"stringOf expected NSData, got %@", [data class]); jens@6: NSString *str = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]; jens@6: CAssert(str!=nil, @"Bad data (couldn't decode UTF-8): %@", data); jens@6: return str; jens@6: } jens@6: */ jens@6: jens@6: static void checkFile (NSString *path) { jens@6: NSError *error; jens@6: NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath: path error: &error]; jens@6: CAssert(fileInfo, @"Couldn't get info for %@: %@", path,error); jens@6: CAssert([fileInfo fileSize] >= 512, @"Unexpected file size %lld", [fileInfo fileSize]); jens@6: NSTimeInterval age = - [[fileInfo fileModificationDate] timeIntervalSinceNow]; jens@6: CAssert(age < 60.0, @"File %@ is too old: %.1lf sec", path,age); jens@6: } jens@6: jens@6: jens@6: TestCase(CreateMYOttoman) { jens@6: MYOttoman *o = [[MYOttoman alloc] init]; jens@6: MYCurrentVersionDictionary *cur = o.currentVersion; jens@6: CAssertEq(o.lastVersion,nil); jens@6: jens@6: [cur setObject: dataOf(@"first value") forKey: dataOf(@"first key")]; jens@6: [cur setObject: dataOf(@"second value") forKey: dataOf(@"second key")]; jens@6: [cur setObject: dataOf(@"third value") forKey: dataOf(@"third key")]; jens@6: [cur removeObjectForKey: dataOf(@"third key")]; jens@6: [cur removeObjectForKey: dataOf(@"bogus")]; jens@6: jens@6: CAssertEq(cur.count, 2); jens@6: CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"first value")); jens@6: CAssertEqual([cur objectForKey: dataOf(@"second key")], dataOf(@"second value")); jens@6: CAssertEqual([cur objectForKey: dataOf(@"third key")], nil); jens@6: jens@6: NSError *error; jens@6: if (![o saveAs: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"] jens@6: overwriteAllowed: YES error: &error]) jens@6: CAssert(NO, @"saveAs: failed: %@", error); jens@6: checkFile(@"/tmp/myottomantest.ottoman"); jens@6: jens@6: MYVersionDictionary *last = o.lastVersion; jens@6: CAssert(last, @"lastVersion is nil after save"); jens@6: CAssertEq(last.generation,0); jens@6: CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"third key")], nil); jens@6: jens@6: [cur setObject: dataOf(@"fourth value") forKey: dataOf(@"fourth key")]; jens@6: [cur setObject: dataOf(@"new first value") forKey: dataOf(@"first key")]; jens@6: [cur removeObjectForKey: dataOf(@"second key")]; jens@6: CAssertEq(cur.count, 2); jens@6: CAssertEqual([cur objectForKey: dataOf(@"first key")], dataOf(@"new first value")); jens@6: CAssertEqual([cur objectForKey: dataOf(@"second key")], nil); jens@6: CAssertEqual([cur objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value")); jens@6: jens@6: if (![o save: &error]) jens@6: CAssert(NO, @"save: failed: %@", error); jens@6: jens@6: CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"first value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"second key")], dataOf(@"second value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"third key")], nil); jens@6: jens@6: last = o.lastVersion; jens@6: CAssertEq(last.generation,1); jens@6: CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"second key")], nil); jens@6: CAssertEqual([last objectForKey: dataOf(@"third key")], nil); jens@6: CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value")); jens@6: jens@6: [o close]; jens@6: [o release]; jens@6: jens@6: o = [[MYOttoman alloc] initWithURL: [NSURL fileURLWithPath: @"/tmp/myottomantest.ottoman"] jens@6: writeable: NO error: &error]; jens@6: CAssert(o, @"Failed to re-open Ottoman: %@", error); jens@6: CAssertEq(o.currentVersion, nil); jens@6: last = o.lastVersion; jens@6: CAssertEq(last.generation,1); jens@6: CAssertEqual([last objectForKey: dataOf(@"first key")], dataOf(@"new first value")); jens@6: CAssertEqual([last objectForKey: dataOf(@"second key")], nil); jens@6: CAssertEqual([last objectForKey: dataOf(@"third key")], nil); jens@6: CAssertEqual([last objectForKey: dataOf(@"fourth key")], dataOf(@"fourth value")); jens@6: jens@6: MYVersionDictionary *prev = last.previousVersion; jens@6: CAssert(prev!=nil); jens@6: CAssertEq(prev.generation,0); jens@6: CAssertEq(prev.previousVersion,nil); jens@6: CAssertEqual([prev objectForKey: dataOf(@"first key")], dataOf(@"first value")); jens@6: CAssertEqual([prev objectForKey: dataOf(@"second key")], dataOf(@"second value")); jens@6: CAssertEqual([prev objectForKey: dataOf(@"third key")], nil); jens@6: jens@6: [o close]; jens@6: [o release]; jens@6: } jens@6: jens@6: jens@6: int main(int argc, const char**argv) { jens@6: const char* testArgs[2] = {"", "Test_All"}; jens@6: RunTestCases(2,testArgs); jens@6: return 0; jens@6: }