2019-05-28 20:54:13 -05:00
|
|
|
// 28 may 2019
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
struct errorCase {
|
2019-06-09 18:00:01 -05:00
|
|
|
void (*f)(testingT *t, struct errorCase *c);
|
|
|
|
void (*g)(testingT *t, struct errorCase *c);
|
2019-05-28 20:54:13 -05:00
|
|
|
bool caught;
|
|
|
|
char *msgGot;
|
|
|
|
const char *msgWant;
|
|
|
|
};
|
|
|
|
|
2019-06-09 14:29:03 -05:00
|
|
|
static void handleProgrammerError(const char *msg, void *data)
|
2019-05-28 20:54:13 -05:00
|
|
|
{
|
2019-06-09 14:29:03 -05:00
|
|
|
struct errorCase *c = (struct errorCase *) data;
|
2019-05-31 01:58:57 -05:00
|
|
|
size_t n;
|
|
|
|
|
2019-06-09 12:57:02 -05:00
|
|
|
c->caught = true;
|
|
|
|
if (strcmp(msg, c->msgWant) != 0) {
|
2019-05-31 01:58:57 -05:00
|
|
|
n = strlen(msg);
|
2019-06-09 13:03:27 -05:00
|
|
|
c->msgGot = testingUtilStrdup(msg);
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 15:14:18 -05:00
|
|
|
static void deferResetProgrammerError(testingT *t, void *data)
|
|
|
|
{
|
|
|
|
uiprivTestHookReportProgrammerError(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2019-06-09 18:00:01 -05:00
|
|
|
static void doCase(testingT *t, void *data)
|
|
|
|
{
|
|
|
|
struct errorCase *c = (struct errorCase *) data;
|
|
|
|
|
|
|
|
c->caught = false;
|
|
|
|
c->msgGot = NULL;
|
|
|
|
uiprivTestHookReportProgrammerError(handleProgrammerError, c);
|
|
|
|
testingTDefer(t, deferResetProgrammerError, NULL);
|
|
|
|
(*(c->f))(t, c);
|
|
|
|
if (!c->caught)
|
|
|
|
testingTErrorf(t, "did not throw a programmer error; should have");
|
|
|
|
if (c->msgGot != NULL) {
|
|
|
|
testingTErrorf(t, "message doesn't match expected string:" diff("%s"),
|
|
|
|
c->msgGot, c->msgWant);
|
|
|
|
testingUtilFreeStrdup(c->msgGot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 14:29:03 -05:00
|
|
|
#define allcallsCase(f, ...) \
|
2019-06-09 18:00:01 -05:00
|
|
|
void doCase ## f(testingT *t, struct errorCase *c) \
|
2019-06-09 14:29:03 -05:00
|
|
|
{ \
|
|
|
|
f(__VA_ARGS__); \
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
2019-06-09 14:29:03 -05:00
|
|
|
allcallsCase(uiQueueMain, NULL, NULL)
|
|
|
|
#include "allcalls.h"
|
|
|
|
#undef allcallsCase
|
2019-05-28 20:54:13 -05:00
|
|
|
|
2019-06-09 14:29:03 -05:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
2019-06-09 18:00:01 -05:00
|
|
|
void (*f)(testingT *t, struct errorCase *c);
|
|
|
|
const char *beforeInitWant;
|
|
|
|
const char *wrongThreadWant;
|
|
|
|
} allCases[] = {
|
|
|
|
#define allcallsCase(f, ...) { #f, doCase ## f, \
|
|
|
|
"attempt to call " #f "() before uiInit()", \
|
|
|
|
"attempt to call " #f "() on a thread other than the GUI thread", \
|
|
|
|
},
|
|
|
|
{ "uiQueueMain", doCaseuiQueueMain, "attempt to call uiQueueMain() before uiInit()", NULL },
|
2019-05-28 20:54:13 -05:00
|
|
|
#include "allcalls.h"
|
2019-06-09 14:29:03 -05:00
|
|
|
#undef allcallsCase
|
2019-06-09 18:00:01 -05:00
|
|
|
{ NULL, NULL, NULL, NULL },
|
2019-06-09 14:29:03 -05:00
|
|
|
};
|
2019-05-28 20:54:13 -05:00
|
|
|
|
|
|
|
testingTestInSet(beforeTests, FunctionsFailBeforeInit)
|
|
|
|
{
|
2019-06-09 14:29:03 -05:00
|
|
|
struct errorCase c;
|
|
|
|
size_t i;
|
2019-05-28 20:54:13 -05:00
|
|
|
|
2019-06-09 18:00:01 -05:00
|
|
|
memset(&c, 0, sizeof (struct errorCase));
|
|
|
|
for (i = 0; allCases[i].name != NULL; i++) {
|
|
|
|
c.f = allCases[i].f;
|
|
|
|
c.msgWant = allCases[i].beforeInitWant;
|
|
|
|
if (c.msgWant == NULL)
|
|
|
|
continue;
|
|
|
|
testingTRun(t, allCases[i].name, doCase, &c);
|
|
|
|
}
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
|
2019-06-09 18:00:01 -05:00
|
|
|
static void runInThreadThreadProc(void *data)
|
|
|
|
{
|
|
|
|
struct errorCase *c = (struct errorCase *) data;
|
2019-05-28 20:54:13 -05:00
|
|
|
|
2019-06-09 18:00:01 -05:00
|
|
|
(*(c->g))(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void runInThread(testingT *t, struct errorCase *c)
|
|
|
|
{
|
|
|
|
threadThread *thread;
|
|
|
|
threadSysError err;
|
|
|
|
|
|
|
|
err = threadNewThread(runInThreadThreadProc, c, &thread);
|
|
|
|
if (err != 0)
|
|
|
|
testingTFatalf(t, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
|
|
|
err = threadThreadWaitAndFree(thread);
|
|
|
|
if (err != 0)
|
|
|
|
testingTFatalf(t, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err));
|
|
|
|
}
|
2019-05-28 20:54:13 -05:00
|
|
|
|
|
|
|
testingTest(FunctionsFailOnWrongThread)
|
|
|
|
{
|
2019-06-09 15:14:18 -05:00
|
|
|
struct errorCase c;
|
|
|
|
size_t i;
|
2019-05-28 20:54:13 -05:00
|
|
|
|
2019-06-09 18:00:01 -05:00
|
|
|
memset(&c, 0, sizeof (struct errorCase));
|
|
|
|
c.f = runInThread;
|
|
|
|
for (i = 0; allCases[i].name != NULL; i++) {
|
|
|
|
c.g = allCases[i].f;
|
|
|
|
c.msgWant = allCases[i].wrongThreadWant;
|
|
|
|
if (c.msgWant == NULL)
|
|
|
|
continue;
|
|
|
|
testingTRun(t, allCases[i].name, doCase, &c);
|
|
|
|
}
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|