Java

Форк
0
127 строк · 4.3 Кб
1
/*
2
 * Copyright (C) 2016 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.CollectionFeature.KNOWN_ORDER;
20
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
22

23
import com.google.common.annotations.GwtCompatible;
24
import com.google.common.collect.testing.AbstractMapTester;
25
import com.google.common.collect.testing.Helpers;
26
import com.google.common.collect.testing.SampleElements;
27
import com.google.common.collect.testing.features.CollectionFeature;
28
import com.google.common.collect.testing.features.CollectionSize;
29
import com.google.common.collect.testing.features.MapFeature;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Map.Entry;
33
import org.junit.Ignore;
34

35
/**
36
 * A generic JUnit test which tests {@code replaceAll()} operations on a map. Can't be invoked
37
 * directly; please see {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
38
 *
39
 * @author Louis Wasserman
40
 */
41
@GwtCompatible
42
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
43
@SuppressWarnings("JUnit4ClassUsedInJUnit3")
44
public class MapReplaceAllTester<K, V> extends AbstractMapTester<K, V> {
45
  private SampleElements<K> keys() {
46
    return new SampleElements<>(k0(), k1(), k2(), k3(), k4());
47
  }
48

49
  private SampleElements<V> values() {
50
    return new SampleElements<>(v0(), v1(), v2(), v3(), v4());
51
  }
52

53
  @MapFeature.Require(SUPPORTS_PUT)
54
  public void testReplaceAllRotate() {
55
    getMap()
56
        .replaceAll(
57
            (K k, V v) -> {
58
              int index = keys().asList().indexOf(k);
59
              return values().asList().get(index + 1);
60
            });
61
    List<Entry<K, V>> expectedEntries = new ArrayList<>();
62
    for (Entry<K, V> entry : getSampleEntries()) {
63
      int index = keys().asList().indexOf(entry.getKey());
64
      expectedEntries.add(Helpers.mapEntry(entry.getKey(), values().asList().get(index + 1)));
65
    }
66
    expectContents(expectedEntries);
67
  }
68

69
  @MapFeature.Require(SUPPORTS_PUT)
70
  @CollectionFeature.Require(KNOWN_ORDER)
71
  public void testReplaceAllPreservesOrder() {
72
    getMap()
73
        .replaceAll(
74
            (K k, V v) -> {
75
              int index = keys().asList().indexOf(k);
76
              return values().asList().get(index + 1);
77
            });
78
    List<Entry<K, V>> orderedEntries = getOrderedElements();
79
    int index = 0;
80
    for (K key : getMap().keySet()) {
81
      assertEquals(orderedEntries.get(index).getKey(), key);
82
      index++;
83
    }
84
  }
85

86
  @MapFeature.Require(absent = SUPPORTS_PUT)
87
  @CollectionSize.Require(absent = ZERO)
88
  public void testReplaceAll_unsupported() {
89
    try {
90
      getMap()
91
          .replaceAll(
92
              (K k, V v) -> {
93
                int index = keys().asList().indexOf(k);
94
                return values().asList().get(index + 1);
95
              });
96
      fail(
97
          "replaceAll() should throw UnsupportedOperation if a map does "
98
              + "not support it and is not empty.");
99
    } catch (UnsupportedOperationException expected) {
100
    }
101
    expectUnchanged();
102
  }
103

104
  @MapFeature.Require(absent = SUPPORTS_PUT)
105
  @CollectionSize.Require(ZERO)
106
  public void testReplaceAll_unsupportedByEmptyCollection() {
107
    try {
108
      getMap()
109
          .replaceAll(
110
              (K k, V v) -> {
111
                int index = keys().asList().indexOf(k);
112
                return values().asList().get(index + 1);
113
              });
114
    } catch (UnsupportedOperationException tolerated) {
115
    }
116
    expectUnchanged();
117
  }
118

119
  @MapFeature.Require(absent = SUPPORTS_PUT)
120
  public void testReplaceAll_unsupportedNoOpFunction() {
121
    try {
122
      getMap().replaceAll((K k, V v) -> v);
123
    } catch (UnsupportedOperationException tolerated) {
124
    }
125
    expectUnchanged();
126
  }
127
}
128

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

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

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

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