Skip to content

test: added tests for scc #25

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

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class UnitTestHelper
Expand Down Expand Up @@ -71,4 +72,39 @@ class UnitTestHelper
}
return result;
}


// This helper method is used to sort the vector of vectors of a particular typename.
// Each inner vector is sorted first.
// Then each of them are sorted by their first element, in increasing order.
template<typename T>
vector<vector<T>> SortVectorOfVectors(vector<vector<T>> data)
{
// Step 1: Sorting each inner vectors.
for (auto& innerVector : data)
{
sort(innerVector.begin(), innerVector.end());
}

// Step 2: Sorting all the vectors by their first element, in increasing order.
sort(data.begin(), data.end(), [](const vector<T>& a, const vector<T>& b)
{
// Checking if both inner vectors are empty to prevent out-of-bounds access.
if (a.empty() && b.empty())
return false;

// Considering empty vector as less than non-empty vector.
if (a.empty())
return true;

// Considering non-empty vector as greater than empty vector.
if (b.empty())
return false;

// Comparing the first elements of each vector.
return (a[0] < b[0]);
});

return data;
}
};
76 changes: 73 additions & 3 deletions Tests/0003_Graph/0004_StronglyConnectedComponentsTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace StronglyConnectedComponentsTest
{
UnitTestHelper unitTestHelper;

// Test case: Testing with a simple graph.
TEST(StronglyConnectedComponentsTesting, SimpleGraphTest)
{
StronglyConnectedComponentsGraph graph;
Expand All @@ -25,8 +26,77 @@ namespace StronglyConnectedComponentsTest
graph.PushDirectedEdge(7, 8);
graph.PushDirectedEdge(8, 6);

string actualResult = unitTestHelper.VerifyVectorResult(graph.FindAllStronglyConnectedComponents());
string expectedResult = "[6 8 7][1 5][2 3][4]";
EXPECT_EQ(actualResult, expectedResult);
auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {6, 8, 7},{1, 5},{2, 3},{4} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}

// Test case: Single Node.
TEST(StronglyConnectedComponentsTesting, SingleNodeTest)
{
StronglyConnectedComponentsGraph graph;
graph.PushSingleNode(1);

auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {1} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}

// Test case: Disconnected Graph.
TEST(StronglyConnectedComponentsTesting, DisconnectedGraphTest)
{
StronglyConnectedComponentsGraph graph;
graph.PushSingleNode(1);
graph.PushSingleNode(2);
graph.PushSingleNode(3);

auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {1},{3},{2} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}

// Test case: Chain of Nodes.
TEST(StronglyConnectedComponentsTesting, ChainOfNodesTest)
{
StronglyConnectedComponentsGraph graph;
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(2, 3);
graph.PushDirectedEdge(3, 4);

auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {2},{1},{3},{4} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}

// Test case: Bidirectional Edge.
TEST(StronglyConnectedComponentsTesting, BidirectionalEdgeTest)
{
StronglyConnectedComponentsGraph graph;
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(2, 1);

auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {2, 1} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}

// Test case: Complex Graph.
TEST(StronglyConnectedComponentsTesting, ComplexGraphTest)
{
StronglyConnectedComponentsGraph graph;

// Graph structure with multiple SCCs and isolated nodes.
graph.PushDirectedEdge(1, 2);
graph.PushDirectedEdge(2, 3);
graph.PushDirectedEdge(3, 1); // Cycle: 1 -> 2 -> 3 -> 1
graph.PushDirectedEdge(4, 5);
graph.PushDirectedEdge(5, 6);
graph.PushDirectedEdge(6, 4); // Cycle: 4 -> 5 -> 6 -> 4
graph.PushDirectedEdge(7, 8); // Single direction: 7 -> 8
graph.PushSingleNode(9); // Isolated node.

auto actualResult = graph.FindAllStronglyConnectedComponents();
vector<vector<int>> expectedResult = { {4, 6, 5},{7}, { 2, 3, 1},{8}, {9} };
EXPECT_EQ(unitTestHelper.SortVectorOfVectors(actualResult), unitTestHelper.SortVectorOfVectors(expectedResult));
}
}
Loading