re-implemented test using new convenience functions

This commit is contained in:
jlamothe 2023-11-21 05:12:41 +00:00
commit 57e4d2ba49
7 changed files with 261 additions and 706 deletions

View File

@ -21,7 +21,6 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include <stdio.h>
#include "../9unit.h" #include "../9unit.h"
#include "util.h" #include "util.h"
@ -44,30 +43,30 @@ static void append_to_existing(TestState *);
static void missing_last(TestState *); static void missing_last(TestState *);
static void missing_first(TestState *); static void missing_first(TestState *);
static void null_message(TestState *); static void null_message(TestState *);
decl_test(null_state); static TestResult null_state(TestState *);
static void chk_last(TestState *, LogData *, const char *); static void chk_last(TestState *, void *);
static void mk_log_data(LogData *); static void mk_log_data(LogData *);
static void chk_ptr_chg( static void chk_ptr_chg(
TestState *, TestState *,
const char *, const char *,
const char *,
const void *, const void *,
const void * const void *
); );
static void chk_ptr_chg_test(TestState *, void *, void *);
// Public Functions // Public Functions
void void
test_append_test_log(TestState *s) test_append_test_log(TestState *s)
{ {
print("append_test_log()\n"); test_context(s, "append to empty", append_to_empty);
append_to_empty(s); test_context(s, "append to existing", append_to_existing);
append_to_existing(s); test_context(s, "missing last_log", missing_last);
missing_last(s); test_context(s, "missing first_log", missing_first);
missing_first(s); test_context(s, "null message", null_message);
null_message(s); single_test_context(s, "null state", null_state);
run_test(s, null_state);
} }
// Local Functions // Local Functions
@ -76,159 +75,71 @@ static void
append_to_empty(TestState *s) append_to_empty(TestState *s)
{ {
TestState test; TestState test;
print("\tappend to empty\n");
mk_sample_state(&test); mk_sample_state(&test);
append_test_log(&test, "foo"); append_test_log(&test, "foo");
chk_ptr_ne(s, "first_log", test.first_log, 0);
// first_log shouldn't be null chk_str_eq(s, "first_log->text", test.first_log->text, "foo");
print("\t\tfirst_log\n"); chk_ptr_eq(s, "last_log", test.last_log, test.first_log);
chk_ptr_ne(
s,
"ERROR: append_test_log(): append to empty: first_log:",
test.first_log,
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(
s,
"ERROR: append_test_log(): append to empty: last_log:",
test.last_log,
test.first_log
);
} }
static void static void
append_to_existing(TestState *s) append_to_existing(TestState *s)
{ {
LogData ld; LogData ld;
print("\tappend to existing\n");
mk_log_data(&ld); mk_log_data(&ld);
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
// first shouldn't change test_context_with(s, "last_log", chk_last, &ld);
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): append to existing: first_log:",
ld.state.first_log,
&ld.first
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): append to existing: last_log:"
);
free(ld.state.last_log);
} }
static void static void
missing_last(TestState *s) missing_last(TestState *s)
{ {
LogData ld; LogData ld;
print("\tlast_log missing\n");
mk_log_data(&ld); mk_log_data(&ld);
ld.state.last_log = 0; ld.state.last_log = 0;
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
// first shouldn't change test_context_with(s, "last_log", chk_last, &ld);
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): last_log missing: first_log:",
ld.state.first_log,
&ld.first
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): last_log missing: last_log:"
);
free(ld.state.last_log);
} }
static void static void
missing_first(TestState *s) missing_first(TestState *s)
{ {
LogData ld; LogData ld;
print("\tfirst_log missing\n");
mk_log_data(&ld); mk_log_data(&ld);
ld.state.first_log = 0; ld.state.first_log = 0;
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.second);
// first_log should point to second test_context_with(s, "last_log", chk_last, &ld);
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): first_log missing: first_log:",
ld.state.first_log,
&ld.second
);
chk_last(
s,
&ld,
"ERROR: append_test_log(): first_log missing: last_log:"
);
free(ld.state.last_log);
} }
static void static void
null_message(TestState *s) null_message(TestState *s)
{ {
LogData ld; LogData ld;
print("\tnull message\n");
mk_log_data(&ld); mk_log_data(&ld);
append_test_log(&ld.state, 0); append_test_log(&ld.state, 0);
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
// first shouldn't change chk_ptr_eq(s, "last_log", ld.state.last_log, &ld.second);
print("\t\tfirst_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): null message: first_log:",
ld.state.first_log,
&ld.first
);
// last shouldn't change
print("\t\tlast_log\n");
chk_ptr_eq(
s,
"ERROR: append_test_log(): null message: last_log:",
ld.state.last_log,
&ld.second
);
} }
decl_test(null_state) static TestResult
null_state(TestState *)
{ {
// ensure it doesn't crash
append_test_log(0, "foo"); append_test_log(0, "foo");
return test_success; return test_success;
} }
static void static void
chk_last(TestState *s, LogData *ld, const char *context) chk_last(TestState *s, void *ptr)
{ {
print("\t\tlast_log\n"); LogData *ld = ptr;
chk_ptr_chg( chk_ptr_chg(s, "last_log", ld->state.last_log, &ld->second);
s, chk_str_eq(s, "last_log->text", ld->state.last_log->text, "baz");
"\t\t\t", free(ld->state.last_log->text);
context, free(ld->state.last_log);
ld->state.last_log,
&ld->second
);
} }
static void static void
@ -246,35 +157,29 @@ mk_log_data(LogData *ld)
static void static void
chk_ptr_chg( chk_ptr_chg(
TestState *s, TestState *s,
const char *prefix,
const char *context, const char *context,
const void *actual, const void *actual,
const void *prohibited const void *prohibited
) )
{ {
char str[STR_BUF_SIZE]; test_context_compare(
// make sure it's changed
print(prefix);
print("changed\n");
snprintf(str, STR_BUF_SIZE, "%s changed:", context);
chk_ptr_ne(
s, s,
str, context,
chk_ptr_chg_test,
actual, actual,
prohibited prohibited
); );
}
// make sure it's not null static void
print(prefix); chk_ptr_chg_test(
print("not null\n"); TestState *s,
snprintf(str, STR_BUF_SIZE, "%s not null:", context); void *actual,
chk_ptr_ne( void *prohibited
s, )
str, {
actual, chk_ptr_ne(s, "has changed", actual, prohibited);
0 chk_ptr_ne(s, "not null", actual, 0);
);
} }
//jl //jl

