blob: c19268c7c28203bfd1ca7146ed1832dc79a2b6ef [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 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import org.junit.Test;
19
20import java.util.Set;
21
22import static org.junit.Assert.assertEquals;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080023import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
tome3489412014-08-29 02:30:38 -070024
25/**
tom144de692014-08-29 11:38:44 -070026 * Test of the BFS and similar path search algorithms.
tome3489412014-08-29 02:30:38 -070027 */
tom144de692014-08-29 11:38:44 -070028public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest {
tome3489412014-08-29 02:30:38 -070029
30 @Override
tom144de692014-08-29 11:38:44 -070031 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
32 return new BreadthFirstSearch<>();
tome3489412014-08-29 02:30:38 -070033 }
34
35 @Test
tom144de692014-08-29 11:38:44 -070036 public void defaultGraphTest() {
Andrey Komarov2398d962016-09-26 15:11:23 +030037 executeDefaultTest(7, 3, new TestDoubleWeight(8.0));
tome3489412014-08-29 02:30:38 -070038 }
39
40 @Test
tom144de692014-08-29 11:38:44 -070041 public void defaultHopCountWeight() {
Andrey Komarov2398d962016-09-26 15:11:23 +030042 weigher = null;
43 executeDefaultTest(7, 3, new ScalarWeight(3.0));
tome3489412014-08-29 02:30:38 -070044 }
45
tom144de692014-08-29 11:38:44 -070046 // Executes the default test
Andrey Komarov2398d962016-09-26 15:11:23 +030047 protected void executeDefaultTest(int pathCount, int pathLength, Weight pathCost) {
tom0633d682014-09-10 12:10:03 -070048 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tome3489412014-08-29 02:30:38 -070049
50 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080051 Set<Path<TestVertex, TestEdge>> paths =
Andrey Komarov2398d962016-09-26 15:11:23 +030052 search.search(graph, A, H, weigher, ALL_PATHS).paths();
tome3489412014-08-29 02:30:38 -070053 assertEquals("incorrect paths count", 1, paths.size());
54
55 Path p = paths.iterator().next();
56 assertEquals("incorrect src", A, p.src());
57 assertEquals("incorrect dst", H, p.dst());
tom144de692014-08-29 11:38:44 -070058 assertEquals("incorrect path length", pathLength, p.edges().size());
Andrey Komarov2398d962016-09-26 15:11:23 +030059 assertEquals("incorrect path cost", pathCost, p.cost());
tome3489412014-08-29 02:30:38 -070060
Andrey Komarov2398d962016-09-26 15:11:23 +030061 paths = search.search(graph, A, null, weigher, ALL_PATHS).paths();
tome3489412014-08-29 02:30:38 -070062 printPaths(paths);
tom144de692014-08-29 11:38:44 -070063 assertEquals("incorrect paths count", pathCount, paths.size());
64 }
65
66 // Executes the search and validates its results.
67 protected void executeSearch(GraphPathSearch<TestVertex, TestEdge> search,
68 Graph<TestVertex, TestEdge> graph,
69 TestVertex src, TestVertex dst,
Andrey Komarov2398d962016-09-26 15:11:23 +030070 EdgeWeigher<TestVertex, TestEdge> weigher,
71 int pathCount, Weight pathCost) {
tom144de692014-08-29 11:38:44 -070072 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +030073 search.search(graph, src, dst, weigher, ALL_PATHS);
tom144de692014-08-29 11:38:44 -070074 Set<Path<TestVertex, TestEdge>> paths = result.paths();
75 printPaths(paths);
76 assertEquals("incorrect paths count", pathCount, paths.size());
77 if (pathCount > 0) {
78 Path<TestVertex, TestEdge> path = paths.iterator().next();
Andrey Komarov2398d962016-09-26 15:11:23 +030079 assertEquals("incorrect path cost", pathCost, path.cost());
tom144de692014-08-29 11:38:44 -070080 }
tome3489412014-08-29 02:30:38 -070081 }
82
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080083 // Executes the single-path search and validates its results.
84 protected void executeSinglePathSearch(GraphPathSearch<TestVertex, TestEdge> search,
85 Graph<TestVertex, TestEdge> graph,
86 TestVertex src, TestVertex dst,
Andrey Komarov2398d962016-09-26 15:11:23 +030087 EdgeWeigher<TestVertex, TestEdge> weigher,
88 int pathCount, Weight pathCost) {
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080089 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +030090 search.search(graph, src, dst, weigher, 1);
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080091 Set<Path<TestVertex, TestEdge>> paths = result.paths();
92 printPaths(paths);
93 assertEquals("incorrect paths count", Math.min(pathCount, 1), paths.size());
94 if (pathCount > 0) {
95 Path<TestVertex, TestEdge> path = paths.iterator().next();
Andrey Komarov2398d962016-09-26 15:11:23 +030096 assertEquals("incorrect path cost", pathCost, path.cost());
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080097 }
98 }
99
tome3489412014-08-29 02:30:38 -0700100}