• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.graph.TestUtil.EdgeType.DIRECTED;
20 import static com.google.common.graph.TestUtil.EdgeType.UNDIRECTED;
21 import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage;
22 import static com.google.common.truth.Truth.assertThat;
23 import static org.junit.Assert.assertThrows;
24 
25 import com.google.common.graph.TestUtil.EdgeType;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Set;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.Parameterized.Parameters;
34 
35 /**
36  * Test for {@link Network} methods which have default implementations. Currently those
37  * implementations are in {@link AbstractNetwork}; in future they might be in {@link Network}
38  * itself, once we are willing to use Java 8 default methods.
39  */
40 @AndroidIncompatible
41 // TODO(cpovirk): Figure out Android JUnit 4 support. Does it work with Gingerbread? @RunWith?
42 @RunWith(Parameterized.class)
43 public final class DefaultNetworkImplementationsTest {
44   private MutableNetwork<Integer, String> network;
45   private NetworkForTest<Integer, String> networkForTest;
46   private static final Integer N1 = 1;
47   private static final Integer N2 = 2;
48   private static final Integer NODE_NOT_IN_GRAPH = 1000;
49   private static final String E11 = "1-1";
50   private static final String E11_A = "1-1a";
51   private static final String E12 = "1-2";
52   private static final String E12_A = "1-2a";
53   private static final String E21 = "2-1";
54   private static final String E23 = "2-3";
55 
56   @Parameters
parameters()57   public static Collection<Object[]> parameters() {
58     return Arrays.asList(
59         new Object[][] {
60           {UNDIRECTED}, {DIRECTED},
61         });
62   }
63 
64   private final EdgeType edgeType;
65 
DefaultNetworkImplementationsTest(EdgeType edgeType)66   public DefaultNetworkImplementationsTest(EdgeType edgeType) {
67     this.edgeType = edgeType;
68   }
69 
70   @Before
setUp()71   public void setUp() throws Exception {
72     NetworkBuilder<Object, Object> builder =
73         (edgeType == EdgeType.DIRECTED) ? NetworkBuilder.directed() : NetworkBuilder.undirected();
74 
75     network = builder.allowsSelfLoops(true).allowsParallelEdges(true).build();
76     networkForTest = NetworkForTest.from(network);
77   }
78 
79   @Test
edgesConnecting_disconnectedNodes()80   public void edgesConnecting_disconnectedNodes() {
81     network.addNode(N1);
82     network.addNode(N2);
83     assertThat(networkForTest.edgesConnecting(N1, N2)).isEmpty();
84   }
85 
86   @Test
edgesConnecting_nodesNotInGraph()87   public void edgesConnecting_nodesNotInGraph() {
88     network.addNode(N1);
89     network.addNode(N2);
90     IllegalArgumentException e =
91         assertThrows(
92             IllegalArgumentException.class,
93             () -> networkForTest.edgesConnecting(N1, NODE_NOT_IN_GRAPH));
94     assertNodeNotInGraphErrorMessage(e);
95     e =
96         assertThrows(
97             IllegalArgumentException.class,
98             () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, N2));
99     assertNodeNotInGraphErrorMessage(e);
100     e =
101         assertThrows(
102             IllegalArgumentException.class,
103             () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH));
104     assertNodeNotInGraphErrorMessage(e);
105   }
106 
107   @Test
edgesConnecting_checkReturnedSetMutability()108   public void edgesConnecting_checkReturnedSetMutability() {
109     network.addNode(N1);
110     network.addNode(N2);
111     Set<String> edgesConnecting = network.edgesConnecting(N1, N2);
112     UnsupportedOperationException e =
113         assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23));
114     network.addEdge(N1, N2, E12);
115     assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting);
116   }
117 
118   @Test
edgesConnecting_oneEdge()119   public void edgesConnecting_oneEdge() {
120     network.addEdge(N1, N2, E12);
121     assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12);
122     if (edgeType == EdgeType.DIRECTED) {
123       assertThat(networkForTest.edgesConnecting(N2, N1)).isEmpty();
124     } else {
125       assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12);
126     }
127   }
128 
129   @Test
edgesConnecting_selfLoop()130   public void edgesConnecting_selfLoop() {
131     network.addEdge(N1, N1, E11);
132     assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11);
133     network.addEdge(N1, N2, E12);
134     assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12);
135     assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11);
136   }
137 
138   @Test
edgesConnecting_parallelEdges()139   public void edgesConnecting_parallelEdges() {
140     network.addEdge(N1, N2, E12);
141     network.addEdge(N1, N2, E12_A);
142     network.addEdge(N2, N1, E21);
143     if (edgeType == EdgeType.DIRECTED) {
144       assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A);
145       assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E21);
146     } else {
147       assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A, E21);
148       assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12, E12_A, E21);
149     }
150   }
151 
152   @Test
edgesConnecting_parallelSelfLoopEdges()153   public void edgesConnecting_parallelSelfLoopEdges() {
154     network.addEdge(N1, N1, E11);
155     network.addEdge(N1, N1, E11_A);
156     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A);
157   }
158 
159   private static class NetworkForTest<N, E> extends AbstractNetwork<N, E> {
160     private final Network<N, E> network;
161 
NetworkForTest(Network<N, E> network)162     NetworkForTest(Network<N, E> network) {
163       this.network = network;
164     }
165 
from(Network<N, E> network)166     static <N, E> NetworkForTest<N, E> from(Network<N, E> network) {
167       return new NetworkForTest<>(network);
168     }
169 
170     @Override
nodes()171     public Set<N> nodes() {
172       return network.nodes();
173     }
174 
175     @Override
edges()176     public Set<E> edges() {
177       return network.edges();
178     }
179 
180     @Override
isDirected()181     public boolean isDirected() {
182       return network.isDirected();
183     }
184 
185     @Override
allowsParallelEdges()186     public boolean allowsParallelEdges() {
187       return network.allowsParallelEdges();
188     }
189 
190     @Override
allowsSelfLoops()191     public boolean allowsSelfLoops() {
192       return network.allowsSelfLoops();
193     }
194 
195     @Override
nodeOrder()196     public ElementOrder<N> nodeOrder() {
197       return network.nodeOrder();
198     }
199 
200     @Override
edgeOrder()201     public ElementOrder<E> edgeOrder() {
202       return network.edgeOrder();
203     }
204 
205     @Override
adjacentNodes(N node)206     public Set<N> adjacentNodes(N node) {
207       return network.adjacentNodes(node);
208     }
209 
210     @Override
predecessors(N node)211     public Set<N> predecessors(N node) {
212       return network.predecessors(node);
213     }
214 
215     @Override
successors(N node)216     public Set<N> successors(N node) {
217       return network.successors(node);
218     }
219 
220     @Override
incidentEdges(N node)221     public Set<E> incidentEdges(N node) {
222       return network.incidentEdges(node);
223     }
224 
225     @Override
inEdges(N node)226     public Set<E> inEdges(N node) {
227       return network.inEdges(node);
228     }
229 
230     @Override
outEdges(N node)231     public Set<E> outEdges(N node) {
232       return network.outEdges(node);
233     }
234 
235     @Override
incidentNodes(E edge)236     public EndpointPair<N> incidentNodes(E edge) {
237       return network.incidentNodes(edge);
238     }
239 
240     @Override
adjacentEdges(E edge)241     public Set<E> adjacentEdges(E edge) {
242       return network.adjacentEdges(edge);
243     }
244 
245     // _don't_ override edge*Connecting*; we want the behavior from AbstractNetwork
246   }
247 }
248