Rewrote test/controls.c to be about counting calls.

This commit is contained in:
Pietro Gagliardi 2019-06-18 10:48:10 -04:00
parent dcae8888d2
commit a1b0979506
1 changed files with 41 additions and 26 deletions

View File

@ -1,30 +1,45 @@
// 8 june 2019 // 8 june 2019
#include "test.h" #include "test.h"
struct testInitData { struct counts {
unsigned long *freeCount; unsigned long countInit;
unsigned long countFree;
}; };
static void checkCountsFull(testingT *t, const char *file, long line, const struct counts *got, const struct counts *want)
{
#define check(method) \
if (got->count ## method != want->count ## method) \
testingTErrorfFull(t, file, line, "wrong number of calls to " #method "():" diff("%lu"), \
got->count ## method, want->count ## method)
check(Init);
check(Free);
#undef check
}
#define checkCounts(t, got, want) checkCountsFull(t, __FILE__, __LINE__, got, want)
struct testImplData { struct testImplData {
bool initCalled; struct counts *counts;
unsigned long *freeCount;
}; };
static struct testInitData failInit; static struct counts failInit;
void *testControlFailInit = &failInit; void *testControlFailInit = &failInit;
// TODO document that impl data is zero-initialized before this is called // TODO document that impl data is zero-initialized before this is called
// TODO we'll also have to eventually deal with the fact that NULL is not required to be 0... or at least confirm that
static bool testVtableInit(uiControl *c, void *implData, void *initData) static bool testVtableInit(uiControl *c, void *implData, void *initData)
{ {
struct testImplData *d = (struct testImplData *) implData; struct testImplData *d = (struct testImplData *) implData;
struct testInitData *tid = (struct testInitData *) initData; struct counts *counts = (struct counts *) initData;
d->initCalled = true; if (initData == testControlFailInit)
if (tid == testControlFailInit)
return false; return false;
if (tid == NULL) if (initData == NULL)
return true; return true;
d->freeCount = tid->freeCount; if (d->counts == NULL)
d->counts = counts;
d->counts->countInit++;
return true; return true;
} }
@ -32,8 +47,8 @@ static void testVtableFree(uiControl *c, void *implData)
{ {
struct testImplData *d = (struct testImplData *) implData; struct testImplData *d = (struct testImplData *) implData;
if (d->freeCount != NULL) if (d->counts != NULL)
(*(d->freeCount))++; d->counts->countFree++;
} }
static const uiControlVtable vtable = { static const uiControlVtable vtable = {
@ -59,23 +74,23 @@ uint32_t testControlType2 = 0;
testingTest(ControlMethodsCalled) testingTest(ControlMethodsCalled)
{ {
uiControl *c; uiControl *c;
struct testImplData *d; struct counts counts;
struct testInitData tid; struct counts want;
unsigned long freeCount = 0;
memset(&tid, 0, sizeof (struct testInitData)); testingTLogf(t, "*** uiNewControl()");
tid.freeCount = &freeCount; memset(&counts, 0, sizeof (struct counts));
c = uiNewControl(testControlType, &tid); c = uiNewControl(testControlType, &counts);
d = (struct testImplData *) uiControlImplData(c); memset(&want, 0, sizeof (struct counts));
if (d == NULL) want.countInit = 1;
testingTErrorf(t, "uiControlImplData() returned NULL; should not have"); checkCounts(t, &counts, &want);
else if (!d->initCalled)
testingTErrorf(t, "uiNewControl() did not call Init(); should have"); testingTLogf(t, "*** uiControlFree()");
memset(&counts, 0, sizeof (struct counts));
// TODO add event handler // TODO add event handler
uiControlFree(c); uiControlFree(c);
if (freeCount != 1) memset(&want, 0, sizeof (struct counts));
testingTErrorf(t, "uiControlFree() wrong Free() call count:" diff("%lu"), want.countFree = 1;
freeCount, 1UL); checkCounts(t, &counts, &want);
} }
// TODO test freeing a parent frees the child // TODO test freeing a parent frees the child