ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
Revision: 1.42
Committed: Sun Oct 16 22:13:15 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.41: +32 -3 lines
Log Message:
populatedDeque: Randomize various aspects of memory layout

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