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