blob: 53efc52c8f6578962850ac7e555561727ac50a58 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom2e1f0712014-08-29 13:32:00 -070019package org.onlab.graph;
20
21import org.junit.Test;
22
23import java.util.HashSet;
24import java.util.Set;
25
26import static org.junit.Assert.assertEquals;
27
28/**
29 * Test of the Bellman-Ford algorithm.
30 */
31public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest {
32
33 @Override
34 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
35 return new BellmanFordGraphSearch<>();
36 }
37
38 @Test
39 @Override
40 public void defaultGraphTest() {
41 executeDefaultTest(7, 5, 5.0);
42 }
43
44 @Test
45 public void defaultHopCountWeight() {
46 weight = null;
47 executeDefaultTest(10, 3, 3.0);
48 }
49
50 @Test
51 public void searchGraphWithNegativeCycles() {
tom0633d682014-09-10 12:10:03 -070052 Set<TestVertex> vertexes = new HashSet<>(vertexes());
tom2e1f0712014-08-29 13:32:00 -070053 vertexes.add(Z);
54
55 Set<TestEdge> edges = new HashSet<>(edges());
56 edges.add(new TestEdge(G, Z, 1.0));
57 edges.add(new TestEdge(Z, G, -2.0));
58
tom0633d682014-09-10 12:10:03 -070059 graph = new AdjacencyListsGraph<>(vertexes, edges);
tom2e1f0712014-08-29 13:32:00 -070060
61 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
tom0633d682014-09-10 12:10:03 -070062 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070063 assertEquals("incorrect paths count", 1, paths.size());
64
65 Path p = paths.iterator().next();
66 assertEquals("incorrect src", A, p.src());
67 assertEquals("incorrect dst", H, p.dst());
68 assertEquals("incorrect path length", 5, p.edges().size());
69 assertEquals("incorrect path cost", 5.0, p.cost(), 0.1);
70
tom0633d682014-09-10 12:10:03 -070071 paths = search.search(graph, A, G, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070072 assertEquals("incorrect paths count", 0, paths.size());
73
tom0633d682014-09-10 12:10:03 -070074 paths = search.search(graph, A, null, weight).paths();
tom2e1f0712014-08-29 13:32:00 -070075 printPaths(paths);
76 assertEquals("incorrect paths count", 6, paths.size());
77 }
78
79}