diff --git a/message.c b/message.c index 86311b7..455f7cd 100644 --- a/message.c +++ b/message.c @@ -33,7 +33,6 @@ ¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ ¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ */ -#include "config.h" #include "message.h" #include @@ -61,9 +60,10 @@ bool message_printer_add(MessagePrinter *printer) { bool ret = false; if((printer != NULL) && (printer->open != NULL)) { ret = printer->open(printer); - printer->next = g_message_printer_head; - g_message_printer_head = printer; - ret = true; + if(ret) { + printer->next = g_message_printer_head; + g_message_printer_head = printer; + } } return ret; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2cf4901..e925998 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -28,6 +28,14 @@ function(add_unit_test) ) endfunction() +add_unit_test( + NAME message + SOURCES + message.c + LIBRARIES cwalk + INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR} +) + add_unit_test( NAME memory SOURCES diff --git a/test/message.c b/test/message.c new file mode 100644 index 0000000..b5680b9 --- /dev/null +++ b/test/message.c @@ -0,0 +1,118 @@ +/* +¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ +¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ + + __/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__ + \_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_ + _> ___/ ¯>__> <<__// __ _/ |> > <<__// /\ // __ _/ + _> \7 <__/:. \__/:. \> \_/ L/ _____/. 7> .\_/:. \__/ <_/ \_ +|:::::::::::::::::::::::/:::::::::::::>::::::::/::::::::::::::::::::::::/:::::| +|¯¯\::::/\:/¯\::::/¯¯¯¯<::::/\::/¯¯\:/¯¯¯¯¯¯\::\::/¯¯\::::/¯¯\::::/¯¯¯¯<::::/¯| +|__ |¯¯| T _ |¯¯¯| ___ |¯¯| |¯| _ T ______ |¯¯¯¯| _ |¯¯¯| _ |¯¯¯| ___ |¯¯| _| + \|__|/\|/ \|___|/ \|__|/\|_|/ \|/ \| |/ \|___|/ \|___|/dNo\|__|/ + +¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ +¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ + + This file is part of Etripator, + copyright (c) 2009--2023 Vincent Cruz. + + Etripator is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Etripator 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Etripator. If not, see . + +¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ +¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ +*/ +#include + +#include "../message.c" + +void* setup(const MunitParameter params[] __attribute__((unused)), void* user_data __attribute__((unused))) { + return NULL; +} + +void tear_down(void* fixture __attribute__((unused))) { +} + +MunitResult message_init_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { + g_message_printer_head = (MessagePrinter*)0xBEEF; + message_printer_init(); + munit_assert_ptr_null(g_message_printer_head); + + return MUNIT_OK; +} + +static unsigned int dummy_open_0_call_count; +static bool dummy_open_0(MessagePrinter *printer) { + dummy_open_0_call_count++; + return true; +} +static bool dummy_open_1(MessagePrinter *printer) { + dummy_open_0_call_count++; + return false; +} + +MunitResult message_add_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) { + message_printer_init(); + munit_assert_ptr_null(g_message_printer_head); + + MessagePrinter printer[4] = { + [0] = { .open = dummy_open_0 }, + [1] = { .open = dummy_open_0 }, + [2] = { .open = dummy_open_1 }, + [3] = { .open = dummy_open_0 }, + }; + + dummy_open_0_call_count = 0; + munit_assert_true(message_printer_add(&printer[0])); + munit_assert_uint(dummy_open_0_call_count, ==, 1); + munit_assert_ptr_equal(g_message_printer_head, &printer[0]); + munit_assert_null(g_message_printer_head->next); + + munit_assert_true(message_printer_add(&printer[1])); + munit_assert_uint(dummy_open_0_call_count, ==, 2); + munit_assert_ptr_equal(g_message_printer_head, &printer[1]); + munit_assert_ptr_equal(g_message_printer_head->next, &printer[0]); + munit_assert_null(printer[0].next); + + munit_assert_false(message_printer_add(&printer[2])); + munit_assert_uint(dummy_open_0_call_count, ==, 3); + munit_assert_ptr_equal(g_message_printer_head, &printer[1]); + munit_assert_ptr_equal(g_message_printer_head->next, &printer[0]); + munit_assert_null(g_message_printer_head->next->next); + + munit_assert_true(message_printer_add(&printer[3])); + munit_assert_uint(dummy_open_0_call_count, ==, 4); + munit_assert_ptr_equal(g_message_printer_head, &printer[3]); + munit_assert_ptr_equal(g_message_printer_head->next, &printer[1]); + munit_assert_ptr_equal(g_message_printer_head->next->next, &printer[0]); + munit_assert_null(g_message_printer_head->next->next->next); + + return MUNIT_OK; +} + +static MunitTest message_tests[] = { + { "/init", message_init_test, setup, tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + { "/add", message_add_test, setup, tear_down, MUNIT_TEST_OPTION_NONE, NULL }, + // [todo] destroy + // [todo] print + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; + +static const MunitSuite message_suite = { + "message testt suite", message_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE +}; + +int main (int argc, char* const* argv) { + return munit_suite_main(&message_suite, NULL, argc, argv); +} \ No newline at end of file