blob: 4e9f5e616cdcd785fbe7343b7fc2d15444d1d201 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
24
25/**
26 * Test of the Bellman-Ford algorithm.
27 */
28public 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() {
tom0633d682014-09-10 12:10:03 -070049 Set<TestVertex> vertexes = new HashSet<>(vertexes());
tom2e1f0712014-08-29 13:32:00 -070050 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
tom0633d682014-09-10 12:10:03 -070056 graph = new AdjacencyListsGraph<>(vertexes, edges);
tom2e1f0712014-08-29 13:32:00 -070057
58 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
tom0633d682014-09-10 12:10:03 -070059 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070060 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
tom0633d682014-09-10 12:10:03 -070068 paths = search.search(graph, A, G, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070069 assertEquals("incorrect paths count", 0, paths.size());
70
tom0633d682014-09-10 12:10:03 -070071 paths = search.search(graph, A, null, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070072 printPaths(paths);
73 assertEquals("incorrect paths count", 6, paths.size());
74 }
75
76}