blob: 3e8900b880011daa269f902b8280156eb79ce3e4 [file] [log] [blame]
weibit0d0ef612014-11-03 14:39:25 -08001/*
alshabibab984662014-12-04 18:56:18 -08002 * Copyright 2014 Open Networking Laboratory
weibit0d0ef612014-11-03 14:39:25 -08003 *
alshabibab984662014-12-04 18:56:18 -08004 * 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
weibit0d0ef612014-11-03 14:39:25 -08007 *
alshabibab984662014-12-04 18:56:18 -08008 * 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.
weibit0d0ef612014-11-03 14:39:25 -080015 */
16package org.onlab.graph;
17
18import static com.google.common.collect.ImmutableSet.of;
19import static org.junit.Assert.*;
20
21import java.io.ByteArrayOutputStream;
22//import java.io.PrintStream;
23import java.util.ArrayList;
24import java.util.Iterator;
25import java.util.List;
26
27import org.junit.After;
28import org.junit.AfterClass;
29import org.junit.Before;
30import org.junit.BeforeClass;
31import org.junit.Test;
32
33public class KshortestPathSearchTest extends BreadthFirstSearchTest {
34
35 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
36
37 @Test
38 public void noPath() {
39 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
40 of(new TestEdge(A, B, 1),
41 new TestEdge(B, A, 1),
42 new TestEdge(C, D, 1),
43 new TestEdge(D, C, 1)));
44 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
45 List<List<TestEdge>> result = gs.search(A, D, weight, 1);
46 List<Path> paths = new ArrayList<>();
47 Iterator<List<TestEdge>> itr = result.iterator();
48 while (itr.hasNext()) {
49 System.out.println(itr.next().toString());
50 }
51 assertEquals("incorrect paths count", 0, result.size());
52 }
53
54 @Test
55 public void test2Path() {
56 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
57 of(new TestEdge(A, B, 1),
58 new TestEdge(B, A, 1),
59 new TestEdge(B, D, 1),
60 new TestEdge(D, B, 1),
61 new TestEdge(A, C, 1),
62 new TestEdge(C, A, 1),
63 new TestEdge(C, D, 1),
64 new TestEdge(D, C, 1)));
65 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
66 List<List<TestEdge>> result = gs.search(A, D, weight, 2);
67 List<Path> paths = new ArrayList<>();
68 Iterator<List<TestEdge>> itr = result.iterator();
69 while (itr.hasNext()) {
70 System.out.println(itr.next().toString());
71 }
72 assertEquals("incorrect paths count", 2, result.size());
73 // assertEquals("printing the paths", outContent.toString());
74 }
75
76 @Test
77 public void test3Path() {
78 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
79 of(new TestEdge(A, B, 1),
80 new TestEdge(B, A, 1),
81 new TestEdge(A, D, 1),
82 new TestEdge(D, A, 1),
83 new TestEdge(B, D, 1),
84 new TestEdge(D, B, 1),
85 new TestEdge(A, C, 1),
86 new TestEdge(C, A, 1),
87 new TestEdge(C, D, 1),
88 new TestEdge(D, C, 1)));
89 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
90 List<List<TestEdge>> result = gs.search(A, D, weight, 3);
91 List<Path> paths = new ArrayList<>();
92 Iterator<List<TestEdge>> itr = result.iterator();
93 while (itr.hasNext()) {
94 System.out.println(itr.next().toString());
95 }
96 assertEquals("incorrect paths count", 3, result.size());
97 // assertEquals("printing the paths", outContent.toString());
98 }
99
100 @Test
101 public void test4Path() {
102 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
103 of(new TestEdge(A, B, 1),
104 new TestEdge(B, A, 1),
105 new TestEdge(A, C, 1),
106 new TestEdge(C, A, 1),
107 new TestEdge(B, D, 1),
108 new TestEdge(D, B, 1),
109 new TestEdge(C, E, 1),
110 new TestEdge(E, C, 1),
111 new TestEdge(D, F, 1),
112 new TestEdge(F, D, 1),
113 new TestEdge(F, E, 1),
114 new TestEdge(E, F, 1),
115 new TestEdge(C, D, 1),
116 new TestEdge(D, C, 1)));
117 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
118 List<List<TestEdge>> result = gs.search(A, F, weight, 4);
119 List<Path> paths = new ArrayList<>();
120 Iterator<List<TestEdge>> itr = result.iterator();
121 while (itr.hasNext()) {
122 System.out.println(itr.next().toString());
123 }
124 assertEquals("incorrect paths count", 4, result.size());
125 // assertEquals("printing the paths", outContent.toString());
126 }
127
128 @Test
129 public void test6Path() {
130 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
131 of(new TestEdge(A, B, 1),
132 new TestEdge(B, A, 1),
133 new TestEdge(A, C, 1),
134 new TestEdge(C, A, 1),
135 new TestEdge(B, D, 1),
136 new TestEdge(D, B, 1),
137 new TestEdge(B, C, 1),
138 new TestEdge(C, B, 1),
139 new TestEdge(D, E, 1),
140 new TestEdge(E, D, 1),
141 new TestEdge(C, E, 1),
142 new TestEdge(E, C, 1),
143 new TestEdge(D, F, 1),
144 new TestEdge(F, D, 1),
145 new TestEdge(E, F, 1),
146 new TestEdge(F, E, 1)));
147 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
148 List<List<TestEdge>> result = gs.search(A, F, weight, 6);
149 List<Path> paths = new ArrayList<>();
150 Iterator<List<TestEdge>> itr = result.iterator();
151 while (itr.hasNext()) {
152 System.out.println(itr.next().toString());
153 }
154 assertEquals("incorrect paths count", 6, result.size());
155 // assertEquals("printing the paths", outContent.toString());
156 }
157
158 @Test
159 public void dualEdgePath() {
160 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
161 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
162 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
163 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
164 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
165 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
166 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
167 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
168 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
169 List<List<TestEdge>> result = gs.search(A, G, weight, 6);
170 List<Path> paths = new ArrayList<>();
171 Iterator<List<TestEdge>> itr = result.iterator();
172 while (itr.hasNext()) {
173 System.out.println(itr.next().toString());
174 }
175 assertEquals("incorrect paths count", 6, result.size());
176 // assertEquals("printing the paths", outContent.toString());
177 }
178
179 @BeforeClass
180 public static void setUpBeforeClass() throws Exception {
181 }
182
183 @AfterClass
184 public static void tearDownAfterClass() throws Exception {
185 }
186
187 @Before
188 public void setUp() throws Exception {
189 // System.setOut(new PrintStream(outContent));
190 }
191
192 @After
193 public void tearDown() throws Exception {
194 // System.setOut(null);
195 }
196
197}