Java

Форк
0
87 строк · 3.0 Кб
1
/*
2
 * Copyright (C) 2008 The Guava Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
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
 * Tests {@link java.util.Map#hashCode}.
32
 *
33
 * @author George van den Driessche
34
 * @author Chris Povirk
35
 */
36
@GwtCompatible
37
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
38
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
39
public class MapHashCodeTester<K, V> extends AbstractMapTester<K, V> {
40
  public void testHashCode() {
41
    int expectedHashCode = 0;
42
    for (Entry<K, V> entry : getSampleEntries()) {
43
      expectedHashCode += hash(entry);
44
    }
45
    assertEquals(
46
        "A Map's hashCode() should be the sum of those of its entries.",
47
        expectedHashCode,
48
        getMap().hashCode());
49
  }
50

51
  @CollectionSize.Require(absent = CollectionSize.ZERO)
52
  @MapFeature.Require(ALLOWS_NULL_KEYS)
53
  public void testHashCode_containingNullKey() {
54
    Entry<K, V> entryWithNull = entry(null, v3());
55
    runEntryWithNullTest(entryWithNull);
56
  }
57

58
  @CollectionSize.Require(absent = CollectionSize.ZERO)
59
  @MapFeature.Require(ALLOWS_NULL_VALUES)
60
  public void testHashCode_containingNullValue() {
61
    Entry<K, V> entryWithNull = entry(k3(), null);
62
    runEntryWithNullTest(entryWithNull);
63
  }
64

65
  private void runEntryWithNullTest(Entry<K, V> entryWithNull) {
66
    Collection<Entry<K, V>> entries = getSampleEntries(getNumEntries() - 1);
67

68
    entries.add(entryWithNull);
69

70
    int expectedHashCode = 0;
71
    for (Entry<K, V> entry : entries) {
72
      expectedHashCode += hash(entry);
73
    }
74

75
    resetContainer(getSubjectGenerator().create(entries.toArray()));
76
    assertEquals(
77
        "A Map's hashCode() should be the sum of those of its entries (where "
78
            + "a null element in an entry counts as having a hash of zero).",
79
        expectedHashCode,
80
        getMap().hashCode());
81
  }
82

83
  private static int hash(Entry<?, ?> e) {
84
    return (e.getKey() == null ? 0 : e.getKey().hashCode())
85
        ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
86
  }
87
}
88

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.