ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
Revision: 1.36
Committed: Sat May 23 00:53:08 2015 UTC (8 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.35: +12 -12 lines
Log Message:
whitespace

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.ArrayDeque;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.Deque;
11 import java.util.Iterator;
12 import java.util.NoSuchElementException;
13 import java.util.Queue;
14 import java.util.Random;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18
19 public class ArrayDequeTest extends JSR166TestCase {
20 public static void main(String[] args) {
21 main(suite(), args);
22 }
23
24 public static Test suite() {
25 return new TestSuite(ArrayDequeTest.class);
26 }
27
28 /**
29 * Returns a new deque of given size containing consecutive
30 * Integers 0 ... n.
31 */
32 private ArrayDeque<Integer> populatedDeque(int n) {
33 ArrayDeque<Integer> q = new ArrayDeque<Integer>();
34 assertTrue(q.isEmpty());
35 for (int i = 0; i < n; ++i)
36 assertTrue(q.offerLast(new Integer(i)));
37 assertFalse(q.isEmpty());
38 assertEquals(n, q.size());
39 return q;
40 }
41
42 /**
43 * new deque is empty
44 */
45 public void testConstructor1() {
46 assertEquals(0, new ArrayDeque().size());
47 }
48
49 /**
50 * Initializing from null Collection throws NPE
51 */
52 public void testConstructor3() {
53 try {
54 new ArrayDeque((Collection)null);
55 shouldThrow();
56 } catch (NullPointerException success) {}
57 }
58
59 /**
60 * Initializing from Collection of null elements throws NPE
61 */
62 public void testConstructor4() {
63 try {
64 new ArrayDeque(Arrays.asList(new Integer[SIZE]));
65 shouldThrow();
66 } catch (NullPointerException success) {}
67 }
68
69 /**
70 * Initializing from Collection with some null elements throws NPE
71 */
72 public void testConstructor5() {
73 Integer[] ints = new Integer[SIZE];
74 for (int i = 0; i < SIZE - 1; ++i)
75 ints[i] = new Integer(i);
76 try {
77 new ArrayDeque(Arrays.asList(ints));
78 shouldThrow();
79 } catch (NullPointerException success) {}
80 }
81
82 /**
83 * Deque contains all elements of collection used to initialize
84 */
85 public void testConstructor6() {
86 Integer[] ints = new Integer[SIZE];
87 for (int i = 0; i < SIZE; ++i)
88 ints[i] = new Integer(i);
89 ArrayDeque q = new ArrayDeque(Arrays.asList(ints));
90 for (int i = 0; i < SIZE; ++i)
91 assertEquals(ints[i], q.pollFirst());
92 }
93
94 /**
95 * isEmpty is true before add, false after
96 */
97 public void testEmpty() {
98 ArrayDeque q = new ArrayDeque();
99 assertTrue(q.isEmpty());
100 q.add(new Integer(1));
101 assertFalse(q.isEmpty());
102 q.add(new Integer(2));
103 q.removeFirst();
104 q.removeFirst();
105 assertTrue(q.isEmpty());
106 }
107
108 /**
109 * size changes when elements added and removed
110 */
111 public void testSize() {
112 ArrayDeque q = populatedDeque(SIZE);
113 for (int i = 0; i < SIZE; ++i) {
114 assertEquals(SIZE - i, q.size());
115 q.removeFirst();
116 }
117 for (int i = 0; i < SIZE; ++i) {
118 assertEquals(i, q.size());
119 q.add(new Integer(i));
120 }
121 }
122
123 /**
124 * push(null) throws NPE
125 */
126 public void testPushNull() {
127 ArrayDeque q = new ArrayDeque(1);
128 try {
129 q.push(null);
130 shouldThrow();
131 } catch (NullPointerException success) {}
132 }
133
134 /**
135 * peekFirst() returns element inserted with push
136 */
137 public void testPush() {
138 ArrayDeque q = populatedDeque(3);
139 q.pollLast();
140 q.push(four);
141 assertSame(four, q.peekFirst());
142 }
143
144 /**
145 * pop() removes next element, or throws NSEE if empty
146 */
147 public void testPop() {
148 ArrayDeque q = populatedDeque(SIZE);
149 for (int i = 0; i < SIZE; ++i) {
150 assertEquals(i, q.pop());
151 }
152 try {
153 q.pop();
154 shouldThrow();
155 } catch (NoSuchElementException success) {}
156 }
157
158 /**
159 * offer(null) throws NPE
160 */
161 public void testOfferNull() {
162 ArrayDeque q = new ArrayDeque();
163 try {
164 q.offer(null);
165 shouldThrow();
166 } catch (NullPointerException success) {}
167 }
168
169 /**
170 * offerFirst(null) throws NPE
171 */
172 public void testOfferFirstNull() {
173 ArrayDeque q = new ArrayDeque();
174 try {
175 q.offerFirst(null);
176 shouldThrow();
177 } catch (NullPointerException success) {}
178 }
179
180 /**
181 * offerLast(null) throws NPE
182 */
183 public void testOfferLastNull() {
184 ArrayDeque q = new ArrayDeque();
185 try {
186 q.offerLast(null);
187 shouldThrow();
188 } catch (NullPointerException success) {}
189 }
190
191 /**
192 * offer(x) succeeds
193 */
194 public void testOffer() {
195 ArrayDeque q = new ArrayDeque();
196 assertTrue(q.offer(zero));
197 assertTrue(q.offer(one));
198 assertSame(zero, q.peekFirst());
199 assertSame(one, q.peekLast());
200 }
201
202 /**
203 * offerFirst(x) succeeds
204 */
205 public void testOfferFirst() {
206 ArrayDeque q = new ArrayDeque();
207 assertTrue(q.offerFirst(zero));
208 assertTrue(q.offerFirst(one));
209 assertSame(one, q.peekFirst());
210 assertSame(zero, q.peekLast());
211 }
212
213 /**
214 * offerLast(x) succeeds
215 */
216 public void testOfferLast() {
217 ArrayDeque q = new ArrayDeque();
218 assertTrue(q.offerLast(zero));
219 assertTrue(q.offerLast(one));
220 assertSame(zero, q.peekFirst());
221 assertSame(one, q.peekLast());
222 }
223
224 /**
225 * add(null) throws NPE
226 */
227 public void testAddNull() {
228 ArrayDeque q = new ArrayDeque();
229 try {
230 q.add(null);
231 shouldThrow();
232 } catch (NullPointerException success) {}
233 }
234
235 /**
236 * addFirst(null) throws NPE
237 */
238 public void testAddFirstNull() {
239 ArrayDeque q = new ArrayDeque();
240 try {
241 q.addFirst(null);
242 shouldThrow();
243 } catch (NullPointerException success) {}
244 }
245
246 /**
247 * addLast(null) throws NPE
248 */
249 public void testAddLastNull() {
250 ArrayDeque q = new ArrayDeque();
251 try {
252 q.addLast(null);
253 shouldThrow();
254 } catch (NullPointerException success) {}
255 }
256
257 /**
258 * add(x) succeeds
259 */
260 public void testAdd() {
261 ArrayDeque q = new ArrayDeque();
262 assertTrue(q.add(zero));
263 assertTrue(q.add(one));
264 assertSame(zero, q.peekFirst());
265 assertSame(one, q.peekLast());
266 }
267
268 /**
269 * addFirst(x) succeeds
270 */
271 public void testAddFirst() {
272 ArrayDeque q = new ArrayDeque();
273 q.addFirst(zero);
274 q.addFirst(one);
275 assertSame(one, q.peekFirst());
276 assertSame(zero, q.peekLast());
277 }
278
279 /**
280 * addLast(x) succeeds
281 */
282 public void testAddLast() {
283 ArrayDeque q = new ArrayDeque();
284 q.addLast(zero);
285 q.addLast(one);
286 assertSame(zero, q.peekFirst());
287 assertSame(one, q.peekLast());
288 }
289
290 /**
291 * addAll(null) throws NPE
292 */
293 public void testAddAll1() {
294 ArrayDeque q = new ArrayDeque();
295 try {
296 q.addAll(null);
297 shouldThrow();
298 } catch (NullPointerException success) {}
299 }
300
301 /**
302 * addAll of a collection with null elements throws NPE
303 */
304 public void testAddAll2() {
305 ArrayDeque q = new ArrayDeque();
306 try {
307 q.addAll(Arrays.asList(new Integer[SIZE]));
308 shouldThrow();
309 } catch (NullPointerException success) {}
310 }
311
312 /**
313 * addAll of a collection with any null elements throws NPE after
314 * possibly adding some elements
315 */
316 public void testAddAll3() {
317 ArrayDeque q = new ArrayDeque();
318 Integer[] ints = new Integer[SIZE];
319 for (int i = 0; i < SIZE - 1; ++i)
320 ints[i] = new Integer(i);
321 try {
322 q.addAll(Arrays.asList(ints));
323 shouldThrow();
324 } catch (NullPointerException success) {}
325 }
326
327 /**
328 * Deque contains all elements, in traversal order, of successful addAll
329 */
330 public void testAddAll5() {
331 Integer[] empty = new Integer[0];
332 Integer[] ints = new Integer[SIZE];
333 for (int i = 0; i < SIZE; ++i)
334 ints[i] = new Integer(i);
335 ArrayDeque q = new ArrayDeque();
336 assertFalse(q.addAll(Arrays.asList(empty)));
337 assertTrue(q.addAll(Arrays.asList(ints)));
338 for (int i = 0; i < SIZE; ++i)
339 assertEquals(ints[i], q.pollFirst());
340 }
341
342 /**
343 * pollFirst() succeeds unless empty
344 */
345 public void testPollFirst() {
346 ArrayDeque q = populatedDeque(SIZE);
347 for (int i = 0; i < SIZE; ++i) {
348 assertEquals(i, q.pollFirst());
349 }
350 assertNull(q.pollFirst());
351 }
352
353 /**
354 * pollLast() succeeds unless empty
355 */
356 public void testPollLast() {
357 ArrayDeque q = populatedDeque(SIZE);
358 for (int i = SIZE - 1; i >= 0; --i) {
359 assertEquals(i, q.pollLast());
360 }
361 assertNull(q.pollLast());
362 }
363
364 /**
365 * poll() succeeds unless empty
366 */
367 public void testPoll() {
368 ArrayDeque q = populatedDeque(SIZE);
369 for (int i = 0; i < SIZE; ++i) {
370 assertEquals(i, q.poll());
371 }
372 assertNull(q.poll());
373 }
374
375 /**
376 * remove() removes next element, or throws NSEE if empty
377 */
378 public void testRemove() {
379 ArrayDeque q = populatedDeque(SIZE);
380 for (int i = 0; i < SIZE; ++i) {
381 assertEquals(i, q.remove());
382 }
383 try {
384 q.remove();
385 shouldThrow();
386 } catch (NoSuchElementException success) {}
387 }
388
389 /**
390 * remove(x) removes x and returns true if present
391 */
392 public void testRemoveElement() {
393 ArrayDeque q = populatedDeque(SIZE);
394 for (int i = 1; i < SIZE; i += 2) {
395 assertTrue(q.contains(i));
396 assertTrue(q.remove(i));
397 assertFalse(q.contains(i));
398 assertTrue(q.contains(i-1));
399 }
400 for (int i = 0; i < SIZE; i += 2) {
401 assertTrue(q.contains(i));
402 assertTrue(q.remove(i));
403 assertFalse(q.contains(i));
404 assertFalse(q.remove(i+1));
405 assertFalse(q.contains(i+1));
406 }
407 assertTrue(q.isEmpty());
408 }
409
410 /**
411 * peekFirst() returns next element, or null if empty
412 */
413 public void testPeekFirst() {
414 ArrayDeque q = populatedDeque(SIZE);
415 for (int i = 0; i < SIZE; ++i) {
416 assertEquals(i, q.peekFirst());
417 assertEquals(i, q.pollFirst());
418 assertTrue(q.peekFirst() == null ||
419 !q.peekFirst().equals(i));
420 }
421 assertNull(q.peekFirst());
422 }
423
424 /**
425 * peek() returns next element, or null if empty
426 */
427 public void testPeek() {
428 ArrayDeque q = populatedDeque(SIZE);
429 for (int i = 0; i < SIZE; ++i) {
430 assertEquals(i, q.peek());
431 assertEquals(i, q.poll());
432 assertTrue(q.peek() == null ||
433 !q.peek().equals(i));
434 }
435 assertNull(q.peek());
436 }
437
438 /**
439 * peekLast() returns next element, or null if empty
440 */
441 public void testPeekLast() {
442 ArrayDeque q = populatedDeque(SIZE);
443 for (int i = SIZE - 1; i >= 0; --i) {
444 assertEquals(i, q.peekLast());
445 assertEquals(i, q.pollLast());
446 assertTrue(q.peekLast() == null ||
447 !q.peekLast().equals(i));
448 }
449 assertNull(q.peekLast());
450 }
451
452 /**
453 * element() returns first element, or throws NSEE if empty
454 */
455 public void testElement() {
456 ArrayDeque q = populatedDeque(SIZE);
457 for (int i = 0; i < SIZE; ++i) {
458 assertEquals(i, q.element());
459 assertEquals(i, q.poll());
460 }
461 try {
462 q.element();
463 shouldThrow();
464 } catch (NoSuchElementException success) {}
465 }
466
467 /**
468 * getFirst() returns first element, or throws NSEE if empty
469 */
470 public void testFirstElement() {
471 ArrayDeque q = populatedDeque(SIZE);
472 for (int i = 0; i < SIZE; ++i) {
473 assertEquals(i, q.getFirst());
474 assertEquals(i, q.pollFirst());
475 }
476 try {
477 q.getFirst();
478 shouldThrow();
479 } catch (NoSuchElementException success) {}
480 }
481
482 /**
483 * getLast() returns last element, or throws NSEE if empty
484 */
485 public void testLastElement() {
486 ArrayDeque q = populatedDeque(SIZE);
487 for (int i = SIZE - 1; i >= 0; --i) {
488 assertEquals(i, q.getLast());
489 assertEquals(i, q.pollLast());
490 }
491 try {
492 q.getLast();
493 shouldThrow();
494 } catch (NoSuchElementException success) {}
495 assertNull(q.peekLast());
496 }
497
498 /**
499 * removeFirst() removes first element, or throws NSEE if empty
500 */
501 public void testRemoveFirst() {
502 ArrayDeque q = populatedDeque(SIZE);
503 for (int i = 0; i < SIZE; ++i) {
504 assertEquals(i, q.removeFirst());
505 }
506 try {
507 q.removeFirst();
508 shouldThrow();
509 } catch (NoSuchElementException success) {}
510 assertNull(q.peekFirst());
511 }
512
513 /**
514 * removeLast() removes last element, or throws NSEE if empty
515 */
516 public void testRemoveLast() {
517 ArrayDeque q = populatedDeque(SIZE);
518 for (int i = SIZE - 1; i >= 0; --i) {
519 assertEquals(i, q.removeLast());
520 }
521 try {
522 q.removeLast();
523 shouldThrow();
524 } catch (NoSuchElementException success) {}
525 assertNull(q.peekLast());
526 }
527
528 /**
529 * removeFirstOccurrence(x) removes x and returns true if present
530 */
531 public void testRemoveFirstOccurrence() {
532 ArrayDeque q = populatedDeque(SIZE);
533 for (int i = 1; i < SIZE; i += 2) {
534 assertTrue(q.removeFirstOccurrence(new Integer(i)));
535 }
536 for (int i = 0; i < SIZE; i += 2) {
537 assertTrue(q.removeFirstOccurrence(new Integer(i)));
538 assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
539 }
540 assertTrue(q.isEmpty());
541 }
542
543 /**
544 * removeLastOccurrence(x) removes x and returns true if present
545 */
546 public void testRemoveLastOccurrence() {
547 ArrayDeque q = populatedDeque(SIZE);
548 for (int i = 1; i < SIZE; i += 2) {
549 assertTrue(q.removeLastOccurrence(new Integer(i)));
550 }
551 for (int i = 0; i < SIZE; i += 2) {
552 assertTrue(q.removeLastOccurrence(new Integer(i)));
553 assertFalse(q.removeLastOccurrence(new Integer(i+1)));
554 }
555 assertTrue(q.isEmpty());
556 }
557
558 /**
559 * contains(x) reports true when elements added but not yet removed
560 */
561 public void testContains() {
562 ArrayDeque q = populatedDeque(SIZE);
563 for (int i = 0; i < SIZE; ++i) {
564 assertTrue(q.contains(new Integer(i)));
565 assertEquals(i, q.pollFirst());
566 assertFalse(q.contains(new Integer(i)));
567 }
568 }
569
570 /**
571 * clear removes all elements
572 */
573 public void testClear() {
574 ArrayDeque q = populatedDeque(SIZE);
575 q.clear();
576 assertTrue(q.isEmpty());
577 assertEquals(0, q.size());
578 assertTrue(q.add(new Integer(1)));
579 assertFalse(q.isEmpty());
580 q.clear();
581 assertTrue(q.isEmpty());
582 }
583
584 /**
585 * containsAll(c) is true when c contains a subset of elements
586 */
587 public void testContainsAll() {
588 ArrayDeque q = populatedDeque(SIZE);
589 ArrayDeque p = new ArrayDeque();
590 for (int i = 0; i < SIZE; ++i) {
591 assertTrue(q.containsAll(p));
592 assertFalse(p.containsAll(q));
593 assertTrue(p.add(new Integer(i)));
594 }
595 assertTrue(p.containsAll(q));
596 }
597
598 /**
599 * retainAll(c) retains only those elements of c and reports true if changed
600 */
601 public void testRetainAll() {
602 ArrayDeque q = populatedDeque(SIZE);
603 ArrayDeque p = populatedDeque(SIZE);
604 for (int i = 0; i < SIZE; ++i) {
605 boolean changed = q.retainAll(p);
606 assertEquals(changed, (i > 0));
607 assertTrue(q.containsAll(p));
608 assertEquals(SIZE - i, q.size());
609 p.removeFirst();
610 }
611 }
612
613 /**
614 * removeAll(c) removes only those elements of c and reports true if changed
615 */
616 public void testRemoveAll() {
617 for (int i = 1; i < SIZE; ++i) {
618 ArrayDeque q = populatedDeque(SIZE);
619 ArrayDeque p = populatedDeque(i);
620 assertTrue(q.removeAll(p));
621 assertEquals(SIZE - i, q.size());
622 for (int j = 0; j < i; ++j) {
623 assertFalse(q.contains(p.removeFirst()));
624 }
625 }
626 }
627
628 void checkToArray(ArrayDeque q) {
629 int size = q.size();
630 Object[] o = q.toArray();
631 assertEquals(size, o.length);
632 Iterator it = q.iterator();
633 for (int i = 0; i < size; i++) {
634 Integer x = (Integer) it.next();
635 assertEquals((Integer)o[0] + i, (int) x);
636 assertSame(o[i], x);
637 }
638 }
639
640 /**
641 * toArray() contains all elements in FIFO order
642 */
643 public void testToArray() {
644 ArrayDeque q = new ArrayDeque();
645 for (int i = 0; i < SIZE; i++) {
646 checkToArray(q);
647 q.addLast(i);
648 }
649 // Provoke wraparound
650 for (int i = 0; i < SIZE; i++) {
651 checkToArray(q);
652 assertEquals(i, q.poll());
653 q.addLast(SIZE + i);
654 }
655 for (int i = 0; i < SIZE; i++) {
656 checkToArray(q);
657 assertEquals(SIZE + i, q.poll());
658 }
659 }
660
661 void checkToArray2(ArrayDeque q) {
662 int size = q.size();
663 Integer[] a1 = size == 0 ? null : new Integer[size-1];
664 Integer[] a2 = new Integer[size];
665 Integer[] a3 = new Integer[size+2];
666 if (size > 0) Arrays.fill(a1, 42);
667 Arrays.fill(a2, 42);
668 Arrays.fill(a3, 42);
669 Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
670 Integer[] b2 = (Integer[]) q.toArray(a2);
671 Integer[] b3 = (Integer[]) q.toArray(a3);
672 assertSame(a2, b2);
673 assertSame(a3, b3);
674 Iterator it = q.iterator();
675 for (int i = 0; i < size; i++) {
676 Integer x = (Integer) it.next();
677 assertSame(b1[i], x);
678 assertEquals(b1[0] + i, (int) x);
679 assertSame(b2[i], x);
680 assertSame(b3[i], x);
681 }
682 assertNull(a3[size]);
683 assertEquals(42, (int) a3[size+1]);
684 if (size > 0) {
685 assertNotSame(a1, b1);
686 assertEquals(size, b1.length);
687 for (int i = 0; i < a1.length; i++) {
688 assertEquals(42, (int) a1[i]);
689 }
690 }
691 }
692
693 /**
694 * toArray(a) contains all elements in FIFO order
695 */
696 public void testToArray2() {
697 ArrayDeque q = new ArrayDeque();
698 for (int i = 0; i < SIZE; i++) {
699 checkToArray2(q);
700 q.addLast(i);
701 }
702 // Provoke wraparound
703 for (int i = 0; i < SIZE; i++) {
704 checkToArray2(q);
705 assertEquals(i, q.poll());
706 q.addLast(SIZE + i);
707 }
708 for (int i = 0; i < SIZE; i++) {
709 checkToArray2(q);
710 assertEquals(SIZE + i, q.poll());
711 }
712 }
713
714 /**
715 * toArray(null) throws NullPointerException
716 */
717 public void testToArray_NullArg() {
718 ArrayDeque l = new ArrayDeque();
719 l.add(new Object());
720 try {
721 l.toArray(null);
722 shouldThrow();
723 } catch (NullPointerException success) {}
724 }
725
726 /**
727 * toArray(incompatible array type) throws ArrayStoreException
728 */
729 public void testToArray1_BadArg() {
730 ArrayDeque l = new ArrayDeque();
731 l.add(new Integer(5));
732 try {
733 l.toArray(new String[10]);
734 shouldThrow();
735 } catch (ArrayStoreException success) {}
736 }
737
738 /**
739 * Iterator iterates through all elements
740 */
741 public void testIterator() {
742 ArrayDeque q = populatedDeque(SIZE);
743 Iterator it = q.iterator();
744 int i;
745 for (i = 0; it.hasNext(); i++)
746 assertTrue(q.contains(it.next()));
747 assertEquals(i, SIZE);
748 assertIteratorExhausted(it);
749 }
750
751 /**
752 * iterator of empty collection has no elements
753 */
754 public void testEmptyIterator() {
755 Deque c = new ArrayDeque();
756 assertIteratorExhausted(c.iterator());
757 assertIteratorExhausted(c.descendingIterator());
758 }
759
760 /**
761 * Iterator ordering is FIFO
762 */
763 public void testIteratorOrdering() {
764 final ArrayDeque q = new ArrayDeque();
765 q.add(one);
766 q.add(two);
767 q.add(three);
768 int k = 0;
769 for (Iterator it = q.iterator(); it.hasNext();) {
770 assertEquals(++k, it.next());
771 }
772
773 assertEquals(3, k);
774 }
775
776 /**
777 * iterator.remove() removes current element
778 */
779 public void testIteratorRemove() {
780 final ArrayDeque q = new ArrayDeque();
781 final Random rng = new Random();
782 for (int iters = 0; iters < 100; ++iters) {
783 int max = rng.nextInt(5) + 2;
784 int split = rng.nextInt(max-1) + 1;
785 for (int j = 1; j <= max; ++j)
786 q.add(new Integer(j));
787 Iterator it = q.iterator();
788 for (int j = 1; j <= split; ++j)
789 assertEquals(it.next(), new Integer(j));
790 it.remove();
791 assertEquals(it.next(), new Integer(split+1));
792 for (int j = 1; j <= split; ++j)
793 q.remove(new Integer(j));
794 it = q.iterator();
795 for (int j = split+1; j <= max; ++j) {
796 assertEquals(it.next(), new Integer(j));
797 it.remove();
798 }
799 assertFalse(it.hasNext());
800 assertTrue(q.isEmpty());
801 }
802 }
803
804 /**
805 * Descending iterator iterates through all elements
806 */
807 public void testDescendingIterator() {
808 ArrayDeque q = populatedDeque(SIZE);
809 int i = 0;
810 Iterator it = q.descendingIterator();
811 while (it.hasNext()) {
812 assertTrue(q.contains(it.next()));
813 ++i;
814 }
815 assertEquals(i, SIZE);
816 assertFalse(it.hasNext());
817 try {
818 it.next();
819 shouldThrow();
820 } catch (NoSuchElementException success) {}
821 }
822
823 /**
824 * Descending iterator ordering is reverse FIFO
825 */
826 public void testDescendingIteratorOrdering() {
827 final ArrayDeque q = new ArrayDeque();
828 for (int iters = 0; iters < 100; ++iters) {
829 q.add(new Integer(3));
830 q.add(new Integer(2));
831 q.add(new Integer(1));
832 int k = 0;
833 for (Iterator it = q.descendingIterator(); it.hasNext();) {
834 assertEquals(++k, it.next());
835 }
836
837 assertEquals(3, k);
838 q.remove();
839 q.remove();
840 q.remove();
841 }
842 }
843
844 /**
845 * descendingIterator.remove() removes current element
846 */
847 public void testDescendingIteratorRemove() {
848 final ArrayDeque q = new ArrayDeque();
849 final Random rng = new Random();
850 for (int iters = 0; iters < 100; ++iters) {
851 int max = rng.nextInt(5) + 2;
852 int split = rng.nextInt(max-1) + 1;
853 for (int j = max; j >= 1; --j)
854 q.add(new Integer(j));
855 Iterator it = q.descendingIterator();
856 for (int j = 1; j <= split; ++j)
857 assertEquals(it.next(), new Integer(j));
858 it.remove();
859 assertEquals(it.next(), new Integer(split+1));
860 for (int j = 1; j <= split; ++j)
861 q.remove(new Integer(j));
862 it = q.descendingIterator();
863 for (int j = split+1; j <= max; ++j) {
864 assertEquals(it.next(), new Integer(j));
865 it.remove();
866 }
867 assertFalse(it.hasNext());
868 assertTrue(q.isEmpty());
869 }
870 }
871
872 /**
873 * toString() contains toStrings of elements
874 */
875 public void testToString() {
876 ArrayDeque q = populatedDeque(SIZE);
877 String s = q.toString();
878 for (int i = 0; i < SIZE; ++i) {
879 assertTrue(s.contains(String.valueOf(i)));
880 }
881 }
882
883 /**
884 * A deserialized serialized deque has same elements in same order
885 */
886 public void testSerialization() throws Exception {
887 Queue x = populatedDeque(SIZE);
888 Queue y = serialClone(x);
889
890 assertNotSame(y, x);
891 assertEquals(x.size(), y.size());
892 assertEquals(x.toString(), y.toString());
893 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
894 while (!x.isEmpty()) {
895 assertFalse(y.isEmpty());
896 assertEquals(x.remove(), y.remove());
897 }
898 assertTrue(y.isEmpty());
899 }
900
901 /**
902 * remove(null), contains(null) always return false
903 */
904 public void testNeverContainsNull() {
905 Deque<?>[] qs = {
906 new ArrayDeque<Object>(),
907 populatedDeque(2),
908 };
909
910 for (Deque<?> q : qs) {
911 assertFalse(q.contains(null));
912 assertFalse(q.remove(null));
913 assertFalse(q.removeFirstOccurrence(null));
914 assertFalse(q.removeLastOccurrence(null));
915 }
916 }
917
918 }