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;
028import org.junit.Ignore;
029
030/**
031 * Tests {@link java.util.Map#hashCode}.
032 *
033 * @author George van den Driessche
034 * @author Chris Povirk
035 */
036@GwtCompatible
037@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
038public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> {
039  public void testHashCode() {
040    int expectedHashCode = 0;
041    for (Entry<K, V> entry : getSampleEntries()) {
042      expectedHashCode += hash(entry);
043    }
044    assertEquals(
045        "A Map's hashCode() should be the sum of those of its entries.",
046        expectedHashCode,
047        getMap().hashCode());
048  }
049
050  @CollectionSize.Require(absent = CollectionSize.ZERO)
051  @MapFeature.Require(ALLOWS_NULL_KEYS)
052  public void testHashCode_containingNullKey() {
053    Entry<K, V> entryWithNull = entry(null, v3());
054    runEntryWithNullTest(entryWithNull);
055  }
056
057  @CollectionSize.Require(absent = CollectionSize.ZERO)
058  @MapFeature.Require(ALLOWS_NULL_VALUES)
059  public void testHashCode_containingNullValue() {
060    Entry<K, V> entryWithNull = entry(k3(), null);
061    runEntryWithNullTest(entryWithNull);
062  }
063
064  private void runEntryWithNullTest(Entry<K, V> entryWithNull) {
065    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
066
067    entries.add(entryWithNull);
068
069    int expectedHashCode = 0;
070    for (Entry<K, V> entry : entries) {
071      expectedHashCode += hash(entry);
072    }
073
074    resetContainer(getSubjectGenerator().create(entries.toArray()));
075    assertEquals(
076        "A Map's hashCode() should be the sum of those of its entries (where "
077            + "a null element in an entry counts as having a hash of zero).",
078        expectedHashCode,
079        getMap().hashCode());
080  }
081
082  private static int hash(Entry<?, ?> e) {
083    return (e.getKey() == null ? 0 : e.getKey().hashCode())
084        ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
085  }
086}