reorganized functions and such

This commit is contained in:
jlamothe
2023-11-19 23:48:11 +00:00
parent 55926ec0d8
commit f808eb4cb0
2 changed files with 68 additions and 68 deletions

98
9unit.c
View File

@@ -87,11 +87,11 @@ static void print_log(TestState *);
static void clear_log(TestState *);
static void reindex(TestState *);
static void report(const char *);
static TestResult run_test_with_test(TestState *);
static TestResult run_test_compare_test(TestState *, void *);
static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *);
static void restore_context(TestState *, ContextData *);
static TestResult run_test_with_test(TestState *);
static TestResult run_test_compare_test(TestState *, void *);
static void single_test_context_test(TestState *, void *);
static void test_context_with_test(TestState *);
static void single_test_context_with_test(TestState *, void *);
@@ -99,6 +99,53 @@ static void check_value_test(TestState *, void *);
// Public Functions
void
run_tests(void (*tests)(TestState *))
{
if (!tests) return;
TestState s;
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
printf("Tests run: %d\n", s.run);
printf("Tests passed: %d\n", s.passed);
printf("Tests failed: %d\n", s.failed);
printf("Tests pending: %d\n", s.pending);
clear_log(&s);
if (s.failed) exits("test(s) failed");
}
void
append_test_log(TestState *s, const char *msg)
{
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;
}
void
run_test(TestState *s, TestResult (*t)(TestState *))
{
@@ -164,53 +211,6 @@ run_test_compare(
run_test_with(s, run_test_compare_test, &d);
}
void
run_tests(void (*tests)(TestState *))
{
if (!tests) return;
TestState s;
memset(&s, 0, sizeof(TestState));
s.report = report;
(*tests)(&s);
print_log(&s);
printf("Tests run: %d\n", s.run);
printf("Tests passed: %d\n", s.passed);
printf("Tests failed: %d\n", s.failed);
printf("Tests pending: %d\n", s.pending);
clear_log(&s);
if (s.failed) exits("test(s) failed");
}
void
append_test_log(TestState *s, const char *msg)
{
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;
}
void
test_context(
TestState *s,