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 import static org.junit.Assert.fail; 22 23 import java.util.Set; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** 29 * Tests for a directed {@link ConfigurableMutableGraph}, creating a simple directed graph 30 * (self-loop edges are not allowed). 31 */ 32 @RunWith(JUnit4.class) 33 public class ConfigurableSimpleDirectedGraphTest extends AbstractDirectedGraphTest { 34 35 @Override createGraph()36 public MutableGraph<Integer> createGraph() { 37 return GraphBuilder.directed().allowsSelfLoops(false).build(); 38 } 39 40 @Override 41 @Test nodes_checkReturnedSetMutability()42 public void nodes_checkReturnedSetMutability() { 43 Set<Integer> nodes = graph.nodes(); 44 try { 45 nodes.add(N2); 46 fail(ERROR_MODIFIABLE_SET); 47 } catch (UnsupportedOperationException e) { 48 addNode(N1); 49 assertThat(graph.nodes()).containsExactlyElementsIn(nodes); 50 } 51 } 52 53 @Override 54 @Test adjacentNodes_checkReturnedSetMutability()55 public void adjacentNodes_checkReturnedSetMutability() { 56 addNode(N1); 57 Set<Integer> adjacentNodes = graph.adjacentNodes(N1); 58 try { 59 adjacentNodes.add(N2); 60 fail(ERROR_MODIFIABLE_SET); 61 } catch (UnsupportedOperationException e) { 62 putEdge(N1, N2); 63 assertThat(graph.adjacentNodes(N1)).containsExactlyElementsIn(adjacentNodes); 64 } 65 } 66 67 @Override 68 @Test predecessors_checkReturnedSetMutability()69 public void predecessors_checkReturnedSetMutability() { 70 addNode(N2); 71 Set<Integer> predecessors = graph.predecessors(N2); 72 try { 73 predecessors.add(N1); 74 fail(ERROR_MODIFIABLE_SET); 75 } catch (UnsupportedOperationException e) { 76 putEdge(N1, N2); 77 assertThat(graph.predecessors(N2)).containsExactlyElementsIn(predecessors); 78 } 79 } 80 81 @Override 82 @Test successors_checkReturnedSetMutability()83 public void successors_checkReturnedSetMutability() { 84 addNode(N1); 85 Set<Integer> successors = graph.successors(N1); 86 try { 87 successors.add(N2); 88 fail(ERROR_MODIFIABLE_SET); 89 } catch (UnsupportedOperationException e) { 90 putEdge(N1, N2); 91 assertThat(successors).containsExactlyElementsIn(graph.successors(N1)); 92 } 93 } 94 95 @Override 96 @Test incidentEdges_checkReturnedSetMutability()97 public void incidentEdges_checkReturnedSetMutability() { 98 addNode(N1); 99 Set<EndpointPair<Integer>> incidentEdges = graph.incidentEdges(N1); 100 try { 101 incidentEdges.add(EndpointPair.ordered(N1, N2)); 102 fail(ERROR_MODIFIABLE_SET); 103 } catch (UnsupportedOperationException e) { 104 putEdge(N1, N2); 105 assertThat(incidentEdges).containsExactlyElementsIn(graph.incidentEdges(N1)); 106 } 107 } 108 109 // Element Mutation 110 111 @Test addEdge_selfLoop()112 public void addEdge_selfLoop() { 113 try { 114 putEdge(N1, N1); 115 fail(ERROR_ADDED_SELF_LOOP); 116 } catch (IllegalArgumentException e) { 117 assertThat(e.getMessage()).contains(ERROR_SELF_LOOP); 118 } 119 } 120 121 /** 122 * This test checks an implementation dependent feature. It tests that the method {@code addEdge} 123 * will silently add the missing nodes to the graph, then add the edge connecting them. We are not 124 * using the proxy methods here as we want to test {@code addEdge} when the end-points are not 125 * elements of the graph. 126 */ 127 @Test addEdge_nodesNotInGraph()128 public void addEdge_nodesNotInGraph() { 129 graph.addNode(N1); 130 assertTrue(graph.putEdge(N1, N5)); 131 assertTrue(graph.putEdge(N4, N1)); 132 assertTrue(graph.putEdge(N2, N3)); 133 assertThat(graph.nodes()).containsExactly(N1, N5, N4, N2, N3).inOrder(); 134 assertThat(graph.successors(N1)).containsExactly(N5); 135 assertThat(graph.successors(N2)).containsExactly(N3); 136 assertThat(graph.successors(N3)).isEmpty(); 137 assertThat(graph.successors(N4)).containsExactly(N1); 138 assertThat(graph.successors(N5)).isEmpty(); 139 } 140 } 141