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; |
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()); |
182 |
|
() -> c.iterator().forEachRemaining(null), |
183 |
|
() -> c.spliterator().forEachRemaining(null), |
184 |
|
() -> c.spliterator().tryAdvance(null), |
185 |
< |
() -> c.toArray(null)); |
185 |
> |
() -> c.toArray((Object[])null)); |
186 |
|
|
187 |
|
if (!impl.permitsNulls()) { |
188 |
|
assertThrows( |
210 |
|
BlockingQueue q = (BlockingQueue) c; |
211 |
|
assertThrows( |
212 |
|
NullPointerException.class, |
213 |
< |
() -> { |
214 |
< |
try { q.offer(null, 1L, HOURS); } |
210 |
< |
catch (InterruptedException ex) { |
211 |
< |
throw new AssertionError(ex); |
212 |
< |
}}, |
213 |
< |
() -> { |
214 |
< |
try { q.put(null); } |
215 |
< |
catch (InterruptedException ex) { |
216 |
< |
throw new AssertionError(ex); |
217 |
< |
}}); |
213 |
> |
() -> q.offer(null, 1L, HOURS), |
214 |
> |
() -> q.put(null)); |
215 |
|
} |
216 |
|
if (c instanceof BlockingDeque) { |
217 |
|
BlockingDeque q = (BlockingDeque) c; |
218 |
|
assertThrows( |
219 |
|
NullPointerException.class, |
220 |
< |
() -> { |
221 |
< |
try { q.offerFirst(null, 1L, HOURS); } |
222 |
< |
catch (InterruptedException ex) { |
223 |
< |
throw new AssertionError(ex); |
227 |
< |
}}, |
228 |
< |
() -> { |
229 |
< |
try { q.offerLast(null, 1L, HOURS); } |
230 |
< |
catch (InterruptedException ex) { |
231 |
< |
throw new AssertionError(ex); |
232 |
< |
}}, |
233 |
< |
() -> { |
234 |
< |
try { q.putFirst(null); } |
235 |
< |
catch (InterruptedException ex) { |
236 |
< |
throw new AssertionError(ex); |
237 |
< |
}}, |
238 |
< |
() -> { |
239 |
< |
try { q.putLast(null); } |
240 |
< |
catch (InterruptedException ex) { |
241 |
< |
throw new AssertionError(ex); |
242 |
< |
}}); |
220 |
> |
() -> q.offerFirst(null, 1L, HOURS), |
221 |
> |
() -> q.offerLast(null, 1L, HOURS), |
222 |
> |
() -> q.putFirst(null), |
223 |
> |
() -> q.putLast(null)); |
224 |
|
} |
225 |
|
} |
226 |
|
|
565 |
|
public void testRemoveAfterForEachRemaining() { |
566 |
|
Collection c = impl.emptyCollection(); |
567 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
568 |
+ |
ArrayList copy = new ArrayList(); |
569 |
+ |
boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
570 |
|
testCollection: { |
571 |
|
int n = 3 + rnd.nextInt(2); |
572 |
< |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
572 |
> |
for (int i = 0; i < n; i++) { |
573 |
> |
Object x = impl.makeElement(i); |
574 |
> |
c.add(x); |
575 |
> |
copy.add(x); |
576 |
> |
} |
577 |
|
Iterator it = c.iterator(); |
578 |
< |
assertTrue(it.hasNext()); |
579 |
< |
assertEquals(impl.makeElement(0), it.next()); |
580 |
< |
assertTrue(it.hasNext()); |
581 |
< |
assertEquals(impl.makeElement(1), it.next()); |
582 |
< |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
578 |
> |
if (ordered) { |
579 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
580 |
> |
assertEquals(impl.makeElement(0), it.next()); |
581 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
582 |
> |
assertEquals(impl.makeElement(1), it.next()); |
583 |
> |
} else { |
584 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
585 |
> |
assertTrue(copy.contains(it.next())); |
586 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
587 |
> |
assertTrue(copy.contains(it.next())); |
588 |
> |
} |
589 |
> |
if (rnd.nextBoolean()) assertTrue(it.hasNext()); |
590 |
> |
it.forEachRemaining( |
591 |
> |
e -> { |
592 |
> |
assertTrue(c.contains(e)); |
593 |
> |
assertTrue(copy.contains(e));}); |
594 |
|
if (testImplementationDetails) { |
595 |
|
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
596 |
|
assertIteratorExhausted(it); |
600 |
|
break testCollection; |
601 |
|
} |
602 |
|
assertEquals(n - 1, c.size()); |
603 |
< |
for (int i = 0; i < n - 1; i++) |
604 |
< |
assertTrue(c.contains(impl.makeElement(i))); |
605 |
< |
assertFalse(c.contains(impl.makeElement(n - 1))); |
603 |
> |
if (ordered) { |
604 |
> |
for (int i = 0; i < n - 1; i++) |
605 |
> |
assertTrue(c.contains(impl.makeElement(i))); |
606 |
> |
assertFalse(c.contains(impl.makeElement(n - 1))); |
607 |
> |
} |
608 |
|
} |
609 |
|
} |
610 |
|
} |
611 |
|
if (c instanceof Deque) { |
612 |
|
Deque d = (Deque) impl.emptyCollection(); |
613 |
+ |
assertTrue(ordered); |
614 |
|
int n = 3 + rnd.nextInt(2); |
615 |
|
for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); |
616 |
|
Iterator it = d.descendingIterator(); |
903 |
|
} |
904 |
|
} |
905 |
|
|
906 |
+ |
public void testCollectionCopies() throws Exception { |
907 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
908 |
+ |
Collection c = impl.emptyCollection(); |
909 |
+ |
for (int n = rnd.nextInt(4); n--> 0; ) |
910 |
+ |
c.add(impl.makeElement(rnd.nextInt())); |
911 |
+ |
assertEquals(c, c); |
912 |
+ |
if (c instanceof List) |
913 |
+ |
assertCollectionsEquals(c, new ArrayList(c)); |
914 |
+ |
else if (c instanceof Set) |
915 |
+ |
assertCollectionsEquals(c, new HashSet(c)); |
916 |
+ |
else if (c instanceof Deque) |
917 |
+ |
assertCollectionsEquivalent(c, new ArrayDeque(c)); |
918 |
+ |
|
919 |
+ |
Collection clone = cloneableClone(c); |
920 |
+ |
if (clone != null) { |
921 |
+ |
assertSame(c.getClass(), clone.getClass()); |
922 |
+ |
assertCollectionsEquivalent(c, clone); |
923 |
+ |
} |
924 |
+ |
try { |
925 |
+ |
Collection serialClone = serialClonePossiblyFailing(c); |
926 |
+ |
assertSame(c.getClass(), serialClone.getClass()); |
927 |
+ |
assertCollectionsEquivalent(c, serialClone); |
928 |
+ |
} catch (java.io.NotSerializableException acceptable) {} |
929 |
+ |
} |
930 |
+ |
|
931 |
+ |
public void testReplaceAllIsNotStructuralModification() { |
932 |
+ |
Collection c = impl.emptyCollection(); |
933 |
+ |
if (!(c instanceof List)) |
934 |
+ |
return; |
935 |
+ |
List list = (List) c; |
936 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
937 |
+ |
for (int n = rnd.nextInt(2, 10); n--> 0; ) |
938 |
+ |
list.add(impl.makeElement(rnd.nextInt())); |
939 |
+ |
ArrayList copy = new ArrayList(list); |
940 |
+ |
int size = list.size(), half = size / 2; |
941 |
+ |
Iterator it = list.iterator(); |
942 |
+ |
for (int i = 0; i < half; i++) |
943 |
+ |
assertEquals(it.next(), copy.get(i)); |
944 |
+ |
list.replaceAll(n -> n); |
945 |
+ |
// ConcurrentModificationException must not be thrown here. |
946 |
+ |
for (int i = half; i < size; i++) |
947 |
+ |
assertEquals(it.next(), copy.get(i)); |
948 |
+ |
} |
949 |
+ |
|
950 |
|
// public void testCollection8DebugFail() { |
951 |
|
// fail(impl.klazz().getSimpleName()); |
952 |
|
// } |