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 assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23)); 113 network.addEdge(N1, N2, E12); 114 assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting); 115 } 116 117 @Test edgesConnecting_oneEdge()118 public void edgesConnecting_oneEdge() { 119 network.addEdge(N1, N2, E12); 120 assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12); 121 if (edgeType == EdgeType.DIRECTED) { 122 assertThat(networkForTest.edgesConnecting(N2, N1)).isEmpty(); 123 } else { 124 assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12); 125 } 126 } 127 128 @Test edgesConnecting_selfLoop()129 public void edgesConnecting_selfLoop() { 130 network.addEdge(N1, N1, E11); 131 assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11); 132 network.addEdge(N1, N2, E12); 133 assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12); 134 assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11); 135 } 136 137 @Test edgesConnecting_parallelEdges()138 public void edgesConnecting_parallelEdges() { 139 network.addEdge(N1, N2, E12); 140 network.addEdge(N1, N2, E12_A); 141 network.addEdge(N2, N1, E21); 142 if (edgeType == EdgeType.DIRECTED) { 143 assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A); 144 assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E21); 145 } else { 146 assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A, E21); 147 assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12, E12_A, E21); 148 } 149 } 150 151 @Test edgesConnecting_parallelSelfLoopEdges()152 public void edgesConnecting_parallelSelfLoopEdges() { 153 network.addEdge(N1, N1, E11); 154 network.addEdge(N1, N1, E11_A); 155 assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A); 156 } 157 158 private static class NetworkForTest<N, E> extends AbstractNetwork<N, E> { 159 private final Network<N, E> network; 160 NetworkForTest(Network<N, E> network)161 NetworkForTest(Network<N, E> network) { 162 this.network = network; 163 } 164 from(Network<N, E> network)165 static <N, E> NetworkForTest<N, E> from(Network<N, E> network) { 166 return new NetworkForTest<>(network); 167 } 168 169 @Override nodes()170 public Set<N> nodes() { 171 return network.nodes(); 172 } 173 174 @Override edges()175 public Set<E> edges() { 176 return network.edges(); 177 } 178 179 @Override isDirected()180 public boolean isDirected() { 181 return network.isDirected(); 182 } 183 184 @Override allowsParallelEdges()185 public boolean allowsParallelEdges() { 186 return network.allowsParallelEdges(); 187 } 188 189 @Override allowsSelfLoops()190 public boolean allowsSelfLoops() { 191 return network.allowsSelfLoops(); 192 } 193 194 @Override nodeOrder()195 public ElementOrder<N> nodeOrder() { 196 return network.nodeOrder(); 197 } 198 199 @Override edgeOrder()200 public ElementOrder<E> edgeOrder() { 201 return network.edgeOrder(); 202 } 203 204 @Override adjacentNodes(N node)205 public Set<N> adjacentNodes(N node) { 206 return network.adjacentNodes(node); 207 } 208 209 @Override predecessors(N node)210 public Set<N> predecessors(N node) { 211 return network.predecessors(node); 212 } 213 214 @Override successors(N node)215 public Set<N> successors(N node) { 216 return network.successors(node); 217 } 218 219 @Override incidentEdges(N node)220 public Set<E> incidentEdges(N node) { 221 return network.incidentEdges(node); 222 } 223 224 @Override inEdges(N node)225 public Set<E> inEdges(N node) { 226 return network.inEdges(node); 227 } 228 229 @Override outEdges(N node)230 public Set<E> outEdges(N node) { 231 return network.outEdges(node); 232 } 233 234 @Override incidentNodes(E edge)235 public EndpointPair<N> incidentNodes(E edge) { 236 return network.incidentNodes(edge); 237 } 238 239 @Override adjacentEdges(E edge)240 public Set<E> adjacentEdges(E edge) { 241 return network.adjacentEdges(edge); 242 } 243 244 // _don't_ override edge*Connecting*; we want the behavior from AbstractNetwork 245 } 246 } 247