Skip to content

Commit c5e2b88

Browse files
committed
Make test creation more convinient
Created a separate CMakeLists.txt for tests and generally made test creation more convinient.
1 parent ed9ad24 commit c5e2b88

File tree

5 files changed

+106
-32
lines changed

5 files changed

+106
-32
lines changed

.github/workflows/cmake-multi-platform.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ jobs:
143143
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
144144

145145
- name: Test
146+
if: ${{ matrix.os == 'ubuntu-latest' && matrix.c_compiler != 'mingw-w64-gcc' }}
146147
working-directory: ${{ steps.strings.outputs.build-output-dir }}
147148
# 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).
148149
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
149-
run: ctest --build-config ${{ matrix.build_type }}
150+
run: ctest --output-on-failure --test-dir test --build-config ${{ matrix.build_type }}

CMakeLists.txt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -267,36 +267,8 @@ if (MSVC)
267267
endif (MSVC)
268268
269269
# TESTS:
270-
IF (CMOCKA_FOUND AND NOT OSX AND NOT CLANG)
271-
find_package(Threads REQUIRED)
272-
enable_testing()
273-
add_executable(test_util test/test_util.c src/util.c)
274-
target_include_directories(test_util PRIVATE ${INCLUDE_DIRS})
275-
target_link_libraries(test_util ${CMOCKA_LIBRARY})
276-
add_test(test_util test_util)
277-
278-
add_executable(test_linkedlist test/test_linkedlist.c src/linkedlist.c src/util.c)
279-
target_include_directories(test_linkedlist PRIVATE ${INCLUDE_DIRS})
280-
target_link_libraries(test_linkedlist ${CMOCKA_LIBRARY})
281-
add_test(test_linkedlist test_linkedlist)
282-
283-
add_executable(test_hashtable test/test_hashtable.c src/hashtable.c src/util.c)
284-
target_include_directories(test_hashtable PRIVATE ${INCLUDE_DIRS})
285-
target_link_libraries(test_hashtable ${CMOCKA_LIBRARY})
286-
add_test(test_hashtable test_hashtable)
287-
288-
add_executable(test_pos_heap test/test_pos_heap.c src/pos_heap.c src/util.c)
289-
target_include_directories(test_pos_heap PRIVATE ${INCLUDE_DIRS})
290-
target_link_libraries(test_pos_heap ${CMOCKA_LIBRARY})
291-
add_test(test_pos_heap test_pos_heap)
292-
293-
add_executable(test_input test/test_input.c src/input.c src/keyboard.c)
294-
target_include_directories(test_input PRIVATE ${INCLUDE_DIRS})
295-
target_link_libraries(test_input
296-
${CMOCKA_LIBRARY}
297-
${SDL_LIBRARY}
298-
)
299-
add_test(test_input test_input)
270+
IF (CMOCKA_FOUND AND NOT OSX)
271+
add_subdirectory(test)
300272
ENDIF ()
301273
302274
# LINT:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ clean:
1717
.PHONY: clean
1818

1919
test:
20-
@cmake --build build/debug --target test
20+
@ctest --output-on-failure --test-dir build/debug/test
2121
.PHONY: test
2222

2323
run: $(all)

test/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
find_package(Threads REQUIRED)
2+
enable_testing()
3+
4+
function(add_test TEST_NAME)
5+
message("Adding test: ${TEST_NAME} -> ${ARGN}")
6+
add_executable(${TEST_NAME} ${ARGN})
7+
target_include_directories(${TEST_NAME} PRIVATE ${INCLUDE_DIRS})
8+
target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARY} ${SDL_LIBRARY})
9+
_add_test(${TEST_NAME} ${TEST_NAME})
10+
endfunction()
11+
12+
add_test(test_util test_util.c ../src/util.c)
13+
add_test(test_linkedlist test_linkedlist.c ../src/linkedlist.c ../src/util.c)
14+
add_test(test_hashtable test_hashtable.c ../src/hashtable.c ../src/util.c)
15+
add_test(test_pos_heap test_pos_heap.c ../src/pos_heap.c ../src/util.c)
16+
add_test(test_input test_input.c ../src/input.c ../src/keyboard.c)
17+
add_test(test_position test_position.c ../src/position.c)

test/test_position.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* BreakHack - A dungeone crawler RPG
3+
* Copyright (C) 2025 Linus Probert <linus.probert@gmail.com>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
#include <stdbool.h>
19+
#include <stdio.h>
20+
#include <stdarg.h>
21+
#include <stddef.h>
22+
#include <stdlib.h>
23+
#include <setjmp.h>
24+
#include <cmocka.h>
25+
#include "../src/position.h"
26+
#include "../src/defines.h"
27+
28+
#define assert_pos_eq(a, b) \
29+
do { \
30+
assert_int_equal(a.x, b.x); \
31+
assert_int_equal(a.y, b.y); \
32+
} while (0)
33+
34+
static void test_position_equals(void **state)
35+
{
36+
Position a = POS(1, 1);
37+
Position b = POS(1, 1);
38+
Position c = POS(1, 2);
39+
40+
assert_true(position_equals(&a, &b));
41+
assert_false(position_equals(&a, &c));
42+
}
43+
44+
static void test_matrix_coords_conversion(void **state)
45+
{
46+
Position pos;
47+
Position mc;
48+
Position expected;
49+
50+
pos = POS(1, 1);
51+
expected = POS(0, 0);
52+
mc = position_to_matrix_coords(&pos);
53+
assert_pos_eq(mc, expected);
54+
55+
pos = POS(TILE_DIMENSION * 5, TILE_DIMENSION * 5);
56+
expected = POS(5, 5);
57+
mc = position_to_matrix_coords(&pos);
58+
assert_pos_eq(mc, expected);
59+
60+
pos = POS(TILE_DIMENSION * 5 + 1, TILE_DIMENSION * 5 + 1);
61+
expected = POS(5, 5);
62+
mc = position_to_matrix_coords(&pos);
63+
assert_pos_eq(mc, expected);
64+
65+
pos = POS(TILE_DIMENSION * 5 + TILE_DIMENSION - 1, TILE_DIMENSION * 5 + TILE_DIMENSION - 1);
66+
expected = POS(5, 5);
67+
mc = position_to_matrix_coords(&pos);
68+
assert_pos_eq(mc, expected);
69+
70+
pos = POS(TILE_DIMENSION * 5 + TILE_DIMENSION, TILE_DIMENSION * 5 + TILE_DIMENSION);
71+
expected = POS(6, 6);
72+
mc = position_to_matrix_coords(&pos);
73+
assert_pos_eq(mc, expected);
74+
}
75+
76+
int main(void)
77+
{
78+
const struct CMUnitTest tests[] = {
79+
cmocka_unit_test(test_position_equals),
80+
cmocka_unit_test(test_matrix_coords_conversion),
81+
};
82+
83+
return cmocka_run_group_tests(tests, NULL, NULL);
84+
}

0 commit comments

Comments
 (0)