ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
Revision: 1.13
Committed: Sun Nov 23 22:27:06 2014 UTC (9 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +27 -0 lines
Log Message:
add tests for contains(null), remove(null)

File Contents

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