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 | 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() { |
| 39 | executeDefaultTest(3, 6, 5.0, 12.0); |
| 40 | executeBroadSearch(); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void defaultHopCountWeight() { |
| 45 | weight = null; |
| 46 | executeDefaultTest(3, 6, 3.0, 6.0); |
| 47 | executeBroadSearch(); |
| 48 | } |
| 49 | |
| 50 | protected void executeDefaultTest(int minLength, int maxLength, |
| 51 | double minCost, double 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 = |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 56 | search.search(graph, A, H, weight, 1); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 57 | Set<Path<TestVertex, TestEdge>> paths = result.paths(); |
| 58 | assertEquals("incorrect path count", 1, paths.size()); |
| 59 | |
| 60 | Path path = paths.iterator().next(); |
| 61 | System.out.println(path); |
| 62 | assertEquals("incorrect src", A, path.src()); |
| 63 | assertEquals("incorrect dst", H, path.dst()); |
| 64 | |
| 65 | int l = path.edges().size(); |
| 66 | assertTrue("incorrect path length " + l, |
| 67 | minLength <= l && l <= maxLength); |
| 68 | assertTrue("incorrect path cost " + path.cost(), |
| 69 | minCost <= path.cost() && path.cost() <= maxCost); |
| 70 | |
| 71 | System.out.println(result.edges()); |
| 72 | printPaths(paths); |
| 73 | } |
| 74 | |
| 75 | public void executeBroadSearch() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 76 | graph = new AdjacencyListsGraph<>(vertexes(), edges()); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 77 | DepthFirstSearch<TestVertex, TestEdge> search = graphSearch(); |
| 78 | |
| 79 | // Perform narrow path search to a specific destination. |
| 80 | DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result = |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 81 | search.search(graph, A, null, weight, ALL_PATHS); |
tom | 41c3fcc | 2014-08-30 17:57:15 -0700 | [diff] [blame] | 82 | assertEquals("incorrect paths count", 7, result.paths().size()); |
| 83 | |
| 84 | int[] types = new int[]{0, 0, 0, 0}; |
| 85 | for (EdgeType t : result.edges().values()) { |
| 86 | types[t.ordinal()] += 1; |
| 87 | } |
| 88 | assertEquals("incorrect tree-edge count", 7, |
| 89 | types[EdgeType.TREE_EDGE.ordinal()]); |
| 90 | assertEquals("incorrect back-edge count", 1, |
| 91 | types[EdgeType.BACK_EDGE.ordinal()]); |
| 92 | assertEquals("incorrect cross-edge & forward-edge count", 4, |
| 93 | types[EdgeType.FORWARD_EDGE.ordinal()] + |
| 94 | types[EdgeType.CROSS_EDGE.ordinal()]); |
| 95 | } |
| 96 | |
| 97 | } |