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

44
9unit.h
View File

@ -19,35 +19,55 @@
*/
// 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 passed; // number of successful tests
int failed; // number of failed 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
typedef enum TestResult
{
test_success,
test_failure,
test_postponed
test_success, // the test succeeded
test_failure, // the test failed
test_postponed // the test was postponed
} TestResult;
// Initializes a TestState value
extern void initTestState(TestState *);
// Runs a single test
extern void runTest(
extern void run_test(
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
extern void runTests(
// function that runs the tests and updates the provided TestState
extern void run_tests(
// runs the tests and updates a provided 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