View File

@ -26,129 +26,23 @@
#include "util.h" #include "util.h"
#include "initial-state.h" #include "initial-state.h"
// Internal Prototypes
static void tests(TestState *);
decl_test(initial_test_count);
decl_test(initial_pass_count);
decl_test(initial_fail_count);
decl_test(initial_pend_count);
decl_test(initial_first_log);
decl_test(initial_last_log);
decl_test(initial_ptr);
decl_test(initial_context);
decl_test(initial_full_context);
decl_test(initial_depth);
decl_test(initial_report);
// Public Functions // Public Functions
void void
test_initial_state(TestState *s) test_initial_state(TestState *s, void *ptr)
{ {
single_test_context(s, "run", initial_test_count); TestState *scpy = ptr;
single_test_context(s, "passsed", initial_pass_count); chk_int_eq(s, "run", scpy->run, 0);
single_test_context(s, "failed", initial_fail_count); chk_int_eq(s, "passsed", scpy->passed, 0);
single_test_context(s, "pending", initial_pend_count); chk_int_eq(s, "failed", scpy->failed, 0);
single_test_context(s, "first_log", initial_first_log); chk_int_eq(s, "pending", scpy->pending, 0);
single_test_context(s, "last_log", initial_last_log); chk_ptr_eq(s, "first_log", scpy->first_log, 0);
single_test_context(s, "ptr", initial_ptr); chk_ptr_eq(s, "last_log", scpy->last_log, 0);
single_test_context(s, "context", initial_context); chk_ptr_eq(s, "ptr", scpy->ptr, 0);
single_test_context(s, "full_context", initial_full_context); chk_ptr_eq(s, "context", scpy->context, 0);
single_test_context(s, "depth", initial_depth); chk_ptr_eq(s, "full_context", scpy->full_context, 0);
single_test_context(s, "report()", initial_report); chk_int_eq(s, "depth", scpy->depth, 0);
} chk_ptr_ne(s, "report()", scpy->report, 0);
// Internal Functions
def_test(initial_test_count, s)
{
TestState *scpy = s->ptr;
if (scpy->run == 0) return test_success;
append_test_log(s, "initial run was nonzero");
return test_failure;
}
def_test(initial_pass_count, s)
{
TestState *scpy = s->ptr;
print("\tpassed\n");
if (scpy->passed == 0) return test_success;
append_test_log(s, "initial passed was nonzero");
return test_failure;
}
def_test(initial_fail_count, s)
{
TestState *scpy = s->ptr;
if (scpy->failed == 0) return test_success;
append_test_log(s, "initial failed was nonzero");
return test_failure;
}
def_test(initial_pend_count, s)
{
TestState *scpy = s->ptr;
print("\tpending\n");
if (scpy->pending == 0) return test_success;
append_test_log(s, "initial pending was nonzero");
return test_failure;
}
def_test(initial_first_log, s)
{
TestState *scpy = s->ptr;
if (scpy->first_log == 0) return test_success;
append_test_log(s, "initial first_log was not null");
return test_failure;
}
def_test(initial_last_log, s)
{
TestState *scpy = s->ptr;
if (scpy->last_log == 0) return test_success;
append_test_log(s, "initial last_log was not null");
return test_failure;
}
def_test(initial_ptr, s)
{
TestState *scpy = s->ptr;
if (scpy->ptr == 0) return test_success;
append_test_log(s, "initial ptr was not null");
return test_failure;
}
def_test(initial_context, s)
{
TestState *scpy = s->ptr;
if (scpy->context == 0) return test_success;
append_test_log(s, "initial context was not null");
return test_failure;
}
def_test(initial_full_context, s)
{
TestState *scpy = s->ptr;
if (scpy->context == 0) return test_success;
append_test_log(s, "initial full_context was not null");
return test_failure;
}
def_test(initial_depth, s)
{
TestState *scpy = s->ptr;
if (scpy->depth == 0) return test_success;
append_test_log(s, "initial depth was not zero");
return test_failure;
}
def_test(initial_report, s)
{
TestState *scpy = s->ptr;
if (scpy->report != 0) return test_success;
append_test_log(s, "initial report was null");
return test_failure;
} }
//jl //jl

