tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 3 | import org.junit.Test; |
| 4 | |
| 5 | import java.util.Set; |
| 6 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 7 | import static com.google.common.collect.ImmutableSet.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 8 | import static org.junit.Assert.assertEquals; |
| 9 | |
| 10 | /** |
| 11 | * Test of the Dijkstra algorithm. |
| 12 | */ |
| 13 | public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { |
| 14 | |
| 15 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 16 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 17 | return new DijkstraGraphSearch<>(); |
| 18 | } |
| 19 | |
| 20 | @Test |
| 21 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 22 | public void defaultGraphTest() { |
| 23 | executeDefaultTest(7, 5, 5.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 27 | @Override |
| 28 | public void defaultHopCountWeight() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 29 | weight = null; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 30 | executeDefaultTest(10, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | @Test |
| 34 | public void noPath() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 35 | 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))); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 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 |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 57 | 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); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 67 | 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); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 82 | 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); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } |