Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.graph; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import static com.google.common.collect.ImmutableSet.of; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | //import static org.junit.Assert.assertTrue; |
| 24 | |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Test of the Suurballe backup path algorithm. |
| 29 | */ |
| 30 | public class SuurballeGraphSearchTest extends BreadthFirstSearchTest { |
| 31 | |
| 32 | @Override |
| 33 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 34 | return new SuurballeGraphSearch<>(); |
| 35 | } |
| 36 | |
| 37 | public void setWeights() { |
| 38 | weight = new EdgeWeight<TestVertex, TestEdge>() { |
| 39 | @Override |
| 40 | public double weight(TestEdge edge) { |
| 41 | return edge.weight(); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | public void setDefaultWeights() { |
| 46 | weight = null; |
| 47 | } |
| 48 | @Override |
| 49 | public void defaultGraphTest() { |
| 50 | |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void defaultHopCountWeight() { |
| 55 | |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void basicGraphTest() { |
| 60 | setDefaultWeights(); |
| 61 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 62 | of(new TestEdge(A, B, 1), |
| 63 | new TestEdge(B, C, 1), |
| 64 | new TestEdge(A, D, 1), |
| 65 | new TestEdge(D, C, 1))); |
| 66 | executeSearch(graphSearch(), graph, A, C, weight, 1, 4.0); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void multiplePathOnePairGraphTest() { |
| 71 | setWeights(); |
| 72 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 73 | of(new TestEdge(A, B, 1), |
| 74 | new TestEdge(B, C, 1), |
| 75 | new TestEdge(A, D, 1), |
| 76 | new TestEdge(D, C, 1), |
| 77 | new TestEdge(B, E, 2), |
| 78 | new TestEdge(C, E, 1))); |
| 79 | executeSearch(graphSearch(), graph, A, E, weight, 1, 6.0); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void multiplePathsMultiplePairs() { |
| 84 | setWeights(); |
| 85 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 86 | of(new TestEdge(A, B, 1), |
| 87 | new TestEdge(B, E, 1), |
| 88 | new TestEdge(A, C, 1), |
| 89 | new TestEdge(C, E, 1), |
| 90 | new TestEdge(A, D, 1), |
| 91 | new TestEdge(D, E, 1), |
| 92 | new TestEdge(A, E, 2))); |
| 93 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
| 94 | graphSearch().search(graph, A, E, weight, GraphPathSearch.ALL_PATHS); |
| 95 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 96 | System.out.println("\n\n" + paths + "\n\n\ndone\n"); |
| 97 | assertEquals("incorrect paths count", 3, paths.size()); |
| 98 | DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next(); |
| 99 | assertEquals("incorrect disjoint paths per path", 2, dpp.size()); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void differingPrimaryAndBackupPathLengths() { |
| 104 | setWeights(); |
| 105 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 106 | of(new TestEdge(A, B, 1), |
| 107 | new TestEdge(B, C, 1), |
| 108 | new TestEdge(A, D, 1), |
| 109 | new TestEdge(D, C, 1), |
| 110 | new TestEdge(B, E, 1), |
| 111 | new TestEdge(C, E, 1))); |
| 112 | executeSearch(graphSearch(), graph, A, E, weight, 1, 5.0); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void onePath() { |
| 117 | setWeights(); |
| 118 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 119 | of(new TestEdge(A, B, 1), |
| 120 | new TestEdge(B, C, 1), |
| 121 | new TestEdge(A, C, 4), |
| 122 | new TestEdge(C, D, 1))); |
| 123 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
| 124 | graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS); |
| 125 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 126 | assertEquals("incorrect paths count", 1, paths.size()); |
| 127 | DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next(); |
| 128 | assertEquals("incorrect disjoint paths count", 1, dpp.size()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void noPath() { |
| 133 | setWeights(); |
| 134 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 135 | of(new TestEdge(A, B, 1), |
| 136 | new TestEdge(B, C, 1), |
| 137 | new TestEdge(A, C, 4))); |
| 138 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
| 139 | graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS); |
| 140 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 141 | assertEquals("incorrect paths count", paths.size(), 0); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void disconnected() { |
| 146 | setWeights(); |
| 147 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 148 | of()); |
| 149 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
| 150 | graphSearch().search(graph, A, D, weight, GraphPathSearch.ALL_PATHS); |
| 151 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 152 | assertEquals("incorrect paths count", 0, paths.size()); |
| 153 | } |
| 154 | } |