re-implemented test using new convenience functions
This commit is contained in:
504
test/util.c
504
test/util.c
@@ -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;
|
||||
}
|
||||
|
||||
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 void
|
||||
chk_TestState_passed_eq(
|
||||
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;
|
||||
}
|
||||
|
||||
def_test(chk_ptr_eq_test, s)
|
||||
static TestResult
|
||||
chk_ptr_eq_test(
|
||||
TestState *s,
|
||||
void *actual,
|
||||
void *expected
|
||||
)
|
||||
{
|
||||
const ComparePtrs *cp = s->ptr;
|
||||
if (cp->chk_val == cp->ref_val) return test_success;
|
||||
if (actual == expected)
|
||||
return test_success;
|
||||
|
||||
// log the error
|
||||
char str[STR_BUF_SIZE];
|
||||
append_test_log(s, cp->context);
|
||||
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", cp->ref_val);
|
||||
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", cp->chk_val);
|
||||
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", actual);
|
||||
append_test_log(s, str);
|
||||
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(chk_ptr_ne_test, s)
|
||||
static TestResult
|
||||
chk_ptr_ne_test(
|
||||
TestState *s,
|
||||
void *actual,
|
||||
void *prohibited
|
||||
)
|
||||
{
|
||||
const ComparePtrs *cp = s->ptr;
|
||||
if (cp->chk_val != cp->ref_val) return test_success;
|
||||
if (actual != prohibited)
|
||||
return test_success;
|
||||
|
||||
// log the error
|
||||
char str[STR_BUF_SIZE];
|
||||
append_test_log(s, cp->context);
|
||||
snprintf(str, STR_BUF_SIZE, "\tcannot be: 0x%x", cp->ref_val);
|
||||
log_test_context(s);
|
||||
snprintf(
|
||||
str,
|
||||
STR_BUF_SIZE,
|
||||
"ERROR: prohibited value: 0x%x",
|
||||
actual
|
||||
);
|
||||
append_test_log(s, str);
|
||||
|
||||
return test_failure;
|
||||
}
|
||||
|
||||
def_test(chk_str_eq_test, s)
|
||||
static TestResult
|
||||
chk_str_eq_test(
|
||||
TestState *s,
|
||||
void *aptr,
|
||||
void *eptr
|
||||
)
|
||||
{
|
||||
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;
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user