| 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.MapFeature.ALLOWS_NULL_KEYS; |
| 20 |
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES; |
| 21 |
|
| 22 |
import com.google.common.annotations.GwtCompatible; |
| 23 |
import com.google.common.collect.testing.AbstractMapTester; |
| 24 |
import com.google.common.collect.testing.features.CollectionSize; |
| 25 |
import com.google.common.collect.testing.features.MapFeature; |
| 26 |
import java.util.Collection; |
| 27 |
import java.util.Map.Entry; |
| 28 |
import org.junit.Ignore; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
@GwtCompatible |
| 37 |
@Ignore |
| 38 |
public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> { |
| 39 |
public void testHashCode() { |
| 40 |
int expectedHashCode = 0; |
| 41 |
for (Entry<K, V> entry : getSampleEntries()) { |
| 42 |
expectedHashCode += hash(entry); |
| 43 |
} |
| 44 |
assertEquals( |
| 45 |
"A Map's hashCode() should be the sum of those of its entries.", |
| 46 |
expectedHashCode, |
| 47 |
getMap().hashCode()); |
| 48 |
} |
| 49 |
|
| 50 |
@CollectionSize.Require(absent = CollectionSize.ZERO) |
| 51 |
@MapFeature.Require(ALLOWS_NULL_KEYS) |
| 52 |
public void testHashCode_containingNullKey() { |
| 53 |
Entry<K, V> entryWithNull = entry(null, v3()); |
| 54 |
runEntryWithNullTest(entryWithNull); |
| 55 |
} |
| 56 |
|
| 57 |
@CollectionSize.Require(absent = CollectionSize.ZERO) |
| 58 |
@MapFeature.Require(ALLOWS_NULL_VALUES) |
| 59 |
public void testHashCode_containingNullValue() { |
| 60 |
Entry<K, V> entryWithNull = entry(k3(), null); |
| 61 |
runEntryWithNullTest(entryWithNull); |
| 62 |
} |
| 63 |
|
| 64 |
private void runEntryWithNullTest(Entry<K, V> entryWithNull) { |
| 65 |
Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1); |
| 66 |
|
| 67 |
entries.add(entryWithNull); |
| 68 |
|
| 69 |
int expectedHashCode = 0; |
| 70 |
for (Entry<K, V> entry : entries) { |
| 71 |
expectedHashCode += hash(entry); |
| 72 |
} |
| 73 |
|
| 74 |
resetContainer(getSubjectGenerator().create(entries.toArray())); |
| 75 |
assertEquals( |
| 76 |
"A Map's hashCode() should be the sum of those of its entries (where " |
| 77 |
+ "a null element in an entry counts as having a hash of zero).", |
| 78 |
expectedHashCode, |
| 79 |
getMap().hashCode()); |
| 80 |
} |
| 81 |
|
| 82 |
private static int hash(Entry<?, ?> e) { |
| 83 |
return (e.getKey() == null ? 0 : e.getKey().hashCode()) |
| 84 |
^ (e.getValue() == null ? 0 : e.getValue().hashCode()); |
| 85 |
} |
| 86 |
} |