guava

Форк
0
/
CollectionRemoveIfTester.java 
105 строк · 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
public class CollectionRemoveIfTester<E> extends AbstractCollectionTester<E> {
44
  @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
45
  public void testRemoveIf_alwaysFalse() {
46
    assertFalse("removeIf(x -> false) should return false", collection.removeIf(x -> false));
47
    expectUnchanged();
48
  }
49

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

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

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

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

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

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

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

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

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