1
1
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .HashMap ;
4
5
import java .util .List ;
5
- import java .util .ArrayList ;
6
6
import java .util .Map ;
7
7
import org .junit .jupiter .api .BeforeEach ;
8
8
import org .junit .jupiter .api .Test ;
@@ -19,41 +19,38 @@ public void setUp() {
19
19
@ Test
20
20
public void testSinglePath () {
21
21
Map <Integer , List <int []>> adjList = new HashMap <>();
22
- adjList .put (0 , List .of (new int []{1 , 1 }));
23
- adjList .put (1 , List .of (new int []{2 , 1 }));
24
- adjList .put (2 , List .of (new int []{3 , 1 }));
22
+ adjList .put (0 , List .of (new int [] {1 , 1 }));
23
+ adjList .put (1 , List .of (new int [] {2 , 1 }));
24
+ adjList .put (2 , List .of (new int [] {3 , 1 }));
25
25
adjList .put (3 , new ArrayList <>());
26
26
27
27
int [] result = dijkstra .shortestPath (4 , adjList , 0 );
28
-
29
28
int [] expected = {0 , 1 , 2 , 3 };
30
- assertArrayEquals (expected , result , "Shortest path distances should match." );
29
+ assertArrayEquals (expected , result , "Distances should match expected shortest path distances ." );
31
30
}
32
31
33
32
@ Test
34
33
public void testDisconnectedGraph () {
35
34
Map <Integer , List <int []>> adjList = new HashMap <>();
36
- adjList .put (0 , List .of (new int []{1 , 2 }));
37
- adjList .put (1 , List .of (new int []{2 , 2 }));
35
+ adjList .put (0 , List .of (new int [] {1 , 2 }));
36
+ adjList .put (1 , List .of (new int [] {2 , 2 }));
38
37
adjList .put (2 , new ArrayList <>());
39
38
adjList .put (3 , new ArrayList <>());
40
39
41
40
int [] result = dijkstra .shortestPath (4 , adjList , 0 );
42
-
43
41
int [] expected = {0 , 2 , 4 , Integer .MAX_VALUE };
44
- assertArrayEquals (expected , result , "Shortest path should indicate unreachable nodes ." );
42
+ assertArrayEquals (expected , result , "Distances should match expected shortest path distances ." );
45
43
}
46
44
47
45
@ Test
48
46
public void testComplexGraph () {
49
47
Map <Integer , List <int []>> adjList = new HashMap <>();
50
- adjList .put (0 , List .of (new int []{1 , 4 }, new int []{2 , 1 }));
51
- adjList .put (1 , List .of (new int []{3 , 1 }));
52
- adjList .put (2 , List .of (new int []{1 , 2 }, new int []{3 , 5 }));
48
+ adjList .put (0 , List .of (new int [] {1 , 4 }, new int [] {2 , 1 }));
49
+ adjList .put (1 , List .of (new int [] {3 , 1 }));
50
+ adjList .put (2 , List .of (new int [] {1 , 2 }, new int [] {3 , 5 }));
53
51
adjList .put (3 , new ArrayList <>());
54
52
55
53
int [] result = dijkstra .shortestPath (4 , adjList , 0 );
56
-
57
54
int [] expected = {0 , 3 , 1 , 4 };
58
55
assertArrayEquals (expected , result , "Distances should match expected shortest path distances." );
59
56
}
@@ -62,15 +59,13 @@ public void testComplexGraph() {
62
59
public void testRevisitedNodeWithHigherDistance () {
63
60
// This graph is set up to test the condition where a node is revisited with a higher distance.
64
61
Map <Integer , List <int []>> adjList = new HashMap <>();
65
- adjList .put (0 , List .of (new int []{1 , 5 }));
66
- adjList .put (1 , List .of (new int []{2 , 1 }));
67
- adjList .put (2 , List .of (new int []{0 , 3 }, new int []{3 , 1 }));
62
+ adjList .put (0 , List .of (new int [] {1 , 5 }));
63
+ adjList .put (1 , List .of (new int [] {2 , 1 }));
64
+ adjList .put (2 , List .of (new int [] {0 , 3 }, new int [] {3 , 1 }));
68
65
adjList .put (3 , new ArrayList <>());
69
66
70
67
int [] result = dijkstra .shortestPath (4 , adjList , 0 );
71
-
72
68
int [] expected = {0 , 5 , 6 , 7 };
73
69
assertArrayEquals (expected , result , "Distances should match expected shortest path distances." );
74
70
}
75
71
}
76
- //added test cases
0 commit comments