Update tarjan test cases and add BFS test case

This commit is contained in:
stefiosif
2022-06-12 17:34:52 +03:00
parent 72741a6a5b
commit 2029900035
2 changed files with 70 additions and 20 deletions

View File

@@ -7,9 +7,9 @@
using namespace graph;
TEST_SUITE("Algorithm.") {
TEST_SUITE("Algorithm") {
TEST_CASE("Tarjan T_1") {
TEST_CASE("Tarjan T1") {
// 1 --> 2 --> 4 --> 1
// 2 --> 3 --> 5 --> 7 --> 3
// 5 --> 9 --> 6 --> 8 --> 9
@@ -29,20 +29,22 @@ TEST_SUITE("Algorithm.") {
REQUIRE_EQ(G.vertices.size(), 9);
algo::Tarjan<std::uint16_t> tarjan(G);
auto result = tarjan.run();
auto res = tarjan.run();
for (auto& scc : result) {
scc.output();
}
std::vector<std::vector<std::uint16_t>> expected{
{1, 2, 4},
std::vector<std::vector<std::uint16_t>> exp = {
{6, 8, 9},
{3, 5, 7},
{6, 8, 9}
{1, 2, 4}
};
for (auto i = 0; i < res.size(); i++) {
auto kv = std::views::keys(res[i].adjMatrix);
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
true);
}
}
TEST_CASE("Tarjan T_2") {
TEST_CASE("Tarjan T2") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1
// 4 --> 6 --> 3
@@ -60,15 +62,21 @@ TEST_SUITE("Algorithm.") {
REQUIRE_EQ(G.vertices.size(), 7);
algo::Tarjan<std::uint16_t> tarjan(G);
auto result = tarjan.run();
auto res = tarjan.run();
std::vector<std::vector<std::uint16_t>> expected{
{1, 3, 4, 6},
{2, 5, 7}
std::vector<std::vector<std::uint16_t>> exp = {
{2, 5, 7},
{1, 3, 4, 6}
};
for (auto i = 0; i < res.size(); i++) {
auto kv = std::views::keys(res[i].adjMatrix);
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
true);
}
}
TEST_CASE("BFS T_1") {
TEST_CASE("BFS T1") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1
// 4 --> 6 --> 3
@@ -84,6 +92,25 @@ TEST_SUITE("Algorithm.") {
G.insert(7, 2);
algo::BFS<std::uint16_t> bfs(G, 1);
auto bfsTree = bfs.run();
auto tree = bfs.run();
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
{1, {2, 4}},
{2, {5}},
{3, {}},
{4, {3, 6}},
{5, {7}},
{6, {}},
{7, {}}
};*/
std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
{1, {2, 4}},
{2, {5}},
{4, {3, 6}},
{5, {7}}
};
CHECK_EQ(tree, exp);
}
}

View File

@@ -8,9 +8,9 @@
using namespace graph;
TEST_SUITE("Graph.") {
TEST_SUITE("Graph") {
TEST_CASE("SCC") {
TEST_CASE("SCC T1") {
// 1 --> 2 --> 4 --> 1
// 2 --> 3 --> 5 --> 7 --> 3
// 5 --> 9 --> 6 --> 8 --> 9
@@ -27,7 +27,30 @@ TEST_SUITE("Graph.") {
G.insert(8, 9);
G.insert(9, 6);
REQUIRE_EQ(G.vertices.size(), 9);
algo::Tarjan<std::uint16_t> tarjan(G);
auto SCCs = tarjan.run();
auto sccs = tarjan.run();
}
TEST_CASE("SCC T2") {
// 1 --> 2 --> 5 --> 7 --> 2
// 1 --> 4 --> 3 --> 1
// 4 --> 6 --> 3
Digraph<std::uint16_t> G;
G.insert(1, 2);
G.insert(1, 4);
G.insert(2, 5);
G.insert(3, 1);
G.insert(4, 3);
G.insert(4, 6);
G.insert(5, 7);
G.insert(6, 3);
G.insert(7, 2);
REQUIRE_EQ(G.vertices.size(), 7);
algo::Tarjan<std::uint16_t> tarjan(G);
auto sccs = tarjan.run();
}
}