12 |
|
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Collections; |
15 |
+ |
import java.util.ConcurrentModificationException; |
16 |
|
import java.util.Deque; |
17 |
|
import java.util.HashSet; |
18 |
|
import java.util.Iterator; |
516 |
|
} |
517 |
|
|
518 |
|
/** |
519 |
+ |
* Iterator.forEachRemaining has same behavior as Iterator's |
520 |
+ |
* default implementation. |
521 |
+ |
*/ |
522 |
+ |
public void testForEachRemainingConsistentWithDefaultImplementation() { |
523 |
+ |
Collection c = impl.emptyCollection(); |
524 |
+ |
if (!testImplementationDetails |
525 |
+ |
|| c.getClass() == java.util.LinkedList.class) |
526 |
+ |
return; |
527 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
528 |
+ |
int n = 1 + rnd.nextInt(3); |
529 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
530 |
+ |
ArrayList iterated = new ArrayList(); |
531 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
532 |
+ |
Iterator it1 = c.iterator(); |
533 |
+ |
Iterator it2 = c.iterator(); |
534 |
+ |
assertTrue(it1.hasNext()); |
535 |
+ |
assertTrue(it2.hasNext()); |
536 |
+ |
c.clear(); |
537 |
+ |
Object r1, r2; |
538 |
+ |
try { |
539 |
+ |
while (it1.hasNext()) iterated.add(it1.next()); |
540 |
+ |
r1 = iterated; |
541 |
+ |
} catch (ConcurrentModificationException ex) { |
542 |
+ |
r1 = ConcurrentModificationException.class; |
543 |
+ |
assertFalse(impl.isConcurrent()); |
544 |
+ |
} catch (UnsupportedOperationException ex) { |
545 |
+ |
r1 = UnsupportedOperationException.class; |
546 |
+ |
} |
547 |
+ |
try { |
548 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
549 |
+ |
r2 = iteratedForEachRemaining; |
550 |
+ |
} catch (ConcurrentModificationException ex) { |
551 |
+ |
r2 = ConcurrentModificationException.class; |
552 |
+ |
} catch (UnsupportedOperationException ex) { |
553 |
+ |
r2 = UnsupportedOperationException.class; |
554 |
+ |
} |
555 |
+ |
if (!r1.equals(r2)) System.err.println(impl.klazz()); |
556 |
+ |
assertEquals(r1, r2); |
557 |
+ |
} |
558 |
+ |
|
559 |
+ |
/** |
560 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
561 |
|
* should (maybe) remove last element |
562 |
|
*/ |