• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 libcore.java.util;
18 
19 import java.util.Collection;
20 import java.util.ConcurrentModificationException;
21 import java.util.function.Predicate;
22 import java.util.function.Supplier;
23 
24 import static junit.framework.Assert.assertTrue;
25 import static junit.framework.Assert.fail;
26 
27 /**
28  * Tests behavior common to all implementations of {@link Collection#removeIf(Predicate)}.
29  */
30 public class RemoveIfTester {
31 
32     private static final Predicate<Integer> isEven = x -> x % 2 == 0;
33     private static final Predicate<Integer> isOdd = isEven.negate();
34 
runBasicRemoveIfTests(Supplier<Collection<Integer>> supp)35     public static void runBasicRemoveIfTests(Supplier<Collection<Integer>> supp) {
36         Collection<Integer> integers = supp.get();
37         for (int h = 0; h < 100; ++h) {
38             // Insert some ordered integers.
39             integers.add(h);
40         }
41 
42         integers.removeIf(isEven);
43         Integer prev = null;
44         for (Integer i : integers) {
45             assertTrue(i % 2 != 0);
46             if (prev != null) {
47                 assertTrue(prev <= i);
48             }
49             prev = i;
50         }
51 
52         integers.removeIf(isOdd);
53         assertTrue(integers.isEmpty());
54     }
55 
runBasicRemoveIfTestsUnordered(Supplier<Collection<Integer>> supp)56     public static void runBasicRemoveIfTestsUnordered(Supplier<Collection<Integer>> supp) {
57         Collection<Integer> integers = supp.get();
58         for (int h = 0; h < 100; ++h) {
59             // Insert a bunch of arbitrary integers.
60             integers.add((h >>> 2) ^ (h >>> 5) ^ (h >>> 11) ^ (h >>> 17));
61         }
62 
63         integers.removeIf(isEven);
64         for (Integer i : integers) {
65             assertTrue(i % 2 != 0);
66         }
67 
68         integers.removeIf(isOdd);
69         assertTrue(integers.isEmpty());
70     }
71 
72     /**
73      * Removing from an empty collection should not fail.
74      */
runRemoveIfOnEmpty(Supplier<Collection<Integer>> supp)75     public static void runRemoveIfOnEmpty(Supplier<Collection<Integer>> supp) {
76         supp.get().removeIf(x -> {
77             fail();
78             return false;
79         });
80     }
81 
testRemoveIfNPE(Supplier<Collection<Integer>> supp)82     public static void testRemoveIfNPE(Supplier<Collection<Integer>> supp) {
83         try {
84             supp.get().removeIf(null);
85             fail();
86         } catch (NullPointerException expected) {}
87     }
88 
testRemoveIfCME(Supplier<Collection<Integer>> supp)89     public static void testRemoveIfCME(Supplier<Collection<Integer>> supp) {
90         Collection<Integer> c = supp.get();
91         c.add(0);
92 
93         try {
94             c.removeIf(x -> {c.add(42); return true;});
95             fail();
96         } catch (ConcurrentModificationException expected) {
97         }
98     }
99 }
100