Compare commits

...

2 Commits

Author SHA1 Message Date
jlamothe 3900a39205 implemented append_log() and some internal maintenance stuff 2023-11-07 04:33:22 +00:00
jlamothe 870f1b619b restructured library to hide struct initialization 2023-11-06 23:23:18 +00:00
2 changed files with 143 additions and 25 deletions

124
9unit.c
View File

@ -19,26 +19,26 @@
*/ */
#include <u.h> #include <u.h>
#include <libc.h>
#include <stdio.h> #include <stdio.h>
#include "9unit.h" #include "9unit.h"
void // Internal Prototypes
initTestState(TestState *s)
{ static void init_TestState(TestState *);
if (!s) return; static void print_log(TestState *);
s->run = 0; static void clear_log(TestState *);
s->passed = 0; static void reindex(TestState *);
s->failed = 0;
s->postponed = 0; // Public Functions
}
void void
runTest(TestState *s, TestResult (*t)(void)) run_test(TestState *s, TestResult (*t)(TestState *))
{ {
if (!(s && t)) return; if (!(s && t)) return;
s->run++; s->run++;
switch ((*t)()) switch ((*t)(s))
{ {
case test_success: case test_success:
s->passed++; s->passed++;
@ -49,20 +49,118 @@ runTest(TestState *s, TestResult (*t)(void))
case test_postponed: case test_postponed:
s->postponed++; s->postponed++;
break; break;
default:
exits("test returned an invalid response");
} }
} }
void void
runTests(void (*tests)(TestState *)) run_tests(void (*tests)(TestState *))
{ {
if(!tests) return; if(!tests) return;
TestState s; TestState s;
initTestState(&s); init_TestState(&s);
(*tests)(&s); (*tests)(&s);
printf("Tests run: %d\n", s.run); printf("Tests run: %d\n", s.run);
printf("Tests passed: %d\n", s.passed); printf("Tests passed: %d\n", s.passed);
printf("Tests failed: %d\n", s.failed); printf("Tests failed: %d\n", s.failed);
printf("Tests postponed: %d\n", s.postponed); printf("Tests postponed: %d\n", s.postponed);
print_log(&s);
clear_log(&s);
}
void
append_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;
}
// Internal Functions
static void
init_TestState(TestState *s)
{
if (!s) return;
s->run = 0;
s->passed = 0;
s->failed = 0;
s->postponed = 0;
s->first_log = 0;
s->last_log = 0;
}
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");
}
} }
//jl //jl

44
9unit.h
View File

@ -19,35 +19,55 @@
*/ */
// Tracks information about the tests being run. // Tracks information about the tests being run.
typedef struct TestState typedef struct TestState TestState;
// Defines a log entry in a TestState struct
typedef struct TestLogEntry TestLogEntry;
// The following structures will typically be maintained by the
// testing framework. You shouldn't need to concern yourself with
// them.
struct TestState
{ {
int run; // number of tests run int run; // number of tests run
int passed; // number of successful tests int passed; // number of successful tests
int failed; // number of failed tests int failed; // number of failed tests
int postponed; // number of postponed tests int postponed; // number of postponed tests
} TestState; TestLogEntry *first_log; // the first log entry
TestLogEntry *last_log; //the last log entry
};
struct TestLogEntry
{
char *text; // the entry text
TestLogEntry *next; // points to the next entry
};
// Possible results of running a single test // Possible results of running a single test
typedef enum TestResult typedef enum TestResult
{ {
test_success, test_success, // the test succeeded
test_failure, test_failure, // the test failed
test_postponed test_postponed // the test was postponed
} TestResult; } TestResult;
// Initializes a TestState value
extern void initTestState(TestState *);
// Runs a single test // Runs a single test
extern void runTest( extern void run_test(
TestState *, // the TestState data TestState *, // the TestState data
TestResult (*)(void) // the test to run TestResult (*)(TestState *) // the test to run
); );
// Runs multiple tests, displaying a summary at the end // Runs multiple tests, displaying a summary at the end
extern void runTests( extern void run_tests(
// function that runs the tests and updates the provided TestState // runs the tests and updates a provided TestState
void (*)(TestState *) void (*)(TestState *)
); );
// Adds an entry to the log that is displayed after the tests have
// completed
extern void append_log(
TestState *, // the current state
const char * // the message to append
);
//jl //jl