Started properly reintegrating the core of noinitwrongthread.c (that is, errors.c).
This commit is contained in:
parent
8b400f33f7
commit
001e439a9b
|
@ -1,6 +1,8 @@
|
||||||
// 28 may 2019
|
// 28 may 2019
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
// Do not put any test cases in this file; they will not be run.
|
||||||
|
|
||||||
struct checkProgrammerErrorParams {
|
struct checkProgrammerErrorParams {
|
||||||
const char *file;
|
const char *file;
|
||||||
long line;
|
long line;
|
||||||
|
@ -11,18 +13,26 @@ struct checkProgrammerErrorParams {
|
||||||
const char *msgWant;
|
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)
|
static void handleProgrammerError(const char *msg, void *data)
|
||||||
{
|
{
|
||||||
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
|
struct checkProgrammerErrorParams *p = (struct checkProgrammerErrorParams *) data;
|
||||||
|
|
||||||
p->caught = true;
|
p->caught = true;
|
||||||
if (strcmp(msg, p->msgWant) != 0)
|
if (strcmp(msg, p->msgWant) != 0)
|
||||||
p->msgGot = testingUtilStrdup(msg);
|
p->msgGot = ourStrdup(p, msg);
|
||||||
}
|
|
||||||
|
|
||||||
static void deferResetProgrammerError(testingT *t, void *data)
|
|
||||||
{
|
|
||||||
uiprivTestHookReportProgrammerError(NULL, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void checkProgrammerErrorThreadProc(void *data)
|
static void checkProgrammerErrorThreadProc(void *data)
|
||||||
|
@ -32,38 +42,36 @@ static void checkProgrammerErrorThreadProc(void *data)
|
||||||
(*(p->f))();
|
(*(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) {
|
if (p->inThread) {
|
||||||
threadThread *thread;
|
threadThread *thread;
|
||||||
threadSysError err;
|
threadSysError err;
|
||||||
|
|
||||||
err = threadNewThread(checkProgrammerErrorThreadProc, p, &thread);
|
err = threadNewThread(checkProgrammerErrorThreadProc, p, &thread);
|
||||||
if (err != 0)
|
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);
|
err = threadThreadWaitAndFree(thread);
|
||||||
if (err != 0)
|
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
|
} else
|
||||||
(*(p->f))();
|
(*(p->f))();
|
||||||
if (!p->caught)
|
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) {
|
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);
|
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;
|
const struct checkErrorCase *c;
|
||||||
struct checkProgrammerErrorParams p;
|
struct checkProgrammerErrorParams p;
|
||||||
|
|
||||||
|
uiprivTestHookReportProgrammerError(handleProgrammerError, p);
|
||||||
memset(&p, 0, sizeof (struct checkProgrammerErrorParams));
|
memset(&p, 0, sizeof (struct checkProgrammerErrorParams));
|
||||||
p.file = file;
|
p.file = file;
|
||||||
p.line = line;
|
p.line = line;
|
||||||
|
@ -73,6 +81,8 @@ void checkProgrammerErrorsFull(testingT *t, const char *file, long line, const s
|
||||||
p.f = c->f;
|
p.f = c->f;
|
||||||
p.msgGot = NULL;
|
p.msgGot = NULL;
|
||||||
p.msgWant = c->msgWant;
|
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);
|
||||||
}
|
}
|
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
libui_test_sources = [
|
libui_test_sources = [
|
||||||
'initmain.c',
|
'initmain.c',
|
||||||
|
'noinitwrongthread.c',
|
||||||
]
|
]
|
||||||
|
|
||||||
libui_test_sources_without_cases = [
|
libui_test_sources_without_cases = [
|
||||||
|
'errors.c',
|
||||||
'main.c',
|
'main.c',
|
||||||
]
|
]
|
||||||
if libui_OS == 'windows'
|
if libui_OS == 'windows'
|
||||||
|
|
10
test/test.h
10
test/test.h
|
@ -97,6 +97,16 @@ sharedbitsPrintfFunc(
|
||||||
|
|
||||||
#define diff(fmt) "\ngot " fmt "\nwant " fmt
|
#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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue