blob: 3ef2fcfdaeff8ff2000e4299ec917e0092977ea4 [file] [log] [blame]
weibit0d0ef612014-11-03 14:39:25 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.graph;
20
21import static com.google.common.collect.ImmutableSet.of;
22import static org.junit.Assert.*;
23
24import java.io.ByteArrayOutputStream;
25//import java.io.PrintStream;
26import java.util.ArrayList;
27import java.util.Iterator;
28import java.util.List;
29
30import org.junit.After;
31import org.junit.AfterClass;
32import org.junit.Before;
33import org.junit.BeforeClass;
34import org.junit.Test;
35
36public class KshortestPathSearchTest extends BreadthFirstSearchTest {
37
38 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
39
40 @Test
41 public void noPath() {
42 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
43 of(new TestEdge(A, B, 1),
44 new TestEdge(B, A, 1),
45 new TestEdge(C, D, 1),
46 new TestEdge(D, C, 1)));
47 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
48 List<List<TestEdge>> result = gs.search(A, D, weight, 1);
49 List<Path> paths = new ArrayList<>();
50 Iterator<List<TestEdge>> itr = result.iterator();
51 while (itr.hasNext()) {
52 System.out.println(itr.next().toString());
53 }
54 assertEquals("incorrect paths count", 0, result.size());
55 }
56
57 @Test
58 public void test2Path() {
59 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
60 of(new TestEdge(A, B, 1),
61 new TestEdge(B, A, 1),
62 new TestEdge(B, D, 1),
63 new TestEdge(D, B, 1),
64 new TestEdge(A, C, 1),
65 new TestEdge(C, A, 1),
66 new TestEdge(C, D, 1),
67 new TestEdge(D, C, 1)));
68 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
69 List<List<TestEdge>> result = gs.search(A, D, weight, 2);
70 List<Path> paths = new ArrayList<>();
71 Iterator<List<TestEdge>> itr = result.iterator();
72 while (itr.hasNext()) {
73 System.out.println(itr.next().toString());
74 }
75 assertEquals("incorrect paths count", 2, result.size());
76 // assertEquals("printing the paths", outContent.toString());
77 }
78
79 @Test
80 public void test3Path() {
81 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
82 of(new TestEdge(A, B, 1),
83 new TestEdge(B, A, 1),
84 new TestEdge(A, D, 1),
85 new TestEdge(D, A, 1),
86 new TestEdge(B, D, 1),
87 new TestEdge(D, B, 1),
88 new TestEdge(A, C, 1),
89 new TestEdge(C, A, 1),
90 new TestEdge(C, D, 1),
91 new TestEdge(D, C, 1)));
92 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
93 List<List<TestEdge>> result = gs.search(A, D, weight, 3);
94 List<Path> paths = new ArrayList<>();
95 Iterator<List<TestEdge>> itr = result.iterator();
96 while (itr.hasNext()) {
97 System.out.println(itr.next().toString());
98 }
99 assertEquals("incorrect paths count", 3, result.size());
100 // assertEquals("printing the paths", outContent.toString());
101 }
102
103 @Test
104 public void test4Path() {
105 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
106 of(new TestEdge(A, B, 1),
107 new TestEdge(B, A, 1),
108 new TestEdge(A, C, 1),
109 new TestEdge(C, A, 1),
110 new TestEdge(B, D, 1),
111 new TestEdge(D, B, 1),
112 new TestEdge(C, E, 1),
113 new TestEdge(E, C, 1),
114 new TestEdge(D, F, 1),
115 new TestEdge(F, D, 1),
116 new TestEdge(F, E, 1),
117 new TestEdge(E, F, 1),
118 new TestEdge(C, D, 1),
119 new TestEdge(D, C, 1)));
120 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
121 List<List<TestEdge>> result = gs.search(A, F, weight, 4);
122 List<Path> paths = new ArrayList<>();
123 Iterator<List<TestEdge>> itr = result.iterator();
124 while (itr.hasNext()) {
125 System.out.println(itr.next().toString());
126 }
127 assertEquals("incorrect paths count", 4, result.size());
128 // assertEquals("printing the paths", outContent.toString());
129 }
130
131 @Test
132 public void test6Path() {
133 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F),
134 of(new TestEdge(A, B, 1),
135 new TestEdge(B, A, 1),
136 new TestEdge(A, C, 1),
137 new TestEdge(C, A, 1),
138 new TestEdge(B, D, 1),
139 new TestEdge(D, B, 1),
140 new TestEdge(B, C, 1),
141 new TestEdge(C, B, 1),
142 new TestEdge(D, E, 1),
143 new TestEdge(E, D, 1),
144 new TestEdge(C, E, 1),
145 new TestEdge(E, C, 1),
146 new TestEdge(D, F, 1),
147 new TestEdge(F, D, 1),
148 new TestEdge(E, F, 1),
149 new TestEdge(F, E, 1)));
150 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
151 List<List<TestEdge>> result = gs.search(A, F, weight, 6);
152 List<Path> paths = new ArrayList<>();
153 Iterator<List<TestEdge>> itr = result.iterator();
154 while (itr.hasNext()) {
155 System.out.println(itr.next().toString());
156 }
157 assertEquals("incorrect paths count", 6, result.size());
158 // assertEquals("printing the paths", outContent.toString());
159 }
160
161 @Test
162 public void dualEdgePath() {
163 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
164 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
165 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
166 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
167 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
168 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
169 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
170 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
171 KshortestPathSearch<TestVertex, TestEdge> gs = new KshortestPathSearch<TestVertex, TestEdge>(graph);
172 List<List<TestEdge>> result = gs.search(A, G, weight, 6);
173 List<Path> paths = new ArrayList<>();
174 Iterator<List<TestEdge>> itr = result.iterator();
175 while (itr.hasNext()) {
176 System.out.println(itr.next().toString());
177 }
178 assertEquals("incorrect paths count", 6, result.size());
179 // assertEquals("printing the paths", outContent.toString());
180 }
181
182 @BeforeClass
183 public static void setUpBeforeClass() throws Exception {
184 }
185
186 @AfterClass
187 public static void tearDownAfterClass() throws Exception {
188 }
189
190 @Before
191 public void setUp() throws Exception {
192 // System.setOut(new PrintStream(outContent));
193 }
194
195 @After
196 public void tearDown() throws Exception {
197 // System.setOut(null);
198 }
199
200}