#include "data.h" #include "test.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) { ElvData e; DlvData 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) { ElvData e; DlvData 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) { ElvData e1; FromProlog(6, e1); { FileView fv{L"tmp.dat", sizeof(e1)}; Write(fv, e1); } { FileView fv{L"tmp.dat", sizeof(e1)}; ElvData* e2 = Read(fv); if (e1.rating != e2->rating) FAIL("rating is different (%d/%d)", e1.rating, e2->rating); if (e1.bWatched != e2->bWatched) FAIL("bWatched is different"); if (e1.bTVOriginal != e2->bTVOriginal) FAIL("bTVOriginal is different"); if (wcscmp(e1.sRating, e2->sRating) != 0) FAIL("sRating is different"); if (wcscmp(e1.siEp, e2->siEp) != 0) FAIL("siEp is different"); if (wcscmp(e1.title, e2->title) != 0) FAIL("title is different"); } //DeleteFile(L"tmp.dat"); } }; int RunTests() { const Test tests[] = { StrcpyWithSmallerDestination{}, //EpisodeDataFromWeb{}, EpisodeDataFromProlog{}, IO{}, }; 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("SUCCESS"); printf("%s: %s\n", t.name, msg); } printf("%d tests failed.\n", cFailed); return cFailed; }