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-12 20:27:07 -05:00
|
|
|
#include <stdio.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-14 14:16:21 -05:00
|
|
|
static void chk_last(TestState *, LogData *, const char *);
|
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 char *,
|
|
|
|
const void *,
|
|
|
|
const 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)
|
|
|
|
{
|
|
|
|
print("append_test_log()\n");
|
|
|
|
append_to_empty(s);
|
2023-11-12 19:37:19 -05:00
|
|
|
append_to_existing(s);
|
2023-11-14 14:16:21 -05:00
|
|
|
missing_last(s);
|
2023-11-14 14:33:02 -05:00
|
|
|
missing_first(s);
|
2023-11-14 14:59:13 -05:00
|
|
|
null_message(s);
|
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;
|
|
|
|
print("\tappend to empty\n");
|
|
|
|
mk_sample_state(&test);
|
|
|
|
append_test_log(&test, "foo");
|
2023-11-11 16:36:05 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
2023-11-11 16:49:52 -05:00
|
|
|
|
2023-11-11 17:53:04 -05:00
|
|
|
// 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"
|
|
|
|
);
|
|
|
|
|
2023-11-11 16:49:52 -05:00
|
|
|
// 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
|
|
|
|
);
|
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;
|
2023-11-12 19:37:19 -05:00
|
|
|
print("\tappend to existing\n");
|
2023-11-12 22:47:21 -05:00
|
|
|
mk_log_data(&ld);
|
|
|
|
append_test_log(&ld.state, "baz");
|
2023-11-12 19:49:33 -05:00
|
|
|
|
|
|
|
// first shouldn't change
|
|
|
|
print("\t\tfirst_log\n");
|
|
|
|
chk_ptr_eq(
|
|
|
|
s,
|
2023-11-14 14:16:21 -05:00
|
|
|
"ERROR: append_test_log(): append to existing: first_log:",
|
2023-11-12 22:47:21 -05:00
|
|
|
ld.state.first_log,
|
|
|
|
&ld.first
|
2023-11-12 19:49:33 -05:00
|
|
|
);
|
2023-11-12 20:27:07 -05:00
|
|
|
|
2023-11-14 14:16:21 -05:00
|
|
|
chk_last(
|
|
|
|
s,
|
|
|
|
&ld,
|
|
|
|
"ERROR: append_test_log(): append to existing: last_log:"
|
|
|
|
);
|
|
|
|
free(ld.state.last_log);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:"
|
|
|
|
);
|
2023-11-14 13:43:06 -05:00
|
|
|
free(ld.state.last_log);
|
2023-11-12 23:05:33 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 14:33:02 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-14 14:59:13 -05:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:05:33 -05:00
|
|
|
static void
|
2023-11-14 14:16:21 -05:00
|
|
|
chk_last(TestState *s, LogData *ld, const char *context)
|
2023-11-12 23:05:33 -05:00
|
|
|
{
|
2023-11-12 20:27:07 -05:00
|
|
|
print("\t\tlast_log\n");
|
|
|
|
chk_ptr_chg(
|
|
|
|
s,
|
|
|
|
"\t\t\t",
|
2023-11-14 14:16:21 -05:00
|
|
|
context,
|
2023-11-12 23:05:33 -05:00
|
|
|
ld->state.last_log,
|
|
|
|
&ld->second
|
2023-11-12 20:27:07 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 *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(
|
|
|
|
s,
|
|
|
|
str,
|
|
|
|
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
|
|
|
|
);
|
2023-11-12 19:37:19 -05:00
|
|
|
}
|
|
|
|
|
2023-11-10 15:30:44 -05:00
|
|
|
//jl
|