ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
Revision: 1.41
Committed: Sun Oct 16 20:44:18 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.40: +3 -1 lines
Log Message:
improve populatedFoo methods

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