View Javadoc
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.MapFeature.ALLOWS_NULL_KEYS;
20  import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.AbstractMapTester;
24  import com.google.common.collect.testing.features.CollectionSize;
25  import com.google.common.collect.testing.features.MapFeature;
26  import java.util.Collection;
27  import java.util.Map;
28  
29  /**
30   * Tests {@link java.util.Map#hashCode}.
31   *
32   * @author George van den Driessche
33   * @author Chris Povirk
34   */
35  @GwtCompatible
36  public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> {
37    public void testHashCode() {
38      int expectedHashCode = 0;
39      for (Map.Entry<K, V> entry : getSampleEntries()) {
40        expectedHashCode += hash(entry);
41      }
42      assertEquals(
43          "A Map's hashCode() should be the sum of those of its entries.",
44          expectedHashCode,
45          getMap().hashCode());
46    }
47  
48    @CollectionSize.Require(absent = CollectionSize.ZERO)
49    @MapFeature.Require(ALLOWS_NULL_KEYS)
50    public void testHashCode_containingNullKey() {
51      Map.Entry<K, V> entryWithNull = entry(null, v3());
52      runEntryWithNullTest(entryWithNull);
53    }
54  
55    @CollectionSize.Require(absent = CollectionSize.ZERO)
56    @MapFeature.Require(ALLOWS_NULL_VALUES)
57    public void testHashCode_containingNullValue() {
58      Map.Entry<K, V> entryWithNull = entry(k3(), null);
59      runEntryWithNullTest(entryWithNull);
60    }
61  
62    private void runEntryWithNullTest(Map.Entry<K, V> entryWithNull) {
63      Collection<Map.Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
64  
65      entries.add(entryWithNull);
66  
67      int expectedHashCode = 0;
68      for (Map.Entry<K, V> entry : entries) {
69        expectedHashCode += hash(entry);
70      }
71  
72      resetContainer(getSubjectGenerator().create(entries.toArray()));
73      assertEquals(
74          "A Map's hashCode() should be the sum of those of its entries (where "
75              + "a null element in an entry counts as having a hash of zero).",
76          expectedHashCode,
77          getMap().hashCode());
78    }
79  
80    private static int hash(Map.Entry<?, ?> e) {
81      return (e.getKey() == null ? 0 : e.getKey().hashCode())
82          ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
83    }
84  }