~jeanfrancois.roy/mpqkit/testsuite

« back to all changes in this revision

Viewing changes to core.m

  • Committer: bahamut
  • Date: 2008-03-02 16:50:32 UTC
  • Revision ID: svn-v4:08a27de9-96f8-0310-9aec-cd9f9b5b01a8:tests:240
- Refactored the deferred operations loop in a function.
- Fixed a byte order bug on Intel when creating a new archive.
- Bumped the version to 1.0b5.
- Expanded the core test suite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
@implementation core
17
17
 
18
18
- (void)setUp {
19
 
        NSFileManager *manager = [NSFileManager defaultManager];
 
19
        NSFileManager* manager = [NSFileManager defaultManager];
20
20
        
21
21
#if defined(__ppc__)
22
 
        NSString *arch = @"ppc";
 
22
        NSString* arch = @"ppc";
23
23
#elif defined(__ppc64__)
24
 
        NSString *arch = @"ppc64";
 
24
        NSString* arch = @"ppc64";
25
25
#elif defined(__i386__)
26
 
        NSString *arch = @"i386";
 
26
        NSString* arch = @"i386";
27
27
#elif defined(__x86_64__)
28
 
        NSString *arch = @"x86_64";
 
28
        NSString* arch = @"x86_64";
29
29
#endif
30
30
 
31
31
        resultsDirectoryPath = [[[manager currentDirectoryPath] stringByAppendingPathComponent:@"results"] stringByAppendingPathComponent:arch];
33
33
        if (![manager fileExistsAtPath:resultsDirectoryPath]) @throw [NSException exceptionWithName:@"MissingResultsDirectoryException" reason:@"failed to find the results directory:" userInfo:nil];
34
34
        
35
35
        compressionTestFilesDirectory = [[manager currentDirectoryPath] stringByAppendingPathComponent:@"content/compression"];
 
36
        archivesDirectory = [[manager currentDirectoryPath] stringByAppendingPathComponent:@"content/archives"];
36
37
}
37
38
 
