ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
Revision: 1.23
Committed: Sun Oct 16 20:16:36 2016 UTC (7 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +9 -1 lines
Log Message:
extend CollectionImplementation mechanism to other collections

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