Debian Codegraph @

guava-testlib/src/com/google/common/collect/testing/testers/SetHashCodeTester.java

raw
1 /*
2 * Copyright (C) 2008 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.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.collect.testing.Helpers;
24 import com.google.common.collect.testing.features.CollectionFeature;
25 import com.google.common.collect.testing.features.CollectionSize;
26 import java.lang.reflect.Method;
27 import java.util.Collection;
28 import org.junit.Ignore;
29
30 /**
31 * Tests {@link java.util.Set#hashCode}.
32 *
33 * @author George van den Driessche
34 */
35 @GwtCompatible(emulated = true)
36 @Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
37 public class SetHashCodeTester<E> extends AbstractSetTester<E> {
38 public void testHashCode() {
39 int expectedHashCode = 0;
40 for (E element : getSampleElements()) {
41 expectedHashCode += ((element == null) ? 0 : element.hashCode());
42 }
43 assertEquals(
44 "A Set's hashCode() should be the sum of those of its elements.",
45 expectedHashCode,
46 getSet().hashCode());
47 }
48
49 @CollectionSize.Require(absent = CollectionSize.ZERO)
50 @CollectionFeature.Require(ALLOWS_NULL_VALUES)
51 public void testHashCode_containingNull() {
52 Collection<E> elements = getSampleElements(getNumElements() - 1);
53 int expectedHashCode = 0;
54 for (E element : elements) {
55 expectedHashCode += ((element == null) ? 0 : element.hashCode());
56 }
57
58 elements.add(null);
59 collection = getSubjectGenerator().create(elements.toArray());
60 assertEquals(
61 "A Set's hashCode() should be the sum of those of its elements (with "
62 + "a null element counting as having a hash of zero).",
63 expectedHashCode,
64 getSet().hashCode());
65 }
66
67 /**
68 * Returns the {@link Method} instances for the test methods in this class which call {@code
69 * hashCode()} on the set values so that set tests on unhashable objects can suppress it with
70 * {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
71 */
72 @GwtIncompatible // reflection
73 public static Method[] getHashCodeMethods() {
74 return new Method[] {
75 Helpers.getMethod(SetHashCodeTester.class, "testHashCode"),
76 Helpers.getMethod(SetHashCodeTester.class, "testHashCode_containingNull")
77 };
78 }
79 }