blob: 3d02dc19b907c499d459f8c49ab0917daad4e0b3 [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() {
37 executeDefaultTest(7, 3, 8.0);
tome3489412014-08-29 02:30:38 -070038 }
39
40 @Test
tom144de692014-08-29 11:38:44 -070041 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070042 weight = null;
tom144de692014-08-29 11:38:44 -070043 executeDefaultTest(7, 3, 3.0);
tome3489412014-08-29 02:30:38 -070044 }
45
tom144de692014-08-29 11:38:44 -070046 // Executes the default test
47 protected void executeDefaultTest(int pathCount, int pathLength, double 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 =
52 search.search(graph, A, H, weight, 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());
59 assertEquals("incorrect path cost", pathCost, p.cost(), 0.1);
tome3489412014-08-29 02:30:38 -070060
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080061 paths = search.search(graph, A, null, weight, 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,
70 EdgeWeight<TestVertex, TestEdge> weight,
71 int pathCount, double pathCost) {
72 GraphPathSearch.Result<TestVertex, TestEdge> result =
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080073 search.search(graph, src, dst, weight, 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();
79 assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
80 }
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,
87 EdgeWeight<TestVertex, TestEdge> weight,
88 int pathCount, double pathCost) {
89 GraphPathSearch.Result<TestVertex, TestEdge> result =
90 search.search(graph, src, dst, weight, 1);
91 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();
96 assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
97 }
98 }
99
tome3489412014-08-29 02:30:38 -0700100}