Compare commits

...

3 Commits

Author SHA1 Message Date
jlamothe 6539b87ea9 warn to log instead of stderr
When the reindex function rebuilds the log because of a missing first_log value, it warns about a potential memory leak to the log itself.
2023-11-21 23:26:32 +00:00
jlamothe 00ffef2f0a more thorough testing of append_test_log() 2023-11-21 22:45:56 +00:00
jlamothe cb3788cf02 check next value for append_test_log() on empty log 2023-11-21 20:55:58 +00:00
2 changed files with 97 additions and 7 deletions

View File

@ -350,7 +350,7 @@ print_log(TestState *s)
TestLogEntry *e = s->first_log; TestLogEntry *e = s->first_log;
while(e) while(e)
{ {
if(e->text) print("%s\n", e->text); if(e->text) print("\n%s", e->text);
else print("(empty message)\n"); else print("(empty message)\n");
e = e->next; e = e->next;
} }
@ -389,7 +389,8 @@ reindex(TestState *s)
else if (s->last_log) // we have a last log but no first? else if (s->last_log) // we have a last log but no first?
{ {
s->first_log = s->last_log; s->first_log = s->last_log;
fprint(2, "potential memory leak in test log\n"); log_test_context(s);
append_test_log(s, "potential memory leak in test log");
} }
} }

View File

@ -44,7 +44,13 @@ static void missing_last(TestState *);
static void missing_first(TestState *); static void missing_first(TestState *);
static void null_message(TestState *); static void null_message(TestState *);
static TestResult null_state(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 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 *); static void mk_log_data(LogData *);
static void chk_ptr_chg( static void chk_ptr_chg(
@ -77,8 +83,16 @@ append_to_empty(TestState *s)
TestState test; TestState test;
mk_sample_state(&test); mk_sample_state(&test);
append_test_log(&test, "foo"); append_test_log(&test, "foo");
chk_ptr_ne(s, "first_log", test.first_log, 0);
chk_str_eq(s, "first_log->text", test.first_log->text, "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); chk_ptr_eq(s, "last_log", test.last_log, test.first_log);
} }
@ -88,7 +102,8 @@ append_to_existing(TestState *s)
LogData ld; LogData ld;
mk_log_data(&ld); mk_log_data(&ld);
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first); 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); test_context_with(s, "last_log", chk_last, &ld);
} }
@ -99,7 +114,8 @@ missing_last(TestState *s)
mk_log_data(&ld); mk_log_data(&ld);
ld.state.last_log = 0; ld.state.last_log = 0;
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.first); 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); test_context_with(s, "last_log", chk_last, &ld);
} }
@ -110,7 +126,7 @@ missing_first(TestState *s)
mk_log_data(&ld); mk_log_data(&ld);
ld.state.first_log = 0; ld.state.first_log = 0;
append_test_log(&ld.state, "baz"); append_test_log(&ld.state, "baz");
chk_ptr_eq(s, "first_log", ld.state.first_log, &ld.second); test_context_with(s, "first_log", mf_chk_first, &ld);
test_context_with(s, "last_log", chk_last, &ld); test_context_with(s, "last_log", chk_last, &ld);
} }
@ -132,6 +148,48 @@ null_state(TestState *)
return test_success; 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 static void
chk_last(TestState *s, void *ptr) chk_last(TestState *s, void *ptr)
{ {
@ -143,6 +201,37 @@ chk_last(TestState *s, void *ptr)
free(ld->state.last_log); 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 static void
mk_log_data(LogData *ld) mk_log_data(LogData *ld)
{ {