2019-05-28 20:54:13 -05:00
|
|
|
// 28 may 2019
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
struct errorCase {
|
|
|
|
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 14:29:03 -05:00
|
|
|
#define allcallsCase(f, ...) \
|
|
|
|
static void beforeInitCase ## f ## Impl(testingT *t, void *data) \
|
|
|
|
{ \
|
|
|
|
struct errorCase *c = (struct errorCase *) data; \
|
|
|
|
memset(c, 0, sizeof (struct errorCase)); \
|
|
|
|
c->msgWant = "attempt to call " #f "() before uiInit()"; \
|
|
|
|
uiprivTestHookReportProgrammerError(handleProgrammerError, c); \
|
2019-06-09 15:14:18 -05:00
|
|
|
testingTDefer(t, deferResetProgrammerError, NULL); \
|
2019-06-09 14:29:03 -05:00
|
|
|
f(__VA_ARGS__); \
|
|
|
|
if (!c->caught) \
|
|
|
|
testingTErrorf(t, "did not throw a programmer error; should have"); \
|
|
|
|
if (c->msgGot != NULL) { \
|
|
|
|
testingTErrorf(t, "message doesn't contain expected string:" diff("%s"), \
|
|
|
|
c->msgGot, c->msgWant); \
|
|
|
|
testingUtilFreeStrdup(c->msgGot); \
|
|
|
|
} \
|
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;
|
|
|
|
void (*f)(testingT *, void *);
|
|
|
|
} beforeInitCases[] = {
|
|
|
|
#define allcallsCase(f, ...) { #f, beforeInitCase ## f ## Impl },
|
|
|
|
allcallsCase(uiQueueMain, NULL, NULL)
|
2019-05-28 20:54:13 -05:00
|
|
|
#include "allcalls.h"
|
2019-06-09 14:29:03 -05:00
|
|
|
#undef allcallsCase
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
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 14:29:03 -05:00
|
|
|
for (i = 0; beforeInitCases[i].name != NULL; i++)
|
|
|
|
testingTRun(t, beforeInitCases[i].name, beforeInitCases[i].f, &c);
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:14:18 -05:00
|
|
|
#define allcallsCase(f, ...) \
|
|
|
|
static void wrongThreadCase ## f ## ThreadProc(void *data) \
|
|
|
|
{ \
|
|
|
|
f(__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
static void wrongThreadCase ## f ## Impl(testingT *t, void *data) \
|
|
|
|
{ \
|
|
|
|
struct errorCase *c = (struct errorCase *) data; \
|
|
|
|
threadThread *thread; \
|
|
|
|
threadSysError err; \
|
|
|
|
memset(c, 0, sizeof (struct errorCase)); \
|
|
|
|
c->msgWant = "attempt to call " #f "() on a thread other than the GUI thread"; \
|
|
|
|
uiprivTestHookReportProgrammerError(handleProgrammerError, c); \
|
|
|
|
testingTDefer(t, deferResetProgrammerError, NULL); \
|
|
|
|
err = threadNewThread(wrongThreadCase ## f ## ThreadProc, NULL, &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)); \
|
|
|
|
if (!c->caught) \
|
|
|
|
testingTErrorf(t, "did not throw a programmer error; should have"); \
|
|
|
|
if (c->msgGot != NULL) { \
|
|
|
|
testingTErrorf(t, "message doesn't contain expected string:" diff("%s"), \
|
|
|
|
c->msgGot, c->msgWant); \
|
|
|
|
testingUtilFreeStrdup(c->msgGot); \
|
|
|
|
} \
|
|
|
|
}
|
2019-05-28 20:54:13 -05:00
|
|
|
#include "allcalls.h"
|
2019-06-09 15:14:18 -05:00
|
|
|
#undef allcallsCase
|
2019-05-28 20:54:13 -05:00
|
|
|
|
2019-06-09 15:14:18 -05:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
void (*f)(testingT *, void *);
|
|
|
|
} wrongThreadCases[] = {
|
|
|
|
#define allcallsCase(f, ...) { #f, wrongThreadCase ## f ## Impl },
|
|
|
|
#include "allcalls.h"
|
|
|
|
#undef allcallsCase
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
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 15:14:18 -05:00
|
|
|
for (i = 0; wrongThreadCases[i].name != NULL; i++)
|
|
|
|
testingTRun(t, wrongThreadCases[i].name, wrongThreadCases[i].f, &c);
|
2019-05-28 20:54:13 -05:00
|
|
|
}
|