Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 19 | package org.onlab.graph; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | |
| 27 | /** |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 28 | * Test of the BFS and similar path search algorithms. |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 29 | */ |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 30 | public class BreadthFirstSearchTest extends AbstractGraphPathSearchTest { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 31 | |
| 32 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
| 34 | return new BreadthFirstSearch<>(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 38 | public void defaultGraphTest() { |
| 39 | executeDefaultTest(7, 3, 8.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 43 | public void defaultHopCountWeight() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 44 | weight = null; |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 45 | executeDefaultTest(7, 3, 3.0); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 46 | } |
| 47 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 48 | // Executes the default test |
| 49 | protected void executeDefaultTest(int pathCount, int pathLength, double pathCost) { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 50 | graph = new AdjacencyListsGraph<>(vertexes(), edges()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 51 | |
| 52 | GraphPathSearch<TestVertex, TestEdge> search = graphSearch(); |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 53 | Set<Path<TestVertex, TestEdge>> paths = search.search(graph, A, H, weight).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 54 | 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()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 59 | assertEquals("incorrect path length", pathLength, p.edges().size()); |
| 60 | assertEquals("incorrect path cost", pathCost, p.cost(), 0.1); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 61 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 62 | paths = search.search(graph, A, null, weight).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 63 | printPaths(paths); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 64 | 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 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } |