blob: df7897153f70d234db5338b53705de4e5b80b345 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Sbhat35bbb5a4b2017-06-21 10:40:58 -070023import static com.google.common.collect.ImmutableSet.of;
tom2e1f0712014-08-29 13:32:00 -070024import static org.junit.Assert.assertEquals;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080025import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
tom2e1f0712014-08-29 13:32:00 -070026
27/**
28 * Test of the Bellman-Ford algorithm.
29 */
30public class BellmanFordGraphSearchTest extends BreadthFirstSearchTest {
31
32 @Override
33 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
34 return new BellmanFordGraphSearch<>();
35 }
36
37 @Test
38 @Override
39 public void defaultGraphTest() {
Andrey Komarov2398d962016-09-26 15:11:23 +030040 executeDefaultTest(7, 5, new TestDoubleWeight(5.0));
tom2e1f0712014-08-29 13:32:00 -070041 }
42
43 @Test
44 public void defaultHopCountWeight() {
Andrey Komarov2398d962016-09-26 15:11:23 +030045 weigher = null;
46 executeDefaultTest(10, 3, new ScalarWeight(3.0));
tom2e1f0712014-08-29 13:32:00 -070047 }
48
49 @Test
50 public void searchGraphWithNegativeCycles() {
tom0633d682014-09-10 12:10:03 -070051 Set<TestVertex> vertexes = new HashSet<>(vertexes());
tom2e1f0712014-08-29 13:32:00 -070052 vertexes.add(Z);
53
54 Set<TestEdge> edges = new HashSet<>(edges());
Andrey Komarov2398d962016-09-26 15:11:23 +030055 edges.add(new TestEdge(G, Z, new TestDoubleWeight(1.0)));
56 edges.add(new TestEdge(Z, G, new TestDoubleWeight(-2.0)));
tom2e1f0712014-08-29 13:32:00 -070057
tom0633d682014-09-10 12:10:03 -070058 graph = new AdjacencyListsGraph<>(vertexes, edges);
tom2e1f0712014-08-29 13:32:00 -070059
60 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
Andrey Komarov2398d962016-09-26 15:11:23 +030061 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070062 assertEquals("incorrect paths count", 1, paths.size());
63
64 Path p = paths.iterator().next();
65 assertEquals("incorrect src", A, p.src());
66 assertEquals("incorrect dst", H, p.dst());
67 assertEquals("incorrect path length", 5, p.edges().size());
Andrey Komarov2398d962016-09-26 15:11:23 +030068 assertEquals("incorrect path cost", new TestDoubleWeight(5), p.cost());
tom2e1f0712014-08-29 13:32:00 -070069
Andrey Komarov2398d962016-09-26 15:11:23 +030070 paths = search.search(graph, A, G, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070071 assertEquals("incorrect paths count", 0, paths.size());
72
Andrey Komarov2398d962016-09-26 15:11:23 +030073 paths = search.search(graph, A, null, weigher, ALL_PATHS).paths();
tom2e1f0712014-08-29 13:32:00 -070074 printPaths(paths);
75 assertEquals("incorrect paths count", 6, paths.size());
76 }
Sbhat35bbb5a4b2017-06-21 10:40:58 -070077 @Test
78 public void testNegativeEdge() {
79 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
80 of(new TestEdge(A, B, W3),
81 new TestEdge(A, C, W4),
82 new TestEdge(C, B, NW2),
83 new TestEdge(B, C, W2),
84 new TestEdge(A, D, W5),
85 new TestEdge(D, B, new TestDoubleWeight(-3))));
86 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
87 Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weigher, 1).paths();
88 printPaths(paths);
89 assertEquals("incorrect path count", 1, paths.size());
90 assertEquals("incoreect path cost", new TestDoubleWeight(2), paths.iterator().next().cost());
91
92 executeSearch(graphSearch(), graph, A, B, weigher, 2, W2);
93 }
94
95 @Test
96 public void multipleNegatives() {
97 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
98 of(new TestEdge(A, B, W1),
99 new TestEdge(B, C, W1),
100 new TestEdge(C, D, W1),
101 new TestEdge(D, E, W1),
102 new TestEdge(A, B, new TestDoubleWeight(-4)),
103 new TestEdge(A, C, new TestDoubleWeight(-3)),
104 new TestEdge(A, D, NW1),
105 new TestEdge(D, C, NW1)));
106 executeSearch(graphSearch(), graph, A, E, weigher, 2, NW1);
107 executeSinglePathSearch(graphSearch(), graph, A, E, weigher, 1, NW1);
108 executeSearch(graphSearch(), graph, B, C, weigher, 1, W1);
109 }
110
111 @Test
112 public void manualWeights() {
113 graph = new AdjacencyListsGraph<>(of(A, B, C),
114 of(new TestEdge(A, B, new TestDoubleWeight(-3.3)),
115 new TestEdge(B, C, new TestDoubleWeight(1.3)),
116 new TestEdge(A, C, new TestDoubleWeight(-1.9)),
117 new TestEdge(B, C, new TestDoubleWeight(1.5))));
118 executeSearch(graphSearch(), graph, A, C, weigher, 1, new TestDoubleWeight(-2.0));
119 }
tom2e1f0712014-08-29 13:32:00 -0700120
121}