Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for n-queens #273

Merged
merged 4 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions C++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ add_library(test_runner STATIC
# Algorithms
# ============================================================================

# -------------------
# Backtracking
# -------------------

# N-Queens
add_executable(n_queens
test/algorithm/backtracking/n_queens.cpp)
target_link_libraries(n_queens test_runner)

# -------------------
# Dynamic programming
# -------------------
Expand Down
49 changes: 49 additions & 0 deletions C++/test/algorithm/backtracking/n_queens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "third_party/catch.hpp"
#include "algorithm/backtracking/n_queens.hpp"

alxmjo marked this conversation as resolved.
Show resolved Hide resolved
TEST_CASE("Unplaceable cases", "[backtracking][n_queens]") {
NQueensSolver b0(0);
REQUIRE_FALSE(b0.can_place_queens());
NQueensSolver b2(2);
REQUIRE_FALSE(b2.can_place_queens());
NQueensSolver b3(3);
REQUIRE_FALSE(b3.can_place_queens());
}

TEST_CASE("Placeable cases", "[backtracking][n_queens]") {
NQueensSolver b1(1);
alxmjo marked this conversation as resolved.
Show resolved Hide resolved
REQUIRE(b1.can_place_queens());
alxmjo marked this conversation as resolved.
Show resolved Hide resolved
REQUIRE(b1.num_solutions() == 1);
Board b1_sln {{true}};
REQUIRE(b1.get_solution() == b1_sln);
std::vector<Board> b1_slns {{{true}}};
REQUIRE(b1.get_solutions() == b1_slns);

NQueensSolver b4(4);
REQUIRE(b4.can_place_queens());
REQUIRE(b4.num_solutions() == 2);
Board b4_sln1 {
{false, true, false, false},
{false, false, false, true},
{true, false, false, false},
{false, false, true, false}
};
Board b4_sln2 {
{false, false, true, false},
{true, false, false, false},
{false, false, false, true},
{false, true, false, false}
};
using Catch::Matchers::VectorContains;
REQUIRE_THAT(b4.get_solutions(), VectorContains(b4_sln1));
REQUIRE_THAT(b4.get_solutions(), VectorContains(b4_sln2));

NQueensSolver b9(9);
REQUIRE(b9.can_place_queens());
REQUIRE(b9.num_solutions() == 352);

NQueensSolver b11(11);
REQUIRE(b11.can_place_queens());
REQUIRE(b11.num_solutions() == 2680);

alxmjo marked this conversation as resolved.
Show resolved Hide resolved
}