Started properly reintegrating the core of noinitwrongthread.c (that is, errors.c).

This commit is contained in:
Pietro Gagliardi 2020-01-26 20:02:03 -05:00
parent 8b400f33f7
commit 001e439a9b
3 changed files with 40 additions and 18 deletions

View File

@ -1,6 +1,8 @@
// 28 may 2019
#include "test.h"
// Do not put any test cases in this file; they will not be run.
struct checkProgrammerErrorParams {
const char *file;
long line;
@ -11,18 +13,26 @@ struct checkProgrammerErrorParams {
const char *msgWant;
};
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;
}
static void handleProgrammerError(const char *msg, void *data)
{
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
p->caught = true;
if (strcmp(msg, p->msgWant) != 0)
p->msgGot = testingUtilStrdup(msg);
}
static void deferResetProgrammerError(testingT *t, void *data)
{
uiprivTestHookReportProgrammerError(NULL, NULL);
p->msgGot = ourStrdup(p, msg);
}
static void checkProgrammerErrorThreadProc(void *data)
@ -32,38 +42,36 @@ static void checkProgrammerErrorThreadProc(void *data)
(*(p->f))();
}
static void checkProgrammerErrorSubtestImpl(testingT *t, void *data)
static void checkProgrammerErrorSubtestImpl(struct checkProgrammerErrorParams *p)
{
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
uiprivTestHookReportProgrammerError(handleProgrammerError, p);
testingTDefer(t, deferResetProgrammerError, NULL);
if (p->inThread) {
threadThread *thread;
threadSysError err;
err = threadNewThread(checkProgrammerErrorThreadProc, p, &thread);
if (err != 0)
testingTFatalfFull(t, p->file, p->line, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
// TODO these should only fatal out of the subtest
TestFatalfFull(p->file, p->line, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
err = threadThreadWaitAndFree(thread);
if (err != 0)
testingTFatalfFull(t, 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))();
if (!p->caught)
testingTErrorfFull(t, p->file, p->line, "did not throw a programmer error; should have");
TestErrorfFull(p->file, p->line, "did not throw a programmer error; should have");
if (p->msgGot != NULL) {
testingTErrorfFull(t, p->file, p->line, "message doesn't match expected string:" diff("%s"),
TestErrorfFull(p->file, p->line, "message doesn't match expected string:" diff("%s"),
p->msgGot, p->msgWant);
testingUtilFreeStrdup(p->msgGot);
free(p->msgGot);
}
}
void checkProgrammerErrorsFull(testingT *t, const char *file, long line, const struct checkErrorCase *cases, bool inThread)
void checkProgrammerErrorsFull(const char *file, long line, const struct checkErrorCase *cases, bool inThread)
{
const struct checkErrorCase *c;
struct checkProgrammerErrorParams p;
uiprivTestHookReportProgrammerError(handleProgrammerError, p);
memset(&p, 0, sizeof (struct checkProgrammerErrorParams));
p.file = file;
p.line = line;
@ -73,6 +81,8 @@ void checkProgrammerErrorsFull(testingT *t, const char *file, long line, const s
p.f = c->f;
p.msgGot = NULL;
p.msgWant = c->msgWant;
testingTRun(t, c->name, checkProgrammerErrorSubtestImpl, &p);
// TODO this should be a proper subtest, but we don't have that facility with meson yet
checkProgrammerErrorSubtestImpl(&p);
}
uiprivTestHookReportProgrammerError(NULL, NULL);
}

View File

@ -2,9 +2,11 @@
libui_test_sources = [
'initmain.c',
'noinitwrongthread.c',
]
libui_test_sources_without_cases = [
'errors.c',
'main.c',
]
if libui_OS == 'windows'

View File

@ -97,6 +97,16 @@ 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)
#ifdef __cplusplus
}
#endif