|
1 | 1 | package algorithms; |
2 | 2 |
|
3 | | -import static org.junit.Assert.*; |
| 3 | +import com.baeldung.algorithms.dijkstra.Dijkstra; |
| 4 | +import com.baeldung.algorithms.dijkstra.Graph; |
| 5 | +import com.baeldung.algorithms.dijkstra.Node; |
| 6 | +import org.junit.Test; |
4 | 7 |
|
5 | 8 | import java.util.Arrays; |
6 | 9 | import java.util.List; |
7 | 10 |
|
8 | | -import org.junit.Test; |
9 | | - |
10 | | -import com.baeldung.algorithms.dijkstra.Dijkstra; |
11 | | -import com.baeldung.algorithms.dijkstra.Graph; |
12 | | -import com.baeldung.algorithms.dijkstra.Node; |
| 11 | +import static org.junit.Assert.assertTrue; |
13 | 12 |
|
14 | 13 | public class DijkstraAlgorithmTest { |
15 | 14 |
|
16 | 15 | @Test |
17 | 16 | public void whenSPPSolved_thenCorrect() { |
18 | | - |
| 17 | + |
19 | 18 | Node nodeA = new Node("A"); |
20 | 19 | Node nodeB = new Node("B"); |
21 | 20 | Node nodeC = new Node("C"); |
22 | | - Node nodeD = new Node("D"); |
| 21 | + Node nodeD = new Node("D"); |
23 | 22 | Node nodeE = new Node("E"); |
24 | 23 | Node nodeF = new Node("F"); |
25 | | - |
| 24 | + |
26 | 25 | nodeA.addDestination(nodeB, 10); |
27 | 26 | nodeA.addDestination(nodeC, 15); |
28 | | - |
| 27 | + |
29 | 28 | nodeB.addDestination(nodeD, 12); |
30 | 29 | nodeB.addDestination(nodeF, 15); |
31 | | - |
| 30 | + |
32 | 31 | nodeC.addDestination(nodeE, 10); |
33 | | - |
| 32 | + |
34 | 33 | nodeD.addDestination(nodeE, 2); |
35 | 34 | nodeD.addDestination(nodeF, 1); |
36 | | - |
| 35 | + |
37 | 36 | nodeF.addDestination(nodeE, 5); |
38 | | - |
39 | | - Graph graph = new Graph(); |
40 | | - |
| 37 | + |
| 38 | + Graph graph = new Graph(); |
| 39 | + |
41 | 40 | graph.addNode(nodeA); |
42 | 41 | graph.addNode(nodeB); |
43 | 42 | graph.addNode(nodeC); |
44 | 43 | graph.addNode(nodeD); |
45 | 44 | graph.addNode(nodeE); |
46 | 45 | graph.addNode(nodeF); |
47 | | - |
| 46 | + |
48 | 47 | graph = Dijkstra.calculateShortestPathFromSource(graph, nodeA); |
49 | | - |
| 48 | + |
50 | 49 | List<Node> shortestPathForNodeB = Arrays.asList(nodeA); |
51 | 50 | List<Node> shortestPathForNodeC = Arrays.asList(nodeA); |
52 | 51 | List<Node> shortestPathForNodeD = Arrays.asList(nodeA, nodeB); |
53 | 52 | List<Node> shortestPathForNodeE = Arrays.asList(nodeA, nodeB, nodeD); |
54 | 53 | List<Node> shortestPathForNodeF = Arrays.asList(nodeA, nodeB, nodeD); |
55 | 54 |
|
56 | | - for(Node node : graph.getNodes()) { |
| 55 | + for (Node node : graph.getNodes()) { |
57 | 56 | switch (node.getName()) { |
58 | 57 | case "B": |
59 | | - assertTrue(node.getShortestPath().equals(shortestPathForNodeB)); |
| 58 | + assertTrue(node |
| 59 | + .getShortestPath() |
| 60 | + .equals(shortestPathForNodeB)); |
60 | 61 | break; |
61 | 62 | case "C": |
62 | | - assertTrue(node.getShortestPath().equals(shortestPathForNodeC)); |
| 63 | + assertTrue(node |
| 64 | + .getShortestPath() |
| 65 | + .equals(shortestPathForNodeC)); |
63 | 66 | break; |
64 | 67 | case "D": |
65 | | - assertTrue(node.getShortestPath().equals(shortestPathForNodeD)); |
| 68 | + assertTrue(node |
| 69 | + .getShortestPath() |
| 70 | + .equals(shortestPathForNodeD)); |
66 | 71 | break; |
67 | 72 | case "E": |
68 | | - assertTrue(node.getShortestPath().equals(shortestPathForNodeE)); |
| 73 | + assertTrue(node |
| 74 | + .getShortestPath() |
| 75 | + .equals(shortestPathForNodeE)); |
69 | 76 | break; |
70 | 77 | case "F": |
71 | | - assertTrue(node.getShortestPath().equals(shortestPathForNodeF)); |
| 78 | + assertTrue(node |
| 79 | + .getShortestPath() |
| 80 | + .equals(shortestPathForNodeF)); |
72 | 81 | break; |
73 | 82 | } |
74 | 83 | } |
|
0 commit comments