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(); |
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 |
|
// } |