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.Helpers.getMethod;
020import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
021
022import com.google.common.annotations.GwtCompatible;
023import com.google.common.annotations.GwtIncompatible;
024import com.google.common.annotations.J2ktIncompatible;
025import com.google.common.collect.testing.features.CollectionFeature;
026import com.google.common.collect.testing.features.CollectionSize;
027import java.lang.reflect.Method;
028import java.util.Collection;
029import org.junit.Ignore;
030
031/**
032 * Tests {@link java.util.Set#hashCode}.
033 *
034 * @author George van den Driessche
035 */
036@GwtCompatible(emulated = true)
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 SetHashCodeTester<E> extends AbstractSetTester<E> {
041  public void testHashCode() {
042    int expectedHashCode = 0;
043    for (E element : getSampleElements()) {
044      expectedHashCode += ((element == null) ? 0 : element.hashCode());
045    }
046    assertEquals(
047        "A Set's hashCode() should be the sum of those of its elements.",
048        expectedHashCode,
049        getSet().hashCode());
050  }
051
052  @CollectionSize.Require(absent = CollectionSize.ZERO)
053  @CollectionFeature.Require(ALLOWS_NULL_VALUES)
054  public void testHashCode_containingNull() {
055    Collection<E> elements = getSampleElements(getNumElements() - 1);
056    int expectedHashCode = 0;
057    for (E element : elements) {
058      expectedHashCode += ((element == null) ? 0 : element.hashCode());
059    }
060
061    elements.add(null);
062    collection = getSubjectGenerator().create(elements.toArray());
063    assertEquals(
064        "A Set's hashCode() should be the sum of those of its elements (with "
065            + "a null element counting as having a hash of zero).",
066        expectedHashCode,
067        getSet().hashCode());
068  }
069
070  /**
071   * Returns the {@link Method} instances for the test methods in this class which call {@code
072   * hashCode()} on the set values so that set tests on unhashable objects can suppress it with
073   * {@code FeatureSpecificTestSuiteBuilder.suppressing()}.
074   */
075  @J2ktIncompatible
076  @GwtIncompatible // reflection
077  public static Method[] getHashCodeMethods() {
078    return new Method[] {
079      getMethod(SetHashCodeTester.class, "testHashCode"),
080      getMethod(SetHashCodeTester.class, "testHashCode_containingNull")
081    };
082  }
083}