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"
|
|
|
|
|
2023-11-12 22:47:21 -05:00
|
|
|
// 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 *);
|
2023-11-14 14:16:21 -05:00
|
|
|
static void missing_last(TestState *);
|
2023-11-14 14:33:02 -05:00
|
|
|
static void missing_first(TestState *);
|
2023-11-14 14:59:13 -05:00
|
|
|
static void null_message(TestState *);
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult null_state(TestState *);
|
2023-11-21 17:45:56 -05:00
|
|
|
static void chk_empty_first(TestState *, void *);
|
|
|
|
static void chk_first(TestState *, void *);
|
|
|
|
static void chk_second(TestState *, void *);
|
2023-11-21 00:12:41 -05:00
|
|
|
static void chk_last(TestState *, void *);
|
2023-11-21 18:26:32 -05:00
|
|
|
static void mf_chk_first(TestState *, void *);
|
|
|
|
static void mf_chk_second(TestState *, void *);
|
|
|
|
static void mf_chk_third(TestState*, void *);
|
2023-11-12 22:47:21 -05:00
|
|
|
static void mk_log_data(LogData *);
|
2023-11-10 16:17:06 -05:00
|
|
|
|
2023-11-12 20:27:07 -05:00
|
|
|
static void chk_ptr_chg(
|
|
|
|
TestState *,
|
|
|
|
const char *,
|
|
|
|
const void *,
|
|
|
|
const void *
|
|
|
|
);
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
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)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
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
|
2023-11-11 16:36:05 -05:00
|
|
|
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");
|
2023-11-21 15:55:58 -05:00
|
|
|
|
|
|
|
// first_log
|
|
|
|
test_context_with(
|
|
|
|
s,
|
|
|
|
"first_log",
|
|
|
|
chk_empty_first,
|
|
|
|
&test
|
|
|
|
);
|
|
|
|
|
|
|
|
// last_log
|
2023-11-21 00:12:41 -05:00
|
|
|
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)
|
|
|
|
{
|
2023-11-12 22:47:21 -05:00
|
|
|
LogData ld;
|
|
|
|
mk_log_data(&ld);
|
|
|
|
append_test_log(&ld.state, "baz");
|
2023-11-21 17:45:56 -05:00
|
|
|
test_context_with(s, "first_log", chk_first, &ld);
|
|
|
|
test_context_with(s, "second log", chk_second, &ld);
|
2023-11-21 00:12:41 -05:00
|
|
|
test_context_with(s, "last_log", chk_last, &ld);
|
2023-11-14 14:16:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
missing_last(TestState *s)
|
|
|
|
{
|
|
|
|
LogData ld;
|
|
|
|
mk_log_data(&ld);
|
|
|
|
ld.state.last_log = 0;
|
|
|
|
append_test_log(&ld.state, "baz");
|
2023-11-21 17:45:56 -05:00
|
|
|
test_context_with(s, "first_log", chk_first, &ld);
|
|
|
|
test_context_with(s, "second log", chk_second, &ld);
|
2023-11-21 00:12:41 -05:00
|
|
|
test_context_with(s, "last_log", chk_last, &ld);
|
2023-11-12 23:05:33 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 14:33:02 -05:00
|
|
|
static void
|
|
|
|
missing_first(TestState *s)
|
|
|
|
{
|
|
|
|
LogData ld;
|
|
|
|
mk_log_data(&ld);
|
|
|
|
ld.state.first_log = 0;
|
|
|
|
append_test_log(&ld.state, "baz");
|
2023-11-21 18:26:32 -05:00
|
|
|
test_context_with(s, "first_log", mf_chk_first, &ld);
|
2023-11-21 00:12:41 -05:00
|
|
|
test_context_with(s, "last_log", chk_last, &ld);
|
2023-11-14 14:33:02 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 14:59:13 -05:00
|
|
|
static void
|
|
|
|
null_message(TestState *s)
|
|
|
|
{
|
|
|
|
LogData ld;
|
|
|
|
mk_log_data(&ld);
|
|
|
|
append_test_log(&ld.state, 0);
|
2023-11-21 00:12:41 -05:00
|
|
|
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first);
|
|
|
|
chk_ptr_eq(s, "last_log", ld.state.last_log, &ld.second);
|
2023-11-14 14:59:13 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
static TestResult
|
|
|
|
null_state(TestState *)
|
2023-11-14 15:16:30 -05:00
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
// ensure it doesn't crash
|
2023-11-14 15:16:30 -05:00
|
|
|
append_test_log(0, "foo");
|
|
|
|
return test_success;
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:45:56 -05:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:05:33 -05:00
|
|
|
static void
|
2023-11-21 00:12:41 -05:00
|
|
|
chk_last(TestState *s, void *ptr)
|
2023-11-12 23:05:33 -05:00
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
LogData *ld = ptr;
|
2023-11-21 13:11:35 -05:00
|
|
|
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);
|
2023-11-21 00:12:41 -05:00
|
|
|
free(ld->state.last_log->text);
|
|
|
|
free(ld->state.last_log);
|
2023-11-12 20:27:07 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 18:26:32 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-12 22:47:21 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-11-12 20:27:07 -05:00
|
|
|
static void
|
|
|
|
chk_ptr_chg(
|
|
|
|
TestState *s,
|
|
|
|
const char *context,
|
|
|
|
const void *actual,
|
|
|
|
const void *prohibited
|
|
|
|
)
|
|
|
|
{
|
2023-11-21 00:12:41 -05:00
|
|
|
test_context_compare(
|
2023-11-12 20:27:07 -05:00
|
|
|
s,
|
2023-11-21 00:12:41 -05:00
|
|
|
context,
|
|
|
|
chk_ptr_chg_test,
|
2023-11-12 20:27:07 -05:00
|
|
|
actual,
|
|
|
|
prohibited
|
|
|
|
);
|
2023-11-21 00:12:41 -05:00
|
|
|
}
|
2023-11-12 20:27:07 -05:00
|
|
|
|
2023-11-21 00:12:41 -05:00
|
|
|
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
|