From 9f4a51d8c71bf2d62dcd1e4cd4f7f7c4d6fd275a Mon Sep 17 00:00:00 2001 From: jlamothe Date: Sat, 11 Nov 2023 21:36:05 +0000 Subject: [PATCH] ensure first_log isn't null after append --- test/append-test-log.c | 11 ++++++++++- test/util.c | 34 ++++++++++++++++++++++++++++++++-- test/util.h | 12 ++++++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/test/append-test-log.c b/test/append-test-log.c index 51d471e..0aa0908 100644 --- a/test/append-test-log.c +++ b/test/append-test-log.c @@ -42,12 +42,21 @@ test_append_test_log(TestState *s) // Local Functions static void -append_to_empty(TestState *) +append_to_empty(TestState *s) { TestState test; print("\tappend to empty\n"); mk_sample_state(&test); append_test_log(&test, "foo"); + + // first_log shouldn't be null + print("\t\tfirst_log\n"); + chk_ptr_ne( + s, + "ERROR: append_test_log(): append to empty: first_log:", + test.first_log, + 0 + ); } //jl diff --git a/test/util.c b/test/util.c index a0c550b..ff329d3 100644 --- a/test/util.c +++ b/test/util.c @@ -110,8 +110,9 @@ static void chk_TestState_ptr_eq( const TestState * ); -decl_test(chk_int_eq_test); -decl_test(chk_ptr_eq_test); +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 // Public Functions @@ -179,6 +180,24 @@ chk_ptr_eq( s->ptr = old_ptr; } +void +chk_ptr_ne( + TestState *s, + const char *context, + const void *actual, + const void *prohibited +) +{ + void *old_ptr = s->ptr; + ComparePtrs cp; + cp.context = context; + cp.chk_val = actual; + cp.ref_val = prohibited; + s->ptr = &cp; + run_test(s, chk_ptr_ne_test); + s->ptr = old_ptr; +} + // Local Functions static void @@ -319,4 +338,15 @@ def_test(chk_ptr_eq_test, s) return test_failure; } +def_test(chk_ptr_ne_test, s) +{ + const ComparePtrs *cp = s->ptr; + if (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, "\tcannot be: 0x%x", cp->ref_val); + append_test_log(s, str); + return test_failure; +} + //jl diff --git a/test/util.h b/test/util.h index 9187799..7b6c9ad 100644 --- a/test/util.h +++ b/test/util.h @@ -48,8 +48,16 @@ extern void chk_int_eq( extern void chk_ptr_eq( TestState *, const char *, // the error context - void *, // the actual value - void * // the expected value + const void *, // the actual value + const void * // the expected value +); + +// ensure two pointers are not equal +extern void chk_ptr_ne( + TestState *, + const char *, // the error context + const void *, // the actual value + const void * // the prohibited value ); //jl