View File

@ -19,6 +19,6 @@
*/ */
void test_initial_state(TestState *); void test_initial_state(TestState *, void *);
//jl //jl

View File

@ -31,23 +31,22 @@
static void test_pass(TestState *); static void test_pass(TestState *);
static void test_fail(TestState *); static void test_fail(TestState *);
static void test_pend(TestState *); static void test_pend(TestState *);
decl_test(null_state); static TestResult null_state(TestState *);
static void null_test(TestState *); static void null_test(TestState *);
decl_test(always_passes); static TestResult always_passes(TestState *);
decl_test(always_fails); static TestResult always_fails(TestState *);
decl_test(always_pends); static TestResult always_pends(TestState *);
// Public Functions // Public Functions
void void
test_run_test(TestState *s) test_run_test(TestState *s)
{ {
print("run_test()\n"); test_context(s, "passing", test_pass);
test_pass(s); test_context(s, "failing", test_fail);
test_fail(s); test_context(s, "pending", test_pend);
test_pend(s); single_test_context(s, "null state", null_state);
run_test(s, null_state); test_context(s, "null test", null_test);
null_test(s);
} }
// Internal Functions // Internal Functions
@ -55,78 +54,58 @@ test_run_test(TestState *s)
static void static void
test_pass(TestState *s) test_pass(TestState *s)
{ {
TestState expected, actual; TestState actual, expected;
print("\tpassing\n");
// actual result
mk_sample_state(&actual);
run_test(&actual, always_passes);
// expected result // expected result
mk_sample_state(&expected); mk_sample_state(&expected);
expected.passed = 2; expected.passed = 2;
expected.run = 7; expected.run = 7;
// actual result chk_TestState_eq(s, 0, &actual, &expected);
mk_sample_state(&actual);
run_test(&actual, always_passes);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): passing:",
&actual,
&expected
);
} }
static void static void
test_fail(TestState *s) test_fail(TestState *s)
{ {
TestState expected, actual; TestState actual, expected;
print("\tfailing\n");
// actual result
mk_sample_state(&actual);
run_test(&actual, always_fails);
// expected result // expected result
mk_sample_state(&expected); mk_sample_state(&expected);
expected.failed = 3; expected.failed = 3;
expected.run = 7; expected.run = 7;
// actual result chk_TestState_eq(s, 0, &actual, &expected);
mk_sample_state(&actual);
run_test(&actual, always_fails);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): failing:",
&expected,
&actual
);
} }
static void static void
test_pend(TestState *s) test_pend(TestState *s)
{ {
TestState expected, actual; TestState actual, expected;
print("\tpending\n");
// actual result
mk_sample_state(&actual);
run_test(&actual, always_pends);
// expected result // expected result
mk_sample_state(&expected); mk_sample_state(&expected);
expected.pending = 4; expected.pending = 4;
expected.run = 7; expected.run = 7;
// actual result chk_TestState_eq(s, 0, &actual, &expected);
mk_sample_state(&actual);
run_test(&actual, always_pends);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): pending:",
&expected,
&actual
);
} }
decl_test(null_state) static TestResult
null_state(TestState *)
{ {
print("\tnull state\n"); // make sure it doesn't crash
run_test(0, always_passes); run_test(0, always_passes);
return test_success; return test_success;
} }
@ -135,37 +114,33 @@ static void
null_test(TestState *s) null_test(TestState *s)
{ {
TestState actual, expected; TestState actual, expected;
print("\tnull test\n");
// actual value
mk_sample_state(&actual);
run_test(&actual, 0);
// expected value // expected value
mk_sample_state(&expected); mk_sample_state(&expected);
expected.pending = 4; expected.pending = 4;
expected.run = 7; expected.run = 7;
// actual value chk_TestState_eq(s, 0, &actual, &expected);
mk_sample_state(&actual);
run_test(&actual, 0);
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): null_test:",
&actual,
&expected
);
} }
decl_test(always_passes) static TestResult
always_passes(TestState *)
{ {
return test_success; return test_success;
} }
decl_test(always_fails) static TestResult
always_fails(TestState *)
{ {
return test_failure; return test_failure;
} }
decl_test(always_pends) static TestResult
always_pends(TestState *)
{ {
return test_pending; return test_pending;
} }

