minor edits and testing of initial TestState value

This commit is contained in:
jlamothe 2023-11-07 23:44:53 +00:00
parent 9f26846a3d
commit e9ed464b60
3 changed files with 124 additions and 10 deletions

14
9unit.c
View File

@ -46,8 +46,8 @@ run_test(TestState *s, TestResult (*t)(TestState *))
case test_failure: case test_failure:
s->failed++; s->failed++;
break; break;
case test_postponed: case test_pending:
s->postponed++; s->pending++;
break; break;
default: default:
exits("test returned an invalid response"); exits("test returned an invalid response");
@ -57,7 +57,7 @@ run_test(TestState *s, TestResult (*t)(TestState *))
void void
run_tests(void (*tests)(TestState *)) run_tests(void (*tests)(TestState *))
{ {
if(!tests) return; if (!tests) return;
TestState s; TestState s;
init_TestState(&s); init_TestState(&s);
(*tests)(&s); (*tests)(&s);
@ -65,12 +65,13 @@ run_tests(void (*tests)(TestState *))
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 pending: %d\n", s.pending);
clear_log(&s); clear_log(&s);
if (s.failed) exits("test(s) failed");
} }
void void
append_log(TestState *s, const char *msg) append_test_log(TestState *s, const char *msg)
{ {
if (!(s && msg)) return; if (!(s && msg)) return;
@ -108,9 +109,10 @@ init_TestState(TestState *s)
s->run = 0; s->run = 0;
s->passed = 0; s->passed = 0;
s->failed = 0; s->failed = 0;
s->postponed = 0; s->pending = 0;
s->first_log = 0; s->first_log = 0;
s->last_log = 0; s->last_log = 0;
s->ptr = 0;
} }
static void static void

View File

@ -32,9 +32,10 @@ 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 pending; // number of pending tests
TestLogEntry *first_log; // the first log entry TestLogEntry *first_log; // the first log entry
TestLogEntry *last_log; //the last log entry TestLogEntry *last_log; //the last log entry
void *ptr; // used for passing data between tests
}; };
struct TestLogEntry struct TestLogEntry
@ -48,7 +49,7 @@ typedef enum TestResult
{ {
test_success, // the test succeeded test_success, // the test succeeded
test_failure, // the test failed test_failure, // the test failed
test_postponed // the test was postponed test_pending // the test is pending
} TestResult; } TestResult;
// Runs a single test // Runs a single test
@ -65,7 +66,7 @@ extern void run_tests(
// Adds an entry to the log that is displayed after the tests have // Adds an entry to the log that is displayed after the tests have
// completed // completed
extern void append_log( extern void append_test_log(
TestState *, // the current state TestState *, // the current state
const char * // the message to append const char * // the message to append
); );

View File

@ -21,11 +21,122 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include "../9unit.h"
#define decl_test(n) static TestResult n(TestState *)
#define def_test(n, s) static TestResult n(TestState *s)
// Internal Prototypes
static void test(TestState *);
static void test_initial_state(TestState *);
static void tests(TestState *);
decl_test(initial_test_count);
decl_test(initial_pass_count);
decl_test(initial_fail_count);
decl_test(initial_pend_count);
decl_test(initial_first_log);
decl_test(initial_last_log);
decl_test(initial_ptr);
// Public Functions
void void
main() main()
{ {
print("Tests not yet implemented.\n"); run_tests(&tests);
exits(0); exits(0);
} }
// Internal Functions
static void
tests(TestState *s)
{
if (!s) exits("ERROR: no TestState");
// Make a copy of the initial TestState and store it
TestState scpy;
memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_initial_state(s);
}
static void
test_initial_state(TestState *s)
{
print("Testing initial state\n");
run_test(s, &initial_test_count);
run_test(s, &initial_pass_count);
run_test(s, &initial_fail_count);
run_test(s, &initial_pend_count);
run_test(s, &initial_first_log);
run_test(s, &initial_last_log);
run_test(s, &initial_ptr);
}
def_test(initial_test_count, s)
{
TestState *scpy = s->ptr;
print("\trun\n");
if (scpy->run == 0) return test_success;
append_test_log(s, "initial run was nonzero");
return test_failure;
}
def_test(initial_pass_count, s)
{
TestState *scpy = s->ptr;
print("\tpassed\n");
if (scpy->passed == 0) return test_success;
append_test_log(s, "initial passed was nonzero");
return test_failure;
}
def_test(initial_fail_count, s)
{
TestState *scpy = s->ptr;
print("\tfailed\n");
if (scpy->failed == 0) return test_success;
append_test_log(s, "initial failed was nonzero");
return test_failure;
}
def_test(initial_pend_count, s)
{
TestState *scpy = s->ptr;
print("\tpending\n");
if (scpy->pending == 0) return test_success;
append_test_log(s, "initial pending was nonzero");
return test_failure;
}
def_test(initial_first_log, s)
{
TestState *scpy = s->ptr;
print("\tfirst_log\n");
if (scpy->first_log == 0) return test_success;
append_test_log(s, "initial first_log was not null");
return test_failure;
}
def_test(initial_last_log, s)
{
TestState *scpy = s->ptr;
print("\tlast_log\n");
if (scpy->last_log == 0) return test_success;
append_test_log(s, "initial last_log was not null");
return test_failure;
}
def_test(initial_ptr, s)
{
TestState *scpy = s->ptr;
print("\tptr\n");
if (scpy->ptr == 0) return test_success;
append_test_log(s, "initial ptr was not null");
return test_failure;
}
//jl //jl