diff --git a/test/controls.c b/test/controls.c index 4dbda774..b94f07c4 100644 --- a/test/controls.c +++ b/test/controls.c @@ -17,72 +17,8 @@ static void testVtableFree(uiControl *c, void *implData) // do nothing } -uiControlVtable *allocVtableFull(testingT *t, const char *file, long line) -{ - uiControlVtable *v; - - v = (uiControlVtable *) malloc(sizeof (uiControlVtable)); - if (v == NULL) - testingTFatalfFull(t, file, line, "memory exhausted allocating uiControlVtable"); - memset(v, 0, sizeof (uiControlVtable)); - v->Size = sizeof (uiControlVtable); - v->Init = testVtableInit; - v->Free = testVtableFree; - return v; -} - -struct checkControlErrorsParams { - const char *namePlaceholder; - uiControlVtable *vtablePlaceholder; - uiControlOSVtable *osVtablePlaceholder; - size_t implDataSizePlaceholder; - uiControlVtable *vtableBadSize; +const uiControlVtable dummyVtable = { + Size: sizeof (uiControlVtable), + Init: testVtableInit, + Free: testVtableFree, }; - -// TODO clean up these macros -#define checkCat(a, b) a ## b -#define checkErrorCaseFull(line, call, msgWant) \ - static void checkCat(doCheck, line)(void *data) \ - { \ - struct checkControlErrorsParams *p = (struct checkControlErrorsParams *) data; \ - (void) p; /* in the event call does not use this */ \ - call; \ - } -#define checkErrorCase(call, msgWant) checkErrorCaseFull(__LINE__, call, msgWant) -#include "controls_errors.h" -#undef checkErrorCaseFull -#undef checkErrorCase - -static const struct { - const char *name; - void (*f)(void *data); - const char *msgWant; -} controlErrorCases[] = { -#define checkErrorCaseFull(line, callstr, msgWant) { callstr, checkCat(doCheck, line), msgWant }, -#define checkErrorCase(call, msgWant) checkErrorCaseFull(__LINE__, #call, msgWant) -#include "controls_errors.h" -#undef checkErrorCase -#undef checkErrorCaseFull -#undef checkCat - { NULL, NULL, NULL, }, -}; - -testingTest(ControlErrors) -{ - struct checkControlErrorsParams p; - uiControlVtable vtableBadSize; - size_t i; - - memset(&p, 0, sizeof (struct checkControlErrorsParams)); - p.namePlaceholder = "name"; - p.vtablePlaceholder = allocVtable(t); - testingTDefer(t, deferFree, p.vtablePlaceholder); - // TODO osVtablePlaceholder - p.implDataSizePlaceholder = sizeof (struct testImplData); - memset(&vtableBadSize, 0, sizeof (uiControlVtable)); - vtableBadSize.Size = 1; - p.vtableBadSize = &vtableBadSize; - - for (i = 0; controlErrorCases[i].name != NULL; i++) - checkProgrammerError(t, controlErrorCases[i].name, controlErrorCases[i].f, &p, controlErrorCases[i].msgWant); -} diff --git a/test/controls_errors.cpp b/test/controls_errors.cpp new file mode 100644 index 00000000..4b5930da --- /dev/null +++ b/test/controls_errors.cpp @@ -0,0 +1,45 @@ +// 11 june 2019 +#include "test.h" + +static const struct checkErrorCase cases[] = { + { + "uiRegisterControlType() with NULL name", + [](void) { + uiRegisterControlType(NULL, NULL, NULL, 0); + }, + "uiRegisterControlType(): invalid null pointer for uiControlOSVtable", + }, + { + "uiRegisterControlType() with NULL vtable", + [](void) { + uiRegisterControlType("name", NULL, NULL, 0); + }, + "uiRegisterControlType(): invalid null pointer for uiControlVtable", + }, + { + "uiRegisterControlType() with vtable with wrong size", + [](void) { + uiControlVtable vtable; + + memset(&vtable, 0, sizeof (uiControlVtable)); + vtable.Size = 1; + uiRegisterControlSize("name", &vtable, NULL, 0); + }, + "uiRegisterControlType(): wrong size 1 for uiControlVtable", + }, + // TODO individual methods + { + "uiRegisterControlType() with NULL OS vtable", + [](void) { + uiRegisterControlType("name", &testVtable, NULL, 0); + }, + "uiRegisterControlType(): invalid null pointer for uiControlOSVtable", + }, + // OS vtable sizes are tested per-OS + { NULL, NULL, NULL }, +}; + +testingTest(ControlErrors) +{ + checkProgrammerErrors(t, cases); +} diff --git a/test/controls_errors.h b/test/controls_errors.h deleted file mode 100644 index 4cae04f0..00000000 --- a/test/controls_errors.h +++ /dev/null @@ -1,12 +0,0 @@ -// 11 june 2019 - -checkErrorCase(uiRegisterControlType(NULL, p->vtablePlaceholder, p->osVtablePlaceholder, p->implDataSizePlaceholder), - "uiRegisterControlType(): invalid null pointer for uiControlOSVtable") -checkErrorCase(uiRegisterControlType(p->namePlaceholder, NULL, p->osVtablePlaceholder, p->implDataSizePlaceholder), - "uiRegisterControlType(): invalid null pointer for uiControlVtable") -checkErrorCase(uiRegisterControlType(p->namePlaceholder, p->vtableBadSize, p->osVtablePlaceholder, p->implDataSizePlaceholder), - "uiRegisterControlType(): wrong size 1 for uiControlVtable") -// TODO individual methods -checkErrorCase(uiRegisterControlType(p->namePlaceholder, p->vtablePlaceholder, NULL, p->implDataSizePlaceholder), - "uiRegisterControlType(): invalid null pointer for uiControlOSVtable") -// OS vtable sizes are tested per-OS diff --git a/test/meson.build b/test/meson.build index bfe47997..a96978af 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,7 +1,8 @@ # 23 march 2019 libui_test_sources = [ -# 'controls.c', + 'controls.c', + 'controls_errors.cpp', 'errors.c', 'events.c', 'events_errors.cpp', diff --git a/test/test.h b/test/test.h index 9816ae7b..7a30687d 100644 --- a/test/test.h +++ b/test/test.h @@ -50,8 +50,7 @@ extern void checkProgrammerErrorsFull(testingT *t, const char *file, long line, #define checkProgrammerErrorsInThread(t, cases) checkProgrammerErrorsFull(t, __FILE__, __LINE__, cases, true) // controls.c -extern uiControlVtable *allocVtableFull(testingT *t, const char *file, long line); -#define allocVtable(t) allocVtableFull(t, __FILE__, __LINE__) +extern const uiControlVtable testVtable; extern uiControlOSVtable *allocOSVtableFull(testingT *t, const char *file, long line); #define allocOSVtable(t) allocOSVtableFull(t, __FILE__, __LINE__)