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() { |
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 |
|
*/ |
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 |
|
*/ |
786 |
|
public void testDetectRaces() throws Throwable { |
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 |
|
// } |