diff --git a/9unit.c b/9unit.c index cd10683..c0573fd 100644 --- a/9unit.c +++ b/9unit.c @@ -68,7 +68,7 @@ run_tests(void (*tests)(TestState *)) { if (!tests) return; TestState s; - init_TestState(&s); + memset(&s, 0, sizeof(TestState)); (*tests)(&s); print_log(&s); printf("Tests run: %d\n", s.run); @@ -111,19 +111,6 @@ append_test_log(TestState *s, const char *msg) // Internal Functions -static void -init_TestState(TestState *s) -{ - if (!s) return; - s->run = 0; - s->passed = 0; - s->failed = 0; - s->pending = 0; - s->first_log = 0; - s->last_log = 0; - s->ptr = 0; -} - static void print_log(TestState *s) { diff --git a/9unit.h b/9unit.h index 9be3a0c..7b1b2d1 100644 --- a/9unit.h +++ b/9unit.h @@ -37,6 +37,9 @@ struct TestState TestLogEntry *first_log; // the first log entry TestLogEntry *last_log; //the last log entry void *ptr; // used for passing data between tests + const char *context; // immediate context of current test + const char *full_context; // full context of current test + int depth; // how many tests "deep" are we? }; struct TestLogEntry diff --git a/test/initial-state.c b/test/initial-state.c index 0320837..b6c3602 100644 --- a/test/initial-state.c +++ b/test/initial-state.c @@ -36,6 +36,9 @@ decl_test(initial_pend_count); decl_test(initial_first_log); decl_test(initial_last_log); decl_test(initial_ptr); +decl_test(initial_context); +decl_test(initial_full_context); +decl_test(initial_depth); // Public Functions @@ -50,6 +53,9 @@ test_initial_state(TestState *s) run_test(s, initial_first_log); run_test(s, initial_last_log); run_test(s, initial_ptr); + run_test(s, initial_context); + run_test(s, initial_full_context); + run_test(s, initial_depth); } // Internal Functions @@ -117,4 +123,31 @@ def_test(initial_ptr, s) return test_failure; } +def_test(initial_context, s) +{ + TestState *scpy = s->ptr; + print("\tcontext\n"); + if (scpy->context == 0) return test_success; + append_test_log(s, "initial context was not null"); + return test_failure; +} + +def_test(initial_full_context, s) +{ + TestState *scpy = s->ptr; + print("\tfull_context\n"); + if (scpy->context == 0) return test_success; + append_test_log(s, "initial full_context was not null"); + return test_failure; +} + +def_test(initial_depth, s) +{ + TestState *scpy = s->ptr; + print("\tdepth\n"); + if (scpy->depth == 0) return test_success; + append_test_log(s, "initial depth was not zero"); + return test_failure; +} + //jl diff --git a/test/util.c b/test/util.c index 26a8702..aad296d 100644 --- a/test/util.c +++ b/test/util.c @@ -110,6 +110,33 @@ static void chk_TestState_ptr_eq( const TestState * ); +// compare the context value of two states +static void chk_TestState_context_eq( + TestState *, + const char *, + const char *, + const TestState *, + const TestState * +); + +// compare the full_context value of two states +static void chk_TestState_full_context_eq( + TestState *, + const char *, + const char *, + const TestState *, + const TestState * +); + +// compare the depth value of two states +static void chk_TestState_depth_eq( + TestState *, + const char *, + const char *, + const TestState *, + const TestState * +); + decl_test(chk_int_eq_test); // ensure ints are equal decl_test(chk_ptr_eq_test); // ensure pointers are equal decl_test(chk_ptr_ne_test); // ensure pointers are not equal @@ -143,6 +170,9 @@ chk_TestState_eq( chk_TestState_first_log_eq(s, prefix, context, actual, expected); chk_TestState_last_log_eq(s, prefix, context, actual, expected); chk_TestState_ptr_eq(s, prefix, context, actual, expected); + chk_TestState_context_eq(s, prefix, context, actual, expected); + chk_TestState_full_context_eq(s, prefix, context, actual, expected); + chk_TestState_depth_eq(s, prefix, context, actual, expected); } void @@ -331,6 +361,54 @@ chk_TestState_ptr_eq( chk_ptr_eq(s, full_context, actual->ptr, expected->ptr); } +static void +chk_TestState_context_eq( + TestState *s, + const char *prefix, + const char *context, + const TestState *actual, + const TestState *expected +) +{ + char full_context[STR_BUF_SIZE]; + print(prefix); + print("context\n"); + snprintf(full_context, STR_BUF_SIZE, "%s context:", context); + chk_str_eq(s, full_context, actual->context, expected->context); +} + +static void +chk_TestState_full_context_eq( + TestState *s, + const char *prefix, + const char *context, + const TestState *actual, + const TestState *expected +) +{ + char full_context[STR_BUF_SIZE]; + print(prefix); + print("full_context\n"); + snprintf(full_context, STR_BUF_SIZE, "%s full_context:", context); + chk_str_eq(s, full_context, actual->full_context, expected->full_context); +} + +static void +chk_TestState_depth_eq( + TestState *s, + const char *prefix, + const char *context, + const TestState *actual, + const TestState *expected +) +{ + char full_context[STR_BUF_SIZE]; + print(prefix); + print("depth\n"); + snprintf(full_context, STR_BUF_SIZE, "%s depth:", context); + chk_int_eq(s, full_context, actual->depth, expected->depth); +} + def_test(chk_int_eq_test, s) { const CompareInts *ci = s->ptr; @@ -371,6 +449,7 @@ def_test(chk_ptr_ne_test, s) def_test(chk_str_eq_test, s) { const ComparePtrs *cp = s->ptr; + if (!cp->chk_val && !cp->ref_val) return test_success; if (!strcmp(cp->chk_val, cp->ref_val)) return test_success; char str[STR_BUF_SIZE]; append_test_log(s, cp->context);