blob: 96a77e6c80c0a9477be9c4ec90e05faa6a19bbeb [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 */
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;
23
24/**
tom144de692014-08-29 11:38:44 -070025 * Test of the BFS and similar path search algorithms.
tome3489412014-08-29 02:30:38 -070026 */
tom144de692014-08-29 11:38:44 -070027public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest {
tome3489412014-08-29 02:30:38 -070028
29 @Override
tom144de692014-08-29 11:38:44 -070030 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
31 return new BreadthFirstSearch<>();
tome3489412014-08-29 02:30:38 -070032 }
33
34 @Test
tom144de692014-08-29 11:38:44 -070035 public void defaultGraphTest() {
36 executeDefaultTest(7, 3, 8.0);
tome3489412014-08-29 02:30:38 -070037 }
38
39 @Test
tom144de692014-08-29 11:38:44 -070040 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070041 weight = null;
tom144de692014-08-29 11:38:44 -070042 executeDefaultTest(7, 3, 3.0);
tome3489412014-08-29 02:30:38 -070043 }
44
tom144de692014-08-29 11:38:44 -070045 // Executes the default test
46 protected void executeDefaultTest(int pathCount, int pathLength, double pathCost) {
tom0633d682014-09-10 12:10:03 -070047 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tome3489412014-08-29 02:30:38 -070048
49 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
tom0633d682014-09-10 12:10:03 -070050 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths();
tome3489412014-08-29 02:30:38 -070051 assertEquals("incorrect paths count", 1, paths.size());
52
53 Path p = paths.iterator().next();
54 assertEquals("incorrect src", A, p.src());
55 assertEquals("incorrect dst", H, p.dst());
tom144de692014-08-29 11:38:44 -070056 assertEquals("incorrect path length", pathLength, p.edges().size());
57 assertEquals("incorrect path cost", pathCost, p.cost(), 0.1);
tome3489412014-08-29 02:30:38 -070058
tom0633d682014-09-10 12:10:03 -070059 paths = search.search(graph, A, null, weight).paths();
tome3489412014-08-29 02:30:38 -070060 printPaths(paths);
tom144de692014-08-29 11:38:44 -070061 assertEquals("incorrect paths count", pathCount, paths.size());
62 }
63
64 // Executes the search and validates its results.
65 protected void executeSearch(GraphPathSearch<TestVertex, TestEdge> search,
66 Graph<TestVertex, TestEdge> graph,
67 TestVertex src, TestVertex dst,
68 EdgeWeight<TestVertex, TestEdge> weight,
69 int pathCount, double pathCost) {
70 GraphPathSearch.Result<TestVertex, TestEdge> result =
71 search.search(graph, src, dst, weight);
72 Set<Path<TestVertex, TestEdge>> paths = result.paths();
73 printPaths(paths);
74 assertEquals("incorrect paths count", pathCount, paths.size());
75 if (pathCount > 0) {
76 Path<TestVertex, TestEdge> path = paths.iterator().next();
77 assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
78 }
tome3489412014-08-29 02:30:38 -070079 }
80
81}