Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Set; |
| 22 | |
Sbhat35 | bbb5a4b | 2017-06-21 10:40:58 -0700 | [diff] [blame] | 23 | import static com.google.common.collect.ImmutableSet.of; |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 25 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Test of the Bellman-Ford algorithm. |
| 29 | */ |
| 30 | public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest { |
| 31 | |
| 32 | @Override |
| 33 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 34 | return new BellmanFordGraphSearch<>(); |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | @Override |
| 39 | public void defaultGraphTest() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 40 | executeDefaultTest(7, 5, new TestDoubleWeight(5.0)); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void defaultHopCountWeight() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 45 | weigher = null; |
| 46 | executeDefaultTest(10, 3, new ScalarWeight(3.0)); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void searchGraphWithNegativeCycles() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 51 | Set<TestVertex> vertexes = new HashSet<>(vertexes()); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 52 | vertexes.add(Z); |
| 53 | |
| 54 | Set<TestEdge> edges = new HashSet<>(edges()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 55 | edges.add(new TestEdge(G, Z, new TestDoubleWeight(1.0))); |
| 56 | edges.add(new TestEdge(Z, G, new TestDoubleWeight(-2.0))); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 57 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 58 | graph = new AdjacencyListsGraph<>(vertexes, edges); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 59 | |
| 60 | GraphPathSearch<TestVertex, TestEdge> search = graphSearch(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 61 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weigher, ALL_PATHS).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 62 | assertEquals("incorrect paths count", 1, paths.size()); |
| 63 | |
| 64 | Path p = paths.iterator().next(); |
| 65 | assertEquals("incorrect src", A, p.src()); |
| 66 | assertEquals("incorrect dst", H, p.dst()); |
| 67 | assertEquals("incorrect path length", 5, p.edges().size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 68 | assertEquals("incorrect path cost", new TestDoubleWeight(5), p.cost()); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 69 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 70 | paths = search.search(graph, A, G, weigher, ALL_PATHS).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 71 | assertEquals("incorrect paths count", 0, paths.size()); |
| 72 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 73 | paths = search.search(graph, A, null, weigher, ALL_PATHS).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 74 | printPaths(paths); |
| 75 | assertEquals("incorrect paths count", 6, paths.size()); |
| 76 | } |
Sbhat35 | bbb5a4b | 2017-06-21 10:40:58 -0700 | [diff] [blame] | 77 | @Test |
| 78 | public void testNegativeEdge() { |
| 79 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 80 | of(new TestEdge(A, B, W3), |
| 81 | new TestEdge(A, C, W4), |
| 82 | new TestEdge(C, B, NW2), |
| 83 | new TestEdge(B, C, W2), |
| 84 | new TestEdge(A, D, W5), |
| 85 | new TestEdge(D, B, new TestDoubleWeight(-3)))); |
| 86 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
| 87 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weigher, 1).paths(); |
| 88 | printPaths(paths); |
| 89 | assertEquals("incorrect path count", 1, paths.size()); |
| 90 | assertEquals("incoreect path cost", new TestDoubleWeight(2), paths.iterator().next().cost()); |
| 91 | |
| 92 | executeSearch(graphSearch(), graph, A, B, weigher, 2, W2); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void multipleNegatives() { |
| 97 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 98 | of(new TestEdge(A, B, W1), |
| 99 | new TestEdge(B, C, W1), |
| 100 | new TestEdge(C, D, W1), |
| 101 | new TestEdge(D, E, W1), |
| 102 | new TestEdge(A, B, new TestDoubleWeight(-4)), |
| 103 | new TestEdge(A, C, new TestDoubleWeight(-3)), |
| 104 | new TestEdge(A, D, NW1), |
| 105 | new TestEdge(D, C, NW1))); |
| 106 | executeSearch(graphSearch(), graph, A, E, weigher, 2, NW1); |
| 107 | executeSinglePathSearch(graphSearch(), graph, A, E, weigher, 1, NW1); |
| 108 | executeSearch(graphSearch(), graph, B, C, weigher, 1, W1); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void manualWeights() { |
| 113 | graph = new AdjacencyListsGraph<>(of(A, B, C), |
| 114 | of(new TestEdge(A, B, new TestDoubleWeight(-3.3)), |
| 115 | new TestEdge(B, C, new TestDoubleWeight(1.3)), |
| 116 | new TestEdge(A, C, new TestDoubleWeight(-1.9)), |
| 117 | new TestEdge(B, C, new TestDoubleWeight(1.5)))); |
| 118 | executeSearch(graphSearch(), graph, A, C, weigher, 1, new TestDoubleWeight(-2.0)); |
| 119 | } |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 120 | |
| 121 | } |