ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.8
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +39 -38 lines
Log Message:
import static TimeUnit.MILLISECONDS

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