From bd857d24443b8c8f5d2f58047c3f8ac5f058acea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 17 Aug 2022 02:33:26 +0200 Subject: Make FileView more type-safe. --- c/test.cpp | 59 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'c/test.cpp') diff --git a/c/test.cpp b/c/test.cpp index cecc408..84e0631 100644 --- a/c/test.cpp +++ b/c/test.cpp @@ -54,40 +54,42 @@ TESTS FromProlog(6, e1_0); FromProlog(10, e2_0); { - FileView fv{L"tmp.dat", sizeof(ElvDataA)*2}; - Write(fv, e1_0); - Write(reinterpret_cast(static_cast(fv)+1), e2_0); + FileView fv{L"tmp.dat", 2}; + memcpy(fv+0, &e1_0, sizeof(e1_0)); + memcpy(fv+1, &e2_0, sizeof(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) + FileView fv{L"tmp.dat", 2}; + const ElvDataA& e1 = *fv; + if (e1.version != 'a') + FAIL("version is different"); + 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) + if (e1_0.bTVOriginal != e1.bTVOriginal) FAIL("bTVOriginal is different"); - if (wcscmp(e1_0.sRating, e1->sRating) != 0) + if (wcscmp(e1_0.sRating, e1.sRating) != 0) FAIL("sRating is different"); - if (wcscmp(e1_0.siEp, e1->siEp) != 0) + if (wcscmp(e1_0.siEp, e1.siEp) != 0) FAIL("siEp is different"); - if (wcscmp(e1_0.title, e1->title) != 0) + if (wcscmp(e1_0.title, e1.title) != 0) FAIL("title is different"); } { - FileView fv{L"tmp.dat", sizeof(ElvDataA)*2}; - ElvDataA* e2 = Read(reinterpret_cast(static_cast(fv)+1)); - if (e2_0.rating != e2->rating) - FAIL("rating is different (%d/%d)", e2_0.rating, e2->rating); - if (e2_0.bWatched != e2->bWatched) + FileView fv{L"tmp.dat", 2*sizeof(ElvDataA)}; + ElvDataA& e2 = *reinterpret_cast(&fv.At(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) + if (e2_0.bTVOriginal != e2.bTVOriginal) FAIL("bTVOriginal is different"); - if (wcscmp(e2_0.sRating, e2->sRating) != 0) + if (wcscmp(e2_0.sRating, e2.sRating) != 0) FAIL("sRating is different"); - if (wcscmp(e2_0.siEp, e2->siEp) != 0) + if (wcscmp(e2_0.siEp, e2.siEp) != 0) FAIL("siEp is different"); - if (wcscmp(e2_0.title, e2->title) != 0) + if (wcscmp(e2_0.title, e2.title) != 0) FAIL("title is different"); } //DeleteFile(L"tmp.dat"); @@ -100,20 +102,19 @@ TESTS return; { - FileView fv{L"tmp.dat", sizeof(ElvDataA)*8192}; - unsigned char* p = fv; + FileView fv{L"tmp.dat", 8192}; + ElvDataA* p = fv; for (int iEp = 1; iEp <= cEp; iEp++) { ElvDataA e; FromProlog(iEp, e); - Write(p, e); - p += sizeof(e); + memcpy(p, &e, sizeof(e)); + p++; } } { - FileView fv{L"tmp.dat", sizeof(ElvDataA)*8192}; - ElvDataA* ve = reinterpret_cast(fv.view); - ElvDataA& e = ve[9]; + FileView fv{L"tmp.dat", 8192}; + ElvDataA& e = fv.At(9); if (wcscmp(e.title, L"Pro Soccer Player Blackmail Case") != 0) FAIL("title is not correct"); } @@ -128,7 +129,7 @@ int RunTests() //EpisodeDataFromWeb{}, EpisodeDataFromProlog{}, IO{}, - MigrateElvDataFromPrologToDisk{}, + //MigrateElvDataFromPrologToDisk{}, }; printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests)); -- cgit v1.2.3