99
1010/**
1111 * JUnit 5 tests for {@link TopologicalSortDFS}.
12- *
13- * Test cases cover:
14- * - Standard DAG with a clear expected topological order
15- * - Multiple valid orderings (validated by constraint checking)
16- * - Graphs with cycles (must throw IllegalStateException)
17- * - Single-node graph
18- * - Disconnected graph
1912 */
2013class TopologicalSortDFSTest {
2114
22- /**
23- * Checks that every edge u → v satisfies: u appears before v in the result.
24- */
2515 private static void assertValidTopologicalOrder (List <Integer > order , int [][] edges ) {
2616 for (int [] edge : edges ) {
2717 int u = edge [0 ];
2818 int v = edge [1 ];
29- assertTrue (
30- order .indexOf (u ) < order .indexOf (v ),
31- "Expected " + u + " to appear before " + v + " in topological order. Got: " + order );
19+ assertTrue (order .indexOf (u ) < order .indexOf (v ), "Expected " + u + " to appear before " + v + " in topological order. Got: " + order );
3220 }
3321 }
3422
3523 @ Test
3624 void testSimpleLinearGraph () {
37- // 0 → 1 → 2 → 3
3825 TopologicalSortDFS graph = new TopologicalSortDFS ();
3926 graph .addEdge (0 , 1 );
4027 graph .addEdge (1 , 2 );
@@ -46,15 +33,9 @@ void testSimpleLinearGraph() {
4633
4734 @ Test
4835 void testDAGWithMultiplePaths () {
49- // Graph:
50- // 5 → 2
51- // 5 → 0
52- // 4 → 0
53- // 4 → 1
54- // 2 → 3
55- // 3 → 1
36+ // 5 → 2, 5 → 0, 4 → 0, 4 → 1, 2 → 3, 3 → 1
5637 TopologicalSortDFS graph = new TopologicalSortDFS ();
57- int [][] edges = { { 5 , 2 }, { 5 , 0 }, { 4 , 0 }, { 4 , 1 }, { 2 , 3 }, { 3 , 1 } };
38+ int [][] edges = {{ 5 , 2 }, {5 , 0 }, {4 , 0 }, {4 , 1 }, {2 , 3 }, {3 , 1 } };
5839 for (int [] edge : edges ) {
5940 graph .addEdge (edge [0 ], edge [1 ]);
6041 }
@@ -66,12 +47,9 @@ void testDAGWithMultiplePaths() {
6647
6748 @ Test
6849 void testBuildOrderDAG () {
69- // Simulates build dependency: A depends on nothing, B depends on A, C depends
70- // on A and B
71- // A=0, B=1, C=2, D=3
7250 // 0 → 1, 0 → 2, 1 → 3, 2 → 3
7351 TopologicalSortDFS graph = new TopologicalSortDFS ();
74- int [][] edges = { { 0 , 1 }, { 0 , 2 }, { 1 , 3 }, { 2 , 3 } };
52+ int [][] edges = {{ 0 , 1 }, {0 , 2 }, {1 , 3 }, {2 , 3 } };
7553 for (int [] edge : edges ) {
7654 graph .addEdge (edge [0 ], edge [1 ]);
7755 }
@@ -92,9 +70,8 @@ void testSingleVertex() {
9270
9371 @ Test
9472 void testDisconnectedGraph () {
95- // Two disconnected components: 0 → 1 and 2 → 3
9673 TopologicalSortDFS graph = new TopologicalSortDFS ();
97- int [][] edges = { { 0 , 1 }, { 2 , 3 } };
74+ int [][] edges = {{ 0 , 1 }, {2 , 3 } };
9875 for (int [] edge : edges ) {
9976 graph .addEdge (edge [0 ], edge [1 ]);
10077 }
@@ -106,43 +83,31 @@ void testDisconnectedGraph() {
10683
10784 @ Test
10885 void testSimpleCycleThrowsException () {
109- // 0 → 1 → 2 → 0 (cycle)
11086 TopologicalSortDFS graph = new TopologicalSortDFS ();
11187 graph .addEdge (0 , 1 );
11288 graph .addEdge (1 , 2 );
11389 graph .addEdge (2 , 0 );
11490
115- assertThrows (
116- IllegalStateException .class ,
117- graph ::topologicalSort ,
118- "Expected IllegalStateException for cyclic graph" );
91+ assertThrows (IllegalStateException .class , graph ::topologicalSort , "Expected IllegalStateException for cyclic graph" );
11992 }
12093
12194 @ Test
12295 void testSelfLoopThrowsException () {
123- // A self-loop is a trivial cycle: 0 → 0
12496 TopologicalSortDFS graph = new TopologicalSortDFS ();
12597 graph .addEdge (0 , 0 );
12698
127- assertThrows (
128- IllegalStateException .class ,
129- graph ::topologicalSort ,
130- "Expected IllegalStateException for self-loop" );
99+ assertThrows (IllegalStateException .class , graph ::topologicalSort , "Expected IllegalStateException for self-loop" );
131100 }
132101
133102 @ Test
134103 void testLargerCycleThrowsException () {
135- // 0 → 1 → 2 → 3 → 4 → 2 (cycle at 2 → 3 → 4 → 2)
136104 TopologicalSortDFS graph = new TopologicalSortDFS ();
137105 graph .addEdge (0 , 1 );
138106 graph .addEdge (1 , 2 );
139107 graph .addEdge (2 , 3 );
140108 graph .addEdge (3 , 4 );
141109 graph .addEdge (4 , 2 );
142110
143- assertThrows (
144- IllegalStateException .class ,
145- graph ::topologicalSort ,
146- "Expected IllegalStateException for graph with cycle" );
111+ assertThrows (IllegalStateException .class , graph ::topologicalSort , "Expected IllegalStateException for graph with cycle" );
147112 }
148113}
0 commit comments