8 |
|
import static java.util.concurrent.TimeUnit.HOURS; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
+ |
import java.util.ArrayDeque; |
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.Arrays; |
14 |
|
import java.util.Collection; |
15 |
|
import java.util.Collections; |
16 |
+ |
import java.util.ConcurrentModificationException; |
17 |
|
import java.util.Deque; |
18 |
|
import java.util.HashSet; |
19 |
|
import java.util.Iterator; |
20 |
|
import java.util.List; |
21 |
|
import java.util.NoSuchElementException; |
22 |
|
import java.util.Queue; |
23 |
+ |
import java.util.Set; |
24 |
|
import java.util.Spliterator; |
25 |
|
import java.util.concurrent.BlockingDeque; |
26 |
|
import java.util.concurrent.BlockingQueue; |
61 |
|
|
62 |
|
Object bomb() { |
63 |
|
return new Object() { |
64 |
< |
public boolean equals(Object x) { throw new AssertionError(); } |
65 |
< |
public int hashCode() { throw new AssertionError(); } |
66 |
< |
}; |
64 |
> |
@Override public boolean equals(Object x) { throw new AssertionError(); } |
65 |
> |
@Override public int hashCode() { throw new AssertionError(); } |
66 |
> |
@Override public String toString() { throw new AssertionError(); } |
67 |
> |
}; |
68 |
|
} |
69 |
|
|
70 |
|
/** Checks properties of empty collections. */ |
95 |
|
assertTrue(c.isEmpty()); |
96 |
|
assertEquals(0, c.size()); |
97 |
|
assertEquals("[]", c.toString()); |
98 |
+ |
if (c instanceof List<?>) { |
99 |
+ |
List x = (List) c; |
100 |
+ |
assertEquals(1, x.hashCode()); |
101 |
+ |
assertEquals(x, Collections.emptyList()); |
102 |
+ |
assertEquals(Collections.emptyList(), x); |
103 |
+ |
assertEquals(-1, x.indexOf(impl.makeElement(86))); |
104 |
+ |
assertEquals(-1, x.lastIndexOf(impl.makeElement(99))); |
105 |
+ |
assertThrows( |
106 |
+ |
IndexOutOfBoundsException.class, |
107 |
+ |
() -> x.get(0), |
108 |
+ |
() -> x.set(0, impl.makeElement(42))); |
109 |
+ |
} |
110 |
+ |
else if (c instanceof Set<?>) { |
111 |
+ |
assertEquals(0, c.hashCode()); |
112 |
+ |
assertEquals(c, Collections.emptySet()); |
113 |
+ |
assertEquals(Collections.emptySet(), c); |
114 |
+ |
} |
115 |
|
{ |
116 |
|
Object[] a = c.toArray(); |
117 |
|
assertEquals(0, a.length); |
160 |
|
} |
161 |
|
if (c instanceof BlockingQueue) { |
162 |
|
BlockingQueue q = (BlockingQueue) c; |
163 |
< |
assertNull(q.poll(0L, MILLISECONDS)); |
163 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
164 |
|
} |
165 |
|
if (c instanceof BlockingDeque) { |
166 |
|
BlockingDeque q = (BlockingDeque) c; |
167 |
< |
assertNull(q.pollFirst(0L, MILLISECONDS)); |
168 |
< |
assertNull(q.pollLast(0L, MILLISECONDS)); |
167 |
> |
assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit())); |
168 |
> |
assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit())); |
169 |
|
} |
170 |
|
} |
171 |
|
|
272 |
|
() -> d.pop(), |
273 |
|
() -> d.descendingIterator().next()); |
274 |
|
} |
275 |
+ |
if (c instanceof List) { |
276 |
+ |
List x = (List) c; |
277 |
+ |
assertThrows( |
278 |
+ |
NoSuchElementException.class, |
279 |
+ |
() -> x.iterator().next(), |
280 |
+ |
() -> x.listIterator().next(), |
281 |
+ |
() -> x.listIterator(0).next(), |
282 |
+ |
() -> x.listIterator().previous(), |
283 |
+ |
() -> x.listIterator(0).previous()); |
284 |
+ |
} |
285 |
|
} |
286 |
|
|
287 |
|
public void testRemoveIf() { |
478 |
|
/** |
479 |
|
* Various ways of traversing a collection yield same elements |
480 |
|
*/ |
481 |
< |
public void testIteratorEquivalence() { |
481 |
> |
public void testTraversalEquivalence() { |
482 |
|
Collection c = impl.emptyCollection(); |
483 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
484 |
|
int n = rnd.nextInt(6); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
+ |
* Iterator.forEachRemaining has same behavior as Iterator's |
550 |
+ |
* default implementation. |
551 |
+ |
*/ |
552 |
+ |
public void testForEachRemainingConsistentWithDefaultImplementation() { |
553 |
+ |
Collection c = impl.emptyCollection(); |
554 |
+ |
if (!testImplementationDetails |
555 |
+ |
|| c.getClass() == java.util.LinkedList.class) |
556 |
+ |
return; |
557 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
558 |
+ |
int n = 1 + rnd.nextInt(3); |
559 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
560 |
+ |
ArrayList iterated = new ArrayList(); |
561 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
562 |
+ |
Iterator it1 = c.iterator(); |
563 |
+ |
Iterator it2 = c.iterator(); |
564 |
+ |
assertTrue(it1.hasNext()); |
565 |
+ |
assertTrue(it2.hasNext()); |
566 |
+ |
c.clear(); |
567 |
+ |
Object r1, r2; |
568 |
+ |
try { |
569 |
+ |
while (it1.hasNext()) iterated.add(it1.next()); |
570 |
+ |
r1 = iterated; |
571 |
+ |
} catch (ConcurrentModificationException ex) { |
572 |
+ |
r1 = ConcurrentModificationException.class; |
573 |
+ |
assertFalse(impl.isConcurrent()); |
574 |
+ |
} |
575 |
+ |
try { |
576 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
577 |
+ |
r2 = iteratedForEachRemaining; |
578 |
+ |
} catch (ConcurrentModificationException ex) { |
579 |
+ |
r2 = ConcurrentModificationException.class; |
580 |
+ |
assertFalse(impl.isConcurrent()); |
581 |
+ |
} |
582 |
+ |
assertEquals(r1, r2); |
583 |
+ |
} |
584 |
+ |
|
585 |
+ |
/** |
586 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
587 |
|
* should (maybe) remove last element |
588 |
|
*/ |
757 |
|
} |
758 |
|
|
759 |
|
/** |
760 |
+ |
* Concurrent Spliterators, once exhausted, stay exhausted. |
761 |
+ |
*/ |
762 |
+ |
public void testStickySpliteratorExhaustion() throws Throwable { |
763 |
+ |
if (!impl.isConcurrent()) return; |
764 |
+ |
if (!testImplementationDetails) return; |
765 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
766 |
+ |
final Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
767 |
+ |
final Collection c = impl.emptyCollection(); |
768 |
+ |
final Spliterator s = c.spliterator(); |
769 |
+ |
if (rnd.nextBoolean()) { |
770 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
771 |
+ |
} else { |
772 |
+ |
s.forEachRemaining(alwaysThrows); |
773 |
+ |
} |
774 |
+ |
final Object one = impl.makeElement(1); |
775 |
+ |
// Spliterator should not notice added element |
776 |
+ |
c.add(one); |
777 |
+ |
if (rnd.nextBoolean()) { |
778 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
779 |
+ |
} else { |
780 |
+ |
s.forEachRemaining(alwaysThrows); |
781 |
+ |
} |
782 |
+ |
} |
783 |
+ |
|
784 |
+ |
/** |
785 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
786 |
|
*/ |
787 |
|
public void testDetectRaces() throws Throwable { |
891 |
|
} |
892 |
|
} |
893 |
|
|
894 |
+ |
/** |
895 |
+ |
* Spliterator.getComparator throws IllegalStateException iff the |
896 |
+ |
* spliterator does not report SORTED. |
897 |
+ |
*/ |
898 |
+ |
public void testGetComparator_IllegalStateException() { |
899 |
+ |
Collection c = impl.emptyCollection(); |
900 |
+ |
Spliterator s = c.spliterator(); |
901 |
+ |
boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); |
902 |
+ |
try { |
903 |
+ |
s.getComparator(); |
904 |
+ |
assertTrue(reportsSorted); |
905 |
+ |
} catch (IllegalStateException ex) { |
906 |
+ |
assertFalse(reportsSorted); |
907 |
+ |
} |
908 |
+ |
} |
909 |
+ |
|
910 |
+ |
public void testCollectionCopies() throws Exception { |
911 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
912 |
+ |
Collection c = impl.emptyCollection(); |
913 |
+ |
for (int n = rnd.nextInt(4); n--> 0; ) |
914 |
+ |
c.add(impl.makeElement(rnd.nextInt())); |
915 |
+ |
assertEquals(c, c); |
916 |
+ |
if (c instanceof List) |
917 |
+ |
assertCollectionsEquals(c, new ArrayList(c)); |
918 |
+ |
else if (c instanceof Set) |
919 |
+ |
assertCollectionsEquals(c, new HashSet(c)); |
920 |
+ |
else if (c instanceof Deque) |
921 |
+ |
assertCollectionsEquivalent(c, new ArrayDeque(c)); |
922 |
+ |
|
923 |
+ |
Collection clone = cloneableClone(c); |
924 |
+ |
if (clone != null) { |
925 |
+ |
assertSame(c.getClass(), clone.getClass()); |
926 |
+ |
assertCollectionsEquivalent(c, clone); |
927 |
+ |
} |
928 |
+ |
try { |
929 |
+ |
Collection serialClone = serialClonePossiblyFailing(c); |
930 |
+ |
assertSame(c.getClass(), serialClone.getClass()); |
931 |
+ |
assertCollectionsEquivalent(c, serialClone); |
932 |
+ |
} catch (java.io.NotSerializableException acceptable) {} |
933 |
+ |
} |
934 |
+ |
|
935 |
+ |
public void testReplaceAllIsNotStructuralModification() { |
936 |
+ |
Collection c = impl.emptyCollection(); |
937 |
+ |
if (!(c instanceof List)) |
938 |
+ |
return; |
939 |
+ |
List list = (List) c; |
940 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
941 |
+ |
for (int n = rnd.nextInt(2, 10); n--> 0; ) |
942 |
+ |
list.add(impl.makeElement(rnd.nextInt())); |
943 |
+ |
ArrayList copy = new ArrayList(list); |
944 |
+ |
int size = list.size(), half = size / 2; |
945 |
+ |
Iterator it = list.iterator(); |
946 |
+ |
for (int i = 0; i < half; i++) |
947 |
+ |
assertEquals(it.next(), copy.get(i)); |
948 |
+ |
list.replaceAll(n -> n); |
949 |
+ |
// ConcurrentModificationException must not be thrown here. |
950 |
+ |
for (int i = half; i < size; i++) |
951 |
+ |
assertEquals(it.next(), copy.get(i)); |
952 |
+ |
} |
953 |
+ |
|
954 |
|
// public void testCollection8DebugFail() { |
955 |
|
// fail(impl.klazz().getSimpleName()); |
956 |
|
// } |