blob: e164f7e7281a6fa74d7fa98438421f8dbc1210ec [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tom41c3fcc2014-08-30 17:57:15 -070016package org.onlab.graph;
17
18import org.junit.Test;
19
20import java.util.Set;
21
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertTrue;
24import static org.onlab.graph.DepthFirstSearch.EdgeType;
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080025import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
tom41c3fcc2014-08-30 17:57:15 -070026
27/**
28 * Test of the DFS algorithm.
29 */
30public 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) {
tom0633d682014-09-10 12:10:03 -070052 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070053 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
54
55 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080056 search.search(graph, A, H, weight, 1);
tom41c3fcc2014-08-30 17:57:15 -070057 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() {
tom0633d682014-09-10 12:10:03 -070076 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070077 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
78
79 // Perform narrow path search to a specific destination.
80 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080081 search.search(graph, A, null, weight, ALL_PATHS);
tom41c3fcc2014-08-30 17:57:15 -070082 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}