blob: 8881e8413982d8b923b670e5a6f13059132e4f6a [file] [log] [blame]
tom41c3fcc2014-08-30 17:57:15 -07001package org.onlab.graph;
2
3import org.junit.Test;
4
5import java.util.Set;
6
7import static org.junit.Assert.assertEquals;
8import static org.junit.Assert.assertTrue;
9import static org.onlab.graph.DepthFirstSearch.EdgeType;
10
11/**
12 * Test of the DFS algorithm.
13 */
14public class DepthFirstSearchTest extends AbstractGraphPathSearchTest {
15
16 @Override
17 protected DepthFirstSearch<TestVertex, TestEdge> graphSearch() {
18 return new DepthFirstSearch<>();
19 }
20
21 @Test
22 public void defaultGraphTest() {
23 executeDefaultTest(3, 6, 5.0, 12.0);
24 executeBroadSearch();
25 }
26
27 @Test
28 public void defaultHopCountWeight() {
29 weight = null;
30 executeDefaultTest(3, 6, 3.0, 6.0);
31 executeBroadSearch();
32 }
33
34 protected void executeDefaultTest(int minLength, int maxLength,
35 double minCost, double maxCost) {
36 g = new AdjacencyListsGraph<>(vertices(), edges());
37 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
38
39 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
40 search.search(g, A, H, weight);
41 Set<Path<TestVertex, TestEdge>> paths = result.paths();
42 assertEquals("incorrect path count", 1, paths.size());
43
44 Path path = paths.iterator().next();
45 System.out.println(path);
46 assertEquals("incorrect src", A, path.src());
47 assertEquals("incorrect dst", H, path.dst());
48
49 int l = path.edges().size();
50 assertTrue("incorrect path length " + l,
51 minLength <= l && l <= maxLength);
52 assertTrue("incorrect path cost " + path.cost(),
53 minCost <= path.cost() && path.cost() <= maxCost);
54
55 System.out.println(result.edges());
56 printPaths(paths);
57 }
58
59 public void executeBroadSearch() {
60 g = new AdjacencyListsGraph<>(vertices(), edges());
61 DepthFirstSearch<TestVertex, TestEdge> search = graphSearch();
62
63 // Perform narrow path search to a specific destination.
64 DepthFirstSearch<TestVertex, TestEdge>.SpanningTreeResult result =
65 search.search(g, A, null, weight);
66 assertEquals("incorrect paths count", 7, result.paths().size());
67
68 int[] types = new int[]{0, 0, 0, 0};
69 for (EdgeType t : result.edges().values()) {
70 types[t.ordinal()] += 1;
71 }
72 assertEquals("incorrect tree-edge count", 7,
73 types[EdgeType.TREE_EDGE.ordinal()]);
74 assertEquals("incorrect back-edge count", 1,
75 types[EdgeType.BACK_EDGE.ordinal()]);
76 assertEquals("incorrect cross-edge & forward-edge count", 4,
77 types[EdgeType.FORWARD_EDGE.ordinal()] +
78 types[EdgeType.CROSS_EDGE.ordinal()]);
79 }
80
81}