From 001e439a9ba3131de41a47a83e073d29516fee2e Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 26 Jan 2020 20:02:03 -0500 Subject: [PATCH] Started properly reintegrating the core of noinitwrongthread.c (that is, errors.c). --- {zNEW_test => test}/errors.c | 46 ++++++++++++++++++++++-------------- test/meson.build | 2 ++ test/test.h | 10 ++++++++ 3 files changed, 40 insertions(+), 18 deletions(-) rename {zNEW_test => test}/errors.c (52%) diff --git a/zNEW_test/errors.c b/test/errors.c similarity index 52% rename from zNEW_test/errors.c rename to test/errors.c index 790a4aca..47002b60 100644 --- a/zNEW_test/errors.c +++ b/test/errors.c @@ -1,6 +1,8 @@ // 28 may 2019 #include "test.h" +// Do not put any test cases in this file; they will not be run. + struct checkProgrammerErrorParams { const char *file; long line; @@ -11,18 +13,26 @@ struct checkProgrammerErrorParams { const char *msgWant; }; +static char *ourStrdup(struct checkProgrammerErrorParams *p, const char *s) +{ + char *t; + size_t n; + + n = (strlen(s) + 1) * sizeof (char); + t = (char *) malloc(n); + if (t == NULL) + TestFatalfFull(p->file, p->line, "memory exhausted in ourStrdup() copying %s", s); + memcpy(t, s, n); + return t; +} + static void handleProgrammerError(const char *msg, void *data) { struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data; p->caught = true; if (strcmp(msg, p->msgWant) != 0) - p->msgGot = testingUtilStrdup(msg); -} - -static void deferResetProgrammerError(testingT *t, void *data) -{ - uiprivTestHookReportProgrammerError(NULL, NULL); + p->msgGot = ourStrdup(p, msg); } static void checkProgrammerErrorThreadProc(void *data) @@ -32,38 +42,36 @@ static void checkProgrammerErrorThreadProc(void *data) (*(p->f))(); } -static void checkProgrammerErrorSubtestImpl(testingT *t, void *data) +static void checkProgrammerErrorSubtestImpl(struct checkProgrammerErrorParams *p) { - struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data; - - uiprivTestHookReportProgrammerError(handleProgrammerError, p); - testingTDefer(t, deferResetProgrammerError, NULL); if (p->inThread) { threadThread *thread; threadSysError err; err = threadNewThread(checkProgrammerErrorThreadProc, p, &thread); if (err != 0) - testingTFatalfFull(t, p->file, p->line, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err)); + // TODO these should only fatal out of the subtest + TestFatalfFull(p->file, p->line, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err)); err = threadThreadWaitAndFree(thread); if (err != 0) - testingTFatalfFull(t, p->file, p->line, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err)); + TestFatalfFull(p->file, p->line, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err)); } else (*(p->f))(); if (!p->caught) - testingTErrorfFull(t, p->file, p->line, "did not throw a programmer error; should have"); + TestErrorfFull(p->file, p->line, "did not throw a programmer error; should have"); if (p->msgGot != NULL) { - testingTErrorfFull(t, p->file, p->line, "message doesn't match expected string:" diff("%s"), + TestErrorfFull(p->file, p->line, "message doesn't match expected string:" diff("%s"), p->msgGot, p->msgWant); - testingUtilFreeStrdup(p->msgGot); + free(p->msgGot); } } -void checkProgrammerErrorsFull(testingT *t, const char *file, long line, const struct checkErrorCase *cases, bool inThread) +void checkProgrammerErrorsFull(const char *file, long line, const struct checkErrorCase *cases, bool inThread) { const struct checkErrorCase *c; struct checkProgrammerErrorParams p; + uiprivTestHookReportProgrammerError(handleProgrammerError, p); memset(&p, 0, sizeof (struct checkProgrammerErrorParams)); p.file = file; p.line = line; @@ -73,6 +81,8 @@ void checkProgrammerErrorsFull(testingT *t, const char *file, long line, const s p.f = c->f; p.msgGot = NULL; p.msgWant = c->msgWant; - testingTRun(t, c->name, checkProgrammerErrorSubtestImpl, &p); + // TODO this should be a proper subtest, but we don't have that facility with meson yet + checkProgrammerErrorSubtestImpl(&p); } + uiprivTestHookReportProgrammerError(NULL, NULL); } diff --git a/test/meson.build b/test/meson.build index 5a6f38cf..1f781651 100644 --- a/test/meson.build +++ b/test/meson.build @@ -2,9 +2,11 @@ libui_test_sources = [ 'initmain.c', + 'noinitwrongthread.c', ] libui_test_sources_without_cases = [ + 'errors.c', 'main.c', ] if libui_OS == 'windows' diff --git a/test/test.h b/test/test.h index 2097531f..922e565c 100644 --- a/test/test.h +++ b/test/test.h @@ -97,6 +97,16 @@ sharedbitsPrintfFunc( #define diff(fmt) "\ngot " fmt "\nwant " fmt +// errors.c +struct checkErrorCase { + const char *name; + void (*f)(void); + const char *msgWant; +}; +extern void checkProgrammerErrorsFull(const char *file, long line, const struct checkErrorCase *cases, bool inThread); +#define checkProgrammerErrors(cases) checkProgrammerErrorsFull(__FILE__, __LINE__, cases, false) +#define checkProgrammerErrorsInThread(cases) checkProgrammerErrorsFull(__FILE__, __LINE__, cases, true) + #ifdef __cplusplus } #endif