ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.4
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +93 -93 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 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     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7     import junit.framework.*;
8     import java.util.*;
9     import java.util.concurrent.*;
10     import java.io.*;
11    
12     public class LinkedBlockingDequeTest extends JSR166TestCase {
13     public static void main(String[] args) {
14 jsr166 1.4 junit.textui.TestRunner.run (suite());
15 dl 1.1 }
16    
17     public static Test suite() {
18     return new TestSuite(LinkedBlockingDequeTest.class);
19     }
20    
21     /**
22     * Create a deque of given size containing consecutive
23     * Integers 0 ... n.
24     */
25     private LinkedBlockingDeque populatedDeque(int n) {
26     LinkedBlockingDeque q = new LinkedBlockingDeque(n);
27     assertTrue(q.isEmpty());
28     for(int i = 0; i < n; i++)
29     assertTrue(q.offer(new Integer(i)));
30     assertFalse(q.isEmpty());
31     assertEquals(0, q.remainingCapacity());
32     assertEquals(n, q.size());
33     return q;
34     }
35    
36     /**
37     * isEmpty is true before add, false after
38     */
39     public void testEmpty() {
40     LinkedBlockingDeque q = new LinkedBlockingDeque();
41     assertTrue(q.isEmpty());
42     q.add(new Integer(1));
43     assertFalse(q.isEmpty());
44     q.add(new Integer(2));
45     q.removeFirst();
46     q.removeFirst();
47     assertTrue(q.isEmpty());
48     }
49    
50     /**
51     * size changes when elements added and removed
52     */
53     public void testSize() {
54     LinkedBlockingDeque q = populatedDeque(SIZE);
55     for (int i = 0; i < SIZE; ++i) {
56     assertEquals(SIZE-i, q.size());
57     q.removeFirst();
58     }
59     for (int i = 0; i < SIZE; ++i) {
60     assertEquals(i, q.size());
61     q.add(new Integer(i));
62     }
63     }
64    
65     /**
66     * offer(null) throws NPE
67     */
68     public void testOfferFirstNull() {
69     try {
70     LinkedBlockingDeque q = new LinkedBlockingDeque();
71     q.offerFirst(null);
72     shouldThrow();
73 jsr166 1.4 } catch (NullPointerException success) {
74     }
75 dl 1.1 }
76    
77     /**
78 jsr166 1.4 * OfferFirst succeeds
79 dl 1.1 */
80     public void testOfferFirst() {
81     LinkedBlockingDeque q = new LinkedBlockingDeque();
82     assertTrue(q.offerFirst(new Integer(0)));
83     assertTrue(q.offerFirst(new Integer(1)));
84     }
85    
86     /**
87 jsr166 1.4 * OfferLast succeeds
88 dl 1.1 */
89     public void testOfferLast() {
90     LinkedBlockingDeque q = new LinkedBlockingDeque();
91     assertTrue(q.offerLast(new Integer(0)));
92     assertTrue(q.offerLast(new Integer(1)));
93     }
94    
95     /**
96     * pollFirst succeeds unless empty
97     */
98     public void testPollFirst() {
99     LinkedBlockingDeque q = populatedDeque(SIZE);
100     for (int i = 0; i < SIZE; ++i) {
101     assertEquals(i, ((Integer)q.pollFirst()).intValue());
102     }
103     assertNull(q.pollFirst());
104     }
105    
106     /**
107     * pollLast succeeds unless empty
108     */
109     public void testPollLast() {
110     LinkedBlockingDeque q = populatedDeque(SIZE);
111     for (int i = SIZE-1; i >= 0; --i) {
112     assertEquals(i, ((Integer)q.pollLast()).intValue());
113     }
114     assertNull(q.pollLast());
115     }
116    
117     /**
118     * peekFirst returns next element, or null if empty
119     */
120     public void testPeekFirst() {
121     LinkedBlockingDeque q = populatedDeque(SIZE);
122     for (int i = 0; i < SIZE; ++i) {
123     assertEquals(i, ((Integer)q.peekFirst()).intValue());
124     q.pollFirst();
125     assertTrue(q.peekFirst() == null ||
126     i != ((Integer)q.peekFirst()).intValue());
127     }
128     assertNull(q.peekFirst());
129     }
130    
131     /**
132     * peek returns next element, or null if empty
133     */
134     public void testPeek() {
135     LinkedBlockingDeque q = populatedDeque(SIZE);
136     for (int i = 0; i < SIZE; ++i) {
137     assertEquals(i, ((Integer)q.peek()).intValue());
138     q.pollFirst();
139     assertTrue(q.peek() == null ||
140     i != ((Integer)q.peek()).intValue());
141     }
142     assertNull(q.peek());
143     }
144    
145     /**
146     * peekLast returns next element, or null if empty
147     */
148     public void testPeekLast() {
149     LinkedBlockingDeque q = populatedDeque(SIZE);
150     for (int i = SIZE-1; i >= 0; --i) {
151     assertEquals(i, ((Integer)q.peekLast()).intValue());
152     q.pollLast();
153     assertTrue(q.peekLast() == null ||
154     i != ((Integer)q.peekLast()).intValue());
155     }
156     assertNull(q.peekLast());
157     }
158    
159     /**
160     * getFirst returns next getFirst, or throws NSEE if empty
161     */
162     public void testFirstElement() {
163     LinkedBlockingDeque q = populatedDeque(SIZE);
164     for (int i = 0; i < SIZE; ++i) {
165     assertEquals(i, ((Integer)q.getFirst()).intValue());
166     q.pollFirst();
167     }
168     try {
169     q.getFirst();
170     shouldThrow();
171     }
172     catch (NoSuchElementException success) {}
173     }
174    
175     /**
176     * getLast returns next element, or throws NSEE if empty
177     */
178     public void testLastElement() {
179     LinkedBlockingDeque q = populatedDeque(SIZE);
180     for (int i = SIZE-1; i >= 0; --i) {
181     assertEquals(i, ((Integer)q.getLast()).intValue());
182     q.pollLast();
183     }
184     try {
185     q.getLast();
186     shouldThrow();
187     }
188     catch (NoSuchElementException success) {}
189     assertNull(q.peekLast());
190     }
191    
192     /**
193     * removeFirst removes next element, or throws NSEE if empty
194     */
195     public void testRemoveFirst() {
196     LinkedBlockingDeque q = populatedDeque(SIZE);
197     for (int i = 0; i < SIZE; ++i) {
198     assertEquals(i, ((Integer)q.removeFirst()).intValue());
199     }
200     try {
201     q.removeFirst();
202     shouldThrow();
203     } catch (NoSuchElementException success){
204 jsr166 1.4 }
205 dl 1.1 }
206    
207     /**
208     * remove removes next element, or throws NSEE if empty
209     */
210     public void testRemove() {
211     LinkedBlockingDeque q = populatedDeque(SIZE);
212     for (int i = 0; i < SIZE; ++i) {
213     assertEquals(i, ((Integer)q.remove()).intValue());
214     }
215     try {
216     q.remove();
217     shouldThrow();
218     } catch (NoSuchElementException success){
219 jsr166 1.4 }
220 dl 1.1 }
221    
222     /**
223     * removeFirstOccurrence(x) removes x and returns true if present
224     */
225     public void testRemoveFirstOccurrence() {
226     LinkedBlockingDeque q = populatedDeque(SIZE);
227     for (int i = 1; i < SIZE; i+=2) {
228     assertTrue(q.removeFirstOccurrence(new Integer(i)));
229     }
230     for (int i = 0; i < SIZE; i+=2) {
231     assertTrue(q.removeFirstOccurrence(new Integer(i)));
232     assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
233     }
234     assertTrue(q.isEmpty());
235     }
236    
237     /**
238     * removeLastOccurrence(x) removes x and returns true if present
239     */
240     public void testRemoveLastOccurrence() {
241     LinkedBlockingDeque q = populatedDeque(SIZE);
242     for (int i = 1; i < SIZE; i+=2) {
243     assertTrue(q.removeLastOccurrence(new Integer(i)));
244     }
245     for (int i = 0; i < SIZE; i+=2) {
246     assertTrue(q.removeLastOccurrence(new Integer(i)));
247     assertFalse(q.removeLastOccurrence(new Integer(i+1)));
248     }
249     assertTrue(q.isEmpty());
250     }
251    
252     /**
253     * peekFirst returns element inserted with addFirst
254     */
255     public void testAddFirst() {
256     LinkedBlockingDeque q = populatedDeque(3);
257     q.pollLast();
258     q.addFirst(four);
259     assertEquals(four,q.peekFirst());
260 jsr166 1.4 }
261 dl 1.1
262     /**
263     * peekLast returns element inserted with addLast
264     */
265     public void testAddLast() {
266     LinkedBlockingDeque q = populatedDeque(3);
267     q.pollLast();
268     q.addLast(four);
269     assertEquals(four,q.peekLast());
270 jsr166 1.4 }
271 dl 1.1
272    
273     /**
274     * A new deque has the indicated capacity, or Integer.MAX_VALUE if
275     * none given
276     */
277     public void testConstructor1() {
278     assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
279     assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
280     }
281    
282     /**
283     * Constructor throws IAE if capacity argument nonpositive
284     */
285     public void testConstructor2() {
286     try {
287     LinkedBlockingDeque q = new LinkedBlockingDeque(0);
288     shouldThrow();
289     }
290     catch (IllegalArgumentException success) {}
291     }
292    
293     /**
294     * Initializing from null Collection throws NPE
295     */
296     public void testConstructor3() {
297     try {
298     LinkedBlockingDeque q = new LinkedBlockingDeque(null);
299     shouldThrow();
300     }
301     catch (NullPointerException success) {}
302     }
303    
304     /**
305     * Initializing from Collection of null elements throws NPE
306     */
307     public void testConstructor4() {
308     try {
309     Integer[] ints = new Integer[SIZE];
310     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
311     shouldThrow();
312     }
313     catch (NullPointerException success) {}
314     }
315    
316     /**
317     * Initializing from Collection with some null elements throws NPE
318     */
319     public void testConstructor5() {
320     try {
321     Integer[] ints = new Integer[SIZE];
322     for (int i = 0; i < SIZE-1; ++i)
323     ints[i] = new Integer(i);
324     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
325     shouldThrow();
326     }
327     catch (NullPointerException success) {}
328     }
329    
330     /**
331     * Deque contains all elements of collection used to initialize
332     */
333     public void testConstructor6() {
334     try {
335     Integer[] ints = new Integer[SIZE];
336     for (int i = 0; i < SIZE; ++i)
337     ints[i] = new Integer(i);
338     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
339     for (int i = 0; i < SIZE; ++i)
340     assertEquals(ints[i], q.poll());
341     }
342     finally {}
343     }
344    
345     /**
346     * Deque transitions from empty to full when elements added
347     */
348     public void testEmptyFull() {
349     LinkedBlockingDeque q = new LinkedBlockingDeque(2);
350     assertTrue(q.isEmpty());
351     assertEquals("should have room for 2", 2, q.remainingCapacity());
352     q.add(one);
353     assertFalse(q.isEmpty());
354     q.add(two);
355     assertFalse(q.isEmpty());
356     assertEquals(0, q.remainingCapacity());
357     assertFalse(q.offer(three));
358     }
359    
360     /**
361     * remainingCapacity decreases on add, increases on remove
362     */
363     public void testRemainingCapacity() {
364     LinkedBlockingDeque q = populatedDeque(SIZE);
365     for (int i = 0; i < SIZE; ++i) {
366     assertEquals(i, q.remainingCapacity());
367     assertEquals(SIZE-i, q.size());
368     q.remove();
369     }
370     for (int i = 0; i < SIZE; ++i) {
371     assertEquals(SIZE-i, q.remainingCapacity());
372     assertEquals(i, q.size());
373     q.add(new Integer(i));
374     }
375     }
376    
377     /**
378     * offer(null) throws NPE
379     */
380     public void testOfferNull() {
381     try {
382     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
383     q.offer(null);
384     shouldThrow();
385 jsr166 1.4 } catch (NullPointerException success) { }
386 dl 1.1 }
387    
388     /**
389     * add(null) throws NPE
390     */
391     public void testAddNull() {
392     try {
393     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
394     q.add(null);
395     shouldThrow();
396 jsr166 1.4 } catch (NullPointerException success) { }
397 dl 1.1 }
398    
399     /**
400     * push(null) throws NPE
401     */
402     public void testPushNull() {
403     try {
404     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
405     q.push(null);
406     shouldThrow();
407 jsr166 1.4 } catch (NullPointerException success) { }
408 dl 1.1 }
409    
410     /**
411     * push succeeds if not full; throws ISE if full
412     */
413     public void testPush() {
414     try {
415     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
416     for (int i = 0; i < SIZE; ++i) {
417     Integer I = new Integer(i);
418     q.push(I);
419     assertEquals(I, q.peek());
420     }
421     assertEquals(0, q.remainingCapacity());
422     q.push(new Integer(SIZE));
423     } catch (IllegalStateException success){
424 jsr166 1.4 }
425 dl 1.1 }
426    
427     /**
428     * peekFirst returns element inserted with push
429     */
430     public void testPushWithPeek() {
431     LinkedBlockingDeque q = populatedDeque(3);
432     q.pollLast();
433     q.push(four);
434     assertEquals(four,q.peekFirst());
435 jsr166 1.4 }
436 dl 1.1
437    
438     /**
439     * pop removes next element, or throws NSEE if empty
440     */
441     public void testPop() {
442     LinkedBlockingDeque q = populatedDeque(SIZE);
443     for (int i = 0; i < SIZE; ++i) {
444     assertEquals(i, ((Integer)q.pop()).intValue());
445     }
446     try {
447     q.pop();
448     shouldThrow();
449     } catch (NoSuchElementException success){
450 jsr166 1.4 }
451 dl 1.1 }
452    
453    
454     /**
455     * Offer succeeds if not full; fails if full
456     */
457     public void testOffer() {
458     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
459     assertTrue(q.offer(zero));
460     assertFalse(q.offer(one));
461     }
462    
463     /**
464     * add succeeds if not full; throws ISE if full
465     */
466     public void testAdd() {
467     try {
468     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
469     for (int i = 0; i < SIZE; ++i) {
470     assertTrue(q.add(new Integer(i)));
471     }
472     assertEquals(0, q.remainingCapacity());
473     q.add(new Integer(SIZE));
474     } catch (IllegalStateException success){
475 jsr166 1.4 }
476 dl 1.1 }
477    
478     /**
479     * addAll(null) throws NPE
480     */
481     public void testAddAll1() {
482     try {
483     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
484     q.addAll(null);
485     shouldThrow();
486     }
487     catch (NullPointerException success) {}
488     }
489    
490     /**
491     * addAll(this) throws IAE
492     */
493     public void testAddAllSelf() {
494     try {
495     LinkedBlockingDeque q = populatedDeque(SIZE);
496     q.addAll(q);
497     shouldThrow();
498     }
499     catch (IllegalArgumentException success) {}
500     }
501    
502     /**
503     * addAll of a collection with null elements throws NPE
504     */
505     public void testAddAll2() {
506     try {
507     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
508     Integer[] ints = new Integer[SIZE];
509     q.addAll(Arrays.asList(ints));
510     shouldThrow();
511     }
512     catch (NullPointerException success) {}
513     }
514     /**
515     * addAll of a collection with any null elements throws NPE after
516     * possibly adding some elements
517     */
518     public void testAddAll3() {
519     try {
520     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
521     Integer[] ints = new Integer[SIZE];
522     for (int i = 0; i < SIZE-1; ++i)
523     ints[i] = new Integer(i);
524     q.addAll(Arrays.asList(ints));
525     shouldThrow();
526     }
527     catch (NullPointerException success) {}
528     }
529     /**
530     * addAll throws ISE if not enough room
531     */
532     public void testAddAll4() {
533     try {
534     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
535     Integer[] ints = new Integer[SIZE];
536     for (int i = 0; i < SIZE; ++i)
537     ints[i] = new Integer(i);
538     q.addAll(Arrays.asList(ints));
539     shouldThrow();
540     }
541     catch (IllegalStateException success) {}
542     }
543     /**
544     * Deque contains all elements, in traversal order, of successful addAll
545     */
546     public void testAddAll5() {
547     try {
548     Integer[] empty = new Integer[0];
549     Integer[] ints = new Integer[SIZE];
550     for (int i = 0; i < SIZE; ++i)
551     ints[i] = new Integer(i);
552     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553     assertFalse(q.addAll(Arrays.asList(empty)));
554     assertTrue(q.addAll(Arrays.asList(ints)));
555     for (int i = 0; i < SIZE; ++i)
556     assertEquals(ints[i], q.poll());
557     }
558     finally {}
559     }
560    
561    
562     /**
563     * put(null) throws NPE
564     */
565     public void testPutNull() {
566     try {
567     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568     q.put(null);
569     shouldThrow();
570 jsr166 1.4 }
571 dl 1.1 catch (NullPointerException success){
572 jsr166 1.4 }
573 dl 1.1 catch (InterruptedException ie) {
574     unexpectedException();
575     }
576     }
577    
578     /**
579     * all elements successfully put are contained
580     */
581     public void testPut() {
582     try {
583     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584     for (int i = 0; i < SIZE; ++i) {
585     Integer I = new Integer(i);
586     q.put(I);
587     assertTrue(q.contains(I));
588     }
589     assertEquals(0, q.remainingCapacity());
590     }
591     catch (InterruptedException ie) {
592     unexpectedException();
593     }
594     }
595    
596     /**
597     * put blocks interruptibly if full
598     */
599     public void testBlockingPut() {
600     Thread t = new Thread(new Runnable() {
601     public void run() {
602     int added = 0;
603     try {
604     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
605     for (int i = 0; i < SIZE; ++i) {
606     q.put(new Integer(i));
607     ++added;
608     }
609     q.put(new Integer(SIZE));
610     threadShouldThrow();
611     } catch (InterruptedException ie){
612     threadAssertEquals(added, SIZE);
613 jsr166 1.4 }
614 dl 1.1 }});
615     t.start();
616 jsr166 1.4 try {
617     Thread.sleep(SHORT_DELAY_MS);
618 dl 1.1 t.interrupt();
619     t.join();
620     }
621     catch (InterruptedException ie) {
622     unexpectedException();
623     }
624     }
625    
626     /**
627     * put blocks waiting for take when full
628     */
629     public void testPutWithTake() {
630     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
631     Thread t = new Thread(new Runnable() {
632     public void run() {
633     int added = 0;
634     try {
635     q.put(new Object());
636     ++added;
637     q.put(new Object());
638     ++added;
639     q.put(new Object());
640     ++added;
641     q.put(new Object());
642     ++added;
643     threadShouldThrow();
644     } catch (InterruptedException e){
645     threadAssertTrue(added >= 2);
646     }
647     }
648     });
649     try {
650     t.start();
651     Thread.sleep(SHORT_DELAY_MS);
652     q.take();
653     t.interrupt();
654     t.join();
655     } catch (Exception e){
656     unexpectedException();
657     }
658     }
659    
660     /**
661     * timed offer times out if full and elements not taken
662     */
663     public void testTimedOffer() {
664     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
665     Thread t = new Thread(new Runnable() {
666     public void run() {
667     try {
668     q.put(new Object());
669     q.put(new Object());
670     threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
671     q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
672     threadShouldThrow();
673     } catch (InterruptedException success){}
674     }
675     });
676 jsr166 1.4
677 dl 1.1 try {
678     t.start();
679     Thread.sleep(SMALL_DELAY_MS);
680     t.interrupt();
681     t.join();
682     } catch (Exception e){
683     unexpectedException();
684     }
685     }
686    
687     /**
688     * take retrieves elements in FIFO order
689     */
690     public void testTake() {
691     try {
692     LinkedBlockingDeque q = populatedDeque(SIZE);
693     for (int i = 0; i < SIZE; ++i) {
694     assertEquals(i, ((Integer)q.take()).intValue());
695     }
696     } catch (InterruptedException e){
697     unexpectedException();
698 jsr166 1.4 }
699 dl 1.1 }
700    
701     /**
702     * take blocks interruptibly when empty
703     */
704     public void testTakeFromEmpty() {
705     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
706     Thread t = new Thread(new Runnable() {
707     public void run() {
708     try {
709     q.take();
710     threadShouldThrow();
711 jsr166 1.4 } catch (InterruptedException success){ }
712 dl 1.1 }
713     });
714     try {
715     t.start();
716     Thread.sleep(SHORT_DELAY_MS);
717     t.interrupt();
718     t.join();
719     } catch (Exception e){
720     unexpectedException();
721     }
722     }
723    
724     /**
725     * Take removes existing elements until empty, then blocks interruptibly
726     */
727     public void testBlockingTake() {
728     Thread t = new Thread(new Runnable() {
729     public void run() {
730     try {
731     LinkedBlockingDeque q = populatedDeque(SIZE);
732     for (int i = 0; i < SIZE; ++i) {
733     assertEquals(i, ((Integer)q.take()).intValue());
734     }
735     q.take();
736     threadShouldThrow();
737     } catch (InterruptedException success){
738 jsr166 1.4 }
739 dl 1.1 }});
740     t.start();
741 jsr166 1.4 try {
742     Thread.sleep(SHORT_DELAY_MS);
743 dl 1.1 t.interrupt();
744     t.join();
745     }
746     catch (InterruptedException ie) {
747     unexpectedException();
748     }
749     }
750    
751    
752     /**
753     * poll succeeds unless empty
754     */
755     public void testPoll() {
756     LinkedBlockingDeque q = populatedDeque(SIZE);
757     for (int i = 0; i < SIZE; ++i) {
758     assertEquals(i, ((Integer)q.poll()).intValue());
759     }
760     assertNull(q.poll());
761     }
762    
763     /**
764     * timed poll with zero timeout succeeds when non-empty, else times out
765     */
766     public void testTimedPoll0() {
767     try {
768     LinkedBlockingDeque q = populatedDeque(SIZE);
769     for (int i = 0; i < SIZE; ++i) {
770     assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
771     }
772     assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773     } catch (InterruptedException e){
774     unexpectedException();
775 jsr166 1.4 }
776 dl 1.1 }
777    
778     /**
779     * timed poll with nonzero timeout succeeds when non-empty, else times out
780     */
781     public void testTimedPoll() {
782     try {
783     LinkedBlockingDeque q = populatedDeque(SIZE);
784     for (int i = 0; i < SIZE; ++i) {
785     assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
786     }
787     assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
788     } catch (InterruptedException e){
789     unexpectedException();
790 jsr166 1.4 }
791 dl 1.1 }
792    
793     /**
794     * Interrupted timed poll throws InterruptedException instead of
795     * returning timeout status
796     */
797     public void testInterruptedTimedPoll() {
798     Thread t = new Thread(new Runnable() {
799     public void run() {
800     try {
801     LinkedBlockingDeque q = populatedDeque(SIZE);
802     for (int i = 0; i < SIZE; ++i) {
803     threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
804     }
805     threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
806     } catch (InterruptedException success){
807 jsr166 1.4 }
808 dl 1.1 }});
809     t.start();
810 jsr166 1.4 try {
811     Thread.sleep(SHORT_DELAY_MS);
812 dl 1.1 t.interrupt();
813     t.join();
814     }
815     catch (InterruptedException ie) {
816     unexpectedException();
817     }
818     }
819    
820     /**
821     * timed poll before a delayed offer fails; after offer succeeds;
822     * on interruption throws
823     */
824     public void testTimedPollWithOffer() {
825     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
826     Thread t = new Thread(new Runnable() {
827     public void run() {
828     try {
829     threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
830     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
831     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
832     threadShouldThrow();
833 jsr166 1.4 } catch (InterruptedException success) { }
834 dl 1.1 }
835     });
836     try {
837     t.start();
838     Thread.sleep(SMALL_DELAY_MS);
839     assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
840     t.interrupt();
841     t.join();
842     } catch (Exception e){
843     unexpectedException();
844     }
845 jsr166 1.4 }
846 dl 1.1
847    
848     /**
849     * putFirst(null) throws NPE
850     */
851     public void testPutFirstNull() {
852     try {
853     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
854     q.putFirst(null);
855     shouldThrow();
856 jsr166 1.4 }
857 dl 1.1 catch (NullPointerException success){
858 jsr166 1.4 }
859 dl 1.1 catch (InterruptedException ie) {
860     unexpectedException();
861     }
862     }
863    
864     /**
865     * all elements successfully putFirst are contained
866     */
867     public void testPutFirst() {
868     try {
869     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
870     for (int i = 0; i < SIZE; ++i) {
871     Integer I = new Integer(i);
872     q.putFirst(I);
873     assertTrue(q.contains(I));
874     }
875     assertEquals(0, q.remainingCapacity());
876     }
877     catch (InterruptedException ie) {
878     unexpectedException();
879     }
880     }
881    
882     /**
883     * putFirst blocks interruptibly if full
884     */
885     public void testBlockingPutFirst() {
886     Thread t = new Thread(new Runnable() {
887     public void run() {
888     int added = 0;
889     try {
890     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
891     for (int i = 0; i < SIZE; ++i) {
892     q.putFirst(new Integer(i));
893     ++added;
894     }
895     q.putFirst(new Integer(SIZE));
896     threadShouldThrow();
897     } catch (InterruptedException ie){
898     threadAssertEquals(added, SIZE);
899 jsr166 1.4 }
900 dl 1.1 }});
901     t.start();
902 jsr166 1.4 try {
903     Thread.sleep(SHORT_DELAY_MS);
904 dl 1.1 t.interrupt();
905     t.join();
906     }
907     catch (InterruptedException ie) {
908     unexpectedException();
909     }
910     }
911    
912     /**
913     * putFirst blocks waiting for take when full
914     */
915     public void testPutFirstWithTake() {
916     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
917     Thread t = new Thread(new Runnable() {
918     public void run() {
919     int added = 0;
920     try {
921     q.putFirst(new Object());
922     ++added;
923     q.putFirst(new Object());
924     ++added;
925     q.putFirst(new Object());
926     ++added;
927     q.putFirst(new Object());
928     ++added;
929     threadShouldThrow();
930     } catch (InterruptedException e){
931     threadAssertTrue(added >= 2);
932     }
933     }
934     });
935     try {
936     t.start();
937     Thread.sleep(SHORT_DELAY_MS);
938     q.take();
939     t.interrupt();
940     t.join();
941     } catch (Exception e){
942     unexpectedException();
943     }
944     }
945    
946     /**
947     * timed offerFirst times out if full and elements not taken
948     */
949     public void testTimedOfferFirst() {
950     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
951     Thread t = new Thread(new Runnable() {
952     public void run() {
953     try {
954     q.putFirst(new Object());
955     q.putFirst(new Object());
956     threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
957     q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
958     threadShouldThrow();
959     } catch (InterruptedException success){}
960     }
961     });
962 jsr166 1.4
963 dl 1.1 try {
964     t.start();
965     Thread.sleep(SMALL_DELAY_MS);
966     t.interrupt();
967     t.join();
968     } catch (Exception e){
969     unexpectedException();
970     }
971     }
972    
973     /**
974     * take retrieves elements in FIFO order
975     */
976     public void testTakeFirst() {
977     try {
978     LinkedBlockingDeque q = populatedDeque(SIZE);
979     for (int i = 0; i < SIZE; ++i) {
980     assertEquals(i, ((Integer)q.takeFirst()).intValue());
981     }
982     } catch (InterruptedException e){
983     unexpectedException();
984 jsr166 1.4 }
985 dl 1.1 }
986    
987     /**
988     * takeFirst blocks interruptibly when empty
989     */
990     public void testTakeFirstFromEmpty() {
991     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
992     Thread t = new Thread(new Runnable() {
993     public void run() {
994     try {
995     q.takeFirst();
996     threadShouldThrow();
997 jsr166 1.4 } catch (InterruptedException success){ }
998 dl 1.1 }
999     });
1000     try {
1001     t.start();
1002     Thread.sleep(SHORT_DELAY_MS);
1003     t.interrupt();
1004     t.join();
1005     } catch (Exception e){
1006     unexpectedException();
1007     }
1008     }
1009    
1010     /**
1011     * TakeFirst removes existing elements until empty, then blocks interruptibly
1012     */
1013     public void testBlockingTakeFirst() {
1014     Thread t = new Thread(new Runnable() {
1015     public void run() {
1016     try {
1017     LinkedBlockingDeque q = populatedDeque(SIZE);
1018     for (int i = 0; i < SIZE; ++i) {
1019     assertEquals(i, ((Integer)q.takeFirst()).intValue());
1020     }
1021     q.takeFirst();
1022     threadShouldThrow();
1023     } catch (InterruptedException success){
1024 jsr166 1.4 }
1025 dl 1.1 }});
1026     t.start();
1027 jsr166 1.4 try {
1028     Thread.sleep(SHORT_DELAY_MS);
1029 dl 1.1 t.interrupt();
1030     t.join();
1031     }
1032     catch (InterruptedException ie) {
1033     unexpectedException();
1034     }
1035     }
1036    
1037    
1038     /**
1039     * timed pollFirst with zero timeout succeeds when non-empty, else times out
1040     */
1041     public void testTimedPollFirst0() {
1042     try {
1043     LinkedBlockingDeque q = populatedDeque(SIZE);
1044     for (int i = 0; i < SIZE; ++i) {
1045     assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
1046     }
1047     assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048     } catch (InterruptedException e){
1049     unexpectedException();
1050 jsr166 1.4 }
1051 dl 1.1 }
1052    
1053     /**
1054     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1055     */
1056     public void testTimedPollFirst() {
1057     try {
1058     LinkedBlockingDeque q = populatedDeque(SIZE);
1059     for (int i = 0; i < SIZE; ++i) {
1060     assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1061     }
1062     assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1063     } catch (InterruptedException e){
1064     unexpectedException();
1065 jsr166 1.4 }
1066 dl 1.1 }
1067    
1068     /**
1069     * Interrupted timed pollFirst throws InterruptedException instead of
1070     * returning timeout status
1071     */
1072     public void testInterruptedTimedPollFirst() {
1073     Thread t = new Thread(new Runnable() {
1074     public void run() {
1075     try {
1076     LinkedBlockingDeque q = populatedDeque(SIZE);
1077     for (int i = 0; i < SIZE; ++i) {
1078     threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1079     }
1080     threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1081     } catch (InterruptedException success){
1082 jsr166 1.4 }
1083 dl 1.1 }});
1084     t.start();
1085 jsr166 1.4 try {
1086     Thread.sleep(SHORT_DELAY_MS);
1087 dl 1.1 t.interrupt();
1088     t.join();
1089     }
1090     catch (InterruptedException ie) {
1091     unexpectedException();
1092     }
1093     }
1094    
1095     /**
1096     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1097     * on interruption throws
1098     */
1099     public void testTimedPollFirstWithOfferFirst() {
1100     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1101     Thread t = new Thread(new Runnable() {
1102     public void run() {
1103     try {
1104     threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1105     q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1106     q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1107     threadShouldThrow();
1108 jsr166 1.4 } catch (InterruptedException success) { }
1109 dl 1.1 }
1110     });
1111     try {
1112     t.start();
1113     Thread.sleep(SMALL_DELAY_MS);
1114     assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1115     t.interrupt();
1116     t.join();
1117     } catch (Exception e){
1118     unexpectedException();
1119     }
1120 jsr166 1.4 }
1121 dl 1.1
1122     /**
1123     * putLast(null) throws NPE
1124     */
1125     public void testPutLastNull() {
1126     try {
1127     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1128     q.putLast(null);
1129     shouldThrow();
1130 jsr166 1.4 }
1131 dl 1.1 catch (NullPointerException success){
1132 jsr166 1.4 }
1133 dl 1.1 catch (InterruptedException ie) {
1134     unexpectedException();
1135     }
1136     }
1137    
1138     /**
1139     * all elements successfully putLast are contained
1140     */
1141     public void testPutLast() {
1142     try {
1143     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1144     for (int i = 0; i < SIZE; ++i) {
1145     Integer I = new Integer(i);
1146     q.putLast(I);
1147     assertTrue(q.contains(I));
1148     }
1149     assertEquals(0, q.remainingCapacity());
1150     }
1151     catch (InterruptedException ie) {
1152     unexpectedException();
1153     }
1154     }
1155    
1156     /**
1157     * putLast blocks interruptibly if full
1158     */
1159     public void testBlockingPutLast() {
1160     Thread t = new Thread(new Runnable() {
1161     public void run() {
1162     int added = 0;
1163     try {
1164     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1165     for (int i = 0; i < SIZE; ++i) {
1166     q.putLast(new Integer(i));
1167     ++added;
1168     }
1169     q.putLast(new Integer(SIZE));
1170     threadShouldThrow();
1171     } catch (InterruptedException ie){
1172     threadAssertEquals(added, SIZE);
1173 jsr166 1.4 }
1174 dl 1.1 }});
1175     t.start();
1176 jsr166 1.4 try {
1177     Thread.sleep(SHORT_DELAY_MS);
1178 dl 1.1 t.interrupt();
1179     t.join();
1180     }
1181     catch (InterruptedException ie) {
1182     unexpectedException();
1183     }
1184     }
1185    
1186     /**
1187     * putLast blocks waiting for take when full
1188     */
1189     public void testPutLastWithTake() {
1190     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1191     Thread t = new Thread(new Runnable() {
1192     public void run() {
1193     int added = 0;
1194     try {
1195     q.putLast(new Object());
1196     ++added;
1197     q.putLast(new Object());
1198     ++added;
1199     q.putLast(new Object());
1200     ++added;
1201     q.putLast(new Object());
1202     ++added;
1203     threadShouldThrow();
1204     } catch (InterruptedException e){
1205     threadAssertTrue(added >= 2);
1206     }
1207     }
1208     });
1209     try {
1210     t.start();
1211     Thread.sleep(SHORT_DELAY_MS);
1212     q.take();
1213     t.interrupt();
1214     t.join();
1215     } catch (Exception e){
1216     unexpectedException();
1217     }
1218     }
1219    
1220     /**
1221     * timed offerLast times out if full and elements not taken
1222     */
1223     public void testTimedOfferLast() {
1224     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1225     Thread t = new Thread(new Runnable() {
1226     public void run() {
1227     try {
1228     q.putLast(new Object());
1229     q.putLast(new Object());
1230     threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1231     q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1232     threadShouldThrow();
1233     } catch (InterruptedException success){}
1234     }
1235     });
1236 jsr166 1.4
1237 dl 1.1 try {
1238     t.start();
1239     Thread.sleep(SMALL_DELAY_MS);
1240     t.interrupt();
1241     t.join();
1242     } catch (Exception e){
1243     unexpectedException();
1244     }
1245     }
1246    
1247     /**
1248     * takeLast retrieves elements in FIFO order
1249     */
1250     public void testTakeLast() {
1251     try {
1252     LinkedBlockingDeque q = populatedDeque(SIZE);
1253     for (int i = 0; i < SIZE; ++i) {
1254     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255     }
1256     } catch (InterruptedException e){
1257     unexpectedException();
1258 jsr166 1.4 }
1259 dl 1.1 }
1260    
1261     /**
1262     * takeLast blocks interruptibly when empty
1263     */
1264     public void testTakeLastFromEmpty() {
1265     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1266     Thread t = new Thread(new Runnable() {
1267     public void run() {
1268     try {
1269     q.takeLast();
1270     threadShouldThrow();
1271 jsr166 1.4 } catch (InterruptedException success){ }
1272 dl 1.1 }
1273     });
1274     try {
1275     t.start();
1276     Thread.sleep(SHORT_DELAY_MS);
1277     t.interrupt();
1278     t.join();
1279     } catch (Exception e){
1280     unexpectedException();
1281     }
1282     }
1283    
1284     /**
1285     * TakeLast removes existing elements until empty, then blocks interruptibly
1286     */
1287     public void testBlockingTakeLast() {
1288     Thread t = new Thread(new Runnable() {
1289     public void run() {
1290     try {
1291     LinkedBlockingDeque q = populatedDeque(SIZE);
1292     for (int i = 0; i < SIZE; ++i) {
1293     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1294     }
1295     q.takeLast();
1296     threadShouldThrow();
1297     } catch (InterruptedException success){
1298 jsr166 1.4 }
1299 dl 1.1 }});
1300     t.start();
1301 jsr166 1.4 try {
1302     Thread.sleep(SHORT_DELAY_MS);
1303 dl 1.1 t.interrupt();
1304     t.join();
1305     }
1306     catch (InterruptedException ie) {
1307     unexpectedException();
1308     }
1309     }
1310    
1311    
1312     /**
1313     * timed pollLast with zero timeout succeeds when non-empty, else times out
1314     */
1315     public void testTimedPollLast0() {
1316     try {
1317     LinkedBlockingDeque q = populatedDeque(SIZE);
1318     for (int i = 0; i < SIZE; ++i) {
1319     assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1320     }
1321     assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322     } catch (InterruptedException e){
1323     unexpectedException();
1324 jsr166 1.4 }
1325 dl 1.1 }
1326    
1327     /**
1328     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1329     */
1330     public void testTimedPollLast() {
1331     try {
1332     LinkedBlockingDeque q = populatedDeque(SIZE);
1333     for (int i = 0; i < SIZE; ++i) {
1334     assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1335     }
1336     assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1337     } catch (InterruptedException e){
1338     unexpectedException();
1339 jsr166 1.4 }
1340 dl 1.1 }
1341    
1342     /**
1343     * Interrupted timed pollLast throws InterruptedException instead of
1344     * returning timeout status
1345     */
1346     public void testInterruptedTimedPollLast() {
1347     Thread t = new Thread(new Runnable() {
1348     public void run() {
1349     try {
1350     LinkedBlockingDeque q = populatedDeque(SIZE);
1351     for (int i = 0; i < SIZE; ++i) {
1352     threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1353     }
1354     threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1355     } catch (InterruptedException success){
1356 jsr166 1.4 }
1357 dl 1.1 }});
1358     t.start();
1359 jsr166 1.4 try {
1360     Thread.sleep(SHORT_DELAY_MS);
1361 dl 1.1 t.interrupt();
1362     t.join();
1363     }
1364     catch (InterruptedException ie) {
1365     unexpectedException();
1366     }
1367     }
1368    
1369     /**
1370     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1371     * on interruption throws
1372     */
1373     public void testTimedPollWithOfferLast() {
1374     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1375     Thread t = new Thread(new Runnable() {
1376     public void run() {
1377     try {
1378     threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1379     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1380     q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1381     threadShouldThrow();
1382 jsr166 1.4 } catch (InterruptedException success) { }
1383 dl 1.1 }
1384     });
1385     try {
1386     t.start();
1387     Thread.sleep(SMALL_DELAY_MS);
1388     assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1389     t.interrupt();
1390     t.join();
1391     } catch (Exception e){
1392     unexpectedException();
1393     }
1394 jsr166 1.4 }
1395 dl 1.1
1396    
1397     /**
1398     * element returns next element, or throws NSEE if empty
1399     */
1400     public void testElement() {
1401     LinkedBlockingDeque q = populatedDeque(SIZE);
1402     for (int i = 0; i < SIZE; ++i) {
1403     assertEquals(i, ((Integer)q.element()).intValue());
1404     q.poll();
1405     }
1406     try {
1407     q.element();
1408     shouldThrow();
1409     }
1410     catch (NoSuchElementException success) {}
1411     }
1412    
1413     /**
1414     * remove(x) removes x and returns true if present
1415     */
1416     public void testRemoveElement() {
1417     LinkedBlockingDeque q = populatedDeque(SIZE);
1418     for (int i = 1; i < SIZE; i+=2) {
1419     assertTrue(q.remove(new Integer(i)));
1420     }
1421     for (int i = 0; i < SIZE; i+=2) {
1422     assertTrue(q.remove(new Integer(i)));
1423     assertFalse(q.remove(new Integer(i+1)));
1424     }
1425     assertTrue(q.isEmpty());
1426     }
1427 jsr166 1.4
1428 dl 1.1 /**
1429     * contains(x) reports true when elements added but not yet removed
1430     */
1431     public void testContains() {
1432     LinkedBlockingDeque q = populatedDeque(SIZE);
1433     for (int i = 0; i < SIZE; ++i) {
1434     assertTrue(q.contains(new Integer(i)));
1435     q.poll();
1436     assertFalse(q.contains(new Integer(i)));
1437     }
1438     }
1439    
1440     /**
1441     * clear removes all elements
1442     */
1443     public void testClear() {
1444     LinkedBlockingDeque q = populatedDeque(SIZE);
1445     q.clear();
1446     assertTrue(q.isEmpty());
1447     assertEquals(0, q.size());
1448     assertEquals(SIZE, q.remainingCapacity());
1449     q.add(one);
1450     assertFalse(q.isEmpty());
1451     assertTrue(q.contains(one));
1452     q.clear();
1453     assertTrue(q.isEmpty());
1454     }
1455    
1456     /**
1457     * containsAll(c) is true when c contains a subset of elements
1458     */
1459     public void testContainsAll() {
1460     LinkedBlockingDeque q = populatedDeque(SIZE);
1461     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1462     for (int i = 0; i < SIZE; ++i) {
1463     assertTrue(q.containsAll(p));
1464     assertFalse(p.containsAll(q));
1465     p.add(new Integer(i));
1466     }
1467     assertTrue(p.containsAll(q));
1468     }
1469    
1470     /**
1471     * retainAll(c) retains only those elements of c and reports true if changed
1472     */
1473     public void testRetainAll() {
1474     LinkedBlockingDeque q = populatedDeque(SIZE);
1475     LinkedBlockingDeque p = populatedDeque(SIZE);
1476     for (int i = 0; i < SIZE; ++i) {
1477     boolean changed = q.retainAll(p);
1478     if (i == 0)
1479     assertFalse(changed);
1480     else
1481     assertTrue(changed);
1482    
1483     assertTrue(q.containsAll(p));
1484     assertEquals(SIZE-i, q.size());
1485     p.remove();
1486     }
1487     }
1488    
1489     /**
1490     * removeAll(c) removes only those elements of c and reports true if changed
1491     */
1492     public void testRemoveAll() {
1493     for (int i = 1; i < SIZE; ++i) {
1494     LinkedBlockingDeque q = populatedDeque(SIZE);
1495     LinkedBlockingDeque p = populatedDeque(i);
1496     assertTrue(q.removeAll(p));
1497     assertEquals(SIZE-i, q.size());
1498     for (int j = 0; j < i; ++j) {
1499     Integer I = (Integer)(p.remove());
1500     assertFalse(q.contains(I));
1501     }
1502     }
1503     }
1504    
1505     /**
1506     * toArray contains all elements
1507     */
1508     public void testToArray() {
1509     LinkedBlockingDeque q = populatedDeque(SIZE);
1510     Object[] o = q.toArray();
1511     try {
1512     for(int i = 0; i < o.length; i++)
1513     assertEquals(o[i], q.take());
1514     } catch (InterruptedException e){
1515     unexpectedException();
1516 jsr166 1.4 }
1517 dl 1.1 }
1518    
1519     /**
1520     * toArray(a) contains all elements
1521     */
1522     public void testToArray2() {
1523     LinkedBlockingDeque q = populatedDeque(SIZE);
1524     Integer[] ints = new Integer[SIZE];
1525     ints = (Integer[])q.toArray(ints);
1526     try {
1527     for(int i = 0; i < ints.length; i++)
1528     assertEquals(ints[i], q.take());
1529     } catch (InterruptedException e){
1530     unexpectedException();
1531 jsr166 1.4 }
1532 dl 1.1 }
1533    
1534     /**
1535     * toArray(null) throws NPE
1536     */
1537     public void testToArray_BadArg() {
1538     try {
1539     LinkedBlockingDeque q = populatedDeque(SIZE);
1540     Object o[] = q.toArray(null);
1541     shouldThrow();
1542     } catch(NullPointerException success){}
1543     }
1544    
1545     /**
1546     * toArray with incompatible array type throws CCE
1547     */
1548     public void testToArray1_BadArg() {
1549     try {
1550     LinkedBlockingDeque q = populatedDeque(SIZE);
1551     Object o[] = q.toArray(new String[10] );
1552     shouldThrow();
1553     } catch(ArrayStoreException success){}
1554     }
1555    
1556 jsr166 1.4
1557 dl 1.1 /**
1558     * iterator iterates through all elements
1559     */
1560     public void testIterator() {
1561     LinkedBlockingDeque q = populatedDeque(SIZE);
1562     Iterator it = q.iterator();
1563     try {
1564     while(it.hasNext()){
1565     assertEquals(it.next(), q.take());
1566     }
1567     } catch (InterruptedException e){
1568     unexpectedException();
1569 jsr166 1.4 }
1570 dl 1.1 }
1571    
1572     /**
1573     * iterator.remove removes current element
1574     */
1575     public void testIteratorRemove () {
1576     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1577     q.add(two);
1578     q.add(one);
1579     q.add(three);
1580    
1581     Iterator it = q.iterator();
1582     it.next();
1583     it.remove();
1584 jsr166 1.4
1585 dl 1.1 it = q.iterator();
1586     assertEquals(it.next(), one);
1587     assertEquals(it.next(), three);
1588     assertFalse(it.hasNext());
1589     }
1590    
1591    
1592     /**
1593     * iterator ordering is FIFO
1594     */
1595     public void testIteratorOrdering() {
1596     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1597     q.add(one);
1598     q.add(two);
1599     q.add(three);
1600     assertEquals(0, q.remainingCapacity());
1601     int k = 0;
1602     for (Iterator it = q.iterator(); it.hasNext();) {
1603     int i = ((Integer)(it.next())).intValue();
1604     assertEquals(++k, i);
1605     }
1606     assertEquals(3, k);
1607     }
1608    
1609     /**
1610     * Modifications do not cause iterators to fail
1611     */
1612     public void testWeaklyConsistentIteration () {
1613     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1614     q.add(one);
1615     q.add(two);
1616     q.add(three);
1617     try {
1618     for (Iterator it = q.iterator(); it.hasNext();) {
1619     q.remove();
1620     it.next();
1621     }
1622     }
1623     catch (ConcurrentModificationException e) {
1624     unexpectedException();
1625     }
1626     assertEquals(0, q.size());
1627     }
1628    
1629    
1630     /**
1631 dl 1.2 * Descending iterator iterates through all elements
1632     */
1633     public void testDescendingIterator() {
1634     LinkedBlockingDeque q = populatedDeque(SIZE);
1635     int i = 0;
1636     Iterator it = q.descendingIterator();
1637     while(it.hasNext()) {
1638     assertTrue(q.contains(it.next()));
1639     ++i;
1640     }
1641     assertEquals(i, SIZE);
1642     assertFalse(it.hasNext());
1643     try {
1644     it.next();
1645     } catch(NoSuchElementException success) {
1646     }
1647     }
1648    
1649     /**
1650     * Descending iterator ordering is reverse FIFO
1651     */
1652     public void testDescendingIteratorOrdering() {
1653     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1654 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1655     q.add(new Integer(3));
1656     q.add(new Integer(2));
1657     q.add(new Integer(1));
1658     int k = 0;
1659     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1660     int i = ((Integer)(it.next())).intValue();
1661     assertEquals(++k, i);
1662     }
1663 jsr166 1.4
1664 dl 1.3 assertEquals(3, k);
1665     q.remove();
1666     q.remove();
1667     q.remove();
1668 dl 1.2 }
1669     }
1670    
1671     /**
1672     * descendingIterator.remove removes current element
1673     */
1674     public void testDescendingIteratorRemove () {
1675     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1676 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1677     q.add(new Integer(3));
1678     q.add(new Integer(2));
1679     q.add(new Integer(1));
1680     Iterator it = q.descendingIterator();
1681     assertEquals(it.next(), new Integer(1));
1682     it.remove();
1683     assertEquals(it.next(), new Integer(2));
1684     it = q.descendingIterator();
1685     assertEquals(it.next(), new Integer(2));
1686     assertEquals(it.next(), new Integer(3));
1687     it.remove();
1688     assertFalse(it.hasNext());
1689     q.remove();
1690     }
1691 dl 1.2 }
1692    
1693    
1694     /**
1695 dl 1.1 * toString contains toStrings of elements
1696     */
1697     public void testToString() {
1698     LinkedBlockingDeque q = populatedDeque(SIZE);
1699     String s = q.toString();
1700     for (int i = 0; i < SIZE; ++i) {
1701     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1702     }
1703 jsr166 1.4 }
1704 dl 1.1
1705    
1706     /**
1707     * offer transfers elements across Executor tasks
1708     */
1709     public void testOfferInExecutor() {
1710     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1711     q.add(one);
1712     q.add(two);
1713     ExecutorService executor = Executors.newFixedThreadPool(2);
1714     executor.execute(new Runnable() {
1715     public void run() {
1716     threadAssertFalse(q.offer(three));
1717     try {
1718     threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1719     threadAssertEquals(0, q.remainingCapacity());
1720     }
1721     catch (InterruptedException e) {
1722     threadUnexpectedException();
1723     }
1724     }
1725     });
1726    
1727     executor.execute(new Runnable() {
1728     public void run() {
1729     try {
1730     Thread.sleep(SMALL_DELAY_MS);
1731     threadAssertEquals(one, q.take());
1732     }
1733     catch (InterruptedException e) {
1734     threadUnexpectedException();
1735     }
1736     }
1737     });
1738 jsr166 1.4
1739 dl 1.1 joinPool(executor);
1740     }
1741    
1742     /**
1743     * poll retrieves elements across Executor threads
1744     */
1745     public void testPollInExecutor() {
1746     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1747     ExecutorService executor = Executors.newFixedThreadPool(2);
1748     executor.execute(new Runnable() {
1749     public void run() {
1750     threadAssertNull(q.poll());
1751     try {
1752     threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1753     threadAssertTrue(q.isEmpty());
1754     }
1755     catch (InterruptedException e) {
1756     threadUnexpectedException();
1757     }
1758     }
1759     });
1760    
1761     executor.execute(new Runnable() {
1762     public void run() {
1763     try {
1764     Thread.sleep(SMALL_DELAY_MS);
1765     q.put(one);
1766     }
1767     catch (InterruptedException e) {
1768     threadUnexpectedException();
1769     }
1770     }
1771     });
1772 jsr166 1.4
1773 dl 1.1 joinPool(executor);
1774     }
1775    
1776     /**
1777     * A deserialized serialized deque has same elements in same order
1778     */
1779     public void testSerialization() {
1780     LinkedBlockingDeque q = populatedDeque(SIZE);
1781    
1782     try {
1783     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1784     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1785     out.writeObject(q);
1786     out.close();
1787    
1788     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1789     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1790     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1791     assertEquals(q.size(), r.size());
1792 jsr166 1.4 while (!q.isEmpty())
1793 dl 1.1 assertEquals(q.remove(), r.remove());
1794     } catch(Exception e){
1795     unexpectedException();
1796     }
1797     }
1798    
1799     /**
1800     * drainTo(null) throws NPE
1801 jsr166 1.4 */
1802 dl 1.1 public void testDrainToNull() {
1803     LinkedBlockingDeque q = populatedDeque(SIZE);
1804     try {
1805     q.drainTo(null);
1806     shouldThrow();
1807     } catch(NullPointerException success) {
1808     }
1809     }
1810    
1811     /**
1812     * drainTo(this) throws IAE
1813 jsr166 1.4 */
1814 dl 1.1 public void testDrainToSelf() {
1815     LinkedBlockingDeque q = populatedDeque(SIZE);
1816     try {
1817     q.drainTo(q);
1818     shouldThrow();
1819     } catch(IllegalArgumentException success) {
1820     }
1821     }
1822    
1823     /**
1824     * drainTo(c) empties deque into another collection c
1825 jsr166 1.4 */
1826 dl 1.1 public void testDrainTo() {
1827     LinkedBlockingDeque q = populatedDeque(SIZE);
1828     ArrayList l = new ArrayList();
1829     q.drainTo(l);
1830     assertEquals(q.size(), 0);
1831     assertEquals(l.size(), SIZE);
1832 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1833 dl 1.1 assertEquals(l.get(i), new Integer(i));
1834     q.add(zero);
1835     q.add(one);
1836     assertFalse(q.isEmpty());
1837     assertTrue(q.contains(zero));
1838     assertTrue(q.contains(one));
1839     l.clear();
1840     q.drainTo(l);
1841     assertEquals(q.size(), 0);
1842     assertEquals(l.size(), 2);
1843 jsr166 1.4 for (int i = 0; i < 2; ++i)
1844 dl 1.1 assertEquals(l.get(i), new Integer(i));
1845     }
1846    
1847     /**
1848     * drainTo empties full deque, unblocking a waiting put.
1849 jsr166 1.4 */
1850 dl 1.1 public void testDrainToWithActivePut() {
1851     final LinkedBlockingDeque q = populatedDeque(SIZE);
1852     Thread t = new Thread(new Runnable() {
1853     public void run() {
1854     try {
1855     q.put(new Integer(SIZE+1));
1856 jsr166 1.4 } catch (InterruptedException ie){
1857 dl 1.1 threadUnexpectedException();
1858     }
1859     }
1860     });
1861     try {
1862     t.start();
1863     ArrayList l = new ArrayList();
1864     q.drainTo(l);
1865     assertTrue(l.size() >= SIZE);
1866 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1867 dl 1.1 assertEquals(l.get(i), new Integer(i));
1868     t.join();
1869     assertTrue(q.size() + l.size() >= SIZE);
1870     } catch(Exception e){
1871     unexpectedException();
1872     }
1873     }
1874    
1875     /**
1876     * drainTo(null, n) throws NPE
1877 jsr166 1.4 */
1878 dl 1.1 public void testDrainToNullN() {
1879     LinkedBlockingDeque q = populatedDeque(SIZE);
1880     try {
1881     q.drainTo(null, 0);
1882     shouldThrow();
1883     } catch(NullPointerException success) {
1884     }
1885     }
1886    
1887     /**
1888     * drainTo(this, n) throws IAE
1889 jsr166 1.4 */
1890 dl 1.1 public void testDrainToSelfN() {
1891     LinkedBlockingDeque q = populatedDeque(SIZE);
1892     try {
1893     q.drainTo(q, 0);
1894     shouldThrow();
1895     } catch(IllegalArgumentException success) {
1896     }
1897     }
1898    
1899     /**
1900     * drainTo(c, n) empties first max {n, size} elements of deque into c
1901 jsr166 1.4 */
1902 dl 1.1 public void testDrainToN() {
1903     LinkedBlockingDeque q = new LinkedBlockingDeque();
1904     for (int i = 0; i < SIZE + 2; ++i) {
1905     for(int j = 0; j < SIZE; j++)
1906     assertTrue(q.offer(new Integer(j)));
1907     ArrayList l = new ArrayList();
1908     q.drainTo(l, i);
1909     int k = (i < SIZE)? i : SIZE;
1910     assertEquals(l.size(), k);
1911     assertEquals(q.size(), SIZE-k);
1912 jsr166 1.4 for (int j = 0; j < k; ++j)
1913 dl 1.1 assertEquals(l.get(j), new Integer(j));
1914     while (q.poll() != null) ;
1915     }
1916     }
1917    
1918     }