ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.19
Committed: Tue Dec 1 06:03:49 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +7 -7 lines
Log Message:
Use MILLISECONDS.toNanos instead of multiplying by 1000*1000; use explicit assertEquals instead of assertTrue(...!= null); improve testPutWithTake

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 jsr166 1.18 assertEquals(i, q.pollFirst());
102 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.pollLast());
113 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.peekFirst());
124     assertEquals(i, q.pollFirst());
125 dl 1.1 assertTrue(q.peekFirst() == null ||
126 jsr166 1.18 !q.peekFirst().equals(i));
127 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.peek());
138     assertEquals(i, q.pollFirst());
139 dl 1.1 assertTrue(q.peek() == null ||
140 jsr166 1.18 !q.peek().equals(i));
141 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.peekLast());
152     assertEquals(i, q.pollLast());
153 dl 1.1 assertTrue(q.peekLast() == null ||
154 jsr166 1.18 !q.peekLast().equals(i));
155 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.getFirst());
166     assertEquals(i, q.pollFirst());
167 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.getLast());
182     assertEquals(i, q.pollLast());
183 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.removeFirst());
198 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.removeLast());
213 jsr166 1.15 }
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 jsr166 1.18 assertEquals(i, q.remove());
228 dl 1.1 }
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 jsr166 1.18 assertEquals(i, q.pop());
451 dl 1.1 }
452     try {
453     q.pop();
454     shouldThrow();
455 jsr166 1.9 } catch (NoSuchElementException success) {}
456 dl 1.1 }
457    
458    
459     /**
460     * Offer succeeds if not full; fails if full
461     */
462     public void testOffer() {
463     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
464     assertTrue(q.offer(zero));
465     assertFalse(q.offer(one));
466     }
467    
468     /**
469     * add succeeds if not full; throws ISE if full
470     */
471     public void testAdd() {
472 jsr166 1.7 try {
473 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
474     for (int i = 0; i < SIZE; ++i) {
475     assertTrue(q.add(new Integer(i)));
476     }
477     assertEquals(0, q.remainingCapacity());
478     q.add(new Integer(SIZE));
479 jsr166 1.9 shouldThrow();
480     } catch (IllegalStateException success) {}
481 dl 1.1 }
482    
483     /**
484     * addAll(null) throws NPE
485     */
486     public void testAddAll1() {
487     try {
488     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
489     q.addAll(null);
490     shouldThrow();
491 jsr166 1.9 } catch (NullPointerException success) {}
492 dl 1.1 }
493    
494     /**
495     * addAll(this) throws IAE
496     */
497     public void testAddAllSelf() {
498     try {
499     LinkedBlockingDeque q = populatedDeque(SIZE);
500     q.addAll(q);
501     shouldThrow();
502 jsr166 1.9 } catch (IllegalArgumentException success) {}
503 dl 1.1 }
504    
505     /**
506     * addAll of a collection with null elements throws NPE
507     */
508     public void testAddAll2() {
509     try {
510     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
511     Integer[] ints = new Integer[SIZE];
512     q.addAll(Arrays.asList(ints));
513     shouldThrow();
514 jsr166 1.9 } catch (NullPointerException success) {}
515 dl 1.1 }
516     /**
517     * addAll of a collection with any null elements throws NPE after
518     * possibly adding some elements
519     */
520     public void testAddAll3() {
521     try {
522     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
523     Integer[] ints = new Integer[SIZE];
524     for (int i = 0; i < SIZE-1; ++i)
525     ints[i] = new Integer(i);
526     q.addAll(Arrays.asList(ints));
527     shouldThrow();
528 jsr166 1.9 } catch (NullPointerException success) {}
529 dl 1.1 }
530     /**
531     * addAll throws ISE if not enough room
532     */
533     public void testAddAll4() {
534     try {
535     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
536     Integer[] ints = new Integer[SIZE];
537     for (int i = 0; i < SIZE; ++i)
538     ints[i] = new Integer(i);
539     q.addAll(Arrays.asList(ints));
540     shouldThrow();
541 jsr166 1.9 } catch (IllegalStateException success) {}
542 dl 1.1 }
543 jsr166 1.16
544 dl 1.1 /**
545     * Deque contains all elements, in traversal order, of successful addAll
546     */
547     public void testAddAll5() {
548 jsr166 1.12 Integer[] empty = new Integer[0];
549     Integer[] ints = new Integer[SIZE];
550     for (int i = 0; i < SIZE; ++i)
551     ints[i] = new Integer(i);
552     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553     assertFalse(q.addAll(Arrays.asList(empty)));
554     assertTrue(q.addAll(Arrays.asList(ints)));
555     for (int i = 0; i < SIZE; ++i)
556     assertEquals(ints[i], q.poll());
557 dl 1.1 }
558    
559    
560     /**
561     * put(null) throws NPE
562     */
563 jsr166 1.10 public void testPutNull() throws InterruptedException {
564 jsr166 1.7 try {
565 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
566     q.put(null);
567     shouldThrow();
568 jsr166 1.9 } catch (NullPointerException success) {}
569 jsr166 1.10 }
570 dl 1.1
571     /**
572     * all elements successfully put are contained
573     */
574 jsr166 1.10 public void testPut() throws InterruptedException {
575     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
576     for (int i = 0; i < SIZE; ++i) {
577     Integer I = new Integer(i);
578     q.put(I);
579     assertTrue(q.contains(I));
580     }
581     assertEquals(0, q.remainingCapacity());
582 dl 1.1 }
583    
584     /**
585     * put blocks interruptibly if full
586     */
587 jsr166 1.9 public void testBlockingPut() throws InterruptedException {
588 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
589 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
590 jsr166 1.17 public void realRun() throws InterruptedException {
591     for (int i = 0; i < SIZE; ++i)
592     q.put(i);
593     assertEquals(SIZE, q.size());
594     assertEquals(0, q.remainingCapacity());
595 jsr166 1.9 try {
596 jsr166 1.17 q.put(99);
597     shouldThrow();
598     } catch (InterruptedException success) {}
599 jsr166 1.9 }});
600    
601 dl 1.1 t.start();
602 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
603     t.interrupt();
604     t.join();
605 jsr166 1.17 assertEquals(SIZE, q.size());
606     assertEquals(0, q.remainingCapacity());
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 jsr166 1.17 final int capacity = 2;
614     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
615 jsr166 1.10 Thread t = new Thread(new CheckedRunnable() {
616 jsr166 1.17 public void realRun() throws InterruptedException {
617     for (int i = 0; i < capacity + 1; i++)
618     q.put(i);
619 jsr166 1.10 try {
620 jsr166 1.17 q.put(99);
621     shouldThrow();
622     } catch (InterruptedException success) {}
623 jsr166 1.10 }});
624 jsr166 1.9
625     t.start();
626     Thread.sleep(SHORT_DELAY_MS);
627 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
628     assertEquals(0, q.take());
629     Thread.sleep(SHORT_DELAY_MS);
630 jsr166 1.9 t.interrupt();
631     t.join();
632 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
633 dl 1.1 }
634    
635     /**
636     * timed offer times out if full and elements not taken
637     */
638 jsr166 1.9 public void testTimedOffer() throws InterruptedException {
639 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
640 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
641 jsr166 1.9 public void realRun() throws InterruptedException {
642     q.put(new Object());
643     q.put(new Object());
644 jsr166 1.16 assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645     try {
646     q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
647     shouldThrow();
648     } catch (InterruptedException success) {}
649     }});
650 jsr166 1.4
651 jsr166 1.9 t.start();
652     Thread.sleep(SMALL_DELAY_MS);
653     t.interrupt();
654     t.join();
655 dl 1.1 }
656    
657     /**
658     * take retrieves elements in FIFO order
659     */
660 jsr166 1.9 public void testTake() throws InterruptedException {
661     LinkedBlockingDeque q = populatedDeque(SIZE);
662     for (int i = 0; i < SIZE; ++i) {
663 jsr166 1.18 assertEquals(i, q.take());
664 jsr166 1.7 }
665 dl 1.1 }
666    
667     /**
668     * take blocks interruptibly when empty
669     */
670 jsr166 1.9 public void testTakeFromEmpty() throws InterruptedException {
671 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
672 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
673     public void realRun() throws InterruptedException {
674     q.take();
675     }};
676    
677     t.start();
678     Thread.sleep(SHORT_DELAY_MS);
679     t.interrupt();
680     t.join();
681 dl 1.1 }
682    
683     /**
684     * Take removes existing elements until empty, then blocks interruptibly
685     */
686 jsr166 1.9 public void testBlockingTake() throws InterruptedException {
687 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
688     Thread t = new Thread(new CheckedRunnable() {
689 jsr166 1.9 public void realRun() throws InterruptedException {
690     for (int i = 0; i < SIZE; ++i) {
691 jsr166 1.18 assertEquals(i, q.take());
692 jsr166 1.9 }
693 jsr166 1.17 try {
694     q.take();
695     shouldThrow();
696     } catch (InterruptedException success) {}
697     }});
698 jsr166 1.9
699 dl 1.1 t.start();
700 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
701     t.interrupt();
702     t.join();
703 dl 1.1 }
704    
705    
706     /**
707     * poll succeeds unless empty
708     */
709     public void testPoll() {
710     LinkedBlockingDeque q = populatedDeque(SIZE);
711     for (int i = 0; i < SIZE; ++i) {
712 jsr166 1.18 assertEquals(i, q.poll());
713 dl 1.1 }
714 jsr166 1.7 assertNull(q.poll());
715 dl 1.1 }
716    
717     /**
718     * timed poll with zero timeout succeeds when non-empty, else times out
719     */
720 jsr166 1.9 public void testTimedPoll0() throws InterruptedException {
721     LinkedBlockingDeque q = populatedDeque(SIZE);
722     for (int i = 0; i < SIZE; ++i) {
723 jsr166 1.18 assertEquals(i, q.poll(0, MILLISECONDS));
724 jsr166 1.7 }
725 jsr166 1.9 assertNull(q.poll(0, MILLISECONDS));
726 dl 1.1 }
727    
728     /**
729     * timed poll with nonzero timeout succeeds when non-empty, else times out
730     */
731 jsr166 1.9 public void testTimedPoll() throws InterruptedException {
732     LinkedBlockingDeque q = populatedDeque(SIZE);
733     for (int i = 0; i < SIZE; ++i) {
734 jsr166 1.18 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
735 jsr166 1.7 }
736 jsr166 1.9 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
737 dl 1.1 }
738    
739     /**
740     * Interrupted timed poll throws InterruptedException instead of
741     * returning timeout status
742     */
743 jsr166 1.9 public void testInterruptedTimedPoll() throws InterruptedException {
744 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
745 jsr166 1.9 public void realRun() throws InterruptedException {
746     LinkedBlockingDeque q = populatedDeque(SIZE);
747     for (int i = 0; i < SIZE; ++i) {
748 jsr166 1.18 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
749 jsr166 1.9 }
750 jsr166 1.13 try {
751     q.poll(SMALL_DELAY_MS, MILLISECONDS);
752     shouldThrow();
753     } catch (InterruptedException success) {}
754     }});
755 jsr166 1.9
756 dl 1.1 t.start();
757 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
758     t.interrupt();
759     t.join();
760 dl 1.1 }
761    
762     /**
763     * timed poll before a delayed offer fails; after offer succeeds;
764     * on interruption throws
765     */
766 jsr166 1.9 public void testTimedPollWithOffer() throws InterruptedException {
767 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
769 jsr166 1.9 public void realRun() throws InterruptedException {
770 jsr166 1.14 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772     try {
773     q.poll(LONG_DELAY_MS, MILLISECONDS);
774     shouldThrow();
775     } catch (InterruptedException success) {}
776     }});
777 jsr166 1.9
778     t.start();
779     Thread.sleep(SMALL_DELAY_MS);
780     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
781     t.interrupt();
782     t.join();
783 jsr166 1.4 }
784 dl 1.1
785    
786     /**
787     * putFirst(null) throws NPE
788     */
789 jsr166 1.9 public void testPutFirstNull() throws InterruptedException {
790 jsr166 1.7 try {
791 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792     q.putFirst(null);
793     shouldThrow();
794 jsr166 1.9 } catch (NullPointerException success) {}
795 dl 1.1 }
796    
797     /**
798     * all elements successfully putFirst are contained
799     */
800 jsr166 1.9 public void testPutFirst() throws InterruptedException {
801     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
802     for (int i = 0; i < SIZE; ++i) {
803     Integer I = new Integer(i);
804     q.putFirst(I);
805     assertTrue(q.contains(I));
806 dl 1.1 }
807 jsr166 1.9 assertEquals(0, q.remainingCapacity());
808 dl 1.1 }
809    
810     /**
811     * putFirst blocks interruptibly if full
812     */
813 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
814 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
815 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
816 jsr166 1.17 public void realRun() throws InterruptedException {
817     for (int i = 0; i < SIZE; ++i)
818     q.putFirst(i);
819     assertEquals(SIZE, q.size());
820     assertEquals(0, q.remainingCapacity());
821 jsr166 1.11 try {
822 jsr166 1.17 q.putFirst(99);
823     shouldThrow();
824     } catch (InterruptedException success) {}
825 jsr166 1.11 }});
826 jsr166 1.9
827 dl 1.1 t.start();
828 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
829     t.interrupt();
830     t.join();
831 jsr166 1.17 assertEquals(SIZE, q.size());
832     assertEquals(0, q.remainingCapacity());
833 dl 1.1 }
834    
835     /**
836     * putFirst blocks waiting for take when full
837     */
838 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
839 jsr166 1.17 final int capacity = 2;
840     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
841 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
842 jsr166 1.17 public void realRun() throws InterruptedException {
843     for (int i = 0; i < capacity + 1; i++)
844     q.putFirst(i);
845 jsr166 1.11 try {
846 jsr166 1.17 q.putFirst(99);
847     shouldThrow();
848     } catch (InterruptedException success) {}
849 jsr166 1.11 }});
850 jsr166 1.9
851     t.start();
852     Thread.sleep(SHORT_DELAY_MS);
853 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
854     assertEquals(capacity - 1, q.take());
855     Thread.sleep(SHORT_DELAY_MS);
856 jsr166 1.9 t.interrupt();
857     t.join();
858 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
859 dl 1.1 }
860    
861     /**
862     * timed offerFirst times out if full and elements not taken
863     */
864 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
865 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
866 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
867 jsr166 1.9 public void realRun() throws InterruptedException {
868     q.putFirst(new Object());
869     q.putFirst(new Object());
870 jsr166 1.16 assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871     try {
872     q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
873     shouldThrow();
874     } catch (InterruptedException success) {}
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 jsr166 1.18 assertEquals(i, q.takeFirst());
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 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
914     Thread t = new Thread(new CheckedRunnable() {
915 jsr166 1.9 public void realRun() throws InterruptedException {
916 jsr166 1.17 for (int i = 0; i < SIZE; ++i)
917     assertEquals(i, q.takeFirst());
918     try {
919     q.takeFirst();
920     shouldThrow();
921     } catch (InterruptedException success) {}
922     }});
923 jsr166 1.9
924 dl 1.1 t.start();
925 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
926     t.interrupt();
927     t.join();
928 dl 1.1 }
929    
930    
931     /**
932     * timed pollFirst with zero timeout succeeds when non-empty, else times out
933     */
934 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
935     LinkedBlockingDeque q = populatedDeque(SIZE);
936     for (int i = 0; i < SIZE; ++i) {
937 jsr166 1.18 assertEquals(i, q.pollFirst(0, MILLISECONDS));
938 jsr166 1.7 }
939 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
940 dl 1.1 }
941    
942     /**
943     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
944     */
945 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
946     LinkedBlockingDeque q = populatedDeque(SIZE);
947     for (int i = 0; i < SIZE; ++i) {
948 jsr166 1.18 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
949 jsr166 1.7 }
950 jsr166 1.9 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
951 dl 1.1 }
952    
953     /**
954     * Interrupted timed pollFirst throws InterruptedException instead of
955     * returning timeout status
956     */
957 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
958 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
959 jsr166 1.9 public void realRun() throws InterruptedException {
960     LinkedBlockingDeque q = populatedDeque(SIZE);
961     for (int i = 0; i < SIZE; ++i) {
962 jsr166 1.18 assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
963 jsr166 1.9 }
964 jsr166 1.13 try {
965     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
966     shouldThrow();
967     } catch (InterruptedException success) {}
968     }});
969 jsr166 1.9
970 dl 1.1 t.start();
971 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
972     t.interrupt();
973     t.join();
974 dl 1.1 }
975    
976     /**
977     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
978     * on interruption throws
979     */
980 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
981 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
982 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
983 jsr166 1.9 public void realRun() throws InterruptedException {
984 jsr166 1.14 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985     assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
986     try {
987     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
988     shouldThrow();
989     } catch (InterruptedException success) {}
990     }});
991 jsr166 1.9
992     t.start();
993     Thread.sleep(SMALL_DELAY_MS);
994     assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
995     t.interrupt();
996     t.join();
997 jsr166 1.4 }
998 dl 1.1
999     /**
1000     * putLast(null) throws NPE
1001     */
1002 jsr166 1.9 public void testPutLastNull() throws InterruptedException {
1003 jsr166 1.7 try {
1004 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1005     q.putLast(null);
1006     shouldThrow();
1007 jsr166 1.9 } catch (NullPointerException success) {}
1008 dl 1.1 }
1009    
1010     /**
1011     * all elements successfully putLast are contained
1012     */
1013 jsr166 1.9 public void testPutLast() throws InterruptedException {
1014     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1015     for (int i = 0; i < SIZE; ++i) {
1016     Integer I = new Integer(i);
1017     q.putLast(I);
1018     assertTrue(q.contains(I));
1019 dl 1.1 }
1020 jsr166 1.9 assertEquals(0, q.remainingCapacity());
1021 dl 1.1 }
1022    
1023     /**
1024     * putLast blocks interruptibly if full
1025     */
1026 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
1027 jsr166 1.17 final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1028 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1029 jsr166 1.17 public void realRun() throws InterruptedException {
1030     for (int i = 0; i < SIZE; ++i)
1031     q.putLast(i);
1032     assertEquals(SIZE, q.size());
1033     assertEquals(0, q.remainingCapacity());
1034 jsr166 1.11 try {
1035 jsr166 1.17 q.putLast(99);
1036     shouldThrow();
1037     } catch (InterruptedException success) {}
1038 jsr166 1.11 }});
1039    
1040 dl 1.1 t.start();
1041 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1042     t.interrupt();
1043     t.join();
1044 jsr166 1.17 assertEquals(SIZE, q.size());
1045     assertEquals(0, q.remainingCapacity());
1046 dl 1.1 }
1047    
1048     /**
1049     * putLast blocks waiting for take when full
1050     */
1051 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1052 jsr166 1.17 final int capacity = 2;
1053     final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1054 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1055 jsr166 1.17 public void realRun() throws InterruptedException {
1056     for (int i = 0; i < capacity + 1; i++)
1057     q.putLast(i);
1058 jsr166 1.11 try {
1059 jsr166 1.17 q.putLast(99);
1060     shouldThrow();
1061     } catch (InterruptedException success) {}
1062 jsr166 1.11 }});
1063 jsr166 1.9
1064     t.start();
1065     Thread.sleep(SHORT_DELAY_MS);
1066 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1067     assertEquals(0, q.take());
1068     Thread.sleep(SHORT_DELAY_MS);
1069 jsr166 1.9 t.interrupt();
1070     t.join();
1071 jsr166 1.17 assertEquals(q.remainingCapacity(), 0);
1072 dl 1.1 }
1073    
1074     /**
1075     * timed offerLast times out if full and elements not taken
1076     */
1077 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1078 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1079 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
1080 jsr166 1.9 public void realRun() throws InterruptedException {
1081     q.putLast(new Object());
1082     q.putLast(new Object());
1083 jsr166 1.16 assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084     try {
1085     q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1086     shouldThrow();
1087     } catch (InterruptedException success) {}
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 jsr166 1.18 assertEquals(SIZE-i-1, q.takeLast());
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 jsr166 1.17 final LinkedBlockingDeque q = populatedDeque(SIZE);
1127     Thread t = new Thread(new CheckedRunnable() {
1128 jsr166 1.9 public void realRun() throws InterruptedException {
1129 jsr166 1.17 for (int i = 0; i < SIZE; ++i)
1130     assertEquals(SIZE - 1 - i, q.takeLast());
1131     try {
1132     q.takeLast();
1133     shouldThrow();
1134     } catch (InterruptedException success) {}
1135     }});
1136 jsr166 1.9
1137 dl 1.1 t.start();
1138 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1139     t.interrupt();
1140     t.join();
1141 dl 1.1 }
1142    
1143     /**
1144     * timed pollLast with zero timeout succeeds when non-empty, else times out
1145     */
1146 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1147     LinkedBlockingDeque q = populatedDeque(SIZE);
1148     for (int i = 0; i < SIZE; ++i) {
1149 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1150 jsr166 1.7 }
1151 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1152 dl 1.1 }
1153    
1154     /**
1155     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1156     */
1157 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1158     LinkedBlockingDeque q = populatedDeque(SIZE);
1159     for (int i = 0; i < SIZE; ++i) {
1160 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1161 jsr166 1.7 }
1162 jsr166 1.9 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1163 dl 1.1 }
1164    
1165     /**
1166     * Interrupted timed pollLast throws InterruptedException instead of
1167     * returning timeout status
1168     */
1169 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1170 jsr166 1.13 Thread t = new Thread(new CheckedRunnable() {
1171 jsr166 1.9 public void realRun() throws InterruptedException {
1172     LinkedBlockingDeque q = populatedDeque(SIZE);
1173     for (int i = 0; i < SIZE; ++i) {
1174 jsr166 1.18 assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1175 jsr166 1.9 }
1176 jsr166 1.13 try {
1177     q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1178     shouldThrow();
1179     } catch (InterruptedException success) {}
1180     }});
1181 jsr166 1.9
1182 dl 1.1 t.start();
1183 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1184     t.interrupt();
1185     t.join();
1186 dl 1.1 }
1187    
1188     /**
1189     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1190     * on interruption throws
1191     */
1192 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1193 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1194 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1195     public void realRun() throws InterruptedException {
1196     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1197     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1198     try {
1199     q.poll(LONG_DELAY_MS, MILLISECONDS);
1200     shouldThrow();
1201     } catch (InterruptedException success) {}
1202     }});
1203    
1204     t.start();
1205     Thread.sleep(SMALL_DELAY_MS);
1206     assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1207     t.interrupt();
1208     t.join();
1209 jsr166 1.4 }
1210 dl 1.1
1211    
1212     /**
1213     * element returns next element, or throws NSEE if empty
1214     */
1215     public void testElement() {
1216     LinkedBlockingDeque q = populatedDeque(SIZE);
1217     for (int i = 0; i < SIZE; ++i) {
1218 jsr166 1.18 assertEquals(i, q.element());
1219 dl 1.1 q.poll();
1220     }
1221     try {
1222     q.element();
1223     shouldThrow();
1224 jsr166 1.9 } catch (NoSuchElementException success) {}
1225 dl 1.1 }
1226    
1227     /**
1228     * remove(x) removes x and returns true if present
1229     */
1230     public void testRemoveElement() {
1231     LinkedBlockingDeque q = populatedDeque(SIZE);
1232     for (int i = 1; i < SIZE; i+=2) {
1233     assertTrue(q.remove(new Integer(i)));
1234     }
1235     for (int i = 0; i < SIZE; i+=2) {
1236     assertTrue(q.remove(new Integer(i)));
1237     assertFalse(q.remove(new Integer(i+1)));
1238     }
1239     assertTrue(q.isEmpty());
1240     }
1241 jsr166 1.4
1242 dl 1.1 /**
1243     * contains(x) reports true when elements added but not yet removed
1244     */
1245     public void testContains() {
1246     LinkedBlockingDeque q = populatedDeque(SIZE);
1247     for (int i = 0; i < SIZE; ++i) {
1248     assertTrue(q.contains(new Integer(i)));
1249     q.poll();
1250     assertFalse(q.contains(new Integer(i)));
1251     }
1252     }
1253    
1254     /**
1255     * clear removes all elements
1256     */
1257     public void testClear() {
1258     LinkedBlockingDeque q = populatedDeque(SIZE);
1259     q.clear();
1260     assertTrue(q.isEmpty());
1261     assertEquals(0, q.size());
1262     assertEquals(SIZE, q.remainingCapacity());
1263     q.add(one);
1264     assertFalse(q.isEmpty());
1265     assertTrue(q.contains(one));
1266     q.clear();
1267     assertTrue(q.isEmpty());
1268     }
1269    
1270     /**
1271     * containsAll(c) is true when c contains a subset of elements
1272     */
1273     public void testContainsAll() {
1274     LinkedBlockingDeque q = populatedDeque(SIZE);
1275     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1276     for (int i = 0; i < SIZE; ++i) {
1277     assertTrue(q.containsAll(p));
1278     assertFalse(p.containsAll(q));
1279     p.add(new Integer(i));
1280     }
1281     assertTrue(p.containsAll(q));
1282     }
1283    
1284     /**
1285     * retainAll(c) retains only those elements of c and reports true if changed
1286     */
1287     public void testRetainAll() {
1288     LinkedBlockingDeque q = populatedDeque(SIZE);
1289     LinkedBlockingDeque p = populatedDeque(SIZE);
1290     for (int i = 0; i < SIZE; ++i) {
1291     boolean changed = q.retainAll(p);
1292     if (i == 0)
1293     assertFalse(changed);
1294     else
1295     assertTrue(changed);
1296    
1297     assertTrue(q.containsAll(p));
1298     assertEquals(SIZE-i, q.size());
1299     p.remove();
1300     }
1301     }
1302    
1303     /**
1304     * removeAll(c) removes only those elements of c and reports true if changed
1305     */
1306     public void testRemoveAll() {
1307     for (int i = 1; i < SIZE; ++i) {
1308     LinkedBlockingDeque q = populatedDeque(SIZE);
1309     LinkedBlockingDeque p = populatedDeque(i);
1310     assertTrue(q.removeAll(p));
1311     assertEquals(SIZE-i, q.size());
1312     for (int j = 0; j < i; ++j) {
1313     Integer I = (Integer)(p.remove());
1314     assertFalse(q.contains(I));
1315     }
1316     }
1317     }
1318    
1319     /**
1320     * toArray contains all elements
1321     */
1322 jsr166 1.9 public void testToArray() throws InterruptedException{
1323 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1324 jsr166 1.7 Object[] o = q.toArray();
1325     for (int i = 0; i < o.length; i++)
1326     assertEquals(o[i], q.take());
1327 dl 1.1 }
1328    
1329     /**
1330     * toArray(a) contains all elements
1331     */
1332 jsr166 1.9 public void testToArray2() throws InterruptedException {
1333 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1334 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1335     ints = (Integer[])q.toArray(ints);
1336 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1337     assertEquals(ints[i], q.take());
1338 dl 1.1 }
1339    
1340     /**
1341     * toArray(null) throws NPE
1342     */
1343     public void testToArray_BadArg() {
1344 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1345 jsr166 1.7 try {
1346     Object o[] = q.toArray(null);
1347     shouldThrow();
1348     } catch (NullPointerException success) {}
1349 dl 1.1 }
1350    
1351     /**
1352     * toArray with incompatible array type throws CCE
1353     */
1354     public void testToArray1_BadArg() {
1355 jsr166 1.18 LinkedBlockingDeque q = populatedDeque(SIZE);
1356 jsr166 1.7 try {
1357 jsr166 1.18 Object o[] = q.toArray(new String[10]);
1358 jsr166 1.7 shouldThrow();
1359 jsr166 1.9 } catch (ArrayStoreException success) {}
1360 dl 1.1 }
1361    
1362 jsr166 1.4
1363 dl 1.1 /**
1364     * iterator iterates through all elements
1365     */
1366 jsr166 1.9 public void testIterator() throws InterruptedException {
1367 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1368 jsr166 1.7 Iterator it = q.iterator();
1369 jsr166 1.9 while (it.hasNext()) {
1370     assertEquals(it.next(), q.take());
1371 jsr166 1.7 }
1372 dl 1.1 }
1373    
1374     /**
1375     * iterator.remove removes current element
1376     */
1377     public void testIteratorRemove () {
1378     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1379     q.add(two);
1380     q.add(one);
1381     q.add(three);
1382    
1383     Iterator it = q.iterator();
1384     it.next();
1385     it.remove();
1386 jsr166 1.4
1387 dl 1.1 it = q.iterator();
1388     assertEquals(it.next(), one);
1389     assertEquals(it.next(), three);
1390     assertFalse(it.hasNext());
1391     }
1392    
1393    
1394     /**
1395     * iterator ordering is FIFO
1396     */
1397     public void testIteratorOrdering() {
1398     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1399     q.add(one);
1400     q.add(two);
1401     q.add(three);
1402     assertEquals(0, q.remainingCapacity());
1403     int k = 0;
1404     for (Iterator it = q.iterator(); it.hasNext();) {
1405 jsr166 1.18 assertEquals(++k, it.next());
1406 dl 1.1 }
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 jsr166 1.18 assertEquals(++k, it.next());
1457 dl 1.3 }
1458 jsr166 1.4
1459 dl 1.3 assertEquals(3, k);
1460     q.remove();
1461     q.remove();
1462     q.remove();
1463 dl 1.2 }
1464     }
1465    
1466     /**
1467     * descendingIterator.remove removes current element
1468     */
1469     public void testDescendingIteratorRemove () {
1470     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1471 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1472     q.add(new Integer(3));
1473     q.add(new Integer(2));
1474     q.add(new Integer(1));
1475     Iterator it = q.descendingIterator();
1476     assertEquals(it.next(), new Integer(1));
1477     it.remove();
1478     assertEquals(it.next(), new Integer(2));
1479     it = q.descendingIterator();
1480     assertEquals(it.next(), new Integer(2));
1481     assertEquals(it.next(), new Integer(3));
1482     it.remove();
1483     assertFalse(it.hasNext());
1484     q.remove();
1485     }
1486 dl 1.2 }
1487    
1488    
1489     /**
1490 dl 1.1 * toString contains toStrings of elements
1491     */
1492     public void testToString() {
1493     LinkedBlockingDeque q = populatedDeque(SIZE);
1494     String s = q.toString();
1495     for (int i = 0; i < SIZE; ++i) {
1496     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1497     }
1498 jsr166 1.4 }
1499 dl 1.1
1500    
1501     /**
1502     * offer transfers elements across Executor tasks
1503     */
1504     public void testOfferInExecutor() {
1505     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1506     q.add(one);
1507     q.add(two);
1508     ExecutorService executor = Executors.newFixedThreadPool(2);
1509 jsr166 1.9 executor.execute(new CheckedRunnable() {
1510     public void realRun() throws InterruptedException {
1511 jsr166 1.19 assertFalse(q.offer(three));
1512     assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1513     assertEquals(0, q.remainingCapacity());
1514 jsr166 1.9 }});
1515    
1516     executor.execute(new CheckedRunnable() {
1517     public void realRun() throws InterruptedException {
1518     Thread.sleep(SMALL_DELAY_MS);
1519 jsr166 1.19 assertSame(one, q.take());
1520 jsr166 1.9 }});
1521 jsr166 1.4
1522 dl 1.1 joinPool(executor);
1523     }
1524    
1525     /**
1526     * poll retrieves elements across Executor threads
1527     */
1528     public void testPollInExecutor() {
1529     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1530     ExecutorService executor = Executors.newFixedThreadPool(2);
1531 jsr166 1.9 executor.execute(new CheckedRunnable() {
1532     public void realRun() throws InterruptedException {
1533 jsr166 1.19 assertNull(q.poll());
1534     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1535     assertTrue(q.isEmpty());
1536 jsr166 1.9 }});
1537    
1538     executor.execute(new CheckedRunnable() {
1539     public void realRun() throws InterruptedException {
1540     Thread.sleep(SMALL_DELAY_MS);
1541     q.put(one);
1542     }});
1543 jsr166 1.4
1544 dl 1.1 joinPool(executor);
1545     }
1546    
1547     /**
1548     * A deserialized serialized deque has same elements in same order
1549     */
1550 jsr166 1.9 public void testSerialization() throws Exception {
1551 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1552    
1553 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1554     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1555     out.writeObject(q);
1556     out.close();
1557    
1558     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1559     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1560     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1561     assertEquals(q.size(), r.size());
1562     while (!q.isEmpty())
1563     assertEquals(q.remove(), r.remove());
1564 dl 1.1 }
1565    
1566     /**
1567     * drainTo(null) throws NPE
1568 jsr166 1.4 */
1569 dl 1.1 public void testDrainToNull() {
1570     LinkedBlockingDeque q = populatedDeque(SIZE);
1571     try {
1572     q.drainTo(null);
1573     shouldThrow();
1574 jsr166 1.9 } catch (NullPointerException success) {}
1575 dl 1.1 }
1576    
1577     /**
1578     * drainTo(this) throws IAE
1579 jsr166 1.4 */
1580 dl 1.1 public void testDrainToSelf() {
1581     LinkedBlockingDeque q = populatedDeque(SIZE);
1582     try {
1583     q.drainTo(q);
1584     shouldThrow();
1585 jsr166 1.10 } catch (IllegalArgumentException success) {}
1586 dl 1.1 }
1587    
1588     /**
1589     * drainTo(c) empties deque into another collection c
1590 jsr166 1.4 */
1591 dl 1.1 public void testDrainTo() {
1592     LinkedBlockingDeque q = populatedDeque(SIZE);
1593     ArrayList l = new ArrayList();
1594     q.drainTo(l);
1595     assertEquals(q.size(), 0);
1596     assertEquals(l.size(), SIZE);
1597 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1598 dl 1.1 assertEquals(l.get(i), new Integer(i));
1599     q.add(zero);
1600     q.add(one);
1601     assertFalse(q.isEmpty());
1602     assertTrue(q.contains(zero));
1603     assertTrue(q.contains(one));
1604     l.clear();
1605     q.drainTo(l);
1606     assertEquals(q.size(), 0);
1607     assertEquals(l.size(), 2);
1608 jsr166 1.4 for (int i = 0; i < 2; ++i)
1609 dl 1.1 assertEquals(l.get(i), new Integer(i));
1610     }
1611    
1612     /**
1613     * drainTo empties full deque, unblocking a waiting put.
1614 jsr166 1.4 */
1615 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1616 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1617 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1618     public void realRun() throws InterruptedException {
1619     q.put(new Integer(SIZE+1));
1620     }});
1621    
1622     t.start();
1623     ArrayList l = new ArrayList();
1624     q.drainTo(l);
1625     assertTrue(l.size() >= SIZE);
1626     for (int i = 0; i < SIZE; ++i)
1627     assertEquals(l.get(i), new Integer(i));
1628     t.join();
1629     assertTrue(q.size() + l.size() >= SIZE);
1630 dl 1.1 }
1631    
1632     /**
1633     * drainTo(null, n) throws NPE
1634 jsr166 1.4 */
1635 dl 1.1 public void testDrainToNullN() {
1636     LinkedBlockingDeque q = populatedDeque(SIZE);
1637     try {
1638     q.drainTo(null, 0);
1639     shouldThrow();
1640 jsr166 1.9 } catch (NullPointerException success) {}
1641 dl 1.1 }
1642    
1643     /**
1644     * drainTo(this, n) throws IAE
1645 jsr166 1.4 */
1646 dl 1.1 public void testDrainToSelfN() {
1647     LinkedBlockingDeque q = populatedDeque(SIZE);
1648     try {
1649     q.drainTo(q, 0);
1650     shouldThrow();
1651 jsr166 1.9 } catch (IllegalArgumentException success) {}
1652 dl 1.1 }
1653    
1654     /**
1655     * drainTo(c, n) empties first max {n, size} elements of deque into c
1656 jsr166 1.4 */
1657 dl 1.1 public void testDrainToN() {
1658     LinkedBlockingDeque q = new LinkedBlockingDeque();
1659     for (int i = 0; i < SIZE + 2; ++i) {
1660 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1661 dl 1.1 assertTrue(q.offer(new Integer(j)));
1662     ArrayList l = new ArrayList();
1663     q.drainTo(l, i);
1664     int k = (i < SIZE)? i : SIZE;
1665     assertEquals(l.size(), k);
1666     assertEquals(q.size(), SIZE-k);
1667 jsr166 1.4 for (int j = 0; j < k; ++j)
1668 dl 1.1 assertEquals(l.get(j), new Integer(j));
1669     while (q.poll() != null) ;
1670     }
1671     }
1672    
1673     }