Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 20 | import java.text.DecimalFormat; |
| 21 | import java.util.HashSet; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 24 | import static com.google.common.collect.ImmutableSet.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | |
| 27 | /** |
| 28 | * Test of the Dijkstra algorithm. |
| 29 | */ |
| 30 | public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { |
| 31 | |
| 32 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 34 | return new DijkstraGraphSearch<>(); |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 39 | public void defaultGraphTest() { |
| 40 | executeDefaultTest(7, 5, 5.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public void defaultHopCountWeight() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 46 | weight = null; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 47 | executeDefaultTest(10, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void noPath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 52 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 53 | of(new TestEdge(A, B, 1), |
| 54 | new TestEdge(B, A, 1), |
| 55 | new TestEdge(C, D, 1), |
| 56 | new TestEdge(D, C, 1))); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 57 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 58 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 59 | printPaths(paths); |
| 60 | assertEquals("incorrect paths count", 1, paths.size()); |
| 61 | assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1); |
| 62 | |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 63 | paths = gs.search(graph, A, D, weight, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | printPaths(paths); |
| 65 | assertEquals("incorrect paths count", 0, paths.size()); |
| 66 | |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 67 | paths = gs.search(graph, A, null, weight, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | printPaths(paths); |
| 69 | assertEquals("incorrect paths count", 1, paths.size()); |
| 70 | assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1); |
| 71 | } |
| 72 | |
| 73 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 74 | public void simpleMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 75 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 76 | of(new TestEdge(A, B, 1), |
| 77 | new TestEdge(A, C, 1), |
| 78 | new TestEdge(B, D, 1), |
| 79 | new TestEdge(C, D, 1))); |
| 80 | executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 81 | executeSinglePathSearch(graphSearch(), graph, A, D, weight, 1, 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); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 97 | executeSinglePathSearch(graphSearch(), graph, A, G, weight, 1, 4.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 101 | public void dualEdgeMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 102 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H), |
| 103 | of(new TestEdge(A, B, 1), new TestEdge(A, C, 3), |
| 104 | new TestEdge(B, D, 2), new TestEdge(B, C, 1), |
| 105 | new TestEdge(B, E, 4), new TestEdge(C, E, 1), |
| 106 | new TestEdge(D, H, 5), new TestEdge(D, E, 1), |
| 107 | new TestEdge(E, F, 1), new TestEdge(F, D, 1), |
| 108 | new TestEdge(F, G, 1), new TestEdge(F, H, 1), |
| 109 | new TestEdge(A, E, 3), new TestEdge(B, D, 1))); |
| 110 | executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 111 | executeSinglePathSearch(graphSearch(), graph, A, E, weight, 1, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 114 | @Test |
| 115 | public void negativeWeights() { |
| 116 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
| 117 | of(new TestEdge(A, B, 1), |
| 118 | new TestEdge(A, C, -1), |
| 119 | new TestEdge(B, D, 1), |
| 120 | new TestEdge(D, A, -2), |
| 121 | new TestEdge(C, D, 1), |
| 122 | new TestEdge(D, E, 1), |
| 123 | new TestEdge(D, F, 1), |
| 124 | new TestEdge(E, G, 1), |
| 125 | new TestEdge(F, G, 1), |
| 126 | new TestEdge(G, A, -5), |
| 127 | new TestEdge(A, G, 4))); |
| 128 | executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 129 | executeSinglePathSearch(graphSearch(), graph, A, G, weight, 1, 4.0); |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 132 | @Test |
| 133 | public void disconnectedPerf() { |
| 134 | disconnected(); |
| 135 | disconnected(); |
| 136 | disconnected(); |
| 137 | disconnected(); |
| 138 | disconnected(); |
| 139 | disconnected(); |
| 140 | disconnected(); |
| 141 | disconnected(); |
| 142 | disconnected(); |
| 143 | disconnected(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | @Test |
| 148 | public void disconnected() { |
| 149 | Set<TestVertex> vertexes = new HashSet<>(); |
| 150 | for (int i = 0; i < 200; i++) { |
| 151 | vertexes.add(new TestVertex("v" + i)); |
| 152 | } |
| 153 | |
| 154 | graph = new AdjacencyListsGraph<>(vertexes, of()); |
| 155 | |
| 156 | long start = System.nanoTime(); |
| 157 | for (TestVertex src : vertexes) { |
| 158 | executeSearch(graphSearch(), graph, src, null, null, 0, 0); |
| 159 | } |
| 160 | long end = System.nanoTime(); |
| 161 | DecimalFormat fmt = new DecimalFormat("#,###"); |
| 162 | System.out.println("Compute cost is " + fmt.format(end - start) + " nanos"); |
| 163 | } |
| 164 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 165 | } |