38
39
- (void)testCreateArchivev0 {
39
 
        NSError *error;
40
 
        MPQArchive *archive = [[MPQArchive alloc] initWithFileLimit:1 error:&error];
 
40
        NSError* error;
 
41
        MPQArchive* archive = [[MPQArchive alloc] initWithFileLimit:1 error:&error];
41
42
        STAssertNotNil(archive, @"failed to create archive: %@", error);
42
43
        STAssertNil(error, @"error should be nil on success");
43
44
        
44
 
        // File limit should be MIN_HASH_TABLE_LENGTH
 
45
        // file limit should be MIN_HASH_TABLE_LENGTH
45
46
        STAssertEquals([archive maximumNumberOfFiles], (uint32_t)MIN_HASH_TABLE_LENGTH, @"[archive maximumNumberOfFiles] did not return MIN_HASH_TABLE_LENGTH");
46
47
        // archive should be modified
47
48
        STAssertTrue([archive modified], @"archive should be modified after creation");
 
49
        // path should be nil
 
50
        STAssertNil([archive path], @"new archive should have a nil path before first write");
48
51
        
49
52
        BOOL ok = [archive writeToFile:[resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev0.mpq"] atomically:NO error:&error];
50
53
        STAssertTrue(ok, @"writeToFile:atomically:error: failed: %@", error);
51
 
        if (ok) {
52
 
                STAssertNil(error, @"error should be nil on success");
53
 
                
54
 
                // archive should not be modified
55
 
                STAssertFalse([archive modified], @"archive should not be modified after writing");
 
54
        
 
55
        // only continue if we're ok
 
56
        if (!ok) {
 
57
                // path should still be nil
 
58
                STAssertNil([archive path], @"archive path should still be nil after failed first write");
 
59
                // error should not be nil
 
60
                STAssertNotNil(error, @"error should not be nil after a failure");
 
61
                return;
 
62
        }
 
63
        
 
64
        // error should be nil after success
 
65
        STAssertNil(error, @"error should be nil on success");
 
66
        // archive should not be modified
 
67
        STAssertFalse([archive modified], @"archive should not be modified after writing");
 
68
        // archive path should match the path used in write
 
69
        STAssertEqualObjects([resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev0.mpq"], [archive path], @"archive path should match write path after successful write");
 
70
        
 
71
        // close the archive
 
72
        [archive release];
 
73
        
 
74
        // try to open the archive
 
75
        archive = [[MPQArchive alloc] initWithPath:[resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev0.mpq"] error:&error];
 
76
        STAssertNotNil(archive, @"initWithPath:error: failed: %@", error);
 
77
        
 
78
        // archive version should be MPQExtendedVersion
 
79
        STAssertEquals([[[archive archiveInfo] objectForKey:MPQArchiveVersion] intValue], MPQOriginalVersion, @"archive version should be equal to MPQExtendedVersion");
 
80
        
 
81
        // close the archive
 
82
        [archive release];
 
83
}
 
84
 
 
85
- (void)testCreateArchivev1 {
 
86
        NSError* error;
 
87
        NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], MPQMaximumNumberOfFiles, [NSNumber numberWithInt:MPQExtendedVersion], MPQArchiveVersion, nil];
 
88
        MPQArchive* archive = [[MPQArchive alloc] initWithAttributes:attributes error:&error];
 
89
        STAssertNotNil(archive, @"failed to create archive: %@", error);
 
90
        STAssertNil(error, @"error should be nil on success");
 
91
        
 
92
        // file limit should be MIN_HASH_TABLE_LENGTH
 
93
        STAssertEquals([archive maximumNumberOfFiles], (uint32_t)MIN_HASH_TABLE_LENGTH, @"[archive maximumNumberOfFiles] did not return MIN_HASH_TABLE_LENGTH");
 
94
        // archive should be modified
 
95
        STAssertTrue([archive modified], @"archive should be modified after creation");
 
96
        // path should be nil
 
97
        STAssertNil([archive path], @"new archive should have a nil path before first write");
 
98
        
 
99
        BOOL ok = [archive writeToFile:[resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev1.mpq"] atomically:NO error:&error];
 
100
        STAssertTrue(ok, @"writeToFile:atomically:error: failed: %@", error);
 
101
        
 
102
        // only continue if we're ok
 
103
        if (!ok) {
 
104
                // path should still be nil
 
105
                STAssertNil([archive path], @"archive path should still be nil after failed first write");
 
106
                // error should not be nil
 
107
                STAssertNotNil(error, @"error should not be nil after a failure");
 
108
                return;
 
109
        }
 
110
        
 
111
        // error should be nil after success
 
112
        STAssertNil(error, @"error should be nil on success");
 
113
        // archive should not be modified
 
114
        STAssertFalse([archive modified], @"archive should not be modified after writing");
 
115
        // archive path should match the path used in write
 
116
        STAssertEqualObjects([resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev1.mpq"], [archive path], @"archive path should match write path after successful write");
 
117
        
 
118
        // close the archive
 
119
        [archive release];
 
120
        
 
121
        // try to open the archive
 
122
        archive = [[MPQArchive alloc] initWithPath:[resultsDirectoryPath stringByAppendingPathComponent:@"testCreateArchivev1.mpq"] error:&error];
 
123
        STAssertNotNil(archive, @"initWithPath:error: failed: %@", error);
 
124
        
 
125
        // archive version should be MPQExtendedVersion
 
126
        STAssertEquals([[[archive archiveInfo] objectForKey:MPQArchiveVersion] intValue], MPQExtendedVersion, @"archive version should be equal to MPQExtendedVersion");
 
127
        
 
128
        // close the archive
 
129
        [archive release];
 
130
}
 
131
 
 
132
- (void)_runCoreArchiveTest:(NSString*)archivePath {
 
133
        NSError* error;
 
134
 
 
135
        MPQArchive* archive = [[MPQArchive alloc] initWithPath:archivePath error:&error];
 
136
        if (!archive) {
 
137
                STAssertNotNil(error, @"error should not be nil on error");
 
138
                NSLog(@"failed to open %@: %@", archivePath, error);
 
139
                return;
 
140
        }
 
141
        STAssertNil(error, @"error should be nil on success");
 
142
        
 
143
        [archive release];
 
144
}
 
145
 
 
146
- (void)testOpenArchives {
 
147
        // all the archives in the archives folder are valid and should open without error
 
148
        NSFileManager* manager = [NSFileManager defaultManager];
 
149
        NSEnumerator* archivesEnum = [[manager directoryContentsAtPath:archivesDirectory] objectEnumerator];
 
150
        NSString* archiveName;
 
151
        BOOL isDir;
 
152
        while ((archiveName = [archivesEnum nextObject])) {
 
153
                NSString* archivePath = [archivesDirectory stringByAppendingPathComponent:archiveName];
 
154
                
 
155
                // skip over invisible files and directories
 
156
                [manager fileExistsAtPath:archivePath isDirectory:&isDir];
 
157
                if ([archiveName hasPrefix:@"."] || isDir) continue;
 
158
                
 
159
                [self _runCoreArchiveTest:archivePath];
56
160
        }
57
161
}
58
162