Skip to content

Commit 28f0f57

Browse files
author
SUZUB
committed
test: add DijkstraPriorityQueue tests; format imports
1 parent 3802111 commit 28f0f57

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/main/java/com/thealgorithms/others/Dijkstra.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.thealgorithms.others;
22

3+
import java.util.Comparator;
34
import java.util.HashMap;
45
import java.util.Map;
56
import java.util.PriorityQueue;
6-
import java.util.Comparator;
7+
78
/**
89
* Dijkstra's algorithm,is a graph search algorithm that solves the
910
* single-source shortest path problem for a graph with nonnegative edge path
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.graphs;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
13+
14+
/**
15+
* Unit tests for {@link DijkstraPriorityQueue}.
16+
*/
17+
public class DijkstraPriorityQueueTest {
18+
19+
private DijkstraPriorityQueue dijkstra;
20+
21+
@BeforeEach
22+
void setUp() {
23+
dijkstra = new DijkstraPriorityQueue();
24+
}
25+
26+
@Test
27+
void testSimpleGraph() {
28+
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
29+
graph.put(0, List.of(new DijkstraPriorityQueue.Edge(1, 7), new DijkstraPriorityQueue.Edge(2, 9)));
30+
graph.put(1, List.of(new DijkstraPriorityQueue.Edge(2, 10)));
31+
graph.put(2, new ArrayList<>());
32+
33+
int[] result = dijkstra.runDijkstra(0, graph, 3);
34+
int[] expected = {0, 7, 9};
35+
assertArrayEquals(expected, result);
36+
}
37+
38+
@Test
39+
void testNegativeWeightThrows() {
40+
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
41+
graph.put(0, List.of(new DijkstraPriorityQueue.Edge(1, -1)));
42+
graph.put(1, new ArrayList<>());
43+
44+
assertThrows(IllegalArgumentException.class, () -> dijkstra.runDijkstra(0, graph, 2));
45+
}
46+
47+
@Test
48+
void testDisconnectedGraph() {
49+
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
50+
graph.put(0, new ArrayList<>());
51+
graph.put(1, new ArrayList<>());
52+
53+
int[] result = dijkstra.runDijkstra(0, graph, 2);
54+
int[] expected = {0, Integer.MAX_VALUE};
55+
assertArrayEquals(expected, result);
56+
}
57+
58+
@Test
59+
void testEmptyGraph() {
60+
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
61+
int[] result = dijkstra.runDijkstra(0, graph, 0);
62+
int[] expected = {};
63+
assertArrayEquals(expected, result);
64+
}
65+
}

0 commit comments

Comments
 (0)