528 |
|
*/ |
529 |
|
public void testIteratorRemove () { |
530 |
|
final ArrayDeque q = new ArrayDeque(); |
531 |
< |
q.add(new Integer(1)); |
532 |
< |
q.add(new Integer(2)); |
533 |
< |
q.add(new Integer(3)); |
534 |
< |
Iterator it = q.iterator(); |
535 |
< |
it.next(); |
536 |
< |
it.remove(); |
537 |
< |
it = q.iterator(); |
538 |
< |
assertEquals(it.next(), new Integer(2)); |
539 |
< |
assertEquals(it.next(), new Integer(3)); |
531 |
> |
final Random rng = new Random(); |
532 |
> |
for (int iters = 0; iters < 100; ++iters) { |
533 |
> |
int max = rng.nextInt(5) + 2; |
534 |
> |
int split = rng.nextInt(max-1) + 1; |
535 |
> |
for (int j = 1; j <= max; ++j) |
536 |
> |
q.add(new Integer(j)); |
537 |
> |
Iterator it = q.iterator(); |
538 |
> |
for (int j = 1; j <= split; ++j) |
539 |
> |
assertEquals(it.next(), new Integer(j)); |
540 |
> |
it.remove(); |
541 |
> |
assertEquals(it.next(), new Integer(split+1)); |
542 |
> |
for (int j = 1; j <= split; ++j) |
543 |
> |
q.remove(new Integer(j)); |
544 |
> |
it = q.iterator(); |
545 |
> |
for (int j = split+1; j <= max; ++j) { |
546 |
> |
assertEquals(it.next(), new Integer(j)); |
547 |
> |
it.remove(); |
548 |
> |
} |
549 |
> |
assertFalse(it.hasNext()); |
550 |
> |
assertTrue(q.isEmpty()); |
551 |
> |
} |
552 |
> |
} |
553 |
> |
|
554 |
> |
/** |
555 |
> |
* Descending iterator iterates through all elements |
556 |
> |
*/ |
557 |
> |
public void testDescendingIterator() { |
558 |
> |
ArrayDeque q = populatedDeque(SIZE); |
559 |
> |
int i = 0; |
560 |
> |
Iterator it = q.descendingIterator(); |
561 |
> |
while(it.hasNext()) { |
562 |
> |
assertTrue(q.contains(it.next())); |
563 |
> |
++i; |
564 |
> |
} |
565 |
> |
assertEquals(i, SIZE); |
566 |
|
assertFalse(it.hasNext()); |
567 |
+ |
try { |
568 |
+ |
it.next(); |
569 |
+ |
} catch(NoSuchElementException success) { |
570 |
+ |
} |
571 |
+ |
} |
572 |
+ |
|
573 |
+ |
/** |
574 |
+ |
* Descending iterator ordering is reverse FIFO |
575 |
+ |
*/ |
576 |
+ |
public void testDescendingIteratorOrdering() { |
577 |
+ |
final ArrayDeque q = new ArrayDeque(); |
578 |
+ |
for (int iters = 0; iters < 100; ++iters) { |
579 |
+ |
q.add(new Integer(3)); |
580 |
+ |
q.add(new Integer(2)); |
581 |
+ |
q.add(new Integer(1)); |
582 |
+ |
int k = 0; |
583 |
+ |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
584 |
+ |
int i = ((Integer)(it.next())).intValue(); |
585 |
+ |
assertEquals(++k, i); |
586 |
+ |
} |
587 |
+ |
|
588 |
+ |
assertEquals(3, k); |
589 |
+ |
q.remove(); |
590 |
+ |
q.remove(); |
591 |
+ |
q.remove(); |
592 |
+ |
} |
593 |
+ |
} |
594 |
+ |
|
595 |
+ |
/** |
596 |
+ |
* descendingIterator.remove removes current element |
597 |
+ |
*/ |
598 |
+ |
public void testDescendingIteratorRemove () { |
599 |
+ |
final ArrayDeque q = new ArrayDeque(); |
600 |
+ |
final Random rng = new Random(); |
601 |
+ |
for (int iters = 0; iters < 100; ++iters) { |
602 |
+ |
int max = rng.nextInt(5) + 2; |
603 |
+ |
int split = rng.nextInt(max-1) + 1; |
604 |
+ |
for (int j = max; j >= 1; --j) |
605 |
+ |
q.add(new Integer(j)); |
606 |
+ |
Iterator it = q.descendingIterator(); |
607 |
+ |
for (int j = 1; j <= split; ++j) |
608 |
+ |
assertEquals(it.next(), new Integer(j)); |
609 |
+ |
it.remove(); |
610 |
+ |
assertEquals(it.next(), new Integer(split+1)); |
611 |
+ |
for (int j = 1; j <= split; ++j) |
612 |
+ |
q.remove(new Integer(j)); |
613 |
+ |
it = q.descendingIterator(); |
614 |
+ |
for (int j = split+1; j <= max; ++j) { |
615 |
+ |
assertEquals(it.next(), new Integer(j)); |
616 |
+ |
it.remove(); |
617 |
+ |
} |
618 |
+ |
assertFalse(it.hasNext()); |
619 |
+ |
assertTrue(q.isEmpty()); |
620 |
+ |
} |
621 |
|
} |
622 |
|
|
623 |
|
|