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