blob: d225377ac7104c569c7c2dda1b229af9ff81211c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
25
26/**
27 * Test of the DFS algorithm.
28 */
29public class DepthFirstSearchTest extends AbstractGraphPathSearchTest {
30
31 @Override
32 protected DepthFirstSearch<TestVertex, TestEdge> graphSearch() {
33 return new DepthFirstSearch<>();
34 }
35
36 @Test
37 public void defaultGraphTest() {
38 executeDefaultTest(3, 6, 5.0, 12.0);
39 executeBroadSearch();
40 }
41
42 @Test
43 public void defaultHopCountWeight() {
44 weight = null;
45 executeDefaultTest(3, 6, 3.0, 6.0);
46 executeBroadSearch();
47 }
48
49 protected void executeDefaultTest(int minLength, int maxLength,
50 double minCost, double maxCost) {
tom0633d682014-09-10 12:10:03 -070051 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070052 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
53
54 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
tom0633d682014-09-10 12:10:03 -070055 search.search(graph, A, H, weight);
tom41c3fcc2014-08-30 17:57:15 -070056 Set<Path<TestVertex, TestEdge>> paths = result.paths();
57 assertEquals("incorrect path count", 1, paths.size());
58
59 Path path = paths.iterator().next();
60 System.out.println(path);
61 assertEquals("incorrect src", A, path.src());
62 assertEquals("incorrect dst", H, path.dst());
63
64 int l = path.edges().size();
65 assertTrue("incorrect path length " + l,
66 minLength <= l && l <= maxLength);
67 assertTrue("incorrect path cost " + path.cost(),
68 minCost <= path.cost() && path.cost() <= maxCost);
69
70 System.out.println(result.edges());
71 printPaths(paths);
72 }
73
74 public void executeBroadSearch() {
tom0633d682014-09-10 12:10:03 -070075 graph = new AdjacencyListsGraph<>(vertexes(), edges());
tom41c3fcc2014-08-30 17:57:15 -070076 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
77
78 // Perform narrow path search to a specific destination.
79 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
tom0633d682014-09-10 12:10:03 -070080 search.search(graph, A, null, weight);
tom41c3fcc2014-08-30 17:57:15 -070081 assertEquals("incorrect paths count", 7, result.paths().size());
82
83 int[] types = new int[]{0, 0, 0, 0};
84 for (EdgeType t : result.edges().values()) {
85 types[t.ordinal()] += 1;
86 }
87 assertEquals("incorrect tree-edge count", 7,
88 types[EdgeType.TREE_EDGE.ordinal()]);
89 assertEquals("incorrect back-edge count", 1,
90 types[EdgeType.BACK_EDGE.ordinal()]);
91 assertEquals("incorrect cross-edge & forward-edge count", 4,
92 types[EdgeType.FORWARD_EDGE.ordinal()] +
93 types[EdgeType.CROSS_EDGE.ordinal()]);
94 }
95
96}