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