Java

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

25
import com.google.common.annotations.GwtCompatible;
26
import com.google.common.collect.testing.AbstractCollectionTester;
27
import com.google.common.collect.testing.features.CollectionFeature;
28
import com.google.common.collect.testing.features.CollectionSize;
29
import java.util.Collection;
30
import java.util.ConcurrentModificationException;
31
import java.util.Iterator;
32
import java.util.function.Predicate;
33
import org.junit.Ignore;
34

35
/**
36
 * A generic JUnit test which tests {@link Collection#removeIf}. Can't be invoked directly; please
37
 * see {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
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 CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
45
  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
46
  public void testRemoveIf_alwaysFalse() {
47
    assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
48
    expectUnchanged();
49
  }
50

51
  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
52
  @CollectionSize.Require(absent = ZERO)
53
  public void testRemoveIf_sometimesTrue() {
54
    assertTrue(
55
        "removeIf(isEqual(present)) should return true",
56
        collection.removeIf(Predicate.isEqual(samples.e0())));
57
    expectMissing(samples.e0());
58
  }
59

60
  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
61
  @CollectionSize.Require(absent = ZERO)
62
  public void testRemoveIf_allPresent() {
63
    assertTrue("removeIf(x -> true) should return true", collection.removeIf(x -> true));
64
    expectContents();
65
  }
66

67
  @CollectionFeature.Require({SUPPORTS_ITERATOR_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
68
  @CollectionSize.Require(SEVERAL)
69
  public void testRemoveIfSomeMatchesConcurrentWithIteration() {
70
    try {
71
      Iterator<E> iterator = collection.iterator();
72
      assertTrue(collection.removeIf(Predicate.isEqual(samples.e0())));
73
      iterator.next();
74
      fail("Expected ConcurrentModificationException");
75
    } catch (ConcurrentModificationException expected) {
76
      // success
77
    }
78
  }
79

80
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
81
  @CollectionSize.Require(ZERO)
82
  public void testRemoveIf_unsupportedEmptyCollection() {
83
    try {
84
      assertFalse(
85
          "removeIf(Predicate) should return false or throw " + "UnsupportedOperationException",
86
          collection.removeIf(
87
              x -> {
88
                throw new AssertionError("predicate should never be called");
89
              }));
90
    } catch (UnsupportedOperationException tolerated) {
91
    }
92
    expectUnchanged();
93
  }
94

95
  @CollectionFeature.Require(absent = SUPPORTS_REMOVE)
96
  @CollectionSize.Require(absent = ZERO)
97
  public void testRemoveIf_alwaysTrueUnsupported() {
98
    try {
99
      collection.removeIf(x -> true);
100
      fail("removeIf(x -> true) should throw " + "UnsupportedOperationException");
101
    } catch (UnsupportedOperationException expected) {
102
    }
103
    expectUnchanged();
104
    assertTrue(collection.contains(samples.e0()));
105
  }
106
}
107

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

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

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

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