114 lines
3.3 KiB
C++
114 lines
3.3 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include "Cell.h"
|
|
#include "Distances.h"
|
|
|
|
TEST_CASE("Cell class tests", "[cell]") {
|
|
Cell cell(2, 3);
|
|
|
|
SECTION("Row and Column are set correctly") {
|
|
REQUIRE(cell.getRow() == 2);
|
|
REQUIRE(cell.getCol() == 3);
|
|
}
|
|
|
|
SECTION("Row and Column are not equal") {
|
|
REQUIRE(cell.getRow() != cell.getCol());
|
|
}
|
|
|
|
SECTION("Linking and unlinking cells", "[cell]") {
|
|
Cell link1(4, 4);
|
|
Cell link2(5, 5);
|
|
|
|
REQUIRE_FALSE(cell.isLinked(link1));
|
|
REQUIRE_FALSE(cell.isLinked(link2));
|
|
|
|
cell.link(link1);
|
|
REQUIRE(cell.isLinked(link1));
|
|
REQUIRE(link1.isLinked(cell));
|
|
REQUIRE_FALSE(cell.isLinked(link2));
|
|
REQUIRE_FALSE(link2.isLinked(cell));
|
|
|
|
cell.link(link2);
|
|
REQUIRE(cell.isLinked(link1));
|
|
REQUIRE(link1.isLinked(cell));
|
|
REQUIRE(cell.isLinked(link2));
|
|
REQUIRE(link2.isLinked(cell));
|
|
|
|
cell.unlink(link1);
|
|
REQUIRE_FALSE(cell.isLinked(link1));
|
|
REQUIRE_FALSE(link1.isLinked(cell));
|
|
REQUIRE(cell.isLinked(link2));
|
|
REQUIRE(link2.isLinked(cell));
|
|
|
|
SECTION("Check getLinks()") {
|
|
auto links = cell.getLinks();
|
|
REQUIRE(links.size() == 1);
|
|
REQUIRE(links[0].get() == link2);
|
|
}
|
|
}
|
|
|
|
SECTION("Equality operator") {
|
|
Cell cell_same(2, 3);
|
|
Cell cell_diff_row(1, 3);
|
|
Cell cell_diff_col(2, 4);
|
|
Cell cell_diff_both(1, 4);
|
|
|
|
REQUIRE(cell == cell_same);
|
|
REQUIRE_FALSE(cell == cell_diff_row);
|
|
REQUIRE_FALSE(cell == cell_diff_col);
|
|
REQUIRE_FALSE(cell == cell_diff_both);
|
|
}
|
|
|
|
SECTION("Check distances get created correctly for single cell", "[cell]") {
|
|
auto distances = cell.distances();
|
|
REQUIRE(distances.getDistance(cell) == 0);
|
|
}
|
|
|
|
SECTION("Check distances get created correctly for multiple cells", "[cell]") {
|
|
Cell cell1(0, 1);
|
|
Cell cell2(1, 0);
|
|
Cell cell3(1, 1);
|
|
cell.link(cell1);
|
|
cell.link(cell2);
|
|
cell1.link(cell3);
|
|
auto distances = cell.distances();
|
|
REQUIRE(distances.getDistance(cell) == 0);
|
|
REQUIRE(distances.getDistance(cell1) == 1);
|
|
REQUIRE(distances.getDistance(cell2) == 1);
|
|
REQUIRE(distances.getDistance(cell3) == 2);
|
|
}
|
|
|
|
SECTION("Check neighbors", "[cell]") {
|
|
Cell cell1(0, 1);
|
|
Cell cell2(1, 0);
|
|
Cell cell3(1, 1);
|
|
cell.north = &cell1;
|
|
cell.south = &cell2;
|
|
cell.east = &cell3;
|
|
|
|
SECTION("Check neighbors for cell with no neighbors") {
|
|
Cell cell4(2, 2);
|
|
auto neighbors = cell4.getNeighbors();
|
|
REQUIRE(neighbors.size() == 0);
|
|
}
|
|
|
|
SECTION("Check hasNorth, hasSouth, hasEast, hasWest") {
|
|
REQUIRE(cell.hasNorth());
|
|
REQUIRE(cell.hasSouth());
|
|
REQUIRE(cell.hasEast());
|
|
REQUIRE_FALSE(cell.hasWest());
|
|
}
|
|
|
|
SECTION("Check getting list of neighbors", "[cell]") {
|
|
auto neighbors = cell.getNeighbors();
|
|
REQUIRE(neighbors.size() == 3);
|
|
REQUIRE(std::find_if(neighbors.begin(), neighbors.end(), [&cell1](const auto &cell) {
|
|
return cell.get() == cell1;
|
|
}) != neighbors.end());
|
|
REQUIRE(std::find_if(neighbors.begin(), neighbors.end(), [&cell2](const auto &cell) {
|
|
return cell.get() == cell2;
|
|
}) != neighbors.end());
|
|
}
|
|
}
|
|
|
|
}
|