diff --git a/src/collisions.c b/src/collisions.c index 6d86a232..27df31bf 100644 --- a/src/collisions.c +++ b/src/collisions.c @@ -19,8 +19,8 @@ #include "collisions.h" bool -position_in_rect(Position *p, SDL_Rect *r) +position_in_rect(const Position *p, const SDL_Rect *r) { - return r->x <= p->x && r->x + r->w >= p->x && - r->y <= p->y && r->y + r->h >= p->y; + SDL_Point point = { p->x, p->y }; + return SDL_PointInRect(&point, r); } diff --git a/src/collisions.h b/src/collisions.h index b57b2b06..0c575bde 100644 --- a/src/collisions.h +++ b/src/collisions.h @@ -25,6 +25,6 @@ #include "position.h" bool -position_in_rect(Position*, SDL_Rect*); +position_in_rect(const Position*, const SDL_Rect*); #endif // COLLISIONS_H_ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 23ad210b..7dd60a58 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,3 +15,4 @@ 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) +add_test(test_collisions test_collisions.c ../src/collisions.c) diff --git a/test/cmocka_include.h b/test/cmocka_include.h new file mode 100644 index 00000000..8001bd7a --- /dev/null +++ b/test/cmocka_include.h @@ -0,0 +1,24 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 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 diff --git a/test/test_collisions.c b/test/test_collisions.c new file mode 100644 index 00000000..d6e3cc54 --- /dev/null +++ b/test/test_collisions.c @@ -0,0 +1,49 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 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 "cmocka_include.h" +#include "../src/collisions.h" + +static void test_position_in_rect(void **state) +{ + const SDL_Rect r = {0, 0, 2, 2}; + Position p; + + p = POS(1, 1); + assert_true(position_in_rect(&p, &r)); + + p = POS(2, 2); + assert_false(position_in_rect(&p, &r)); + + p = POS(0, 0); + assert_true(position_in_rect(&p, &r)); + + p = POS(2, 0); + assert_false(position_in_rect(&p, &r)); + + p = POS(0, 2); + assert_false(position_in_rect(&p, &r)); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_position_in_rect), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/test_hashtable.c b/test/test_hashtable.c index fa2da923..c3ccf891 100644 --- a/test/test_hashtable.c +++ b/test/test_hashtable.c @@ -16,13 +16,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include -#include -#include +#include "cmocka_include.h" #include "../src/hashtable.h" #include "../src/util.h" diff --git a/test/test_input.c b/test/test_input.c index 7185b9a0..17798e01 100644 --- a/test/test_input.c +++ b/test/test_input.c @@ -17,11 +17,7 @@ */ #include -#include -#include -#include -#include -#include +#include "cmocka_include.h" #include "../src/input.h" static void diff --git a/test/test_linkedlist.c b/test/test_linkedlist.c index 8ae3e8a2..eb474edf 100644 --- a/test/test_linkedlist.c +++ b/test/test_linkedlist.c @@ -16,11 +16,7 @@ * along with this program. If not, see . */ -#include -#include -#include -#include -#include +#include "cmocka_include.h" #include "../src/linkedlist.h" static void test_linkedlist_create(void **state) diff --git a/test/test_pos_heap.c b/test/test_pos_heap.c index 1996625a..65fd86c3 100644 --- a/test/test_pos_heap.c +++ b/test/test_pos_heap.c @@ -15,13 +15,7 @@ * 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 "cmocka_include.h" #include "../src/pos_heap.h" #include "../src/util.h" diff --git a/test/test_position.c b/test/test_position.c index bedca6b8..be5b1a1e 100644 --- a/test/test_position.c +++ b/test/test_position.c @@ -15,13 +15,7 @@ * 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 "cmocka_include.h" #include "../src/position.h" #include "../src/defines.h" @@ -73,11 +67,37 @@ static void test_matrix_coords_conversion(void **state) assert_pos_eq(mc, expected); } +static void test_map_coords_conversion(void **state) +{ + Position pos, coords, expected; + + pos = POS(0, 0); + coords = position_to_room_coords(&pos); + expected = POS(0, 0); + assert_pos_eq(expected, coords); + + pos = POS(MAP_ROOM_WIDTH * TILE_DIMENSION - 1, MAP_ROOM_HEIGHT * TILE_DIMENSION - 1); + coords = position_to_room_coords(&pos); + expected = POS(0, 0); + assert_pos_eq(expected, coords); + + pos = POS(MAP_ROOM_WIDTH * TILE_DIMENSION, MAP_ROOM_HEIGHT * TILE_DIMENSION); + coords = position_to_room_coords(&pos); + expected = POS(1, 1); + assert_pos_eq(expected, coords); + + pos = POS(MAP_ROOM_WIDTH * TILE_DIMENSION + TILE_DIMENSION * 5, MAP_ROOM_HEIGHT * TILE_DIMENSION + TILE_DIMENSION * 5); + coords = position_to_room_coords(&pos); + expected = POS(1, 1); + assert_pos_eq(expected, coords); +} + int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_position_equals), cmocka_unit_test(test_matrix_coords_conversion), + cmocka_unit_test(test_map_coords_conversion), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/test/test_util.c b/test/test_util.c index 09d2487d..1a397af2 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -15,12 +15,7 @@ * 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 "cmocka_include.h" #include "../src/util.h" static void test_util_ec_malloc(void **state)