ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.24
Committed: Wed Oct 6 07:49:22 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.23: +18 -27 lines
Log Message:
start of a big refactoring, with only one test refactored: testTimedPollWithOffer

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