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