blob: 90c1122fb4fb0aaca0233738fb17a2c47070806e [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
tome3489412014-08-29 02:30:38 -070021import org.junit.Test;
22
23import java.util.Set;
24
tom144de692014-08-29 11:38:44 -070025import static com.google.common.collect.ImmutableSet.of;
tome3489412014-08-29 02:30:38 -070026import static org.junit.Assert.assertEquals;
27
28/**
29 * Test of the Dijkstra algorithm.
30 */
31public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
32
33 @Override
tom144de692014-08-29 11:38:44 -070034 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
tome3489412014-08-29 02:30:38 -070035 return new DijkstraGraphSearch<>();
36 }
37
38 @Test
39 @Override
tom144de692014-08-29 11:38:44 -070040 public void defaultGraphTest() {
41 executeDefaultTest(7, 5, 5.0);
tome3489412014-08-29 02:30:38 -070042 }
43
44 @Test
tom144de692014-08-29 11:38:44 -070045 @Override
46 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070047 weight = null;
tom144de692014-08-29 11:38:44 -070048 executeDefaultTest(10, 3, 3.0);
tome3489412014-08-29 02:30:38 -070049 }
50
51 @Test
52 public void noPath() {
tom0633d682014-09-10 12:10:03 -070053 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
54 of(new TestEdge(A, B, 1),
55 new TestEdge(B, A, 1),
56 new TestEdge(C, D, 1),
57 new TestEdge(D, C, 1)));
tome3489412014-08-29 02:30:38 -070058 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
tom0633d682014-09-10 12:10:03 -070059 Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight).paths();
tome3489412014-08-29 02:30:38 -070060 printPaths(paths);
61 assertEquals("incorrect paths count", 1, paths.size());
62 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
63
tom0633d682014-09-10 12:10:03 -070064 paths = gs.search(graph, A, D, weight).paths();
tome3489412014-08-29 02:30:38 -070065 printPaths(paths);
66 assertEquals("incorrect paths count", 0, paths.size());
67
tom0633d682014-09-10 12:10:03 -070068 paths = gs.search(graph, A, null, weight).paths();
tome3489412014-08-29 02:30:38 -070069 printPaths(paths);
70 assertEquals("incorrect paths count", 1, paths.size());
71 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
72 }
73
74 @Test
tom144de692014-08-29 11:38:44 -070075 public void simpleMultiplePath() {
tom0633d682014-09-10 12:10:03 -070076 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
77 of(new TestEdge(A, B, 1),
78 new TestEdge(A, C, 1),
79 new TestEdge(B, D, 1),
80 new TestEdge(C, D, 1)));
81 executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0);
tome3489412014-08-29 02:30:38 -070082 }
83
84 @Test
tom144de692014-08-29 11:38:44 -070085 public void denseMultiplePath() {
tom0633d682014-09-10 12:10:03 -070086 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
87 of(new TestEdge(A, B, 1),
88 new TestEdge(A, C, 1),
89 new TestEdge(B, D, 1),
90 new TestEdge(C, D, 1),
91 new TestEdge(D, E, 1),
92 new TestEdge(D, F, 1),
93 new TestEdge(E, G, 1),
94 new TestEdge(F, G, 1),
95 new TestEdge(A, G, 4)));
96 executeSearch(graphSearch(), graph, A, G, weight, 5, 4.0);
tome3489412014-08-29 02:30:38 -070097 }
98
99 @Test
tom144de692014-08-29 11:38:44 -0700100 public void dualEdgeMultiplePath() {
tom0633d682014-09-10 12:10:03 -0700101 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
102 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
103 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
104 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
105 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
106 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
107 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
108 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
109 executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0);
tome3489412014-08-29 02:30:38 -0700110 }
111
Thomas Vachuska4d690872014-10-27 08:57:08 -0700112 @Test
113 public void negativeWeights() {
114 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
115 of(new TestEdge(A, B, 1),
116 new TestEdge(A, C, -1),
117 new TestEdge(B, D, 1),
118 new TestEdge(D, A, -2),
119 new TestEdge(C, D, 1),
120 new TestEdge(D, E, 1),
121 new TestEdge(D, F, 1),
122 new TestEdge(E, G, 1),
123 new TestEdge(F, G, 1),
124 new TestEdge(G, A, -5),
125 new TestEdge(A, G, 4)));
126 executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
127 }
128
tome3489412014-08-29 02:30:38 -0700129}