98 |
|
assertSame(3, a[2]); |
99 |
|
} |
100 |
|
assertIteratorExhausted(c.iterator()); |
101 |
< |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
101 |
> |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
102 |
|
c.forEach(alwaysThrows); |
103 |
|
c.iterator().forEachRemaining(alwaysThrows); |
104 |
|
c.spliterator().forEachRemaining(alwaysThrows); |
241 |
|
|
242 |
|
public void testRemoveIf() { |
243 |
|
Collection c = impl.emptyCollection(); |
244 |
+ |
boolean ordered = |
245 |
+ |
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
246 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
247 |
|
int n = rnd.nextInt(6); |
248 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
258 |
|
ArrayList accepts = new ArrayList(); |
259 |
|
ArrayList rejects = new ArrayList(); |
260 |
|
|
261 |
< |
Predicate randomPredicate = (e) -> { |
261 |
> |
Predicate randomPredicate = e -> { |
262 |
|
assertNull(threwAt.get()); |
263 |
|
switch (rnd.nextInt(3)) { |
264 |
|
case 0: accepts.add(e); return true; |
274 |
|
assertEquals(modified, accepts.size() > 0); |
275 |
|
assertEquals(modified, rejects.size() != n); |
276 |
|
assertEquals(accepts.size() + rejects.size(), n); |
277 |
< |
assertEquals(rejects, Arrays.asList(c.toArray())); |
277 |
> |
if (ordered) { |
278 |
> |
assertEquals(rejects, |
279 |
> |
Arrays.asList(c.toArray())); |
280 |
> |
} else { |
281 |
> |
assertEquals(new HashSet(rejects), |
282 |
> |
new HashSet(Arrays.asList(c.toArray()))); |
283 |
> |
} |
284 |
|
} catch (ArithmeticException ok) { |
285 |
|
assertNotNull(threwAt.get()); |
286 |
|
assertTrue(c.contains(threwAt.get())); |
301 |
|
assertTrue(c.containsAll(rejects)); |
302 |
|
assertTrue(c.containsAll(survivors)); |
303 |
|
assertTrue(survivors.containsAll(rejects)); |
304 |
< |
assertEquals(n - accepts.size(), c.size()); |
305 |
< |
for (Object x : accepts) assertFalse(c.contains(x)); |
304 |
> |
if (threwAt.get() == null) { |
305 |
> |
assertEquals(n - accepts.size(), c.size()); |
306 |
> |
for (Object x : accepts) assertFalse(c.contains(x)); |
307 |
> |
} else { |
308 |
> |
// Two acceptable behaviors: entire removeIf call is one |
309 |
> |
// transaction, or each element processed is one transaction. |
310 |
> |
assertTrue(n == c.size() || n == c.size() + accepts.size()); |
311 |
> |
int k = 0; |
312 |
> |
for (Object x : accepts) if (c.contains(x)) k++; |
313 |
> |
assertTrue(k == accepts.size() || k == 0); |
314 |
> |
} |
315 |
|
} catch (Throwable ex) { |
316 |
|
System.err.println(impl.klazz()); |
317 |
|
System.err.printf("c=%s%n", c); |
382 |
|
|
383 |
|
/** |
384 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
385 |
< |
* should remove last element |
385 |
> |
* should (maybe) remove last element |
386 |
|
*/ |
387 |
|
public void testRemoveAfterForEachRemaining() { |
388 |
|
Collection c = impl.emptyCollection(); |
395 |
|
assertEquals(impl.makeElement(0), it.next()); |
396 |
|
assertTrue(it.hasNext()); |
397 |
|
assertEquals(impl.makeElement(1), it.next()); |
398 |
< |
it.forEachRemaining((e) -> {}); |
399 |
< |
it.remove(); |
400 |
< |
assertEquals(n - 1, c.size()); |
401 |
< |
for (int i = 0; i < n - 1; i++) |
402 |
< |
assertTrue(c.contains(impl.makeElement(i))); |
403 |
< |
assertFalse(c.contains(impl.makeElement(n - 1))); |
398 |
> |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
399 |
> |
if (testImplementationDetails) { |
400 |
> |
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
401 |
> |
assertIteratorExhausted(it); |
402 |
> |
} else { |
403 |
> |
it.remove(); |
404 |
> |
assertEquals(n - 1, c.size()); |
405 |
> |
for (int i = 0; i < n - 1; i++) |
406 |
> |
assertTrue(c.contains(impl.makeElement(i))); |
407 |
> |
assertFalse(c.contains(impl.makeElement(n - 1))); |
408 |
> |
} |
409 |
> |
} |
410 |
|
} |
411 |
|
if (c instanceof Deque) { |
412 |
|
Deque d = (Deque) impl.emptyCollection(); |
417 |
|
assertEquals(impl.makeElement(n - 1), it.next()); |
418 |
|
assertTrue(it.hasNext()); |
419 |
|
assertEquals(impl.makeElement(n - 2), it.next()); |
420 |
< |
it.forEachRemaining((e) -> {}); |
421 |
< |
it.remove(); |
422 |
< |
assertEquals(n - 1, d.size()); |
423 |
< |
for (int i = 1; i < n; i++) |
424 |
< |
assertTrue(d.contains(impl.makeElement(i))); |
425 |
< |
assertFalse(d.contains(impl.makeElement(0))); |
420 |
> |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
421 |
> |
if (testImplementationDetails) { |
422 |
> |
it.remove(); |
423 |
> |
assertEquals(n - 1, d.size()); |
424 |
> |
for (int i = 1; i < n; i++) |
425 |
> |
assertTrue(d.contains(impl.makeElement(i))); |
426 |
> |
assertFalse(d.contains(impl.makeElement(0))); |
427 |
> |
} |
428 |
|
} |
429 |
|
} |
430 |
|
|
437 |
|
final Object x = impl.makeElement(1); |
438 |
|
final Object y = impl.makeElement(2); |
439 |
|
final ArrayList found = new ArrayList(); |
440 |
< |
Consumer<Object> spy = (o) -> { found.add(o); }; |
440 |
> |
Consumer<Object> spy = o -> found.add(o); |
441 |
|
c.stream().forEach(spy); |
442 |
|
assertTrue(found.isEmpty()); |
443 |
|
|
471 |
|
Runnable checkElt = () -> { |
472 |
|
threadsStarted.countDown(); |
473 |
|
while (!done.get()) |
474 |
< |
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
474 |
> |
c.stream().forEach(x -> assertSame(x, elt)); }; |
475 |
|
Runnable addRemove = () -> { |
476 |
|
threadsStarted.countDown(); |
477 |
|
while (!done.get()) { |
495 |
|
final Object x = impl.makeElement(1); |
496 |
|
final Object y = impl.makeElement(2); |
497 |
|
final ArrayList found = new ArrayList(); |
498 |
< |
Consumer<Object> spy = (o) -> { found.add(o); }; |
498 |
> |
Consumer<Object> spy = o -> found.add(o); |
499 |
|
c.forEach(spy); |
500 |
|
assertTrue(found.isEmpty()); |
501 |
|
|
529 |
|
Runnable checkElt = () -> { |
530 |
|
threadsStarted.countDown(); |
531 |
|
while (!done.get()) |
532 |
< |
c.forEach((x) -> { assertSame(x, elt); }); }; |
532 |
> |
c.forEach(x -> assertSame(x, elt)); }; |
533 |
|
Runnable addRemove = () -> { |
534 |
|
threadsStarted.countDown(); |
535 |
|
while (!done.get()) { |