From 875a06c44c45f881e955fd52332be88d85ec33a4 Mon Sep 17 00:00:00 2001 From: Periicles Date: Tue, 16 Jun 2026 18:40:09 +0200 Subject: [PATCH] test: cover the menu state machine and more helpers Add 10 criterion tests: - test_menu: exercises the menu transitions (push/pop/set_tab) and every predicate (overworld, hud, pause, pause tabs, settings tabs, win/lose), locking in that the named helpers behave like the old decimal arithmetic. - test_str_more: my_strcat, my_strndup, my_str_to_word_array, and my_put_nbr / my_float (captured through a temp fd) across the full int range. Suite is now 20 tests, all passing under `make tests_run` and `make tests_asan` (no ASan/UBSan findings). --- tests/test_menu.c | 89 +++++++++++++++++++++++++++++++++++++++++++ tests/test_str_more.c | 81 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 tests/test_menu.c create mode 100644 tests/test_str_more.c diff --git a/tests/test_menu.c b/tests/test_menu.c new file mode 100644 index 0000000..3663194 --- /dev/null +++ b/tests/test_menu.c @@ -0,0 +1,89 @@ +/* +** EPITECH PROJECT, 2023 +** rpg +** File description: +** tests for the named menu state machine (behaviour must match the old +** raw decimal arithmetic) +*/ + +#include +#include "game.h" +#include "menu.h" + +Test(menu_transitions, push_pop_set_tab) +{ + game_t g = {0}; + + g.menu = MENU_GAME; + menu_push(&g, SCREEN_PAUSE); + cr_assert_eq(g.menu, 56); + menu_pop(&g); + cr_assert_eq(g.menu, 5); + g.menu = MENU_PAUSE_CHARACTER; + menu_set_tab(&g, 2); + cr_assert_eq(g.menu, MENU_PAUSE_QUEST); + g.menu = 0; + menu_push(&g, SCREEN_SAVE); + cr_assert_eq(g.menu, 3); +} + +Test(menu_predicates, start_overworld_hud) +{ + game_t g = {0}; + + cr_assert(menu_is_start(&g)); + g.menu = MENU_GAME; + cr_assert(menu_in_overworld(&g)); + cr_assert(menu_show_hud(&g)); + cr_assert(!menu_is_start(&g)); + g.menu = 56; + cr_assert(!menu_in_overworld(&g)); + cr_assert(!menu_show_hud(&g)); + cr_assert(menu_pause_open(&g)); +} + +Test(menu_predicates, pause_tabs) +{ + game_t g = {0}; + + g.menu = MENU_PAUSE_INVENTORY; + cr_assert(menu_tab_inventory(&g)); + cr_assert(menu_in_pause_tab(&g)); + cr_assert(menu_pause_active(&g)); + g.menu = MENU_PAUSE_CHARACTER; + cr_assert(menu_tab_character(&g)); + g.menu = MENU_PAUSE_QUEST; + cr_assert(menu_tab_quest(&g)); + cr_assert(!menu_tab_inventory(&g)); +} + +Test(menu_predicates, settings_tabs) +{ + game_t g = {0}; + + g.menu = 21; + cr_assert(menu_tab_music(&g)); + g.menu = 22; + cr_assert(menu_tab_keyboard(&g)); + g.menu = 24; + cr_assert(menu_tab_window(&g)); + g.menu = 25; + cr_assert(menu_tab_fps(&g)); + g.menu = MENU_SETTINGS; + cr_assert(menu_in_settings(&g)); +} + +Test(menu_predicates, win_and_lose) +{ + game_t g = {0}; + + g.is_finished = 1; + g.menu = MENU_WIN; + cr_assert(menu_won(&g)); + cr_assert(!menu_lost(&g)); + g.menu = MENU_LOSE; + cr_assert(menu_lost(&g)); + cr_assert(!menu_won(&g)); + g.is_finished = 0; + cr_assert(!menu_lost(&g)); +} diff --git a/tests/test_str_more.c b/tests/test_str_more.c new file mode 100644 index 0000000..241ab98 --- /dev/null +++ b/tests/test_str_more.c @@ -0,0 +1,81 @@ +/* +** EPITECH PROJECT, 2023 +** rpg +** File description: +** tests for the remaining string/number helpers +*/ + +#include +#include +#include +#include +#include +#include "my_str.h" + +Test(my_strcat, joins_two_strings) +{ + char *r = my_strcat("foo", "bar"); + + cr_assert_str_eq(r, "foobar"); + free(r); + r = my_strcat("", "x"); + cr_assert_str_eq(r, "x"); + free(r); +} + +Test(my_strndup, copies_first_n) +{ + char *r = my_strndup("hello", 3); + + cr_assert_str_eq(r, "hel"); + free(r); +} + +Test(my_str_to_word_array, splits_on_separator) +{ + char **t = my_str_to_word_array("a b c", ' '); + + cr_assert_str_eq(t[0], "a"); + cr_assert_str_eq(t[1], "b"); + cr_assert_str_eq(t[2], "c"); + cr_assert_null(t[3]); + free_tab(t); +} + +static void check_put_nbr(int nb, const char *expected) +{ + char buf[64] = {0}; + int fd = open("/tmp/cr_put_nbr", O_CREAT | O_TRUNC | O_RDWR, 0644); + + my_put_nbr(nb, fd); + lseek(fd, 0, SEEK_SET); + cr_assert(read(fd, buf, sizeof(buf) - 1) >= 0); + close(fd); + cr_assert_str_eq(buf, expected); +} + +Test(my_put_nbr, writes_full_int_range) +{ + check_put_nbr(0, "0"); + check_put_nbr(-5, "-5"); + check_put_nbr(INT_MIN, "-2147483648"); + check_put_nbr(INT_MAX, "2147483647"); +} + +static void check_float(double n, const char *expected) +{ + char buf[64] = {0}; + int fd = open("/tmp/cr_float", O_CREAT | O_TRUNC | O_RDWR, 0644); + + my_float(2, n, fd); + lseek(fd, 0, SEEK_SET); + cr_assert(read(fd, buf, sizeof(buf) - 1) >= 0); + close(fd); + cr_assert_str_eq(buf, expected); +} + +Test(my_float, writes_two_decimals) +{ + check_float(3.14, "3.14"); + check_float(-1.5, "-1.50"); +}