Compare commits

...

6 Commits

Author SHA1 Message Date
jlamothe c0f28d41b0 renamed various checking functions 2023-11-11 03:40:00 +00:00
jlamothe 3309ac7aee made compare_ints() and compare_ptrs() accessible from outside util 2023-11-10 22:44:42 +00:00
jlamothe a14b11e853 append to an empty test log 2023-11-10 21:17:06 +00:00
jlamothe 4c0d33693b moved mk_sample_state() into util 2023-11-10 20:48:40 +00:00
jlamothe a962eff7b4 test module for append_test_log() 2023-11-10 20:30:44 +00:00
jlamothe c3bace917d fixed wihtespace issue 2023-11-10 20:05:37 +00:00
8 changed files with 215 additions and 134 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 *)
);

53
test/append-test-log.c Normal file
View File

@ -0,0 +1,53 @@
/*
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

24
test/append-test-log.h Normal file
View File

@ -0,0 +1,24 @@
/*
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
OFILES=util.$O initial-state.$O run-test.$O
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
LIB=../9unit.a
TEST=tests

View File

@ -31,7 +31,6 @@
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);
@ -66,12 +65,12 @@ test_pass(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_passes);
compare_states(
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): passing:",
&expected,
&actual
&actual,
&expected
);
}
@ -92,7 +91,7 @@ test_fail(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_fails);
compare_states(
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): failing:",
@ -118,7 +117,7 @@ test_pend(TestState *s)
mk_sample_state(&actual);
run_test(&actual, always_pends);
compare_states(
chk_TestState_eq(
s,
"\t\t",
"ERROR: run_test(): pending:",
@ -127,16 +126,6 @@ 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,6 +25,7 @@
#include "../9unit.h"
#include "initial-state.h"
#include "run-test.h"
#include "append-test-log.h"
// Internal Prototypes
@ -53,6 +54,7 @@ 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 expected;
int actual;
int chk_val;
int ref_val;
};
struct ComparePtrs
{
const char *context;
const void *expected;
const void *actual;
const void *chk_val;
const void *ref_val;
};
// Local Prototypes
// compare the run value of two states
static void compare_run(
static void chk_TestState_run_eq(
TestState *,
const char *,
const char *,
@ -57,7 +57,7 @@ static void compare_run(
);
// compare the passed value of two states
static void compare_passed(
static void chk_TestState_passed_eq(
TestState *,
const char *,
const char *,
@ -66,7 +66,7 @@ static void compare_passed(
);
// compare the failed value of two states
static void compare_failed(
static void chk_TestState_failed_eq(
TestState *,
const char *,
const char *,
@ -75,7 +75,7 @@ static void compare_failed(
);
// compare the pending value of two states
static void compare_pending(
static void chk_TestState_pending_eq(
TestState *,
const char *,
const char *,
@ -84,7 +84,7 @@ static void compare_pending(
);
// compare the first_log value of two states
static void compare_first_log(
static void chk_TestState_first_log_eq(
TestState *,
const char *,
const char *,
@ -93,7 +93,7 @@ static void compare_first_log(
);
// compare the last_log value of two states
static void compare_last_log(
static void chk_TestState_last_log_eq(
TestState *,
const char *,
const char *,
@ -102,7 +102,7 @@ static void compare_last_log(
);
// compare the ptr value of two states
static void compare_ptr(
static void chk_TestState_ptr_eq(
TestState *,
const char *,
const char *,
@ -110,217 +110,211 @@ static void compare_ptr(
const TestState *
);
// 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);
decl_test(chk_int_eq_test);
decl_test(chk_ptr_eq_test);
// Public Functions
void
compare_states(
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(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
const TestState *actual,
const TestState *expected
)
{
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);
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;
}
// Local Functions
static void
compare_run(
chk_TestState_run_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("run\n");
snprintf(full_context, STR_BUF_SIZE, "%s run:", context);
compare_ints(s, full_context, expected->run, actual->run);
chk_int_eq(s, full_context, actual->run, expected->run);
}
static void
compare_passed(
chk_TestState_passed_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
const TestState *actual,
const TestState *expected
)
{
char full_context[STR_BUF_SIZE];
print(prefix);
print("passed\n");
snprintf(full_context, STR_BUF_SIZE, "%s passed:", context);
compare_ints(s, full_context, expected->passed, actual->passed);
chk_int_eq(s, full_context, actual->passed, expected->passed);
}
static void
compare_failed(
chk_TestState_failed_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
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);
compare_ints(s, full_context, expected->failed, actual->failed);
chk_int_eq(s, full_context, actual->failed, expected->failed);
}
static void
compare_pending(
chk_TestState_pending_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
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);
compare_ints(s, full_context, expected->pending, actual->pending);
chk_int_eq(s, full_context, actual->pending, expected->pending);
}
static void
compare_first_log(
chk_TestState_first_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
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);
compare_ptrs(s, full_context, expected->first_log, actual->first_log);
chk_ptr_eq(s, full_context, actual->first_log, expected->first_log);
}
static void
compare_last_log(
chk_TestState_last_log_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
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);
compare_ptrs(s, full_context, expected->last_log, actual->last_log);
chk_ptr_eq(s, full_context, actual->last_log, expected->last_log);
}
static void
compare_ptr(
chk_TestState_ptr_eq(
TestState *s,
const char *prefix,
const char *context,
const TestState *expected,
const TestState *actual
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);
compare_ptrs(s, full_context, expected->ptr, actual->ptr);
chk_ptr_eq(s, full_context, actual->ptr, expected->ptr);
}
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)
def_test(chk_int_eq_test, s)
{
const CompareInts *ci = s->ptr;
if (ci->actual == ci->expected) return test_success;
if (ci->chk_val == ci->ref_val) return test_success;
char str[STR_BUF_SIZE];
append_test_log(s, ci->context);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->expected);
snprintf(str, STR_BUF_SIZE, "\texpected: %d", ci->ref_val);
append_test_log(s, str);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->actual);
snprintf(str, STR_BUF_SIZE, "\tactual: %d", ci->chk_val);
append_test_log(s, str);
return test_failure;
}
def_test(compare_ptrs_test, s)
def_test(chk_ptr_eq_test, s)
{
const ComparePtrs *cp = s->ptr;
if (cp->actual == cp->expected) return test_success;
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->expected);
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->actual);
snprintf(str, STR_BUF_SIZE, "\tactual: 0x%x", cp->chk_val);
append_test_log(s, str);
return test_failure;
}

View File

@ -24,13 +24,32 @@
#define decl_test(n) static TestResult n(TestState *)
#define def_test(n, s) static TestResult n(TestState *s)
// compares two TestState values
extern void compare_states(
// initializes a sample TestState value
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 TestState *, // expected state
const TestState * // actual state
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
);
//jl