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 | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 21 | import static com.google.common.collect.ImmutableList.of; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertNull; |
| 24 | |
| 25 | /** |
| 26 | * Test of the default mutable path. |
| 27 | */ |
| 28 | public class DefaultMutablePathTest extends DefaultPathTest { |
| 29 | |
| 30 | @Test |
| 31 | public void equality() { |
| 32 | DefaultPath<TestVertex, TestEdge> p1 = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 33 | new DefaultPath<>(of(new TestEdge(A, B), |
| 34 | new TestEdge(B, C)), |
| 35 | new TestDoubleWeight(2.0)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 36 | DefaultPath<TestVertex, TestEdge> p2 = |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 37 | new DefaultPath<>(of(new TestEdge(A, B), |
| 38 | new TestEdge(B, D)), |
| 39 | new TestDoubleWeight(2.0)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 40 | 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 Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 52 | assertEquals("incorrect path cost", null, p.cost()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void pathCost() { |
| 57 | MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 58 | Weight weight = new TestDoubleWeight(4); |
| 59 | p.setCost(weight); |
| 60 | assertEquals("incorrect path cost", weight, p.cost()); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private void validatePath(Path<TestVertex, TestEdge> p, |
| 64 | TestVertex src, TestVertex dst, int length) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 65 | validatePath(p, src, dst, length, null); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void insertEdge() { |
| 70 | MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 71 | p.insertEdge(new TestEdge(B, C)); |
| 72 | p.insertEdge(new TestEdge(A, B)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 73 | validatePath(p, A, C, 2); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void appendEdge() { |
| 78 | MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 79 | p.appendEdge(new TestEdge(A, B)); |
| 80 | p.appendEdge(new TestEdge(B, C)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 81 | validatePath(p, A, C, 2); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void removeEdge() { |
| 86 | MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 87 | 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)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 91 | validatePath(p, A, D, 4); |
| 92 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 93 | p.removeEdge(new TestEdge(A, B)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 94 | validatePath(p, B, D, 3); |
| 95 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 96 | p.removeEdge(new TestEdge(C, C)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 97 | validatePath(p, B, D, 2); |
| 98 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 99 | p.removeEdge(new TestEdge(C, D)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 100 | validatePath(p, B, C, 1); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void toImmutable() { |
| 105 | MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 106 | p.appendEdge(new TestEdge(A, B)); |
| 107 | p.appendEdge(new TestEdge(B, C)); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 108 | validatePath(p, A, C, 2); |
| 109 | |
| 110 | assertEquals("immutables should equal", p.toImmutable(), p.toImmutable()); |
| 111 | validatePath(p.toImmutable(), A, C, 2); |
| 112 | } |
| 113 | } |