aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-06-02 19:51:54 +0200
committerHampusM <hampus@hampusmat.com>2022-06-13 17:57:00 +0200
commiteecf4b1e666211a13afa56f93477c55e8fd01621 (patch)
tree410510d6e058995174d5a5b0f535fb457a0c3542 /test
parent87f55120f96d0f4f80b497dc9006d89df2dda125 (diff)
feat: implement game of lifev0.1.0
Diffstat (limited to 'test')
-rw-r--r--test/CMakeLists.txt41
-rw-r--r--test/cell_helper_test.cpp99
-rw-r--r--test/function.test.cpp52
-rw-r--r--test/main.cpp2
-rw-r--r--test/mocks/matrix.hpp31
-rw-r--r--test/string_matrix.test.cpp53
6 files changed, 152 insertions, 126 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 243c1dd..0c74bf2 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,18 +1,20 @@
+project(tests CXX)
+
+enable_testing()
+
file(GLOB SOURCES
- main.cpp
- string_matrix.test.cpp
- "function.test.cpp"
+ cell_helper_test.cpp
+ ${CMAKE_SOURCE_DIR}/src/game/cell_helper.cpp
${CMAKE_SOURCE_DIR}/src/engine/data/vector2.cpp
${CMAKE_SOURCE_DIR}/src/engine/data/bounds.cpp
- ${CMAKE_SOURCE_DIR}/src/engine/graphics/string_matrix.cpp
)
-add_executable(tests ${SOURCES})
+add_executable(${PROJECT_NAME} ${SOURCES})
-target_compile_features(tests PUBLIC cxx_std_20)
+target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
target_compile_options(
- tests
+ ${PROJECT_NAME}
PRIVATE
-Wall -Wextra -Wpedantic -Wshadow
-Wold-style-cast -Wcast-align -Wno-unused
@@ -26,21 +28,22 @@ target_compile_options(
-pedantic -fsanitize=address -fno-exceptions
)
-if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
- target_compile_options(
- tests
- PRIVATE
- -Wlogical-op -Wnoexcept -Wstrict-null-sentinel
- )
-endif()
-
target_include_directories(
- tests
+ ${PROJECT_NAME}
PRIVATE
"${CMAKE_SOURCE_DIR}/src"
- "${CMAKE_SOURCE_DIR}/test"
)
-target_link_libraries(tests GSL doctest)
+target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address)
+
+target_link_libraries(
+ ${PROJECT_NAME}
+ GSL
+ yacppdic
+ gtest_main
+ gmock
+)
+
+include(GoogleTest)
-target_link_options(tests PRIVATE -fsanitize=address)
+gtest_discover_tests(${PROJECT_NAME})
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 <gtest/gtest.h>
+
+#include <list>
+#include <memory>
+
+class CellHelperTest : public testing::Test
+{
+protected:
+};
+
+TEST_F(CellHelperTest, IsCellDying)
+{
+ const auto mock_matrix = MockMatrix<char>();
+
+ 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<char>(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<char>();
+
+ 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<char>(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<Vector2>(
+ {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()})));
+}
+
diff --git a/test/function.test.cpp b/test/function.test.cpp
deleted file mode 100644
index 9aca0eb..0000000
--- a/test/function.test.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "util/function.hpp"
-
-#include <cstdint>
-#include <doctest/doctest.h>
-
-TEST_CASE("normalize_lamda")
-{
- SUBCASE("Can return a function that returns a int")
- {
- const int number = 58;
-
- CHECK(normalize_lambda(
- [number]()
- {
- return number;
- })() == number);
- }
-
- SUBCASE("Can preserve object state")
- {
- class Book
- {
- public:
- Book() noexcept = default;
-
- void read_page() noexcept
- {
- _pages_read++;
- }
-
- [[nodiscard]] uint32_t pages_read() const noexcept
- {
- return _pages_read;
- }
-
- private:
- uint32_t _pages_read{0U};
- };
-
- auto book = Book();
-
- book.read_page();
- book.read_page();
- book.read_page();
-
- CHECK(normalize_lambda(
- [book]()
- {
- return book.pages_read();
- })() == 3);
- }
-}
diff --git a/test/main.cpp b/test/main.cpp
deleted file mode 100644
index 0a3f254..0000000
--- a/test/main.cpp
+++ /dev/null
@@ -1,2 +0,0 @@
-#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
-#include <doctest/doctest.h>
diff --git a/test/mocks/matrix.hpp b/test/mocks/matrix.hpp
new file mode 100644
index 0000000..1602ebf
--- /dev/null
+++ b/test/mocks/matrix.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include "interfaces/matrix.hpp"
+
+#include <gmock/gmock.h>
+
+template <typename Element>
+class MockMatrix : public IMatrix<Element>
+{
+public:
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(void, fill, (Element element), (noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(Element, get, (const Vector2 &pos), (const, noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(void, set, (const Vector2 &pos, Element element), (noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(uint32_t, get_row_cnt, (), (const, noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(uint32_t, get_column_cnt, (), (const, noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(MatrixIterator<Element>, begin, (), (const, noexcept, override));
+
+ // NOLINTNEXTLINE(modernize-use-trailing-return-type)
+ MOCK_METHOD(MatrixIterator<Element>, end, (), (const, noexcept));
+};
diff --git a/test/string_matrix.test.cpp b/test/string_matrix.test.cpp
deleted file mode 100644
index 522a274..0000000
--- a/test/string_matrix.test.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-#include "engine/graphics/string_matrix.hpp"
-#include "engine/data/bounds.hpp"
-
-#include <doctest/doctest.h>
-#include <type_traits>
-
-constexpr uint32_t MATRIX_WIDTH = 76;
-constexpr uint32_t MATRIX_HEIGHT = 31;
-
-TEST_CASE("String matrix")
-{
- auto string_matrix =
- StringMatrix(Bounds({.width = MATRIX_WIDTH, .height = MATRIX_HEIGHT}));
-
- SUBCASE("Can set & get elements")
- {
- // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
- const auto position = Vector2({.x = 56, .y = 20});
-
- string_matrix.set(position, "#");
-
- CHECK(string_matrix.get(position) == "#");
- }
-
- SUBCASE("Can iterate")
- {
- CHECK(std::is_same_v<decltype(string_matrix.begin()),
- MatrixIterator<std::string_view>>);
-
- CHECK(std::is_same_v<decltype(string_matrix.end()),
- MatrixIterator<std::string_view>>);
-
- uint32_t row_iter_cnt = 0;
-
- for (auto row : string_matrix)
- {
- row_iter_cnt++;
-
- CHECK(std::is_same_v<decltype(row), MatrixRow<std::string_view>>);
-
- uint32_t col_iter_cnt = 0;
-
- for (auto &col : row)
- {
- col_iter_cnt++;
- }
-
- CHECK(col_iter_cnt == MATRIX_WIDTH);
- }
-
- CHECK(row_iter_cnt == MATRIX_HEIGHT);
- }
-}