Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 2 | * Copyright 2014 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 | 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; |
| 24 | |
| 25 | /** |
| 26 | * Test of the Bellman-Ford algorithm. |
| 27 | */ |
| 28 | public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest { |
| 29 | |
| 30 | @Override |
| 31 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 32 | return new BellmanFordGraphSearch<>(); |
| 33 | } |
| 34 | |
| 35 | @Test |
| 36 | @Override |
| 37 | public void defaultGraphTest() { |
| 38 | executeDefaultTest(7, 5, 5.0); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void defaultHopCountWeight() { |
| 43 | weight = null; |
| 44 | executeDefaultTest(10, 3, 3.0); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void searchGraphWithNegativeCycles() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 49 | Set<TestVertex> vertexes = new HashSet<>(vertexes()); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 50 | vertexes.add(Z); |
| 51 | |
| 52 | Set<TestEdge> edges = new HashSet<>(edges()); |
| 53 | edges.add(new TestEdge(G, Z, 1.0)); |
| 54 | edges.add(new TestEdge(Z, G, -2.0)); |
| 55 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 56 | graph = new AdjacencyListsGraph<>(vertexes, edges); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 57 | |
| 58 | GraphPathSearch<TestVertex, TestEdge> search = graphSearch(); |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 59 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 60 | assertEquals("incorrect paths count", 1, paths.size()); |
| 61 | |
| 62 | Path p = paths.iterator().next(); |
| 63 | assertEquals("incorrect src", A, p.src()); |
| 64 | assertEquals("incorrect dst", H, p.dst()); |
| 65 | assertEquals("incorrect path length", 5, p.edges().size()); |
| 66 | assertEquals("incorrect path cost", 5.0, p.cost(), 0.1); |
| 67 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 68 | paths = search.search(graph, A, G, weight).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 69 | assertEquals("incorrect paths count", 0, paths.size()); |
| 70 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 71 | paths = search.search(graph, A, null, weight).paths(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 72 | printPaths(paths); |
| 73 | assertEquals("incorrect paths count", 6, paths.size()); |
| 74 | } |
| 75 | |
| 76 | } |