| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing.testers; |
| 18 |
|
| 19 |
import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES; |
| 20 |
|
| 21 |
import com.google.common.annotations.GwtCompatible; |
| 22 |
import com.google.common.annotations.GwtIncompatible; |
| 23 |
import com.google.common.collect.testing.Helpers; |
| 24 |
import com.google.common.collect.testing.features.CollectionFeature; |
| 25 |
import com.google.common.collect.testing.features.CollectionSize; |
| 26 |
import java.lang.reflect.Method; |
| 27 |
import java.util.Collection; |
| 28 |
import org.junit.Ignore; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@GwtCompatible(emulated = true) |
| 36 |
@Ignore |
| 37 |
public class SetHashCodeTester<E> extends AbstractSetTester<E> { |
| 38 |
public void testHashCode() { |
| 39 |
int expectedHashCode = 0; |
| 40 |
for (E element : getSampleElements()) { |
| 41 |
expectedHashCode += ((element == null) ? 0 : element.hashCode()); |
| 42 |
} |
| 43 |
assertEquals( |
| 44 |
"A Set's hashCode() should be the sum of those of its elements.", |
| 45 |
expectedHashCode, |
| 46 |
getSet().hashCode()); |
| 47 |
} |
| 48 |
|
| 49 |
@CollectionSize.Require(absent = CollectionSize.ZERO) |
| 50 |
@CollectionFeature.Require(ALLOWS_NULL_VALUES) |
| 51 |
public void testHashCode_containingNull() { |
| 52 |
Collection<E> elements = getSampleElements(getNumElements() - 1); |
| 53 |
int expectedHashCode = 0; |
| 54 |
for (E element : elements) { |
| 55 |
expectedHashCode += ((element == null) ? 0 : element.hashCode()); |
| 56 |
} |
| 57 |
|
| 58 |
elements.add(null); |
| 59 |
collection = getSubjectGenerator().create(elements.toArray()); |
| 60 |
assertEquals( |
| 61 |
"A Set's hashCode() should be the sum of those of its elements (with " |
| 62 |
+ "a null element counting as having a hash of zero).", |
| 63 |
expectedHashCode, |
| 64 |
getSet().hashCode()); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@GwtIncompatible |
| 73 |
public static Method[] getHashCodeMethods() { |
| 74 |
return new Method[] { |
| 75 |
Helpers.getMethod(SetHashCodeTester.class, "testHashCode"), |
| 76 |
Helpers.getMethod(SetHashCodeTester.class, "testHashCode_containingNull") |
| 77 |
}; |
| 78 |
} |
| 79 |
} |