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 com.google.common.testing.EqualsTester; 22 import org.junit.After; 23 import org.junit.Test; 24 25 /** 26 * Abstract base class for testing undirected implementations of the {@link Graph} interface. 27 * 28 * <p>This class is responsible for testing that an undirected implementation of {@link Graph} is 29 * correctly handling undirected edges. Implementation-dependent test cases are left to subclasses. 30 * Test cases that do not require the graph to be undirected are found in superclasses. 31 */ 32 public abstract class AbstractUndirectedGraphTest extends AbstractGraphTest { 33 34 @After validateUndirectedEdges()35 public void validateUndirectedEdges() { 36 for (Integer node : graph.nodes()) { 37 new EqualsTester() 38 .addEqualityGroup( 39 graph.predecessors(node), graph.successors(node), graph.adjacentNodes(node)) 40 .testEquals(); 41 } 42 } 43 44 @Test predecessors_oneEdge()45 public void predecessors_oneEdge() { 46 putEdge(N1, N2); 47 assertThat(graph.predecessors(N2)).containsExactly(N1); 48 assertThat(graph.predecessors(N1)).containsExactly(N2); 49 } 50 51 @Test successors_oneEdge()52 public void successors_oneEdge() { 53 putEdge(N1, N2); 54 assertThat(graph.successors(N1)).containsExactly(N2); 55 assertThat(graph.successors(N2)).containsExactly(N1); 56 } 57 58 @Test incidentEdges_oneEdge()59 public void incidentEdges_oneEdge() { 60 putEdge(N1, N2); 61 EndpointPair<Integer> expectedEndpoints = EndpointPair.unordered(N1, N2); 62 assertThat(graph.incidentEdges(N1)).containsExactly(expectedEndpoints); 63 assertThat(graph.incidentEdges(N2)).containsExactly(expectedEndpoints); 64 } 65 66 @Test inDegree_oneEdge()67 public void inDegree_oneEdge() { 68 putEdge(N1, N2); 69 assertThat(graph.inDegree(N2)).isEqualTo(1); 70 assertThat(graph.inDegree(N1)).isEqualTo(1); 71 } 72 73 @Test outDegree_oneEdge()74 public void outDegree_oneEdge() { 75 putEdge(N1, N2); 76 assertThat(graph.outDegree(N1)).isEqualTo(1); 77 assertThat(graph.outDegree(N2)).isEqualTo(1); 78 } 79 80 @Test hasEdgeConnecting_correct()81 public void hasEdgeConnecting_correct() { 82 putEdge(N1, N2); 83 assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(N1, N2))).isTrue(); 84 assertThat(graph.hasEdgeConnecting(EndpointPair.unordered(N2, N1))).isTrue(); 85 } 86 87 @Test hasEdgeConnecting_mismatch()88 public void hasEdgeConnecting_mismatch() { 89 putEdge(N1, N2); 90 assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(N1, N2))).isTrue(); 91 assertThat(graph.hasEdgeConnecting(EndpointPair.ordered(N2, N1))).isTrue(); 92 } 93 94 // Element Mutation 95 96 @Test addEdge_existingNodes()97 public void addEdge_existingNodes() { 98 // Adding nodes initially for safety (insulating from possible future 99 // modifications to proxy methods) 100 addNode(N1); 101 addNode(N2); 102 assertThat(putEdge(N1, N2)).isTrue(); 103 } 104 105 @Test addEdge_existingEdgeBetweenSameNodes()106 public void addEdge_existingEdgeBetweenSameNodes() { 107 putEdge(N1, N2); 108 assertThat(putEdge(N2, N1)).isFalse(); 109 } 110 111 @Test removeEdge_antiparallelEdges()112 public void removeEdge_antiparallelEdges() { 113 putEdge(N1, N2); 114 putEdge(N2, N1); // no-op 115 116 assertThat(graph.removeEdge(N1, N2)).isTrue(); 117 assertThat(graph.adjacentNodes(N1)).isEmpty(); 118 assertThat(graph.edges()).isEmpty(); 119 assertThat(graph.removeEdge(N2, N1)).isFalse(); 120 } 121 } 122