21 |
|
import java.util.Spliterator; |
22 |
|
import java.util.concurrent.BlockingDeque; |
23 |
|
import java.util.concurrent.BlockingQueue; |
24 |
+ |
import java.util.concurrent.ConcurrentLinkedQueue; |
25 |
|
import java.util.concurrent.CountDownLatch; |
26 |
|
import java.util.concurrent.Executors; |
27 |
|
import java.util.concurrent.ExecutorService; |
342 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
+ |
* All elements removed in the middle of CONCURRENT traversal. |
346 |
+ |
*/ |
347 |
+ |
public void testElementRemovalDuringTraversal() { |
348 |
+ |
Collection c = impl.emptyCollection(); |
349 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
350 |
+ |
int n = rnd.nextInt(6); |
351 |
+ |
ArrayList copy = new ArrayList(); |
352 |
+ |
for (int i = 0; i < n; i++) { |
353 |
+ |
Object x = impl.makeElement(i); |
354 |
+ |
copy.add(x); |
355 |
+ |
c.add(x); |
356 |
+ |
} |
357 |
+ |
ArrayList iterated = new ArrayList(); |
358 |
+ |
ArrayList spliterated = new ArrayList(); |
359 |
+ |
Spliterator s = c.spliterator(); |
360 |
+ |
Iterator it = c.iterator(); |
361 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
362 |
+ |
assertTrue(s.tryAdvance(spliterated::add)); |
363 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
364 |
+ |
iterated.add(it.next()); |
365 |
+ |
} |
366 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
367 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
368 |
+ |
c.clear(); // TODO: many more removal methods |
369 |
+ |
if (testImplementationDetails |
370 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) { |
371 |
+ |
if (rnd.nextBoolean()) |
372 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
373 |
+ |
else |
374 |
+ |
s.forEachRemaining(alwaysThrows); |
375 |
+ |
} |
376 |
+ |
if (it.hasNext()) iterated.add(it.next()); |
377 |
+ |
if (rnd.nextBoolean()) assertIteratorExhausted(it); |
378 |
+ |
} |
379 |
+ |
assertTrue(copy.containsAll(iterated)); |
380 |
+ |
assertTrue(copy.containsAll(spliterated)); |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
/** |
384 |
+ |
* Some elements randomly disappear in the middle of traversal. |
385 |
+ |
*/ |
386 |
+ |
public void testRandomElementRemovalDuringTraversal() { |
387 |
+ |
Collection c = impl.emptyCollection(); |
388 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
389 |
+ |
int n = rnd.nextInt(6); |
390 |
+ |
ArrayList copy = new ArrayList(); |
391 |
+ |
for (int i = 0; i < n; i++) { |
392 |
+ |
Object x = impl.makeElement(i); |
393 |
+ |
copy.add(x); |
394 |
+ |
c.add(x); |
395 |
+ |
} |
396 |
+ |
ArrayList iterated = new ArrayList(); |
397 |
+ |
ArrayList spliterated = new ArrayList(); |
398 |
+ |
ArrayList removed = new ArrayList(); |
399 |
+ |
Spliterator s = c.spliterator(); |
400 |
+ |
Iterator it = c.iterator(); |
401 |
+ |
if (! (s.hasCharacteristics(Spliterator.CONCURRENT) || |
402 |
+ |
s.hasCharacteristics(Spliterator.IMMUTABLE))) |
403 |
+ |
return; |
404 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
405 |
+ |
assertTrue(s.tryAdvance(e -> {})); |
406 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
407 |
+ |
it.next(); |
408 |
+ |
} |
409 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
410 |
+ |
// TODO: many more removal methods |
411 |
+ |
if (rnd.nextBoolean()) { |
412 |
+ |
for (Iterator z = c.iterator(); z.hasNext(); ) { |
413 |
+ |
Object e = z.next(); |
414 |
+ |
if (rnd.nextBoolean()) { |
415 |
+ |
try { |
416 |
+ |
z.remove(); |
417 |
+ |
} catch (UnsupportedOperationException ok) { return; } |
418 |
+ |
removed.add(e); |
419 |
+ |
} |
420 |
+ |
} |
421 |
+ |
} else { |
422 |
+ |
Predicate randomlyRemove = e -> { |
423 |
+ |
if (rnd.nextBoolean()) { removed.add(e); return true; } |
424 |
+ |
else return false; |
425 |
+ |
}; |
426 |
+ |
c.removeIf(randomlyRemove); |
427 |
+ |
} |
428 |
+ |
s.forEachRemaining(spliterated::add); |
429 |
+ |
while (it.hasNext()) |
430 |
+ |
iterated.add(it.next()); |
431 |
+ |
assertTrue(copy.containsAll(iterated)); |
432 |
+ |
assertTrue(copy.containsAll(spliterated)); |
433 |
+ |
assertTrue(copy.containsAll(removed)); |
434 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
435 |
+ |
ArrayList iteratedAndRemoved = new ArrayList(iterated); |
436 |
+ |
ArrayList spliteratedAndRemoved = new ArrayList(spliterated); |
437 |
+ |
iteratedAndRemoved.retainAll(removed); |
438 |
+ |
spliteratedAndRemoved.retainAll(removed); |
439 |
+ |
assertTrue(iteratedAndRemoved.size() <= 1); |
440 |
+ |
assertTrue(spliteratedAndRemoved.size() <= 1); |
441 |
+ |
if (testImplementationDetails |
442 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) |
443 |
+ |
assertTrue(spliteratedAndRemoved.isEmpty()); |
444 |
+ |
} |
445 |
+ |
} |
446 |
+ |
|
447 |
+ |
/** |
448 |
|
* Various ways of traversing a collection yield same elements |
449 |
|
*/ |
450 |
|
public void testIteratorEquivalence() { |
456 |
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
457 |
|
ArrayList tryAdvanced = new ArrayList(); |
458 |
|
ArrayList spliterated = new ArrayList(); |
459 |
+ |
ArrayList splitonced = new ArrayList(); |
460 |
|
ArrayList forEached = new ArrayList(); |
461 |
+ |
ArrayList streamForEached = new ArrayList(); |
462 |
+ |
ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue(); |
463 |
|
ArrayList removeIfed = new ArrayList(); |
464 |
|
for (Object x : c) iterated.add(x); |
465 |
|
c.iterator().forEachRemaining(iteratedForEachRemaining::add); |
466 |
|
for (Spliterator s = c.spliterator(); |
467 |
|
s.tryAdvance(tryAdvanced::add); ) {} |
468 |
|
c.spliterator().forEachRemaining(spliterated::add); |
469 |
+ |
{ // trySplit returns "strict prefix" |
470 |
+ |
Spliterator s1 = c.spliterator(), s2 = s1.trySplit(); |
471 |
+ |
if (s2 != null) s2.forEachRemaining(splitonced::add); |
472 |
+ |
s1.forEachRemaining(splitonced::add); |
473 |
+ |
} |
474 |
|
c.forEach(forEached::add); |
475 |
+ |
c.stream().forEach(streamForEached::add); |
476 |
+ |
c.parallelStream().forEach(parallelStreamForEached::add); |
477 |
|
c.removeIf(e -> { removeIfed.add(e); return false; }); |
478 |
|
boolean ordered = |
479 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
480 |
|
if (c instanceof List || c instanceof Deque) |
481 |
|
assertTrue(ordered); |
482 |
+ |
HashSet cset = new HashSet(c); |
483 |
+ |
assertEquals(cset, new HashSet(parallelStreamForEached)); |
484 |
|
if (ordered) { |
485 |
|
assertEquals(iterated, iteratedForEachRemaining); |
486 |
|
assertEquals(iterated, tryAdvanced); |
487 |
|
assertEquals(iterated, spliterated); |
488 |
+ |
assertEquals(iterated, splitonced); |
489 |
|
assertEquals(iterated, forEached); |
490 |
+ |
assertEquals(iterated, streamForEached); |
491 |
|
assertEquals(iterated, removeIfed); |
492 |
|
} else { |
375 |
– |
HashSet cset = new HashSet(c); |
493 |
|
assertEquals(cset, new HashSet(iterated)); |
494 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
495 |
|
assertEquals(cset, new HashSet(tryAdvanced)); |
496 |
|
assertEquals(cset, new HashSet(spliterated)); |
497 |
+ |
assertEquals(cset, new HashSet(splitonced)); |
498 |
|
assertEquals(cset, new HashSet(forEached)); |
499 |
+ |
assertEquals(cset, new HashSet(streamForEached)); |
500 |
|
assertEquals(cset, new HashSet(removeIfed)); |
501 |
|
} |
502 |
|
if (c instanceof Deque) { |
653 |
|
assertTrue(found.isEmpty()); |
654 |
|
} |
655 |
|
|
656 |
+ |
/** TODO: promote to a common utility */ |
657 |
+ |
static <T> T chooseOne(T ... ts) { |
658 |
+ |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
659 |
+ |
} |
660 |
+ |
|
661 |
+ |
/** TODO: more random adders and removers */ |
662 |
+ |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
663 |
+ |
return chooseOne( |
664 |
+ |
() -> { |
665 |
+ |
assertTrue(c.add(e)); |
666 |
+ |
assertTrue(c.contains(e)); |
667 |
+ |
assertTrue(c.remove(e)); |
668 |
+ |
assertFalse(c.contains(e)); |
669 |
+ |
}, |
670 |
+ |
() -> { |
671 |
+ |
assertTrue(c.add(e)); |
672 |
+ |
assertTrue(c.contains(e)); |
673 |
+ |
assertTrue(c.removeIf(x -> x == e)); |
674 |
+ |
assertFalse(c.contains(e)); |
675 |
+ |
}, |
676 |
+ |
() -> { |
677 |
+ |
assertTrue(c.add(e)); |
678 |
+ |
assertTrue(c.contains(e)); |
679 |
+ |
for (Iterator it = c.iterator();; ) |
680 |
+ |
if (it.next() == e) { |
681 |
+ |
try { it.remove(); } |
682 |
+ |
catch (UnsupportedOperationException ok) { |
683 |
+ |
c.remove(e); |
684 |
+ |
} |
685 |
+ |
break; |
686 |
+ |
} |
687 |
+ |
assertFalse(c.contains(e)); |
688 |
+ |
}); |
689 |
+ |
} |
690 |
+ |
|
691 |
|
/** |
692 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
693 |
|
*/ |
695 |
|
if (!impl.isConcurrent()) return; |
696 |
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
697 |
|
final Collection c = impl.emptyCollection(); |
698 |
< |
final long testDurationMillis = timeoutMillis(); |
698 |
> |
final long testDurationMillis |
699 |
> |
= expensiveTests ? LONG_DELAY_MS : timeoutMillis(); |
700 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
701 |
|
final Object one = impl.makeElement(1); |
702 |
|
final Object two = impl.makeElement(2); |
703 |
+ |
final Consumer checkSanity = x -> assertTrue(x == one || x == two); |
704 |
+ |
final Consumer<Object[]> checkArraySanity = array -> { |
705 |
+ |
// assertTrue(array.length <= 2); // duplicates are permitted |
706 |
+ |
for (Object x : array) assertTrue(x == one || x == two); |
707 |
+ |
}; |
708 |
|
final Object[] emptyArray = |
709 |
|
(Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); |
710 |
|
final List<Future<?>> futures; |
711 |
|
final Phaser threadsStarted = new Phaser(1); // register this thread |
712 |
|
final Runnable[] frobbers = { |
713 |
< |
() -> c.forEach(x -> assertTrue(x == one || x == two)), |
714 |
< |
() -> c.stream().forEach(x -> assertTrue(x == one || x == two)), |
713 |
> |
() -> c.forEach(checkSanity), |
714 |
> |
() -> c.stream().forEach(checkSanity), |
715 |
> |
() -> c.parallelStream().forEach(checkSanity), |
716 |
|
() -> c.spliterator().trySplit(), |
717 |
|
() -> { |
718 |
|
Spliterator s = c.spliterator(); |
719 |
< |
s.tryAdvance(x -> assertTrue(x == one || x == two)); |
719 |
> |
s.tryAdvance(checkSanity); |
720 |
|
s.trySplit(); |
721 |
|
}, |
722 |
|
() -> { |
723 |
|
Spliterator s = c.spliterator(); |
724 |
< |
do {} while (s.tryAdvance(x -> assertTrue(x == one || x == two))); |
724 |
> |
do {} while (s.tryAdvance(checkSanity)); |
725 |
|
}, |
726 |
+ |
() -> { for (Object x : c) checkSanity.accept(x); }, |
727 |
+ |
() -> checkArraySanity.accept(c.toArray()), |
728 |
+ |
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
729 |
|
() -> { |
730 |
< |
for (Object x : c) assertTrue(x == one || x == two); |
731 |
< |
}, |
732 |
< |
() -> { |
733 |
< |
for (Object x : c.toArray()) assertTrue(x == one || x == two); |
734 |
< |
}, |
735 |
< |
() -> { |
736 |
< |
for (Object x : c.toArray(emptyArray)) assertTrue(x == one || x == two); |
737 |
< |
}, |
738 |
< |
() -> { |
739 |
< |
assertTrue(c.add(one)); |
740 |
< |
assertTrue(c.contains(one)); |
741 |
< |
assertTrue(c.remove(one)); |
742 |
< |
assertFalse(c.contains(one)); |
743 |
< |
}, |
580 |
< |
() -> { |
581 |
< |
assertTrue(c.add(two)); |
582 |
< |
assertTrue(c.contains(two)); |
583 |
< |
assertTrue(c.remove(two)); |
584 |
< |
assertFalse(c.contains(two)); |
585 |
< |
}, |
730 |
> |
Object[] a = new Object[5]; |
731 |
> |
Object three = impl.makeElement(3); |
732 |
> |
Arrays.fill(a, 0, a.length, three); |
733 |
> |
Object[] x = c.toArray(a); |
734 |
> |
if (x == a) |
735 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
736 |
> |
checkSanity.accept(a[i]); |
737 |
> |
// A careful reading of the spec does not support: |
738 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
739 |
> |
else |
740 |
> |
checkArraySanity.accept(x); |
741 |
> |
}, |
742 |
> |
adderRemover(c, one), |
743 |
> |
adderRemover(c, two), |
744 |
|
}; |
745 |
|
final List<Runnable> tasks = |
746 |
|
Arrays.stream(frobbers) |
771 |
|
*/ |
772 |
|
public void testLateBindingStyle() { |
773 |
|
if (!testImplementationDetails) return; |
774 |
+ |
if (impl.klazz() == ArrayList.class) return; // for jdk8 |
775 |
|
// Immutable (snapshot) spliterators are exempt |
776 |
|
if (impl.emptyCollection().spliterator() |
777 |
|
.hasCharacteristics(Spliterator.IMMUTABLE)) |