blob: a0a175f338bd2e0a70f6934ad3be5c600c229fb3 [file] [log] [blame]
Yuta HIGUCHIe3ebe692017-04-17 10:00:42 -07001/*
2 * Copyright 2017-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 */
16package org.onlab.graph;
17
18import static com.google.common.collect.ImmutableSet.of;
19import static org.hamcrest.Matchers.containsInAnyOrder;
20import static org.junit.Assert.*;
21
22import java.util.ArrayList;
23import java.util.List;
24import java.util.stream.Collectors;
25import java.util.stream.Stream;
26
27import org.junit.Before;
28import org.junit.Test;
29
30import com.google.common.collect.ImmutableList;
31
32public class LazyKShortestPathsSearchTest extends GraphTest {
33
34 LazyKShortestPathsSearch<TestVertex, TestEdge> sut;
35
36 @Before
37 public void setUp() {
38 sut = new LazyKShortestPathsSearch<>();
39 }
40
41 @Test
42 public void noPath() {
43 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
44 of(new TestEdge(A, B),
45 new TestEdge(B, A),
46 new TestEdge(C, D),
47 new TestEdge(D, C)));
48 Stream<Path<TestVertex, TestEdge>> result = sut.lazyPathSearch(graph, A, D, weigher);
49 assertEquals("There should not be any paths.", 0, result.count());
50 }
51
52 @Test
53 public void fourPath() {
54 graph = new AdjacencyListsGraph<>(vertexes(), edges());
55 Stream<Path<TestVertex, TestEdge>> result = sut.lazyPathSearch(graph, A, E, weigher);
56 List<Path<TestVertex, TestEdge>> rList = result.limit(42).collect(Collectors.toList());
57
58 assertEquals("There are an unexpected number of paths.", 4, rList.size());
59
60 // The shortest path
61 List<TestEdge> expectedEdges = new ArrayList<>();
62 expectedEdges.add(new TestEdge(A, B, W1));
63 expectedEdges.add(new TestEdge(B, C, W1));
64 expectedEdges.add(new TestEdge(C, E, W1));
65
66 assertEquals("The first path from A to E was incorrect.",
67 expectedEdges, rList.get(0).edges());
68 assertEquals(W3, rList.get(0).cost());
69
70
71 // There are two paths of equal cost as next shortest path
72 expectedEdges.clear();
73 expectedEdges.add(new TestEdge(A, C, W3));
74 expectedEdges.add(new TestEdge(C, E, W1));
75
76 List<TestEdge> alternateEdges = new ArrayList<>();
77 alternateEdges.add(new TestEdge(A, B, W1));
78 alternateEdges.add(new TestEdge(B, D, W2));
79 alternateEdges.add(new TestEdge(D, E, W1));
80
81 assertThat(ImmutableList.of(rList.get(1).edges(), rList.get(2).edges()),
82 containsInAnyOrder(expectedEdges, alternateEdges));
83 assertEquals(W4, rList.get(1).cost());
84 assertEquals(W4, rList.get(2).cost());
85
86
87 // last shortest path
88 expectedEdges.clear();
89 expectedEdges.add(new TestEdge(A, B, W1));
90 expectedEdges.add(new TestEdge(B, E, W4));
91
92 assertEquals("The fourth path rom A to E was incorrect",
93 expectedEdges, rList.get(3).edges());
94 assertEquals(W5, rList.get(3).cost());
95 }
96
97}