blob: a3f47f5d7cfb4111de1b5daf69d72718a482d432 [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 */
tom0633d682014-09-10 12:10:03 -070016package org.onlab.graph;
17
18import org.junit.Test;
19
20import static com.google.common.collect.ImmutableSet.of;
21import static org.junit.Assert.assertEquals;
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080022import static org.onlab.graph.TarjanGraphSearch.SccResult;
tom0633d682014-09-10 12:10:03 -070023
24/**
25 * Tarjan graph search tests.
26 */
27public class TarjanGraphSearchTest extends GraphTest {
28
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080029 private void validate(SccResult<TestVertex, TestEdge> result, int cc) {
tom0633d682014-09-10 12:10:03 -070030 System.out.println("Cluster count: " + result.clusterVertexes().size());
31 System.out.println("Clusters: " + result.clusterVertexes());
32 assertEquals("incorrect cluster count", cc, result.clusterCount());
33 }
34
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080035 private void validate(SccResult<TestVertex, TestEdge> result,
tom0633d682014-09-10 12:10:03 -070036 int i, int vc, int ec) {
37 assertEquals("incorrect cluster count", vc, result.clusterVertexes().get(i).size());
38 assertEquals("incorrect edge count", ec, result.clusterEdges().get(i).size());
39 }
40
41 @Test
42 public void basic() {
43 graph = new AdjacencyListsGraph<>(vertexes(), edges());
44 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080045 SccResult<TestVertex, TestEdge> result = gs.search(graph, null);
tom0633d682014-09-10 12:10:03 -070046 validate(result, 6);
47 }
48
49 @Test
50 public void singleCluster() {
51 graph = new AdjacencyListsGraph<>(vertexes(),
Andrey Komarov2398d962016-09-26 15:11:23 +030052 of(new TestEdge(A, B),
53 new TestEdge(B, C),
54 new TestEdge(C, D),
55 new TestEdge(D, E),
56 new TestEdge(E, F),
57 new TestEdge(F, G),
58 new TestEdge(G, H),
59 new TestEdge(H, A)));
tom0633d682014-09-10 12:10:03 -070060
61 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080062 SccResult<TestVertex, TestEdge> result = gs.search(graph, null);
tom0633d682014-09-10 12:10:03 -070063 validate(result, 1);
64 validate(result, 0, 8, 8);
65 }
66
67 @Test
68 public void twoUnconnectedCluster() {
69 graph = new AdjacencyListsGraph<>(vertexes(),
Andrey Komarov2398d962016-09-26 15:11:23 +030070 of(new TestEdge(A, B),
71 new TestEdge(B, C),
72 new TestEdge(C, D),
73 new TestEdge(D, A),
74 new TestEdge(E, F),
75 new TestEdge(F, G),
76 new TestEdge(G, H),
77 new TestEdge(H, E)));
tom0633d682014-09-10 12:10:03 -070078 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080079 SccResult<TestVertex, TestEdge> result = gs.search(graph, null);
tom0633d682014-09-10 12:10:03 -070080 validate(result, 2);
81 validate(result, 0, 4, 4);
82 validate(result, 1, 4, 4);
83 }
84
85 @Test
86 public void twoWeaklyConnectedClusters() {
87 graph = new AdjacencyListsGraph<>(vertexes(),
Andrey Komarov2398d962016-09-26 15:11:23 +030088 of(new TestEdge(A, B),
89 new TestEdge(B, C),
90 new TestEdge(C, D),
91 new TestEdge(D, A),
92 new TestEdge(E, F),
93 new TestEdge(F, G),
94 new TestEdge(G, H),
95 new TestEdge(H, E),
96 new TestEdge(B, E)));
tom0633d682014-09-10 12:10:03 -070097 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080098 SccResult<TestVertex, TestEdge> result = gs.search(graph, null);
tom0633d682014-09-10 12:10:03 -070099 validate(result, 2);
100 validate(result, 0, 4, 4);
101 validate(result, 1, 4, 4);
102 }
103
104 @Test
105 public void twoClustersConnectedWithIgnoredEdges() {
106 graph = new AdjacencyListsGraph<>(vertexes(),
Andrey Komarov2398d962016-09-26 15:11:23 +0300107 of(new TestEdge(A, B),
108 new TestEdge(B, C),
109 new TestEdge(C, D),
110 new TestEdge(D, A),
111 new TestEdge(E, F),
112 new TestEdge(F, G),
113 new TestEdge(G, H),
114 new TestEdge(H, E),
115 new TestEdge(B, E,
116 weigher.getNonViableWeight()),
117 new TestEdge(E, B,
118 weigher.getNonViableWeight())));
tom0633d682014-09-10 12:10:03 -0700119
120 TarjanGraphSearch<TestVertex, TestEdge> gs = new TarjanGraphSearch<>();
Andrey Komarov2398d962016-09-26 15:11:23 +0300121 SccResult<TestVertex, TestEdge> result = gs.search(graph, weigher);
tom0633d682014-09-10 12:10:03 -0700122 validate(result, 2);
123 validate(result, 0, 4, 4);
124 validate(result, 1, 4, 4);
125 }
126
127}