Compare commits

..

No commits in common. "57e4d2ba49746e4b276d96ba9bb7231f46112a07" and "01e9cb8794edd0dc578a2ec247f7632339feb542" have entirely different histories.

7 changed files with 706 additions and 261 deletions

View File

@ -21,6 +21,7 @@
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include "../9unit.h"
#include "util.h"
@ -43,30 +44,30 @@ static void append_to_existing(TestState *);
static void missing_last(TestState *);
static void missing_first(TestState *);
static void null_message(TestState *);
static TestResult null_state(TestState *);
static void chk_last(TestState *, void *);
decl_test(null_state);
static void chk_last(TestState *, LogData *, const char *);
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)
{
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);
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);
}
// Local Functions
@ -75,71 +76,159 @@ static void
append_to_empty(TestState *s)
{
TestState test;
print("\tappend to empty\n");
mk_sample_state(&test);
append_test_log(&test, "foo");
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);
// 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
);
}
static void
append_to_existing(TestState *s)
{
LogData ld;
print("\tappend to existing\n");
mk_log_data(&ld);
append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
test_context_with(s, "last_log", chk_last, &ld);
// 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);
}
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");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
test_context_with(s, "last_log", chk_last, &ld);
// 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);
}
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");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.second);
test_context_with(s, "last_log", chk_last, &ld);
// 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);
}
static void
null_message(TestState *s)
{
LogData ld;
print("\tnull message\n");
mk_log_data(&ld);
append_test_log(&ld.state, 0);
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
chk_ptr_eq(s, "last_log", ld.state.last_log, &ld.second);
// 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
);
}
static TestResult
null_state(TestState *)
decl_test(null_state)
{
// ensure it doesn't crash
append_test_log(0, "foo");
return test_success;
}
static void
chk_last(TestState *s, void *ptr)
chk_last(TestState *s, LogData *ld, const char *context)
{
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);
print("\t\tlast_log\n");
chk_ptr_chg(
s,
"\t\t\t",
context,
ld->state.last_log,
&ld->second
);
}
static void
@ -157,29 +246,35 @@ mk_log_data(LogData *ld)
static void
chk_ptr_chg(
TestState *s,
const char *prefix,
const char *context,
const void *actual,
const void *prohibited
)
{
test_context_compare(
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(
s,
context,
chk_ptr_chg_test,
str,
actual,
prohibited
);
}
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);
// 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
);
}
//jl

View File

@ -26,23 +26,129 @@
#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, void *ptr)
test_initial_state(TestState *s)
{
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);
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;
}
//jl

View File

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

View File

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

View File

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

View File

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

View File

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