Compare commits

...

5 Commits

Author SHA1 Message Date
jlamothe 6c191d8405 implemented run_test_with() 2023-11-17 23:35:02 +00:00
jlamothe 37ce34b160 made check_value() use test_context_with()
eliminates some redundant code
2023-11-17 23:07:05 +00:00
jlamothe 5d67b7aeb8 implemented test_context_with() 2023-11-17 21:30:52 +00:00
jlamothe 7818e3d646 changed order of params for check_value() 2023-11-16 22:41:42 +00:00
jlamothe a7351b5667 implemented check_value() 2023-11-16 19:53:06 +00:00
2 changed files with 132 additions and 3 deletions

104
9unit.c
View File

@ -27,11 +27,18 @@
// Internal Types
// data required by the context management functions
typedef struct RunTestWith RunTestWith;
typedef struct ContextData ContextData;
// data required to run a single test with a label
typedef struct SingleTestContext SingleTestContext;
typedef struct TestContextWith TestContextWith;
typedef struct CheckValue CheckValue;
struct RunTestWith
{
void *ptr; // state's prevoius ptr value
TestResult (*test)(TestState *, void *); // the test
void *val; // the value being passed in
};
struct ContextData
{
@ -47,6 +54,20 @@ struct SingleTestContext
TestResult (*test)(TestState *); // the test to run
};
struct TestContextWith
{
void *ptr; // state's original ptr value
void *val; // the value being passed in
void (*test)(TestState *, void *); // the test function
};
struct CheckValue
{
void *chk_val; // the value being checked
void *ref_val; // the reference value
void (*test)(TestState *, void *, void *); // the test
};
// Internal Prototypes
static void init_TestState(TestState *);
@ -54,10 +75,13 @@ static void print_log(TestState *);
static void clear_log(TestState *);
static void reindex(TestState *);
static void report(const char *);
static TestResult run_test_with_test(TestState *);
static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *);
static void restore_context(TestState *, ContextData *);
static void run_single_test_context(TestState *);
static void test_context_with_test(TestState *);
static void check_value_test(TestState *, void *);
// Public Functions
@ -90,6 +114,23 @@ run_test(TestState *s, TestResult (*t)(TestState *))
}
}
void
run_test_with(
TestState *s, // the state
TestResult (*test)(TestState *, void *), // the test
void *val // the value being passed in
)
{
if (!s) return;
if (!test) run_test(s, 0);
RunTestWith d;
d.ptr = s->ptr;
d.test = test;
d.val = val;
s->ptr = &d;
run_test(s, run_test_with_test);
}
void
run_tests(void (*tests)(TestState *))
{
@ -172,6 +213,40 @@ single_test_context(
test_context(s, label, run_single_test_context);
}
void
test_context_with(
TestState *s,
const char *context,
void (*test)(TestState *, void *),
void *val
)
{
if (!(s && test)) return;
TestContextWith d;
d.ptr = s->ptr;
d.val = val;
d.test = test;
s->ptr = &d;
test_context(s, context, test_context_with_test);
}
void
check_value(
TestState *s,
const char *context,
void (*test)(TestState *, void *, void *),
void *chk_val,
void *ref_val
)
{
if (!(s && test)) return;
CheckValue d;
d.chk_val = chk_val;
d.ref_val = ref_val;
d.test = test;
test_context_with(s, context, check_value_test, &d);
}
// Internal Functions
static void
@ -230,6 +305,14 @@ report(const char *str)
print("%s", str);
}
static TestResult
run_test_with_test(TestState *s)
{
RunTestWith *d = s->ptr;
s->ptr = d->ptr;
return (*d->test)(s, d->val);
}
static void
build_new_context(TestState *s, ContextData *cd)
{
@ -276,4 +359,19 @@ run_single_test_context(TestState *s)
run_test(s, stc->test);
}
static void
test_context_with_test(TestState *s)
{
TestContextWith *d = s->ptr;
s->ptr = d->ptr;
(*d->test)(s, d->val);
}
static void
check_value_test(TestState *s, void *ptr)
{
CheckValue *d = ptr;
(*d->test)(s, d->chk_val, d->ref_val);
}
//jl

31
9unit.h
View File

@ -67,6 +67,13 @@ extern void run_test(
TestResult (*)(TestState *) // the test to run
);
// Runs a single test with an arbitrary input
extern void run_test_with(
TestState *, // the state
TestResult (*)(TestState *, void *), // the test
void * // the value to pass in
);
// Runs multiple tests, displaying a summary at the end
extern void run_tests(
// runs the tests and updates a provided TestState
@ -94,4 +101,28 @@ extern void single_test_context(
TestResult (*)(TestState *) // the actual test
);
// Runs a test with a context and an additional input
extern void test_context_with(
TestState *, // the current state
const char *, // a description of the context
void (*)(TestState *, void *), // the test function
void * // the value being passed in
);
// Runs a test to check a value
extern void check_value(
TestState *, // the current test state
const char *, // a description of the context
// the test function
void (*)(
TestState *,
void *, // the check value
void * // the reference value
),
void *, // the value being checked
void * // the reference value
);
//jl