From e9ed464b60e05a6239f91623937487e2eab79150 Mon Sep 17 00:00:00 2001 From: jlamothe Date: Tue, 7 Nov 2023 23:44:53 +0000 Subject: [PATCH] minor edits and testing of initial TestState value --- 9unit.c | 14 ++++--- 9unit.h | 7 ++-- test/tests.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 10 deletions(-) diff --git a/9unit.c b/9unit.c index 0ce0f32..3003314 100644 --- a/9unit.c +++ b/9unit.c @@ -46,8 +46,8 @@ run_test(TestState *s, TestResult (*t)(TestState *)) case test_failure: s->failed++; break; - case test_postponed: - s->postponed++; + case test_pending: + s->pending++; break; default: exits("test returned an invalid response"); @@ -57,7 +57,7 @@ run_test(TestState *s, TestResult (*t)(TestState *)) void run_tests(void (*tests)(TestState *)) { - if(!tests) return; + if (!tests) return; TestState s; init_TestState(&s); (*tests)(&s); @@ -65,12 +65,13 @@ run_tests(void (*tests)(TestState *)) 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); + printf("Tests pending: %d\n", s.pending); clear_log(&s); + if (s.failed) exits("test(s) failed"); } void -append_log(TestState *s, const char *msg) +append_test_log(TestState *s, const char *msg) { if (!(s && msg)) return; @@ -108,9 +109,10 @@ init_TestState(TestState *s) s->run = 0; s->passed = 0; s->failed = 0; - s->postponed = 0; + s->pending = 0; s->first_log = 0; s->last_log = 0; + s->ptr = 0; } static void diff --git a/9unit.h b/9unit.h index fa51a7d..bb96137 100644 --- a/9unit.h +++ b/9unit.h @@ -32,9 +32,10 @@ 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 + int pending; // number of pending tests TestLogEntry *first_log; // the first log entry TestLogEntry *last_log; //the last log entry + void *ptr; // used for passing data between tests }; struct TestLogEntry @@ -48,7 +49,7 @@ typedef enum TestResult { test_success, // the test succeeded test_failure, // the test failed - test_postponed // the test was postponed + test_pending // the test is pending } TestResult; // 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 // completed -extern void append_log( +extern void append_test_log( TestState *, // the current state const char * // the message to append ); diff --git a/test/tests.c b/test/tests.c index ac48511..f538b58 100644 --- a/test/tests.c +++ b/test/tests.c @@ -21,11 +21,122 @@ #include #include +#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 main() { - print("Tests not yet implemented.\n"); + run_tests(&tests); 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