Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 19 | package org.onlab.graph; |
| 20 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 25 | import static com.google.common.collect.ImmutableSet.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
| 27 | |
| 28 | /** |
| 29 | * Test of the Dijkstra algorithm. |
| 30 | */ |
| 31 | public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { |
| 32 | |
| 33 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 34 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 35 | return new DijkstraGraphSearch<>(); |
| 36 | } |
| 37 | |
| 38 | @Test |
| 39 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 40 | public void defaultGraphTest() { |
| 41 | executeDefaultTest(7, 5, 5.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 45 | @Override |
| 46 | public void defaultHopCountWeight() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 47 | weight = null; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 48 | executeDefaultTest(10, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void noPath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 53 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 54 | of(new TestEdge(A, B, 1), |
| 55 | new TestEdge(B, A, 1), |
| 56 | new TestEdge(C, D, 1), |
| 57 | new TestEdge(D, C, 1))); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 58 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 59 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 60 | printPaths(paths); |
| 61 | assertEquals("incorrect paths count", 1, paths.size()); |
| 62 | assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1); |
| 63 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 64 | paths = gs.search(graph, A, D, weight).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 65 | printPaths(paths); |
| 66 | assertEquals("incorrect paths count", 0, paths.size()); |
| 67 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 68 | paths = gs.search(graph, A, null, weight).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 69 | printPaths(paths); |
| 70 | assertEquals("incorrect paths count", 1, paths.size()); |
| 71 | assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1); |
| 72 | } |
| 73 | |
| 74 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 75 | public void simpleMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 76 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 77 | of(new TestEdge(A, B, 1), |
| 78 | new TestEdge(A, C, 1), |
| 79 | new TestEdge(B, D, 1), |
| 80 | new TestEdge(C, D, 1))); |
| 81 | executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 85 | public void denseMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 86 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
| 87 | of(new TestEdge(A, B, 1), |
| 88 | new TestEdge(A, C, 1), |
| 89 | new TestEdge(B, D, 1), |
| 90 | new TestEdge(C, D, 1), |
| 91 | new TestEdge(D, E, 1), |
| 92 | new TestEdge(D, F, 1), |
| 93 | new TestEdge(E, G, 1), |
| 94 | new TestEdge(F, G, 1), |
| 95 | new TestEdge(A, G, 4))); |
| 96 | executeSearch(graphSearch(), graph, A, G, weight, 5, 4.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 100 | public void dualEdgeMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 101 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H), |
| 102 | of(new TestEdge(A, B, 1), new TestEdge(A, C, 3), |
| 103 | new TestEdge(B, D, 2), new TestEdge(B, C, 1), |
| 104 | new TestEdge(B, E, 4), new TestEdge(C, E, 1), |
| 105 | new TestEdge(D, H, 5), new TestEdge(D, E, 1), |
| 106 | new TestEdge(E, F, 1), new TestEdge(F, D, 1), |
| 107 | new TestEdge(F, G, 1), new TestEdge(F, H, 1), |
| 108 | new TestEdge(A, E, 3), new TestEdge(B, D, 1))); |
| 109 | executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 112 | @Test |
| 113 | public void negativeWeights() { |
| 114 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
| 115 | of(new TestEdge(A, B, 1), |
| 116 | new TestEdge(A, C, -1), |
| 117 | new TestEdge(B, D, 1), |
| 118 | new TestEdge(D, A, -2), |
| 119 | new TestEdge(C, D, 1), |
| 120 | new TestEdge(D, E, 1), |
| 121 | new TestEdge(D, F, 1), |
| 122 | new TestEdge(E, G, 1), |
| 123 | new TestEdge(F, G, 1), |
| 124 | new TestEdge(G, A, -5), |
| 125 | new TestEdge(A, G, 4))); |
| 126 | executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0); |
| 127 | } |
| 128 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 129 | } |