Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
32 changes: 2 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
84 changes: 84 additions & 0 deletions test/test_position.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2025 Linus Probert <linus.probert@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <cmocka.h>
#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);
}
Loading