Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 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 | |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 37 | @Override |
| 38 | public void defaultGraphTest() { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void defaultHopCountWeight() { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void basicGraphTest() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 49 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 50 | of(new TestEdge(A, B), |
| 51 | new TestEdge(B, C), |
| 52 | new TestEdge(A, D), |
| 53 | new TestEdge(D, C))); |
| 54 | executeSearch(graphSearch(), graph, A, C, null, 1, new ScalarWeight(4.0)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void multiplePathOnePairGraphTest() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 59 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 60 | of(new TestEdge(A, B, W1), |
| 61 | new TestEdge(B, C, W1), |
| 62 | new TestEdge(A, D, W1), |
| 63 | new TestEdge(D, C, W1), |
| 64 | new TestEdge(B, E, W2), |
| 65 | new TestEdge(C, E, W1))); |
| 66 | executeSearch(graphSearch(), graph, A, E, weigher, 1, new TestDoubleWeight(6.0)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void multiplePathsMultiplePairs() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 71 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 72 | of(new TestEdge(A, B, W1), |
| 73 | new TestEdge(B, E, W1), |
| 74 | new TestEdge(A, C, W1), |
| 75 | new TestEdge(C, E, W1), |
| 76 | new TestEdge(A, D, W1), |
| 77 | new TestEdge(D, E, W1), |
| 78 | new TestEdge(A, E, W2))); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 79 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 80 | graphSearch().search(graph, A, E, weigher, GraphPathSearch.ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 81 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 82 | System.out.println("\n\n" + paths + "\n\n\ndone\n"); |
| 83 | assertEquals("incorrect paths count", 3, paths.size()); |
| 84 | DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next(); |
| 85 | assertEquals("incorrect disjoint paths per path", 2, dpp.size()); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void differingPrimaryAndBackupPathLengths() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 90 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 91 | of(new TestEdge(A, B), |
| 92 | new TestEdge(B, C), |
| 93 | new TestEdge(A, D), |
| 94 | new TestEdge(D, C), |
| 95 | new TestEdge(B, E), |
| 96 | new TestEdge(C, E))); |
| 97 | executeSearch(graphSearch(), graph, A, E, weigher, 1, new TestDoubleWeight(5.0)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void onePath() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 102 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 103 | of(new TestEdge(A, B, W1), |
| 104 | new TestEdge(B, C, W1), |
| 105 | new TestEdge(A, C, W4), |
| 106 | new TestEdge(C, D, W1))); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 107 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 108 | graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 109 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 110 | assertEquals("incorrect paths count", 1, paths.size()); |
| 111 | DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next(); |
| 112 | assertEquals("incorrect disjoint paths count", 1, dpp.size()); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void noPath() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 117 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 118 | of(new TestEdge(A, B, W1), |
| 119 | new TestEdge(B, C, W1), |
| 120 | new TestEdge(A, C, W4))); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 121 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 122 | graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 123 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 124 | assertEquals("incorrect paths count", paths.size(), 0); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void disconnected() { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 129 | Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 130 | of()); |
| 131 | GraphPathSearch.Result<TestVertex, TestEdge> result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 132 | graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 133 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 134 | assertEquals("incorrect paths count", 0, paths.size()); |
| 135 | } |
| 136 | } |