2023-11-06 00:04:12 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
9unit
|
2023-11-07 14:48:41 -05:00
|
|
|
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2023-11-09 18:24:48 -05:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2023-11-09 18:24:48 -05:00
|
|
|
Lesser General Public License for more details.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
2023-11-09 18:24:48 -05:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>.
|
2023-11-06 00:04:12 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2023-11-06 13:59:19 -05:00
|
|
|
#include <u.h>
|
2023-11-06 18:23:18 -05:00
|
|
|
#include <libc.h>
|
2023-11-06 15:20:09 -05:00
|
|
|
#include <stdio.h>
|
2023-11-06 13:59:19 -05:00
|
|
|
|
|
|
|
#include "9unit.h"
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
// Internal Prototypes
|
|
|
|
|
2023-11-06 23:33:22 -05:00
|
|
|
static void init_TestState(TestState *);
|
|
|
|
static void print_log(TestState *);
|
|
|
|
static void clear_log(TestState *);
|
|
|
|
static void reindex(TestState *);
|
2023-11-06 18:23:18 -05:00
|
|
|
|
|
|
|
// Public Functions
|
2023-11-06 14:46:01 -05:00
|
|
|
|
|
|
|
void
|
2023-11-06 18:23:18 -05:00
|
|
|
run_test(TestState *s, TestResult (*t)(TestState *))
|
2023-11-06 14:46:01 -05:00
|
|
|
{
|
2023-11-11 20:25:55 -05:00
|
|
|
if (!s) return;
|
2023-11-06 14:46:01 -05:00
|
|
|
s->run++;
|
2023-11-11 20:25:55 -05:00
|
|
|
|
|
|
|
// a null test function is a pending test
|
|
|
|
if (!t)
|
|
|
|
{
|
|
|
|
s->pending++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
switch ((*t)(s))
|
2023-11-06 14:26:55 -05:00
|
|
|
{
|
2023-11-06 14:46:01 -05:00
|
|
|
case test_success:
|
2023-11-06 15:20:09 -05:00
|
|
|
s->passed++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
|
|
|
case test_failure:
|
2023-11-06 15:20:09 -05:00
|
|
|
s->failed++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
2023-11-07 18:44:53 -05:00
|
|
|
case test_pending:
|
|
|
|
s->pending++;
|
2023-11-06 14:46:01 -05:00
|
|
|
break;
|
2023-11-06 18:23:18 -05:00
|
|
|
default:
|
|
|
|
exits("test returned an invalid response");
|
2023-11-06 14:26:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 15:20:09 -05:00
|
|
|
void
|
2023-11-06 18:23:18 -05:00
|
|
|
run_tests(void (*tests)(TestState *))
|
2023-11-06 15:20:09 -05:00
|
|
|
{
|
2023-11-07 18:44:53 -05:00
|
|
|
if (!tests) return;
|
2023-11-06 15:20:09 -05:00
|
|
|
TestState s;
|
2023-11-06 18:23:18 -05:00
|
|
|
init_TestState(&s);
|
2023-11-06 15:20:09 -05:00
|
|
|
(*tests)(&s);
|
2023-11-07 14:36:54 -05:00
|
|
|
print_log(&s);
|
2023-11-06 15:20:09 -05:00
|
|
|
printf("Tests run: %d\n", s.run);
|
|
|
|
printf("Tests passed: %d\n", s.passed);
|
|
|
|
printf("Tests failed: %d\n", s.failed);
|
2023-11-07 18:44:53 -05:00
|
|
|
printf("Tests pending: %d\n", s.pending);
|
2023-11-06 23:33:22 -05:00
|
|
|
clear_log(&s);
|
2023-11-07 18:44:53 -05:00
|
|
|
if (s.failed) exits("test(s) failed");
|
2023-11-06 23:33:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-11-07 18:44:53 -05:00
|
|
|
append_test_log(TestState *s, const char *msg)
|
2023-11-06 23:33:22 -05:00
|
|
|
{
|
|
|
|
if (!(s && msg)) return;
|
|
|
|
|
|
|
|
// build a new entry:
|
|
|
|
TestLogEntry *entry = malloc(sizeof(TestLogEntry));
|
|
|
|
entry->text = malloc(strlen(msg) + 1);
|
|
|
|
strcpy(entry->text, msg);
|
|
|
|
entry->next = 0;
|
|
|
|
|
|
|
|
// add it to the list:
|
|
|
|
if (!s->last_log)
|
|
|
|
{
|
|
|
|
if (s->first_log) // no last entry but we have a first?
|
|
|
|
{
|
|
|
|
reindex(s);
|
|
|
|
s->last_log->next = entry;
|
|
|
|
}
|
|
|
|
else s->first_log = entry;
|
|
|
|
}
|
|
|
|
else // there's already a last entry
|
|
|
|
{
|
|
|
|
if (!s->first_log) // no first entry but we have a last?
|
|
|
|
reindex(s); // do our best to fix that
|
|
|
|
s->last_log->next = entry;
|
|
|
|
}
|
|
|
|
s->last_log = entry;
|
2023-11-06 15:20:09 -05:00
|
|
|
}
|
|
|
|
|
2023-11-06 18:23:18 -05:00
|
|
|
// Internal Functions
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_TestState(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
s->run = 0;
|
|
|
|
s->passed = 0;
|
|
|
|
s->failed = 0;
|
2023-11-07 18:44:53 -05:00
|
|
|
s->pending = 0;
|
2023-11-06 18:23:18 -05:00
|
|
|
s->first_log = 0;
|
|
|
|
s->last_log = 0;
|
2023-11-07 18:44:53 -05:00
|
|
|
s->ptr = 0;
|
2023-11-06 18:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-11-06 23:33:22 -05:00
|
|
|
static void
|
|
|
|
print_log(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
TestLogEntry *e = s->first_log;
|
|
|
|
while(e)
|
|
|
|
{
|
|
|
|
if(e->text) printf("%s\n", e->text);
|
|
|
|
else print("(empty message)\n");
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_log(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
if(s->last_log && !s->first_log) reindex(s); // fix if broken
|
|
|
|
TestLogEntry *e = s->first_log, *next;
|
|
|
|
s->first_log = 0;
|
|
|
|
s->last_log = 0;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
next = e->next;
|
|
|
|
free(e->text);
|
|
|
|
free(e);
|
|
|
|
e = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reindex(TestState *s)
|
|
|
|
{
|
|
|
|
if (!s) return;
|
|
|
|
if (s->first_log)
|
|
|
|
{
|
|
|
|
TestLogEntry *e = s->first_log;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
s->last_log = e;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (s->last_log) // we have a last log but no first?
|
|
|
|
{
|
|
|
|
s->first_log = s->last_log;
|
|
|
|
fprint(2, "potential memory leak in test log\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:04:12 -05:00
|
|
|
//jl
|