-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathDijkstraShortestPathTest.java
63 lines (50 loc) · 2.12 KB
/
DijkstraShortestPathTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DijkstraShortestPathTest {
private DijkstraShortestPath dijkstra;
@BeforeEach
public void setUp() {
dijkstra = new DijkstraShortestPath();
}
@Test
public void testSinglePath() {
// Simple graph where the path is straightforward
Map<Integer, List<int[]>> adjList = new HashMap<>();
adjList.put(0, List.of(new int[]{1, 1}));
adjList.put(1, List.of(new int[]{2, 1}));
adjList.put(2, List.of(new int[]{3, 1}));
adjList.put(3, new ArrayList<>());
int[] result = dijkstra.shortestPath(4, adjList, 0);
int[] expected = {0, 1, 2, 3};
assertArrayEquals(expected, result, "Shortest path distances should match.");
}
@Test
public void testDisconnectedGraph() {
// Graph where some nodes are unreachable
Map<Integer, List<int[]>> adjList = new HashMap<>();
adjList.put(0, List.of(new int[]{1, 2}));
adjList.put(1, List.of(new int[]{2, 2}));
adjList.put(2, new ArrayList<>());
adjList.put(3, new ArrayList<>());
int[] result = dijkstra.shortestPath(4, adjList, 0);
int[] expected = {0, 2, 4, Integer.MAX_VALUE}; // node 3 is disconnected
assertArrayEquals(expected, result, "Shortest path should indicate unreachable nodes.");
}
@Test
public void testComplexGraph() {
// Complex graph with multiple paths
Map<Integer, List<int[]>> adjList = new HashMap<>();
adjList.put(0, List.of(new int[]{1, 4}, new int[]{2, 1}));
adjList.put(1, List.of(new int[]{3, 1}));
adjList.put(2, List.of(new int[]{1, 2}, new int[]{3, 5}));
adjList.put(3, new ArrayList<>());
int[] result = dijkstra.shortestPath(4, adjList, 0);
int[] expected = {0, 3, 1, 4};
assertArrayEquals(expected, result, "Distances should match expected shortest path distances.");
}
}