12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Collections; |
15 |
+ |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Deque; |
17 |
|
import java.util.HashSet; |
18 |
|
import java.util.Iterator; |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
+ |
* All elements removed in the middle of CONCURRENT traversal. |
347 |
+ |
*/ |
348 |
+ |
public void testElementRemovalDuringTraversal() { |
349 |
+ |
Collection c = impl.emptyCollection(); |
350 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
351 |
+ |
int n = rnd.nextInt(6); |
352 |
+ |
ArrayList copy = new ArrayList(); |
353 |
+ |
for (int i = 0; i < n; i++) { |
354 |
+ |
Object x = impl.makeElement(i); |
355 |
+ |
copy.add(x); |
356 |
+ |
c.add(x); |
357 |
+ |
} |
358 |
+ |
ArrayList iterated = new ArrayList(); |
359 |
+ |
ArrayList spliterated = new ArrayList(); |
360 |
+ |
Spliterator s = c.spliterator(); |
361 |
+ |
Iterator it = c.iterator(); |
362 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
363 |
+ |
assertTrue(s.tryAdvance(spliterated::add)); |
364 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
365 |
+ |
iterated.add(it.next()); |
366 |
+ |
} |
367 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
368 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
369 |
+ |
c.clear(); // TODO: many more removal methods |
370 |
+ |
if (testImplementationDetails |
371 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) { |
372 |
+ |
if (rnd.nextBoolean()) |
373 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
374 |
+ |
else |
375 |
+ |
s.forEachRemaining(alwaysThrows); |
376 |
+ |
} |
377 |
+ |
if (it.hasNext()) iterated.add(it.next()); |
378 |
+ |
if (rnd.nextBoolean()) assertIteratorExhausted(it); |
379 |
+ |
} |
380 |
+ |
assertTrue(copy.containsAll(iterated)); |
381 |
+ |
assertTrue(copy.containsAll(spliterated)); |
382 |
+ |
} |
383 |
+ |
|
384 |
+ |
/** |
385 |
+ |
* Some elements randomly disappear in the middle of traversal. |
386 |
+ |
*/ |
387 |
+ |
public void testRandomElementRemovalDuringTraversal() { |
388 |
+ |
Collection c = impl.emptyCollection(); |
389 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
390 |
+ |
int n = rnd.nextInt(6); |
391 |
+ |
ArrayList copy = new ArrayList(); |
392 |
+ |
for (int i = 0; i < n; i++) { |
393 |
+ |
Object x = impl.makeElement(i); |
394 |
+ |
copy.add(x); |
395 |
+ |
c.add(x); |
396 |
+ |
} |
397 |
+ |
ArrayList iterated = new ArrayList(); |
398 |
+ |
ArrayList spliterated = new ArrayList(); |
399 |
+ |
ArrayList removed = new ArrayList(); |
400 |
+ |
Spliterator s = c.spliterator(); |
401 |
+ |
Iterator it = c.iterator(); |
402 |
+ |
if (! (s.hasCharacteristics(Spliterator.CONCURRENT) || |
403 |
+ |
s.hasCharacteristics(Spliterator.IMMUTABLE))) |
404 |
+ |
return; |
405 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
406 |
+ |
assertTrue(s.tryAdvance(e -> {})); |
407 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
408 |
+ |
it.next(); |
409 |
+ |
} |
410 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
411 |
+ |
// TODO: many more removal methods |
412 |
+ |
if (rnd.nextBoolean()) { |
413 |
+ |
for (Iterator z = c.iterator(); z.hasNext(); ) { |
414 |
+ |
Object e = z.next(); |
415 |
+ |
if (rnd.nextBoolean()) { |
416 |
+ |
try { |
417 |
+ |
z.remove(); |
418 |
+ |
} catch (UnsupportedOperationException ok) { return; } |
419 |
+ |
removed.add(e); |
420 |
+ |
} |
421 |
+ |
} |
422 |
+ |
} else { |
423 |
+ |
Predicate randomlyRemove = e -> { |
424 |
+ |
if (rnd.nextBoolean()) { removed.add(e); return true; } |
425 |
+ |
else return false; |
426 |
+ |
}; |
427 |
+ |
c.removeIf(randomlyRemove); |
428 |
+ |
} |
429 |
+ |
s.forEachRemaining(spliterated::add); |
430 |
+ |
while (it.hasNext()) |
431 |
+ |
iterated.add(it.next()); |
432 |
+ |
assertTrue(copy.containsAll(iterated)); |
433 |
+ |
assertTrue(copy.containsAll(spliterated)); |
434 |
+ |
assertTrue(copy.containsAll(removed)); |
435 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
436 |
+ |
ArrayList iteratedAndRemoved = new ArrayList(iterated); |
437 |
+ |
ArrayList spliteratedAndRemoved = new ArrayList(spliterated); |
438 |
+ |
iteratedAndRemoved.retainAll(removed); |
439 |
+ |
spliteratedAndRemoved.retainAll(removed); |
440 |
+ |
assertTrue(iteratedAndRemoved.size() <= 1); |
441 |
+ |
assertTrue(spliteratedAndRemoved.size() <= 1); |
442 |
+ |
if (testImplementationDetails |
443 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) |
444 |
+ |
assertTrue(spliteratedAndRemoved.isEmpty()); |
445 |
+ |
} |
446 |
+ |
} |
447 |
+ |
|
448 |
+ |
/** |
449 |
|
* Various ways of traversing a collection yield same elements |
450 |
|
*/ |
451 |
< |
public void testIteratorEquivalence() { |
451 |
> |
public void testTraversalEquivalence() { |
452 |
|
Collection c = impl.emptyCollection(); |
453 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
454 |
|
int n = rnd.nextInt(6); |
516 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
+ |
* Iterator.forEachRemaining has same behavior as Iterator's |
520 |
+ |
* default implementation. |
521 |
+ |
*/ |
522 |
+ |
public void testForEachRemainingConsistentWithDefaultImplementation() { |
523 |
+ |
Collection c = impl.emptyCollection(); |
524 |
+ |
if (!testImplementationDetails |
525 |
+ |
|| c.getClass() == java.util.LinkedList.class) |
526 |
+ |
return; |
527 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
528 |
+ |
int n = 1 + rnd.nextInt(3); |
529 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
530 |
+ |
ArrayList iterated = new ArrayList(); |
531 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
532 |
+ |
Iterator it1 = c.iterator(); |
533 |
+ |
Iterator it2 = c.iterator(); |
534 |
+ |
assertTrue(it1.hasNext()); |
535 |
+ |
assertTrue(it2.hasNext()); |
536 |
+ |
c.clear(); |
537 |
+ |
Object r1, r2; |
538 |
+ |
try { |
539 |
+ |
while (it1.hasNext()) iterated.add(it1.next()); |
540 |
+ |
r1 = iterated; |
541 |
+ |
} catch (ConcurrentModificationException ex) { |
542 |
+ |
r1 = ConcurrentModificationException.class; |
543 |
+ |
assertFalse(impl.isConcurrent()); |
544 |
+ |
} |
545 |
+ |
try { |
546 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
547 |
+ |
r2 = iteratedForEachRemaining; |
548 |
+ |
} catch (ConcurrentModificationException ex) { |
549 |
+ |
r2 = ConcurrentModificationException.class; |
550 |
+ |
assertFalse(impl.isConcurrent()); |
551 |
+ |
} |
552 |
+ |
assertEquals(r1, r2); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
557 |
|
* should (maybe) remove last element |
558 |
|
*/ |
691 |
|
assertTrue(found.isEmpty()); |
692 |
|
} |
693 |
|
|
694 |
+ |
/** TODO: promote to a common utility */ |
695 |
+ |
static <T> T chooseOne(T ... ts) { |
696 |
+ |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
697 |
+ |
} |
698 |
+ |
|
699 |
+ |
/** TODO: more random adders and removers */ |
700 |
+ |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
701 |
+ |
return chooseOne( |
702 |
+ |
() -> { |
703 |
+ |
assertTrue(c.add(e)); |
704 |
+ |
assertTrue(c.contains(e)); |
705 |
+ |
assertTrue(c.remove(e)); |
706 |
+ |
assertFalse(c.contains(e)); |
707 |
+ |
}, |
708 |
+ |
() -> { |
709 |
+ |
assertTrue(c.add(e)); |
710 |
+ |
assertTrue(c.contains(e)); |
711 |
+ |
assertTrue(c.removeIf(x -> x == e)); |
712 |
+ |
assertFalse(c.contains(e)); |
713 |
+ |
}, |
714 |
+ |
() -> { |
715 |
+ |
assertTrue(c.add(e)); |
716 |
+ |
assertTrue(c.contains(e)); |
717 |
+ |
for (Iterator it = c.iterator();; ) |
718 |
+ |
if (it.next() == e) { |
719 |
+ |
try { it.remove(); } |
720 |
+ |
catch (UnsupportedOperationException ok) { |
721 |
+ |
c.remove(e); |
722 |
+ |
} |
723 |
+ |
break; |
724 |
+ |
} |
725 |
+ |
assertFalse(c.contains(e)); |
726 |
+ |
}); |
727 |
+ |
} |
728 |
+ |
|
729 |
+ |
/** |
730 |
+ |
* Concurrent Spliterators, once exhausted, stay exhausted. |
731 |
+ |
*/ |
732 |
+ |
public void testStickySpliteratorExhaustion() throws Throwable { |
733 |
+ |
if (!impl.isConcurrent()) return; |
734 |
+ |
if (!testImplementationDetails) return; |
735 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
736 |
+ |
final Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
737 |
+ |
final Collection c = impl.emptyCollection(); |
738 |
+ |
final Spliterator s = c.spliterator(); |
739 |
+ |
if (rnd.nextBoolean()) { |
740 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
741 |
+ |
} else { |
742 |
+ |
s.forEachRemaining(alwaysThrows); |
743 |
+ |
} |
744 |
+ |
final Object one = impl.makeElement(1); |
745 |
+ |
// Spliterator should not notice added element |
746 |
+ |
c.add(one); |
747 |
+ |
if (rnd.nextBoolean()) { |
748 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
749 |
+ |
} else { |
750 |
+ |
s.forEachRemaining(alwaysThrows); |
751 |
+ |
} |
752 |
+ |
} |
753 |
+ |
|
754 |
|
/** |
755 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
756 |
|
*/ |
790 |
|
() -> checkArraySanity.accept(c.toArray()), |
791 |
|
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
792 |
|
() -> { |
793 |
< |
assertTrue(c.add(one)); |
794 |
< |
assertTrue(c.contains(one)); |
795 |
< |
assertTrue(c.remove(one)); |
796 |
< |
assertFalse(c.contains(one)); |
797 |
< |
}, |
798 |
< |
() -> { |
799 |
< |
assertTrue(c.add(two)); |
800 |
< |
assertTrue(c.contains(two)); |
801 |
< |
assertTrue(c.remove(two)); |
802 |
< |
assertFalse(c.contains(two)); |
803 |
< |
}, |
793 |
> |
Object[] a = new Object[5]; |
794 |
> |
Object three = impl.makeElement(3); |
795 |
> |
Arrays.fill(a, 0, a.length, three); |
796 |
> |
Object[] x = c.toArray(a); |
797 |
> |
if (x == a) |
798 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
799 |
> |
checkSanity.accept(a[i]); |
800 |
> |
// A careful reading of the spec does not support: |
801 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
802 |
> |
else |
803 |
> |
checkArraySanity.accept(x); |
804 |
> |
}, |
805 |
> |
adderRemover(c, one), |
806 |
> |
adderRemover(c, two), |
807 |
|
}; |
808 |
|
final List<Runnable> tasks = |
809 |
|
Arrays.stream(frobbers) |
861 |
|
} |
862 |
|
} |
863 |
|
|
864 |
+ |
/** |
865 |
+ |
* Spliterator.getComparator throws IllegalStateException iff the |
866 |
+ |
* spliterator does not report SORTED. |
867 |
+ |
*/ |
868 |
+ |
public void testGetComparator_IllegalStateException() { |
869 |
+ |
Collection c = impl.emptyCollection(); |
870 |
+ |
Spliterator s = c.spliterator(); |
871 |
+ |
boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); |
872 |
+ |
try { |
873 |
+ |
s.getComparator(); |
874 |
+ |
assertTrue(reportsSorted); |
875 |
+ |
} catch (IllegalStateException ex) { |
876 |
+ |
assertFalse(reportsSorted); |
877 |
+ |
} |
878 |
+ |
} |
879 |
+ |
|
880 |
|
// public void testCollection8DebugFail() { |
881 |
|
// fail(impl.klazz().getSimpleName()); |
882 |
|
// } |