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