• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Guava Authors
3  *
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
7  *
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.
15  */
16 
17 package com.google.common.graph;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertTrue;
21 
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /**
27  * Tests for a directed {@link ConfigurableMutableNetwork} allowing parallel edges and self-loops.
28  */
29 @RunWith(JUnit4.class)
30 public class ConfigurableDirectedMultiNetworkTest extends ConfigurableDirectedNetworkTest {
31   @Override
createGraph()32   public MutableNetwork<Integer, String> createGraph() {
33     return NetworkBuilder.directed().allowsParallelEdges(true).allowsSelfLoops(true).build();
34   }
35 
36   @Test
adjacentEdges_parallelEdges()37   public void adjacentEdges_parallelEdges() {
38     addEdge(N1, N2, E12);
39     addEdge(N1, N2, E12_A);
40     addEdge(N1, N2, E12_B);
41     addEdge(N3, N4, E34);
42     assertThat(network.adjacentEdges(E12)).containsExactly(E12_A, E12_B);
43   }
44 
45   @Test
edgesConnecting_parallelEdges()46   public void edgesConnecting_parallelEdges() {
47     assertTrue(addEdge(N1, N2, E12));
48     assertTrue(addEdge(N1, N2, E12_A));
49     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12, E12_A);
50     // Passed nodes should be in the correct edge direction, first is the
51     // source node and the second is the target node
52     assertThat(network.edgesConnecting(N2, N1)).isEmpty();
53   }
54 
55   @Test
edgesConnecting_parallelSelfLoopEdges()56   public void edgesConnecting_parallelSelfLoopEdges() {
57     assertTrue(addEdge(N1, N1, E11));
58     assertTrue(addEdge(N1, N1, E11_A));
59     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A);
60   }
61 
62   @Override
63   @Test
addEdge_parallelEdge()64   public void addEdge_parallelEdge() {
65     assertTrue(addEdge(N1, N2, E12));
66     assertTrue(addEdge(N1, N2, E12_A));
67     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12, E12_A);
68   }
69 
70   @Override
71   @Test
addEdge_parallelSelfLoopEdge()72   public void addEdge_parallelSelfLoopEdge() {
73     assertTrue(addEdge(N1, N1, E11));
74     assertTrue(addEdge(N1, N1, E11_A));
75     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A);
76   }
77 
78   @Test
removeEdge_parallelEdge()79   public void removeEdge_parallelEdge() {
80     addEdge(N1, N2, E12);
81     addEdge(N1, N2, E12_A);
82     assertTrue(network.removeEdge(E12_A));
83     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
84   }
85 
86   @Test
removeEdge_parallelSelfLoopEdge()87   public void removeEdge_parallelSelfLoopEdge() {
88     addEdge(N1, N1, E11);
89     addEdge(N1, N1, E11_A);
90     addEdge(N1, N2, E12);
91     assertTrue(network.removeEdge(E11_A));
92     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11);
93     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
94     assertTrue(network.removeEdge(E11));
95     assertThat(network.edgesConnecting(N1, N1)).isEmpty();
96     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
97   }
98 }
99