66import static org .junit .jupiter .api .Assertions .assertThrows ;
77import static org .junit .jupiter .api .Assertions .assertTrue ;
88
9+ import java .util .ArrayList ;
910import java .util .Arrays ;
1011import java .util .List ;
1112import org .junit .jupiter .api .DisplayName ;
@@ -16,16 +17,19 @@ class TopologicalSortTest {
1617 @ Test
1718 @ DisplayName ("Simple DAG returns correct ordering" )
1819 void simpleDAG () {
19- List <int []> edges = Arrays .asList (new int [] { 0 , 1 }, new int [] { 0 , 2 }, new int [] { 1 , 3 },
20- new int [] { 2 , 3 });
20+ List <int []> edges = new ArrayList <>();
21+ edges .add (new int [] { 0 , 1 });
22+ edges .add (new int [] { 0 , 2 });
23+ edges .add (new int [] { 1 , 3 });
24+ edges .add (new int [] { 2 , 3 });
2125 int [] result = TopologicalSort .sort (4 , edges );
2226 assertArrayEquals (new int [] { 0 , 1 , 2 , 3 }, result );
2327 }
2428
2529 @ Test
2630 @ DisplayName ("Empty graph returns valid ordering" )
2731 void emptyGraph () {
28- List <int []> edges = Arrays . asList ();
32+ List <int []> edges = new ArrayList <> ();
2933 int [] result = TopologicalSort .sort (3 , edges );
3034 assertEquals (3 , result .length );
3135 // Any permutation is valid for empty graph
@@ -35,7 +39,10 @@ void emptyGraph() {
3539 @ Test
3640 @ DisplayName ("Graph with cycle returns null" )
3741 void graphWithCycle () {
38- List <int []> edges = Arrays .asList (new int [] { 0 , 1 }, new int [] { 1 , 2 }, new int [] { 2 , 0 });
42+ List <int []> edges = new ArrayList <>();
43+ edges .add (new int [] { 0 , 1 });
44+ edges .add (new int [] { 1 , 2 });
45+ edges .add (new int [] { 2 , 0 });
3946 assertNull (TopologicalSort .sort (3 , edges ));
4047 }
4148
@@ -44,11 +51,11 @@ void graphWithCycle() {
4451 void coursePrerequisites () {
4552 // Example: Course prerequisites where edge [a,b] means course a must be taken
4653 // before b
47- List <int []> edges = Arrays . asList ( new int [] { 1 , 0 }, // Calculus I -> Calculus II
48- new int [] { 2 , 0 }, // Linear Algebra -> Calculus II
49- new int [] { 1 , 3 }, // Calculus I -> Differential Equations
50- new int [] { 2 , 3 } // Linear Algebra -> Differential Equations
51- );
54+ List <int []> edges = new ArrayList <>();
55+ edges . add ( new int [] { 1 , 0 }); // Calculus I -> Calculus II
56+ edges . add ( new int [] { 2 , 0 }); // Linear Algebra -> Calculus II
57+ edges . add ( new int [] { 1 , 3 }); // Calculus I -> Differential Equations
58+ edges . add ( new int [] { 2 , 3 }); // Linear Algebra -> Differential Equations
5259 List <Integer > result = TopologicalSort .sortAndDetectCycle (4 , edges );
5360 // Either [1,2,0,3] or [2,1,0,3] is valid
5461 assertEquals (4 , result .size ());
@@ -62,8 +69,8 @@ void coursePrerequisites() {
6269 @ Test
6370 @ DisplayName ("Invalid vertex throws exception" )
6471 void invalidVertex () {
65- List <int []> edges = Arrays . asList ( new int [] { 0 , 5 } // Vertex 5 is invalid for a graph with 3 vertices
66- );
72+ List <int []> edges = new ArrayList <>();
73+ edges . add ( new int [] { 0 , 5 }); // Vertex 5 is invalid for a graph with 3 vertices
6774 assertThrows (IllegalArgumentException .class , () -> TopologicalSort .sort (3 , edges ));
6875 }
6976
@@ -76,7 +83,10 @@ void nullEdgeList() {
7683 @ Test
7784 @ DisplayName ("sortAndDetectCycle throws exception for cyclic graph" )
7885 void detectCycleThrowsException () {
79- List <int []> edges = Arrays .asList (new int [] { 0 , 1 }, new int [] { 1 , 2 }, new int [] { 2 , 0 });
86+ List <int []> edges = new ArrayList <>();
87+ edges .add (new int [] { 0 , 1 });
88+ edges .add (new int [] { 1 , 2 });
89+ edges .add (new int [] { 2 , 0 });
8090 assertThrows (IllegalArgumentException .class , () -> TopologicalSort .sortAndDetectCycle (3 , edges ));
8191 }
8292}
0 commit comments