Implemented error checks in test/initmain.c.

This commit is contained in:
Pietro Gagliardi 2019-05-04 19:21:57 -04:00
parent 09e98af110
commit 4c097f93f7
1 changed files with 57 additions and 35 deletions

View File

@ -48,29 +48,36 @@ testingTestBefore(Init)
testingTErrorf(t, "uiInit() after a previous successful call returned bad error message: got %s, want %s", err.Message, errAlreadyInitialized); testingTErrorf(t, "uiInit() after a previous successful call returned bad error message: got %s, want %s", err.Message, errAlreadyInitialized);
} }
struct testParams {
uint32_t flag;
timerSysError err;
};
static void queued(void *data) static void queued(void *data)
{ {
int *flag = (int *) data; struct testParams *p = (struct testParams *) data;
*flag = 1; p->flag = 1;
uiQuit(); uiQuit();
} }
testingTest(QueueMain) testingTest(QueueMain)
{ {
int flag = 0; struct testParams p;
uiQueueMain(queued, &flag); memset(&p, 0, sizeof (struct testParams));
p.flag = 0;
uiQueueMain(queued, &p);
timeout_uiMain(t, 5 * timerSecond); timeout_uiMain(t, 5 * timerSecond);
if (flag != 1) if (p.flag != 1)
testingTErrorf(t, "uiQueueMain() didn't set flag properly: got %d, want 1", flag); testingTErrorf(t, "uiQueueMain() didn't set flag properly: got %d, want 1", p.flag);
} }
#define queueOp(name, expr) \ #define queueOp(name, expr) \
static void name(void *data) \ static void name(void *data) \
{ \ { \
uint32_t *flag = (uint32_t *) data; \ struct testParams *p = (struct testParams *) data; \
*flag = *flag expr; \ p->flag = p->flag expr; \
} }
queueOp(sub2, - 2) queueOp(sub2, - 2)
queueOp(div3, / 3) queueOp(div3, / 3)
@ -93,12 +100,12 @@ static const struct {
{ 16, "mul8 -> div3 -> sub2" }, { 16, "mul8 -> div3 -> sub2" },
}; };
static void queueOrder(uint32_t *flag) static void queueOrder(struct testParams *p)
{ {
*flag = 7; p->flag = 7;
uiQueueMain(sub2, flag); uiQueueMain(sub2, p);
uiQueueMain(div3, flag); uiQueueMain(div3, p);
uiQueueMain(mul8, flag); uiQueueMain(mul8, p);
uiQueueMain(done, NULL); uiQueueMain(done, NULL);
} }
@ -119,54 +126,69 @@ static void checkOrder(testingT *t, uint32_t flag)
testingTest(QueueMain_Sequence) testingTest(QueueMain_Sequence)
{ {
uint32_t flag; struct testParams p;
queueOrder(&flag); memset(&p, 0, sizeof (struct testParams));
queueOrder(&p);
timeout_uiMain(t, 5 * timerSecond); timeout_uiMain(t, 5 * timerSecond);
checkOrder(t, flag); checkOrder(t, p.flag);
} }
static void queueThread(void *data) static void queueThread(void *data)
{ {
int *flag = (int *) data; struct testParams *p = (struct testParams *) data;
// TODO error check p->err = timerSleep(1250 * timerMillisecond);
timerSleep(1250 * timerMillisecond); uiQueueMain(queued, p);
uiQueueMain(queued, flag);
} }
testingTest(QueueMain_DifferentThread) testingTest(QueueMain_DifferentThread)
{ {
threadThread *thread; threadThread *thread;
int flag = 0; threadSysError err;
struct testParams p;
// TODO check errors memset(&p, 0, sizeof (struct testParams));
threadNewThread(queueThread, &flag, &thread); p.flag = 0;
err = threadNewThread(queueThread, &p, &thread);
if (err != 0)
testingTFatalf(t, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
timeout_uiMain(t, 5 * timerSecond); timeout_uiMain(t, 5 * timerSecond);
threadThreadWaitAndFree(thread); err = threadThreadWaitAndFree(thread);
if (flag != 1) if (err != 0)
testingTErrorf(t, "uiQueueMain() didn't set flag properly: got %d, want 1", flag); testingTFatalf(t, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err));
if (p.err != 0)
testingTErrorf(t, "error sleeping in thread to ensure a high likelihood the uiQueueMain() is run after uiMain() starts: " timerSysErrorFmt, timerSysErrorFmtArg(p.err));
if (p.flag != 1)
testingTErrorf(t, "uiQueueMain() didn't set flag properly: got %d, want 1", p.flag);
} }
static void queueOrderThread(void *data) static void queueOrderThread(void *data)
{ {
uint32_t *flag = (uint32_t *) data; struct testParams *p = (struct testParams *) data;
// TODO error check p->err = timerSleep(1250 * timerMillisecond);
timerSleep(1250 * timerMillisecond); queueOrder(p);
queueOrder(flag);
} }
testingTest(QueueMain_DifferentThreadSequence) testingTest(QueueMain_DifferentThreadSequence)
{ {
threadThread *thread; threadThread *thread;
uint32_t flag = 1; // make sure it's initialized just in case threadSysError err;
struct testParams p;
// TODO check errors memset(&p, 0, sizeof (struct testParams));
threadNewThread(queueOrderThread, &flag, &thread); p.flag = 1; // make sure it's initialized just in case
err = threadNewThread(queueOrderThread, &p, &thread);
if (err != 0)
testingTFatalf(t, "error creating thread: " threadSysErrorFmt, threadSysErrorFmtArg(err));
timeout_uiMain(t, 5 * timerSecond); timeout_uiMain(t, 5 * timerSecond);
threadThreadWaitAndFree(thread); err = threadThreadWaitAndFree(thread);
checkOrder(t, flag); if (err != 0)
testingTFatalf(t, "error waiting for thread to finish: " threadSysErrorFmt, threadSysErrorFmtArg(err));
if (p.err != 0)
testingTErrorf(t, "error sleeping in thread to ensure a high likelihood the uiQueueMain() is run after uiMain() starts: " timerSysErrorFmt, timerSysErrorFmtArg(p.err));
checkOrder(t, p.flag);
} }
#if 0 #if 0