-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
121 lines (91 loc) · 2.95 KB
/
test.c
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
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include "s3dat.h"
#define CRASH(msg) \
do { \
printf("%s\n", msg); \
return_value = 1; \
goto end; \
} while(0)
int search_gfx() {
int return_value = 0;
DIR* dd = opendir("GFX");
if(dd == NULL) CRASH("Failed to open GFX directory in your build directory\n");
end:
if(dd != NULL) closedir(dd);
return return_value;
}
int open_landscape_file() {
int return_value = 0;
s3util_exception_t* ex = NULL;
s3dat_t* datfile = NULL;
s3dat_ref_t* bmp = NULL;
if(search_gfx() != 0) CRASH("test skipped, because search_gfx has failed\n");
datfile = s3dat_new_malloc();
s3dat_readfile_name(datfile, "GFX/Siedler3_00.f8007e01f.dat", &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t open GFX/Siedler3_00.f8007e01f.dat !\n");
bmp = s3dat_extract_landscape(datfile, 0, &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t extract first bitmap\n");
end:
if(bmp) s3dat_unref(bmp);
if(datfile != NULL) s3dat_delete(datfile);
return return_value;
}
int try_blending() {
int return_value = 0;
s3util_exception_t* ex = NULL;
s3dat_t* datfile = NULL;
s3dat_ref_t* bmp = NULL;
if(search_gfx() != 0) CRASH("test skipped, because search_gfx has failed\n");
datfile = s3dat_new_malloc();
s3dat_readfile_name(datfile, "GFX/Siedler3_00.f8007e01f.dat", &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t open GFX/Siedler3_00.f8007e01f.dat !\n");
bmp = s3dat_extract_landscape(datfile, 0x1B, &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t extract bitmap 0x1B\n");
if(s3dat_bmpdata(bmp)->red != 0 || s3dat_bmpdata(bmp)->green != 0xCE || s3dat_bmpdata(bmp)->blue != 0xEE || s3dat_bmpdata(bmp)->alpha != 0xFF) CRASH("the bitmap has a wrong color at it first pixel\n");
s3dat_unref(bmp);
bmp = NULL;
s3dat_add_landscape_blending(datfile, &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t add landscape blending\n");
bmp = s3dat_extract_landscape(datfile, 0x1B, &ex);
if(!s3util_catch_exception(&ex)) CRASH("couldn`t extract bitmap 0x1B two times\n");
if(s3dat_bmpdata(bmp)->alpha != 0) CRASH("landscape blending failed\n");
end:
if(bmp) s3dat_unref(bmp);
if(datfile != NULL) s3dat_delete(datfile);
return return_value;
}
typedef struct {
char* name;
int (*func) ();
} testcase_t;
testcase_t tests[] = {
{"search_gfx", search_gfx},
{"open_landscape", open_landscape_file},
{"try_blending", try_blending},
{NULL, NULL},
};
int main(int argc, char** argv) {
if(argc == 2) {
int caseid = 0;
if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
printf("all tests:\n");
while(tests[caseid].name != NULL) {
printf("\t%s\n", tests[caseid].name);
caseid++;
}
return 0;
}
while(tests[caseid].name != NULL) {
if(strncmp(tests[caseid].name, argv[1], strlen(tests[caseid].name)) == 0) {
return tests[caseid].func();
}
caseid++;
}
}
printf("%s <testname>\n", argv[0]);
printf("%s [-h] [--help]\n", argv[0]);
return 0;
}