5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import java.util.ArrayList; |
9 |
|
import java.util.Collection; |
10 |
|
import java.util.Deque; |
11 |
|
import java.util.NoSuchElementException; |
12 |
|
import java.util.Queue; |
13 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
14 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
import java.util.function.Consumer; |
16 |
+ |
import java.util.function.Predicate; |
17 |
|
|
18 |
|
import junit.framework.Test; |
19 |
|
|
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
+ |
public void testRemoveIf() { |
136 |
+ |
Collection c = impl.emptyCollection(); |
137 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
138 |
+ |
int n = rnd.nextInt(6); |
139 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
140 |
+ |
AtomicReference threwAt = new AtomicReference(null); |
141 |
+ |
ArrayList survivors = new ArrayList(c); |
142 |
+ |
ArrayList accepts = new ArrayList(); |
143 |
+ |
ArrayList rejects = new ArrayList(); |
144 |
+ |
Predicate randomPredicate = (e) -> { |
145 |
+ |
assertNull(threwAt.get()); |
146 |
+ |
switch (rnd.nextInt(3)) { |
147 |
+ |
case 0: accepts.add(e); return true; |
148 |
+ |
case 1: rejects.add(e); return false; |
149 |
+ |
case 2: threwAt.set(e); throw new ArithmeticException(); |
150 |
+ |
default: throw new AssertionError(); |
151 |
+ |
} |
152 |
+ |
}; |
153 |
+ |
try { |
154 |
+ |
boolean modified = c.removeIf(randomPredicate); |
155 |
+ |
if (!modified) { |
156 |
+ |
assertNull(threwAt.get()); |
157 |
+ |
assertEquals(n, rejects.size()); |
158 |
+ |
assertEquals(0, accepts.size()); |
159 |
+ |
} |
160 |
+ |
} catch (ArithmeticException ok) {} |
161 |
+ |
survivors.removeAll(accepts); |
162 |
+ |
assertEquals(n - accepts.size(), c.size()); |
163 |
+ |
assertTrue(c.containsAll(survivors)); |
164 |
+ |
assertTrue(survivors.containsAll(rejects)); |
165 |
+ |
for (Object x : accepts) assertFalse(c.contains(x)); |
166 |
+ |
if (threwAt.get() == null) |
167 |
+ |
assertEquals(accepts.size() + rejects.size(), n); |
168 |
+ |
} |
169 |
+ |
|
170 |
|
// public void testCollectionDebugFail() { |
171 |
|
// fail(impl.klazz().getSimpleName()); |
172 |
|
// } |