/* 9unit Copyright (C) Jonathan Lamothe 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 . */ #include #include #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 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"); chk_ptr_ne(s, "first_log", test.first_log, 0); chk_str_eq(s, "first_log->text", test.first_log->text, "foo"); chk_ptr_eq(s, "last_log", test.last_log, test.first_log); } 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, "last_log", ld->state.last_log, &ld->second); chk_str_eq(s, "last_log->text", ld->state.last_log->text, "baz"); 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