Compare commits

..

No commits in common. "c0f28d41b074b78158ae03c4b2e7c0a78d386ac9" and "f30c093a812164b0946b30113cacee739d83894c" have entirely different histories.

8 changed files with 134 additions and 215 deletions

View File

@ -61,7 +61,7 @@ extern void run_test(
// Runs multiple tests, displaying a summary at the end
extern void run_tests(
// runs the tests and updates a provided TestState
// runs the tests and updates a provided TestState
void (*)(TestState *)
);

View File

@ -1,53 +0,0 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
#include "../9unit.h"
#include "util.h"
#include "append-test-log.h"
// Local Prototypes
static void append_to_empty(TestState *);
// Public Functions
void
test_append_test_log(TestState *s)
{
print("append_test_log()\n");
append_to_empty(s);
}
// Local Functions
static void
append_to_empty(TestState *)
{
TestState test;
print("\tappend to empty\n");
mk_sample_state(&test);
append_test_log(&test, "foo");
}
//jl

View File

@ -1,24 +0,0 @@
/*
9unit
Copyright (C) Jonathan Lamothe <jonathan@jlamothe.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
extern void test_append_test_log(TestState *);
//jl

View File

@ -17,8 +17,8 @@
</$objtype/mkfile
HFILES=../9unit.h util.h initial-state.h run-test.h append-test-log.h
OFILES=util.$O initial-state.$O run-test.$O append-test-log.$O
HFILES=../9unit.h util.h initial-state.h run-test.h
OFILES=util.$O initial-state.$O run-test.$O
LIB=../9unit.a
TEST=tests

View File

@ -31,6 +31,7 @@
static void test_pass(TestState *);
static void test_fail(TestState *);
static void test_pend(TestState *);
static void mk_sample_state(TestState *);
decl_test(always_passes);
decl_test(always_fails);
decl_test(always_pends);
@ -65,12 +66,12 @@ test_pass(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_passes);
chk_TestState_eq(
compare_states(
s,
"\t\t",
"ERROR: run_test(): passing:",
&actual,
&expected
&expected,
&actual
);
}
@ -91,7 +92,7 @@ test_fail(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_fails);
chk_TestState_eq(
compare_states(
s,
"\t\t",
"ERROR: run_test(): failing:",
@ -117,7 +118,7 @@ test_pend(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_pends);
chk_TestState_eq(
compare_states(
s,
"\t\t",
"ERROR: run_test(): pending:",
@ -126,6 +127,16 @@ test_pend(TestState *s)
);
}
static void
mk_sample_state(TestState *s)
{
memset(s, 0, sizeof(TestState));
s->passed = 1;
s->failed = 2;
s->pending = 3;
s->run = 6;
}
decl_test(always_passes)
{
return test_success;

View File

@ -25,7 +25,6 @@
#include "../9unit.h"
#include "initial-state.h"
#include "run-test.h"
#include "append-test-log.h"
// Internal Prototypes
@ -54,7 +53,6 @@ tests(TestState *s)
test_initial_state(s);
test_run_test(s);
test_append_test_log(s);
}
//jl

View File

@ -34,21 +34,21 @@ typedef struct ComparePtrs ComparePtrs;
struct CompareInts
{
const char *context;
int chk_val;
int ref_val;
int expected;
int actual;
};
struct ComparePtrs
{
const char *context;
const void *chk_val;
const void *ref_val;
const void *expected;
const void *actual;
};
// Local Prototypes
// compare the run value of two states
static void chk_TestState_run_eq(
static void compare_run(
TestState *,
const char *,
const char *,
@ -57,7 +57,7 @@ static void chk_TestState_run_eq(
);
// compare the passed value of two states
static void chk_TestState_passed_eq(
static void compare_passed(
TestState *,
const char *,
const char *,
@ -66,7 +66,7 @@ static void chk_TestState_passed_eq(
);
// compare the failed value of two states
static void chk_TestState_failed_eq(
static void compare_failed(
TestState *,
const char *,
const char *,
@ -75,7 +75,7 @@ static void chk_TestState_failed_eq(
);
// compare the pending value of two states
static void chk_TestState_pending_eq(
static void compare_pending(
TestState *,
const char *,
const char *,
@ -84,7 +84,7 @@ static void chk_TestState_pending_eq(
);
// compare the first_log value of two states
static void chk_TestState_first_log_eq(
static void compare_first_log(
TestState *,
const char *,
const char *,
@ -93,7 +93,7 @@ static void chk_TestState_first_log_eq(
);
// compare the last_log value of two states
static void chk_TestState_last_log_eq(
static void compare_last_log(
TestState *,
const char *,
const char *,
@ -102,7 +102,7 @@ static void chk_TestState_last_log_eq(
);
// compare the ptr value of two states
static void chk_TestState_ptr_eq(
static void compare_ptr(
TestState *,
const char *,
const char *,
@ -110,211 +110,217 @@ static void chk_TestState_ptr_eq(
const TestState *
);
decl_test(chk_int_eq_test);
decl_test(chk_ptr_eq_test);
// compare two ints
static void compare_ints(
TestState *,
const char *,
int,
int
);
// compare two pointers
static void compare_ptrs(
TestState *,
const char *,
void *,
void *
);
decl_test(compare_ints_test);
decl_test(compare_ptrs_test);
// Public Functions
void
mk_sample_state(TestState *s)
{
memset(s, 0, sizeof(TestState));
s->passed = 1;
s->failed = 2;
s->pending = 3;
s->run = 6;
}
void
chk_TestState_eq(
compare_states(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
}
void
chk_int_eq(
TestState *s,
const char *context,
int actual,
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;
}
void
chk_ptr_eq(
TestState *s,
const char *context,
void *actual,
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;
compare_run(s, prefix, context, expected, actual);
compare_passed(s, prefix, context, expected, actual);
compare_failed(s, prefix, context, expected, actual);
compare_pending(s, prefix, context, expected, actual);
compare_first_log(s, prefix, context, expected, actual);
compare_last_log(s, prefix, context, expected, actual);
compare_ptr(s, prefix, context, expected, actual);
}
// Local Functions
static void
chk_TestState_run_eq(
compare_run(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ints(s, full_context, expected->run, actual->run);
}
static void
chk_TestState_passed_eq(
compare_passed(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ints(s, full_context, expected->passed, actual->passed);
}
static void
chk_TestState_failed_eq(
compare_failed(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ints(s, full_context, expected->failed, actual->failed);
}
static void
chk_TestState_pending_eq(
compare_pending(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ints(s, full_context, expected->pending, actual->pending);
}
static void
chk_TestState_first_log_eq(
compare_first_log(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ptrs(s, full_context, expected->first_log, actual->first_log);
}
static void
chk_TestState_last_log_eq(
compare_last_log(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ptrs(s, full_context, expected->last_log, actual->last_log);
}
static void
chk_TestState_ptr_eq(
compare_ptr(
TestState *s,
const char *prefix,
const char *context,
const TestState *actual,
const TestState *expected
const TestState *expected,
const TestState *actual
)
{
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);
compare_ptrs(s, full_context, expected->ptr, actual->ptr);
}
def_test(chk_int_eq_test, s)
static void
compare_ints(
TestState *s,
const char *context,
int expected,
int actual
)
{
void *old_ptr = s->ptr;
CompareInts ci;
ci.context = context;
ci.expected = expected;
ci.actual = actual;
s->ptr = &ci;
run_test(s, compare_ints_test);
s->ptr = old_ptr;
}
static void
compare_ptrs(
TestState *s,
const char *context,
void *expected,
void *actual
)
{
void *old_ptr = s->ptr;
ComparePtrs cp;
cp.context = context;
cp.expected = expected;
cp.actual = actual;
s->ptr = &cp;
run_test(s, compare_ptrs_test);
s->ptr = old_ptr;
}
def_test(compare_ints_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->chk_val == ci->ref_val) return test_success;
if (ci->actual == ci->expected) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, ci->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->ref_val);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->expected);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->chk_val);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->actual);
append_test_log(s, str);
return test_failure;
}
def_test(chk_ptr_eq_test, s)
def_test(compare_ptrs_test, s)
{
const ComparePtrs *cp = s->ptr;
if (cp->chk_val == cp->ref_val) return test_success;
if (cp->actual == cp->expected) 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);
snprintf(str, STR_BUF_SIZE, "\texpected: 0x%x", cp->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", cp->actual);
append_test_log(s, str);
return test_failure;
}

View File

@ -24,32 +24,13 @@
#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 *);
// ensures two TestState values are equal
extern void chk_TestState_eq(
// compares two TestState values
extern void compare_states(
TestState *, // the state we are *actually* updating ;)
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 *,
const char *, // the error 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
void *, // the actual value
void * // the expected value
const TestState *, // expected state
const TestState * // actual state
);
//jl