blob: 0a36d77c25726fd4290c6c6cf2afc8c86eb11df6 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom2e1f0712014-08-29 13:32:00 -070016package org.onlab.graph;
17
18import org.junit.Test;
19
20import java.util.HashSet;
21import java.util.Set;
22
23import static org.junit.Assert.assertEquals;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080024import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
tom2e1f0712014-08-29 13:32:00 -070025
26/**
27 * Test of the Bellman-Ford algorithm.
28 */
29public 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 Komarov2398d962016-09-26 15:11:23 +030039 executeDefaultTest(7, 5, new TestDoubleWeight(5.0));
tom2e1f0712014-08-29 13:32:00 -070040 }
41
42 @Test
43 public void defaultHopCountWeight() {
Andrey Komarov2398d962016-09-26 15:11:23 +030044 weigher = null;
45 executeDefaultTest(10, 3, new ScalarWeight(3.0));
tom2e1f0712014-08-29 13:32:00 -070046 }
47
48 @Test
49 public void searchGraphWithNegativeCycles() {
tom0633d682014-09-10 12:10:03 -070050 Set<TestVertex> vertexes = new HashSet<>(vertexes());
tom2e1f0712014-08-29 13:32:00 -070051 vertexes.add(Z);
52
53 Set<TestEdge> edges = new HashSet<>(edges());
Andrey Komarov2398d962016-09-26 15:11:23 +030054 edges.add(new TestEdge(G, Z, new TestDoubleWeight(1.0)));
55 edges.add(new TestEdge(Z, G, new TestDoubleWeight(-2.0)));
tom2e1f0712014-08-29 13:32:00 -070056
tom0633d682014-09-10 12:10:03 -070057 graph = new AdjacencyListsGraph<>(vertexes, edges);
tom2e1f0712014-08-29 13:32:00 -070058
59 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
Andrey Komarov2398d962016-09-26 15:11:23 +030060 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070061 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 Komarov2398d962016-09-26 15:11:23 +030067 assertEquals("incorrect path cost", new TestDoubleWeight(5), p.cost());
tom2e1f0712014-08-29 13:32:00 -070068
Andrey Komarov2398d962016-09-26 15:11:23 +030069 paths = search.search(graph, A, G, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070070 assertEquals("incorrect paths count", 0, paths.size());
71
Andrey Komarov2398d962016-09-26 15:11:23 +030072 paths = search.search(graph, A, null, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070073 printPaths(paths);
74 assertEquals("incorrect paths count", 6, paths.size());
75 }
76
77}