ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.11
Committed: Sat Nov 21 10:25:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +62 -63 lines
Log Message:
improve exception handling

File Contents

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