From 55c93fe609888be73677317978959040cf35b2ff Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 19 Mar 2022 13:21:54 +0100 Subject: refactor: implement matrix iterator --- test/CMakeLists.txt | 45 ++++++++++++++++++++++++++++++++++++++ test/main.cpp | 2 ++ test/string_matrix.test.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 test/CMakeLists.txt create mode 100644 test/main.cpp create mode 100644 test/string_matrix.test.cpp (limited to 'test') diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..ac84f42 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,45 @@ +file(GLOB SOURCES + main.cpp + string_matrix.test.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}) + +target_compile_features(tests PUBLIC cxx_std_20) + +target_compile_options( + tests + PRIVATE + -Wall -Wextra -Wpedantic -Wshadow + -Wold-style-cast -Wcast-align -Wno-unused + -Wconversion -Wcast-qual -Wctor-dtor-privacy + -Wdisabled-optimization -Wformat=2 -Winit-self + -Wmissing-declarations -Wmissing-include-dirs + -Woverloaded-virtual -Wredundant-decls + -Wsign-conversion -Wsign-promo + -Wstrict-overflow=5 -Wswitch-default + -Wundef -Werror + -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 + PRIVATE + "${CMAKE_SOURCE_DIR}/src" + "${CMAKE_SOURCE_DIR}/test" +) + +target_link_libraries(tests GSL doctest) + +target_link_options(tests PRIVATE -fsanitize=address) diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..0a3f254 --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,2 @@ +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include diff --git a/test/string_matrix.test.cpp b/test/string_matrix.test.cpp new file mode 100644 index 0000000..522a274 --- /dev/null +++ b/test/string_matrix.test.cpp @@ -0,0 +1,53 @@ +#include "engine/graphics/string_matrix.hpp" +#include "engine/data/bounds.hpp" + +#include +#include + +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>); + + CHECK(std::is_same_v>); + + uint32_t row_iter_cnt = 0; + + for (auto row : string_matrix) + { + row_iter_cnt++; + + CHECK(std::is_same_v>); + + 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); + } +} -- cgit v1.2.3-18-g5258