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
6 changes: 3 additions & 3 deletions src/collisions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/collisions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 24 additions & 0 deletions test/cmocka_include.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 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>
49 changes: 49 additions & 0 deletions test/test_collisions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 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 "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);
}
8 changes: 1 addition & 7 deletions test/test_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
* 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 "cmocka_include.h"
#include "../src/hashtable.h"
#include "../src/util.h"

Expand Down
6 changes: 1 addition & 5 deletions test/test_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
*/

#include <SDL3/SDL.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <cmocka.h>
#include "cmocka_include.h"
#include "../src/input.h"

static void
Expand Down
6 changes: 1 addition & 5 deletions test/test_linkedlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <setjmp.h>
#include <cmocka.h>
#include "cmocka_include.h"
#include "../src/linkedlist.h"

static void test_linkedlist_create(void **state)
Expand Down
8 changes: 1 addition & 7 deletions test/test_pos_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
* 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 "cmocka_include.h"
#include "../src/pos_heap.h"
#include "../src/util.h"

Expand Down
34 changes: 27 additions & 7 deletions test/test_position.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
* 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 "cmocka_include.h"
#include "../src/position.h"
#include "../src/defines.h"

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions test/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
* 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 <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include "cmocka_include.h"
#include "../src/util.h"

static void test_util_ec_malloc(void **state)
Expand Down
Loading