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

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