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; |
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 |
+ |
assertThrows( |
105 |
+ |
IndexOutOfBoundsException.class, |
106 |
+ |
() -> x.get(0), |
107 |
+ |
() -> x.set(0, impl.makeElement(42))); |
108 |
+ |
} |
109 |
+ |
else if (c instanceof Set<?>) { |
110 |
+ |
assertEquals(0, c.hashCode()); |
111 |
+ |
assertEquals(c, Collections.emptySet()); |
112 |
+ |
assertEquals(Collections.emptySet(), c); |
113 |
+ |
} |
114 |
|
{ |
115 |
|
Object[] a = c.toArray(); |
116 |
|
assertEquals(0, a.length); |
159 |
|
} |
160 |
|
if (c instanceof BlockingQueue) { |
161 |
|
BlockingQueue q = (BlockingQueue) c; |
162 |
< |
assertNull(q.poll(0L, MILLISECONDS)); |
162 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
163 |
|
} |
164 |
|
if (c instanceof BlockingDeque) { |
165 |
|
BlockingDeque q = (BlockingDeque) c; |
166 |
< |
assertNull(q.pollFirst(0L, MILLISECONDS)); |
167 |
< |
assertNull(q.pollLast(0L, MILLISECONDS)); |
166 |
> |
assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit())); |
167 |
> |
assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit())); |
168 |
|
} |
169 |
|
} |
170 |
|
|
271 |
|
() -> d.pop(), |
272 |
|
() -> d.descendingIterator().next()); |
273 |
|
} |
274 |
+ |
if (c instanceof List) { |
275 |
+ |
List x = (List) c; |
276 |
+ |
assertThrows( |
277 |
+ |
NoSuchElementException.class, |
278 |
+ |
() -> x.iterator().next(), |
279 |
+ |
() -> x.listIterator().next(), |
280 |
+ |
() -> x.listIterator(0).next(), |
281 |
+ |
() -> x.listIterator().previous(), |
282 |
+ |
() -> x.listIterator(0).previous()); |
283 |
+ |
} |
284 |
|
} |
285 |
|
|
286 |
|
public void testRemoveIf() { |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
+ |
* All elements removed in the middle of CONCURRENT traversal. |
376 |
+ |
*/ |
377 |
+ |
public void testElementRemovalDuringTraversal() { |
378 |
+ |
Collection c = impl.emptyCollection(); |
379 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
380 |
+ |
int n = rnd.nextInt(6); |
381 |
+ |
ArrayList copy = new ArrayList(); |
382 |
+ |
for (int i = 0; i < n; i++) { |
383 |
+ |
Object x = impl.makeElement(i); |
384 |
+ |
copy.add(x); |
385 |
+ |
c.add(x); |
386 |
+ |
} |
387 |
+ |
ArrayList iterated = new ArrayList(); |
388 |
+ |
ArrayList spliterated = new ArrayList(); |
389 |
+ |
Spliterator s = c.spliterator(); |
390 |
+ |
Iterator it = c.iterator(); |
391 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
392 |
+ |
assertTrue(s.tryAdvance(spliterated::add)); |
393 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
394 |
+ |
iterated.add(it.next()); |
395 |
+ |
} |
396 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
397 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
398 |
+ |
c.clear(); // TODO: many more removal methods |
399 |
+ |
if (testImplementationDetails |
400 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) { |
401 |
+ |
if (rnd.nextBoolean()) |
402 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
403 |
+ |
else |
404 |
+ |
s.forEachRemaining(alwaysThrows); |
405 |
+ |
} |
406 |
+ |
if (it.hasNext()) iterated.add(it.next()); |
407 |
+ |
if (rnd.nextBoolean()) assertIteratorExhausted(it); |
408 |
+ |
} |
409 |
+ |
assertTrue(copy.containsAll(iterated)); |
410 |
+ |
assertTrue(copy.containsAll(spliterated)); |
411 |
+ |
} |
412 |
+ |
|
413 |
+ |
/** |
414 |
+ |
* Some elements randomly disappear in the middle of traversal. |
415 |
+ |
*/ |
416 |
+ |
public void testRandomElementRemovalDuringTraversal() { |
417 |
+ |
Collection c = impl.emptyCollection(); |
418 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
419 |
+ |
int n = rnd.nextInt(6); |
420 |
+ |
ArrayList copy = new ArrayList(); |
421 |
+ |
for (int i = 0; i < n; i++) { |
422 |
+ |
Object x = impl.makeElement(i); |
423 |
+ |
copy.add(x); |
424 |
+ |
c.add(x); |
425 |
+ |
} |
426 |
+ |
ArrayList iterated = new ArrayList(); |
427 |
+ |
ArrayList spliterated = new ArrayList(); |
428 |
+ |
ArrayList removed = new ArrayList(); |
429 |
+ |
Spliterator s = c.spliterator(); |
430 |
+ |
Iterator it = c.iterator(); |
431 |
+ |
if (! (s.hasCharacteristics(Spliterator.CONCURRENT) || |
432 |
+ |
s.hasCharacteristics(Spliterator.IMMUTABLE))) |
433 |
+ |
return; |
434 |
+ |
for (int i = rnd.nextInt(n + 1); --i >= 0; ) { |
435 |
+ |
assertTrue(s.tryAdvance(e -> {})); |
436 |
+ |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
437 |
+ |
it.next(); |
438 |
+ |
} |
439 |
+ |
Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
440 |
+ |
// TODO: many more removal methods |
441 |
+ |
if (rnd.nextBoolean()) { |
442 |
+ |
for (Iterator z = c.iterator(); z.hasNext(); ) { |
443 |
+ |
Object e = z.next(); |
444 |
+ |
if (rnd.nextBoolean()) { |
445 |
+ |
try { |
446 |
+ |
z.remove(); |
447 |
+ |
} catch (UnsupportedOperationException ok) { return; } |
448 |
+ |
removed.add(e); |
449 |
+ |
} |
450 |
+ |
} |
451 |
+ |
} else { |
452 |
+ |
Predicate randomlyRemove = e -> { |
453 |
+ |
if (rnd.nextBoolean()) { removed.add(e); return true; } |
454 |
+ |
else return false; |
455 |
+ |
}; |
456 |
+ |
c.removeIf(randomlyRemove); |
457 |
+ |
} |
458 |
+ |
s.forEachRemaining(spliterated::add); |
459 |
+ |
while (it.hasNext()) |
460 |
+ |
iterated.add(it.next()); |
461 |
+ |
assertTrue(copy.containsAll(iterated)); |
462 |
+ |
assertTrue(copy.containsAll(spliterated)); |
463 |
+ |
assertTrue(copy.containsAll(removed)); |
464 |
+ |
if (s.hasCharacteristics(Spliterator.CONCURRENT)) { |
465 |
+ |
ArrayList iteratedAndRemoved = new ArrayList(iterated); |
466 |
+ |
ArrayList spliteratedAndRemoved = new ArrayList(spliterated); |
467 |
+ |
iteratedAndRemoved.retainAll(removed); |
468 |
+ |
spliteratedAndRemoved.retainAll(removed); |
469 |
+ |
assertTrue(iteratedAndRemoved.size() <= 1); |
470 |
+ |
assertTrue(spliteratedAndRemoved.size() <= 1); |
471 |
+ |
if (testImplementationDetails |
472 |
+ |
&& !(c instanceof java.util.concurrent.ArrayBlockingQueue)) |
473 |
+ |
assertTrue(spliteratedAndRemoved.isEmpty()); |
474 |
+ |
} |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
/** |
478 |
|
* Various ways of traversing a collection yield same elements |
479 |
|
*/ |
480 |
< |
public void testIteratorEquivalence() { |
480 |
> |
public void testTraversalEquivalence() { |
481 |
|
Collection c = impl.emptyCollection(); |
482 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
483 |
|
int n = rnd.nextInt(6); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
+ |
* Iterator.forEachRemaining has same behavior as Iterator's |
549 |
+ |
* default implementation. |
550 |
+ |
*/ |
551 |
+ |
public void testForEachRemainingConsistentWithDefaultImplementation() { |
552 |
+ |
Collection c = impl.emptyCollection(); |
553 |
+ |
if (!testImplementationDetails |
554 |
+ |
|| c.getClass() == java.util.LinkedList.class) |
555 |
+ |
return; |
556 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
557 |
+ |
int n = 1 + rnd.nextInt(3); |
558 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
559 |
+ |
ArrayList iterated = new ArrayList(); |
560 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
561 |
+ |
Iterator it1 = c.iterator(); |
562 |
+ |
Iterator it2 = c.iterator(); |
563 |
+ |
assertTrue(it1.hasNext()); |
564 |
+ |
assertTrue(it2.hasNext()); |
565 |
+ |
c.clear(); |
566 |
+ |
Object r1, r2; |
567 |
+ |
try { |
568 |
+ |
while (it1.hasNext()) iterated.add(it1.next()); |
569 |
+ |
r1 = iterated; |
570 |
+ |
} catch (ConcurrentModificationException ex) { |
571 |
+ |
r1 = ConcurrentModificationException.class; |
572 |
+ |
assertFalse(impl.isConcurrent()); |
573 |
+ |
} |
574 |
+ |
try { |
575 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
576 |
+ |
r2 = iteratedForEachRemaining; |
577 |
+ |
} catch (ConcurrentModificationException ex) { |
578 |
+ |
r2 = ConcurrentModificationException.class; |
579 |
+ |
assertFalse(impl.isConcurrent()); |
580 |
+ |
} |
581 |
+ |
assertEquals(r1, r2); |
582 |
+ |
} |
583 |
+ |
|
584 |
+ |
/** |
585 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
586 |
|
* should (maybe) remove last element |
587 |
|
*/ |
720 |
|
assertTrue(found.isEmpty()); |
721 |
|
} |
722 |
|
|
723 |
+ |
/** TODO: promote to a common utility */ |
724 |
+ |
static <T> T chooseOne(T ... ts) { |
725 |
+ |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
726 |
+ |
} |
727 |
+ |
|
728 |
+ |
/** TODO: more random adders and removers */ |
729 |
+ |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
730 |
+ |
return chooseOne( |
731 |
+ |
() -> { |
732 |
+ |
assertTrue(c.add(e)); |
733 |
+ |
assertTrue(c.contains(e)); |
734 |
+ |
assertTrue(c.remove(e)); |
735 |
+ |
assertFalse(c.contains(e)); |
736 |
+ |
}, |
737 |
+ |
() -> { |
738 |
+ |
assertTrue(c.add(e)); |
739 |
+ |
assertTrue(c.contains(e)); |
740 |
+ |
assertTrue(c.removeIf(x -> x == e)); |
741 |
+ |
assertFalse(c.contains(e)); |
742 |
+ |
}, |
743 |
+ |
() -> { |
744 |
+ |
assertTrue(c.add(e)); |
745 |
+ |
assertTrue(c.contains(e)); |
746 |
+ |
for (Iterator it = c.iterator();; ) |
747 |
+ |
if (it.next() == e) { |
748 |
+ |
try { it.remove(); } |
749 |
+ |
catch (UnsupportedOperationException ok) { |
750 |
+ |
c.remove(e); |
751 |
+ |
} |
752 |
+ |
break; |
753 |
+ |
} |
754 |
+ |
assertFalse(c.contains(e)); |
755 |
+ |
}); |
756 |
+ |
} |
757 |
+ |
|
758 |
+ |
/** |
759 |
+ |
* Concurrent Spliterators, once exhausted, stay exhausted. |
760 |
+ |
*/ |
761 |
+ |
public void testStickySpliteratorExhaustion() throws Throwable { |
762 |
+ |
if (!impl.isConcurrent()) return; |
763 |
+ |
if (!testImplementationDetails) return; |
764 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
765 |
+ |
final Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
766 |
+ |
final Collection c = impl.emptyCollection(); |
767 |
+ |
final Spliterator s = c.spliterator(); |
768 |
+ |
if (rnd.nextBoolean()) { |
769 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
770 |
+ |
} else { |
771 |
+ |
s.forEachRemaining(alwaysThrows); |
772 |
+ |
} |
773 |
+ |
final Object one = impl.makeElement(1); |
774 |
+ |
// Spliterator should not notice added element |
775 |
+ |
c.add(one); |
776 |
+ |
if (rnd.nextBoolean()) { |
777 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
778 |
+ |
} else { |
779 |
+ |
s.forEachRemaining(alwaysThrows); |
780 |
+ |
} |
781 |
+ |
} |
782 |
+ |
|
783 |
|
/** |
784 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
785 |
|
*/ |
819 |
|
() -> checkArraySanity.accept(c.toArray()), |
820 |
|
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
821 |
|
() -> { |
822 |
< |
assertTrue(c.add(one)); |
823 |
< |
assertTrue(c.contains(one)); |
824 |
< |
assertTrue(c.remove(one)); |
825 |
< |
assertFalse(c.contains(one)); |
826 |
< |
}, |
827 |
< |
() -> { |
828 |
< |
assertTrue(c.add(two)); |
829 |
< |
assertTrue(c.contains(two)); |
830 |
< |
assertTrue(c.remove(two)); |
831 |
< |
assertFalse(c.contains(two)); |
832 |
< |
}, |
822 |
> |
Object[] a = new Object[5]; |
823 |
> |
Object three = impl.makeElement(3); |
824 |
> |
Arrays.fill(a, 0, a.length, three); |
825 |
> |
Object[] x = c.toArray(a); |
826 |
> |
if (x == a) |
827 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
828 |
> |
checkSanity.accept(a[i]); |
829 |
> |
// A careful reading of the spec does not support: |
830 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
831 |
> |
else |
832 |
> |
checkArraySanity.accept(x); |
833 |
> |
}, |
834 |
> |
adderRemover(c, one), |
835 |
> |
adderRemover(c, two), |
836 |
|
}; |
837 |
|
final List<Runnable> tasks = |
838 |
|
Arrays.stream(frobbers) |
890 |
|
} |
891 |
|
} |
892 |
|
|
893 |
+ |
/** |
894 |
+ |
* Spliterator.getComparator throws IllegalStateException iff the |
895 |
+ |
* spliterator does not report SORTED. |
896 |
+ |
*/ |
897 |
+ |
public void testGetComparator_IllegalStateException() { |
898 |
+ |
Collection c = impl.emptyCollection(); |
899 |
+ |
Spliterator s = c.spliterator(); |
900 |
+ |
boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); |
901 |
+ |
try { |
902 |
+ |
s.getComparator(); |
903 |
+ |
assertTrue(reportsSorted); |
904 |
+ |
} catch (IllegalStateException ex) { |
905 |
+ |
assertFalse(reportsSorted); |
906 |
+ |
} |
907 |
+ |
} |
908 |
+ |
|
909 |
|
// public void testCollection8DebugFail() { |
910 |
|
// fail(impl.klazz().getSimpleName()); |
911 |
|
// } |