Java

Форк
0
205 строк · 6.9 Кб
1
/*
2
 * Copyright (C) 2015 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.CollectionSize.ZERO;
20
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
21
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
22
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
24

25
import com.google.common.annotations.GwtCompatible;
26
import com.google.common.collect.testing.AbstractMapTester;
27
import com.google.common.collect.testing.features.CollectionSize;
28
import com.google.common.collect.testing.features.MapFeature;
29
import java.util.Map;
30
import org.junit.Ignore;
31

32
/**
33
 * A generic JUnit test which tests {@link Map#compute}. Can't be invoked directly; please see
34
 * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
35
 *
36
 * @author Louis Wasserman
37
 */
38
@GwtCompatible
39
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
40
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
41
public class MapComputeTester<K, V> extends AbstractMapTester<K, V> {
42
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
43
  public void testCompute_absentToPresent() {
44
    assertEquals(
45
        "Map.compute(absent, functionReturningValue) should return value",
46
        v3(),
47
        getMap()
48
            .compute(
49
                k3(),
50
                (k, v) -> {
51
                  assertEquals(k3(), k);
52
                  assertNull(v);
53
                  return v3();
54
                }));
55
    expectAdded(e3());
56
    assertEquals(getNumElements() + 1, getMap().size());
57
  }
58

59
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
60
  public void testCompute_absentToAbsent() {
61
    assertNull(
62
        "Map.compute(absent, functionReturningNull) should return null",
63
        getMap()
64
            .compute(
65
                k3(),
66
                (k, v) -> {
67
                  assertEquals(k3(), k);
68
                  assertNull(v);
69
                  return null;
70
                }));
71
    expectUnchanged();
72
    assertEquals(getNumElements(), getMap().size());
73
  }
74

75
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
76
  @CollectionSize.Require(absent = ZERO)
77
  public void testCompute_presentToPresent() {
78
    assertEquals(
79
        "Map.compute(present, functionReturningValue) should return new value",
80
        v3(),
81
        getMap()
82
            .compute(
83
                k0(),
84
                (k, v) -> {
85
                  assertEquals(k0(), k);
86
                  assertEquals(v0(), v);
87
                  return v3();
88
                }));
89
    expectReplacement(entry(k0(), v3()));
90
    assertEquals(getNumElements(), getMap().size());
91
  }
92

93
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
94
  @CollectionSize.Require(absent = ZERO)
95
  public void testCompute_presentToAbsent() {
96
    assertNull(
97
        "Map.compute(present, functionReturningNull) should return null",
98
        getMap()
99
            .compute(
100
                k0(),
101
                (k, v) -> {
102
                  assertEquals(k0(), k);
103
                  assertEquals(v0(), v);
104
                  return null;
105
                }));
106
    expectMissing(e0());
107
    expectMissingKeys(k0());
108
    assertEquals(getNumElements() - 1, getMap().size());
109
  }
110

111
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
112
  @CollectionSize.Require(absent = ZERO)
113
  public void testCompute_presentNullToPresentNonnull() {
114
    initMapWithNullValue();
115
    V value = getValueForNullKey();
116
    assertEquals(
117
        "Map.compute(presentMappedToNull, functionReturningValue) should return new value",
118
        value,
119
        getMap()
120
            .compute(
121
                getKeyForNullValue(),
122
                (k, v) -> {
123
                  assertEquals(getKeyForNullValue(), k);
124
                  assertNull(v);
125
                  return value;
126
                }));
127
    expectReplacement(entry(getKeyForNullValue(), value));
128
    assertEquals(getNumElements(), getMap().size());
129
  }
130

131
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
132
  @CollectionSize.Require(absent = ZERO)
133
  public void testCompute_presentNullToNull() {
134
    // The spec is somewhat ambiguous about this case, but the actual default implementation
135
    // in Map will remove a present null.
136
    initMapWithNullValue();
137
    assertNull(
138
        "Map.compute(presentMappedToNull, functionReturningNull) should return null",
139
        getMap()
140
            .compute(
141
                getKeyForNullValue(),
142
                (k, v) -> {
143
                  assertEquals(getKeyForNullValue(), k);
144
                  assertNull(v);
145
                  return null;
146
                }));
147
    expectMissingKeys(getKeyForNullValue());
148
    assertEquals(getNumElements() - 1, getMap().size());
149
  }
150

151
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
152
  @CollectionSize.Require(absent = ZERO)
153
  public void testCompute_nullKeyPresentToPresent() {
154
    initMapWithNullKey();
155
    assertEquals(
156
        "Map.compute(present, functionReturningValue) should return new value",
157
        v3(),
158
        getMap()
159
            .compute(
160
                null,
161
                (k, v) -> {
162
                  assertNull(k);
163
                  assertEquals(getValueForNullKey(), v);
164
                  return v3();
165
                }));
166
    assertEquals(getNumElements(), getMap().size());
167
  }
168

169
  static class ExpectedException extends RuntimeException {}
170

171
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
172
  @CollectionSize.Require(absent = ZERO)
173
  public void testCompute_presentFunctionThrows() {
174
    try {
175
      getMap()
176
          .compute(
177
              k0(),
178
              (k, v) -> {
179
                assertEquals(k0(), k);
180
                assertEquals(v0(), v);
181
                throw new ExpectedException();
182
              });
183
      fail("Expected ExpectedException");
184
    } catch (ExpectedException expected) {
185
    }
186
    expectUnchanged();
187
  }
188

189
  @MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
190
  public void testCompute_absentFunctionThrows() {
191
    try {
192
      getMap()
193
          .compute(
194
              k3(),
195
              (k, v) -> {
196
                assertEquals(k3(), k);
197
                assertNull(v);
198
                throw new ExpectedException();
199
              });
200
      fail("Expected ExpectedException");
201
    } catch (ExpectedException expected) {
202
    }
203
    expectUnchanged();
204
  }
205
}
206

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

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

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

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