blob: 5ee4caa656a9d8f0368f3ad6fd00eca573beca0d [file] [log] [blame]
tome3489412014-08-29 02:30:38 -07001package org.onlab.graph;
2
3import com.google.common.collect.ImmutableSet;
4import org.junit.Test;
5
6import java.util.Set;
7
8import static org.junit.Assert.assertEquals;
9
10/**
11 * Test of the Dijkstra algorithm.
12 */
13public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
14
15 @Override
16 protected GraphPathSearch<TestVertex, TestEdge> graphSearch() {
17 return new DijkstraGraphSearch<>();
18 }
19
20 @Test
21 @Override
22 public void basics() {
23 runBasics(5, 5.0, 7);
24 }
25
26 @Test
27 public void defaultWeight() {
28 weight = null;
29 runBasics(3, 3.0, 10);
30 }
31
32 @Test
33 public void noPath() {
34 g = new AdjacencyListsGraph<>(ImmutableSet.of(A, B, C, D),
35 ImmutableSet.of(new TestEdge(A, B, 1),
36 new TestEdge(B, A, 1),
37 new TestEdge(C, D, 1),
38 new TestEdge(D, C, 1)));
39
40 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
57 public void multiPath1() {
58 g = new AdjacencyListsGraph<>(ImmutableSet.of(A, B, C, D),
59 ImmutableSet.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
64 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
65 Set<Path<TestVertex, TestEdge>> paths = gs.search(g, A, D, weight).paths();
66 printPaths(paths);
67 assertEquals("incorrect paths count", 2, paths.size());
68 assertEquals("incorrect path cost", 2.0, paths.iterator().next().cost(), 0.1);
69 }
70
71 @Test
72 public void multiPath2() {
73 g = new AdjacencyListsGraph<>(ImmutableSet.of(A, B, C, D, E, F, G),
74 ImmutableSet.of(new TestEdge(A, B, 1),
75 new TestEdge(A, C, 1),
76 new TestEdge(B, D, 1),
77 new TestEdge(C, D, 1),
78 new TestEdge(D, E, 1),
79 new TestEdge(D, F, 1),
80 new TestEdge(E, G, 1),
81 new TestEdge(F, G, 1),
82 new TestEdge(A, G, 4)));
83
84 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
85 GraphPathSearch.Result<TestVertex, TestEdge> gsr = gs.search(g, A, G, weight);
86 Set<Path<TestVertex, TestEdge>> paths = gsr.paths();
87 printPaths(paths);
88 assertEquals("incorrect paths count", 5, paths.size());
89 assertEquals("incorrect path cost", 4.0, paths.iterator().next().cost(), 0.1);
90 }
91
92 @Test
93 public void multiPath3() {
94 g = new AdjacencyListsGraph<>(ImmutableSet.of(A, B, C, D, E, F, G, H),
95 ImmutableSet.of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
96 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
97 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
98 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
99 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
100 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
101 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
102
103 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
104 Set<Path<TestVertex, TestEdge>> paths = gs.search(g, A, E, weight).paths();
105 printPaths(paths);
106 assertEquals("incorrect paths count", 3, paths.size());
107 assertEquals("incorrect path cost", 3.0, paths.iterator().next().cost(), 0.1);
108 }
109
110}