From eecf4b1e666211a13afa56f93477c55e8fd01621 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Jun 2022 19:51:54 +0200 Subject: feat: implement game of life --- test/cell_helper_test.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 test/cell_helper_test.cpp (limited to 'test/cell_helper_test.cpp') diff --git a/test/cell_helper_test.cpp b/test/cell_helper_test.cpp new file mode 100644 index 0000000..b570aba --- /dev/null +++ b/test/cell_helper_test.cpp @@ -0,0 +1,99 @@ +#include "engine/data/vector2.hpp" +#include "game/cell_helper.hpp" + +#include "mocks/matrix.hpp" + +#include + +#include +#include + +class CellHelperTest : public testing::Test +{ +protected: +}; + +TEST_F(CellHelperTest, IsCellDying) +{ + const auto mock_matrix = MockMatrix(); + + const auto rows = 8U; + const auto columns = 6U; + + const auto position = Vector2({.x = 3, .y = 5}); + + EXPECT_CALL(mock_matrix, get_row_cnt()).WillRepeatedly(testing::Return(rows)); + + EXPECT_CALL(mock_matrix, get_column_cnt()).WillRepeatedly(testing::Return(columns)); + + EXPECT_CALL(mock_matrix, get(position + Vector2::up())) + .WillOnce(testing::Return('x')) + .WillOnce(testing::Return(' ')) + .WillOnce(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::down())) + .WillRepeatedly(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::left())) + .WillOnce(testing::Return('x')) + .WillOnce(testing::Return('x')) + .WillOnce(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::right())) + .WillOnce(testing::Return('x')) + .WillOnce(testing::Return(' ')) + .WillOnce(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::left())) + .WillRepeatedly(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::up() + Vector2::right())) + .WillRepeatedly(testing::Return('x')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::left())) + .WillRepeatedly(testing::Return(' ')); + + EXPECT_CALL(mock_matrix, get(position + Vector2::down() + Vector2::right())) + .WillRepeatedly(testing::Return(' ')); + + auto cell_helper = CellHelper(mock_matrix); + + EXPECT_EQ(cell_helper.is_cell_dying(position), true); + + EXPECT_EQ(cell_helper.is_cell_dying(position), false); + + EXPECT_EQ(cell_helper.is_cell_dying(position), true); +} + +TEST_F(CellHelperTest, FindNeighbours) +{ + const auto mock_matrix = MockMatrix(); + + const auto rows = 8U; + const auto columns = 6U; + + const auto position = Vector2({.x = 3, .y = 5}); + + EXPECT_CALL(mock_matrix, get_row_cnt()).WillOnce(testing::Return(rows)); + + EXPECT_CALL(mock_matrix, get_column_cnt()).WillOnce(testing::Return(columns)); + + auto cell_helper = CellHelper(mock_matrix); + + const auto living_neighbour_cells = cell_helper.find_neighbours(position); + + EXPECT_EQ(living_neighbour_cells.size(), 8U); + + EXPECT_THAT( + living_neighbour_cells, + testing::ContainerEq(std::list( + {position + Vector2::up(), + position + Vector2::down(), + position + Vector2::left(), + position + Vector2::right(), + position + Vector2::up() + Vector2::left(), + position + Vector2::up() + Vector2::right(), + position + Vector2::down() + Vector2::left(), + position + Vector2::down() + Vector2::right()}))); +} + -- cgit v1.2.3-18-g5258