1+ #include < gtest/gtest.h>
2+ #include " ../Headers/0003_Graph/0003_TopologicalSort.h"
3+ #include " ../0000_CommonUtilities/UnitTestHelper.h"
4+
5+ namespace TopologicalSortTest
6+ {
7+ UnitTestHelper unitTestHelper;
8+
9+ TEST (TopoSortTesting, ShowTopoSortResult)
10+ {
11+ TopologicalSortGraph graph;
12+
13+ graph.PushDirectedEdge (1 , 2 );
14+ graph.PushDirectedEdge (1 , 4 );
15+ graph.PushDirectedEdge (2 , 3 );
16+ graph.PushDirectedEdge (4 , 3 );
17+ graph.PushSingleNode (5 );
18+ graph.PushDirectedEdge (6 , 7 );
19+ graph.PushDirectedEdge (6 , 8 );
20+ graph.PushDirectedEdge (7 , 4 );
21+ graph.PushDirectedEdge (7 , 8 );
22+ graph.PushDirectedEdge (9 , 8 );
23+
24+ graph.TopologicalSort ();
25+
26+ string actualResult = unitTestHelper.VerifyVectorResult (graph.ShowTopologicalSortResult ());
27+ string expectedResult = " 9(17,18) 6(11,16) 7(12,15) 8(13,14) 5(9,10) 1(1,8) 4(6,7) 2(2,5) 3(3,4)" ;
28+
29+ EXPECT_EQ (actualResult, expectedResult);
30+ }
31+
32+ // Test with a larger graph and multiple paths between nodes
33+ TEST (TopoSortTesting, LargeGraphMultiplePaths)
34+ {
35+ TopologicalSortGraph graph;
36+ graph.PushDirectedEdge (1 , 2 );
37+ graph.PushDirectedEdge (1 , 3 );
38+ graph.PushDirectedEdge (2 , 4 );
39+ graph.PushDirectedEdge (3 , 4 );
40+ graph.PushDirectedEdge (4 , 5 );
41+ graph.PushDirectedEdge (5 , 6 );
42+ graph.PushDirectedEdge (6 , 7 );
43+
44+ graph.TopologicalSort ();
45+ string actualResult = unitTestHelper.VerifyVectorResult (graph.ShowTopologicalSortResult ());
46+ string expectedResult = " 1(1,14) 3(12,13) 2(2,11) 4(3,10) 5(4,9) 6(5,8) 7(6,7)" ;
47+
48+ EXPECT_EQ (actualResult, expectedResult);
49+ }
50+
51+ // Test with a graph containing disconnected components
52+ TEST (TopoSortTesting, DisconnectedGraph)
53+ {
54+ TopologicalSortGraph graph;
55+ graph.PushDirectedEdge (1 , 2 );
56+ graph.PushDirectedEdge (3 , 4 );
57+ graph.PushDirectedEdge (5 , 6 );
58+
59+ graph.TopologicalSort ();
60+ string actualResult = unitTestHelper.VerifyVectorResult (graph.ShowTopologicalSortResult ());
61+ string expectedResult = " 5(9,12) 6(10,11) 3(5,8) 4(6,7) 1(1,4) 2(2,3)" ;
62+
63+ EXPECT_EQ (actualResult, expectedResult);
64+ }
65+
66+ // Test with a graph that has multiple nodes pointing to the same node
67+ TEST (TopoSortTesting, MultipleIncomingEdges)
68+ {
69+ TopologicalSortGraph graph;
70+ graph.PushDirectedEdge (1 , 3 );
71+ graph.PushDirectedEdge (2 , 3 );
72+ graph.PushDirectedEdge (3 , 4 );
73+
74+ graph.TopologicalSort ();
75+ string actualResult = unitTestHelper.VerifyVectorResult (graph.ShowTopologicalSortResult ());
76+ string expectedResult = " 2(7,8) 1(1,6) 3(2,5) 4(3,4)" ;
77+
78+ EXPECT_EQ (actualResult, expectedResult);
79+ }
80+
81+ // Test for a single-node graph to check the base case
82+ TEST (TopoSortTesting, SingleNodeGraph)
83+ {
84+ TopologicalSortGraph graph;
85+ graph.PushSingleNode (1 );
86+
87+ graph.TopologicalSort ();
88+ string actualResult = unitTestHelper.VerifyVectorResult (graph.ShowTopologicalSortResult ());
89+ string expectedResult = " 1(1,2)" ;
90+
91+ EXPECT_EQ (actualResult, expectedResult);
92+ }
93+
94+ // Test with a cyclic graph to verify it can detect cycles (assuming cycle detection is implemented)
95+ TEST (TopoSortTesting, CyclicGraph)
96+ {
97+ TopologicalSortGraph graph;
98+ graph.PushDirectedEdge (1 , 2 );
99+ graph.PushDirectedEdge (2 , 3 );
100+ graph.PushDirectedEdge (3 , 1 ); // Cycle: 1 -> 2 -> 3 -> 1
101+
102+ graph.TopologicalSort ();
103+
104+ // Expected output if cycle detection is implemented
105+ EXPECT_THROW (graph.ShowTopologicalSortResult (), runtime_error);
106+ }
107+ }
0 commit comments