ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.31
Committed: Thu Nov 4 01:04:54 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +6 -6 lines
Log Message:
strengthen toArray tests

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