ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
Revision: 1.16
Committed: Wed Dec 31 20:17:39 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +2 -2 lines
Log Message:
coding style

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 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import java.util.Arrays;
10 import java.util.Collection;
11 import java.util.Deque;
12 import java.util.Iterator;
13 import java.util.NoSuchElementException;
14 import java.util.Queue;
15 import java.util.Random;
16 import java.util.concurrent.ConcurrentLinkedDeque;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20
21 public class ConcurrentLinkedDequeTest extends JSR166TestCase {
22
23 public static void main(String[] args) {
24 junit.textui.TestRunner.run(suite());
25 }
26
27 public static Test suite() {
28 return new TestSuite(ConcurrentLinkedDequeTest.class);
29 }
30
31 /**
32 * Returns a new deque of given size containing consecutive
33 * Integers 0 ... n.
34 */
35 private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
36 ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
37 assertTrue(q.isEmpty());
38 for (int i = 0; i < n; ++i)
39 assertTrue(q.offer(new Integer(i)));
40 assertFalse(q.isEmpty());
41 assertEquals(n, q.size());
42 return q;
43 }
44
45 /**
46 * new deque is empty
47 */
48 public void testConstructor1() {
49 assertTrue(new ConcurrentLinkedDeque().isEmpty());
50 assertEquals(0, new ConcurrentLinkedDeque().size());
51 }
52
53 /**
54 * Initializing from null Collection throws NPE
55 */
56 public void testConstructor3() {
57 try {
58 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null);
59 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];
69 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
70 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);
82 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
83 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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
95 for (int i = 0; i < SIZE; ++i)
96 assertEquals(ints[i], q.poll());
97 }
98
99 /**
100 * isEmpty is true before add, false after
101 */
102 public void testEmpty() {
103 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
104 assertTrue(q.isEmpty());
105 q.add(one);
106 assertFalse(q.isEmpty());
107 q.add(two);
108 q.remove();
109 q.remove();
110 assertTrue(q.isEmpty());
111 }
112
113 /**
114 * size() changes when elements added and removed
115 */
116 public void testSize() {
117 ConcurrentLinkedDeque q = populatedDeque(SIZE);
118 for (int i = 0; i < SIZE; ++i) {
119 assertEquals(SIZE-i, q.size());
120 q.remove();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = populatedDeque(3);
144 q.pollLast();
145 q.push(four);
146 assertSame(four, q.peekFirst());
147 }
148
149 /**
150 * pop() removes first element, or throws NSEE if empty
151 */
152 public void testPop() {
153 ConcurrentLinkedDeque 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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
191 q.offerLast(null);
192 shouldThrow();
193 } catch (NullPointerException success) {}
194 }
195
196 /**
197 * offer(x) succeeds
198 */
199 public void testOffer() {
200 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
257 q.addLast(null);
258 shouldThrow();
259 } catch (NullPointerException success) {}
260 }
261
262 /**
263 * add(x) succeeds
264 */
265 public void testAdd() {
266 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
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 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
301 q.addAll(null);
302 shouldThrow();
303 } catch (NullPointerException success) {}
304 }
305
306 /**
307 * addAll(this) throws IAE
308 */
309 public void testAddAllSelf() {
310 try {
311 ConcurrentLinkedDeque q = populatedDeque(SIZE);
312 q.addAll(q);
313 shouldThrow();
314 } catch (IllegalArgumentException success) {}
315 }
316
317 /**
318 * addAll of a collection with null elements throws NPE
319 */
320 public void testAddAll2() {
321 try {
322 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
323 Integer[] ints = new Integer[SIZE];
324 q.addAll(Arrays.asList(ints));
325 shouldThrow();
326 } catch (NullPointerException success) {}
327 }
328
329 /**
330 * addAll of a collection with any null elements throws NPE after
331 * possibly adding some elements
332 */
333 public void testAddAll3() {
334 try {
335 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
336 Integer[] ints = new Integer[SIZE];
337 for (int i = 0; i < SIZE-1; ++i)
338 ints[i] = new Integer(i);
339 q.addAll(Arrays.asList(ints));
340 shouldThrow();
341 } catch (NullPointerException success) {}
342 }
343
344 /**
345 * Deque contains all elements, in traversal order, of successful addAll
346 */
347 public void testAddAll5() {
348 Integer[] empty = new Integer[0];
349 Integer[] ints = new Integer[SIZE];
350 for (int i = 0; i < SIZE; ++i)
351 ints[i] = new Integer(i);
352 ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
353 assertFalse(q.addAll(Arrays.asList(empty)));
354 assertTrue(q.addAll(Arrays.asList(ints)));
355 for (int i = 0; i < SIZE; ++i)
356 assertEquals(ints[i], q.poll());
357 }
358
359 /**
360 * pollFirst() succeeds unless empty
361 */
362 public void testPollFirst() {
363 ConcurrentLinkedDeque q = populatedDeque(SIZE);
364 for (int i = 0; i < SIZE; ++i) {
365 assertEquals(i, q.pollFirst());
366 }
367 assertNull(q.pollFirst());
368 }
369
370 /**
371 * pollLast() succeeds unless empty
372 */
373 public void testPollLast() {
374 ConcurrentLinkedDeque q = populatedDeque(SIZE);
375 for (int i = SIZE-1; i >= 0; --i) {
376 assertEquals(i, q.pollLast());
377 }
378 assertNull(q.pollLast());
379 }
380
381 /**
382 * poll() succeeds unless empty
383 */
384 public void testPoll() {
385 ConcurrentLinkedDeque q = populatedDeque(SIZE);
386 for (int i = 0; i < SIZE; ++i) {
387 assertEquals(i, q.poll());
388 }
389 assertNull(q.poll());
390 }
391
392 /**
393 * peek() returns next element, or null if empty
394 */
395 public void testPeek() {
396 ConcurrentLinkedDeque q = populatedDeque(SIZE);
397 for (int i = 0; i < SIZE; ++i) {
398 assertEquals(i, q.peek());
399 assertEquals(i, q.poll());
400 assertTrue(q.peek() == null ||
401 !q.peek().equals(i));
402 }
403 assertNull(q.peek());
404 }
405
406 /**
407 * element() returns first element, or throws NSEE if empty
408 */
409 public void testElement() {
410 ConcurrentLinkedDeque q = populatedDeque(SIZE);
411 for (int i = 0; i < SIZE; ++i) {
412 assertEquals(i, q.element());
413 assertEquals(i, q.poll());
414 }
415 try {
416 q.element();
417 shouldThrow();
418 } catch (NoSuchElementException success) {}
419 }
420
421 /**
422 * remove() removes next element, or throws NSEE if empty
423 */
424 public void testRemove() {
425 ConcurrentLinkedDeque q = populatedDeque(SIZE);
426 for (int i = 0; i < SIZE; ++i) {
427 assertEquals(i, q.remove());
428 }
429 try {
430 q.remove();
431 shouldThrow();
432 } catch (NoSuchElementException success) {}
433 }
434
435 /**
436 * remove(x) removes x and returns true if present
437 */
438 public void testRemoveElement() {
439 ConcurrentLinkedDeque q = populatedDeque(SIZE);
440 for (int i = 1; i < SIZE; i += 2) {
441 assertTrue(q.contains(i));
442 assertTrue(q.remove(i));
443 assertFalse(q.contains(i));
444 assertTrue(q.contains(i-1));
445 }
446 for (int i = 0; i < SIZE; i += 2) {
447 assertTrue(q.contains(i));
448 assertTrue(q.remove(i));
449 assertFalse(q.contains(i));
450 assertFalse(q.remove(i+1));
451 assertFalse(q.contains(i+1));
452 }
453 assertTrue(q.isEmpty());
454 }
455
456 /**
457 * peekFirst() returns next element, or null if empty
458 */
459 public void testPeekFirst() {
460 ConcurrentLinkedDeque q = populatedDeque(SIZE);
461 for (int i = 0; i < SIZE; ++i) {
462 assertEquals(i, q.peekFirst());
463 assertEquals(i, q.pollFirst());
464 assertTrue(q.peekFirst() == null ||
465 !q.peekFirst().equals(i));
466 }
467 assertNull(q.peekFirst());
468 }
469
470 /**
471 * peekLast() returns next element, or null if empty
472 */
473 public void testPeekLast() {
474 ConcurrentLinkedDeque q = populatedDeque(SIZE);
475 for (int i = SIZE-1; i >= 0; --i) {
476 assertEquals(i, q.peekLast());
477 assertEquals(i, q.pollLast());
478 assertTrue(q.peekLast() == null ||
479 !q.peekLast().equals(i));
480 }
481 assertNull(q.peekLast());
482 }
483
484 /**
485 * getFirst() returns first element, or throws NSEE if empty
486 */
487 public void testFirstElement() {
488 ConcurrentLinkedDeque q = populatedDeque(SIZE);
489 for (int i = 0; i < SIZE; ++i) {
490 assertEquals(i, q.getFirst());
491 assertEquals(i, q.pollFirst());
492 }
493 try {
494 q.getFirst();
495 shouldThrow();
496 } catch (NoSuchElementException success) {}
497 }
498
499 /**
500 * getLast() returns last element, or throws NSEE if empty
501 */
502 public void testLastElement() {
503 ConcurrentLinkedDeque q = populatedDeque(SIZE);
504 for (int i = SIZE-1; i >= 0; --i) {
505 assertEquals(i, q.getLast());
506 assertEquals(i, q.pollLast());
507 }
508 try {
509 q.getLast();
510 shouldThrow();
511 } catch (NoSuchElementException success) {}
512 assertNull(q.peekLast());
513 }
514
515 /**
516 * removeFirst() removes first element, or throws NSEE if empty
517 */
518 public void testRemoveFirst() {
519 ConcurrentLinkedDeque q = populatedDeque(SIZE);
520 for (int i = 0; i < SIZE; ++i) {
521 assertEquals(i, q.removeFirst());
522 }
523 try {
524 q.removeFirst();
525 shouldThrow();
526 } catch (NoSuchElementException success) {}
527 assertNull(q.peekFirst());
528 }
529
530 /**
531 * removeLast() removes last element, or throws NSEE if empty
532 */
533 public void testRemoveLast() {
534 ConcurrentLinkedDeque q = populatedDeque(SIZE);
535 for (int i = SIZE - 1; i >= 0; --i) {
536 assertEquals(i, q.removeLast());
537 }
538 try {
539 q.removeLast();
540 shouldThrow();
541 } catch (NoSuchElementException success) {}
542 assertNull(q.peekLast());
543 }
544
545 /**
546 * removeFirstOccurrence(x) removes x and returns true if present
547 */
548 public void testRemoveFirstOccurrence() {
549 ConcurrentLinkedDeque q = populatedDeque(SIZE);
550 for (int i = 1; i < SIZE; i += 2) {
551 assertTrue(q.removeFirstOccurrence(new Integer(i)));
552 }
553 for (int i = 0; i < SIZE; i += 2) {
554 assertTrue(q.removeFirstOccurrence(new Integer(i)));
555 assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
556 }
557 assertTrue(q.isEmpty());
558 }
559
560 /**
561 * removeLastOccurrence(x) removes x and returns true if present
562 */
563 public void testRemoveLastOccurrence() {
564 ConcurrentLinkedDeque q = populatedDeque(SIZE);
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 }
574
575 /**
576 * contains(x) reports true when elements added but not yet removed
577 */
578 public void testContains() {
579 ConcurrentLinkedDeque q = populatedDeque(SIZE);
580 for (int i = 0; i < SIZE; ++i) {
581 assertTrue(q.contains(new Integer(i)));
582 q.poll();
583 assertFalse(q.contains(new Integer(i)));
584 }
585 }
586
587 /**
588 * clear() removes all elements
589 */
590 public void testClear() {
591 ConcurrentLinkedDeque q = populatedDeque(SIZE);
592 q.clear();
593 assertTrue(q.isEmpty());
594 assertEquals(0, q.size());
595 q.add(one);
596 assertFalse(q.isEmpty());
597 q.clear();
598 assertTrue(q.isEmpty());
599 }
600
601 /**
602 * containsAll(c) is true when c contains a subset of elements
603 */
604 public void testContainsAll() {
605 ConcurrentLinkedDeque q = populatedDeque(SIZE);
606 ConcurrentLinkedDeque p = new ConcurrentLinkedDeque();
607 for (int i = 0; i < SIZE; ++i) {
608 assertTrue(q.containsAll(p));
609 assertFalse(p.containsAll(q));
610 p.add(new Integer(i));
611 }
612 assertTrue(p.containsAll(q));
613 }
614
615 /**
616 * retainAll(c) retains only those elements of c and reports true if change
617 */
618 public void testRetainAll() {
619 ConcurrentLinkedDeque q = populatedDeque(SIZE);
620 ConcurrentLinkedDeque p = populatedDeque(SIZE);
621 for (int i = 0; i < SIZE; ++i) {
622 boolean changed = q.retainAll(p);
623 if (i == 0)
624 assertFalse(changed);
625 else
626 assertTrue(changed);
627
628 assertTrue(q.containsAll(p));
629 assertEquals(SIZE-i, q.size());
630 p.remove();
631 }
632 }
633
634 /**
635 * removeAll(c) removes only those elements of c and reports true if changed
636 */
637 public void testRemoveAll() {
638 for (int i = 1; i < SIZE; ++i) {
639 ConcurrentLinkedDeque q = populatedDeque(SIZE);
640 ConcurrentLinkedDeque p = populatedDeque(i);
641 assertTrue(q.removeAll(p));
642 assertEquals(SIZE-i, q.size());
643 for (int j = 0; j < i; ++j) {
644 Integer x = (Integer)(p.remove());
645 assertFalse(q.contains(x));
646 }
647 }
648 }
649
650 /**
651 * toArray() contains all elements in FIFO order
652 */
653 public void testToArray() {
654 ConcurrentLinkedDeque q = populatedDeque(SIZE);
655 Object[] o = q.toArray();
656 for (int i = 0; i < o.length; i++)
657 assertSame(o[i], q.poll());
658 }
659
660 /**
661 * toArray(a) contains all elements in FIFO order
662 */
663 public void testToArray2() {
664 ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
665 Integer[] ints = new Integer[SIZE];
666 Integer[] array = q.toArray(ints);
667 assertSame(ints, array);
668 for (int i = 0; i < ints.length; i++)
669 assertSame(ints[i], q.poll());
670 }
671
672 /**
673 * toArray(null) throws NullPointerException
674 */
675 public void testToArray_NullArg() {
676 ConcurrentLinkedDeque q = populatedDeque(SIZE);
677 try {
678 q.toArray(null);
679 shouldThrow();
680 } catch (NullPointerException success) {}
681 }
682
683 /**
684 * toArray(incompatible array type) throws ArrayStoreException
685 */
686 public void testToArray1_BadArg() {
687 ConcurrentLinkedDeque q = populatedDeque(SIZE);
688 try {
689 q.toArray(new String[10]);
690 shouldThrow();
691 } catch (ArrayStoreException success) {}
692 }
693
694 /**
695 * Iterator iterates through all elements
696 */
697 public void testIterator() {
698 ConcurrentLinkedDeque q = populatedDeque(SIZE);
699 int i = 0;
700 Iterator it = q.iterator();
701 while (it.hasNext()) {
702 assertTrue(q.contains(it.next()));
703 ++i;
704 }
705 assertEquals(i, SIZE);
706 }
707
708 /**
709 * Iterator ordering is FIFO
710 */
711 public void testIteratorOrdering() {
712 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
713 q.add(one);
714 q.add(two);
715 q.add(three);
716
717 int k = 0;
718 for (Iterator it = q.iterator(); it.hasNext();) {
719 assertEquals(++k, it.next());
720 }
721
722 assertEquals(3, k);
723 }
724
725 /**
726 * Modifications do not cause iterators to fail
727 */
728 public void testWeaklyConsistentIteration() {
729 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
730 q.add(one);
731 q.add(two);
732 q.add(three);
733
734 for (Iterator it = q.iterator(); it.hasNext();) {
735 q.remove();
736 it.next();
737 }
738
739 assertEquals("deque should be empty again", 0, q.size());
740 }
741
742 /**
743 * iterator.remove() removes current element
744 */
745 public void testIteratorRemove() {
746 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
747 final Random rng = new Random();
748 for (int iters = 0; iters < 100; ++iters) {
749 int max = rng.nextInt(5) + 2;
750 int split = rng.nextInt(max-1) + 1;
751 for (int j = 1; j <= max; ++j)
752 q.add(new Integer(j));
753 Iterator it = q.iterator();
754 for (int j = 1; j <= split; ++j)
755 assertEquals(it.next(), new Integer(j));
756 it.remove();
757 assertEquals(it.next(), new Integer(split+1));
758 for (int j = 1; j <= split; ++j)
759 q.remove(new Integer(j));
760 it = q.iterator();
761 for (int j = split+1; j <= max; ++j) {
762 assertEquals(it.next(), new Integer(j));
763 it.remove();
764 }
765 assertFalse(it.hasNext());
766 assertTrue(q.isEmpty());
767 }
768 }
769
770 /**
771 * Descending iterator iterates through all elements
772 */
773 public void testDescendingIterator() {
774 ConcurrentLinkedDeque q = populatedDeque(SIZE);
775 int i = 0;
776 Iterator it = q.descendingIterator();
777 while (it.hasNext()) {
778 assertTrue(q.contains(it.next()));
779 ++i;
780 }
781 assertEquals(i, SIZE);
782 assertFalse(it.hasNext());
783 try {
784 it.next();
785 shouldThrow();
786 } catch (NoSuchElementException success) {}
787 }
788
789 /**
790 * Descending iterator ordering is reverse FIFO
791 */
792 public void testDescendingIteratorOrdering() {
793 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
794 for (int iters = 0; iters < 100; ++iters) {
795 q.add(new Integer(3));
796 q.add(new Integer(2));
797 q.add(new Integer(1));
798 int k = 0;
799 for (Iterator it = q.descendingIterator(); it.hasNext();) {
800 assertEquals(++k, it.next());
801 }
802
803 assertEquals(3, k);
804 q.remove();
805 q.remove();
806 q.remove();
807 }
808 }
809
810 /**
811 * descendingIterator.remove() removes current element
812 */
813 public void testDescendingIteratorRemove() {
814 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
815 final Random rng = new Random();
816 for (int iters = 0; iters < 100; ++iters) {
817 int max = rng.nextInt(5) + 2;
818 int split = rng.nextInt(max-1) + 1;
819 for (int j = max; j >= 1; --j)
820 q.add(new Integer(j));
821 Iterator it = q.descendingIterator();
822 for (int j = 1; j <= split; ++j)
823 assertEquals(it.next(), new Integer(j));
824 it.remove();
825 assertEquals(it.next(), new Integer(split+1));
826 for (int j = 1; j <= split; ++j)
827 q.remove(new Integer(j));
828 it = q.descendingIterator();
829 for (int j = split+1; j <= max; ++j) {
830 assertEquals(it.next(), new Integer(j));
831 it.remove();
832 }
833 assertFalse(it.hasNext());
834 assertTrue(q.isEmpty());
835 }
836 }
837
838 /**
839 * toString() contains toStrings of elements
840 */
841 public void testToString() {
842 ConcurrentLinkedDeque q = populatedDeque(SIZE);
843 String s = q.toString();
844 for (int i = 0; i < SIZE; ++i) {
845 assertTrue(s.contains(String.valueOf(i)));
846 }
847 }
848
849 /**
850 * A deserialized serialized deque has same elements in same order
851 */
852 public void testSerialization() throws Exception {
853 Queue x = populatedDeque(SIZE);
854 Queue y = serialClone(x);
855
856 assertNotSame(x, y);
857 assertEquals(x.size(), y.size());
858 assertEquals(x.toString(), y.toString());
859 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
860 while (!x.isEmpty()) {
861 assertFalse(y.isEmpty());
862 assertEquals(x.remove(), y.remove());
863 }
864 assertTrue(y.isEmpty());
865 }
866
867 /**
868 * contains(null) always return false.
869 * remove(null) always throws NullPointerException.
870 */
871 public void testNeverContainsNull() {
872 Deque<?>[] qs = {
873 new ConcurrentLinkedDeque<Object>(),
874 populatedDeque(2),
875 };
876
877 for (Deque<?> q : qs) {
878 assertFalse(q.contains(null));
879 try {
880 assertFalse(q.remove(null));
881 shouldThrow();
882 } catch (NullPointerException success) {}
883 try {
884 assertFalse(q.removeFirstOccurrence(null));
885 shouldThrow();
886 } catch (NullPointerException success) {}
887 try {
888 assertFalse(q.removeLastOccurrence(null));
889 shouldThrow();
890 } catch (NullPointerException success) {}
891 }
892 }
893 }