blob: 0e15eb75de806721fd7629c7dffc94ce187420eb [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
tome3489412014-08-29 02:30:38 -070018import java.util.Set;
19
tom144de692014-08-29 11:38:44 -070020import static com.google.common.collect.ImmutableSet.of;
21
tome3489412014-08-29 02:30:38 -070022/**
23 * Base class for various graph-related tests.
24 */
25public 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");
tom2e1f0712014-08-29 13:32:00 -070035 static final TestVertex Z = new TestVertex("Z");
tome3489412014-08-29 02:30:38 -070036
Andrey Komarov2398d962016-09-26 15:11:23 +030037 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
tom0633d682014-09-10 12:10:03 -070047 protected Graph<TestVertex, TestEdge> graph;
tome3489412014-08-29 02:30:38 -070048
Andrey Komarov2398d962016-09-26 15:11:23 +030049 protected EdgeWeigher<TestVertex, TestEdge> weigher =
50 new EdgeWeigher<TestVertex, TestEdge>() {
tomc53fa0d2014-08-29 11:57:11 -070051 @Override
Andrey Komarov2398d962016-09-26 15:11:23 +030052 public Weight weight(TestEdge edge) {
tomc53fa0d2014-08-29 11:57:11 -070053 return edge.weight();
54 }
Andrey Komarov2398d962016-09-26 15:11:23 +030055
56 @Override
57 public Weight getInitialWeight() {
58 return ZW;
59 }
60
61 @Override
62 public Weight getNonViableWeight() {
63 return TestDoubleWeight.NON_VIABLE_WEIGHT;
64 }
tomc53fa0d2014-08-29 11:57:11 -070065 };
tome3489412014-08-29 02:30:38 -070066
Yuta HIGUCHIc5a088c2017-03-30 19:07:07 -070067 /**
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
tome3489412014-08-29 02:30:38 -070088 protected void printPaths(Set<Path<TestVertex, TestEdge>> paths) {
89 for (Path p : paths) {
90 System.out.println(p);
91 }
92 }
93
Yuta HIGUCHIc5a088c2017-03-30 19:07:07 -070094 /**
95 * @return 8 vertices A to H.
96 */
tom0633d682014-09-10 12:10:03 -070097 protected Set<TestVertex> vertexes() {
tom144de692014-08-29 11:38:44 -070098 return of(A, B, C, D, E, F, G, H);
tome3489412014-08-29 02:30:38 -070099 }
100
Yuta HIGUCHIc5a088c2017-03-30 19:07:07 -0700101 /**
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 */
tome3489412014-08-29 02:30:38 -0700110 protected Set<TestEdge> edges() {
Andrey Komarov2398d962016-09-26 15:11:23 +0300111 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));
tome3489412014-08-29 02:30:38 -0700123 }
124
125}