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("test runners must not instantiate and run this directly, only via suites we build")
038// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
039@SuppressWarnings("JUnit4ClassUsedInJUnit3")
040public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> {
041  public void testHashCode() {
042    int expectedHashCode = 0;
043    for (Entry<K, V> entry : getSampleEntries()) {
044      expectedHashCode += hash(entry);
045    }
046    assertEquals(
047        "A Map's hashCode() should be the sum of those of its entries.",
048        expectedHashCode,
049        getMap().hashCode());
050  }
051
052  @CollectionSize.Require(absent = CollectionSize.ZERO)
053  @MapFeature.Require(ALLOWS_NULL_KEYS)
054  public void testHashCode_containingNullKey() {
055    Entry<K, V> entryWithNull = entry(null, v3());
056    runEntryWithNullTest(entryWithNull);
057  }
058
059  @CollectionSize.Require(absent = CollectionSize.ZERO)
060  @MapFeature.Require(ALLOWS_NULL_VALUES)
061  public void testHashCode_containingNullValue() {
062    Entry<K, V> entryWithNull = entry(k3(), null);
063    runEntryWithNullTest(entryWithNull);
064  }
065
066  private void runEntryWithNullTest(Entry<K, V> entryWithNull) {
067    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
068
069    entries.add(entryWithNull);
070
071    int expectedHashCode = 0;
072    for (Entry<K, V> entry : entries) {
073      expectedHashCode += hash(entry);
074    }
075
076    resetContainer(getSubjectGenerator().create(entries.toArray()));
077    assertEquals(
078        "A Map's hashCode() should be the sum of those of its entries (where "
079            + "a null element in an entry counts as having a hash of zero).",
080        expectedHashCode,
081        getMap().hashCode());
082  }
083
084  private static int hash(Entry<?, ?> e) {
085    return (e.getKey() == null ? 0 : e.getKey().hashCode())
086        ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
087  }
088}