Reorganized test sets into growing arrays. We can sort them next.

This commit is contained in:
Pietro Gagliardi 2019-04-17 22:12:32 -04:00
parent dcf34e6dab
commit 16c6425200
1 changed files with 44 additions and 41 deletions

View File

@ -15,69 +15,68 @@ struct defer {
struct testingT { struct testingT {
const char *name; const char *name;
void (*f)(testingT *); void (*f)(testingT *);
const char *file;
long line;
int failed; int failed;
int skipped; int skipped;
int returned; int returned;
jmp_buf returnNowBuf; jmp_buf returnNowBuf;
struct defer *defers; struct defer *defers;
int defersRun; int defersRun;
testingT *next;
}; };
static testingT *tests = NULL; static void initTest(testingT *t, const char *name, void (*f)(testingT *), const char *file, long line)
static testingT *testsTail = NULL;
static testingT *testsBefore = NULL;
static testingT *testsBeforeTail = NULL;
static testingT *testsAfter = NULL;
static testingT *testsAfterTail = NULL;
static testingT *newTest(const char *name, void (*f)(testingT *), const char *file, long line, testingT *prev)
{ {
testingT *t;
printf("%s %s %ld\n", name, file, line);
t = testingprivNew(testingT);
t->name = name; t->name = name;
t->f = f; t->f = f;
t->file = file;
t->line = line;
t->failed = 0; t->failed = 0;
t->skipped = 0; t->skipped = 0;
t->returned = 0; t->returned = 0;
t->defers = NULL; t->defers = NULL;
t->defersRun = 0; t->defersRun = 0;
t->next = NULL; }
if (prev != NULL)
prev->next = t; #define nGrow 32
return t;
struct testset {
testingT *tests;
size_t len;
size_t cap;
};
static struct testset tests = { NULL, 0, 0 };
static struct testset testsBefore = { NULL, 0, 0 };
static struct testset testsAfter = { NULL, 0, 0 };
static void testsetAdd(struct testset *set, const char *name, void (*f)(testingT *), const char *file, long line)
{
if (set->len == set->cap) {
testingT *newbuf;
set->cap += nGrow;
newbuf = (testingT *) realloc(set->tests, set->cap * sizeof (testingT));
// TODO abort if newbuf is NULL
set->tests = newbuf;
}
initTest(set->tests + set->len, name, f, file, line);
set->len++;
} }
void testingprivRegisterTest(const char *name, void (*f)(testingT *), const char *file, long line) void testingprivRegisterTest(const char *name, void (*f)(testingT *), const char *file, long line)
{ {
testingT *t; testsetAdd(&tests, name, f, file, line);
t = newTest(name, f, file, line, testsTail);
testsTail = t;
if (tests == NULL)
tests = t;
} }
void testingprivRegisterTestBefore(const char *name, void (*f)(testingT *), const char *file, long line) void testingprivRegisterTestBefore(const char *name, void (*f)(testingT *), const char *file, long line)
{ {
testingT *t; testsetAdd(&testsBefore, name, f, file, line);
t = newTest(name, f, file, line, testsBeforeTail);
testsBeforeTail = t;
if (testsBefore == NULL)
testsBefore = t;
} }
void testingprivRegisterTestAfter(const char *name, void (*f)(testingT *), const char *file, long line) void testingprivRegisterTestAfter(const char *name, void (*f)(testingT *), const char *file, long line)
{ {
testingT *t; testsetAdd(&testsAfter, name, f, file, line);
t = newTest(name, f, file, line, testsAfterTail);
testsAfterTail = t;
if (testsAfter == NULL)
testsAfter = t;
} }
static void runDefers(testingT *t) static void runDefers(testingT *t)
@ -91,11 +90,14 @@ static void runDefers(testingT *t)
(*(d->f))(t, d->data); (*(d->f))(t, d->data);
} }
static void runTestSet(testingT *t, int *anyFailed) static void testsetRun(struct testset *set, int *anyFailed)
{ {
size_t i;
testingT *t;
const char *status; const char *status;
for (; t != NULL; t = t->next) { t = set->tests;
for (i = 0; i < set->len; i++) {
printf("=== RUN %s\n", t->name); printf("=== RUN %s\n", t->name);
if (setjmp(t->returnNowBuf) == 0) if (setjmp(t->returnNowBuf) == 0)
(*(t->f))(t); (*(t->f))(t);
@ -109,6 +111,7 @@ static void runTestSet(testingT *t, int *anyFailed)
// note that failed overrides skipped // note that failed overrides skipped
status = "SKIP"; status = "SKIP";
printf("--- %s: %s (%s)\n", status, t->name, "TODO"); printf("--- %s: %s (%s)\n", status, t->name, "TODO");
t++;
} }
} }
@ -117,16 +120,16 @@ int testingMain(void)
int anyFailed; int anyFailed;
// TODO see if this should run if all tests are skipped // TODO see if this should run if all tests are skipped
if (tests == NULL) { if ((testsBefore.len + tests.len + testsAfter.len) == 0) {
fprintf(stderr, "warning: no tests to run\n"); fprintf(stderr, "warning: no tests to run\n");
// imitate Go here (TODO confirm this) // imitate Go here (TODO confirm this)
return 0; return 0;
} }
anyFailed = 0; anyFailed = 0;
runTestSet(testsBefore, &anyFailed); testsetRun(&testsBefore, &anyFailed);
runTestSet(tests, &anyFailed); testsetRun(&tests, &anyFailed);
runTestSet(testsAfter, &anyFailed); testsetRun(&testsAfter, &anyFailed);
if (anyFailed) { if (anyFailed) {
printf("FAIL\n"); printf("FAIL\n");
return 1; return 1;