blob: 806d0956f85777d29334302d6c6e089da33b45e5 [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 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
21import org.junit.Test;
22
23import java.util.Set;
24
25import static org.junit.Assert.assertEquals;
26
27/**
tom144de692014-08-29 11:38:44 -070028 * Test of the BFS and similar path search algorithms.
tome3489412014-08-29 02:30:38 -070029 */
tom144de692014-08-29 11:38:44 -070030public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest {
tome3489412014-08-29 02:30:38 -070031
32 @Override
tom144de692014-08-29 11:38:44 -070033 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
34 return new BreadthFirstSearch<>();
tome3489412014-08-29 02:30:38 -070035 }
36
37 @Test
tom144de692014-08-29 11:38:44 -070038 public void defaultGraphTest() {
39 executeDefaultTest(7, 3, 8.0);
tome3489412014-08-29 02:30:38 -070040 }
41
42 @Test
tom144de692014-08-29 11:38:44 -070043 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070044 weight = null;
tom144de692014-08-29 11:38:44 -070045 executeDefaultTest(7, 3, 3.0);
tome3489412014-08-29 02:30:38 -070046 }
47
tom144de692014-08-29 11:38:44 -070048 // Executes the default test
49 protected void executeDefaultTest(int pathCount, int pathLength, double pathCost) {
tom0633d682014-09-10 12:10:03 -070050 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tome3489412014-08-29 02:30:38 -070051
52 GraphPathSearch<TestVertex, TestEdge> search = graphSearch();
tom0633d682014-09-10 12:10:03 -070053 Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths();
tome3489412014-08-29 02:30:38 -070054 assertEquals("incorrect paths count", 1, paths.size());
55
56 Path p = paths.iterator().next();
57 assertEquals("incorrect src", A, p.src());
58 assertEquals("incorrect dst", H, p.dst());
tom144de692014-08-29 11:38:44 -070059 assertEquals("incorrect path length", pathLength, p.edges().size());
60 assertEquals("incorrect path cost", pathCost, p.cost(), 0.1);
tome3489412014-08-29 02:30:38 -070061
tom0633d682014-09-10 12:10:03 -070062 paths = search.search(graph, A, null, weight).paths();
tome3489412014-08-29 02:30:38 -070063 printPaths(paths);
tom144de692014-08-29 11:38:44 -070064 assertEquals("incorrect paths count", pathCount, paths.size());
65 }
66
67 // Executes the search and validates its results.
68 protected void executeSearch(GraphPathSearch<TestVertex, TestEdge> search,
69 Graph<TestVertex, TestEdge> graph,
70 TestVertex src, TestVertex dst,
71 EdgeWeight<TestVertex, TestEdge> weight,
72 int pathCount, double pathCost) {
73 GraphPathSearch.Result<TestVertex, TestEdge> result =
74 search.search(graph, src, dst, weight);
75 Set<Path<TestVertex, TestEdge>> paths = result.paths();
76 printPaths(paths);
77 assertEquals("incorrect paths count", pathCount, paths.size());
78 if (pathCount > 0) {
79 Path<TestVertex, TestEdge> path = paths.iterator().next();
80 assertEquals("incorrect path cost", pathCost, path.cost(), 0.1);
81 }
tome3489412014-08-29 02:30:38 -070082 }
83
84}