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 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 /** Tests for an undirected {@link ConfigurableMutableGraph} allowing self-loops. */ 26 @RunWith(JUnit4.class) 27 public class ConfigurableUndirectedGraphTest extends ConfigurableSimpleUndirectedGraphTest { 28 29 @Override createGraph()30 public MutableGraph<Integer> createGraph() { 31 return GraphBuilder.undirected().allowsSelfLoops(true).build(); 32 } 33 34 @Test adjacentNodes_selfLoop()35 public void adjacentNodes_selfLoop() { 36 putEdge(N1, N1); 37 putEdge(N1, N2); 38 assertThat(graph.adjacentNodes(N1)).containsExactly(N1, N2); 39 } 40 41 @Test predecessors_selfLoop()42 public void predecessors_selfLoop() { 43 putEdge(N1, N1); 44 assertThat(graph.predecessors(N1)).containsExactly(N1); 45 putEdge(N1, N2); 46 assertThat(graph.predecessors(N1)).containsExactly(N1, N2); 47 } 48 49 @Test successors_selfLoop()50 public void successors_selfLoop() { 51 putEdge(N1, N1); 52 assertThat(graph.successors(N1)).containsExactly(N1); 53 putEdge(N2, N1); 54 assertThat(graph.successors(N1)).containsExactly(N1, N2); 55 } 56 57 @Test incidentEdges_selfLoop()58 public void incidentEdges_selfLoop() { 59 putEdge(N1, N1); 60 assertThat(graph.incidentEdges(N1)).containsExactly(EndpointPair.unordered(N1, N1)); 61 putEdge(N1, N2); 62 assertThat(graph.incidentEdges(N1)) 63 .containsExactly(EndpointPair.unordered(N1, N1), EndpointPair.unordered(N1, N2)); 64 } 65 66 @Test degree_selfLoop()67 public void degree_selfLoop() { 68 putEdge(N1, N1); 69 assertThat(graph.degree(N1)).isEqualTo(2); 70 putEdge(N1, N2); 71 assertThat(graph.degree(N1)).isEqualTo(3); 72 } 73 74 @Test inDegree_selfLoop()75 public void inDegree_selfLoop() { 76 putEdge(N1, N1); 77 assertThat(graph.inDegree(N1)).isEqualTo(2); 78 putEdge(N1, N2); 79 assertThat(graph.inDegree(N1)).isEqualTo(3); 80 } 81 82 @Test outDegree_selfLoop()83 public void outDegree_selfLoop() { 84 putEdge(N1, N1); 85 assertThat(graph.outDegree(N1)).isEqualTo(2); 86 putEdge(N2, N1); 87 assertThat(graph.outDegree(N1)).isEqualTo(3); 88 } 89 90 @Override 91 @Test addEdge_selfLoop()92 public void addEdge_selfLoop() { 93 assertThat(putEdge(N1, N1)).isTrue(); 94 assertThat(graph.adjacentNodes(N1)).containsExactly(N1); 95 } 96 97 @Test addEdge_existingSelfLoopEdgeBetweenSameNodes()98 public void addEdge_existingSelfLoopEdgeBetweenSameNodes() { 99 putEdge(N1, N1); 100 assertThat(putEdge(N1, N1)).isFalse(); 101 } 102 103 @Test removeNode_existingNodeWithSelfLoopEdge()104 public void removeNode_existingNodeWithSelfLoopEdge() { 105 addNode(N1); 106 putEdge(N1, N1); 107 assertThat(graph.removeNode(N1)).isTrue(); 108 assertThat(graph.nodes()).isEmpty(); 109 } 110 111 @Test removeEdge_existingSelfLoopEdge()112 public void removeEdge_existingSelfLoopEdge() { 113 putEdge(N1, N1); 114 assertThat(graph.removeEdge(N1, N1)).isTrue(); 115 assertThat(graph.nodes()).containsExactly(N1); 116 assertThat(graph.adjacentNodes(N1)).isEmpty(); 117 } 118 } 119