maze-cpp/tests/Grid.cpp
2023-05-03 23:52:33 -04:00

93 lines
2.5 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "Grid.h"
TEST_CASE("Grid class tests", "[grid]") {
Grid grid(3, 4);
SECTION("Check basic grid construction") {
REQUIRE(grid.getRows() == 3);
REQUIRE(grid.getCols() == 4);
REQUIRE(grid(0, 0) != nullptr);
}
SECTION("Check grid edges are set correctly") {
auto cell1 = grid(0, 0);
REQUIRE(cell1 != nullptr);
REQUIRE(cell1->north == nullptr);
REQUIRE(cell1->west == nullptr);
REQUIRE(cell1->east != nullptr);
REQUIRE(cell1->south != nullptr);
auto cell2 = grid(0, 2);
REQUIRE(cell2 != nullptr);
REQUIRE(cell2->south == nullptr);
REQUIRE(cell2->east != nullptr);
REQUIRE(cell2->north != nullptr);
REQUIRE(cell2->west == nullptr);
}
SECTION("Check that grid cells directions are set correctly") {
REQUIRE(grid(1, 1)->south == grid(1, 2));
REQUIRE(grid(1, 1)->north == grid(1, 0));
REQUIRE(grid(1, 1)->east == grid(2, 1));
REQUIRE(grid(1, 1)->west == grid(0, 1));
}
SECTION("Check that grid size is correct") {
REQUIRE(grid.size() == 12);
}
SECTION("Check that cell iteration works") {
int count = 0;
for ([[maybe_unused]] auto& c : grid) {
count++;
}
REQUIRE(count == grid.size());
}
SECTION("Check that const cell iteration works") {
const Grid& cgrid = grid;
int count = 0;
for ([[maybe_unused]] auto& c : cgrid) {
count++;
}
REQUIRE(count == grid.size());
}
SECTION("Check row iterator hits every cell") {
int row_count = 0;
int count = 0;
for (auto row : grid.byRow()) {
row_count++;
for (auto& cell : row) {
count++;
}
}
REQUIRE(row_count == grid.getRows());
REQUIRE(count == grid.size());
}
}
TEST_CASE("Count dead ends", "[grid]") {
Grid grid(1, 3);
REQUIRE(grid.deadEnds().size() == 0);
grid(1, 0)->link(grid.getCellRef(0, 0));
grid(1, 0)->link(grid.getCellRef(2, 0));
REQUIRE(grid.deadEnds().size() == 2);
}
TEST_CASE("MaskedGrid", "[grid]") {
Mask mask(2, 2);
SECTION("MaskedGrid size with all entries enabled") {
MaskedGrid grid(mask);
REQUIRE(mask.size() == 4);
REQUIRE(grid.size() == 4);
}
SECTION("MaskedGrid size with a disabled cell") {
mask.disable(0, 0);
MaskedGrid grid(mask);
REQUIRE(mask.size() == 3);
REQUIRE(grid.size() == 3);
}
}