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 | 41c3fcc | 2014-08-30 17:57:15 -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 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.onlab.graph.DepthFirstSearch.EdgeType; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 25 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Test of the DFS algorithm. |
| 29 | */ |
| 30 | public class DepthFirstSearchTest extends AbstractGraphPathSearchTest { |
| 31 | |
| 32 | @Override |
| 33 | protected DepthFirstSearch<TestVertex, TestEdge> graphSearch() { |
| 34 | return new DepthFirstSearch<>(); |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | public void defaultGraphTest() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 39 | executeDefaultTest(3, 6, new TestDoubleWeight(5.0), new TestDoubleWeight(12.0)); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 40 | executeBroadSearch(); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void defaultHopCountWeight() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 45 | weigher = null; |
| 46 | executeDefaultTest(3, 6, new ScalarWeight(3.0), new ScalarWeight(6.0)); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 47 | executeBroadSearch(); |
| 48 | } |
| 49 | |
| 50 | protected void executeDefaultTest(int minLength, int maxLength, |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 51 | Weight minCost, Weight maxCost) { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 52 | graph = new AdjacencyListsGraph<>(vertexes(), edges()); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 53 | DepthFirstSearch<TestVertex, TestEdge> search = graphSearch(); |
| 54 | |
| 55 | DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 56 | (DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult) |
| 57 | search.search(graph, A, H, weigher, 1); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 58 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 59 | assertEquals("incorrect path count", 1, paths.size()); |
| 60 | |
| 61 | Path path = paths.iterator().next(); |
| 62 | System.out.println(path); |
| 63 | assertEquals("incorrect src", A, path.src()); |
| 64 | assertEquals("incorrect dst", H, path.dst()); |
| 65 | |
| 66 | int l = path.edges().size(); |
| 67 | assertTrue("incorrect path length " + l, |
| 68 | minLength <= l && l <= maxLength); |
| 69 | assertTrue("incorrect path cost " + path.cost(), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 70 | path.cost().compareTo(minCost) >= 0 && |
| 71 | path.cost().compareTo(maxCost) <= 0); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 72 | |
| 73 | System.out.println(result.edges()); |
| 74 | printPaths(paths); |
| 75 | } |
| 76 | |
| 77 | public void executeBroadSearch() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 78 | graph = new AdjacencyListsGraph<>(vertexes(), edges()); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 79 | DepthFirstSearch<TestVertex, TestEdge> search = graphSearch(); |
| 80 | |
| 81 | // Perform narrow path search to a specific destination. |
| 82 | DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 83 | (DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult) |
| 84 | search.search(graph, A, null, weigher, ALL_PATHS); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 85 | assertEquals("incorrect paths count", 7, result.paths().size()); |
| 86 | |
| 87 | int[] types = new int[]{0, 0, 0, 0}; |
| 88 | for (EdgeType t : result.edges().values()) { |
| 89 | types[t.ordinal()] += 1; |
| 90 | } |
| 91 | assertEquals("incorrect tree-edge count", 7, |
| 92 | types[EdgeType.TREE_EDGE.ordinal()]); |
| 93 | assertEquals("incorrect back-edge count", 1, |
| 94 | types[EdgeType.BACK_EDGE.ordinal()]); |
| 95 | assertEquals("incorrect cross-edge & forward-edge count", 4, |
| 96 | types[EdgeType.FORWARD_EDGE.ordinal()] + |
| 97 | types[EdgeType.CROSS_EDGE.ordinal()]); |
| 98 | } |
| 99 | |
| 100 | } |