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

View File

@ -26,129 +26,23 @@
#include "util.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
void
test_initial_state(TestState *s)
test_initial_state(TestState *s, void *ptr)
{
single_test_context(s, "run", initial_test_count);
single_test_context(s, "passsed", initial_pass_count);
single_test_context(s, "failed", initial_fail_count);
single_test_context(s, "pending", initial_pend_count);
single_test_context(s, "first_log", initial_first_log);
single_test_context(s, "last_log", initial_last_log);
single_test_context(s, "ptr", initial_ptr);
single_test_context(s, "context", initial_context);
single_test_context(s, "full_context", initial_full_context);
single_test_context(s, "depth", initial_depth);
single_test_context(s, "report()", initial_report);
}
// 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;
TestState *scpy = ptr;
chk_int_eq(s, "run", scpy->run, 0);
chk_int_eq(s, "passsed", scpy->passed, 0);
chk_int_eq(s, "failed", scpy->failed, 0);
chk_int_eq(s, "pending", scpy->pending, 0);
chk_ptr_eq(s, "first_log", scpy->first_log, 0);
chk_ptr_eq(s, "last_log", scpy->last_log, 0);
chk_ptr_eq(s, "ptr", scpy->ptr, 0);
chk_ptr_eq(s, "context", scpy->context, 0);
chk_ptr_eq(s, "full_context", scpy->full_context, 0);
chk_int_eq(s, "depth", scpy->depth, 0);
chk_ptr_ne(s, "report()", scpy->report, 0);
}
//jl

View File

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

View File

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

View File

@ -48,14 +48,13 @@ tests(TestState *s)
{
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;
memcpy(&scpy, s, sizeof(TestState));
s->ptr = &scpy;
test_context(s, "initial state", test_initial_state);
test_run_test(s);
test_append_test_log(s);
test_context_with(s, "initial state", test_initial_state, &scpy);
test_context(s, "run_test()", test_run_test);
test_context(s, "append_test_log()", test_append_test_log);
test_context(s, "test_context()", test_test_context);
}

View File

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

View File

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