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
|
#include "data.h"
#include "test.h"
#include "pl.h"
#include "util.h"
#include "win.h"
Test::Test(const char* name_) : name(name_) {}
#define CAT(a, b) a##b
#define APPLY(a, ...) a(__VA_ARGS__)
#define TESTS struct APPLY(CAT, tests_, __COUNTER__)
#define TEST(id) }; struct id : public Test { id() : Test(#id)
#define FAIL(...) do { Sprintf(error, __VA_ARGS__); return; } while (0)
TESTS
{
TEST(StrcpyWithSmallerDestination)
{
char src[15], dst[10];
Sprintf(src, "abcdefghijklmn");
Strcpy(dst, src);
if (dst[8] != 'i')
FAIL("dst[8] is not 'i', but '%c'", dst[8]);
if (dst[9] != 0)
FAIL("dst is not NUL-terminated");
}
TEST(EpisodeDataFromWeb)
{
ElvDataA e;
DlvDataA d;
FromWeb(10, e, d);
if (wcscmp(e.title, L"Pro Soccer Player Blackmail Case") != 0)
FAIL("title is not correct");
if (wcscmp(d.date, L"March 11, 1996") != 0)
FAIL("date is not correct");
}
TEST(EpisodeDataFromProlog)
{
ElvDataA e;
DlvDataA d;
FromProlog(10, e);
FromProlog(10, d);
if (wcscmp(e.title, L"Pro Soccer Player Blackmail Case") != 0)
FAIL("title is not correct");
if (wcscmp(d.date, L"March 11, 1996") != 0)
FAIL("date is not correct");
}
TEST(IO)
{
ElvDataA e1_0, e2_0;
FromProlog(6, e1_0);
FromProlog(10, e2_0);
{
FileView fv{L"tmp.dat", sizeof(ElvDataA)*2};
Write(fv, e1_0);
Write(fv+sizeof(ElvDataA), e2_0);
}
{
FileView fv{L"tmp.dat", sizeof(ElvDataA)};
ElvDataA* e1 = Read(fv);
if (e1_0.rating != e1->rating)
FAIL("rating is different (%d/%d)", e1_0.rating, e1->rating);
if (e1_0.bWatched != e1->bWatched)
FAIL("bWatched is different");
if (e1_0.bTVOriginal != e1->bTVOriginal)
FAIL("bTVOriginal is different");
if (wcscmp(e1_0.sRating, e1->sRating) != 0)
FAIL("sRating is different");
if (wcscmp(e1_0.siEp, e1->siEp) != 0)
FAIL("siEp is different");
if (wcscmp(e1_0.title, e1->title) != 0)
FAIL("title is different");
}
{
FileView fv{L"tmp.dat", sizeof(ElvDataA)*2};
ElvDataA* e2 = Read(fv+sizeof(ElvDataA));
if (e2_0.rating != e2->rating)
FAIL("rating is different (%d/%d)", e2_0.rating, e2->rating);
if (e2_0.bWatched != e2->bWatched)
FAIL("bWatched is different");
if (e2_0.bTVOriginal != e2->bTVOriginal)
FAIL("bTVOriginal is different");
if (wcscmp(e2_0.sRating, e2->sRating) != 0)
FAIL("sRating is different");
if (wcscmp(e2_0.siEp, e2->siEp) != 0)
FAIL("siEp is different");
if (wcscmp(e2_0.title, e2->title) != 0)
FAIL("title is different");
}
//DeleteFile(L"tmp.dat");
}
TEST(MigrateElvDataFromPrologToDisk)
{
int cEp;
if (!Pl("episode_data","episode_count",&cEp))
return;
{
FileView fv{L"tmp.dat", sizeof(ElvDataA)*cEp};
unsigned char* p = fv;
for (int iEp = 1; iEp <= cEp; iEp++) {
ElvDataA e;
FromProlog(iEp, e);
Write(p, e);
p += sizeof(e);
}
}
{
FileView fv{L"tmp.dat", sizeof(ElvDataA)*cEp};
ElvDataA* ve = reinterpret_cast<ElvDataA*>(fv.view);
ElvDataA e = ve[9];
if (wcscmp(e.title, L"Pro Soccer Player Blackmail Case") != 0)
FAIL("title is not correct");
}
//DeleteFile(L"tmp.dat");
}
};
int RunTests()
{
const Test tests[] = {
StrcpyWithSmallerDestination{},
//EpisodeDataFromWeb{},
EpisodeDataFromProlog{},
IO{},
MigrateElvDataFromPrologToDisk{},
};
printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests));
int cFailed = 0;
for (const Test& t : tests)
{
const char* msg;
if (t.error[0]) {
msg = t.error;
cFailed++;
} else
msg = const_cast<char*>("SUCCESS");
printf("%s: %s\n", t.name, msg);
}
printf("%d tests failed.\n", cFailed);
return cFailed;
}
|