diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 44b0b7d8..b2466abf 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -143,7 +143,8 @@ jobs: run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} - name: Test + if: ${{ matrix.os == 'ubuntu-latest' && matrix.c_compiler != 'mingw-w64-gcc' }} working-directory: ${{ steps.strings.outputs.build-output-dir }} # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest --build-config ${{ matrix.build_type }} + run: ctest --output-on-failure --test-dir test --build-config ${{ matrix.build_type }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bb5df0b..f54c4a75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -267,36 +267,8 @@ if (MSVC) endif (MSVC) # TESTS: -IF (CMOCKA_FOUND AND NOT OSX AND NOT CLANG) - find_package(Threads REQUIRED) - enable_testing() - add_executable(test_util test/test_util.c src/util.c) - target_include_directories(test_util PRIVATE ${INCLUDE_DIRS}) - target_link_libraries(test_util ${CMOCKA_LIBRARY}) - add_test(test_util test_util) - - add_executable(test_linkedlist test/test_linkedlist.c src/linkedlist.c src/util.c) - target_include_directories(test_linkedlist PRIVATE ${INCLUDE_DIRS}) - target_link_libraries(test_linkedlist ${CMOCKA_LIBRARY}) - add_test(test_linkedlist test_linkedlist) - - add_executable(test_hashtable test/test_hashtable.c src/hashtable.c src/util.c) - target_include_directories(test_hashtable PRIVATE ${INCLUDE_DIRS}) - target_link_libraries(test_hashtable ${CMOCKA_LIBRARY}) - add_test(test_hashtable test_hashtable) - - add_executable(test_pos_heap test/test_pos_heap.c src/pos_heap.c src/util.c) - target_include_directories(test_pos_heap PRIVATE ${INCLUDE_DIRS}) - target_link_libraries(test_pos_heap ${CMOCKA_LIBRARY}) - add_test(test_pos_heap test_pos_heap) - - add_executable(test_input test/test_input.c src/input.c src/keyboard.c) - target_include_directories(test_input PRIVATE ${INCLUDE_DIRS}) - target_link_libraries(test_input - ${CMOCKA_LIBRARY} - ${SDL_LIBRARY} - ) - add_test(test_input test_input) +IF (CMOCKA_FOUND AND NOT OSX) + add_subdirectory(test) ENDIF () # LINT: diff --git a/Makefile b/Makefile index e08dd2ae..f2fccc70 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ clean: .PHONY: clean test: - @cmake --build build/debug --target test + @ctest --output-on-failure --test-dir build/debug/test .PHONY: test run: $(all) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..23ad210b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,17 @@ +find_package(Threads REQUIRED) +enable_testing() + +function(add_test TEST_NAME) + message("Adding test: ${TEST_NAME} -> ${ARGN}") + add_executable(${TEST_NAME} ${ARGN}) + target_include_directories(${TEST_NAME} PRIVATE ${INCLUDE_DIRS}) + target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARY} ${SDL_LIBRARY}) + _add_test(${TEST_NAME} ${TEST_NAME}) +endfunction() + +add_test(test_util test_util.c ../src/util.c) +add_test(test_linkedlist test_linkedlist.c ../src/linkedlist.c ../src/util.c) +add_test(test_hashtable test_hashtable.c ../src/hashtable.c ../src/util.c) +add_test(test_pos_heap test_pos_heap.c ../src/pos_heap.c ../src/util.c) +add_test(test_input test_input.c ../src/input.c ../src/keyboard.c) +add_test(test_position test_position.c ../src/position.c) diff --git a/test/test_position.c b/test/test_position.c new file mode 100644 index 00000000..bedca6b8 --- /dev/null +++ b/test/test_position.c @@ -0,0 +1,84 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2025 Linus Probert + * + * This program 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. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include "../src/position.h" +#include "../src/defines.h" + +#define assert_pos_eq(a, b) \ + do { \ + assert_int_equal(a.x, b.x); \ + assert_int_equal(a.y, b.y); \ + } while (0) + +static void test_position_equals(void **state) +{ + Position a = POS(1, 1); + Position b = POS(1, 1); + Position c = POS(1, 2); + + assert_true(position_equals(&a, &b)); + assert_false(position_equals(&a, &c)); +} + +static void test_matrix_coords_conversion(void **state) +{ + Position pos; + Position mc; + Position expected; + + pos = POS(1, 1); + expected = POS(0, 0); + mc = position_to_matrix_coords(&pos); + assert_pos_eq(mc, expected); + + pos = POS(TILE_DIMENSION * 5, TILE_DIMENSION * 5); + expected = POS(5, 5); + mc = position_to_matrix_coords(&pos); + assert_pos_eq(mc, expected); + + pos = POS(TILE_DIMENSION * 5 + 1, TILE_DIMENSION * 5 + 1); + expected = POS(5, 5); + mc = position_to_matrix_coords(&pos); + assert_pos_eq(mc, expected); + + pos = POS(TILE_DIMENSION * 5 + TILE_DIMENSION - 1, TILE_DIMENSION * 5 + TILE_DIMENSION - 1); + expected = POS(5, 5); + mc = position_to_matrix_coords(&pos); + assert_pos_eq(mc, expected); + + pos = POS(TILE_DIMENSION * 5 + TILE_DIMENSION, TILE_DIMENSION * 5 + TILE_DIMENSION); + expected = POS(6, 6); + mc = position_to_matrix_coords(&pos); + assert_pos_eq(mc, expected); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_position_equals), + cmocka_unit_test(test_matrix_coords_conversion), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}