Add new tests, remove duplicate tests (scc creation ~ tarjan's algorithm)
This commit is contained in:
@@ -7,7 +7,7 @@ using namespace graph;
|
|||||||
|
|
||||||
TEST_SUITE("Algorithm") {
|
TEST_SUITE("Algorithm") {
|
||||||
|
|
||||||
TEST_CASE("Tarjan T1") {
|
TEST_CASE("Tarjan::execute 1") {
|
||||||
// 1 --> 2 --> 4 --> 1
|
// 1 --> 2 --> 4 --> 1
|
||||||
// 2 --> 3 --> 5 --> 7 --> 3
|
// 2 --> 3 --> 5 --> 7 --> 3
|
||||||
// 5 --> 9 --> 6 --> 8 --> 9
|
// 5 --> 9 --> 6 --> 8 --> 9
|
||||||
@@ -24,25 +24,24 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(8, 9);
|
G.insert(8, 9);
|
||||||
G.insert(9, 6);
|
G.insert(9, 6);
|
||||||
|
|
||||||
REQUIRE_EQ(G.vertices.size(), 9);
|
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
algo::Tarjan<std::uint16_t> tarjan(G);
|
||||||
auto res = tarjan.execute();
|
auto r = tarjan.execute();
|
||||||
|
|
||||||
std::vector<std::vector<std::uint16_t>> exp = {
|
std::vector<std::vector<std::uint16_t>> x = {
|
||||||
{6, 8, 9},
|
{6, 8, 9},
|
||||||
{3, 5, 7},
|
{3, 5, 7},
|
||||||
{1, 2, 4}
|
{1, 2, 4}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto i = 0; i < res.size(); i++) {
|
for (auto i = 0; i < r.size(); i++) {
|
||||||
auto kv = std::views::keys(res[i].adjMatrix);
|
auto kv = std::views::keys(r[i].adjMatrix);
|
||||||
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
||||||
true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Tarjan T2") {
|
TEST_CASE("Tarjan::execute 2") {
|
||||||
// 1 --> 2 --> 5 --> 7 --> 2
|
// 1 --> 2 --> 5 --> 7 --> 2
|
||||||
// 1 --> 4 --> 3 --> 1
|
// 1 --> 4 --> 3 --> 1
|
||||||
// 4 --> 6 --> 3
|
// 4 --> 6 --> 3
|
||||||
@@ -57,24 +56,66 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(6, 3);
|
G.insert(6, 3);
|
||||||
G.insert(7, 2);
|
G.insert(7, 2);
|
||||||
|
|
||||||
REQUIRE_EQ(G.vertices.size(), 7);
|
REQUIRE_EQ(G.adjMatrix.size(), 7);
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
algo::Tarjan<std::uint16_t> tarjan(G);
|
||||||
auto res = tarjan.execute();
|
auto r = tarjan.execute();
|
||||||
|
|
||||||
std::vector<std::vector<std::uint16_t>> exp = {
|
std::vector<std::vector<std::uint16_t>> x = {
|
||||||
{2, 5, 7},
|
{2, 5, 7},
|
||||||
{1, 3, 4, 6}
|
{1, 3, 4, 6}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto i = 0; i < res.size(); i++) {
|
for (auto i = 0; i < r.size(); i++) {
|
||||||
auto kv = std::views::keys(res[i].adjMatrix);
|
auto kv = std::views::keys(r[i].adjMatrix);
|
||||||
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), exp[i].begin()),
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
||||||
true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("BFS T1") {
|
TEST_CASE("Tarjan::execute 3") {
|
||||||
|
// 1 --> 2 --> 3 --> 1
|
||||||
|
// 3 --> 4 --> 5 --> 3
|
||||||
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
|
// 12 --> 10
|
||||||
|
Digraph<std::uint16_t> G;
|
||||||
|
G.insert(1, 2);
|
||||||
|
G.insert(2, 3);
|
||||||
|
G.insert(3, 1);
|
||||||
|
G.insert(3, 4);
|
||||||
|
G.insert(4, 5);
|
||||||
|
G.insert(5, 3);
|
||||||
|
G.insert(2, 6);
|
||||||
|
G.insert(6, 7);
|
||||||
|
G.insert(7, 8);
|
||||||
|
G.insert(7, 9);
|
||||||
|
G.insert(8, 6);
|
||||||
|
G.insert(6, 9);
|
||||||
|
G.insert(9, 10);
|
||||||
|
G.insert(10, 11);
|
||||||
|
G.insert(11, 12);
|
||||||
|
G.insert(12, 13);
|
||||||
|
G.insert(13, 9);
|
||||||
|
G.insert(12, 10);
|
||||||
|
|
||||||
|
REQUIRE_EQ(G.adjMatrix.size(), 13);
|
||||||
|
|
||||||
|
algo::Tarjan<std::uint16_t> tarjan(G);
|
||||||
|
auto r = tarjan.execute();
|
||||||
|
|
||||||
|
std::vector<std::vector<std::uint16_t>> x = {
|
||||||
|
{9, 10, 11, 12, 13},
|
||||||
|
{6, 7, 8},
|
||||||
|
{1, 2, 3, 4, 5}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto i = 0; i < r.size(); i++) {
|
||||||
|
auto kv = std::views::keys(r[i].adjMatrix);
|
||||||
|
CHECK_EQ(std::is_permutation(kv.begin(), kv.end(), x[i].begin()), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BreadthFirstSearch::execute 1") {
|
||||||
// 1 --> 2 --> 5 --> 7 --> 2
|
// 1 --> 2 --> 5 --> 7 --> 2
|
||||||
// 1 --> 4 --> 3 --> 1
|
// 1 --> 4 --> 3 --> 1
|
||||||
// 4 --> 6 --> 3
|
// 4 --> 6 --> 3
|
||||||
@@ -90,36 +131,24 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(7, 2);
|
G.insert(7, 2);
|
||||||
|
|
||||||
algo::BreadthFirstSearch<std::uint16_t> bfs(G);
|
algo::BreadthFirstSearch<std::uint16_t> bfs(G);
|
||||||
auto tree = bfs.execute(1);
|
auto r = bfs.execute(1);
|
||||||
|
|
||||||
/*std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
std::map<std::uint16_t, std::set<std::uint16_t>> x = {
|
||||||
{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}},
|
{1, {2, 4}},
|
||||||
{2, {5}},
|
{2, {5}},
|
||||||
{4, {3, 6}},
|
{4, {3, 6}},
|
||||||
{5, {7}}
|
{5, {7}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(tree, exp);
|
CHECK_EQ(r, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("BreadthFirstSearch::execute 2") {
|
||||||
|
|
||||||
TEST_CASE("BFS") {
|
|
||||||
// 1 --> 2 --> 3 --> 1
|
// 1 --> 2 --> 3 --> 1
|
||||||
// 3 --> 4 --> 5 --> 3
|
// 3 --> 4 --> 5 --> 3
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
// 2 --> 6 --> 7 --> 8 --> 6
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
||||||
// 12 --> 10
|
// 12 --> 10
|
||||||
Digraph<std::uint16_t> G;
|
Digraph<std::uint16_t> G;
|
||||||
G.insert(1, 2);
|
G.insert(1, 2);
|
||||||
G.insert(2, 3);
|
G.insert(2, 3);
|
||||||
@@ -130,6 +159,7 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(2, 6);
|
G.insert(2, 6);
|
||||||
G.insert(6, 7);
|
G.insert(6, 7);
|
||||||
G.insert(7, 8);
|
G.insert(7, 8);
|
||||||
|
G.insert(7, 9);
|
||||||
G.insert(8, 6);
|
G.insert(8, 6);
|
||||||
G.insert(6, 9);
|
G.insert(6, 9);
|
||||||
G.insert(9, 10);
|
G.insert(9, 10);
|
||||||
@@ -138,5 +168,23 @@ TEST_SUITE("Algorithm") {
|
|||||||
G.insert(12, 13);
|
G.insert(12, 13);
|
||||||
G.insert(13, 9);
|
G.insert(13, 9);
|
||||||
G.insert(12, 10);
|
G.insert(12, 10);
|
||||||
|
|
||||||
|
algo::BreadthFirstSearch<std::uint16_t> bfs(G);
|
||||||
|
auto r = bfs.execute(1);
|
||||||
|
|
||||||
|
std::map<std::uint16_t, std::set<std::uint16_t>> x = {
|
||||||
|
{1, {2}},
|
||||||
|
{2, {3, 6}},
|
||||||
|
{3, {4}},
|
||||||
|
{4, {5}},
|
||||||
|
{6, {7, 9}},
|
||||||
|
{7, {8}},
|
||||||
|
{9, {10}},
|
||||||
|
{10, {11}},
|
||||||
|
{11, {12}},
|
||||||
|
{12, {13}}
|
||||||
|
};
|
||||||
|
|
||||||
|
CHECK_EQ(r, x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ using namespace graph;
|
|||||||
|
|
||||||
TEST_SUITE("Graph") {
|
TEST_SUITE("Graph") {
|
||||||
|
|
||||||
TEST_CASE("Reverse") {
|
TEST_CASE("Digraph::reverse") {
|
||||||
// 1 --> 2 --> 4 --> 1
|
// 1 --> 2 --> 4 --> 1
|
||||||
// 2 --> 3 --> 5 --> 7 --> 3
|
// 2 --> 3 --> 5 --> 7 --> 3
|
||||||
// 5 --> 9 --> 6 --> 8 --> 9
|
// 5 --> 9 --> 6 --> 8 --> 9
|
||||||
@@ -26,11 +26,11 @@ TEST_SUITE("Graph") {
|
|||||||
G.insert(8, 9);
|
G.insert(8, 9);
|
||||||
G.insert(9, 6);
|
G.insert(9, 6);
|
||||||
|
|
||||||
REQUIRE_EQ(G.vertices.size(), 9);
|
REQUIRE_EQ(G.adjMatrix.size(), 9);
|
||||||
|
|
||||||
auto RG = G.reverse();
|
auto R = G.reverse();
|
||||||
|
|
||||||
std::map<std::uint16_t, std::set<std::uint16_t>> exp = {
|
std::map<std::uint16_t, std::set<std::uint16_t>> X = {
|
||||||
{1, {4}},
|
{1, {4}},
|
||||||
{2, {1}},
|
{2, {1}},
|
||||||
{3, {2, 7}},
|
{3, {2, 7}},
|
||||||
@@ -42,81 +42,7 @@ TEST_SUITE("Graph") {
|
|||||||
{9, {5, 8}}
|
{9, {5, 8}}
|
||||||
};
|
};
|
||||||
|
|
||||||
CHECK_EQ(RG.adjMatrix, exp);
|
CHECK_EQ(R, X);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("SCC T1") {
|
|
||||||
// 1 --> 2 --> 4 --> 1
|
|
||||||
// 2 --> 3 --> 5 --> 7 --> 3
|
|
||||||
// 5 --> 9 --> 6 --> 8 --> 9
|
|
||||||
Digraph<std::uint16_t> G;
|
|
||||||
G.insert(1, 2);
|
|
||||||
G.insert(2, 3);
|
|
||||||
G.insert(2, 4);
|
|
||||||
G.insert(3, 5);
|
|
||||||
G.insert(4, 1);
|
|
||||||
G.insert(5, 7);
|
|
||||||
G.insert(5, 9);
|
|
||||||
G.insert(6, 8);
|
|
||||||
G.insert(7, 3);
|
|
||||||
G.insert(8, 9);
|
|
||||||
G.insert(9, 6);
|
|
||||||
|
|
||||||
REQUIRE_EQ(G.vertices.size(), 9);
|
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
||||||
auto sccs = tarjan.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
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.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("SCC T3") {
|
|
||||||
// 1 --> 2 --> 3 --> 1
|
|
||||||
// 3 --> 4 --> 5 --> 3
|
|
||||||
// 2 --> 6 --> 7 --> 8 --> 6
|
|
||||||
// 6 --> 9 --> 10 --> 11 --> 12 --> 13 --> 9
|
|
||||||
// 12 --> 10
|
|
||||||
Digraph<std::uint16_t> G;
|
|
||||||
G.insert(1, 2);
|
|
||||||
G.insert(2, 3);
|
|
||||||
G.insert(3, 1);
|
|
||||||
G.insert(3, 4);
|
|
||||||
G.insert(4, 5);
|
|
||||||
G.insert(5, 3);
|
|
||||||
G.insert(2, 6);
|
|
||||||
G.insert(6, 7);
|
|
||||||
G.insert(7, 8);
|
|
||||||
G.insert(8, 6);
|
|
||||||
G.insert(6, 9);
|
|
||||||
G.insert(9, 10);
|
|
||||||
G.insert(10, 11);
|
|
||||||
G.insert(11, 12);
|
|
||||||
G.insert(12, 13);
|
|
||||||
G.insert(13, 9);
|
|
||||||
G.insert(12, 10);
|
|
||||||
|
|
||||||
REQUIRE_EQ(G.vertices.size(), 13);
|
|
||||||
|
|
||||||
algo::Tarjan<std::uint16_t> tarjan(G);
|
|
||||||
auto sccs = tarjan.execute();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user