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