From 55926ec0d8e5c6190b87c99e067f46fa51d2868a Mon Sep 17 00:00:00 2001 From: jlamothe Date: Sun, 19 Nov 2023 22:14:21 +0000 Subject: [PATCH] implemented run_test_compare() --- 9unit.c | 35 +++++++++++++++++++++++++++++++++++ 9unit.h | 15 +++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/9unit.c b/9unit.c index ace5fe5..2bd2ab5 100644 --- a/9unit.c +++ b/9unit.c @@ -28,6 +28,7 @@ // Internal Types typedef struct RunTestWith RunTestWith; +typedef struct RunTestCompare RunTestCompare; typedef struct ContextData ContextData; typedef struct TestContextWith TestContextWith; typedef struct SingleTestContextWith SingleTestContextWith; @@ -41,6 +42,14 @@ struct RunTestWith void *val; // the value passed in }; +// data needed by run_test_compare() +struct RunTestCompare +{ + TestResult (*test)(TestState *, void *, void *); + void *val1; + void *val2; +}; + struct ContextData { const char *old_c; // previous context @@ -79,6 +88,7 @@ static void clear_log(TestState *); static void reindex(TestState *); static void report(const char *); static TestResult run_test_with_test(TestState *); +static TestResult run_test_compare_test(TestState *, void *); static void build_new_context(TestState *, ContextData *); static void display_context(TestState *); static void restore_context(TestState *, ContextData *); @@ -139,6 +149,21 @@ run_test_with( run_test(s, run_test_with_test); } +void +run_test_compare( + TestState *s, + TestResult (*test)(TestState *, void *, void *), + void *val1, + void *val2 +) +{ + RunTestCompare d; + d.test = test; + d.val1 = val1; + d.val2 = val2; + run_test_with(s, run_test_compare_test, &d); +} + void run_tests(void (*tests)(TestState *)) { @@ -373,6 +398,16 @@ run_test_with_test(TestState *s) return (*d->test)(s, d->val); } +static TestResult +run_test_compare_test( + TestState *s, + void *ptr +) +{ + RunTestCompare *d = ptr; + return (*d->test)(s, d->val1, d->val2); +} + static void test_context_with_test(TestState *s) { diff --git a/9unit.h b/9unit.h index a8f4056..3816498 100644 --- a/9unit.h +++ b/9unit.h @@ -74,6 +74,21 @@ extern void run_test_with( void * // the value to pass in ); +// Runs a single test passing in two values to be compared +extern void run_test_compare( + TestState *, // the current state + + // the test to be run + TestResult (*test)( + TestState *, // the current state + void *, // the first value + void * // the second value + ), + + void *, // the first value + void * // the second value +); + // Creates an initial TestState, passes it to the supplied function, // and displays the resulting log and summary extern void run_tests(void (*)(TestState *));