Rewrote test/errors.c as previously planned. checkProgrammerErrors() is no longer self-contained, but rather a header and a footer function. We'll need to change the handling of test/noinitwrongthread.c in the Python appropriately, but this'll be fine. (Plus, we can localize the inThread logic into that file too!)

This commit is contained in:
Pietro Gagliardi 2020-02-23 18:04:23 -05:00
parent 5a7777ffaa
commit 0d9f58b5e1
2 changed files with 32 additions and 39 deletions

View File

@ -6,16 +6,12 @@
// Do not put any test cases in this file; they will not be run. // Do not put any test cases in this file; they will not be run.
struct checkProgrammerErrorParams { struct checkProgrammerErrorParams {
const char *file;
long line;
bool inThread;
bool caught; bool caught;
void (*f)(void);
char *msgGot; char *msgGot;
const char *msgWant; const char *msgWant;
}; };
static char *ourStrdup(struct checkProgrammerErrorParams *p, const char *s) static char *ourStrdup(const char *s)
{ {
char *t; char *t;
size_t n; size_t n;
@ -23,7 +19,7 @@ static char *ourStrdup(struct checkProgrammerErrorParams *p, const char *s)
n = (strlen(s) + 1) * sizeof (char); n = (strlen(s) + 1) * sizeof (char);
t = (char *) malloc(n); t = (char *) malloc(n);
if (t == NULL) 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); memcpy(t, s, n);
return t; return t;
} }
@ -34,9 +30,11 @@ static void handleProgrammerError(const char *msg, void *data)
p->caught = true; p->caught = true;
if (strcmp(msg, p->msgWant) != 0) if (strcmp(msg, p->msgWant) != 0)
p->msgGot = ourStrdup(p, msg); p->msgGot = ourStrdup(msg);
} }
#if 0
// TODO
static void checkProgrammerErrorThreadProc(void *data) static void checkProgrammerErrorThreadProc(void *data)
{ {
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data; struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
@ -57,34 +55,34 @@ static void checkProgrammerErrorSubtestImpl(struct checkProgrammerErrorParams *p
err = threadThreadWaitAndFree(thread); err = threadThreadWaitAndFree(thread);
if (err != 0) if (err != 0)
TestFatalfFull(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))(); }
#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) 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) { 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); p->msgGot, p->msgWant);
free(p->msgGot); free(p->msgGot);
} }
} free(p);
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);
}
uiprivTestHookReportProgrammerError(NULL, NULL); uiprivTestHookReportProgrammerError(NULL, NULL);
} }

View File

@ -78,14 +78,9 @@ sharedbitsPrintfFunc(
#define diff(fmt) "\ngot " fmt "\nwant " fmt #define diff(fmt) "\ngot " fmt "\nwant " fmt
// errors.c // errors.c
struct checkErrorCase { extern void *beginCheckProgrammerError(const char *want);
const char *name; extern void endCheckProgrammerErrorFull(const char *file, long line, void *context);
void (*f)(void); #define endCheckProgrammerError(context) checkProgrammerErrorFull(__FILE__, __LINE__, context)
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 #ifdef __cplusplus
} }