guava

Форк
0
/
CollectionRemoveAllTester.java 
207 строк · 7.7 Кб
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.CollectionFeature.ALLOWS_NULL_QUERIES;
20
import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
21
import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
23
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
24
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
25

26
import com.google.common.annotations.GwtCompatible;
27
import com.google.common.collect.testing.AbstractCollectionTester;
28
import com.google.common.collect.testing.MinimalCollection;
29
import com.google.common.collect.testing.WrongType;
30
import com.google.common.collect.testing.features.CollectionFeature;
31
import com.google.common.collect.testing.features.CollectionSize;
32
import java.util.AbstractSet;
33
import java.util.Collections;
34
import java.util.ConcurrentModificationException;
35
import java.util.Iterator;
36
import org.junit.Ignore;
37

38
/**
39
 * A generic JUnit test which tests {@code removeAll} operations on a collection. Can't be invoked
40
 * directly; please see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
41
 *
42
 * @author George van den Driessche
43
 * @author Chris Povirk
44
 */
45
@GwtCompatible
46
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
47
public class CollectionRemoveAllTester<E> extends AbstractCollectionTester<E> {
48
  @CollectionFeature.Require(SUPPORTS_REMOVE)
49
  public void testRemoveAll_emptyCollection() {
50
    assertFalse(
51
        "removeAll(emptyCollection) should return false",
52
        collection.removeAll(MinimalCollection.of()));
53
    expectUnchanged();
54
  }
55

56
  @CollectionFeature.Require(SUPPORTS_REMOVE)
57
  public void testRemoveAll_nonePresent() {
58
    assertFalse(
59
        "removeAll(disjointCollection) should return false",
60
        collection.removeAll(MinimalCollection.of(e3())));
61
    expectUnchanged();
62
  }
63

64
  @CollectionFeature.Require(SUPPORTS_REMOVE)
65
  @CollectionSize.Require(absent = ZERO)
66
  public void testRemoveAll_allPresent() {
67
    assertTrue(
68
        "removeAll(intersectingCollection) should return true",
69
        collection.removeAll(MinimalCollection.of(e0())));
70
    expectMissing(e0());
71
  }
72

73
  @CollectionFeature.Require(SUPPORTS_REMOVE)
74
  @CollectionSize.Require(absent = ZERO)
75
  public void testRemoveAll_somePresent() {
76
    assertTrue(
77
        "removeAll(intersectingCollection) should return true",
78
        collection.removeAll(MinimalCollection.of(e0(), e3())));
79
    expectMissing(e0());
80
  }
81

82
  @CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
83
  @CollectionSize.Require(SEVERAL)
84
  public void testRemoveAllSomePresentConcurrentWithIteration() {
85
    try {
86
      Iterator<E> iterator = collection.iterator();
87
      assertTrue(collection.removeAll(MinimalCollection.of(e0(), e3())));
88
      iterator.next();
89
      fail("Expected ConcurrentModificationException");
90
    } catch (ConcurrentModificationException expected) {
91
      // success
92
    }
93
  }
94

95
  /** Trigger the {@code other.size() >= this.size()} case in {@link AbstractSet#removeAll}. */
96
  @CollectionFeature.Require(SUPPORTS_REMOVE)
97
  @CollectionSize.Require(absent = ZERO)
98
  public void testRemoveAll_somePresentLargeCollectionToRemove() {
99
    assertTrue(
100
        "removeAll(largeIntersectingCollection) should return true",
101
        collection.removeAll(MinimalCollection.of(e0(), e0(), e0(), e3(), e3(), e3())));
102
    expectMissing(e0());
103
  }
104

105
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
106
  public void testRemoveAll_unsupportedEmptyCollection() {
107
    try {
108
      assertFalse(
109
          "removeAll(emptyCollection) should return false or throw "
110
              + "UnsupportedOperationException",
111
          collection.removeAll(MinimalCollection.of()));
112
    } catch (UnsupportedOperationException tolerated) {
113
    }
114
    expectUnchanged();
115
  }
116

117
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
118
  public void testRemoveAll_unsupportedNonePresent() {
119
    try {
120
      assertFalse(
121
          "removeAll(disjointCollection) should return false or throw "
122
              + "UnsupportedOperationException",
123
          collection.removeAll(MinimalCollection.of(e3())));
124
    } catch (UnsupportedOperationException tolerated) {
125
    }
126
    expectUnchanged();
127
  }
128

129
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
130
  @CollectionSize.Require(absent = ZERO)
131
  public void testRemoveAll_unsupportedPresent() {
132
    try {
133
      collection.removeAll(MinimalCollection.of(e0()));
134
      fail("removeAll(intersectingCollection) should throw UnsupportedOperationException");
135
    } catch (UnsupportedOperationException expected) {
136
    }
137
    expectUnchanged();
138
    assertTrue(collection.contains(e0()));
139
  }
140

141
  /*
142
   * AbstractCollection fails the removeAll(null) test when the subject
143
   * collection is empty, but we'd still like to test removeAll(null) when we
144
   * can. We split the test into empty and non-empty cases. This allows us to
145
   * suppress only the former.
146
   */
147

148
  @CollectionFeature.Require(SUPPORTS_REMOVE)
149
  @CollectionSize.Require(ZERO)
150
  public void testRemoveAll_nullCollectionReferenceEmptySubject() {
151
    try {
152
      collection.removeAll(null);
153
      // Returning successfully is not ideal, but tolerated.
154
    } catch (NullPointerException tolerated) {
155
    }
156
  }
157

158
  @CollectionFeature.Require(SUPPORTS_REMOVE)
159
  @CollectionSize.Require(absent = ZERO)
160
  public void testRemoveAll_nullCollectionReferenceNonEmptySubject() {
161
    try {
162
      collection.removeAll(null);
163
      fail("removeAll(null) should throw NullPointerException");
164
    } catch (NullPointerException expected) {
165
    }
166
  }
167

168
  @CollectionFeature.Require(value = SUPPORTS_REMOVE, absent = ALLOWS_NULL_QUERIES)
169
  public void testRemoveAll_containsNullNo() {
170
    MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
171
    try {
172
      assertFalse(
173
          "removeAll(containsNull) should return false or throw",
174
          collection.removeAll(containsNull));
175
    } catch (NullPointerException tolerated) {
176
    }
177
    expectUnchanged();
178
  }
179

180
  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_QUERIES})
181
  public void testRemoveAll_containsNullNoButAllowed() {
182
    MinimalCollection<?> containsNull = MinimalCollection.of((Object) null);
183
    assertFalse("removeAll(containsNull) should return false", collection.removeAll(containsNull));
184
    expectUnchanged();
185
  }
186

187
  @CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
188
  @CollectionSize.Require(absent = ZERO)
189
  public void testRemoveAll_containsNullYes() {
190
    initCollectionWithNullElement();
191
    assertTrue(
192
        "removeAll(containsNull) should return true",
193
        collection.removeAll(Collections.singleton(null)));
194
    // TODO: make this work with MinimalCollection
195
  }
196

197
  @CollectionFeature.Require(SUPPORTS_REMOVE)
198
  public void testRemoveAll_containsWrongType() {
199
    try {
200
      assertFalse(
201
          "removeAll(containsWrongType) should return false or throw",
202
          collection.removeAll(MinimalCollection.of(WrongType.VALUE)));
203
    } catch (ClassCastException tolerated) {
204
    }
205
    expectUnchanged();
206
  }
207
}
208

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

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

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

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