blob: e855b338268d0ff7299dffe6f16e2ed1c713d949 [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() {
Andrey Komarov2398d962016-09-26 15:11:23 +030039 executeDefaultTest(3, 6, new TestDoubleWeight(5.0), new TestDoubleWeight(12.0));
tom41c3fcc2014-08-30 17:57:15 -070040 executeBroadSearch();
41 }
42
43 @Test
44 public void defaultHopCountWeight() {
Andrey Komarov2398d962016-09-26 15:11:23 +030045 weigher = null;
46 executeDefaultTest(3, 6, new ScalarWeight(3.0), new ScalarWeight(6.0));
tom41c3fcc2014-08-30 17:57:15 -070047 executeBroadSearch();
48 }
49
50 protected void executeDefaultTest(int minLength, int maxLength,
Andrey Komarov2398d962016-09-26 15:11:23 +030051 Weight minCost, Weight 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 =
Andrey Komarov2398d962016-09-26 15:11:23 +030056 (DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult)
57 search.search(graph, A, H, weigher, 1);
tom41c3fcc2014-08-30 17:57:15 -070058 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 Komarov2398d962016-09-26 15:11:23 +030070 path.cost().compareTo(minCost) >= 0 &&
71 path.cost().compareTo(maxCost) <= 0);
tom41c3fcc2014-08-30 17:57:15 -070072
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 =
Andrey Komarov2398d962016-09-26 15:11:23 +030083 (DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult)
84 search.search(graph, A, null, weigher, ALL_PATHS);
tom41c3fcc2014-08-30 17:57:15 -070085 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}