-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make test creation more convinient (#86)
Created a separate CMakeLists.txt for tests and generally made test creation more convinient.
- Loading branch information
1 parent
ed9ad24
commit 17e644c
Showing
5 changed files
with
106 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* BreakHack - A dungeone crawler RPG | ||
* Copyright (C) 2025 Linus Probert <[email protected]> | ||
* | ||
* 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); | ||
} |