blob: 5d0a032cd6391c51723e13251f31e77c12369f0b [file] [log] [blame]
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -07003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onlab.graph;
17
18import org.junit.Test;
19import java.util.Set;
20
21import static com.google.common.collect.ImmutableSet.of;
22import static org.junit.Assert.assertEquals;
23//import static org.junit.Assert.assertTrue;
24
25
26
27/**
28 * Test of the Suurballe backup path algorithm.
29 */
30public class SuurballeGraphSearchTest extends BreadthFirstSearchTest {
31
32 @Override
33 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
34 return new SuurballeGraphSearch<>();
35 }
36
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070037 @Override
38 public void defaultGraphTest() {
39
40 }
41
42 @Override
43 public void defaultHopCountWeight() {
44
45 }
46
47 @Test
48 public void basicGraphTest() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070049 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
Andrey Komarov2398d962016-09-26 15:11:23 +030050 of(new TestEdge(A, B),
51 new TestEdge(B, C),
52 new TestEdge(A, D),
53 new TestEdge(D, C)));
54 executeSearch(graphSearch(), graph, A, C, null, 1, new ScalarWeight(4.0));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070055 }
56
57 @Test
58 public void multiplePathOnePairGraphTest() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070059 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
Andrey Komarov2398d962016-09-26 15:11:23 +030060 of(new TestEdge(A, B, W1),
61 new TestEdge(B, C, W1),
62 new TestEdge(A, D, W1),
63 new TestEdge(D, C, W1),
64 new TestEdge(B, E, W2),
65 new TestEdge(C, E, W1)));
66 executeSearch(graphSearch(), graph, A, E, weigher, 1, new TestDoubleWeight(6.0));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070067 }
68
69 @Test
70 public void multiplePathsMultiplePairs() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070071 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
Andrey Komarov2398d962016-09-26 15:11:23 +030072 of(new TestEdge(A, B, W1),
73 new TestEdge(B, E, W1),
74 new TestEdge(A, C, W1),
75 new TestEdge(C, E, W1),
76 new TestEdge(A, D, W1),
77 new TestEdge(D, E, W1),
78 new TestEdge(A, E, W2)));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070079 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +030080 graphSearch().search(graph, A, E, weigher, GraphPathSearch.ALL_PATHS);
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070081 Set<Path<TestVertex, TestEdge>> paths = result.paths();
82 System.out.println("\n\n" + paths + "\n\n\ndone\n");
83 assertEquals("incorrect paths count", 3, paths.size());
84 DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next();
85 assertEquals("incorrect disjoint paths per path", 2, dpp.size());
86 }
87
88 @Test
89 public void differingPrimaryAndBackupPathLengths() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070090 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D, E),
Andrey Komarov2398d962016-09-26 15:11:23 +030091 of(new TestEdge(A, B),
92 new TestEdge(B, C),
93 new TestEdge(A, D),
94 new TestEdge(D, C),
95 new TestEdge(B, E),
96 new TestEdge(C, E)));
97 executeSearch(graphSearch(), graph, A, E, weigher, 1, new TestDoubleWeight(5.0));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070098 }
99
100 @Test
101 public void onePath() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700102 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
Andrey Komarov2398d962016-09-26 15:11:23 +0300103 of(new TestEdge(A, B, W1),
104 new TestEdge(B, C, W1),
105 new TestEdge(A, C, W4),
106 new TestEdge(C, D, W1)));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700107 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +0300108 graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS);
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700109 Set<Path<TestVertex, TestEdge>> paths = result.paths();
110 assertEquals("incorrect paths count", 1, paths.size());
111 DisjointPathPair<TestVertex, TestEdge> dpp = (DisjointPathPair<TestVertex, TestEdge>) paths.iterator().next();
112 assertEquals("incorrect disjoint paths count", 1, dpp.size());
113 }
114
115 @Test
116 public void noPath() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700117 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
Andrey Komarov2398d962016-09-26 15:11:23 +0300118 of(new TestEdge(A, B, W1),
119 new TestEdge(B, C, W1),
120 new TestEdge(A, C, W4)));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700121 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +0300122 graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS);
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700123 Set<Path<TestVertex, TestEdge>> paths = result.paths();
124 assertEquals("incorrect paths count", paths.size(), 0);
125 }
126
127 @Test
128 public void disconnected() {
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700129 Graph<TestVertex, TestEdge> graph = new AdjacencyListsGraph<>(of(A, B, C, D),
130 of());
131 GraphPathSearch.Result<TestVertex, TestEdge> result =
Andrey Komarov2398d962016-09-26 15:11:23 +0300132 graphSearch().search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS);
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -0700133 Set<Path<TestVertex, TestEdge>> paths = result.paths();
134 assertEquals("incorrect paths count", 0, paths.size());
135 }
136}