ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.16
Committed: Sat Nov 21 22:00:46 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +22 -12 lines
Log Message:
reduce scope of check for IE in testTimedOffer*

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.9 } catch (NullPointerException success) {}
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 jsr166 1.7 assertNull(q.pollFirst());
104 dl 1.1 }
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 jsr166 1.7 assertNull(q.pollLast());
115 dl 1.1 }
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 jsr166 1.7 assertNull(q.peekFirst());
129 dl 1.1 }
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 jsr166 1.7 assertNull(q.peek());
143 dl 1.1 }
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 jsr166 1.7 assertNull(q.peekLast());
157 dl 1.1 }
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 jsr166 1.9 } catch (NoSuchElementException success) {}
172 jsr166 1.15 assertNull(q.peekFirst());
173 dl 1.1 }
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 jsr166 1.9 } catch (NoSuchElementException success) {}
188 jsr166 1.7 assertNull(q.peekLast());
189 dl 1.1 }
190    
191     /**
192     * removeFirst removes next element, or throws NSEE if empty
193     */
194     public void testRemoveFirst() {
195     LinkedBlockingDeque q = populatedDeque(SIZE);
196     for (int i = 0; i < SIZE; ++i) {
197     assertEquals(i, ((Integer)q.removeFirst()).intValue());
198     }
199     try {
200     q.removeFirst();
201     shouldThrow();
202 jsr166 1.9 } catch (NoSuchElementException success) {}
203 jsr166 1.15 assertNull(q.peekFirst());
204     }
205    
206     /**
207     * removeLast removes last element, or throws NSEE if empty
208     */
209     public void testRemoveLast() {
210     LinkedBlockingDeque q = populatedDeque(SIZE);
211     for (int i = SIZE - 1; i >= 0; --i) {
212     assertEquals(i, ((Integer)q.removeLast()).intValue());
213     }
214     try {
215     q.removeLast();
216     shouldThrow();
217     } catch (NoSuchElementException success) {}
218     assertNull(q.peekLast());
219 dl 1.1 }
220    
221     /**
222     * remove removes next element, or throws NSEE if empty
223     */
224     public void testRemove() {
225     LinkedBlockingDeque q = populatedDeque(SIZE);
226     for (int i = 0; i < SIZE; ++i) {
227     assertEquals(i, ((Integer)q.remove()).intValue());
228     }
229     try {
230     q.remove();
231     shouldThrow();
232 jsr166 1.9 } catch (NoSuchElementException success) {}
233 dl 1.1 }
234    
235     /**
236     * removeFirstOccurrence(x) removes x and returns true if present
237     */
238     public void testRemoveFirstOccurrence() {
239     LinkedBlockingDeque q = populatedDeque(SIZE);
240     for (int i = 1; i < SIZE; i+=2) {
241     assertTrue(q.removeFirstOccurrence(new Integer(i)));
242     }
243     for (int i = 0; i < SIZE; i+=2) {
244     assertTrue(q.removeFirstOccurrence(new Integer(i)));
245     assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
246     }
247     assertTrue(q.isEmpty());
248     }
249    
250     /**
251     * removeLastOccurrence(x) removes x and returns true if present
252     */
253     public void testRemoveLastOccurrence() {
254     LinkedBlockingDeque q = populatedDeque(SIZE);
255     for (int i = 1; i < SIZE; i+=2) {
256     assertTrue(q.removeLastOccurrence(new Integer(i)));
257     }
258     for (int i = 0; i < SIZE; i+=2) {
259     assertTrue(q.removeLastOccurrence(new Integer(i)));
260     assertFalse(q.removeLastOccurrence(new Integer(i+1)));
261     }
262     assertTrue(q.isEmpty());
263     }
264    
265     /**
266     * peekFirst returns element inserted with addFirst
267     */
268     public void testAddFirst() {
269     LinkedBlockingDeque q = populatedDeque(3);
270     q.pollLast();
271 jsr166 1.7 q.addFirst(four);
272     assertEquals(four,q.peekFirst());
273 jsr166 1.4 }
274 dl 1.1
275     /**
276     * peekLast returns element inserted with addLast
277     */
278     public void testAddLast() {
279     LinkedBlockingDeque q = populatedDeque(3);
280     q.pollLast();
281 jsr166 1.7 q.addLast(four);
282     assertEquals(four,q.peekLast());
283 jsr166 1.4 }
284 dl 1.1
285    
286     /**
287     * A new deque has the indicated capacity, or Integer.MAX_VALUE if
288     * none given
289     */
290     public void testConstructor1() {
291     assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
292     assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
293     }
294    
295     /**
296 jsr166 1.11 * Constructor throws IAE if capacity argument nonpositive
297 dl 1.1 */
298     public void testConstructor2() {
299     try {
300     LinkedBlockingDeque q = new LinkedBlockingDeque(0);
301     shouldThrow();
302 jsr166 1.9 } catch (IllegalArgumentException success) {}
303 dl 1.1 }
304    
305     /**
306     * Initializing from null Collection throws NPE
307     */
308     public void testConstructor3() {
309     try {
310     LinkedBlockingDeque q = new LinkedBlockingDeque(null);
311     shouldThrow();
312 jsr166 1.9 } catch (NullPointerException success) {}
313 dl 1.1 }
314    
315     /**
316     * Initializing from Collection of null elements throws NPE
317     */
318     public void testConstructor4() {
319     try {
320     Integer[] ints = new Integer[SIZE];
321     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
322     shouldThrow();
323 jsr166 1.9 } catch (NullPointerException success) {}
324 dl 1.1 }
325    
326     /**
327     * Initializing from Collection with some null elements throws NPE
328     */
329     public void testConstructor5() {
330     try {
331     Integer[] ints = new Integer[SIZE];
332     for (int i = 0; i < SIZE-1; ++i)
333     ints[i] = new Integer(i);
334     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
335     shouldThrow();
336 jsr166 1.9 } catch (NullPointerException success) {}
337 dl 1.1 }
338    
339     /**
340     * Deque contains all elements of collection used to initialize
341     */
342     public void testConstructor6() {
343 jsr166 1.12 Integer[] ints = new Integer[SIZE];
344     for (int i = 0; i < SIZE; ++i)
345     ints[i] = new Integer(i);
346     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
347     for (int i = 0; i < SIZE; ++i)
348     assertEquals(ints[i], q.poll());
349 dl 1.1 }
350    
351     /**
352     * Deque transitions from empty to full when elements added
353     */
354     public void testEmptyFull() {
355     LinkedBlockingDeque q = new LinkedBlockingDeque(2);
356     assertTrue(q.isEmpty());
357     assertEquals("should have room for 2", 2, q.remainingCapacity());
358     q.add(one);
359     assertFalse(q.isEmpty());
360     q.add(two);
361     assertFalse(q.isEmpty());
362     assertEquals(0, q.remainingCapacity());
363     assertFalse(q.offer(three));
364     }
365    
366     /**
367     * remainingCapacity decreases on add, increases on remove
368     */
369     public void testRemainingCapacity() {
370     LinkedBlockingDeque q = populatedDeque(SIZE);
371     for (int i = 0; i < SIZE; ++i) {
372     assertEquals(i, q.remainingCapacity());
373     assertEquals(SIZE-i, q.size());
374     q.remove();
375     }
376     for (int i = 0; i < SIZE; ++i) {
377     assertEquals(SIZE-i, q.remainingCapacity());
378     assertEquals(i, q.size());
379     q.add(new Integer(i));
380     }
381     }
382    
383     /**
384     * offer(null) throws NPE
385     */
386     public void testOfferNull() {
387 jsr166 1.7 try {
388 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
389     q.offer(null);
390     shouldThrow();
391 jsr166 1.9 } catch (NullPointerException success) {}
392 dl 1.1 }
393    
394     /**
395     * add(null) throws NPE
396     */
397     public void testAddNull() {
398 jsr166 1.7 try {
399 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
400     q.add(null);
401     shouldThrow();
402 jsr166 1.9 } catch (NullPointerException success) {}
403 dl 1.1 }
404    
405     /**
406     * push(null) throws NPE
407     */
408     public void testPushNull() {
409 jsr166 1.7 try {
410 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
411     q.push(null);
412     shouldThrow();
413 jsr166 1.9 } catch (NullPointerException success) {}
414 dl 1.1 }
415    
416     /**
417     * push succeeds if not full; throws ISE if full
418     */
419     public void testPush() {
420 jsr166 1.7 try {
421 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
422     for (int i = 0; i < SIZE; ++i) {
423     Integer I = new Integer(i);
424     q.push(I);
425     assertEquals(I, q.peek());
426     }
427     assertEquals(0, q.remainingCapacity());
428     q.push(new Integer(SIZE));
429 jsr166 1.9 shouldThrow();
430     } catch (IllegalStateException success) {}
431 dl 1.1 }
432    
433     /**
434     * peekFirst returns element inserted with push
435     */
436     public void testPushWithPeek() {
437     LinkedBlockingDeque q = populatedDeque(3);
438     q.pollLast();
439 jsr166 1.7 q.push(four);
440     assertEquals(four,q.peekFirst());
441 jsr166 1.4 }
442 dl 1.1
443    
444     /**
445     * pop removes next element, or throws NSEE if empty
446     */
447     public void testPop() {
448     LinkedBlockingDeque q = populatedDeque(SIZE);
449     for (int i = 0; i < SIZE; ++i) {
450     assertEquals(i, ((Integer)q.pop()).intValue());
451     }
452     try {
453     q.pop();
454     shouldThrow();
455 jsr166 1.9 } catch (NoSuchElementException success) {}
456 dl 1.1 }
457    
458    
459     /**
460     * Offer succeeds if not full; fails if full
461     */
462     public void testOffer() {
463     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
464     assertTrue(q.offer(zero));
465     assertFalse(q.offer(one));
466     }
467    
468     /**
469     * add succeeds if not full; throws ISE if full
470     */
471     public void testAdd() {
472 jsr166 1.7 try {
473 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
474     for (int i = 0; i < SIZE; ++i) {
475     assertTrue(q.add(new Integer(i)));
476     }
477     assertEquals(0, q.remainingCapacity());
478     q.add(new Integer(SIZE));
479 jsr166 1.9 shouldThrow();
480     } catch (IllegalStateException success) {}
481 dl 1.1 }
482    
483     /**
484     * addAll(null) throws NPE
485     */
486     public void testAddAll1() {
487     try {
488     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489     q.addAll(null);
490     shouldThrow();
491 jsr166 1.9 } catch (NullPointerException success) {}
492 dl 1.1 }
493    
494     /**
495     * addAll(this) throws IAE
496     */
497     public void testAddAllSelf() {
498     try {
499     LinkedBlockingDeque q = populatedDeque(SIZE);
500     q.addAll(q);
501     shouldThrow();
502 jsr166 1.9 } catch (IllegalArgumentException success) {}
503 dl 1.1 }
504    
505     /**
506     * addAll of a collection with null elements throws NPE
507     */
508     public void testAddAll2() {
509     try {
510     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
511     Integer[] ints = new Integer[SIZE];
512     q.addAll(Arrays.asList(ints));
513     shouldThrow();
514 jsr166 1.9 } catch (NullPointerException success) {}
515 dl 1.1 }
516     /**
517     * addAll of a collection with any null elements throws NPE after
518     * possibly adding some elements
519     */
520     public void testAddAll3() {
521     try {
522     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
523     Integer[] ints = new Integer[SIZE];
524     for (int i = 0; i < SIZE-1; ++i)
525     ints[i] = new Integer(i);
526     q.addAll(Arrays.asList(ints));
527     shouldThrow();
528 jsr166 1.9 } catch (NullPointerException success) {}
529 dl 1.1 }
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 jsr166 1.9 } catch (IllegalStateException success) {}
542 dl 1.1 }
543 jsr166 1.16
544 dl 1.1 /**
545     * Deque contains all elements, in traversal order, of successful addAll
546     */
547     public void testAddAll5() {
548 jsr166 1.12 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 dl 1.1 }
558    
559    
560     /**
561     * put(null) throws NPE
562     */
563 jsr166 1.10 public void testPutNull() throws InterruptedException {
564 jsr166 1.7 try {
565 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
566     q.put(null);
567     shouldThrow();
568 jsr166 1.9 } catch (NullPointerException success) {}
569 jsr166 1.10 }
570 dl 1.1
571     /**
572     * all elements successfully put are contained
573     */
574 jsr166 1.10 public void testPut() throws InterruptedException {
575     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
576     for (int i = 0; i < SIZE; ++i) {
577     Integer I = new Integer(i);
578     q.put(I);
579     assertTrue(q.contains(I));
580     }
581     assertEquals(0, q.remainingCapacity());
582 dl 1.1 }
583    
584     /**
585     * put blocks interruptibly if full
586     */
587 jsr166 1.9 public void testBlockingPut() throws InterruptedException {
588     Thread t = new Thread(new CheckedRunnable() {
589     public void realRun() {
590     int added = 0;
591     try {
592     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
593     for (int i = 0; i < SIZE; ++i) {
594     q.put(new Integer(i));
595     ++added;
596 jsr166 1.4 }
597 jsr166 1.9 q.put(new Integer(SIZE));
598     threadShouldThrow();
599     } catch (InterruptedException success) {
600     threadAssertEquals(added, SIZE);
601     }
602     }});
603    
604 dl 1.1 t.start();
605 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
606     t.interrupt();
607     t.join();
608 dl 1.1 }
609    
610     /**
611     * put blocks waiting for take when full
612     */
613 jsr166 1.9 public void testPutWithTake() throws InterruptedException {
614 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
615 jsr166 1.10 Thread t = new Thread(new CheckedRunnable() {
616     public void realRun() {
617     int added = 0;
618     try {
619     q.put(new Object());
620     ++added;
621     q.put(new Object());
622     ++added;
623     q.put(new Object());
624     ++added;
625     q.put(new Object());
626     ++added;
627     threadShouldThrow();
628     } catch (InterruptedException success) {
629     threadAssertTrue(added >= 2);
630 dl 1.1 }
631 jsr166 1.10 }});
632 jsr166 1.9
633     t.start();
634     Thread.sleep(SHORT_DELAY_MS);
635     q.take();
636     t.interrupt();
637     t.join();
638 dl 1.1 }
639    
640     /**
641     * timed offer times out if full and elements not taken
642     */
643 jsr166 1.9 public void testTimedOffer() throws InterruptedException {
644 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
645 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
646 jsr166 1.9 public void realRun() throws InterruptedException {
647     q.put(new Object());
648     q.put(new Object());
649 jsr166 1.16 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
650     try {
651     q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
652     shouldThrow();
653     } catch (InterruptedException success) {}
654     }});
655 jsr166 1.4
656 jsr166 1.9 t.start();
657     Thread.sleep(SMALL_DELAY_MS);
658     t.interrupt();
659     t.join();
660 dl 1.1 }
661    
662     /**
663     * take retrieves elements in FIFO order
664     */
665 jsr166 1.9 public void testTake() throws InterruptedException {
666     LinkedBlockingDeque q = populatedDeque(SIZE);
667     for (int i = 0; i < SIZE; ++i) {
668     assertEquals(i, ((Integer)q.take()).intValue());
669 jsr166 1.7 }
670 dl 1.1 }
671    
672     /**
673     * take blocks interruptibly when empty
674     */
675 jsr166 1.9 public void testTakeFromEmpty() throws InterruptedException {
676 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
677 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
678     public void realRun() throws InterruptedException {
679     q.take();
680     }};
681    
682     t.start();
683     Thread.sleep(SHORT_DELAY_MS);
684     t.interrupt();
685     t.join();
686 dl 1.1 }
687    
688     /**
689     * Take removes existing elements until empty, then blocks interruptibly
690     */
691 jsr166 1.9 public void testBlockingTake() throws InterruptedException {
692     Thread t = new ThreadShouldThrow(InterruptedException.class) {
693     public void realRun() throws InterruptedException {
694     LinkedBlockingDeque q = populatedDeque(SIZE);
695     for (int i = 0; i < SIZE; ++i) {
696     assertEquals(i, ((Integer)q.take()).intValue());
697     }
698     q.take();
699     }};
700    
701 dl 1.1 t.start();
702 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
703     t.interrupt();
704     t.join();
705 dl 1.1 }
706    
707    
708     /**
709     * poll succeeds unless empty
710     */
711     public void testPoll() {
712     LinkedBlockingDeque q = populatedDeque(SIZE);
713     for (int i = 0; i < SIZE; ++i) {
714     assertEquals(i, ((Integer)q.poll()).intValue());
715     }
716 jsr166 1.7 assertNull(q.poll());
717 dl 1.1 }
718    
719     /**
720     * timed poll with zero timeout succeeds when non-empty, else times out
721     */
722 jsr166 1.9 public void testTimedPoll0() throws InterruptedException {
723     LinkedBlockingDeque q = populatedDeque(SIZE);
724     for (int i = 0; i < SIZE; ++i) {
725     assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
726 jsr166 1.7 }
727 jsr166 1.9 assertNull(q.poll(0, MILLISECONDS));
728 dl 1.1 }
729    
730     /**
731     * timed poll with nonzero timeout succeeds when non-empty, else times out
732     */
733 jsr166 1.9 public void testTimedPoll() throws InterruptedException {
734     LinkedBlockingDeque q = populatedDeque(SIZE);
735     for (int i = 0; i < SIZE; ++i) {
736     assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
737 jsr166 1.7 }
738 jsr166 1.9 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
739 dl 1.1 }
740    
741     /**
742     * Interrupted timed poll throws InterruptedException instead of
743     * returning timeout status
744     */
745 jsr166 1.9 public void testInterruptedTimedPoll() throws InterruptedException {
746 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
747 jsr166 1.9 public void realRun() throws InterruptedException {
748     LinkedBlockingDeque q = populatedDeque(SIZE);
749     for (int i = 0; i < SIZE; ++i) {
750 jsr166 1.13 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
751 jsr166 1.9 }
752 jsr166 1.13 try {
753     q.poll(SMALL_DELAY_MS, MILLISECONDS);
754     shouldThrow();
755     } catch (InterruptedException success) {}
756     }});
757 jsr166 1.9
758 dl 1.1 t.start();
759 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
760     t.interrupt();
761     t.join();
762 dl 1.1 }
763    
764     /**
765     * timed poll before a delayed offer fails; after offer succeeds;
766     * on interruption throws
767     */
768 jsr166 1.9 public void testTimedPollWithOffer() throws InterruptedException {
769 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
770 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
771 jsr166 1.9 public void realRun() throws InterruptedException {
772 jsr166 1.14 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
773     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
774     try {
775     q.poll(LONG_DELAY_MS, MILLISECONDS);
776     shouldThrow();
777     } catch (InterruptedException success) {}
778     }});
779 jsr166 1.9
780     t.start();
781     Thread.sleep(SMALL_DELAY_MS);
782     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
783     t.interrupt();
784     t.join();
785 jsr166 1.4 }
786 dl 1.1
787    
788     /**
789     * putFirst(null) throws NPE
790     */
791 jsr166 1.9 public void testPutFirstNull() throws InterruptedException {
792 jsr166 1.7 try {
793 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794     q.putFirst(null);
795     shouldThrow();
796 jsr166 1.9 } catch (NullPointerException success) {}
797 dl 1.1 }
798    
799     /**
800     * all elements successfully putFirst are contained
801     */
802 jsr166 1.9 public void testPutFirst() throws InterruptedException {
803     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
804     for (int i = 0; i < SIZE; ++i) {
805     Integer I = new Integer(i);
806     q.putFirst(I);
807     assertTrue(q.contains(I));
808 dl 1.1 }
809 jsr166 1.9 assertEquals(0, q.remainingCapacity());
810 dl 1.1 }
811    
812     /**
813     * putFirst blocks interruptibly if full
814     */
815 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
816 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
817     public void realRun() {
818     int added = 0;
819     try {
820     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
821     for (int i = 0; i < SIZE; ++i) {
822     q.putFirst(new Integer(i));
823     ++added;
824 jsr166 1.4 }
825 jsr166 1.11 q.putFirst(new Integer(SIZE));
826     threadShouldThrow();
827     } catch (InterruptedException success) {
828     threadAssertEquals(added, SIZE);
829     }
830     }});
831 jsr166 1.9
832 dl 1.1 t.start();
833 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
834     t.interrupt();
835     t.join();
836 dl 1.1 }
837    
838     /**
839     * putFirst blocks waiting for take when full
840     */
841 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
842 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
843 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
844     public void realRun() {
845     int added = 0;
846     try {
847     q.putFirst(new Object());
848     ++added;
849     q.putFirst(new Object());
850     ++added;
851     q.putFirst(new Object());
852     ++added;
853     q.putFirst(new Object());
854     ++added;
855     threadShouldThrow();
856     } catch (InterruptedException success) {
857     threadAssertTrue(added >= 2);
858 dl 1.1 }
859 jsr166 1.11 }});
860 jsr166 1.9
861     t.start();
862     Thread.sleep(SHORT_DELAY_MS);
863     q.take();
864     t.interrupt();
865     t.join();
866 dl 1.1 }
867    
868     /**
869     * timed offerFirst times out if full and elements not taken
870     */
871 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
872 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
873 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
874 jsr166 1.9 public void realRun() throws InterruptedException {
875     q.putFirst(new Object());
876     q.putFirst(new Object());
877 jsr166 1.16 assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
878     try {
879     q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
880     shouldThrow();
881     } catch (InterruptedException success) {}
882     }});
883 jsr166 1.4
884 jsr166 1.9 t.start();
885     Thread.sleep(SMALL_DELAY_MS);
886     t.interrupt();
887     t.join();
888 dl 1.1 }
889    
890     /**
891     * take retrieves elements in FIFO order
892     */
893 jsr166 1.9 public void testTakeFirst() throws InterruptedException {
894     LinkedBlockingDeque q = populatedDeque(SIZE);
895     for (int i = 0; i < SIZE; ++i) {
896     assertEquals(i, ((Integer)q.takeFirst()).intValue());
897 jsr166 1.7 }
898 dl 1.1 }
899    
900     /**
901     * takeFirst blocks interruptibly when empty
902     */
903 jsr166 1.9 public void testTakeFirstFromEmpty() throws InterruptedException {
904 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
905 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
906     public void realRun() throws InterruptedException {
907     q.takeFirst();
908     }};
909    
910     t.start();
911     Thread.sleep(SHORT_DELAY_MS);
912     t.interrupt();
913     t.join();
914 dl 1.1 }
915    
916     /**
917     * TakeFirst removes existing elements until empty, then blocks interruptibly
918     */
919 jsr166 1.9 public void testBlockingTakeFirst() throws InterruptedException {
920     Thread t = new ThreadShouldThrow(InterruptedException.class) {
921     public void realRun() throws InterruptedException {
922     LinkedBlockingDeque q = populatedDeque(SIZE);
923     for (int i = 0; i < SIZE; ++i) {
924     assertEquals(i, ((Integer)q.takeFirst()).intValue());
925     }
926     q.takeFirst();
927     }};
928    
929 dl 1.1 t.start();
930 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
931     t.interrupt();
932     t.join();
933 dl 1.1 }
934    
935    
936     /**
937     * timed pollFirst with zero timeout succeeds when non-empty, else times out
938     */
939 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
940     LinkedBlockingDeque q = populatedDeque(SIZE);
941     for (int i = 0; i < SIZE; ++i) {
942     assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
943 jsr166 1.7 }
944 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
945 dl 1.1 }
946    
947     /**
948     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
949     */
950 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
951     LinkedBlockingDeque q = populatedDeque(SIZE);
952     for (int i = 0; i < SIZE; ++i) {
953     assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
954 jsr166 1.7 }
955 jsr166 1.9 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
956 dl 1.1 }
957    
958     /**
959     * Interrupted timed pollFirst throws InterruptedException instead of
960     * returning timeout status
961     */
962 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
963 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
964 jsr166 1.9 public void realRun() throws InterruptedException {
965     LinkedBlockingDeque q = populatedDeque(SIZE);
966     for (int i = 0; i < SIZE; ++i) {
967 jsr166 1.13 assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
968 jsr166 1.9 }
969 jsr166 1.13 try {
970     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
971     shouldThrow();
972     } catch (InterruptedException success) {}
973     }});
974 jsr166 1.9
975 dl 1.1 t.start();
976 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
977     t.interrupt();
978     t.join();
979 dl 1.1 }
980    
981     /**
982     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
983     * on interruption throws
984     */
985 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
986 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
987 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
988 jsr166 1.9 public void realRun() throws InterruptedException {
989 jsr166 1.14 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
990     assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
991     try {
992     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
993     shouldThrow();
994     } catch (InterruptedException success) {}
995     }});
996 jsr166 1.9
997     t.start();
998     Thread.sleep(SMALL_DELAY_MS);
999     assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
1000     t.interrupt();
1001     t.join();
1002 jsr166 1.4 }
1003 dl 1.1
1004     /**
1005     * putLast(null) throws NPE
1006     */
1007 jsr166 1.9 public void testPutLastNull() throws InterruptedException {
1008 jsr166 1.7 try {
1009 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1010     q.putLast(null);
1011     shouldThrow();
1012 jsr166 1.9 } catch (NullPointerException success) {}
1013 dl 1.1 }
1014    
1015     /**
1016     * all elements successfully putLast are contained
1017     */
1018 jsr166 1.9 public void testPutLast() throws InterruptedException {
1019     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1020     for (int i = 0; i < SIZE; ++i) {
1021     Integer I = new Integer(i);
1022     q.putLast(I);
1023     assertTrue(q.contains(I));
1024 dl 1.1 }
1025 jsr166 1.9 assertEquals(0, q.remainingCapacity());
1026 dl 1.1 }
1027    
1028     /**
1029     * putLast blocks interruptibly if full
1030     */
1031 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
1032 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1033     public void realRun() {
1034     int added = 0;
1035     try {
1036     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1037     for (int i = 0; i < SIZE; ++i) {
1038     q.putLast(new Integer(i));
1039     ++added;
1040 jsr166 1.4 }
1041 jsr166 1.11 q.putLast(new Integer(SIZE));
1042     threadShouldThrow();
1043     } catch (InterruptedException success) {
1044     threadAssertEquals(added, SIZE);
1045     }
1046     }});
1047    
1048 dl 1.1 t.start();
1049 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1050     t.interrupt();
1051     t.join();
1052 dl 1.1 }
1053    
1054     /**
1055     * putLast blocks waiting for take when full
1056     */
1057 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1058 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1059 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1060     public void realRun() {
1061     int added = 0;
1062     try {
1063     q.putLast(new Object());
1064     ++added;
1065     q.putLast(new Object());
1066     ++added;
1067     q.putLast(new Object());
1068     ++added;
1069     q.putLast(new Object());
1070     ++added;
1071     threadShouldThrow();
1072     } catch (InterruptedException success) {
1073     threadAssertTrue(added >= 2);
1074 dl 1.1 }
1075 jsr166 1.11 }});
1076 jsr166 1.9
1077     t.start();
1078     Thread.sleep(SHORT_DELAY_MS);
1079     q.take();
1080     t.interrupt();
1081     t.join();
1082 dl 1.1 }
1083    
1084     /**
1085     * timed offerLast times out if full and elements not taken
1086     */
1087 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1088 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1089 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
1090 jsr166 1.9 public void realRun() throws InterruptedException {
1091     q.putLast(new Object());
1092     q.putLast(new Object());
1093 jsr166 1.16 assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1094     try {
1095     q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1096     shouldThrow();
1097     } catch (InterruptedException success) {}
1098     }});
1099 jsr166 1.4
1100 jsr166 1.9 t.start();
1101     Thread.sleep(SMALL_DELAY_MS);
1102     t.interrupt();
1103     t.join();
1104 dl 1.1 }
1105    
1106     /**
1107     * takeLast retrieves elements in FIFO order
1108     */
1109 jsr166 1.9 public void testTakeLast() throws InterruptedException {
1110     LinkedBlockingDeque q = populatedDeque(SIZE);
1111     for (int i = 0; i < SIZE; ++i) {
1112     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1113 jsr166 1.7 }
1114 dl 1.1 }
1115    
1116     /**
1117     * takeLast blocks interruptibly when empty
1118     */
1119 jsr166 1.9 public void testTakeLastFromEmpty() throws InterruptedException {
1120 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1121 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1122     public void realRun() throws InterruptedException {
1123     q.takeLast();
1124     }};
1125    
1126     t.start();
1127     Thread.sleep(SHORT_DELAY_MS);
1128     t.interrupt();
1129     t.join();
1130 dl 1.1 }
1131    
1132     /**
1133     * TakeLast removes existing elements until empty, then blocks interruptibly
1134     */
1135 jsr166 1.9 public void testBlockingTakeLast() throws InterruptedException {
1136     Thread t = new ThreadShouldThrow(InterruptedException.class) {
1137     public void realRun() throws InterruptedException {
1138     LinkedBlockingDeque q = populatedDeque(SIZE);
1139     for (int i = 0; i < SIZE; ++i) {
1140     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1141     }
1142     q.takeLast();
1143     }};
1144    
1145 dl 1.1 t.start();
1146 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1147     t.interrupt();
1148     t.join();
1149 dl 1.1 }
1150    
1151    
1152     /**
1153     * timed pollLast with zero timeout succeeds when non-empty, else times out
1154     */
1155 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1156     LinkedBlockingDeque q = populatedDeque(SIZE);
1157     for (int i = 0; i < SIZE; ++i) {
1158     assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1159 jsr166 1.7 }
1160 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1161 dl 1.1 }
1162    
1163     /**
1164     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1165     */
1166 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1167     LinkedBlockingDeque q = populatedDeque(SIZE);
1168     for (int i = 0; i < SIZE; ++i) {
1169     assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1170 jsr166 1.7 }
1171 jsr166 1.9 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1172 dl 1.1 }
1173    
1174     /**
1175     * Interrupted timed pollLast throws InterruptedException instead of
1176     * returning timeout status
1177     */
1178 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1179 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
1180 jsr166 1.9 public void realRun() throws InterruptedException {
1181     LinkedBlockingDeque q = populatedDeque(SIZE);
1182     for (int i = 0; i < SIZE; ++i) {
1183 jsr166 1.13 assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1184 jsr166 1.9 }
1185 jsr166 1.13 try {
1186     q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1187     shouldThrow();
1188     } catch (InterruptedException success) {}
1189     }});
1190 jsr166 1.9
1191 dl 1.1 t.start();
1192 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1193     t.interrupt();
1194     t.join();
1195 dl 1.1 }
1196    
1197     /**
1198     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1199     * on interruption throws
1200     */
1201 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1202 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1203 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1204     public void realRun() throws InterruptedException {
1205     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1206     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1207     try {
1208     q.poll(LONG_DELAY_MS, MILLISECONDS);
1209     shouldThrow();
1210     } catch (InterruptedException success) {}
1211     }});
1212    
1213     t.start();
1214     Thread.sleep(SMALL_DELAY_MS);
1215     assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1216     t.interrupt();
1217     t.join();
1218 jsr166 1.4 }
1219 dl 1.1
1220    
1221     /**
1222     * element returns next element, or throws NSEE if empty
1223     */
1224     public void testElement() {
1225     LinkedBlockingDeque q = populatedDeque(SIZE);
1226     for (int i = 0; i < SIZE; ++i) {
1227     assertEquals(i, ((Integer)q.element()).intValue());
1228     q.poll();
1229     }
1230     try {
1231     q.element();
1232     shouldThrow();
1233 jsr166 1.9 } catch (NoSuchElementException success) {}
1234 dl 1.1 }
1235    
1236     /**
1237     * remove(x) removes x and returns true if present
1238     */
1239     public void testRemoveElement() {
1240     LinkedBlockingDeque q = populatedDeque(SIZE);
1241     for (int i = 1; i < SIZE; i+=2) {
1242     assertTrue(q.remove(new Integer(i)));
1243     }
1244     for (int i = 0; i < SIZE; i+=2) {
1245     assertTrue(q.remove(new Integer(i)));
1246     assertFalse(q.remove(new Integer(i+1)));
1247     }
1248     assertTrue(q.isEmpty());
1249     }
1250 jsr166 1.4
1251 dl 1.1 /**
1252     * contains(x) reports true when elements added but not yet removed
1253     */
1254     public void testContains() {
1255     LinkedBlockingDeque q = populatedDeque(SIZE);
1256     for (int i = 0; i < SIZE; ++i) {
1257     assertTrue(q.contains(new Integer(i)));
1258     q.poll();
1259     assertFalse(q.contains(new Integer(i)));
1260     }
1261     }
1262    
1263     /**
1264     * clear removes all elements
1265     */
1266     public void testClear() {
1267     LinkedBlockingDeque q = populatedDeque(SIZE);
1268     q.clear();
1269     assertTrue(q.isEmpty());
1270     assertEquals(0, q.size());
1271     assertEquals(SIZE, q.remainingCapacity());
1272     q.add(one);
1273     assertFalse(q.isEmpty());
1274     assertTrue(q.contains(one));
1275     q.clear();
1276     assertTrue(q.isEmpty());
1277     }
1278    
1279     /**
1280     * containsAll(c) is true when c contains a subset of elements
1281     */
1282     public void testContainsAll() {
1283     LinkedBlockingDeque q = populatedDeque(SIZE);
1284     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1285     for (int i = 0; i < SIZE; ++i) {
1286     assertTrue(q.containsAll(p));
1287     assertFalse(p.containsAll(q));
1288     p.add(new Integer(i));
1289     }
1290     assertTrue(p.containsAll(q));
1291     }
1292    
1293     /**
1294     * retainAll(c) retains only those elements of c and reports true if changed
1295     */
1296     public void testRetainAll() {
1297     LinkedBlockingDeque q = populatedDeque(SIZE);
1298     LinkedBlockingDeque p = populatedDeque(SIZE);
1299     for (int i = 0; i < SIZE; ++i) {
1300     boolean changed = q.retainAll(p);
1301     if (i == 0)
1302     assertFalse(changed);
1303     else
1304     assertTrue(changed);
1305    
1306     assertTrue(q.containsAll(p));
1307     assertEquals(SIZE-i, q.size());
1308     p.remove();
1309     }
1310     }
1311    
1312     /**
1313     * removeAll(c) removes only those elements of c and reports true if changed
1314     */
1315     public void testRemoveAll() {
1316     for (int i = 1; i < SIZE; ++i) {
1317     LinkedBlockingDeque q = populatedDeque(SIZE);
1318     LinkedBlockingDeque p = populatedDeque(i);
1319     assertTrue(q.removeAll(p));
1320     assertEquals(SIZE-i, q.size());
1321     for (int j = 0; j < i; ++j) {
1322     Integer I = (Integer)(p.remove());
1323     assertFalse(q.contains(I));
1324     }
1325     }
1326     }
1327    
1328     /**
1329     * toArray contains all elements
1330     */
1331 jsr166 1.9 public void testToArray() throws InterruptedException{
1332 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1333 jsr166 1.7 Object[] o = q.toArray();
1334     for (int i = 0; i < o.length; i++)
1335     assertEquals(o[i], q.take());
1336 dl 1.1 }
1337    
1338     /**
1339     * toArray(a) contains all elements
1340     */
1341 jsr166 1.9 public void testToArray2() throws InterruptedException {
1342 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1343 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1344     ints = (Integer[])q.toArray(ints);
1345 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1346     assertEquals(ints[i], q.take());
1347 dl 1.1 }
1348    
1349     /**
1350     * toArray(null) throws NPE
1351     */
1352     public void testToArray_BadArg() {
1353 jsr166 1.7 try {
1354 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1355 jsr166 1.7 Object o[] = q.toArray(null);
1356     shouldThrow();
1357     } catch (NullPointerException success) {}
1358 dl 1.1 }
1359    
1360     /**
1361     * toArray with incompatible array type throws CCE
1362     */
1363     public void testToArray1_BadArg() {
1364 jsr166 1.7 try {
1365 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1366 jsr166 1.7 Object o[] = q.toArray(new String[10] );
1367     shouldThrow();
1368 jsr166 1.9 } catch (ArrayStoreException success) {}
1369 dl 1.1 }
1370    
1371 jsr166 1.4
1372 dl 1.1 /**
1373     * iterator iterates through all elements
1374     */
1375 jsr166 1.9 public void testIterator() throws InterruptedException {
1376 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1377 jsr166 1.7 Iterator it = q.iterator();
1378 jsr166 1.9 while (it.hasNext()) {
1379     assertEquals(it.next(), q.take());
1380 jsr166 1.7 }
1381 dl 1.1 }
1382    
1383     /**
1384     * iterator.remove removes current element
1385     */
1386     public void testIteratorRemove () {
1387     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1388     q.add(two);
1389     q.add(one);
1390     q.add(three);
1391    
1392     Iterator it = q.iterator();
1393     it.next();
1394     it.remove();
1395 jsr166 1.4
1396 dl 1.1 it = q.iterator();
1397     assertEquals(it.next(), one);
1398     assertEquals(it.next(), three);
1399     assertFalse(it.hasNext());
1400     }
1401    
1402    
1403     /**
1404     * iterator ordering is FIFO
1405     */
1406     public void testIteratorOrdering() {
1407     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1408     q.add(one);
1409     q.add(two);
1410     q.add(three);
1411     assertEquals(0, q.remainingCapacity());
1412     int k = 0;
1413     for (Iterator it = q.iterator(); it.hasNext();) {
1414     int i = ((Integer)(it.next())).intValue();
1415     assertEquals(++k, i);
1416     }
1417     assertEquals(3, k);
1418     }
1419    
1420     /**
1421     * Modifications do not cause iterators to fail
1422     */
1423     public void testWeaklyConsistentIteration () {
1424     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1425     q.add(one);
1426     q.add(two);
1427     q.add(three);
1428 jsr166 1.9 for (Iterator it = q.iterator(); it.hasNext();) {
1429     q.remove();
1430     it.next();
1431 dl 1.1 }
1432     assertEquals(0, q.size());
1433     }
1434    
1435    
1436     /**
1437 dl 1.2 * Descending iterator iterates through all elements
1438     */
1439     public void testDescendingIterator() {
1440     LinkedBlockingDeque q = populatedDeque(SIZE);
1441     int i = 0;
1442 jsr166 1.7 Iterator it = q.descendingIterator();
1443 jsr166 1.5 while (it.hasNext()) {
1444 dl 1.2 assertTrue(q.contains(it.next()));
1445     ++i;
1446     }
1447     assertEquals(i, SIZE);
1448     assertFalse(it.hasNext());
1449     try {
1450     it.next();
1451 jsr166 1.9 shouldThrow();
1452     } catch (NoSuchElementException success) {}
1453 dl 1.2 }
1454    
1455     /**
1456     * Descending iterator ordering is reverse FIFO
1457     */
1458     public void testDescendingIteratorOrdering() {
1459     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1460 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1461     q.add(new Integer(3));
1462     q.add(new Integer(2));
1463     q.add(new Integer(1));
1464     int k = 0;
1465     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1466     int i = ((Integer)(it.next())).intValue();
1467     assertEquals(++k, i);
1468     }
1469 jsr166 1.4
1470 dl 1.3 assertEquals(3, k);
1471     q.remove();
1472     q.remove();
1473     q.remove();
1474 dl 1.2 }
1475     }
1476    
1477     /**
1478     * descendingIterator.remove removes current element
1479     */
1480     public void testDescendingIteratorRemove () {
1481     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1482 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1483     q.add(new Integer(3));
1484     q.add(new Integer(2));
1485     q.add(new Integer(1));
1486     Iterator it = q.descendingIterator();
1487     assertEquals(it.next(), new Integer(1));
1488     it.remove();
1489     assertEquals(it.next(), new Integer(2));
1490     it = q.descendingIterator();
1491     assertEquals(it.next(), new Integer(2));
1492     assertEquals(it.next(), new Integer(3));
1493     it.remove();
1494     assertFalse(it.hasNext());
1495     q.remove();
1496     }
1497 dl 1.2 }
1498    
1499    
1500     /**
1501 dl 1.1 * toString contains toStrings of elements
1502     */
1503     public void testToString() {
1504     LinkedBlockingDeque q = populatedDeque(SIZE);
1505     String s = q.toString();
1506     for (int i = 0; i < SIZE; ++i) {
1507     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1508     }
1509 jsr166 1.4 }
1510 dl 1.1
1511    
1512     /**
1513     * offer transfers elements across Executor tasks
1514     */
1515     public void testOfferInExecutor() {
1516     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1517     q.add(one);
1518     q.add(two);
1519     ExecutorService executor = Executors.newFixedThreadPool(2);
1520 jsr166 1.9 executor.execute(new CheckedRunnable() {
1521     public void realRun() throws InterruptedException {
1522 dl 1.1 threadAssertFalse(q.offer(three));
1523 jsr166 1.9 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1524     threadAssertEquals(0, q.remainingCapacity());
1525     }});
1526    
1527     executor.execute(new CheckedRunnable() {
1528     public void realRun() throws InterruptedException {
1529     Thread.sleep(SMALL_DELAY_MS);
1530     threadAssertEquals(one, q.take());
1531     }});
1532 jsr166 1.4
1533 dl 1.1 joinPool(executor);
1534     }
1535    
1536     /**
1537     * poll retrieves elements across Executor threads
1538     */
1539     public void testPollInExecutor() {
1540     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1541     ExecutorService executor = Executors.newFixedThreadPool(2);
1542 jsr166 1.9 executor.execute(new CheckedRunnable() {
1543     public void realRun() throws InterruptedException {
1544 dl 1.1 threadAssertNull(q.poll());
1545 jsr166 1.9 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1546     threadAssertTrue(q.isEmpty());
1547     }});
1548    
1549     executor.execute(new CheckedRunnable() {
1550     public void realRun() throws InterruptedException {
1551     Thread.sleep(SMALL_DELAY_MS);
1552     q.put(one);
1553     }});
1554 jsr166 1.4
1555 dl 1.1 joinPool(executor);
1556     }
1557    
1558     /**
1559     * A deserialized serialized deque has same elements in same order
1560     */
1561 jsr166 1.9 public void testSerialization() throws Exception {
1562 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1563    
1564 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1565     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1566     out.writeObject(q);
1567     out.close();
1568    
1569     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1570     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1571     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1572     assertEquals(q.size(), r.size());
1573     while (!q.isEmpty())
1574     assertEquals(q.remove(), r.remove());
1575 dl 1.1 }
1576    
1577     /**
1578     * drainTo(null) throws NPE
1579 jsr166 1.4 */
1580 dl 1.1 public void testDrainToNull() {
1581     LinkedBlockingDeque q = populatedDeque(SIZE);
1582     try {
1583     q.drainTo(null);
1584     shouldThrow();
1585 jsr166 1.9 } catch (NullPointerException success) {}
1586 dl 1.1 }
1587    
1588     /**
1589     * drainTo(this) throws IAE
1590 jsr166 1.4 */
1591 dl 1.1 public void testDrainToSelf() {
1592     LinkedBlockingDeque q = populatedDeque(SIZE);
1593     try {
1594     q.drainTo(q);
1595     shouldThrow();
1596 jsr166 1.10 } catch (IllegalArgumentException success) {}
1597 dl 1.1 }
1598    
1599     /**
1600     * drainTo(c) empties deque into another collection c
1601 jsr166 1.4 */
1602 dl 1.1 public void testDrainTo() {
1603     LinkedBlockingDeque q = populatedDeque(SIZE);
1604     ArrayList l = new ArrayList();
1605     q.drainTo(l);
1606     assertEquals(q.size(), 0);
1607     assertEquals(l.size(), SIZE);
1608 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1609 dl 1.1 assertEquals(l.get(i), new Integer(i));
1610     q.add(zero);
1611     q.add(one);
1612     assertFalse(q.isEmpty());
1613     assertTrue(q.contains(zero));
1614     assertTrue(q.contains(one));
1615     l.clear();
1616     q.drainTo(l);
1617     assertEquals(q.size(), 0);
1618     assertEquals(l.size(), 2);
1619 jsr166 1.4 for (int i = 0; i < 2; ++i)
1620 dl 1.1 assertEquals(l.get(i), new Integer(i));
1621     }
1622    
1623     /**
1624     * drainTo empties full deque, unblocking a waiting put.
1625 jsr166 1.4 */
1626 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1627 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1628 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1629     public void realRun() throws InterruptedException {
1630     q.put(new Integer(SIZE+1));
1631     }});
1632    
1633     t.start();
1634     ArrayList l = new ArrayList();
1635     q.drainTo(l);
1636     assertTrue(l.size() >= SIZE);
1637     for (int i = 0; i < SIZE; ++i)
1638     assertEquals(l.get(i), new Integer(i));
1639     t.join();
1640     assertTrue(q.size() + l.size() >= SIZE);
1641 dl 1.1 }
1642    
1643     /**
1644     * drainTo(null, n) throws NPE
1645 jsr166 1.4 */
1646 dl 1.1 public void testDrainToNullN() {
1647     LinkedBlockingDeque q = populatedDeque(SIZE);
1648     try {
1649     q.drainTo(null, 0);
1650     shouldThrow();
1651 jsr166 1.9 } catch (NullPointerException success) {}
1652 dl 1.1 }
1653    
1654     /**
1655     * drainTo(this, n) throws IAE
1656 jsr166 1.4 */
1657 dl 1.1 public void testDrainToSelfN() {
1658     LinkedBlockingDeque q = populatedDeque(SIZE);
1659     try {
1660     q.drainTo(q, 0);
1661     shouldThrow();
1662 jsr166 1.9 } catch (IllegalArgumentException success) {}
1663 dl 1.1 }
1664    
1665     /**
1666     * drainTo(c, n) empties first max {n, size} elements of deque into c
1667 jsr166 1.4 */
1668 dl 1.1 public void testDrainToN() {
1669     LinkedBlockingDeque q = new LinkedBlockingDeque();
1670     for (int i = 0; i < SIZE + 2; ++i) {
1671 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1672 dl 1.1 assertTrue(q.offer(new Integer(j)));
1673     ArrayList l = new ArrayList();
1674     q.drainTo(l, i);
1675     int k = (i < SIZE)? i : SIZE;
1676     assertEquals(l.size(), k);
1677     assertEquals(q.size(), SIZE-k);
1678 jsr166 1.4 for (int j = 0; j < k; ++j)
1679 dl 1.1 assertEquals(l.get(j), new Integer(j));
1680     while (q.poll() != null) ;
1681     }
1682     }
1683    
1684     }