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; |
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() { |
571 |
|
} catch (ConcurrentModificationException ex) { |
572 |
|
r1 = ConcurrentModificationException.class; |
573 |
|
assertFalse(impl.isConcurrent()); |
544 |
– |
} catch (UnsupportedOperationException ex) { |
545 |
– |
r1 = UnsupportedOperationException.class; |
574 |
|
} |
575 |
|
try { |
576 |
|
it2.forEachRemaining(iteratedForEachRemaining::add); |
577 |
|
r2 = iteratedForEachRemaining; |
578 |
|
} catch (ConcurrentModificationException ex) { |
579 |
|
r2 = ConcurrentModificationException.class; |
580 |
< |
} catch (UnsupportedOperationException ex) { |
553 |
< |
r2 = UnsupportedOperationException.class; |
580 |
> |
assertFalse(impl.isConcurrent()); |
581 |
|
} |
555 |
– |
if (!r1.equals(r2)) System.err.println(impl.klazz()); |
582 |
|
assertEquals(r1, r2); |
583 |
|
} |
584 |
|
|
589 |
|
public void testRemoveAfterForEachRemaining() { |
590 |
|
Collection c = impl.emptyCollection(); |
591 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
592 |
+ |
ArrayList copy = new ArrayList(); |
593 |
+ |
boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
594 |
|
testCollection: { |
595 |
|
int n = 3 + rnd.nextInt(2); |
596 |
< |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
596 |
> |
for (int i = 0; i < n; i++) { |
597 |
> |
Object x = impl.makeElement(i); |
598 |
> |
c.add(x); |
599 |
> |
copy.add(x); |
600 |
> |
} |
601 |
|
Iterator it = c.iterator(); |
602 |
< |
assertTrue(it.hasNext()); |
603 |
< |
assertEquals(impl.makeElement(0), it.next()); |
604 |
< |
assertTrue(it.hasNext()); |
605 |
< |
assertEquals(impl.makeElement(1), it.next()); |
606 |
< |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
602 |
> |
if (ordered) { |
603 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
604 |
> |
assertEquals(impl.makeElement(0), it.next()); |
605 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
606 |
> |
assertEquals(impl.makeElement(1), it.next()); |
607 |
> |
} else { |
608 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
609 |
> |
assertTrue(copy.contains(it.next())); |
610 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
611 |
> |
assertTrue(copy.contains(it.next())); |
612 |
> |
} |
613 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
614 |
> |
it.forEachRemaining( |
615 |
> |
e -> { |
616 |
> |
assertTrue(c.contains(e)); |
617 |
> |
assertTrue(copy.contains(e));}); |
618 |
|
if (testImplementationDetails) { |
619 |
|
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
620 |
|
assertIteratorExhausted(it); |
624 |
|
break testCollection; |
625 |
|
} |
626 |
|
assertEquals(n - 1, c.size()); |
627 |
< |
for (int i = 0; i < n - 1; i++) |
628 |
< |
assertTrue(c.contains(impl.makeElement(i))); |
629 |
< |
assertFalse(c.contains(impl.makeElement(n - 1))); |
627 |
> |
if (ordered) { |
628 |
> |
for (int i = 0; i < n - 1; i++) |
629 |
> |
assertTrue(c.contains(impl.makeElement(i))); |
630 |
> |
assertFalse(c.contains(impl.makeElement(n - 1))); |
631 |
> |
} |
632 |
|
} |
633 |
|
} |
634 |
|
} |
635 |
|
if (c instanceof Deque) { |
636 |
|
Deque d = (Deque) impl.emptyCollection(); |
637 |
+ |
assertTrue(ordered); |
638 |
|
int n = 3 + rnd.nextInt(2); |
639 |
|
for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); |
640 |
|
Iterator it = d.descendingIterator(); |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
+ |
* Concurrent Spliterators, once exhausted, stay exhausted. |
781 |
+ |
*/ |
782 |
+ |
public void testStickySpliteratorExhaustion() throws Throwable { |
783 |
+ |
if (!impl.isConcurrent()) return; |
784 |
+ |
if (!testImplementationDetails) return; |
785 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
786 |
+ |
final Consumer alwaysThrows = e -> { throw new AssertionError(); }; |
787 |
+ |
final Collection c = impl.emptyCollection(); |
788 |
+ |
final Spliterator s = c.spliterator(); |
789 |
+ |
if (rnd.nextBoolean()) { |
790 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
791 |
+ |
} else { |
792 |
+ |
s.forEachRemaining(alwaysThrows); |
793 |
+ |
} |
794 |
+ |
final Object one = impl.makeElement(1); |
795 |
+ |
// Spliterator should not notice added element |
796 |
+ |
c.add(one); |
797 |
+ |
if (rnd.nextBoolean()) { |
798 |
+ |
assertFalse(s.tryAdvance(alwaysThrows)); |
799 |
+ |
} else { |
800 |
+ |
s.forEachRemaining(alwaysThrows); |
801 |
+ |
} |
802 |
+ |
} |
803 |
+ |
|
804 |
+ |
/** |
805 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
806 |
|
*/ |
807 |
|
public void testDetectRaces() throws Throwable { |
927 |
|
} |
928 |
|
} |
929 |
|
|
930 |
+ |
public void testCollectionCopies() throws Exception { |
931 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
932 |
+ |
Collection c = impl.emptyCollection(); |
933 |
+ |
for (int n = rnd.nextInt(4); n--> 0; ) |
934 |
+ |
c.add(impl.makeElement(rnd.nextInt())); |
935 |
+ |
assertEquals(c, c); |
936 |
+ |
if (c instanceof List) |
937 |
+ |
assertCollectionsEquals(c, new ArrayList(c)); |
938 |
+ |
else if (c instanceof Set) |
939 |
+ |
assertCollectionsEquals(c, new HashSet(c)); |
940 |
+ |
else if (c instanceof Deque) |
941 |
+ |
assertCollectionsEquivalent(c, new ArrayDeque(c)); |
942 |
+ |
|
943 |
+ |
Collection clone = cloneableClone(c); |
944 |
+ |
if (clone != null) { |
945 |
+ |
assertSame(c.getClass(), clone.getClass()); |
946 |
+ |
assertCollectionsEquivalent(c, clone); |
947 |
+ |
} |
948 |
+ |
try { |
949 |
+ |
Collection serialClone = serialClonePossiblyFailing(c); |
950 |
+ |
assertSame(c.getClass(), serialClone.getClass()); |
951 |
+ |
assertCollectionsEquivalent(c, serialClone); |
952 |
+ |
} catch (java.io.NotSerializableException acceptable) {} |
953 |
+ |
} |
954 |
+ |
|
955 |
+ |
public void testReplaceAllIsNotStructuralModification() { |
956 |
+ |
Collection c = impl.emptyCollection(); |
957 |
+ |
if (!(c instanceof List)) |
958 |
+ |
return; |
959 |
+ |
List list = (List) c; |
960 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
961 |
+ |
for (int n = rnd.nextInt(2, 10); n--> 0; ) |
962 |
+ |
list.add(impl.makeElement(rnd.nextInt())); |
963 |
+ |
ArrayList copy = new ArrayList(list); |
964 |
+ |
int size = list.size(), half = size / 2; |
965 |
+ |
Iterator it = list.iterator(); |
966 |
+ |
for (int i = 0; i < half; i++) |
967 |
+ |
assertEquals(it.next(), copy.get(i)); |
968 |
+ |
list.replaceAll(n -> n); |
969 |
+ |
// ConcurrentModificationException must not be thrown here. |
970 |
+ |
for (int i = half; i < size; i++) |
971 |
+ |
assertEquals(it.next(), copy.get(i)); |
972 |
+ |
} |
973 |
+ |
|
974 |
|
// public void testCollection8DebugFail() { |
975 |
|
// fail(impl.klazz().getSimpleName()); |
976 |
|
// } |