View File

@ -48,14 +48,13 @@ tests(TestState *s)
{ {
if (!s) exits("ERROR: no TestState"); if (!s) exits("ERROR: no TestState");
// Make a copy of the initial TestState and store it // Make a copy of the initial TestState
TestState scpy; TestState scpy;
memcpy(&scpy, s, sizeof(TestState)); memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_context(s, "initial state", test_initial_state); test_context_with(s, "initial state", test_initial_state, &scpy);
test_run_test(s); test_context(s, "run_test()", test_run_test);
test_append_test_log(s); test_context(s, "append_test_log()", test_append_test_log);
test_context(s, "test_context()", test_test_context); test_context(s, "test_context()", test_test_context);
} }

View File

@ -26,134 +26,17 @@
#include "../9unit.h" #include "../9unit.h"
#include "util.h" #include "util.h"
// Local Types // Internal Prototypes
typedef struct CompareInts CompareInts; static void chk_TestState_eq_test(TestState *, void *, void *);
typedef struct ComparePtrs ComparePtrs; static TestResult chk_int_eq_test(TestState *, void *, void *);
static TestResult chk_ptr_eq_test(TestState *, void *, void *);
struct CompareInts static TestResult chk_ptr_ne_test(TestState *, void *, void *);
{ static TestResult chk_str_eq_test(TestState *, void *, void *);
const char *context;
int chk_val;
int ref_val;
};
struct ComparePtrs
{
const char *context;
const void *chk_val;
const void *ref_val;
};
// Local Prototypes
// compare the run value of two states
static void chk_TestState_run_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the passed value of two states
static void chk_TestState_passed_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the failed value of two states
static void chk_TestState_failed_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the pending value of two states
static void chk_TestState_pending_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the first_log value of two states
static void chk_TestState_first_log_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the last_log value of two states
static void chk_TestState_last_log_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the ptr value of two states
static void chk_TestState_ptr_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the context value of two states
static void chk_TestState_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the full_context value of two states
static void chk_TestState_full_context_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the depth value of two states
static void chk_TestState_depth_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// compare the report pointer of two states
static void chk_TestState_report_eq(
TestState *,
const char *,
const char *,
const TestState *,
const TestState *
);
// discard report data // discard report data
static void report(const char *); static void report(const char *);
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 // Public Functions
void void
@ -170,23 +53,18 @@ mk_sample_state(TestState *s)
void void
chk_TestState_eq( chk_TestState_eq(
TestState *s, TestState *s,
const char *prefix,
const char *context, const char *context,
const TestState *actual, const TestState *actual,
const TestState *expected const TestState *expected
) )
{ {
chk_TestState_run_eq(s, prefix, context, actual, expected); test_context_compare(
chk_TestState_passed_eq(s, prefix, context, actual, expected); s,
chk_TestState_failed_eq(s, prefix, context, actual, expected); context,
chk_TestState_pending_eq(s, prefix, context, actual, expected); chk_TestState_eq_test,
chk_TestState_first_log_eq(s, prefix, context, actual, expected); actual,
chk_TestState_last_log_eq(s, prefix, context, actual, expected); expected
chk_TestState_ptr_eq(s, prefix, context, actual, expected); );
chk_TestState_context_eq(s, prefix, context, actual, expected);
chk_TestState_full_context_eq(s, prefix, context, actual, expected);
chk_TestState_depth_eq(s, prefix, context, actual, expected);
chk_TestState_report_eq(s, prefix, context, actual, expected);
} }
void void
@ -197,32 +75,30 @@ chk_int_eq(
int expected int expected
) )
{ {
void *old_ptr = s->ptr; single_test_context_compare(
CompareInts ci; s,
ci.context = context; context,
ci.chk_val = actual; chk_int_eq_test,
ci.ref_val = expected; &actual,
s->ptr = &ci; &expected
run_test(s, chk_int_eq_test); );
s->ptr = old_ptr;
} }
void void
chk_ptr_eq( chk_ptr_eq(
TestState *s, TestState *s,
const char *context, const char *context,
void *actual, const void *actual,
void *expected const void *expected
) )
{ {
void *old_ptr = s->ptr; single_test_context_compare(
ComparePtrs cp; s,
cp.context = context; context,
cp.chk_val = actual; chk_ptr_eq_test,
cp.ref_val = expected; actual,
s->ptr = &cp; expected
run_test(s, chk_ptr_eq_test); );
s->ptr = old_ptr;
} }
void void
@ -233,14 +109,13 @@ chk_ptr_ne(
const void *prohibited const void *prohibited
) )
{ {
void *old_ptr = s->ptr; single_test_context_compare(
ComparePtrs cp; s,
cp.context = context; context,
cp.chk_val = actual; chk_ptr_ne_test,
cp.ref_val = prohibited; actual,
s->ptr = &cp; prohibited
run_test(s, chk_ptr_ne_test); );
s->ptr = old_ptr;
} }
void void
@ -251,242 +126,153 @@ chk_str_eq(
const char *expected const char *expected
) )
{ {
void *old_ptr = s->ptr; single_test_context_compare(
ComparePtrs cp; s,
cp.context = context; context,
cp.chk_val = actual; chk_str_eq_test,
cp.ref_val = expected; actual,
s->ptr = &cp; expected
run_test(s, chk_str_eq_test); );
s->ptr = old_ptr;
} }
// Local Functions // Local Functions
static void static void
chk_TestState_run_eq( chk_TestState_eq_test(
TestState *s, TestState *s,
const char *prefix, void *aptr,
const char *context, void *eptr
const TestState *actual,
const TestState *expected
) )
{ {
char full_context[STR_BUF_SIZE]; if (!eptr)
print(prefix); {
print("run\n"); chk_ptr_eq(s, "is null", aptr, 0);
snprintf(full_context, STR_BUF_SIZE, "%s run:", context); return;
chk_int_eq(s, full_context, actual->run, expected->run);
} }
static void TestState
chk_TestState_passed_eq( *actual = aptr,
*expected = eptr;
chk_int_eq(s, "run", actual->run, expected->run);
chk_int_eq(s, "passed", actual->passed, expected->passed);
chk_int_eq(s, "failed", actual->failed, expected->failed);
chk_int_eq(s, "pending", actual->pending, expected->pending);
chk_ptr_eq(s, "first_log", actual->first_log, expected->first_log);
chk_ptr_eq(s, "last_log", actual->last_log, expected->last_log);
chk_ptr_eq(s, "ptr", actual->ptr, expected->ptr);
chk_str_eq(s, "context", actual->context, expected->context);
chk_str_eq(s, "full_context", actual->full_context, expected->full_context);
chk_int_eq(s, "depth", actual->depth, expected->depth);
chk_ptr_eq(s, "report()", actual->report, expected->report);
}
static TestResult
chk_int_eq_test(
TestState *s, TestState *s,
const char *prefix, void *aptr,
const char *context, void *eptr
const TestState *actual,
const TestState *expected
) )
{ {
char full_context[STR_BUF_SIZE]; int
print(prefix); *actual = aptr,
print("passed\n"); *expected = eptr;
snprintf(full_context, STR_BUF_SIZE, "%s passed:", context);
chk_int_eq(s, full_context, actual->passed, expected->passed);
}
static void if (*actual == *expected)
chk_TestState_failed_eq( return test_success;
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("failed\n");
snprintf(full_context, STR_BUF_SIZE, "%s failed:", context);
chk_int_eq(s, full_context, actual->failed, expected->failed);
}
static void // log the error
chk_TestState_pending_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("pending\n");
snprintf(full_context, STR_BUF_SIZE, "%s pending:", context);
chk_int_eq(s, full_context, actual->pending, expected->pending);
}
static void
chk_TestState_first_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("first_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s first_log:", context);
chk_ptr_eq(s, full_context, actual->first_log, expected->first_log);
}
static void
chk_TestState_last_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("last_log\n");
snprintf(full_context, STR_BUF_SIZE, "%s last_log:", context);
chk_ptr_eq(s, full_context, actual->last_log, expected->last_log);
}
static void
chk_TestState_ptr_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("ptr\n");
snprintf(full_context, STR_BUF_SIZE, "%s ptr:", context);
chk_ptr_eq(s, full_context, actual->ptr, expected->ptr);
}
static void
chk_TestState_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("context\n");
snprintf(full_context, STR_BUF_SIZE, "%s context:", context);
chk_str_eq(s, full_context, actual->context, expected->context);
}
static void
chk_TestState_full_context_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("full_context\n");
snprintf(full_context, STR_BUF_SIZE, "%s full_context:", context);
chk_str_eq(s, full_context, actual->full_context, expected->full_context);
}
static void
chk_TestState_depth_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("depth\n");
snprintf(full_context, STR_BUF_SIZE, "%s depth:", context);
chk_int_eq(s, full_context, actual->depth, expected->depth);
}
static void
chk_TestState_report_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("report()\n");
snprintf(full_context, STR_BUF_SIZE, "%s report():", context);
chk_ptr_eq(s, full_context, actual->report, expected->report);
}
def_test(chk_int_eq_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->chk_val == ci->ref_val) return test_success;
char str[STR_BUF_SIZE]; char str[STR_BUF_SIZE];
append_test_log(s, ci->context); log_test_context(s);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->ref_val); append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: %d", *expected);
append_test_log(s, str); append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->chk_val); snprintf(str, STR_BUF_SIZE, "\tactual: %d", *actual);
append_test_log(s, str);
return test_failure;
}
static TestResult
chk_ptr_eq_test(
TestState *s,
void *actual,
void *expected
)
{
if (actual == expected)
return test_success;
// log the error
char str[STR_BUF_SIZE];
log_test_context(s);
append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", actual);
append_test_log(s, str);
return test_failure;
}
static TestResult
chk_ptr_ne_test(
TestState *s,
void *actual,
void *prohibited
)
{
if (actual != prohibited)
return test_success;
// log the error
char str[STR_BUF_SIZE];
log_test_context(s);
snprintf(
str,
STR_BUF_SIZE,
"ERROR: prohibited value: 0x%x",
actual
);
append_test_log(s, str);
return test_failure;
}
static TestResult
chk_str_eq_test(
TestState *s,
void *aptr,
void *eptr
)
{
if (!eptr)
{
if (!aptr) return test_success;
char str[STR_BUF_SIZE];
log_test_context(s);
append_test_log(s, "ERROR:");
append_test_log(s, "\texpected: 0x0");
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", aptr);
append_test_log(s, str); append_test_log(s, str);
return test_failure; return test_failure;
} }
def_test(chk_ptr_eq_test, s) const char
{ *actual = aptr,
const ComparePtrs *cp = s->ptr; *expected = eptr;
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, "\texpected: 0x%x", cp->ref_val);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", cp->chk_val);
append_test_log(s, str);
return test_failure;
}
def_test(chk_ptr_ne_test, s) if (!strcmp(actual, expected)) return;
{
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;
}
def_test(chk_str_eq_test, s) // log the error
{
const ComparePtrs *cp = s->ptr;
if (!cp->chk_val && !cp->ref_val) return test_success;
if (!strcmp(cp->chk_val, cp->ref_val)) return test_success;
char str[STR_BUF_SIZE]; char str[STR_BUF_SIZE];
append_test_log(s, cp->context); log_test_context(s);
snprintf(str, STR_BUF_SIZE, "\texpected: %s", cp->ref_val); append_test_log(s, "ERROR:");
snprintf(str, STR_BUF_SIZE, "\texpected: %s", expected);
append_test_log(s, str); append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %s", cp->ref_val); snprintf(str, STR_BUF_SIZE, "\tactual: %s", actual);
append_test_log(s, str); append_test_log(s, str);
return test_failure; return test_failure;
} }

