blob: cfa313ccd2005a736ee9c4fa867c9137b98a6463 [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() {
39 executeDefaultTest(7, 5, 5.0);
40 }
41
42 @Test
43 public void defaultHopCountWeight() {
44 weight = null;
45 executeDefaultTest(10, 3, 3.0);
46 }
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());
54 edges.add(new TestEdge(G, Z, 1.0));
55 edges.add(new TestEdge(Z, G, -2.0));
56
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();
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080060 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight, 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());
67 assertEquals("incorrect path cost", 5.0, p.cost(), 0.1);
68
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080069 paths = search.search(graph, A, G, weight, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070070 assertEquals("incorrect paths count", 0, paths.size());
71
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080072 paths = search.search(graph, A, null, weight, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070073 printPaths(paths);
74 assertEquals("incorrect paths count", 6, paths.size());
75 }
76
77}