blob: d196fa088823bec11f7d4ab41fb3f6b0506588e9 [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 */
tom144de692014-08-29 11:38:44 -070016package org.onlab.graph;
17
tom144de692014-08-29 11:38:44 -070018import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20
tom144de692014-08-29 11:38:44 -070021import static com.google.common.collect.ImmutableList.of;
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNull;
24
25/**
26 * Test of the default mutable path.
27 */
28public class DefaultMutablePathTest extends DefaultPathTest {
29
30 @Test
31 public void equality() {
32 DefaultPath<TestVertex, TestEdge> p1 =
Andrey Komarov2398d962016-09-26 15:11:23 +030033 new DefaultPath<>(of(new TestEdge(A, B),
34 new TestEdge(B, C)),
35 new TestDoubleWeight(2.0));
tom144de692014-08-29 11:38:44 -070036 DefaultPath<TestVertex, TestEdge> p2 =
Andrey Komarov2398d962016-09-26 15:11:23 +030037 new DefaultPath<>(of(new TestEdge(A, B),
38 new TestEdge(B, D)),
39 new TestDoubleWeight(2.0));
tom144de692014-08-29 11:38:44 -070040 new EqualsTester().addEqualityGroup(new DefaultMutablePath<>(p1),
41 new DefaultMutablePath<>(p1))
42 .addEqualityGroup(new DefaultMutablePath<>(p2))
43 .testEquals();
44 }
45
46 @Test
47 public void empty() {
48 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
49 assertNull("src should be null", p.src());
50 assertNull("dst should be null", p.dst());
51 assertEquals("incorrect edge count", 0, p.edges().size());
Andrey Komarov2398d962016-09-26 15:11:23 +030052 assertEquals("incorrect path cost", null, p.cost());
tom144de692014-08-29 11:38:44 -070053 }
54
55 @Test
56 public void pathCost() {
57 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
Andrey Komarov2398d962016-09-26 15:11:23 +030058 Weight weight = new TestDoubleWeight(4);
59 p.setCost(weight);
60 assertEquals("incorrect path cost", weight, p.cost());
tom144de692014-08-29 11:38:44 -070061 }
62
63 private void validatePath(Path<TestVertex, TestEdge> p,
64 TestVertex src, TestVertex dst, int length) {
Andrey Komarov2398d962016-09-26 15:11:23 +030065 validatePath(p, src, dst, length, null);
tom144de692014-08-29 11:38:44 -070066 }
67
68 @Test
69 public void insertEdge() {
70 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
Andrey Komarov2398d962016-09-26 15:11:23 +030071 p.insertEdge(new TestEdge(B, C));
72 p.insertEdge(new TestEdge(A, B));
tom144de692014-08-29 11:38:44 -070073 validatePath(p, A, C, 2);
74 }
75
76 @Test
77 public void appendEdge() {
78 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
Andrey Komarov2398d962016-09-26 15:11:23 +030079 p.appendEdge(new TestEdge(A, B));
80 p.appendEdge(new TestEdge(B, C));
tom144de692014-08-29 11:38:44 -070081 validatePath(p, A, C, 2);
82 }
83
84 @Test
85 public void removeEdge() {
86 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
Andrey Komarov2398d962016-09-26 15:11:23 +030087 p.appendEdge(new TestEdge(A, B));
88 p.appendEdge(new TestEdge(B, C));
89 p.appendEdge(new TestEdge(C, C));
90 p.appendEdge(new TestEdge(C, D));
tom144de692014-08-29 11:38:44 -070091 validatePath(p, A, D, 4);
92
Andrey Komarov2398d962016-09-26 15:11:23 +030093 p.removeEdge(new TestEdge(A, B));
tom144de692014-08-29 11:38:44 -070094 validatePath(p, B, D, 3);
95
Andrey Komarov2398d962016-09-26 15:11:23 +030096 p.removeEdge(new TestEdge(C, C));
tom144de692014-08-29 11:38:44 -070097 validatePath(p, B, D, 2);
98
Andrey Komarov2398d962016-09-26 15:11:23 +030099 p.removeEdge(new TestEdge(C, D));
tom144de692014-08-29 11:38:44 -0700100 validatePath(p, B, C, 1);
101 }
102
103 @Test
104 public void toImmutable() {
105 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
Andrey Komarov2398d962016-09-26 15:11:23 +0300106 p.appendEdge(new TestEdge(A, B));
107 p.appendEdge(new TestEdge(B, C));
tom144de692014-08-29 11:38:44 -0700108 validatePath(p, A, C, 2);
109
110 assertEquals("immutables should equal", p.toImmutable(), p.toImmutable());
111 validatePath(p.toImmutable(), A, C, 2);
112 }
113}