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

276 lines
5.6 KiB
C
Raw Normal View History

2023-11-10 15:30:44 -05:00
/*
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>
2023-11-10 16:17:06 -05:00
#include <libc.h>
2023-11-10 15:30:44 -05:00
#include "../9unit.h"
2023-11-10 16:17:06 -05:00
#include "util.h"
2023-11-10 15:30:44 -05:00
#include "append-test-log.h"
// Local Types
typedef struct LogData LogData;
struct LogData
{
TestState state;
TestLogEntry first, second;
};
2023-11-10 16:17:06 -05:00
// Local Prototypes
static void append_to_empty(TestState *);
2023-11-12 19:37:19 -05:00
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_empty_first(TestState *, void *);
static void chk_first(TestState *, void *);
static void chk_second(TestState *, void *);
static void chk_last(TestState *, void *);
static void mf_chk_first(TestState *, void *);
static void mf_chk_second(TestState *, void *);
static void mf_chk_third(TestState*, void *);
static void mk_log_data(LogData *);
2023-11-10 16:17:06 -05:00
static void chk_ptr_chg(
TestState *,
const char *,
const void *,
const void *
);
static void chk_ptr_chg_test(TestState *, void *, void *);
2023-11-10 15:30:44 -05:00
// Public Functions
void
2023-11-10 16:17:06 -05:00
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);
2023-11-10 16:17:06 -05:00
}
// Local Functions
static void
append_to_empty(TestState *s)
2023-11-10 15:30:44 -05:00
{
2023-11-10 16:17:06 -05:00
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);
2023-11-10 15:30:44 -05:00
}
2023-11-12 19:37:19 -05:00
static void
append_to_existing(TestState *s)
{
LogData ld;
mk_log_data(&ld);
append_test_log(&ld.state, "baz");
test_context_with(s, "first_log", chk_first, &ld);
test_context_with(s, "second log", chk_second, &ld);
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");
test_context_with(s, "first_log", chk_first, &ld);
test_context_with(s, "second log", chk_second, &ld);
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");
test_context_with(s, "first_log", mf_chk_first, &ld);
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_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
chk_first(TestState *s, void *ptr)
{
LogData *ld = ptr;
// first_log
chk_ptr_eq(
s,
0,
ld->state.first_log,
&ld->first
);
// next
chk_ptr_eq(
s,
"next",
ld->state.first_log->next,
&ld->second
);
}
static void
chk_second(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_eq(
s,
"next",
ld->second.next,
ld->state.last_log
);
}
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
mf_chk_first(TestState *s, void *ptr)
{
LogData *ld = ptr;
chk_ptr_eq(s, 0, ld->state.first_log, &ld->second);
test_context_with(s, "next", mf_chk_second, ptr);
}
static void
mf_chk_second(TestState *s, void *ptr)
{
LogData *ld = ptr;
TestLogEntry *e = ld->second.next;
chk_str_eq(s, "text", e->text, "<no context>");
test_context_with(s, "next", mf_chk_third, ptr);
}
static void
mf_chk_third(TestState *s, void *ptr)
{
LogData *ld = ptr;
TestLogEntry *e = ld->second.next->next;
chk_str_eq(
s,
"text",
e->text,
"potential memory leak in test log"
);
chk_ptr_eq(s, "next", e->next, 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);
2023-11-12 19:37:19 -05:00
}
2023-11-10 15:30:44 -05:00
//jl