001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
020import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.collect.testing.AbstractMapTester;
024import com.google.common.collect.testing.features.CollectionSize;
025import com.google.common.collect.testing.features.MapFeature;
026import java.util.Collection;
027import java.util.Map.Entry;
028
029/**
030 * Tests {@link java.util.Map#hashCode}.
031 *
032 * @author George van den Driessche
033 * @author Chris Povirk
034 */
035@GwtCompatible
036public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> {
037  public void testHashCode() {
038    int expectedHashCode = 0;
039    for (Entry<K, V> entry : getSampleEntries()) {
040      expectedHashCode += hash(entry);
041    }
042    assertEquals(
043        "A Map's hashCode() should be the sum of those of its entries.",
044        expectedHashCode,
045        getMap().hashCode());
046  }
047
048  @CollectionSize.Require(absent = CollectionSize.ZERO)
049  @MapFeature.Require(ALLOWS_NULL_KEYS)
050  public void testHashCode_containingNullKey() {
051    Entry<K, V> entryWithNull = entry(null, v3());
052    runEntryWithNullTest(entryWithNull);
053  }
054
055  @CollectionSize.Require(absent = CollectionSize.ZERO)
056  @MapFeature.Require(ALLOWS_NULL_VALUES)
057  public void testHashCode_containingNullValue() {
058    Entry<K, V> entryWithNull = entry(k3(), null);
059    runEntryWithNullTest(entryWithNull);
060  }
061
062  private void runEntryWithNullTest(Entry<K, V> entryWithNull) {
063    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
064
065    entries.add(entryWithNull);
066
067    int expectedHashCode = 0;
068    for (Entry<K, V> entry : entries) {
069      expectedHashCode += hash(entry);
070    }
071
072    resetContainer(getSubjectGenerator().create(entries.toArray()));
073    assertEquals(
074        "A Map's hashCode() should be the sum of those of its entries (where "
075            + "a null element in an entry counts as having a hash of zero).",
076        expectedHashCode,
077        getMap().hashCode());
078  }
079
080  private static int hash(Entry<?, ?> e) {
081    return (e.getKey() == null ? 0 : e.getKey().hashCode())
082        ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
083  }
084}