Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -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 | |
| 23 | import static org.junit.Assert.assertEquals; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 24 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Test of the Bellman-Ford algorithm. |
| 28 | */ |
| 29 | public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest { |
| 30 | |
| 31 | @Override |
| 32 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 33 | return new BellmanFordGraphSearch<>(); |
| 34 | } |
| 35 | |
| 36 | @Test |
| 37 | @Override |
| 38 | public void defaultGraphTest() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 39 | executeDefaultTest(7, 5, new TestDoubleWeight(5.0)); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void defaultHopCountWeight() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 44 | weigher = null; |
| 45 | executeDefaultTest(10, 3, new ScalarWeight(3.0)); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Test |
| 49 | public void searchGraphWithNegativeCycles() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 50 | Set<TestVertex> vertexes = new HashSet<>(vertexes()); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 51 | vertexes.add(Z); |
| 52 | |
| 53 | Set<TestEdge> edges = new HashSet<>(edges()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 54 | edges.add(new TestEdge(G, Z, new TestDoubleWeight(1.0))); |
| 55 | edges.add(new TestEdge(Z, G, new TestDoubleWeight(-2.0))); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 56 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 57 | graph = new AdjacencyListsGraph<>(vertexes, edges); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 58 | |
| 59 | GraphPathSearch<TestVertex, TestEdge> search = graphSearch(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 60 | 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] | 61 | assertEquals("incorrect paths count", 1, paths.size()); |
| 62 | |
| 63 | Path p = paths.iterator().next(); |
| 64 | assertEquals("incorrect src", A, p.src()); |
| 65 | assertEquals("incorrect dst", H, p.dst()); |
| 66 | assertEquals("incorrect path length", 5, p.edges().size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 67 | assertEquals("incorrect path cost", new TestDoubleWeight(5), p.cost()); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 68 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 69 | paths = search.search(graph, A, G, weigher, ALL_PATHS).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 70 | assertEquals("incorrect paths count", 0, paths.size()); |
| 71 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 72 | paths = search.search(graph, A, null, weigher, ALL_PATHS).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 73 | printPaths(paths); |
| 74 | assertEquals("incorrect paths count", 6, paths.size()); |
| 75 | } |
| 76 | |
| 77 | } |