blob: ca02435f491369008c84b8ff0433d65c13a6318c [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 */
tom144de692014-08-29 11:38:44 -070019package org.onlab.graph;
20
tom144de692014-08-29 11:38:44 -070021import com.google.common.testing.EqualsTester;
22import org.junit.Test;
23
tom144de692014-08-29 11:38:44 -070024import static com.google.common.collect.ImmutableList.of;
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNull;
27
28/**
29 * Test of the default mutable path.
30 */
31public class DefaultMutablePathTest extends DefaultPathTest {
32
33 @Test
34 public void equality() {
35 DefaultPath<TestVertex, TestEdge> p1 =
36 new DefaultPath<>(of(new TestEdge(A, B, 1),
37 new TestEdge(B, C, 1)), 2.0);
38 DefaultPath<TestVertex, TestEdge> p2 =
39 new DefaultPath<>(of(new TestEdge(A, B, 1),
40 new TestEdge(B, D, 1)), 2.0);
41 new EqualsTester().addEqualityGroup(new DefaultMutablePath<>(p1),
42 new DefaultMutablePath<>(p1))
43 .addEqualityGroup(new DefaultMutablePath<>(p2))
44 .testEquals();
45 }
46
47 @Test
48 public void empty() {
49 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
50 assertNull("src should be null", p.src());
51 assertNull("dst should be null", p.dst());
52 assertEquals("incorrect edge count", 0, p.edges().size());
53 assertEquals("incorrect path cost", 0.0, p.cost(), 0.1);
54 }
55
56 @Test
57 public void pathCost() {
58 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
59 p.setCost(4);
60 assertEquals("incorrect path cost", 4.0, p.cost(), 0.1);
61 }
62
63 private void validatePath(Path<TestVertex, TestEdge> p,
64 TestVertex src, TestVertex dst, int length) {
65 validatePath(p, src, dst, length, 0.0);
66 }
67
68 @Test
69 public void insertEdge() {
70 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
71 p.insertEdge(new TestEdge(B, C, 1));
72 p.insertEdge(new TestEdge(A, B, 1));
73 validatePath(p, A, C, 2);
74 }
75
76 @Test
77 public void appendEdge() {
78 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
79 p.appendEdge(new TestEdge(A, B, 1));
80 p.appendEdge(new TestEdge(B, C, 1));
81 validatePath(p, A, C, 2);
82 }
83
84 @Test
85 public void removeEdge() {
86 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
87 p.appendEdge(new TestEdge(A, B, 1));
88 p.appendEdge(new TestEdge(B, C, 1));
89 p.appendEdge(new TestEdge(C, C, 2));
90 p.appendEdge(new TestEdge(C, D, 1));
91 validatePath(p, A, D, 4);
92
93 p.removeEdge(new TestEdge(A, B, 1));
94 validatePath(p, B, D, 3);
95
96 p.removeEdge(new TestEdge(C, C, 2));
97 validatePath(p, B, D, 2);
98
99 p.removeEdge(new TestEdge(C, D, 1));
100 validatePath(p, B, C, 1);
101 }
102
103 @Test
104 public void toImmutable() {
105 MutablePath<TestVertex, TestEdge> p = new DefaultMutablePath<>();
106 p.appendEdge(new TestEdge(A, B, 1));
107 p.appendEdge(new TestEdge(B, C, 1));
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}