View File

@ -19,10 +19,7 @@
*/ */
#define STR_BUF_SIZE 256 // buffer size for constructing arbitrary strings #define STR_BUF_SIZE 256 // maximum string buffer size
#define decl_test(n) static TestResult n(TestState *)
#define def_test(n, s) static TestResult n(TestState *s)
// initializes a sample TestState value // initializes a sample TestState value
void mk_sample_state(TestState *); void mk_sample_state(TestState *);
@ -30,40 +27,39 @@ void mk_sample_state(TestState *);
// ensures two TestState values are equal // ensures two TestState values are equal
extern void chk_TestState_eq( extern void chk_TestState_eq(
TestState *, // the state we are *actually* updating ;) TestState *, // the state we are *actually* updating ;)
const char *, // prefix for each status line const char *, // the context
const char *, // context for errors
const TestState *, // actual state const TestState *, // actual state
const TestState * // expected state const TestState * // expected state
); );
// ensure two integers are equal // ensure two integers are equal
extern void chk_int_eq( extern void chk_int_eq(
TestState *, TestState *, // the test state
const char *, // the error context const char *, // the context
int, // the actual value int, // the actual value
int // the expected value int // the expected value
); );
// ensure two pointers are equal // ensure two pointers are equal
extern void chk_ptr_eq( extern void chk_ptr_eq(
TestState *, TestState *, // the test state
const char *, // the error context const char *, // the context
const void *, // the actual value void *, // the actual value
const void * // the expected value void * // the expected value
); );
// ensure two pointers are not equal // ensure two pointers are not equal
extern void chk_ptr_ne( extern void chk_ptr_ne(
TestState *, TestState *, // the test state
const char *, // the error context const char *, // the context
const void *, // the actual value void *, // the actual value
const void * // the prohibited value void * // the prohibited value
); );
// ensure two strings are equal // ensure two strings are equal
extern void chk_str_eq( extern void chk_str_eq(
TestState *, TestState *, // the test state
const char *, // the error context const char *, // the context
const char *, // the actual value const char *, // the actual value
const char * // the expected value const char * // the expected value
); );