blob: adf62b9fcb9d496cda70ff856de0698c254f7340 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2015-present Open Networking Laboratory
3 *
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 */
Aaron Kruglikov07d80382015-12-07 16:54:37 -080016package org.onlab.graph;
17
18import com.google.common.collect.Lists;
19import org.junit.Before;
20import org.junit.Test;
21
22import java.util.Iterator;
23import java.util.List;
24import java.util.Set;
25
26import static com.google.common.collect.ImmutableSet.of;
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertTrue;
29
30/**
31 * Class for test KshortestPathsSearch.
32 */
33public class KShortestPathsSearchTest<V extends Vertex, E extends Edge<V>> extends GraphTest {
34 private KShortestPathsSearch<TestVertex, TestEdge> kShortestPathsSearch = new KShortestPathsSearch<>();
35 private GraphPathSearch.Result<TestVertex, TestEdge> result;
36
37 @Before
38 public void setUp() {
39 graph = new AdjacencyListsGraph<>(vertexes(), edges());
40 }
41 @Test
42 public void noPath() {
43 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
44 of(new TestEdge(A, B, 1),
45 new TestEdge(B, A, 1),
46 new TestEdge(C, D, 1),
47 new TestEdge(D, C, 1)));
48 KShortestPathsSearch<TestVertex, TestEdge> kShortestPathsSearch = new KShortestPathsSearch<>();
49 GraphPathSearch.Result<TestVertex, TestEdge> result = kShortestPathsSearch.search(graph, A, D, weight, 1);
50 Set<Path<TestVertex, TestEdge>> resultPathSet = result.paths();
51 assertTrue("There should not be any paths.", resultPathSet.isEmpty());
52 }
53
54 @Test
55 public void testSinglePath() {
56 //Tests that there is only a single path possible between A and B
57 graph = new AdjacencyListsGraph<>(vertexes(), edges());
58 this.result = kShortestPathsSearch.search(graph, A, B, weight, 2);
59 Iterator<Path<TestVertex, TestEdge>> itr = result.paths().iterator();
60 assertEquals("incorrect paths count", 1, result.paths().size());
61 List<TestEdge> correctEdgeList = Lists.newArrayList();
62 correctEdgeList.add(new TestEdge(A, B, 1));
63 assertTrue("That wrong path was returned.",
64 edgeListsAreEqual(correctEdgeList, result.paths().iterator().next().edges()));
65 }
66
67 @Test
68 public void testTwoPath() {
69 //Tests that there are only two paths between A and C and that they are returned in the correct order
70 result = kShortestPathsSearch.search(graph, A, C, weight, 3);
71 assertTrue("There are an unexpected number of paths.", result.paths().size() == 2);
72 Iterator<Path<TestVertex, TestEdge>> edgeListIterator = result.paths().iterator();
73 List<TestEdge> correctEdgeList = Lists.newArrayList();
74 correctEdgeList.add(new TestEdge(A, B, 1));
75 correctEdgeList.add(new TestEdge(B, C, 1));
76 assertTrue("The first path from A to C was incorrect.",
77 edgeListsAreEqual(edgeListIterator.next().edges(), correctEdgeList));
78 correctEdgeList.clear();
79 correctEdgeList.add(new TestEdge(A, C, 3));
80 assertTrue("The second path from A to C was incorrect.",
81 edgeListsAreEqual(edgeListIterator.next().edges(), correctEdgeList));
82 }
83
84 @Test
85 public void testFourPath() {
86 //Tests that there are only four paths between A and E and that they are returned in the correct order
87 //Also tests the special case where some correct solutions are equal
88 result = kShortestPathsSearch.search(graph, A, E, weight, 5);
89 assertTrue("There are an unexpected number of paths.", result.paths().size() == 4);
90 Iterator<Path<TestVertex, TestEdge>> edgeListIterator = result.paths().iterator();
91 List<TestEdge> correctEdgeList = Lists.newArrayList();
92 correctEdgeList.add(new TestEdge(A, B, 1));
93 correctEdgeList.add(new TestEdge(B, C, 1));
94 correctEdgeList.add(new TestEdge(C, E, 1));
95 assertTrue("The first path from A to E was incorrect.",
96 edgeListsAreEqual(edgeListIterator.next().edges(), correctEdgeList));
97 correctEdgeList.clear();
98 //There are two paths of equal length that should hold positions two and three
99 List<TestEdge> alternateCorrectEdgeList = Lists.newArrayList();
100 correctEdgeList.add(new TestEdge(A, C, 3));
101 correctEdgeList.add(new TestEdge(C, E, 1));
102 alternateCorrectEdgeList.add(new TestEdge(A, B, 1));
103 alternateCorrectEdgeList.add(new TestEdge(B, D, 2));
104 alternateCorrectEdgeList.add(new TestEdge(D, E, 1));
105 List<TestEdge> candidateOne = edgeListIterator.next().edges();
106 List<TestEdge> candidateTwo = edgeListIterator.next().edges();
107 if (candidateOne.size() == 2) {
108 assertTrue("The second path from A to E was incorrect.",
109 edgeListsAreEqual(candidateOne, correctEdgeList));
110 assertTrue("The third path from A to E was incorrect.",
111 edgeListsAreEqual(candidateTwo, alternateCorrectEdgeList));
112 } else {
113 assertTrue("The second path from A to E was incorrect.",
114 edgeListsAreEqual(candidateOne, alternateCorrectEdgeList));
115 assertTrue("The third path from A to E was incorrect.",
116 edgeListsAreEqual(candidateTwo, correctEdgeList));
117 }
118 correctEdgeList.clear();
119 correctEdgeList.add(new TestEdge(A, B, 1));
120 correctEdgeList.add(new TestEdge(B, E, 4));
121 assertTrue("The fourth path rom A to E was incorrect",
122 edgeListsAreEqual(edgeListIterator.next().edges(), correctEdgeList));
123
124 }
125
126 @Test
127 public void testPathsFromSink() {
128 //H is a sink in this topology, insure there are no paths from it to any other location
129 for (TestVertex vertex : vertexes()) {
130 assertTrue("There should be no paths from vertex H to any other node.",
131 kShortestPathsSearch.search(graph, H, vertex, weight, 1).paths().size() == 0);
132 }
133 }
134
135 @Test
136 public void testLimitPathSetSize() {
137 //Checks to make sure that no more than K paths are returned
138 result = kShortestPathsSearch.search(graph, A, E, weight, 3);
139 assertTrue("There are an unexpected number of paths.", result.paths().size() == 3);
140 result = kShortestPathsSearch.search(graph, A, G, weight, 1);
141 assertTrue("There are an unexpected number of paths.", result.paths().size() == 1);
142 }
143
144 private boolean edgeListsAreEqual(List<TestEdge> edgeListOne, List<TestEdge> edgeListTwo) {
145 if (edgeListOne.size() != edgeListTwo.size()) {
146 return false;
147 }
148 TestEdge edgeOne;
149 TestEdge edgeTwo;
150 for (int i = 0; i < edgeListOne.size(); i++) {
151 edgeOne = edgeListOne.get(i);
152 edgeTwo = edgeListTwo.get(i);
153 if (!edgeOne.equals(edgeTwo)) {
154 return false;
155 }
156 }
157 return true;
158 }
159}