2019-06-09 21:45:44 -05:00
|
|
|
// 28 may 2019
|
|
|
|
#include "test.h"
|
2020-01-26 19:06:12 -06:00
|
|
|
#include "thread.h"
|
|
|
|
#include "../common/testhooks.h"
|
2019-06-09 21:45:44 -05:00
|
|
|
|
2020-01-26 19:02:03 -06:00
|
|
|
// Do not put any test cases in this file; they will not be run.
|
|
|
|
|
2019-06-09 21:45:44 -05:00
|
|
|
struct checkProgrammerErrorParams {
|
|
|
|
const char *file;
|
|
|
|
long line;
|
|
|
|
bool inThread;
|
|
|
|
bool caught;
|
2019-06-15 18:53:50 -05:00
|
|
|
void (*f)(void);
|
2019-06-09 21:45:44 -05:00
|
|
|
char *msgGot;
|
|
|
|
const char *msgWant;
|
|
|
|
};
|
|
|
|
|
2020-01-26 19:02:03 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-09 21:45:44 -05:00
|
|
|
static void handleProgrammerError(const char *msg, void *data)
|
|
|
|
{
|
|
|
|
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
|
|
|
|
|
|
|
|
p->caught = true;
|
|
|
|
if (strcmp(msg, p->msgWant) != 0)
|
2020-01-26 19:02:03 -06:00
|
|
|
p->msgGot = ourStrdup(p, msg);
|
2019-06-09 21:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkProgrammerErrorThreadProc(void *data)
|
|
|
|
{
|
|
|
|
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
|
|
|
|
|
2019-06-15 18:53:50 -05:00
|
|
|
(*(p->f))();
|
2019-06-09 21:45:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-26 19:02:03 -06:00
|
|
|
static void checkProgrammerErrorSubtestImpl(struct checkProgrammerErrorParams *p)
|
2019-06-09 21:45:44 -05:00
|
|
|
{
|
|
|
|
if (p->inThread) {
|
|
|
|
threadThread *thread;
|
|
|
|
threadSysError err;
|
|
|
|
|
|
|
|
err = threadNewThread(checkProgrammerErrorThreadProc, p, &thread);
|
|
|
|
if (err != 0)
|
2020-02-09 12:37:45 -06:00
|
|
|
// TODO these should only fatal out of the subtest (see TODO below)
|
2020-01-26 19:02:03 -06:00
|
|
|
TestFatalfFull(p->file, p->line, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
2019-06-09 21:45:44 -05:00
|
|
|
err = threadThreadWaitAndFree(thread);
|
|
|
|
if (err != 0)
|
2020-01-26 19:02:03 -06:00
|
|
|
TestFatalfFull(p->file, p->line, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
2019-06-09 21:45:44 -05:00
|
|
|
} else
|
2019-06-15 18:53:50 -05:00
|
|
|
(*(p->f))();
|
2019-06-09 21:45:44 -05:00
|
|
|
if (!p->caught)
|
2020-01-26 19:02:03 -06:00
|
|
|
TestErrorfFull(p->file, p->line, "did not throw a programmer error; should have");
|
2019-06-09 21:45:44 -05:00
|
|
|
if (p->msgGot != NULL) {
|
2020-01-26 19:02:03 -06:00
|
|
|
TestErrorfFull(p->file, p->line, "message doesn't match expected string:" diff("%s"),
|
2019-06-09 21:45:44 -05:00
|
|
|
p->msgGot, p->msgWant);
|
2020-01-26 19:02:03 -06:00
|
|
|
free(p->msgGot);
|
2019-06-09 21:45:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 19:02:03 -06:00
|
|
|
void checkProgrammerErrorsFull(const char *file, long line, const struct checkErrorCase *cases, bool inThread)
|
2019-06-09 21:45:44 -05:00
|
|
|
{
|
2019-06-15 18:53:50 -05:00
|
|
|
const struct checkErrorCase *c;
|
2019-06-09 21:45:44 -05:00
|
|
|
struct checkProgrammerErrorParams p;
|
|
|
|
|
|
|
|
memset(&p, 0, sizeof (struct checkProgrammerErrorParams));
|
|
|
|
p.file = file;
|
|
|
|
p.line = line;
|
|
|
|
p.inThread = inThread;
|
2020-01-26 19:06:12 -06:00
|
|
|
uiprivTestHookReportProgrammerError(handleProgrammerError, &p);
|
2019-06-15 18:53:50 -05:00
|
|
|
for (c = cases; c->name != NULL; c++) {
|
|
|
|
p.caught = false;
|
|
|
|
p.f = c->f;
|
|
|
|
p.msgGot = NULL;
|
|
|
|
p.msgWant = c->msgWant;
|
2020-01-26 19:02:03 -06:00
|
|
|
// TODO this should be a proper subtest, but we don't have that facility with meson yet
|
|
|
|
checkProgrammerErrorSubtestImpl(&p);
|
2019-06-15 18:53:50 -05:00
|
|
|
}
|
2020-01-26 19:02:03 -06:00
|
|
|
uiprivTestHookReportProgrammerError(NULL, NULL);
|
2019-06-09 21:45:44 -05:00
|
|
|
}
|