blob: ab3624c03af403078f85189bc76703d835ae05b4 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 */
tom41c3fcc2014-08-30 17:57:15 -070019package org.onlab.graph;
20
21import org.junit.Test;
22
23import java.util.Set;
24
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertTrue;
27import static org.onlab.graph.DepthFirstSearch.EdgeType;
28
29/**
30 * Test of the DFS algorithm.
31 */
32public 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) {
tom0633d682014-09-10 12:10:03 -070054 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070055 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
56
57 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
tom0633d682014-09-10 12:10:03 -070058 search.search(graph, A, H, weight);
tom41c3fcc2014-08-30 17:57:15 -070059 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() {
tom0633d682014-09-10 12:10:03 -070078 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070079 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
80
81 // Perform narrow path search to a specific destination.
82 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
tom0633d682014-09-10 12:10:03 -070083 search.search(graph, A, null, weight);
tom41c3fcc2014-08-30 17:57:15 -070084 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}