11package com .thealgorithms .graphs ;
22
3- import org .junit .jupiter .api .Test ;
43import static org .junit .jupiter .api .Assertions .*;
54
65import java .util .Arrays ;
76import java .util .List ;
7+ import org .junit .jupiter .api .Test ;
88
99/**
1010 * Test cases for TopologicalSortDFS
11- *
11+ *
1212 * @author gowtham1412-p
1313 */
1414class TopologicalSortDFSTest {
15-
15+
1616 @ Test
1717 void testSimpleDAG () {
1818 TopologicalSortDFS graph = new TopologicalSortDFS (6 );
@@ -22,97 +22,89 @@ void testSimpleDAG() {
2222 graph .addEdge (4 , 1 );
2323 graph .addEdge (2 , 3 );
2424 graph .addEdge (3 , 1 );
25-
25+
2626 List <Integer > result = graph .topologicalSort ();
27-
27+
2828 // Verify the order is valid
29- assertTrue (isValidTopologicalOrder (result , new int [][]{{5 ,2 }, {5 ,0 }, {4 ,0 }, {4 ,1 }, {2 ,3 }, {3 ,1 }}));
29+ assertTrue (isValidTopologicalOrder (result , new int [][] {{5 , 2 }, {5 , 0 }, {4 , 0 }, {4 , 1 }, {2 , 3 }, {3 , 1 }}));
3030 }
31-
31+
3232 @ Test
3333 void testLinearGraph () {
3434 TopologicalSortDFS graph = new TopologicalSortDFS (4 );
3535 graph .addEdge (0 , 1 );
3636 graph .addEdge (1 , 2 );
3737 graph .addEdge (2 , 3 );
38-
38+
3939 List <Integer > result = graph .topologicalSort ();
4040 assertEquals (Arrays .asList (0 , 1 , 2 , 3 ), result );
4141 }
42-
42+
4343 @ Test
4444 void testSingleVertex () {
4545 TopologicalSortDFS graph = new TopologicalSortDFS (1 );
4646 List <Integer > result = graph .topologicalSort ();
4747 assertEquals (Arrays .asList (0 ), result );
4848 }
49-
49+
5050 @ Test
5151 void testDisconnectedGraph () {
5252 TopologicalSortDFS graph = new TopologicalSortDFS (4 );
5353 graph .addEdge (0 , 1 );
5454 graph .addEdge (2 , 3 );
55-
55+
5656 List <Integer > result = graph .topologicalSort ();
5757 assertEquals (4 , result .size ());
5858 }
59-
59+
6060 @ Test
6161 void testCycleDetection () {
6262 TopologicalSortDFS graph = new TopologicalSortDFS (3 );
6363 graph .addEdge (0 , 1 );
6464 graph .addEdge (1 , 2 );
6565 graph .addEdge (2 , 0 ); // Creates a cycle
66-
67- assertThrows (IllegalArgumentException .class , () -> {
68- graph .topologicalSort ();
69- });
66+
67+ assertThrows (IllegalArgumentException .class , () -> { graph .topologicalSort (); });
7068 }
71-
69+
7270 @ Test
7371 void testSelfLoop () {
7472 TopologicalSortDFS graph = new TopologicalSortDFS (3 );
7573 graph .addEdge (0 , 1 );
7674 graph .addEdge (1 , 1 ); // Self loop
77-
78- assertThrows (IllegalArgumentException .class , () -> {
79- graph .topologicalSort ();
80- });
75+
76+ assertThrows (IllegalArgumentException .class , () -> { graph .topologicalSort (); });
8177 }
82-
78+
8379 @ Test
8480 void testIsDAG () {
8581 TopologicalSortDFS graph = new TopologicalSortDFS (4 );
8682 graph .addEdge (0 , 1 );
8783 graph .addEdge (1 , 2 );
8884 graph .addEdge (2 , 3 );
89-
85+
9086 assertTrue (graph .isDAG ());
9187 }
92-
88+
9389 @ Test
9490 void testIsNotDAG () {
9591 TopologicalSortDFS graph = new TopologicalSortDFS (3 );
9692 graph .addEdge (0 , 1 );
9793 graph .addEdge (1 , 2 );
9894 graph .addEdge (2 , 0 );
99-
95+
10096 assertFalse (graph .isDAG ());
10197 }
102-
98+
10399 @ Test
104100 void testInvalidVertex () {
105101 TopologicalSortDFS graph = new TopologicalSortDFS (3 );
106-
107- assertThrows (IllegalArgumentException .class , () -> {
108- graph .addEdge (-1 , 0 );
109- });
110-
111- assertThrows (IllegalArgumentException .class , () -> {
112- graph .addEdge (0 , 5 );
113- });
102+
103+ assertThrows (IllegalArgumentException .class , () -> { graph .addEdge (-1 , 0 ); });
104+
105+ assertThrows (IllegalArgumentException .class , () -> { graph .addEdge (0 , 5 ); });
114106 }
115-
107+
116108 @ Test
117109 void testComplexDAG () {
118110 TopologicalSortDFS graph = new TopologicalSortDFS (8 );
@@ -125,15 +117,15 @@ void testComplexDAG() {
125117 graph .addEdge (3 , 6 );
126118 graph .addEdge (3 , 7 );
127119 graph .addEdge (4 , 6 );
128-
120+
129121 List <Integer > result = graph .topologicalSort ();
130-
122+
131123 // Verify valid ordering
132124 assertTrue (result .indexOf (0 ) < result .indexOf (3 ));
133125 assertTrue (result .indexOf (3 ) < result .indexOf (5 ));
134126 assertTrue (result .indexOf (4 ) < result .indexOf (6 ));
135127 }
136-
128+
137129 /**
138130 * Helper method to verify topological order
139131 */
@@ -147,4 +139,4 @@ private boolean isValidTopologicalOrder(List<Integer> order, int[][] edges) {
147139 }
148140 return true ;
149141 }
150- }
142+ }
0 commit comments