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; |
448 |
|
/** |
449 |
|
* Various ways of traversing a collection yield same elements |
450 |
|
*/ |
451 |
< |
public void testIteratorEquivalence() { |
451 |
> |
public void testTraversalEquivalence() { |
452 |
|
Collection c = impl.emptyCollection(); |
453 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
454 |
|
int n = rnd.nextInt(6); |
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 |
+ |
} |
545 |
+ |
try { |
546 |
+ |
it2.forEachRemaining(iteratedForEachRemaining::add); |
547 |
+ |
r2 = iteratedForEachRemaining; |
548 |
+ |
} catch (ConcurrentModificationException ex) { |
549 |
+ |
r2 = ConcurrentModificationException.class; |
550 |
+ |
assertFalse(impl.isConcurrent()); |
551 |
+ |
} |
552 |
+ |
assertEquals(r1, r2); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
/** |
556 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
557 |
|
* should (maybe) remove last element |
558 |
|
*/ |
836 |
|
} |
837 |
|
} |
838 |
|
|
839 |
+ |
/** |
840 |
+ |
* Spliterator.getComparator throws IllegalStateException iff the |
841 |
+ |
* spliterator does not report SORTED. |
842 |
+ |
*/ |
843 |
+ |
public void testGetComparator_IllegalStateException() { |
844 |
+ |
Collection c = impl.emptyCollection(); |
845 |
+ |
Spliterator s = c.spliterator(); |
846 |
+ |
boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); |
847 |
+ |
try { |
848 |
+ |
s.getComparator(); |
849 |
+ |
assertTrue(reportsSorted); |
850 |
+ |
} catch (IllegalStateException ex) { |
851 |
+ |
assertFalse(reportsSorted); |
852 |
+ |
} |
853 |
+ |
} |
854 |
+ |
|
855 |
|
// public void testCollection8DebugFail() { |
856 |
|
// fail(impl.klazz().getSimpleName()); |
857 |
|
// } |