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

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.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
750     public void realRun() throws InterruptedException {
751     threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
752     q.poll(LONG_DELAY_MS, MILLISECONDS);
753     q.poll(LONG_DELAY_MS, MILLISECONDS);
754     }};
755    
756     t.start();
757     Thread.sleep(SMALL_DELAY_MS);
758     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
759     t.interrupt();
760     t.join();
761 jsr166 1.4 }
762 dl 1.1
763    
764     /**
765     * putFirst(null) throws NPE
766     */
767 jsr166 1.9 public void testPutFirstNull() throws InterruptedException {
768 jsr166 1.7 try {
769 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
770     q.putFirst(null);
771     shouldThrow();
772 jsr166 1.9 } catch (NullPointerException success) {}
773 dl 1.1 }
774    
775     /**
776     * all elements successfully putFirst are contained
777     */
778 jsr166 1.9 public void testPutFirst() throws InterruptedException {
779     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
780     for (int i = 0; i < SIZE; ++i) {
781     Integer I = new Integer(i);
782     q.putFirst(I);
783     assertTrue(q.contains(I));
784 dl 1.1 }
785 jsr166 1.9 assertEquals(0, q.remainingCapacity());
786 dl 1.1 }
787    
788     /**
789     * putFirst blocks interruptibly if full
790     */
791 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
792 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
793     public void realRun() {
794     int added = 0;
795     try {
796     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
797     for (int i = 0; i < SIZE; ++i) {
798     q.putFirst(new Integer(i));
799     ++added;
800 jsr166 1.4 }
801 jsr166 1.11 q.putFirst(new Integer(SIZE));
802     threadShouldThrow();
803     } catch (InterruptedException success) {
804     threadAssertEquals(added, SIZE);
805     }
806     }});
807 jsr166 1.9
808 dl 1.1 t.start();
809 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
810     t.interrupt();
811     t.join();
812 dl 1.1 }
813    
814     /**
815     * putFirst blocks waiting for take when full
816     */
817 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
818 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
819 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
820     public void realRun() {
821     int added = 0;
822     try {
823     q.putFirst(new Object());
824     ++added;
825     q.putFirst(new Object());
826     ++added;
827     q.putFirst(new Object());
828     ++added;
829     q.putFirst(new Object());
830     ++added;
831     threadShouldThrow();
832     } catch (InterruptedException success) {
833     threadAssertTrue(added >= 2);
834 dl 1.1 }
835 jsr166 1.11 }});
836 jsr166 1.9
837     t.start();
838     Thread.sleep(SHORT_DELAY_MS);
839     q.take();
840     t.interrupt();
841     t.join();
842 dl 1.1 }
843    
844     /**
845     * timed offerFirst times out if full and elements not taken
846     */
847 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
848 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
849 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
850     public void realRun() throws InterruptedException {
851     q.putFirst(new Object());
852     q.putFirst(new Object());
853     threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
854     q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
855     }};
856 jsr166 1.4
857 jsr166 1.9 t.start();
858     Thread.sleep(SMALL_DELAY_MS);
859     t.interrupt();
860     t.join();
861 dl 1.1 }
862    
863     /**
864     * take retrieves elements in FIFO order
865     */
866 jsr166 1.9 public void testTakeFirst() throws InterruptedException {
867     LinkedBlockingDeque q = populatedDeque(SIZE);
868     for (int i = 0; i < SIZE; ++i) {
869     assertEquals(i, ((Integer)q.takeFirst()).intValue());
870 jsr166 1.7 }
871 dl 1.1 }
872    
873     /**
874     * takeFirst blocks interruptibly when empty
875     */
876 jsr166 1.9 public void testTakeFirstFromEmpty() throws InterruptedException {
877 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
878 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
879     public void realRun() throws InterruptedException {
880     q.takeFirst();
881     }};
882    
883     t.start();
884     Thread.sleep(SHORT_DELAY_MS);
885     t.interrupt();
886     t.join();
887 dl 1.1 }
888    
889     /**
890     * TakeFirst removes existing elements until empty, then blocks interruptibly
891     */
892 jsr166 1.9 public void testBlockingTakeFirst() throws InterruptedException {
893     Thread t = new ThreadShouldThrow(InterruptedException.class) {
894     public void realRun() throws InterruptedException {
895     LinkedBlockingDeque q = populatedDeque(SIZE);
896     for (int i = 0; i < SIZE; ++i) {
897     assertEquals(i, ((Integer)q.takeFirst()).intValue());
898     }
899     q.takeFirst();
900     }};
901    
902 dl 1.1 t.start();
903 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
904     t.interrupt();
905     t.join();
906 dl 1.1 }
907    
908    
909     /**
910     * timed pollFirst with zero timeout succeeds when non-empty, else times out
911     */
912 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
913     LinkedBlockingDeque q = populatedDeque(SIZE);
914     for (int i = 0; i < SIZE; ++i) {
915     assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
916 jsr166 1.7 }
917 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
918 dl 1.1 }
919    
920     /**
921     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
922     */
923 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
924     LinkedBlockingDeque q = populatedDeque(SIZE);
925     for (int i = 0; i < SIZE; ++i) {
926     assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
927 jsr166 1.7 }
928 jsr166 1.9 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
929 dl 1.1 }
930    
931     /**
932     * Interrupted timed pollFirst throws InterruptedException instead of
933     * returning timeout status
934     */
935 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
936 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
937 jsr166 1.9 public void realRun() throws InterruptedException {
938     LinkedBlockingDeque q = populatedDeque(SIZE);
939     for (int i = 0; i < SIZE; ++i) {
940 jsr166 1.13 assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
941 jsr166 1.9 }
942 jsr166 1.13 try {
943     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
944     shouldThrow();
945     } catch (InterruptedException success) {}
946     }});
947 jsr166 1.9
948 dl 1.1 t.start();
949 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
950     t.interrupt();
951     t.join();
952 dl 1.1 }
953    
954     /**
955     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
956     * on interruption throws
957     */
958 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
959 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
960 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
961     public void realRun() throws InterruptedException {
962     threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
963     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
964     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
965     }};
966    
967     t.start();
968     Thread.sleep(SMALL_DELAY_MS);
969     assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
970     t.interrupt();
971     t.join();
972 jsr166 1.4 }
973 dl 1.1
974     /**
975     * putLast(null) throws NPE
976     */
977 jsr166 1.9 public void testPutLastNull() throws InterruptedException {
978 jsr166 1.7 try {
979 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
980     q.putLast(null);
981     shouldThrow();
982 jsr166 1.9 } catch (NullPointerException success) {}
983 dl 1.1 }
984    
985     /**
986     * all elements successfully putLast are contained
987     */
988 jsr166 1.9 public void testPutLast() throws InterruptedException {
989     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
990     for (int i = 0; i < SIZE; ++i) {
991     Integer I = new Integer(i);
992     q.putLast(I);
993     assertTrue(q.contains(I));
994 dl 1.1 }
995 jsr166 1.9 assertEquals(0, q.remainingCapacity());
996 dl 1.1 }
997    
998     /**
999     * putLast blocks interruptibly if full
1000     */
1001 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
1002 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1003     public void realRun() {
1004     int added = 0;
1005     try {
1006     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007     for (int i = 0; i < SIZE; ++i) {
1008     q.putLast(new Integer(i));
1009     ++added;
1010 jsr166 1.4 }
1011 jsr166 1.11 q.putLast(new Integer(SIZE));
1012     threadShouldThrow();
1013     } catch (InterruptedException success) {
1014     threadAssertEquals(added, SIZE);
1015     }
1016     }});
1017    
1018 dl 1.1 t.start();
1019 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1020     t.interrupt();
1021     t.join();
1022 dl 1.1 }
1023    
1024     /**
1025     * putLast blocks waiting for take when full
1026     */
1027 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1028 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1029 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1030     public void realRun() {
1031     int added = 0;
1032     try {
1033     q.putLast(new Object());
1034     ++added;
1035     q.putLast(new Object());
1036     ++added;
1037     q.putLast(new Object());
1038     ++added;
1039     q.putLast(new Object());
1040     ++added;
1041     threadShouldThrow();
1042     } catch (InterruptedException success) {
1043     threadAssertTrue(added >= 2);
1044 dl 1.1 }
1045 jsr166 1.11 }});
1046 jsr166 1.9
1047     t.start();
1048     Thread.sleep(SHORT_DELAY_MS);
1049     q.take();
1050     t.interrupt();
1051     t.join();
1052 dl 1.1 }
1053    
1054     /**
1055     * timed offerLast times out if full and elements not taken
1056     */
1057 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1058 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1059 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1060     public void realRun() throws InterruptedException {
1061     q.putLast(new Object());
1062     q.putLast(new Object());
1063     threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1064     q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1065     }};
1066 jsr166 1.4
1067 jsr166 1.9 t.start();
1068     Thread.sleep(SMALL_DELAY_MS);
1069     t.interrupt();
1070     t.join();
1071 dl 1.1 }
1072    
1073     /**
1074     * takeLast retrieves elements in FIFO order
1075     */
1076 jsr166 1.9 public void testTakeLast() throws InterruptedException {
1077     LinkedBlockingDeque q = populatedDeque(SIZE);
1078     for (int i = 0; i < SIZE; ++i) {
1079     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1080 jsr166 1.7 }
1081 dl 1.1 }
1082    
1083     /**
1084     * takeLast blocks interruptibly when empty
1085     */
1086 jsr166 1.9 public void testTakeLastFromEmpty() throws InterruptedException {
1087 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1088 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1089     public void realRun() throws InterruptedException {
1090     q.takeLast();
1091     }};
1092    
1093     t.start();
1094     Thread.sleep(SHORT_DELAY_MS);
1095     t.interrupt();
1096     t.join();
1097 dl 1.1 }
1098    
1099     /**
1100     * TakeLast removes existing elements until empty, then blocks interruptibly
1101     */
1102 jsr166 1.9 public void testBlockingTakeLast() throws InterruptedException {
1103     Thread t = new ThreadShouldThrow(InterruptedException.class) {
1104     public void realRun() throws InterruptedException {
1105     LinkedBlockingDeque q = populatedDeque(SIZE);
1106     for (int i = 0; i < SIZE; ++i) {
1107     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1108     }
1109     q.takeLast();
1110     }};
1111    
1112 dl 1.1 t.start();
1113 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1114     t.interrupt();
1115     t.join();
1116 dl 1.1 }
1117    
1118    
1119     /**
1120     * timed pollLast with zero timeout succeeds when non-empty, else times out
1121     */
1122 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1123     LinkedBlockingDeque q = populatedDeque(SIZE);
1124     for (int i = 0; i < SIZE; ++i) {
1125     assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1126 jsr166 1.7 }
1127 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1128 dl 1.1 }
1129    
1130     /**
1131     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1132     */
1133 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1134     LinkedBlockingDeque q = populatedDeque(SIZE);
1135     for (int i = 0; i < SIZE; ++i) {
1136     assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1137 jsr166 1.7 }
1138 jsr166 1.9 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1139 dl 1.1 }
1140    
1141     /**
1142     * Interrupted timed pollLast throws InterruptedException instead of
1143     * returning timeout status
1144     */
1145 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1146 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
1147 jsr166 1.9 public void realRun() throws InterruptedException {
1148     LinkedBlockingDeque q = populatedDeque(SIZE);
1149     for (int i = 0; i < SIZE; ++i) {
1150 jsr166 1.13 assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1151 jsr166 1.9 }
1152 jsr166 1.13 try {
1153     q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1154     shouldThrow();
1155     } catch (InterruptedException success) {}
1156     }});
1157 jsr166 1.9
1158 dl 1.1 t.start();
1159 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1160     t.interrupt();
1161     t.join();
1162 dl 1.1 }
1163    
1164     /**
1165     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1166     * on interruption throws
1167     */
1168 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1169 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1170 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1171     public void realRun() throws InterruptedException {
1172     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1173     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1174     try {
1175     q.poll(LONG_DELAY_MS, MILLISECONDS);
1176     shouldThrow();
1177     } catch (InterruptedException success) {}
1178     }});
1179    
1180     t.start();
1181     Thread.sleep(SMALL_DELAY_MS);
1182     assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1183     t.interrupt();
1184     t.join();
1185 jsr166 1.4 }
1186 dl 1.1
1187    
1188     /**
1189     * element returns next element, or throws NSEE if empty
1190     */
1191     public void testElement() {
1192     LinkedBlockingDeque q = populatedDeque(SIZE);
1193     for (int i = 0; i < SIZE; ++i) {
1194     assertEquals(i, ((Integer)q.element()).intValue());
1195     q.poll();
1196     }
1197     try {
1198     q.element();
1199     shouldThrow();
1200 jsr166 1.9 } catch (NoSuchElementException success) {}
1201 dl 1.1 }
1202    
1203     /**
1204     * remove(x) removes x and returns true if present
1205     */
1206     public void testRemoveElement() {
1207     LinkedBlockingDeque q = populatedDeque(SIZE);
1208     for (int i = 1; i < SIZE; i+=2) {
1209     assertTrue(q.remove(new Integer(i)));
1210     }
1211     for (int i = 0; i < SIZE; i+=2) {
1212     assertTrue(q.remove(new Integer(i)));
1213     assertFalse(q.remove(new Integer(i+1)));
1214     }
1215     assertTrue(q.isEmpty());
1216     }
1217 jsr166 1.4
1218 dl 1.1 /**
1219     * contains(x) reports true when elements added but not yet removed
1220     */
1221     public void testContains() {
1222     LinkedBlockingDeque q = populatedDeque(SIZE);
1223     for (int i = 0; i < SIZE; ++i) {
1224     assertTrue(q.contains(new Integer(i)));
1225     q.poll();
1226     assertFalse(q.contains(new Integer(i)));
1227     }
1228     }
1229    
1230     /**
1231     * clear removes all elements
1232     */
1233     public void testClear() {
1234     LinkedBlockingDeque q = populatedDeque(SIZE);
1235     q.clear();
1236     assertTrue(q.isEmpty());
1237     assertEquals(0, q.size());
1238     assertEquals(SIZE, q.remainingCapacity());
1239     q.add(one);
1240     assertFalse(q.isEmpty());
1241     assertTrue(q.contains(one));
1242     q.clear();
1243     assertTrue(q.isEmpty());
1244     }
1245    
1246     /**
1247     * containsAll(c) is true when c contains a subset of elements
1248     */
1249     public void testContainsAll() {
1250     LinkedBlockingDeque q = populatedDeque(SIZE);
1251     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1252     for (int i = 0; i < SIZE; ++i) {
1253     assertTrue(q.containsAll(p));
1254     assertFalse(p.containsAll(q));
1255     p.add(new Integer(i));
1256     }
1257     assertTrue(p.containsAll(q));
1258     }
1259    
1260     /**
1261     * retainAll(c) retains only those elements of c and reports true if changed
1262     */
1263     public void testRetainAll() {
1264     LinkedBlockingDeque q = populatedDeque(SIZE);
1265     LinkedBlockingDeque p = populatedDeque(SIZE);
1266     for (int i = 0; i < SIZE; ++i) {
1267     boolean changed = q.retainAll(p);
1268     if (i == 0)
1269     assertFalse(changed);
1270     else
1271     assertTrue(changed);
1272    
1273     assertTrue(q.containsAll(p));
1274     assertEquals(SIZE-i, q.size());
1275     p.remove();
1276     }
1277     }
1278    
1279     /**
1280     * removeAll(c) removes only those elements of c and reports true if changed
1281     */
1282     public void testRemoveAll() {
1283     for (int i = 1; i < SIZE; ++i) {
1284     LinkedBlockingDeque q = populatedDeque(SIZE);
1285     LinkedBlockingDeque p = populatedDeque(i);
1286     assertTrue(q.removeAll(p));
1287     assertEquals(SIZE-i, q.size());
1288     for (int j = 0; j < i; ++j) {
1289     Integer I = (Integer)(p.remove());
1290     assertFalse(q.contains(I));
1291     }
1292     }
1293     }
1294    
1295     /**
1296     * toArray contains all elements
1297     */
1298 jsr166 1.9 public void testToArray() throws InterruptedException{
1299 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1300 jsr166 1.7 Object[] o = q.toArray();
1301     for (int i = 0; i < o.length; i++)
1302     assertEquals(o[i], q.take());
1303 dl 1.1 }
1304    
1305     /**
1306     * toArray(a) contains all elements
1307     */
1308 jsr166 1.9 public void testToArray2() throws InterruptedException {
1309 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1310 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1311     ints = (Integer[])q.toArray(ints);
1312 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1313     assertEquals(ints[i], q.take());
1314 dl 1.1 }
1315    
1316     /**
1317     * toArray(null) throws NPE
1318     */
1319     public void testToArray_BadArg() {
1320 jsr166 1.7 try {
1321 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1322 jsr166 1.7 Object o[] = q.toArray(null);
1323     shouldThrow();
1324     } catch (NullPointerException success) {}
1325 dl 1.1 }
1326    
1327     /**
1328     * toArray with incompatible array type throws CCE
1329     */
1330     public void testToArray1_BadArg() {
1331 jsr166 1.7 try {
1332 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1333 jsr166 1.7 Object o[] = q.toArray(new String[10] );
1334     shouldThrow();
1335 jsr166 1.9 } catch (ArrayStoreException success) {}
1336 dl 1.1 }
1337    
1338 jsr166 1.4
1339 dl 1.1 /**
1340     * iterator iterates through all elements
1341     */
1342 jsr166 1.9 public void testIterator() throws InterruptedException {
1343 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1344 jsr166 1.7 Iterator it = q.iterator();
1345 jsr166 1.9 while (it.hasNext()) {
1346     assertEquals(it.next(), q.take());
1347 jsr166 1.7 }
1348 dl 1.1 }
1349    
1350     /**
1351     * iterator.remove removes current element
1352     */
1353     public void testIteratorRemove () {
1354     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1355     q.add(two);
1356     q.add(one);
1357     q.add(three);
1358    
1359     Iterator it = q.iterator();
1360     it.next();
1361     it.remove();
1362 jsr166 1.4
1363 dl 1.1 it = q.iterator();
1364     assertEquals(it.next(), one);
1365     assertEquals(it.next(), three);
1366     assertFalse(it.hasNext());
1367     }
1368    
1369    
1370     /**
1371     * iterator ordering is FIFO
1372     */
1373     public void testIteratorOrdering() {
1374     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1375     q.add(one);
1376     q.add(two);
1377     q.add(three);
1378     assertEquals(0, q.remainingCapacity());
1379     int k = 0;
1380     for (Iterator it = q.iterator(); it.hasNext();) {
1381     int i = ((Integer)(it.next())).intValue();
1382     assertEquals(++k, i);
1383     }
1384     assertEquals(3, k);
1385     }
1386    
1387     /**
1388     * Modifications do not cause iterators to fail
1389     */
1390     public void testWeaklyConsistentIteration () {
1391     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1392     q.add(one);
1393     q.add(two);
1394     q.add(three);
1395 jsr166 1.9 for (Iterator it = q.iterator(); it.hasNext();) {
1396     q.remove();
1397     it.next();
1398 dl 1.1 }
1399     assertEquals(0, q.size());
1400     }
1401    
1402    
1403     /**
1404 dl 1.2 * Descending iterator iterates through all elements
1405     */
1406     public void testDescendingIterator() {
1407     LinkedBlockingDeque q = populatedDeque(SIZE);
1408     int i = 0;
1409 jsr166 1.7 Iterator it = q.descendingIterator();
1410 jsr166 1.5 while (it.hasNext()) {
1411 dl 1.2 assertTrue(q.contains(it.next()));
1412     ++i;
1413     }
1414     assertEquals(i, SIZE);
1415     assertFalse(it.hasNext());
1416     try {
1417     it.next();
1418 jsr166 1.9 shouldThrow();
1419     } catch (NoSuchElementException success) {}
1420 dl 1.2 }
1421    
1422     /**
1423     * Descending iterator ordering is reverse FIFO
1424     */
1425     public void testDescendingIteratorOrdering() {
1426     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1427 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1428     q.add(new Integer(3));
1429     q.add(new Integer(2));
1430     q.add(new Integer(1));
1431     int k = 0;
1432     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1433     int i = ((Integer)(it.next())).intValue();
1434     assertEquals(++k, i);
1435     }
1436 jsr166 1.4
1437 dl 1.3 assertEquals(3, k);
1438     q.remove();
1439     q.remove();
1440     q.remove();
1441 dl 1.2 }
1442     }
1443    
1444     /**
1445     * descendingIterator.remove removes current element
1446     */
1447     public void testDescendingIteratorRemove () {
1448     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1449 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1450     q.add(new Integer(3));
1451     q.add(new Integer(2));
1452     q.add(new Integer(1));
1453     Iterator it = q.descendingIterator();
1454     assertEquals(it.next(), new Integer(1));
1455     it.remove();
1456     assertEquals(it.next(), new Integer(2));
1457     it = q.descendingIterator();
1458     assertEquals(it.next(), new Integer(2));
1459     assertEquals(it.next(), new Integer(3));
1460     it.remove();
1461     assertFalse(it.hasNext());
1462     q.remove();
1463     }
1464 dl 1.2 }
1465    
1466    
1467     /**
1468 dl 1.1 * toString contains toStrings of elements
1469     */
1470     public void testToString() {
1471     LinkedBlockingDeque q = populatedDeque(SIZE);
1472     String s = q.toString();
1473     for (int i = 0; i < SIZE; ++i) {
1474     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1475     }
1476 jsr166 1.4 }
1477 dl 1.1
1478    
1479     /**
1480     * offer transfers elements across Executor tasks
1481     */
1482     public void testOfferInExecutor() {
1483     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1484     q.add(one);
1485     q.add(two);
1486     ExecutorService executor = Executors.newFixedThreadPool(2);
1487 jsr166 1.9 executor.execute(new CheckedRunnable() {
1488     public void realRun() throws InterruptedException {
1489 dl 1.1 threadAssertFalse(q.offer(three));
1490 jsr166 1.9 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1491     threadAssertEquals(0, q.remainingCapacity());
1492     }});
1493    
1494     executor.execute(new CheckedRunnable() {
1495     public void realRun() throws InterruptedException {
1496     Thread.sleep(SMALL_DELAY_MS);
1497     threadAssertEquals(one, q.take());
1498     }});
1499 jsr166 1.4
1500 dl 1.1 joinPool(executor);
1501     }
1502    
1503     /**
1504     * poll retrieves elements across Executor threads
1505     */
1506     public void testPollInExecutor() {
1507     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1508     ExecutorService executor = Executors.newFixedThreadPool(2);
1509 jsr166 1.9 executor.execute(new CheckedRunnable() {
1510     public void realRun() throws InterruptedException {
1511 dl 1.1 threadAssertNull(q.poll());
1512 jsr166 1.9 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1513     threadAssertTrue(q.isEmpty());
1514     }});
1515    
1516     executor.execute(new CheckedRunnable() {
1517     public void realRun() throws InterruptedException {
1518     Thread.sleep(SMALL_DELAY_MS);
1519     q.put(one);
1520     }});
1521 jsr166 1.4
1522 dl 1.1 joinPool(executor);
1523     }
1524    
1525     /**
1526     * A deserialized serialized deque has same elements in same order
1527     */
1528 jsr166 1.9 public void testSerialization() throws Exception {
1529 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1530    
1531 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1532     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1533     out.writeObject(q);
1534     out.close();
1535    
1536     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1537     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1538     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1539     assertEquals(q.size(), r.size());
1540     while (!q.isEmpty())
1541     assertEquals(q.remove(), r.remove());
1542 dl 1.1 }
1543    
1544     /**
1545     * drainTo(null) throws NPE
1546 jsr166 1.4 */
1547 dl 1.1 public void testDrainToNull() {
1548     LinkedBlockingDeque q = populatedDeque(SIZE);
1549     try {
1550     q.drainTo(null);
1551     shouldThrow();
1552 jsr166 1.9 } catch (NullPointerException success) {}
1553 dl 1.1 }
1554    
1555     /**
1556     * drainTo(this) throws IAE
1557 jsr166 1.4 */
1558 dl 1.1 public void testDrainToSelf() {
1559     LinkedBlockingDeque q = populatedDeque(SIZE);
1560     try {
1561     q.drainTo(q);
1562     shouldThrow();
1563 jsr166 1.10 } catch (IllegalArgumentException success) {}
1564 dl 1.1 }
1565    
1566     /**
1567     * drainTo(c) empties deque into another collection c
1568 jsr166 1.4 */
1569 dl 1.1 public void testDrainTo() {
1570     LinkedBlockingDeque q = populatedDeque(SIZE);
1571     ArrayList l = new ArrayList();
1572     q.drainTo(l);
1573     assertEquals(q.size(), 0);
1574     assertEquals(l.size(), SIZE);
1575 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1576 dl 1.1 assertEquals(l.get(i), new Integer(i));
1577     q.add(zero);
1578     q.add(one);
1579     assertFalse(q.isEmpty());
1580     assertTrue(q.contains(zero));
1581     assertTrue(q.contains(one));
1582     l.clear();
1583     q.drainTo(l);
1584     assertEquals(q.size(), 0);
1585     assertEquals(l.size(), 2);
1586 jsr166 1.4 for (int i = 0; i < 2; ++i)
1587 dl 1.1 assertEquals(l.get(i), new Integer(i));
1588     }
1589    
1590     /**
1591     * drainTo empties full deque, unblocking a waiting put.
1592 jsr166 1.4 */
1593 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1594 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1595 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1596     public void realRun() throws InterruptedException {
1597     q.put(new Integer(SIZE+1));
1598     }});
1599    
1600     t.start();
1601     ArrayList l = new ArrayList();
1602     q.drainTo(l);
1603     assertTrue(l.size() >= SIZE);
1604     for (int i = 0; i < SIZE; ++i)
1605     assertEquals(l.get(i), new Integer(i));
1606     t.join();
1607     assertTrue(q.size() + l.size() >= SIZE);
1608 dl 1.1 }
1609    
1610     /**
1611     * drainTo(null, n) throws NPE
1612 jsr166 1.4 */
1613 dl 1.1 public void testDrainToNullN() {
1614     LinkedBlockingDeque q = populatedDeque(SIZE);
1615     try {
1616     q.drainTo(null, 0);
1617     shouldThrow();
1618 jsr166 1.9 } catch (NullPointerException success) {}
1619 dl 1.1 }
1620    
1621     /**
1622     * drainTo(this, n) throws IAE
1623 jsr166 1.4 */
1624 dl 1.1 public void testDrainToSelfN() {
1625     LinkedBlockingDeque q = populatedDeque(SIZE);
1626     try {
1627     q.drainTo(q, 0);
1628     shouldThrow();
1629 jsr166 1.9 } catch (IllegalArgumentException success) {}
1630 dl 1.1 }
1631    
1632     /**
1633     * drainTo(c, n) empties first max {n, size} elements of deque into c
1634 jsr166 1.4 */
1635 dl 1.1 public void testDrainToN() {
1636     LinkedBlockingDeque q = new LinkedBlockingDeque();
1637     for (int i = 0; i < SIZE + 2; ++i) {
1638 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1639 dl 1.1 assertTrue(q.offer(new Integer(j)));
1640     ArrayList l = new ArrayList();
1641     q.drainTo(l, i);
1642     int k = (i < SIZE)? i : SIZE;
1643     assertEquals(l.size(), k);
1644     assertEquals(q.size(), SIZE-k);
1645 jsr166 1.4 for (int j = 0; j < k; ++j)
1646 dl 1.1 assertEquals(l.get(j), new Integer(j));
1647     while (q.poll() != null) ;
1648     }
1649     }
1650    
1651     }