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