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; |
19 |
|
import java.util.List; |
20 |
|
import java.util.NoSuchElementException; |
21 |
|
import java.util.Queue; |
22 |
+ |
import java.util.Set; |
23 |
|
import java.util.Spliterator; |
24 |
|
import java.util.concurrent.BlockingDeque; |
25 |
|
import java.util.concurrent.BlockingQueue; |
26 |
+ |
import java.util.concurrent.ConcurrentLinkedQueue; |
27 |
|
import java.util.concurrent.CountDownLatch; |
28 |
|
import java.util.concurrent.Executors; |
29 |
|
import java.util.concurrent.ExecutorService; |
60 |
|
|
61 |
|
Object bomb() { |
62 |
|
return new Object() { |
63 |
< |
public boolean equals(Object x) { throw new AssertionError(); } |
64 |
< |
public int hashCode() { throw new AssertionError(); } |
65 |
< |
}; |
63 |
> |
@Override public boolean equals(Object x) { throw new AssertionError(); } |
64 |
> |
@Override public int hashCode() { throw new AssertionError(); } |
65 |
> |
@Override public String toString() { throw new AssertionError(); } |
66 |
> |
}; |
67 |
|
} |
68 |
|
|
69 |
|
/** Checks properties of empty collections. */ |
94 |
|
assertTrue(c.isEmpty()); |
95 |
|
assertEquals(0, c.size()); |
96 |
|
assertEquals("[]", c.toString()); |
97 |
+ |
if (c instanceof List<?>) { |
98 |
+ |
List x = (List) c; |
99 |
+ |
assertEquals(1, x.hashCode()); |
100 |
+ |
assertEquals(x, Collections.emptyList()); |
101 |
+ |
assertEquals(Collections.emptyList(), x); |
102 |
+ |
assertEquals(-1, x.indexOf(impl.makeElement(86))); |
103 |
+ |
assertEquals(-1, x.lastIndexOf(impl.makeElement(99))); |
104 |
+ |
} |
105 |
+ |
else if (c instanceof Set<?>) { |
106 |
+ |
assertEquals(0, c.hashCode()); |
107 |
+ |
assertEquals(c, Collections.emptySet()); |
108 |
+ |
assertEquals(Collections.emptySet(), c); |
109 |
+ |
} |
110 |
|
{ |
111 |
|
Object[] a = c.toArray(); |
112 |
|
assertEquals(0, a.length); |
155 |
|
} |
156 |
|
if (c instanceof BlockingQueue) { |
157 |
|
BlockingQueue q = (BlockingQueue) c; |
158 |
< |
assertNull(q.poll(0L, MILLISECONDS)); |
158 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
159 |
|
} |
160 |
|
if (c instanceof BlockingDeque) { |
161 |
|
BlockingDeque q = (BlockingDeque) c; |
162 |
< |
assertNull(q.pollFirst(0L, MILLISECONDS)); |
163 |
< |
assertNull(q.pollLast(0L, MILLISECONDS)); |
162 |
> |
assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit())); |
163 |
> |
assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit())); |
164 |
|
} |
165 |
|
} |
166 |
|
|
267 |
|
() -> d.pop(), |
268 |
|
() -> d.descendingIterator().next()); |
269 |
|
} |
270 |
+ |
if (c instanceof List) { |
271 |
+ |
List x = (List) c; |
272 |
+ |
assertThrows( |
273 |
+ |
NoSuchElementException.class, |
274 |
+ |
() -> x.iterator().next(), |
275 |
+ |
() -> x.listIterator().next(), |
276 |
+ |
() -> x.listIterator(0).next(), |
277 |
+ |
() -> x.listIterator().previous(), |
278 |
+ |
() -> x.listIterator(0).previous()); |
279 |
+ |
} |
280 |
|
} |
281 |
|
|
282 |
|
public void testRemoveIf() { |
331 |
|
switch (rnd.nextInt(4)) { |
332 |
|
case 0: survivors.addAll(c); break; |
333 |
|
case 1: survivors.addAll(Arrays.asList(c.toArray())); break; |
334 |
< |
case 2: c.forEach(e -> survivors.add(e)); break; |
334 |
> |
case 2: c.forEach(survivors::add); break; |
335 |
|
case 3: for (Object e : c) survivors.add(e); break; |
336 |
|
} |
337 |
|
assertTrue(orig.containsAll(accepts)); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
+ |
* All elements removed in the middle of CONCURRENT traversal. |
372 |
+ |
*/ |
373 |
+ |
public void testElementRemovalDuringTraversal() { |
374 |
+ |
Collection c = impl.emptyCollection(); |
375 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
376 |
+ |
int n = rnd.nextInt(6); |
377 |
+ |
ArrayList copy = new ArrayList(); |
378 |
+ |
for (int i = 0; i < n; i++) { |
379 |
+ |
Object x = impl.makeElement(i); |
380 |
+ |
copy.add(x); |
381 |
+ |
c.add(x); |
382 |
+ |
} |
383 |
+ |
ArrayList iterated = new ArrayList(); |
384 |
+ |
ArrayList spliterated = new ArrayList(); |
385 |
+ |
Spliterator s = c.spliterator(); |
386 |
+ |
Iterator it = c.iterator(); |
387 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
388 |
+ |
assertTrue(s.tryAdvance(spliterated::add)); |
389 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
390 |
+ |
iterated.add(it.next()); |
391 |
+ |
} |
392 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
393 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
394 |
+ |
c.clear(); // TODO: many more removal methods |
395 |
+ |
if (testImplementationDetails |
396 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) { |
397 |
+ |
if (rnd.nextBoolean()) |
398 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
399 |
+ |
else |
400 |
+ |
s.forEachRemaining(alwaysThrows); |
401 |
+ |
} |
402 |
+ |
if (it.hasNext()) iterated.add(it.next()); |
403 |
+ |
if (rnd.nextBoolean()) assertIteratorExhausted(it); |
404 |
+ |
} |
405 |
+ |
assertTrue(copy.containsAll(iterated)); |
406 |
+ |
assertTrue(copy.containsAll(spliterated)); |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
/** |
410 |
+ |
* Some elements randomly disappear in the middle of traversal. |
411 |
+ |
*/ |
412 |
+ |
public void testRandomElementRemovalDuringTraversal() { |
413 |
+ |
Collection c = impl.emptyCollection(); |
414 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
415 |
+ |
int n = rnd.nextInt(6); |
416 |
+ |
ArrayList copy = new ArrayList(); |
417 |
+ |
for (int i = 0; i < n; i++) { |
418 |
+ |
Object x = impl.makeElement(i); |
419 |
+ |
copy.add(x); |
420 |
+ |
c.add(x); |
421 |
+ |
} |
422 |
+ |
ArrayList iterated = new ArrayList(); |
423 |
+ |
ArrayList spliterated = new ArrayList(); |
424 |
+ |
ArrayList removed = new ArrayList(); |
425 |
+ |
Spliterator s = c.spliterator(); |
426 |
+ |
Iterator it = c.iterator(); |
427 |
+ |
if (! (s.hasCharacteristics(Spliterator.CONCURRENT) || |
428 |
+ |
s.hasCharacteristics(Spliterator.IMMUTABLE))) |
429 |
+ |
return; |
430 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
431 |
+ |
assertTrue(s.tryAdvance(e -> {})); |
432 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
433 |
+ |
it.next(); |
434 |
+ |
} |
435 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
436 |
+ |
// TODO: many more removal methods |
437 |
+ |
if (rnd.nextBoolean()) { |
438 |
+ |
for (Iterator z = c.iterator(); z.hasNext(); ) { |
439 |
+ |
Object e = z.next(); |
440 |
+ |
if (rnd.nextBoolean()) { |
441 |
+ |
try { |
442 |
+ |
z.remove(); |
443 |
+ |
} catch (UnsupportedOperationException ok) { return; } |
444 |
+ |
removed.add(e); |
445 |
+ |
} |
446 |
+ |
} |
447 |
+ |
} else { |
448 |
+ |
Predicate randomlyRemove = e -> { |
449 |
+ |
if (rnd.nextBoolean()) { removed.add(e); return true; } |
450 |
+ |
else return false; |
451 |
+ |
}; |
452 |
+ |
c.removeIf(randomlyRemove); |
453 |
+ |
} |
454 |
+ |
s.forEachRemaining(spliterated::add); |
455 |
+ |
while (it.hasNext()) |
456 |
+ |
iterated.add(it.next()); |
457 |
+ |
assertTrue(copy.containsAll(iterated)); |
458 |
+ |
assertTrue(copy.containsAll(spliterated)); |
459 |
+ |
assertTrue(copy.containsAll(removed)); |
460 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
461 |
+ |
ArrayList iteratedAndRemoved = new ArrayList(iterated); |
462 |
+ |
ArrayList spliteratedAndRemoved = new ArrayList(spliterated); |
463 |
+ |
iteratedAndRemoved.retainAll(removed); |
464 |
+ |
spliteratedAndRemoved.retainAll(removed); |
465 |
+ |
assertTrue(iteratedAndRemoved.size() <= 1); |
466 |
+ |
assertTrue(spliteratedAndRemoved.size() <= 1); |
467 |
+ |
if (testImplementationDetails |
468 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) |
469 |
+ |
assertTrue(spliteratedAndRemoved.isEmpty()); |
470 |
+ |
} |
471 |
+ |
} |
472 |
+ |
|
473 |
+ |
/** |
474 |
|
* Various ways of traversing a collection yield same elements |
475 |
|
*/ |
476 |
< |
public void testIteratorEquivalence() { |
476 |
> |
public void testTraversalEquivalence() { |
477 |
|
Collection c = impl.emptyCollection(); |
478 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
479 |
|
int n = rnd.nextInt(6); |
482 |
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
483 |
|
ArrayList tryAdvanced = new ArrayList(); |
484 |
|
ArrayList spliterated = new ArrayList(); |
485 |
+ |
ArrayList splitonced = new ArrayList(); |
486 |
|
ArrayList forEached = new ArrayList(); |
487 |
+ |
ArrayList streamForEached = new ArrayList(); |
488 |
+ |
ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue(); |
489 |
|
ArrayList removeIfed = new ArrayList(); |
490 |
|
for (Object x : c) iterated.add(x); |
491 |
< |
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
491 |
> |
c.iterator().forEachRemaining(iteratedForEachRemaining::add); |
492 |
|
for (Spliterator s = c.spliterator(); |
493 |
< |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
494 |
< |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
495 |
< |
c.forEach(e -> forEached.add(e)); |
493 |
> |
s.tryAdvance(tryAdvanced::add); ) {} |
494 |
> |
c.spliterator().forEachRemaining(spliterated::add); |
495 |
> |
{ // trySplit returns "strict prefix" |
496 |
> |
Spliterator s1 = c.spliterator(), s2 = s1.trySplit(); |
497 |
> |
if (s2 != null) s2.forEachRemaining(splitonced::add); |
498 |
> |
s1.forEachRemaining(splitonced::add); |
499 |
> |
} |
500 |
> |
c.forEach(forEached::add); |
501 |
> |
c.stream().forEach(streamForEached::add); |
502 |
> |
c.parallelStream().forEach(parallelStreamForEached::add); |
503 |
|
c.removeIf(e -> { removeIfed.add(e); return false; }); |
504 |
|
boolean ordered = |
505 |
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
506 |
|
if (c instanceof List || c instanceof Deque) |
507 |
|
assertTrue(ordered); |
508 |
+ |
HashSet cset = new HashSet(c); |
509 |
+ |
assertEquals(cset, new HashSet(parallelStreamForEached)); |
510 |
|
if (ordered) { |
511 |
|
assertEquals(iterated, iteratedForEachRemaining); |
512 |
|
assertEquals(iterated, tryAdvanced); |
513 |
|
assertEquals(iterated, spliterated); |
514 |
+ |
assertEquals(iterated, splitonced); |
515 |
|
assertEquals(iterated, forEached); |
516 |
+ |
assertEquals(iterated, streamForEached); |
517 |
|
assertEquals(iterated, removeIfed); |
518 |
|
} else { |
375 |
– |
HashSet cset = new HashSet(c); |
519 |
|
assertEquals(cset, new HashSet(iterated)); |
520 |
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
521 |
|
assertEquals(cset, new HashSet(tryAdvanced)); |
522 |
|
assertEquals(cset, new HashSet(spliterated)); |
523 |
+ |
assertEquals(cset, new HashSet(splitonced)); |
524 |
|
assertEquals(cset, new HashSet(forEached)); |
525 |
+ |
assertEquals(cset, new HashSet(streamForEached)); |
526 |
|
assertEquals(cset, new HashSet(removeIfed)); |
527 |
|
} |
528 |
|
if (c instanceof Deque) { |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
+ |
* Iterator.forEachRemaining has same behavior as Iterator's |
545 |
+ |
* default implementation. |
546 |
+ |
*/ |
547 |
+ |
public void testForEachRemainingConsistentWithDefaultImplementation() { |
548 |
+ |
Collection c = impl.emptyCollection(); |
549 |
+ |
if (!testImplementationDetails |
550 |
+ |
|| c.getClass() == java.util.LinkedList.class) |
551 |
+ |
return; |
552 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
553 |
+ |
int n = 1 + rnd.nextInt(3); |
554 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
555 |
+ |
ArrayList iterated = new ArrayList(); |
556 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
557 |
+ |
Iterator it1 = c.iterator(); |
558 |
+ |
Iterator it2 = c.iterator(); |
559 |
+ |
assertTrue(it1.hasNext()); |
560 |
+ |
assertTrue(it2.hasNext()); |
561 |
+ |
c.clear(); |
562 |
+ |
Object r1, r2; |
563 |
+ |
try { |
564 |
+ |
while (it1.hasNext()) iterated.add(it1.next()); |
565 |
+ |
r1 = iterated; |
566 |
+ |
} catch (ConcurrentModificationException ex) { |
567 |
+ |
r1 = ConcurrentModificationException.class; |
568 |
+ |
assertFalse(impl.isConcurrent()); |
569 |
+ |
} |
570 |
+ |
try { |
571 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
572 |
+ |
r2 = iteratedForEachRemaining; |
573 |
+ |
} catch (ConcurrentModificationException ex) { |
574 |
+ |
r2 = ConcurrentModificationException.class; |
575 |
+ |
assertFalse(impl.isConcurrent()); |
576 |
+ |
} |
577 |
+ |
assertEquals(r1, r2); |
578 |
+ |
} |
579 |
+ |
|
580 |
+ |
/** |
581 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
582 |
|
* should (maybe) remove last element |
583 |
|
*/ |
716 |
|
assertTrue(found.isEmpty()); |
717 |
|
} |
718 |
|
|
719 |
+ |
/** TODO: promote to a common utility */ |
720 |
+ |
static <T> T chooseOne(T ... ts) { |
721 |
+ |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
722 |
+ |
} |
723 |
+ |
|
724 |
+ |
/** TODO: more random adders and removers */ |
725 |
+ |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
726 |
+ |
return chooseOne( |
727 |
+ |
() -> { |
728 |
+ |
assertTrue(c.add(e)); |
729 |
+ |
assertTrue(c.contains(e)); |
730 |
+ |
assertTrue(c.remove(e)); |
731 |
+ |
assertFalse(c.contains(e)); |
732 |
+ |
}, |
733 |
+ |
() -> { |
734 |
+ |
assertTrue(c.add(e)); |
735 |
+ |
assertTrue(c.contains(e)); |
736 |
+ |
assertTrue(c.removeIf(x -> x == e)); |
737 |
+ |
assertFalse(c.contains(e)); |
738 |
+ |
}, |
739 |
+ |
() -> { |
740 |
+ |
assertTrue(c.add(e)); |
741 |
+ |
assertTrue(c.contains(e)); |
742 |
+ |
for (Iterator it = c.iterator();; ) |
743 |
+ |
if (it.next() == e) { |
744 |
+ |
try { it.remove(); } |
745 |
+ |
catch (UnsupportedOperationException ok) { |
746 |
+ |
c.remove(e); |
747 |
+ |
} |
748 |
+ |
break; |
749 |
+ |
} |
750 |
+ |
assertFalse(c.contains(e)); |
751 |
+ |
}); |
752 |
+ |
} |
753 |
+ |
|
754 |
+ |
/** |
755 |
+ |
* Concurrent Spliterators, once exhausted, stay exhausted. |
756 |
+ |
*/ |
757 |
+ |
public void testStickySpliteratorExhaustion() throws Throwable { |
758 |
+ |
if (!impl.isConcurrent()) return; |
759 |
+ |
if (!testImplementationDetails) return; |
760 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
761 |
+ |
final Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
762 |
+ |
final Collection c = impl.emptyCollection(); |
763 |
+ |
final Spliterator s = c.spliterator(); |
764 |
+ |
if (rnd.nextBoolean()) { |
765 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
766 |
+ |
} else { |
767 |
+ |
s.forEachRemaining(alwaysThrows); |
768 |
+ |
} |
769 |
+ |
final Object one = impl.makeElement(1); |
770 |
+ |
// Spliterator should not notice added element |
771 |
+ |
c.add(one); |
772 |
+ |
if (rnd.nextBoolean()) { |
773 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
774 |
+ |
} else { |
775 |
+ |
s.forEachRemaining(alwaysThrows); |
776 |
+ |
} |
777 |
+ |
} |
778 |
+ |
|
779 |
|
/** |
780 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
781 |
|
*/ |
783 |
|
if (!impl.isConcurrent()) return; |
784 |
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
785 |
|
final Collection c = impl.emptyCollection(); |
786 |
< |
final long testDurationMillis = timeoutMillis(); |
786 |
> |
final long testDurationMillis |
787 |
> |
= expensiveTests ? LONG_DELAY_MS : timeoutMillis(); |
788 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
789 |
|
final Object one = impl.makeElement(1); |
790 |
|
final Object two = impl.makeElement(2); |
791 |
+ |
final Consumer checkSanity = x -> assertTrue(x == one || x == two); |
792 |
+ |
final Consumer<Object[]> checkArraySanity = array -> { |
793 |
+ |
// assertTrue(array.length <= 2); // duplicates are permitted |
794 |
+ |
for (Object x : array) assertTrue(x == one || x == two); |
795 |
+ |
}; |
796 |
+ |
final Object[] emptyArray = |
797 |
+ |
(Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); |
798 |
|
final List<Future<?>> futures; |
799 |
|
final Phaser threadsStarted = new Phaser(1); // register this thread |
800 |
|
final Runnable[] frobbers = { |
801 |
< |
() -> c.forEach(x -> assertTrue(x == one || x == two)), |
802 |
< |
() -> c.stream().forEach(x -> assertTrue(x == one || x == two)), |
801 |
> |
() -> c.forEach(checkSanity), |
802 |
> |
() -> c.stream().forEach(checkSanity), |
803 |
> |
() -> c.parallelStream().forEach(checkSanity), |
804 |
|
() -> c.spliterator().trySplit(), |
805 |
|
() -> { |
806 |
|
Spliterator s = c.spliterator(); |
807 |
< |
s.tryAdvance(x -> assertTrue(x == one || x == two)); |
807 |
> |
s.tryAdvance(checkSanity); |
808 |
|
s.trySplit(); |
809 |
|
}, |
810 |
|
() -> { |
811 |
|
Spliterator s = c.spliterator(); |
812 |
< |
do {} while (s.tryAdvance(x -> assertTrue(x == one || x == two))); |
562 |
< |
}, |
563 |
< |
() -> { |
564 |
< |
for (Object x : c) assertTrue(x == one || x == two); |
565 |
< |
}, |
566 |
< |
() -> { |
567 |
< |
assertTrue(c.add(one)); |
568 |
< |
assertTrue(c.contains(one)); |
569 |
< |
assertTrue(c.remove(one)); |
570 |
< |
assertFalse(c.contains(one)); |
812 |
> |
do {} while (s.tryAdvance(checkSanity)); |
813 |
|
}, |
814 |
+ |
() -> { for (Object x : c) checkSanity.accept(x); }, |
815 |
+ |
() -> checkArraySanity.accept(c.toArray()), |
816 |
+ |
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
817 |
|
() -> { |
818 |
< |
assertTrue(c.add(two)); |
819 |
< |
assertTrue(c.contains(two)); |
820 |
< |
assertTrue(c.remove(two)); |
821 |
< |
assertFalse(c.contains(two)); |
822 |
< |
}, |
818 |
> |
Object[] a = new Object[5]; |
819 |
> |
Object three = impl.makeElement(3); |
820 |
> |
Arrays.fill(a, 0, a.length, three); |
821 |
> |
Object[] x = c.toArray(a); |
822 |
> |
if (x == a) |
823 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
824 |
> |
checkSanity.accept(a[i]); |
825 |
> |
// A careful reading of the spec does not support: |
826 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
827 |
> |
else |
828 |
> |
checkArraySanity.accept(x); |
829 |
> |
}, |
830 |
> |
adderRemover(c, one), |
831 |
> |
adderRemover(c, two), |
832 |
|
}; |
833 |
|
final List<Runnable> tasks = |
834 |
|
Arrays.stream(frobbers) |
843 |
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
844 |
|
threadsStarted.bulkRegister(tasks.size()); |
845 |
|
futures = tasks.stream() |
846 |
< |
.map(task -> pool.submit(task)) |
846 |
> |
.map(pool::submit) |
847 |
|
.collect(Collectors.toList()); |
848 |
|
threadsStarted.arriveAndDeregister(); |
849 |
|
Thread.sleep(testDurationMillis); |
852 |
|
assertNull(future.get(0L, MILLISECONDS)); |
853 |
|
} |
854 |
|
|
855 |
+ |
/** |
856 |
+ |
* Spliterators are either IMMUTABLE or truly late-binding or, if |
857 |
+ |
* concurrent, use the same "late-binding style" of returning |
858 |
+ |
* elements added between creation and first use. |
859 |
+ |
*/ |
860 |
+ |
public void testLateBindingStyle() { |
861 |
+ |
if (!testImplementationDetails) return; |
862 |
+ |
if (impl.klazz() == ArrayList.class) return; // for jdk8 |
863 |
+ |
// Immutable (snapshot) spliterators are exempt |
864 |
+ |
if (impl.emptyCollection().spliterator() |
865 |
+ |
.hasCharacteristics(Spliterator.IMMUTABLE)) |
866 |
+ |
return; |
867 |
+ |
final Object one = impl.makeElement(1); |
868 |
+ |
{ |
869 |
+ |
final Collection c = impl.emptyCollection(); |
870 |
+ |
final Spliterator split = c.spliterator(); |
871 |
+ |
c.add(one); |
872 |
+ |
assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); |
873 |
+ |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
874 |
+ |
assertTrue(c.contains(one)); |
875 |
+ |
} |
876 |
+ |
{ |
877 |
+ |
final AtomicLong count = new AtomicLong(0); |
878 |
+ |
final Collection c = impl.emptyCollection(); |
879 |
+ |
final Spliterator split = c.spliterator(); |
880 |
+ |
c.add(one); |
881 |
+ |
split.forEachRemaining( |
882 |
+ |
e -> { assertSame(e, one); count.getAndIncrement(); }); |
883 |
+ |
assertEquals(1L, count.get()); |
884 |
+ |
assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); |
885 |
+ |
assertTrue(c.contains(one)); |
886 |
+ |
} |
887 |
+ |
} |
888 |
+ |
|
889 |
+ |
/** |
890 |
+ |
* Spliterator.getComparator throws IllegalStateException iff the |
891 |
+ |
* spliterator does not report SORTED. |
892 |
+ |
*/ |
893 |
+ |
public void testGetComparator_IllegalStateException() { |
894 |
+ |
Collection c = impl.emptyCollection(); |
895 |
+ |
Spliterator s = c.spliterator(); |
896 |
+ |
boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); |
897 |
+ |
try { |
898 |
+ |
s.getComparator(); |
899 |
+ |
assertTrue(reportsSorted); |
900 |
+ |
} catch (IllegalStateException ex) { |
901 |
+ |
assertFalse(reportsSorted); |
902 |
+ |
} |
903 |
+ |
} |
904 |
+ |
|
905 |
|
// public void testCollection8DebugFail() { |
906 |
|
// fail(impl.klazz().getSimpleName()); |
907 |
|
// } |