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() { |
570 |
|
} catch (ConcurrentModificationException ex) { |
571 |
|
r1 = ConcurrentModificationException.class; |
572 |
|
assertFalse(impl.isConcurrent()); |
544 |
– |
} catch (UnsupportedOperationException ex) { |
545 |
– |
r1 = UnsupportedOperationException.class; |
573 |
|
} |
574 |
|
try { |
575 |
|
it2.forEachRemaining(iteratedForEachRemaining::add); |
576 |
|
r2 = iteratedForEachRemaining; |
577 |
|
} catch (ConcurrentModificationException ex) { |
578 |
|
r2 = ConcurrentModificationException.class; |
579 |
< |
} catch (UnsupportedOperationException ex) { |
553 |
< |
r2 = UnsupportedOperationException.class; |
579 |
> |
assertFalse(impl.isConcurrent()); |
580 |
|
} |
581 |
|
assertEquals(r1, r2); |
582 |
|
} |
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 { |
906 |
|
} |
907 |
|
} |
908 |
|
|
909 |
+ |
public void testObjectMethods() { |
910 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
911 |
+ |
Collection c = impl.emptyCollection(); |
912 |
+ |
for (int n = rnd.nextInt(3); n--> 0; ) |
913 |
+ |
c.add(impl.makeElement(rnd.nextInt())); |
914 |
+ |
assertEquals(c, c); |
915 |
+ |
if (c instanceof List) { |
916 |
+ |
List copy = new ArrayList(c); |
917 |
+ |
assertEquals(copy, c); |
918 |
+ |
assertEquals(c, copy); |
919 |
+ |
assertEquals(copy.hashCode(), c.hashCode()); |
920 |
+ |
} |
921 |
+ |
if (c instanceof Set) { |
922 |
+ |
Set copy = new HashSet(c); |
923 |
+ |
assertEquals(copy, c); |
924 |
+ |
assertEquals(c, copy); |
925 |
+ |
assertEquals(copy.hashCode(), c.hashCode()); |
926 |
+ |
} |
927 |
+ |
} |
928 |
+ |
|
929 |
|
// public void testCollection8DebugFail() { |
930 |
|
// fail(impl.klazz().getSimpleName()); |
931 |
|
// } |