re-implemented test using new convenience functions

This commit is contained in:
jlamothe
2023-11-21 05:12:41 +00:00
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