Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 18 | import java.util.Set; |
| 19 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 20 | import static com.google.common.collect.ImmutableSet.of; |
| 21 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Base class for various graph-related tests. |
| 24 | */ |
| 25 | public class GraphTest { |
| 26 | |
| 27 | static final TestVertex A = new TestVertex("A"); |
| 28 | static final TestVertex B = new TestVertex("B"); |
| 29 | static final TestVertex C = new TestVertex("C"); |
| 30 | static final TestVertex D = new TestVertex("D"); |
| 31 | static final TestVertex E = new TestVertex("E"); |
| 32 | static final TestVertex F = new TestVertex("F"); |
| 33 | static final TestVertex G = new TestVertex("G"); |
| 34 | static final TestVertex H = new TestVertex("H"); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 35 | static final TestVertex Z = new TestVertex("Z"); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 36 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 37 | static final TestDoubleWeight ZW = new TestDoubleWeight(0); |
| 38 | static final TestDoubleWeight NW5 = new TestDoubleWeight(-5); |
| 39 | static final TestDoubleWeight NW2 = new TestDoubleWeight(-2); |
| 40 | static final TestDoubleWeight NW1 = new TestDoubleWeight(-1); |
| 41 | static final TestDoubleWeight W1 = new TestDoubleWeight(1); |
| 42 | static final TestDoubleWeight W2 = new TestDoubleWeight(2); |
| 43 | static final TestDoubleWeight W3 = new TestDoubleWeight(3); |
| 44 | static final TestDoubleWeight W4 = new TestDoubleWeight(4); |
| 45 | static final TestDoubleWeight W5 = new TestDoubleWeight(5); |
| 46 | |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 47 | protected Graph<TestVertex, TestEdge> graph; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 48 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 49 | protected EdgeWeigher<TestVertex, TestEdge> weigher = |
| 50 | new EdgeWeigher<TestVertex, TestEdge>() { |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 51 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 52 | public Weight weight(TestEdge edge) { |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 53 | return edge.weight(); |
| 54 | } |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public Weight getInitialWeight() { |
| 58 | return ZW; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Weight getNonViableWeight() { |
| 63 | return TestDoubleWeight.NON_VIABLE_WEIGHT; |
| 64 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 65 | }; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 66 | |
Yuta HIGUCHI | c5a088c | 2017-03-30 19:07:07 -0700 | [diff] [blame] | 67 | /** |
| 68 | * EdgeWeigher which only looks at hop count. |
| 69 | */ |
| 70 | protected final EdgeWeigher<TestVertex, TestEdge> hopWeigher = |
| 71 | new EdgeWeigher<TestVertex, TestEdge>() { |
| 72 | @Override |
| 73 | public Weight weight(TestEdge edge) { |
| 74 | return W1; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Weight getInitialWeight() { |
| 79 | return ZW; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Weight getNonViableWeight() { |
| 84 | return TestDoubleWeight.NON_VIABLE_WEIGHT; |
| 85 | } |
| 86 | }; |
| 87 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 88 | protected void printPaths(Set<Path<TestVertex, TestEdge>> paths) { |
| 89 | for (Path p : paths) { |
| 90 | System.out.println(p); |
| 91 | } |
| 92 | } |
| 93 | |
Yuta HIGUCHI | c5a088c | 2017-03-30 19:07:07 -0700 | [diff] [blame] | 94 | /** |
| 95 | * @return 8 vertices A to H. |
| 96 | */ |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 97 | protected Set<TestVertex> vertexes() { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 98 | return of(A, B, C, D, E, F, G, H); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Yuta HIGUCHI | c5a088c | 2017-03-30 19:07:07 -0700 | [diff] [blame] | 101 | /** |
| 102 | * <pre> |
| 103 | * A → B → D → H |
| 104 | * ↓ ↙ ↓ ↙ ↑ ↗ |
| 105 | * C → E → F → G |
| 106 | * </pre> |
| 107 | * Note: not all edges have same weight, see method body for details. |
| 108 | * @return 12 edges illustrated as above. |
| 109 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 110 | protected Set<TestEdge> edges() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 111 | return of(new TestEdge(A, B, W1), |
| 112 | new TestEdge(A, C, W3), |
| 113 | new TestEdge(B, D, W2), |
| 114 | new TestEdge(B, C, W1), |
| 115 | new TestEdge(B, E, W4), |
| 116 | new TestEdge(C, E, W1), |
| 117 | new TestEdge(D, H, W5), |
| 118 | new TestEdge(D, E, W1), |
| 119 | new TestEdge(E, F, W1), |
| 120 | new TestEdge(F, D, W1), |
| 121 | new TestEdge(F, G, W1), |
| 122 | new TestEdge(F, H, W1)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } |