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   private static final String NODE_STRING = "Node";
32   private static final String EDGE_STRING = "Edge";
33 
34   enum EdgeType {
35     UNDIRECTED,
36     DIRECTED;
37   }
38 
TestUtil()39   private TestUtil() {}
40 
assertNodeNotInGraphErrorMessage(Throwable throwable)41   static void assertNodeNotInGraphErrorMessage(Throwable throwable) {
42     assertThat(throwable).hasMessageThat().startsWith(NODE_STRING);
43     assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH);
44   }
45 
assertEdgeNotInGraphErrorMessage(Throwable throwable)46   static void assertEdgeNotInGraphErrorMessage(Throwable throwable) {
47     assertThat(throwable).hasMessageThat().startsWith(EDGE_STRING);
48     assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH);
49   }
50 
assertStronglyEquivalent(Graph<?> graphA, Graph<?> graphB)51   static void assertStronglyEquivalent(Graph<?> graphA, Graph<?> graphB) {
52     // Properties not covered by equals()
53     assertThat(graphA.allowsSelfLoops()).isEqualTo(graphB.allowsSelfLoops());
54     assertThat(graphA.nodeOrder()).isEqualTo(graphB.nodeOrder());
55 
56     assertThat(graphA).isEqualTo(graphB);
57   }
58 
assertStronglyEquivalent(ValueGraph<?, ?> graphA, ValueGraph<?, ?> graphB)59   static void assertStronglyEquivalent(ValueGraph<?, ?> graphA, ValueGraph<?, ?> graphB) {
60     // Properties not covered by equals()
61     assertThat(graphA.allowsSelfLoops()).isEqualTo(graphB.allowsSelfLoops());
62     assertThat(graphA.nodeOrder()).isEqualTo(graphB.nodeOrder());
63 
64     assertThat(graphA).isEqualTo(graphB);
65   }
66 
assertStronglyEquivalent(Network<?, ?> networkA, Network<?, ?> networkB)67   static void assertStronglyEquivalent(Network<?, ?> networkA, Network<?, ?> networkB) {
68     // Properties not covered by equals()
69     assertThat(networkA.allowsParallelEdges()).isEqualTo(networkB.allowsParallelEdges());
70     assertThat(networkA.allowsSelfLoops()).isEqualTo(networkB.allowsSelfLoops());
71     assertThat(networkA.nodeOrder()).isEqualTo(networkB.nodeOrder());
72     assertThat(networkA.edgeOrder()).isEqualTo(networkB.edgeOrder());
73 
74     assertThat(networkA).isEqualTo(networkB);
75   }
76 
77   /**
78    * In some cases our graph implementations return custom sets that define their own size() and
79    * contains(). Verify that these sets are consistent with the elements of their iterator.
80    */
81   @CanIgnoreReturnValue
sanityCheckSet(Set<T> set)82   static <T> Set<T> sanityCheckSet(Set<T> set) {
83     assertThat(set).hasSize(Iterators.size(set.iterator()));
84     for (Object element : set) {
85       assertThat(set).contains(element);
86     }
87     assertThat(set).doesNotContain(new Object());
88     assertThat(set).isEqualTo(ImmutableSet.copyOf(set));
89     return set;
90   }
91 }
92