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