9unit/test/append-test-log.c

204 lines
4.2 KiB
C

/*
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 Types
typedef struct LogData LogData;
struct LogData
{
TestState state;
TestLogEntry first, second;
};
// Local Prototypes
static void append_to_empty(TestState *);
static void chk_empty_first(TestState *, void *);
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 *);
static void mk_log_data(LogData *);
static void chk_ptr_chg(
TestState *,
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);
}
// Local Functions
static void
append_to_empty(TestState *s)
{
TestState test;
mk_sample_state(&test);
append_test_log(&test, "foo");
// first_log
test_context_with(
s,
"first_log",
chk_empty_first,
&test
);
// last_log
chk_ptr_eq(s, "last_log", test.last_log, test.first_log);
}
static void
chk_empty_first(TestState *s, void *ptr)
{
TestState *test = ptr;
chk_str_eq(s, "text", test->first_log->text, "foo");
chk_ptr_eq(s, "next", test->first_log->next, 0);
}
static void
append_to_existing(TestState *s)
{
LogData ld;
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);
}
static void
missing_last(TestState *s)
{
LogData ld;
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);
}
static void
missing_first(TestState *s)
{
LogData ld;
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);
}
static void
null_message(TestState *s)
{
LogData ld;
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);
}
static TestResult
null_state(TestState *)
{
// ensure it doesn't crash
append_test_log(0, "foo");
return test_success;
}
static void
chk_last(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_chg(s, 0, ld->state.last_log, &ld->second);
chk_str_eq(s, "text", ld->state.last_log->text, "baz");
chk_ptr_eq(s, "next", ld->state.last_log->next, 0);
free(ld->state.last_log->text);
free(ld->state.last_log);
}
static void
mk_log_data(LogData *ld)
{
mk_sample_state(&ld->state);
ld->state.first_log = &ld->first;
ld->state.last_log = &ld->second;
ld->first.text = "foo";
ld->first.next = &ld->second;
ld->second.text = "bar";
ld->second.next = 0;
}
static void
chk_ptr_chg(
TestState *s,
const char *context,
const void *actual,
const void *prohibited
)
{
test_context_compare(
s,
context,
chk_ptr_chg_test,
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);
}
//jl