blob: 1731440144ffd816e530be49fb1995250c68bf62 [file] [log] [blame]
tome3489412014-08-29 02:30:38 -07001package org.onlab.graph;
2
tome3489412014-08-29 02:30:38 -07003import org.junit.Test;
4
5import java.util.Set;
6
tom144de692014-08-29 11:38:44 -07007import static com.google.common.collect.ImmutableSet.of;
tome3489412014-08-29 02:30:38 -07008import static org.junit.Assert.assertEquals;
9
10/**
11 * Test of the Dijkstra algorithm.
12 */
13public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
14
15 @Override
tom144de692014-08-29 11:38:44 -070016 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
tome3489412014-08-29 02:30:38 -070017 return new DijkstraGraphSearch<>();
18 }
19
20 @Test
21 @Override
tom144de692014-08-29 11:38:44 -070022 public void defaultGraphTest() {
23 executeDefaultTest(7, 5, 5.0);
tome3489412014-08-29 02:30:38 -070024 }
25
26 @Test
tom144de692014-08-29 11:38:44 -070027 @Override
28 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070029 weight = null;
tom144de692014-08-29 11:38:44 -070030 executeDefaultTest(10, 3, 3.0);
tome3489412014-08-29 02:30:38 -070031 }
32
33 @Test
34 public void noPath() {
tom144de692014-08-29 11:38:44 -070035 g = new AdjacencyListsGraph<>(of(A, B, C, D),
36 of(new TestEdge(A, B, 1),
37 new TestEdge(B, A, 1),
38 new TestEdge(C, D, 1),
39 new TestEdge(D, C, 1)));
tome3489412014-08-29 02:30:38 -070040 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
41 Set<Path<TestVertex, TestEdge>> paths = gs.search(g, A, B, weight).paths();
42 printPaths(paths);
43 assertEquals("incorrect paths count", 1, paths.size());
44 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
45
46 paths = gs.search(g, A, D, weight).paths();
47 printPaths(paths);
48 assertEquals("incorrect paths count", 0, paths.size());
49
50 paths = gs.search(g, A, null, weight).paths();
51 printPaths(paths);
52 assertEquals("incorrect paths count", 1, paths.size());
53 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
54 }
55
56 @Test
tom144de692014-08-29 11:38:44 -070057 public void simpleMultiplePath() {
58 g = new AdjacencyListsGraph<>(of(A, B, C, D),
59 of(new TestEdge(A, B, 1),
60 new TestEdge(A, C, 1),
61 new TestEdge(B, D, 1),
62 new TestEdge(C, D, 1)));
63 executeSearch(graphSearch(), g, A, D, weight, 2, 2.0);
tome3489412014-08-29 02:30:38 -070064 }
65
66 @Test
tom144de692014-08-29 11:38:44 -070067 public void denseMultiplePath() {
68 g = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
69 of(new TestEdge(A, B, 1),
70 new TestEdge(A, C, 1),
71 new TestEdge(B, D, 1),
72 new TestEdge(C, D, 1),
73 new TestEdge(D, E, 1),
74 new TestEdge(D, F, 1),
75 new TestEdge(E, G, 1),
76 new TestEdge(F, G, 1),
77 new TestEdge(A, G, 4)));
78 executeSearch(graphSearch(), g, A, G, weight, 5, 4.0);
tome3489412014-08-29 02:30:38 -070079 }
80
81 @Test
tom144de692014-08-29 11:38:44 -070082 public void dualEdgeMultiplePath() {
83 g = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
84 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
85 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
86 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
87 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
88 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
89 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
90 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
91 executeSearch(graphSearch(), g, A, E, weight, 3, 3.0);
tome3489412014-08-29 02:30:38 -070092 }
93
94}