From 3393c3b9affb1d1056c7a80948e05b24613676f7 Mon Sep 17 00:00:00 2001 From: jlamothe Date: Sat, 11 Nov 2023 22:53:04 +0000 Subject: [PATCH] ensure latest log text is correct when appended --- test/append-test-log.c | 9 +++++++++ test/util.c | 32 ++++++++++++++++++++++++++++++++ test/util.h | 8 ++++++++ 3 files changed, 49 insertions(+) diff --git a/test/append-test-log.c b/test/append-test-log.c index b6a1d34..99f476d 100644 --- a/test/append-test-log.c +++ b/test/append-test-log.c @@ -58,6 +58,15 @@ append_to_empty(TestState *s) 0 ); + // log should say "foo" + print("\t\t\ttext\n"); + chk_str_eq( + s, + "ERROR: append_test_log(): append to empty: first_log: text:", + test.first_log->text, + "foo" + ); + // last_log should match first_log print("\t\tlast_log\n"); chk_ptr_eq( diff --git a/test/util.c b/test/util.c index ff329d3..26a8702 100644 --- a/test/util.c +++ b/test/util.c @@ -113,6 +113,7 @@ static void chk_TestState_ptr_eq( 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 +decl_test(chk_str_eq_test); // ensure strings are equal // Public Functions @@ -198,6 +199,24 @@ chk_ptr_ne( s->ptr = old_ptr; } +void +chk_str_eq( + TestState *s, + const char *context, + const char *actual, + const char *expected +) +{ + void *old_ptr = s->ptr; + ComparePtrs cp; + cp.context = context; + cp.chk_val = actual; + cp.ref_val = expected; + s->ptr = &cp; + run_test(s, chk_str_eq_test); + s->ptr = old_ptr; +} + // Local Functions static void @@ -349,4 +368,17 @@ def_test(chk_ptr_ne_test, s) return test_failure; } +def_test(chk_str_eq_test, s) +{ + const ComparePtrs *cp = s->ptr; + if (!strcmp(cp->chk_val, cp->ref_val)) return test_success; + char str[STR_BUF_SIZE]; + append_test_log(s, cp->context); + snprintf(str, STR_BUF_SIZE, "\texpected: %s", cp->ref_val); + append_test_log(s, str); + snprintf(str, STR_BUF_SIZE, "\tactual: %s", cp->ref_val); + append_test_log(s, str); + return test_failure; +} + //jl diff --git a/test/util.h b/test/util.h index 7b6c9ad..8ab30e9 100644 --- a/test/util.h +++ b/test/util.h @@ -60,4 +60,12 @@ extern void chk_ptr_ne( const void * // the prohibited value ); +// ensure two strings are equal +extern void chk_str_eq( + TestState *, + const char *, // the error context + const char *, // the actual value + const char * // the expected value +); + //jl