1 /* 2 * Copyright (C) 2016 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.collect.ImmutableSet; 22 import com.google.common.collect.Iterators; 23 import com.google.errorprone.annotations.CanIgnoreReturnValue; 24 import java.util.Set; 25 26 /** Utility methods used in various common.graph tests. */ 27 final class TestUtil { 28 static final String ERROR_ELEMENT_NOT_IN_GRAPH = "not an element of this graph"; 29 static final String ERROR_NODE_NOT_IN_GRAPH = 30 "Should not be allowed to pass a node that is not an element of the graph."; 31 static final String ERROR_ELEMENT_REMOVED = "used to generate this set"; 32 private static final String NODE_STRING = "Node"; 33 private static final String EDGE_STRING = "Edge"; 34 35 enum EdgeType { 36 UNDIRECTED, 37 DIRECTED; 38 } 39 TestUtil()40 private TestUtil() {} 41 assertNodeNotInGraphErrorMessage(Throwable throwable)42 static void assertNodeNotInGraphErrorMessage(Throwable throwable) { 43 assertThat(throwable).hasMessageThat().startsWith(NODE_STRING); 44 assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH); 45 } 46 assertEdgeNotInGraphErrorMessage(Throwable throwable)47 static void assertEdgeNotInGraphErrorMessage(Throwable throwable) { 48 assertThat(throwable).hasMessageThat().startsWith(EDGE_STRING); 49 assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH); 50 } 51 assertNodeRemovedFromGraphErrorMessage(Throwable throwable)52 static void assertNodeRemovedFromGraphErrorMessage(Throwable throwable) { 53 assertThat(throwable).hasMessageThat().startsWith(NODE_STRING); 54 assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_REMOVED); 55 } 56 assertEdgeRemovedFromGraphErrorMessage(Throwable throwable)57 static void assertEdgeRemovedFromGraphErrorMessage(Throwable throwable) { 58 assertThat(throwable).hasMessageThat().startsWith(EDGE_STRING); 59 assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_REMOVED); 60 } 61 assertStronglyEquivalent(Graph<?> graphA, Graph<?> graphB)62 static void assertStronglyEquivalent(Graph<?> graphA, Graph<?> graphB) { 63 // Properties not covered by equals() 64 assertThat(graphA.allowsSelfLoops()).isEqualTo(graphB.allowsSelfLoops()); 65 assertThat(graphA.nodeOrder()).isEqualTo(graphB.nodeOrder()); 66 67 assertThat(graphA).isEqualTo(graphB); 68 } 69 assertStronglyEquivalent(ValueGraph<?, ?> graphA, ValueGraph<?, ?> graphB)70 static void assertStronglyEquivalent(ValueGraph<?, ?> graphA, ValueGraph<?, ?> graphB) { 71 // Properties not covered by equals() 72 assertThat(graphA.allowsSelfLoops()).isEqualTo(graphB.allowsSelfLoops()); 73 assertThat(graphA.nodeOrder()).isEqualTo(graphB.nodeOrder()); 74 75 assertThat(graphA).isEqualTo(graphB); 76 } 77 assertStronglyEquivalent(Network<?, ?> networkA, Network<?, ?> networkB)78 static void assertStronglyEquivalent(Network<?, ?> networkA, Network<?, ?> networkB) { 79 // Properties not covered by equals() 80 assertThat(networkA.allowsParallelEdges()).isEqualTo(networkB.allowsParallelEdges()); 81 assertThat(networkA.allowsSelfLoops()).isEqualTo(networkB.allowsSelfLoops()); 82 assertThat(networkA.nodeOrder()).isEqualTo(networkB.nodeOrder()); 83 assertThat(networkA.edgeOrder()).isEqualTo(networkB.edgeOrder()); 84 85 assertThat(networkA).isEqualTo(networkB); 86 } 87 88 /** 89 * In some cases our graph implementations return custom sets that define their own size() and 90 * contains(). Verify that these sets are consistent with the elements of their iterator. 91 */ 92 @CanIgnoreReturnValue sanityCheckSet(Set<T> set)93 static <T> Set<T> sanityCheckSet(Set<T> set) { 94 assertThat(set).hasSize(Iterators.size(set.iterator())); 95 for (Object element : set) { 96 assertThat(set).contains(element); 97 } 98 assertThat(set).doesNotContain(new Object()); 99 assertThat(set).isEqualTo(ImmutableSet.copyOf(set)); 100 return set; 101 } 102 } 103