ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.15
Committed: Sat Nov 21 21:12:55 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +17 -0 lines
Log Message:
add new test testRemoveLast

File Contents

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