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