• 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.graph.GraphConstants.ENDPOINTS_MISMATCH;
20 import static com.google.common.graph.TestUtil.assertEdgeNotInGraphErrorMessage;
21 import static com.google.common.truth.Truth.assertThat;
22 import static com.google.common.truth.TruthJUnit.assume;
23 import static org.junit.Assert.assertThrows;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 
27 import com.google.common.collect.ImmutableSet;
28 import java.util.Collections;
29 import java.util.Set;
30 import org.junit.After;
31 import org.junit.Test;
32 
33 /**
34  * Abstract base class for testing directed {@link Network} implementations defined in this package.
35  */
36 public abstract class AbstractStandardDirectedNetworkTest extends AbstractNetworkTest {
37 
38   @After
validateSourceAndTarget()39   public void validateSourceAndTarget() {
40     for (Integer node : network.nodes()) {
41       for (String inEdge : network.inEdges(node)) {
42         EndpointPair<Integer> endpointPair = network.incidentNodes(inEdge);
43         assertThat(endpointPair.source()).isEqualTo(endpointPair.adjacentNode(node));
44         assertThat(endpointPair.target()).isEqualTo(node);
45       }
46 
47       for (String outEdge : network.outEdges(node)) {
48         EndpointPair<Integer> endpointPair = network.incidentNodes(outEdge);
49         assertThat(endpointPair.source()).isEqualTo(node);
50         assertThat(endpointPair.target()).isEqualTo(endpointPair.adjacentNode(node));
51       }
52 
53       for (Integer adjacentNode : network.adjacentNodes(node)) {
54         Set<String> edges = network.edgesConnecting(node, adjacentNode);
55         Set<String> antiParallelEdges = network.edgesConnecting(adjacentNode, node);
56         assertThat(node.equals(adjacentNode) || Collections.disjoint(edges, antiParallelEdges))
57             .isTrue();
58       }
59     }
60   }
61 
62   @Override
63   @Test
nodes_checkReturnedSetMutability()64   public void nodes_checkReturnedSetMutability() {
65     assume().that(graphIsMutable()).isTrue();
66 
67     Set<Integer> nodes = network.nodes();
68     UnsupportedOperationException e =
69         assertThrows(UnsupportedOperationException.class, () -> nodes.add(N2));
70     addNode(N1);
71     assertThat(network.nodes()).containsExactlyElementsIn(nodes);
72   }
73 
74   @Override
75   @Test
edges_checkReturnedSetMutability()76   public void edges_checkReturnedSetMutability() {
77     assume().that(graphIsMutable()).isTrue();
78 
79     Set<String> edges = network.edges();
80     UnsupportedOperationException e =
81         assertThrows(UnsupportedOperationException.class, () -> edges.add(E12));
82     addEdge(N1, N2, E12);
83     assertThat(network.edges()).containsExactlyElementsIn(edges);
84   }
85 
86   @Override
87   @Test
incidentEdges_checkReturnedSetMutability()88   public void incidentEdges_checkReturnedSetMutability() {
89     assume().that(graphIsMutable()).isTrue();
90 
91     addNode(N1);
92     Set<String> incidentEdges = network.incidentEdges(N1);
93     UnsupportedOperationException e =
94         assertThrows(UnsupportedOperationException.class, () -> incidentEdges.add(E12));
95     addEdge(N1, N2, E12);
96     assertThat(network.incidentEdges(N1)).containsExactlyElementsIn(incidentEdges);
97   }
98 
99   @Override
100   @Test
adjacentNodes_checkReturnedSetMutability()101   public void adjacentNodes_checkReturnedSetMutability() {
102     assume().that(graphIsMutable()).isTrue();
103 
104     addNode(N1);
105     Set<Integer> adjacentNodes = network.adjacentNodes(N1);
106     UnsupportedOperationException e =
107         assertThrows(UnsupportedOperationException.class, () -> adjacentNodes.add(N2));
108     addEdge(N1, N2, E12);
109     assertThat(network.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes);
110   }
111 
112   @Override
adjacentEdges_checkReturnedSetMutability()113   public void adjacentEdges_checkReturnedSetMutability() {
114     assume().that(graphIsMutable()).isTrue();
115 
116     addEdge(N1, N2, E12);
117     Set<String> adjacentEdges = network.adjacentEdges(E12);
118     try {
119       adjacentEdges.add(E23);
120       fail(ERROR_MODIFIABLE_COLLECTION);
121     } catch (UnsupportedOperationException e) {
122       addEdge(N2, N3, E23);
123       assertThat(network.adjacentEdges(E12)).containsExactlyElementsIn(adjacentEdges);
124     }
125   }
126 
127   @Override
128   @Test
edgesConnecting_checkReturnedSetMutability()129   public void edgesConnecting_checkReturnedSetMutability() {
130     assume().that(graphIsMutable()).isTrue();
131 
132     addNode(N1);
133     addNode(N2);
134     Set<String> edgesConnecting = network.edgesConnecting(N1, N2);
135     UnsupportedOperationException e =
136         assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23));
137     addEdge(N1, N2, E12);
138     assertThat(network.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting);
139   }
140 
141   @Override
142   @Test
inEdges_checkReturnedSetMutability()143   public void inEdges_checkReturnedSetMutability() {
144     assume().that(graphIsMutable()).isTrue();
145 
146     addNode(N2);
147     Set<String> inEdges = network.inEdges(N2);
148     UnsupportedOperationException e =
149         assertThrows(UnsupportedOperationException.class, () -> inEdges.add(E12));
150     addEdge(N1, N2, E12);
151     assertThat(network.inEdges(N2)).containsExactlyElementsIn(inEdges);
152   }
153 
154   @Override
155   @Test
outEdges_checkReturnedSetMutability()156   public void outEdges_checkReturnedSetMutability() {
157     assume().that(graphIsMutable()).isTrue();
158 
159     addNode(N1);
160     Set<String> outEdges = network.outEdges(N1);
161     UnsupportedOperationException e =
162         assertThrows(UnsupportedOperationException.class, () -> outEdges.add(E12));
163     addEdge(N1, N2, E12);
164     assertThat(network.outEdges(N1)).containsExactlyElementsIn(outEdges);
165   }
166 
167   @Override
168   @Test
predecessors_checkReturnedSetMutability()169   public void predecessors_checkReturnedSetMutability() {
170     assume().that(graphIsMutable()).isTrue();
171 
172     addNode(N2);
173     Set<Integer> predecessors = network.predecessors(N2);
174     UnsupportedOperationException e =
175         assertThrows(UnsupportedOperationException.class, () -> predecessors.add(N1));
176     addEdge(N1, N2, E12);
177     assertThat(network.predecessors(N2)).containsExactlyElementsIn(predecessors);
178   }
179 
180   @Override
181   @Test
successors_checkReturnedSetMutability()182   public void successors_checkReturnedSetMutability() {
183     assume().that(graphIsMutable()).isTrue();
184 
185     addNode(N1);
186     Set<Integer> successors = network.successors(N1);
187     UnsupportedOperationException e =
188         assertThrows(UnsupportedOperationException.class, () -> successors.add(N2));
189     addEdge(N1, N2, E12);
190     assertThat(successors).containsExactlyElementsIn(network.successors(N1));
191   }
192 
193   @Test
edges_containsOrderMismatch()194   public void edges_containsOrderMismatch() {
195     addEdge(N1, N2, E12);
196     EndpointPair<Integer> endpointsN1N2 = EndpointPair.unordered(N1, N2);
197     EndpointPair<Integer> endpointsN2N1 = EndpointPair.unordered(N2, N1);
198     assertThat(network.asGraph().edges()).doesNotContain(endpointsN1N2);
199     assertThat(network.asGraph().edges()).doesNotContain(endpointsN2N1);
200   }
201 
202   @Test
edgesConnecting_orderMismatch()203   public void edgesConnecting_orderMismatch() {
204     addEdge(N1, N2, E12);
205     IllegalArgumentException e =
206         assertThrows(
207             IllegalArgumentException.class,
208             () -> {
209               Set<String> unused = network.edgesConnecting(EndpointPair.unordered(N1, N2));
210             });
211     assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH);
212   }
213 
214   @Test
edgeConnectingOrNull_orderMismatch()215   public void edgeConnectingOrNull_orderMismatch() {
216     addEdge(N1, N2, E12);
217     IllegalArgumentException e =
218         assertThrows(
219             IllegalArgumentException.class,
220             () -> {
221               String unused = network.edgeConnectingOrNull(EndpointPair.unordered(N1, N2));
222             });
223     assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH);
224   }
225 
226   @Override
227   @Test
incidentNodes_oneEdge()228   public void incidentNodes_oneEdge() {
229     addEdge(N1, N2, E12);
230     assertThat(network.incidentNodes(E12).source()).isEqualTo(N1);
231     assertThat(network.incidentNodes(E12).target()).isEqualTo(N2);
232   }
233 
234   @Test
edgesConnecting_oneEdge()235   public void edgesConnecting_oneEdge() {
236     addEdge(N1, N2, E12);
237     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
238     // Passed nodes should be in the correct edge direction, first is the
239     // source node and the second is the target node
240     assertThat(network.edgesConnecting(N2, N1)).isEmpty();
241   }
242 
243   @Test
inEdges_oneEdge()244   public void inEdges_oneEdge() {
245     addEdge(N1, N2, E12);
246     assertThat(network.inEdges(N2)).containsExactly(E12);
247     // Edge direction handled correctly
248     assertThat(network.inEdges(N1)).isEmpty();
249   }
250 
251   @Test
outEdges_oneEdge()252   public void outEdges_oneEdge() {
253     addEdge(N1, N2, E12);
254     assertThat(network.outEdges(N1)).containsExactly(E12);
255     // Edge direction handled correctly
256     assertThat(network.outEdges(N2)).isEmpty();
257   }
258 
259   @Test
predecessors_oneEdge()260   public void predecessors_oneEdge() {
261     addEdge(N1, N2, E12);
262     assertThat(network.predecessors(N2)).containsExactly(N1);
263     // Edge direction handled correctly
264     assertThat(network.predecessors(N1)).isEmpty();
265   }
266 
267   @Test
successors_oneEdge()268   public void successors_oneEdge() {
269     addEdge(N1, N2, E12);
270     assertThat(network.successors(N1)).containsExactly(N2);
271     // Edge direction handled correctly
272     assertThat(network.successors(N2)).isEmpty();
273   }
274 
275   @Test
source_oneEdge()276   public void source_oneEdge() {
277     addEdge(N1, N2, E12);
278     assertThat(network.incidentNodes(E12).source()).isEqualTo(N1);
279   }
280 
281   @Test
source_edgeNotInGraph()282   public void source_edgeNotInGraph() {
283     IllegalArgumentException e =
284         assertThrows(
285             IllegalArgumentException.class,
286             () -> network.incidentNodes(EDGE_NOT_IN_GRAPH).source());
287     assertEdgeNotInGraphErrorMessage(e);
288   }
289 
290   @Test
target_oneEdge()291   public void target_oneEdge() {
292     addEdge(N1, N2, E12);
293     assertThat(network.incidentNodes(E12).target()).isEqualTo(N2);
294   }
295 
296   @Test
target_edgeNotInGraph()297   public void target_edgeNotInGraph() {
298     IllegalArgumentException e =
299         assertThrows(
300             IllegalArgumentException.class,
301             () -> network.incidentNodes(EDGE_NOT_IN_GRAPH).target());
302     assertEdgeNotInGraphErrorMessage(e);
303   }
304 
305   @Test
inDegree_oneEdge()306   public void inDegree_oneEdge() {
307     addEdge(N1, N2, E12);
308     assertThat(network.inDegree(N2)).isEqualTo(1);
309     // Edge direction handled correctly
310     assertThat(network.inDegree(N1)).isEqualTo(0);
311   }
312 
313   @Test
outDegree_oneEdge()314   public void outDegree_oneEdge() {
315     addEdge(N1, N2, E12);
316     assertThat(network.outDegree(N1)).isEqualTo(1);
317     // Edge direction handled correctly
318     assertThat(network.outDegree(N2)).isEqualTo(0);
319   }
320 
321   @Test
edges_selfLoop()322   public void edges_selfLoop() {
323     assume().that(network.allowsSelfLoops()).isTrue();
324 
325     addEdge(N1, N1, E11);
326     assertThat(network.edges()).containsExactly(E11);
327   }
328 
329   @Test
incidentEdges_selfLoop()330   public void incidentEdges_selfLoop() {
331     assume().that(network.allowsSelfLoops()).isTrue();
332 
333     addEdge(N1, N1, E11);
334     assertThat(network.incidentEdges(N1)).containsExactly(E11);
335   }
336 
337   @Test
incidentNodes_selfLoop()338   public void incidentNodes_selfLoop() {
339     assume().that(network.allowsSelfLoops()).isTrue();
340 
341     addEdge(N1, N1, E11);
342     assertThat(network.incidentNodes(E11).source()).isEqualTo(N1);
343     assertThat(network.incidentNodes(E11).target()).isEqualTo(N1);
344   }
345 
346   @Test
adjacentNodes_selfLoop()347   public void adjacentNodes_selfLoop() {
348     assume().that(network.allowsSelfLoops()).isTrue();
349 
350     addEdge(N1, N1, E11);
351     addEdge(N1, N2, E12);
352     assertThat(network.adjacentNodes(N1)).containsExactly(N1, N2);
353   }
354 
355   @Test
adjacentEdges_selfLoop()356   public void adjacentEdges_selfLoop() {
357     assume().that(network.allowsSelfLoops()).isTrue();
358 
359     addEdge(N1, N1, E11);
360     addEdge(N1, N2, E12);
361     assertThat(network.adjacentEdges(E11)).containsExactly(E12);
362   }
363 
364   @Test
edgesConnecting_selfLoop()365   public void edgesConnecting_selfLoop() {
366     assume().that(network.allowsSelfLoops()).isTrue();
367 
368     addEdge(N1, N1, E11);
369     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11);
370     addEdge(N1, N2, E12);
371     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
372     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11);
373   }
374 
375   @Test
inEdges_selfLoop()376   public void inEdges_selfLoop() {
377     assume().that(network.allowsSelfLoops()).isTrue();
378 
379     addEdge(N1, N1, E11);
380     assertThat(network.inEdges(N1)).containsExactly(E11);
381     addEdge(N4, N1, E41);
382     assertThat(network.inEdges(N1)).containsExactly(E11, E41);
383   }
384 
385   @Test
outEdges_selfLoop()386   public void outEdges_selfLoop() {
387     assume().that(network.allowsSelfLoops()).isTrue();
388 
389     addEdge(N1, N1, E11);
390     assertThat(network.outEdges(N1)).containsExactly(E11);
391     addEdge(N1, N2, E12);
392     assertThat(network.outEdges(N1)).containsExactly(E11, E12);
393   }
394 
395   @Test
predecessors_selfLoop()396   public void predecessors_selfLoop() {
397     assume().that(network.allowsSelfLoops()).isTrue();
398 
399     addEdge(N1, N1, E11);
400     assertThat(network.predecessors(N1)).containsExactly(N1);
401     addEdge(N4, N1, E41);
402     assertThat(network.predecessors(N1)).containsExactly(N1, N4);
403   }
404 
405   @Test
successors_selfLoop()406   public void successors_selfLoop() {
407     assume().that(network.allowsSelfLoops()).isTrue();
408 
409     addEdge(N1, N1, E11);
410     assertThat(network.successors(N1)).containsExactly(N1);
411     addEdge(N1, N2, E12);
412     assertThat(network.successors(N1)).containsExactly(N1, N2);
413   }
414 
415   @Test
source_selfLoop()416   public void source_selfLoop() {
417     assume().that(network.allowsSelfLoops()).isTrue();
418 
419     addEdge(N1, N1, E11);
420     assertThat(network.incidentNodes(E11).source()).isEqualTo(N1);
421   }
422 
423   @Test
target_selfLoop()424   public void target_selfLoop() {
425     assume().that(network.allowsSelfLoops()).isTrue();
426 
427     addEdge(N1, N1, E11);
428     assertThat(network.incidentNodes(E11).target()).isEqualTo(N1);
429   }
430 
431   @Test
degree_selfLoop()432   public void degree_selfLoop() {
433     assume().that(network.allowsSelfLoops()).isTrue();
434 
435     addEdge(N1, N1, E11);
436     assertThat(network.degree(N1)).isEqualTo(2);
437     addEdge(N1, N2, E12);
438     assertThat(network.degree(N1)).isEqualTo(3);
439   }
440 
441   @Test
inDegree_selfLoop()442   public void inDegree_selfLoop() {
443     assume().that(network.allowsSelfLoops()).isTrue();
444 
445     addEdge(N1, N1, E11);
446     assertThat(network.inDegree(N1)).isEqualTo(1);
447     addEdge(N4, N1, E41);
448     assertThat(network.inDegree(N1)).isEqualTo(2);
449   }
450 
451   @Test
outDegree_selfLoop()452   public void outDegree_selfLoop() {
453     assume().that(network.allowsSelfLoops()).isTrue();
454 
455     addEdge(N1, N1, E11);
456     assertThat(network.outDegree(N1)).isEqualTo(1);
457     addEdge(N1, N2, E12);
458     assertThat(network.outDegree(N1)).isEqualTo(2);
459   }
460 
461   // Element Mutation
462 
463   @Test
addEdge_existingNodes()464   public void addEdge_existingNodes() {
465     assume().that(graphIsMutable()).isTrue();
466 
467     // Adding nodes initially for safety (insulating from possible future
468     // modifications to proxy methods)
469     addNode(N1);
470     addNode(N2);
471     assertThat(networkAsMutableNetwork.addEdge(N1, N2, E12)).isTrue();
472     assertThat(network.edges()).contains(E12);
473     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12);
474     // Direction of the added edge is correctly handled
475     assertThat(network.edgesConnecting(N2, N1)).isEmpty();
476   }
477 
478   @Test
addEdge_existingEdgeBetweenSameNodes()479   public void addEdge_existingEdgeBetweenSameNodes() {
480     assume().that(graphIsMutable()).isTrue();
481 
482     addEdge(N1, N2, E12);
483     ImmutableSet<String> edges = ImmutableSet.copyOf(network.edges());
484     assertThat(networkAsMutableNetwork.addEdge(N1, N2, E12)).isFalse();
485     assertThat(network.edges()).containsExactlyElementsIn(edges);
486   }
487 
488   @Test
addEdge_existingEdgeBetweenDifferentNodes()489   public void addEdge_existingEdgeBetweenDifferentNodes() {
490     assume().that(graphIsMutable()).isTrue();
491 
492     addEdge(N1, N2, E12);
493     IllegalArgumentException e =
494         assertThrows(
495             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N4, N5, E12));
496     assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE);
497     e = assertThrows(IllegalArgumentException.class, () -> addEdge(N2, N1, E12));
498     assertThat(e).hasMessageThat().contains(ERROR_REUSE_EDGE);
499   }
500 
501   @Test
addEdge_parallelEdge_notAllowed()502   public void addEdge_parallelEdge_notAllowed() {
503     assume().that(graphIsMutable()).isTrue();
504     assume().that(network.allowsParallelEdges()).isFalse();
505 
506     addEdge(N1, N2, E12);
507     IllegalArgumentException e =
508         assertThrows(
509             IllegalArgumentException.class,
510             () -> networkAsMutableNetwork.addEdge(N1, N2, EDGE_NOT_IN_GRAPH));
511     assertThat(e).hasMessageThat().contains(ERROR_PARALLEL_EDGE);
512   }
513 
514   @Test
addEdge_parallelEdge_allowsParallelEdges()515   public void addEdge_parallelEdge_allowsParallelEdges() {
516     assume().that(graphIsMutable()).isTrue();
517     assume().that(network.allowsParallelEdges()).isTrue();
518 
519     assertTrue(networkAsMutableNetwork.addEdge(N1, N2, E12));
520     assertTrue(networkAsMutableNetwork.addEdge(N1, N2, E12_A));
521     assertThat(network.edgesConnecting(N1, N2)).containsExactly(E12, E12_A);
522   }
523 
524   @Test
addEdge_orderMismatch()525   public void addEdge_orderMismatch() {
526     assume().that(graphIsMutable()).isTrue();
527 
528     EndpointPair<Integer> endpoints = EndpointPair.unordered(N1, N2);
529     IllegalArgumentException e =
530         assertThrows(
531             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(endpoints, E12));
532     assertThat(e).hasMessageThat().contains(ENDPOINTS_MISMATCH);
533   }
534 
535   @Test
addEdge_selfLoop_notAllowed()536   public void addEdge_selfLoop_notAllowed() {
537     assume().that(graphIsMutable()).isTrue();
538     assume().that(network.allowsSelfLoops()).isFalse();
539 
540     IllegalArgumentException e =
541         assertThrows(
542             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E11));
543     assertThat(e).hasMessageThat().contains(ERROR_SELF_LOOP);
544   }
545 
546   /**
547    * This test checks an implementation dependent feature. It tests that the method {@code addEdge}
548    * will silently add the missing nodes to the graph, then add the edge connecting them. We are not
549    * using the proxy methods here as we want to test {@code addEdge} when the end-points are not
550    * elements of the graph.
551    */
552   @Test
addEdge_nodesNotInGraph()553   public void addEdge_nodesNotInGraph() {
554     assume().that(graphIsMutable()).isTrue();
555 
556     networkAsMutableNetwork.addNode(N1);
557     assertTrue(networkAsMutableNetwork.addEdge(N1, N5, E15));
558     assertTrue(networkAsMutableNetwork.addEdge(N4, N1, E41));
559     assertTrue(networkAsMutableNetwork.addEdge(N2, N3, E23));
560     assertThat(network.nodes()).containsExactly(N1, N5, N4, N2, N3);
561     assertThat(network.edges()).containsExactly(E15, E41, E23);
562     assertThat(network.edgesConnecting(N1, N5)).containsExactly(E15);
563     assertThat(network.edgesConnecting(N4, N1)).containsExactly(E41);
564     assertThat(network.edgesConnecting(N2, N3)).containsExactly(E23);
565     // Direction of the added edge is correctly handled
566     assertThat(network.edgesConnecting(N3, N2)).isEmpty();
567   }
568 
569   @Test
addEdge_selfLoop_allowed()570   public void addEdge_selfLoop_allowed() {
571     assume().that(graphIsMutable()).isTrue();
572     assume().that(network.allowsSelfLoops()).isTrue();
573 
574     assertThat(networkAsMutableNetwork.addEdge(N1, N1, E11)).isTrue();
575     assertThat(network.edges()).contains(E11);
576     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11);
577   }
578 
579   @Test
addEdge_existingSelfLoopEdgeBetweenSameNodes()580   public void addEdge_existingSelfLoopEdgeBetweenSameNodes() {
581     assume().that(graphIsMutable()).isTrue();
582     assume().that(network.allowsSelfLoops()).isTrue();
583 
584     addEdge(N1, N1, E11);
585     ImmutableSet<String> edges = ImmutableSet.copyOf(network.edges());
586     assertThat(networkAsMutableNetwork.addEdge(N1, N1, E11)).isFalse();
587     assertThat(network.edges()).containsExactlyElementsIn(edges);
588   }
589 
590   @Test
addEdge_existingEdgeBetweenDifferentNodes_selfLoops()591   public void addEdge_existingEdgeBetweenDifferentNodes_selfLoops() {
592     assume().that(graphIsMutable()).isTrue();
593     assume().that(network.allowsSelfLoops()).isTrue();
594 
595     addEdge(N1, N1, E11);
596     IllegalArgumentException e =
597         assertThrows(
598             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N2, E11));
599     assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
600     e =
601         assertThrows(
602             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N2, N2, E11));
603     assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
604     addEdge(N1, N2, E12);
605     e =
606         assertThrows(
607             IllegalArgumentException.class, () -> networkAsMutableNetwork.addEdge(N1, N1, E12));
608     assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
609   }
610 
611   @Test
addEdge_parallelSelfLoopEdge_notAllowed()612   public void addEdge_parallelSelfLoopEdge_notAllowed() {
613     assume().that(graphIsMutable()).isTrue();
614     assume().that(network.allowsSelfLoops()).isTrue();
615     assume().that(network.allowsParallelEdges()).isFalse();
616 
617     addEdge(N1, N1, E11);
618     IllegalArgumentException e =
619         assertThrows(
620             IllegalArgumentException.class,
621             () -> networkAsMutableNetwork.addEdge(N1, N1, EDGE_NOT_IN_GRAPH));
622     assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
623   }
624 
625   @Test
addEdge_parallelSelfLoopEdge_allowsParallelEdges()626   public void addEdge_parallelSelfLoopEdge_allowsParallelEdges() {
627     assume().that(graphIsMutable()).isTrue();
628     assume().that(network.allowsSelfLoops()).isTrue();
629     assume().that(network.allowsParallelEdges()).isTrue();
630 
631     assertTrue(networkAsMutableNetwork.addEdge(N1, N1, E11));
632     assertTrue(networkAsMutableNetwork.addEdge(N1, N1, E11_A));
633     assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A);
634   }
635 
636   @Test
removeNode_existingNodeWithSelfLoopEdge()637   public void removeNode_existingNodeWithSelfLoopEdge() {
638     assume().that(graphIsMutable()).isTrue();
639     assume().that(network.allowsSelfLoops()).isTrue();
640 
641     addNode(N1);
642     addEdge(N1, N1, E11);
643     assertThat(networkAsMutableNetwork.removeNode(N1)).isTrue();
644     assertThat(network.nodes()).isEmpty();
645     assertThat(network.edges()).doesNotContain(E11);
646   }
647 
648   @Test
removeEdge_existingSelfLoopEdge()649   public void removeEdge_existingSelfLoopEdge() {
650     assume().that(graphIsMutable()).isTrue();
651     assume().that(network.allowsSelfLoops()).isTrue();
652 
653     addEdge(N1, N1, E11);
654     assertThat(networkAsMutableNetwork.removeEdge(E11)).isTrue();
655     assertThat(network.edges()).doesNotContain(E11);
656     assertThat(network.edgesConnecting(N1, N1)).isEmpty();
657   }
658 }
659