Compare commits

..

2 Commits

Author SHA1 Message Date
jlamothe 55926ec0d8 implemented run_test_compare() 2023-11-19 17:19:45 -05:00
jlamothe 01e01243dd cleaned up comments 2023-11-19 17:19:45 -05:00
2 changed files with 58 additions and 10 deletions

35
9unit.c
View File

@ -28,6 +28,7 @@
// Internal Types // Internal Types
typedef struct RunTestWith RunTestWith; typedef struct RunTestWith RunTestWith;
typedef struct RunTestCompare RunTestCompare;
typedef struct ContextData ContextData; typedef struct ContextData ContextData;
typedef struct TestContextWith TestContextWith; typedef struct TestContextWith TestContextWith;
typedef struct SingleTestContextWith SingleTestContextWith; typedef struct SingleTestContextWith SingleTestContextWith;
@ -41,6 +42,14 @@ struct RunTestWith
void *val; // the value passed in 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 struct ContextData
{ {
const char *old_c; // previous context const char *old_c; // previous context
@ -79,6 +88,7 @@ static void clear_log(TestState *);
static void reindex(TestState *); static void reindex(TestState *);
static void report(const char *); static void report(const char *);
static TestResult run_test_with_test(TestState *); static TestResult run_test_with_test(TestState *);
static TestResult run_test_compare_test(TestState *, void *);
static void build_new_context(TestState *, ContextData *); static void build_new_context(TestState *, ContextData *);
static void display_context(TestState *); static void display_context(TestState *);
static void restore_context(TestState *, ContextData *); static void restore_context(TestState *, ContextData *);
@ -139,6 +149,21 @@ run_test_with(
run_test(s, run_test_with_test); 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 void
run_tests(void (*tests)(TestState *)) run_tests(void (*tests)(TestState *))
{ {
@ -373,6 +398,16 @@ run_test_with_test(TestState *s)
return (*d->test)(s, d->val); 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 static void
test_context_with_test(TestState *s) test_context_with_test(TestState *s)
{ {

33
9unit.h
View File

@ -21,15 +21,14 @@
// Types & Structs // Types & Structs
// Tracks information about the tests being run.
typedef struct TestState TestState;
// Defines a log entry in a TestState struct
typedef struct TestLogEntry TestLogEntry;
// The following structures will typically be maintained by the // The following structures will typically be maintained by the
// testing framework. You shouldn't need to concern yourself with // testing framework. You shouldn't need to concern yourself with
// them. // them.
typedef struct TestState TestState;
typedef struct TestLogEntry TestLogEntry;
// Tracks information about the tests being run.
struct TestState struct TestState
{ {
int run; // number of tests run int run; // number of tests run
@ -45,6 +44,7 @@ struct TestState
void (*report)(const char *); // prints a string immediately void (*report)(const char *); // prints a string immediately
}; };
// Defines a log entry in a TestState struct
struct TestLogEntry struct TestLogEntry
{ {
char *text; // the entry text char *text; // the entry text
@ -74,12 +74,25 @@ extern void run_test_with(
void * // the value to pass in void * // the value to pass in
); );
// Runs multiple tests, displaying a summary at the end // Runs a single test passing in two values to be compared
extern void run_tests( extern void run_test_compare(
// runs the tests and updates a provided TestState TestState *, // the current state
void (*)(TestState *)
// 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 *));
// Adds an entry to the log that is displayed after the tests have // Adds an entry to the log that is displayed after the tests have
// completed // completed
extern void append_test_log( extern void append_test_log(