diff --git a/test/errors.c b/test/errors.c index df0061b5..9b32d6b1 100644 --- a/test/errors.c +++ b/test/errors.c @@ -6,16 +6,12 @@ // Do not put any test cases in this file; they will not be run. struct checkProgrammerErrorParams { - const char *file; - long line; - bool inThread; bool caught; - void (*f)(void); char *msgGot; const char *msgWant; }; -static char *ourStrdup(struct checkProgrammerErrorParams *p, const char *s) +static char *ourStrdup(const char *s) { char *t; size_t n; @@ -23,7 +19,7 @@ static char *ourStrdup(struct checkProgrammerErrorParams *p, const char *s) 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); + TestFatalf("memory exhausted in ourStrdup() copying %s", s); memcpy(t, s, n); return t; } @@ -34,9 +30,11 @@ static void handleProgrammerError(const char *msg, void *data) p->caught = true; if (strcmp(msg, p->msgWant) != 0) - p->msgGot = ourStrdup(p, msg); + p->msgGot = ourStrdup(msg); } +#if 0 +// TODO static void checkProgrammerErrorThreadProc(void *data) { struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data; @@ -57,34 +55,34 @@ static void checkProgrammerErrorSubtestImpl(struct checkProgrammerErrorParams *p err = threadThreadWaitAndFree(thread); if (err != 0) TestFatalfFull(p->file, p->line, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err)); - } else - (*(p->f))(); + } +} +#endif + +void *beginCheckProgrammerError(const char *want) +{ + struct checkProgrammerErrorParams *p; + + p = (struct checkProgrammerErrorParams *) malloc(sizeof (struct checkProgrammerErrorParams)); + if (p == NULL) + TestFatalf("memory exhausted allocating beginCheckProgrammerError() context"); + memset(p, 0, sizeof (struct checkProgrammerErrorParams)); + p->msgWant = want; + uiprivTestHookReportProgrammerError(handleProgrammerError, p); + return p; +} + +void endCheckProgrammerErrorFull(const char *file, long line, void *context) +{ + struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) context; + if (!p->caught) - TestErrorfFull(p->file, p->line, "did not throw a programmer error; should have"); + TestErrorfFull(file, line, "did not throw a programmer error; should have"); if (p->msgGot != NULL) { - TestErrorfFull(p->file, p->line, "message doesn't match expected string:" diff("%s"), + TestErrorfFull(file, line, "message doesn't match expected string:" diff("%s"), p->msgGot, p->msgWant); free(p->msgGot); } -} - -void checkProgrammerErrorsFull(const char *file, long line, const struct checkErrorCase *cases, bool inThread) -{ - const struct checkErrorCase *c; - struct checkProgrammerErrorParams p; - - memset(&p, 0, sizeof (struct checkProgrammerErrorParams)); - p.file = file; - p.line = line; - p.inThread = inThread; - uiprivTestHookReportProgrammerError(handleProgrammerError, &p); - for (c = cases; c->name != NULL; c++) { - p.caught = false; - p.f = c->f; - p.msgGot = NULL; - p.msgWant = c->msgWant; - // TODO this should be a proper subtest, but we don't have that facility with meson yet - checkProgrammerErrorSubtestImpl(&p); - } + free(p); uiprivTestHookReportProgrammerError(NULL, NULL); } diff --git a/test/test.h b/test/test.h index 3071180d..16b8f235 100644 --- a/test/test.h +++ b/test/test.h @@ -78,14 +78,9 @@ 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) +extern void *beginCheckProgrammerError(const char *want); +extern void endCheckProgrammerErrorFull(const char *file, long line, void *context); +#define endCheckProgrammerError(context) checkProgrammerErrorFull(__FILE__, __LINE__, context) #ifdef __cplusplus }