ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.12
Committed: Sat Nov 21 10:29:50 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +15 -21 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 jsr166 1.12 Integer[] ints = new Integer[SIZE];
327     for (int i = 0; i < SIZE; ++i)
328     ints[i] = new Integer(i);
329     LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
330     for (int i = 0; i < SIZE; ++i)
331     assertEquals(ints[i], q.poll());
332 dl 1.1 }
333    
334     /**
335     * Deque transitions from empty to full when elements added
336     */
337     public void testEmptyFull() {
338     LinkedBlockingDeque q = new LinkedBlockingDeque(2);
339     assertTrue(q.isEmpty());
340     assertEquals("should have room for 2", 2, q.remainingCapacity());
341     q.add(one);
342     assertFalse(q.isEmpty());
343     q.add(two);
344     assertFalse(q.isEmpty());
345     assertEquals(0, q.remainingCapacity());
346     assertFalse(q.offer(three));
347     }
348    
349     /**
350     * remainingCapacity decreases on add, increases on remove
351     */
352     public void testRemainingCapacity() {
353     LinkedBlockingDeque q = populatedDeque(SIZE);
354     for (int i = 0; i < SIZE; ++i) {
355     assertEquals(i, q.remainingCapacity());
356     assertEquals(SIZE-i, q.size());
357     q.remove();
358     }
359     for (int i = 0; i < SIZE; ++i) {
360     assertEquals(SIZE-i, q.remainingCapacity());
361     assertEquals(i, q.size());
362     q.add(new Integer(i));
363     }
364     }
365    
366     /**
367     * offer(null) throws NPE
368     */
369     public void testOfferNull() {
370 jsr166 1.7 try {
371 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
372     q.offer(null);
373     shouldThrow();
374 jsr166 1.9 } catch (NullPointerException success) {}
375 dl 1.1 }
376    
377     /**
378     * add(null) throws NPE
379     */
380     public void testAddNull() {
381 jsr166 1.7 try {
382 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
383     q.add(null);
384     shouldThrow();
385 jsr166 1.9 } catch (NullPointerException success) {}
386 dl 1.1 }
387    
388     /**
389     * push(null) throws NPE
390     */
391     public void testPushNull() {
392 jsr166 1.7 try {
393 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(1);
394     q.push(null);
395     shouldThrow();
396 jsr166 1.9 } catch (NullPointerException success) {}
397 dl 1.1 }
398    
399     /**
400     * push succeeds if not full; throws ISE if full
401     */
402     public void testPush() {
403 jsr166 1.7 try {
404 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
405     for (int i = 0; i < SIZE; ++i) {
406     Integer I = new Integer(i);
407     q.push(I);
408     assertEquals(I, q.peek());
409     }
410     assertEquals(0, q.remainingCapacity());
411     q.push(new Integer(SIZE));
412 jsr166 1.9 shouldThrow();
413     } catch (IllegalStateException success) {}
414 dl 1.1 }
415    
416     /**
417     * peekFirst returns element inserted with push
418     */
419     public void testPushWithPeek() {
420     LinkedBlockingDeque q = populatedDeque(3);
421     q.pollLast();
422 jsr166 1.7 q.push(four);
423     assertEquals(four,q.peekFirst());
424 jsr166 1.4 }
425 dl 1.1
426    
427     /**
428     * pop removes next element, or throws NSEE if empty
429     */
430     public void testPop() {
431     LinkedBlockingDeque q = populatedDeque(SIZE);
432     for (int i = 0; i < SIZE; ++i) {
433     assertEquals(i, ((Integer)q.pop()).intValue());
434     }
435     try {
436     q.pop();
437     shouldThrow();
438 jsr166 1.9 } catch (NoSuchElementException success) {}
439 dl 1.1 }
440    
441    
442     /**
443     * Offer succeeds if not full; fails if full
444     */
445     public void testOffer() {
446     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
447     assertTrue(q.offer(zero));
448     assertFalse(q.offer(one));
449     }
450    
451     /**
452     * add succeeds if not full; throws ISE if full
453     */
454     public void testAdd() {
455 jsr166 1.7 try {
456 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
457     for (int i = 0; i < SIZE; ++i) {
458     assertTrue(q.add(new Integer(i)));
459     }
460     assertEquals(0, q.remainingCapacity());
461     q.add(new Integer(SIZE));
462 jsr166 1.9 shouldThrow();
463     } catch (IllegalStateException success) {}
464 dl 1.1 }
465    
466     /**
467     * addAll(null) throws NPE
468     */
469     public void testAddAll1() {
470     try {
471     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
472     q.addAll(null);
473     shouldThrow();
474 jsr166 1.9 } catch (NullPointerException success) {}
475 dl 1.1 }
476    
477     /**
478     * addAll(this) throws IAE
479     */
480     public void testAddAllSelf() {
481     try {
482     LinkedBlockingDeque q = populatedDeque(SIZE);
483     q.addAll(q);
484     shouldThrow();
485 jsr166 1.9 } catch (IllegalArgumentException success) {}
486 dl 1.1 }
487    
488     /**
489     * addAll of a collection with null elements throws NPE
490     */
491     public void testAddAll2() {
492     try {
493     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
494     Integer[] ints = new Integer[SIZE];
495     q.addAll(Arrays.asList(ints));
496     shouldThrow();
497 jsr166 1.9 } catch (NullPointerException success) {}
498 dl 1.1 }
499     /**
500     * addAll of a collection with any null elements throws NPE after
501     * possibly adding some elements
502     */
503     public void testAddAll3() {
504     try {
505     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
506     Integer[] ints = new Integer[SIZE];
507     for (int i = 0; i < SIZE-1; ++i)
508     ints[i] = new Integer(i);
509     q.addAll(Arrays.asList(ints));
510     shouldThrow();
511 jsr166 1.9 } catch (NullPointerException success) {}
512 dl 1.1 }
513     /**
514     * addAll throws ISE if not enough room
515     */
516     public void testAddAll4() {
517     try {
518     LinkedBlockingDeque q = new LinkedBlockingDeque(1);
519     Integer[] ints = new Integer[SIZE];
520     for (int i = 0; i < SIZE; ++i)
521     ints[i] = new Integer(i);
522     q.addAll(Arrays.asList(ints));
523     shouldThrow();
524 jsr166 1.9 } catch (IllegalStateException success) {}
525 dl 1.1 }
526     /**
527     * Deque contains all elements, in traversal order, of successful addAll
528     */
529     public void testAddAll5() {
530 jsr166 1.12 Integer[] empty = new Integer[0];
531     Integer[] ints = new Integer[SIZE];
532     for (int i = 0; i < SIZE; ++i)
533     ints[i] = new Integer(i);
534     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
535     assertFalse(q.addAll(Arrays.asList(empty)));
536     assertTrue(q.addAll(Arrays.asList(ints)));
537     for (int i = 0; i < SIZE; ++i)
538     assertEquals(ints[i], q.poll());
539 dl 1.1 }
540    
541    
542     /**
543     * put(null) throws NPE
544     */
545 jsr166 1.10 public void testPutNull() throws InterruptedException {
546 jsr166 1.7 try {
547 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
548     q.put(null);
549     shouldThrow();
550 jsr166 1.9 } catch (NullPointerException success) {}
551 jsr166 1.10 }
552 dl 1.1
553     /**
554     * all elements successfully put are contained
555     */
556 jsr166 1.10 public void testPut() throws InterruptedException {
557     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
558     for (int i = 0; i < SIZE; ++i) {
559     Integer I = new Integer(i);
560     q.put(I);
561     assertTrue(q.contains(I));
562     }
563     assertEquals(0, q.remainingCapacity());
564 dl 1.1 }
565    
566     /**
567     * put blocks interruptibly if full
568     */
569 jsr166 1.9 public void testBlockingPut() throws InterruptedException {
570     Thread t = new Thread(new CheckedRunnable() {
571     public void realRun() {
572     int added = 0;
573     try {
574     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575     for (int i = 0; i < SIZE; ++i) {
576     q.put(new Integer(i));
577     ++added;
578 jsr166 1.4 }
579 jsr166 1.9 q.put(new Integer(SIZE));
580     threadShouldThrow();
581     } catch (InterruptedException success) {
582     threadAssertEquals(added, SIZE);
583     }
584     }});
585    
586 dl 1.1 t.start();
587 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
588     t.interrupt();
589     t.join();
590 dl 1.1 }
591    
592     /**
593     * put blocks waiting for take when full
594     */
595 jsr166 1.9 public void testPutWithTake() throws InterruptedException {
596 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
597 jsr166 1.10 Thread t = new Thread(new CheckedRunnable() {
598     public void realRun() {
599     int added = 0;
600     try {
601     q.put(new Object());
602     ++added;
603     q.put(new Object());
604     ++added;
605     q.put(new Object());
606     ++added;
607     q.put(new Object());
608     ++added;
609     threadShouldThrow();
610     } catch (InterruptedException success) {
611     threadAssertTrue(added >= 2);
612 dl 1.1 }
613 jsr166 1.10 }});
614 jsr166 1.9
615     t.start();
616     Thread.sleep(SHORT_DELAY_MS);
617     q.take();
618     t.interrupt();
619     t.join();
620 dl 1.1 }
621    
622     /**
623     * timed offer times out if full and elements not taken
624     */
625 jsr166 1.9 public void testTimedOffer() throws InterruptedException {
626 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
627 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
628     public void realRun() throws InterruptedException {
629     q.put(new Object());
630     q.put(new Object());
631     threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
632     q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
633     }};
634 jsr166 1.4
635 jsr166 1.9 t.start();
636     Thread.sleep(SMALL_DELAY_MS);
637     t.interrupt();
638     t.join();
639 dl 1.1 }
640    
641     /**
642     * take retrieves elements in FIFO order
643     */
644 jsr166 1.9 public void testTake() throws InterruptedException {
645     LinkedBlockingDeque q = populatedDeque(SIZE);
646     for (int i = 0; i < SIZE; ++i) {
647     assertEquals(i, ((Integer)q.take()).intValue());
648 jsr166 1.7 }
649 dl 1.1 }
650    
651     /**
652     * take blocks interruptibly when empty
653     */
654 jsr166 1.9 public void testTakeFromEmpty() throws InterruptedException {
655 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
656 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
657     public void realRun() throws InterruptedException {
658     q.take();
659     }};
660    
661     t.start();
662     Thread.sleep(SHORT_DELAY_MS);
663     t.interrupt();
664     t.join();
665 dl 1.1 }
666    
667     /**
668     * Take removes existing elements until empty, then blocks interruptibly
669     */
670 jsr166 1.9 public void testBlockingTake() throws InterruptedException {
671     Thread t = new ThreadShouldThrow(InterruptedException.class) {
672     public void realRun() throws InterruptedException {
673     LinkedBlockingDeque q = populatedDeque(SIZE);
674     for (int i = 0; i < SIZE; ++i) {
675     assertEquals(i, ((Integer)q.take()).intValue());
676     }
677     q.take();
678     }};
679    
680 dl 1.1 t.start();
681 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
682     t.interrupt();
683     t.join();
684 dl 1.1 }
685    
686    
687     /**
688     * poll succeeds unless empty
689     */
690     public void testPoll() {
691     LinkedBlockingDeque q = populatedDeque(SIZE);
692     for (int i = 0; i < SIZE; ++i) {
693     assertEquals(i, ((Integer)q.poll()).intValue());
694     }
695 jsr166 1.7 assertNull(q.poll());
696 dl 1.1 }
697    
698     /**
699     * timed poll with zero timeout succeeds when non-empty, else times out
700     */
701 jsr166 1.9 public void testTimedPoll0() throws InterruptedException {
702     LinkedBlockingDeque q = populatedDeque(SIZE);
703     for (int i = 0; i < SIZE; ++i) {
704     assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
705 jsr166 1.7 }
706 jsr166 1.9 assertNull(q.poll(0, MILLISECONDS));
707 dl 1.1 }
708    
709     /**
710     * timed poll with nonzero timeout succeeds when non-empty, else times out
711     */
712 jsr166 1.9 public void testTimedPoll() throws InterruptedException {
713     LinkedBlockingDeque q = populatedDeque(SIZE);
714     for (int i = 0; i < SIZE; ++i) {
715     assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
716 jsr166 1.7 }
717 jsr166 1.9 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
718 dl 1.1 }
719    
720     /**
721     * Interrupted timed poll throws InterruptedException instead of
722     * returning timeout status
723     */
724 jsr166 1.9 public void testInterruptedTimedPoll() throws InterruptedException {
725     Thread t = new ThreadShouldThrow(InterruptedException.class) {
726     public void realRun() throws InterruptedException {
727     LinkedBlockingDeque q = populatedDeque(SIZE);
728     for (int i = 0; i < SIZE; ++i) {
729     threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
730     }
731     q.poll(SMALL_DELAY_MS, MILLISECONDS);
732     }};
733    
734 dl 1.1 t.start();
735 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
736     t.interrupt();
737     t.join();
738 dl 1.1 }
739    
740     /**
741     * timed poll before a delayed offer fails; after offer succeeds;
742     * on interruption throws
743     */
744 jsr166 1.9 public void testTimedPollWithOffer() throws InterruptedException {
745 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
746 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
747     public void realRun() throws InterruptedException {
748     threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
749     q.poll(LONG_DELAY_MS, MILLISECONDS);
750     q.poll(LONG_DELAY_MS, MILLISECONDS);
751     }};
752    
753     t.start();
754     Thread.sleep(SMALL_DELAY_MS);
755     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
756     t.interrupt();
757     t.join();
758 jsr166 1.4 }
759 dl 1.1
760    
761     /**
762     * putFirst(null) throws NPE
763     */
764 jsr166 1.9 public void testPutFirstNull() throws InterruptedException {
765 jsr166 1.7 try {
766 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
767     q.putFirst(null);
768     shouldThrow();
769 jsr166 1.9 } catch (NullPointerException success) {}
770 dl 1.1 }
771    
772     /**
773     * all elements successfully putFirst are contained
774     */
775 jsr166 1.9 public void testPutFirst() throws InterruptedException {
776     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
777     for (int i = 0; i < SIZE; ++i) {
778     Integer I = new Integer(i);
779     q.putFirst(I);
780     assertTrue(q.contains(I));
781 dl 1.1 }
782 jsr166 1.9 assertEquals(0, q.remainingCapacity());
783 dl 1.1 }
784    
785     /**
786     * putFirst blocks interruptibly if full
787     */
788 jsr166 1.9 public void testBlockingPutFirst() throws InterruptedException {
789 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
790     public void realRun() {
791     int added = 0;
792     try {
793     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
794     for (int i = 0; i < SIZE; ++i) {
795     q.putFirst(new Integer(i));
796     ++added;
797 jsr166 1.4 }
798 jsr166 1.11 q.putFirst(new Integer(SIZE));
799     threadShouldThrow();
800     } catch (InterruptedException success) {
801     threadAssertEquals(added, SIZE);
802     }
803     }});
804 jsr166 1.9
805 dl 1.1 t.start();
806 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
807     t.interrupt();
808     t.join();
809 dl 1.1 }
810    
811     /**
812     * putFirst blocks waiting for take when full
813     */
814 jsr166 1.9 public void testPutFirstWithTake() throws InterruptedException {
815 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
816 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
817     public void realRun() {
818     int added = 0;
819     try {
820     q.putFirst(new Object());
821     ++added;
822     q.putFirst(new Object());
823     ++added;
824     q.putFirst(new Object());
825     ++added;
826     q.putFirst(new Object());
827     ++added;
828     threadShouldThrow();
829     } catch (InterruptedException success) {
830     threadAssertTrue(added >= 2);
831 dl 1.1 }
832 jsr166 1.11 }});
833 jsr166 1.9
834     t.start();
835     Thread.sleep(SHORT_DELAY_MS);
836     q.take();
837     t.interrupt();
838     t.join();
839 dl 1.1 }
840    
841     /**
842     * timed offerFirst times out if full and elements not taken
843     */
844 jsr166 1.9 public void testTimedOfferFirst() throws InterruptedException {
845 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
846 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
847     public void realRun() throws InterruptedException {
848     q.putFirst(new Object());
849     q.putFirst(new Object());
850     threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
851     q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
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     assertEquals(i, ((Integer)q.takeFirst()).intValue());
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     Thread t = new ThreadShouldThrow(InterruptedException.class) {
891     public void realRun() throws InterruptedException {
892     LinkedBlockingDeque q = populatedDeque(SIZE);
893     for (int i = 0; i < SIZE; ++i) {
894     assertEquals(i, ((Integer)q.takeFirst()).intValue());
895     }
896     q.takeFirst();
897     }};
898    
899 dl 1.1 t.start();
900 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
901     t.interrupt();
902     t.join();
903 dl 1.1 }
904    
905    
906     /**
907     * timed pollFirst with zero timeout succeeds when non-empty, else times out
908     */
909 jsr166 1.9 public void testTimedPollFirst0() throws InterruptedException {
910     LinkedBlockingDeque q = populatedDeque(SIZE);
911     for (int i = 0; i < SIZE; ++i) {
912     assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
913 jsr166 1.7 }
914 jsr166 1.9 assertNull(q.pollFirst(0, MILLISECONDS));
915 dl 1.1 }
916    
917     /**
918     * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
919     */
920 jsr166 1.9 public void testTimedPollFirst() throws InterruptedException {
921     LinkedBlockingDeque q = populatedDeque(SIZE);
922     for (int i = 0; i < SIZE; ++i) {
923     assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
924 jsr166 1.7 }
925 jsr166 1.9 assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
926 dl 1.1 }
927    
928     /**
929     * Interrupted timed pollFirst throws InterruptedException instead of
930     * returning timeout status
931     */
932 jsr166 1.9 public void testInterruptedTimedPollFirst() throws InterruptedException {
933     Thread t = new ThreadShouldThrow(InterruptedException.class) {
934     public void realRun() throws InterruptedException {
935     LinkedBlockingDeque q = populatedDeque(SIZE);
936     for (int i = 0; i < SIZE; ++i) {
937     threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
938     }
939     q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
940     }};
941    
942 dl 1.1 t.start();
943 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
944     t.interrupt();
945     t.join();
946 dl 1.1 }
947    
948     /**
949     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
950     * on interruption throws
951     */
952 jsr166 1.9 public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
953 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
954 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
955     public void realRun() throws InterruptedException {
956     threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
957     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
958     q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
959     }};
960    
961     t.start();
962     Thread.sleep(SMALL_DELAY_MS);
963     assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
964     t.interrupt();
965     t.join();
966 jsr166 1.4 }
967 dl 1.1
968     /**
969     * putLast(null) throws NPE
970     */
971 jsr166 1.9 public void testPutLastNull() throws InterruptedException {
972 jsr166 1.7 try {
973 dl 1.1 LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
974     q.putLast(null);
975     shouldThrow();
976 jsr166 1.9 } catch (NullPointerException success) {}
977 dl 1.1 }
978    
979     /**
980     * all elements successfully putLast are contained
981     */
982 jsr166 1.9 public void testPutLast() throws InterruptedException {
983     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
984     for (int i = 0; i < SIZE; ++i) {
985     Integer I = new Integer(i);
986     q.putLast(I);
987     assertTrue(q.contains(I));
988 dl 1.1 }
989 jsr166 1.9 assertEquals(0, q.remainingCapacity());
990 dl 1.1 }
991    
992     /**
993     * putLast blocks interruptibly if full
994     */
995 jsr166 1.9 public void testBlockingPutLast() throws InterruptedException {
996 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
997     public void realRun() {
998     int added = 0;
999     try {
1000     LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001     for (int i = 0; i < SIZE; ++i) {
1002     q.putLast(new Integer(i));
1003     ++added;
1004 jsr166 1.4 }
1005 jsr166 1.11 q.putLast(new Integer(SIZE));
1006     threadShouldThrow();
1007     } catch (InterruptedException success) {
1008     threadAssertEquals(added, SIZE);
1009     }
1010     }});
1011    
1012 dl 1.1 t.start();
1013 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1014     t.interrupt();
1015     t.join();
1016 dl 1.1 }
1017    
1018     /**
1019     * putLast blocks waiting for take when full
1020     */
1021 jsr166 1.9 public void testPutLastWithTake() throws InterruptedException {
1022 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1023 jsr166 1.11 Thread t = new Thread(new CheckedRunnable() {
1024     public void realRun() {
1025     int added = 0;
1026     try {
1027     q.putLast(new Object());
1028     ++added;
1029     q.putLast(new Object());
1030     ++added;
1031     q.putLast(new Object());
1032     ++added;
1033     q.putLast(new Object());
1034     ++added;
1035     threadShouldThrow();
1036     } catch (InterruptedException success) {
1037     threadAssertTrue(added >= 2);
1038 dl 1.1 }
1039 jsr166 1.11 }});
1040 jsr166 1.9
1041     t.start();
1042     Thread.sleep(SHORT_DELAY_MS);
1043     q.take();
1044     t.interrupt();
1045     t.join();
1046 dl 1.1 }
1047    
1048     /**
1049     * timed offerLast times out if full and elements not taken
1050     */
1051 jsr166 1.9 public void testTimedOfferLast() throws InterruptedException {
1052 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1053 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1054     public void realRun() throws InterruptedException {
1055     q.putLast(new Object());
1056     q.putLast(new Object());
1057     threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1058     q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1059     }};
1060 jsr166 1.4
1061 jsr166 1.9 t.start();
1062     Thread.sleep(SMALL_DELAY_MS);
1063     t.interrupt();
1064     t.join();
1065 dl 1.1 }
1066    
1067     /**
1068     * takeLast retrieves elements in FIFO order
1069     */
1070 jsr166 1.9 public void testTakeLast() throws InterruptedException {
1071     LinkedBlockingDeque q = populatedDeque(SIZE);
1072     for (int i = 0; i < SIZE; ++i) {
1073     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1074 jsr166 1.7 }
1075 dl 1.1 }
1076    
1077     /**
1078     * takeLast blocks interruptibly when empty
1079     */
1080 jsr166 1.9 public void testTakeLastFromEmpty() throws InterruptedException {
1081 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1082 jsr166 1.9 Thread t = new ThreadShouldThrow(InterruptedException.class) {
1083     public void realRun() throws InterruptedException {
1084     q.takeLast();
1085     }};
1086    
1087     t.start();
1088     Thread.sleep(SHORT_DELAY_MS);
1089     t.interrupt();
1090     t.join();
1091 dl 1.1 }
1092    
1093     /**
1094     * TakeLast removes existing elements until empty, then blocks interruptibly
1095     */
1096 jsr166 1.9 public void testBlockingTakeLast() throws InterruptedException {
1097     Thread t = new ThreadShouldThrow(InterruptedException.class) {
1098     public void realRun() throws InterruptedException {
1099     LinkedBlockingDeque q = populatedDeque(SIZE);
1100     for (int i = 0; i < SIZE; ++i) {
1101     assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1102     }
1103     q.takeLast();
1104     }};
1105    
1106 dl 1.1 t.start();
1107 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1108     t.interrupt();
1109     t.join();
1110 dl 1.1 }
1111    
1112    
1113     /**
1114     * timed pollLast with zero timeout succeeds when non-empty, else times out
1115     */
1116 jsr166 1.9 public void testTimedPollLast0() throws InterruptedException {
1117     LinkedBlockingDeque q = populatedDeque(SIZE);
1118     for (int i = 0; i < SIZE; ++i) {
1119     assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1120 jsr166 1.7 }
1121 jsr166 1.9 assertNull(q.pollLast(0, MILLISECONDS));
1122 dl 1.1 }
1123    
1124     /**
1125     * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1126     */
1127 jsr166 1.9 public void testTimedPollLast() throws InterruptedException {
1128     LinkedBlockingDeque q = populatedDeque(SIZE);
1129     for (int i = 0; i < SIZE; ++i) {
1130     assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1131 jsr166 1.7 }
1132 jsr166 1.9 assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1133 dl 1.1 }
1134    
1135     /**
1136     * Interrupted timed pollLast throws InterruptedException instead of
1137     * returning timeout status
1138     */
1139 jsr166 1.9 public void testInterruptedTimedPollLast() throws InterruptedException {
1140     Thread t = new ThreadShouldThrow(InterruptedException.class) {
1141     public void realRun() throws InterruptedException {
1142     LinkedBlockingDeque q = populatedDeque(SIZE);
1143     for (int i = 0; i < SIZE; ++i) {
1144     threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1145     }
1146     q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1147     }};
1148    
1149 dl 1.1 t.start();
1150 jsr166 1.9 Thread.sleep(SHORT_DELAY_MS);
1151     t.interrupt();
1152     t.join();
1153 dl 1.1 }
1154    
1155     /**
1156     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1157     * on interruption throws
1158     */
1159 jsr166 1.9 public void testTimedPollWithOfferLast() throws InterruptedException {
1160 dl 1.1 final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1161 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1162     public void realRun() throws InterruptedException {
1163     assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
1164     assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1165     try {
1166     q.poll(LONG_DELAY_MS, MILLISECONDS);
1167     shouldThrow();
1168     } catch (InterruptedException success) {}
1169     }});
1170    
1171     t.start();
1172     Thread.sleep(SMALL_DELAY_MS);
1173     assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1174     t.interrupt();
1175     t.join();
1176 jsr166 1.4 }
1177 dl 1.1
1178    
1179     /**
1180     * element returns next element, or throws NSEE if empty
1181     */
1182     public void testElement() {
1183     LinkedBlockingDeque q = populatedDeque(SIZE);
1184     for (int i = 0; i < SIZE; ++i) {
1185     assertEquals(i, ((Integer)q.element()).intValue());
1186     q.poll();
1187     }
1188     try {
1189     q.element();
1190     shouldThrow();
1191 jsr166 1.9 } catch (NoSuchElementException success) {}
1192 dl 1.1 }
1193    
1194     /**
1195     * remove(x) removes x and returns true if present
1196     */
1197     public void testRemoveElement() {
1198     LinkedBlockingDeque q = populatedDeque(SIZE);
1199     for (int i = 1; i < SIZE; i+=2) {
1200     assertTrue(q.remove(new Integer(i)));
1201     }
1202     for (int i = 0; i < SIZE; i+=2) {
1203     assertTrue(q.remove(new Integer(i)));
1204     assertFalse(q.remove(new Integer(i+1)));
1205     }
1206     assertTrue(q.isEmpty());
1207     }
1208 jsr166 1.4
1209 dl 1.1 /**
1210     * contains(x) reports true when elements added but not yet removed
1211     */
1212     public void testContains() {
1213     LinkedBlockingDeque q = populatedDeque(SIZE);
1214     for (int i = 0; i < SIZE; ++i) {
1215     assertTrue(q.contains(new Integer(i)));
1216     q.poll();
1217     assertFalse(q.contains(new Integer(i)));
1218     }
1219     }
1220    
1221     /**
1222     * clear removes all elements
1223     */
1224     public void testClear() {
1225     LinkedBlockingDeque q = populatedDeque(SIZE);
1226     q.clear();
1227     assertTrue(q.isEmpty());
1228     assertEquals(0, q.size());
1229     assertEquals(SIZE, q.remainingCapacity());
1230     q.add(one);
1231     assertFalse(q.isEmpty());
1232     assertTrue(q.contains(one));
1233     q.clear();
1234     assertTrue(q.isEmpty());
1235     }
1236    
1237     /**
1238     * containsAll(c) is true when c contains a subset of elements
1239     */
1240     public void testContainsAll() {
1241     LinkedBlockingDeque q = populatedDeque(SIZE);
1242     LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1243     for (int i = 0; i < SIZE; ++i) {
1244     assertTrue(q.containsAll(p));
1245     assertFalse(p.containsAll(q));
1246     p.add(new Integer(i));
1247     }
1248     assertTrue(p.containsAll(q));
1249     }
1250    
1251     /**
1252     * retainAll(c) retains only those elements of c and reports true if changed
1253     */
1254     public void testRetainAll() {
1255     LinkedBlockingDeque q = populatedDeque(SIZE);
1256     LinkedBlockingDeque p = populatedDeque(SIZE);
1257     for (int i = 0; i < SIZE; ++i) {
1258     boolean changed = q.retainAll(p);
1259     if (i == 0)
1260     assertFalse(changed);
1261     else
1262     assertTrue(changed);
1263    
1264     assertTrue(q.containsAll(p));
1265     assertEquals(SIZE-i, q.size());
1266     p.remove();
1267     }
1268     }
1269    
1270     /**
1271     * removeAll(c) removes only those elements of c and reports true if changed
1272     */
1273     public void testRemoveAll() {
1274     for (int i = 1; i < SIZE; ++i) {
1275     LinkedBlockingDeque q = populatedDeque(SIZE);
1276     LinkedBlockingDeque p = populatedDeque(i);
1277     assertTrue(q.removeAll(p));
1278     assertEquals(SIZE-i, q.size());
1279     for (int j = 0; j < i; ++j) {
1280     Integer I = (Integer)(p.remove());
1281     assertFalse(q.contains(I));
1282     }
1283     }
1284     }
1285    
1286     /**
1287     * toArray contains all elements
1288     */
1289 jsr166 1.9 public void testToArray() throws InterruptedException{
1290 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1291 jsr166 1.7 Object[] o = q.toArray();
1292     for (int i = 0; i < o.length; i++)
1293     assertEquals(o[i], q.take());
1294 dl 1.1 }
1295    
1296     /**
1297     * toArray(a) contains all elements
1298     */
1299 jsr166 1.9 public void testToArray2() throws InterruptedException {
1300 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1301 jsr166 1.7 Integer[] ints = new Integer[SIZE];
1302     ints = (Integer[])q.toArray(ints);
1303 jsr166 1.9 for (int i = 0; i < ints.length; i++)
1304     assertEquals(ints[i], q.take());
1305 dl 1.1 }
1306    
1307     /**
1308     * toArray(null) throws NPE
1309     */
1310     public void testToArray_BadArg() {
1311 jsr166 1.7 try {
1312 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1313 jsr166 1.7 Object o[] = q.toArray(null);
1314     shouldThrow();
1315     } catch (NullPointerException success) {}
1316 dl 1.1 }
1317    
1318     /**
1319     * toArray with incompatible array type throws CCE
1320     */
1321     public void testToArray1_BadArg() {
1322 jsr166 1.7 try {
1323 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1324 jsr166 1.7 Object o[] = q.toArray(new String[10] );
1325     shouldThrow();
1326 jsr166 1.9 } catch (ArrayStoreException success) {}
1327 dl 1.1 }
1328    
1329 jsr166 1.4
1330 dl 1.1 /**
1331     * iterator iterates through all elements
1332     */
1333 jsr166 1.9 public void testIterator() throws InterruptedException {
1334 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1335 jsr166 1.7 Iterator it = q.iterator();
1336 jsr166 1.9 while (it.hasNext()) {
1337     assertEquals(it.next(), q.take());
1338 jsr166 1.7 }
1339 dl 1.1 }
1340    
1341     /**
1342     * iterator.remove removes current element
1343     */
1344     public void testIteratorRemove () {
1345     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1346     q.add(two);
1347     q.add(one);
1348     q.add(three);
1349    
1350     Iterator it = q.iterator();
1351     it.next();
1352     it.remove();
1353 jsr166 1.4
1354 dl 1.1 it = q.iterator();
1355     assertEquals(it.next(), one);
1356     assertEquals(it.next(), three);
1357     assertFalse(it.hasNext());
1358     }
1359    
1360    
1361     /**
1362     * iterator ordering is FIFO
1363     */
1364     public void testIteratorOrdering() {
1365     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1366     q.add(one);
1367     q.add(two);
1368     q.add(three);
1369     assertEquals(0, q.remainingCapacity());
1370     int k = 0;
1371     for (Iterator it = q.iterator(); it.hasNext();) {
1372     int i = ((Integer)(it.next())).intValue();
1373     assertEquals(++k, i);
1374     }
1375     assertEquals(3, k);
1376     }
1377    
1378     /**
1379     * Modifications do not cause iterators to fail
1380     */
1381     public void testWeaklyConsistentIteration () {
1382     final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1383     q.add(one);
1384     q.add(two);
1385     q.add(three);
1386 jsr166 1.9 for (Iterator it = q.iterator(); it.hasNext();) {
1387     q.remove();
1388     it.next();
1389 dl 1.1 }
1390     assertEquals(0, q.size());
1391     }
1392    
1393    
1394     /**
1395 dl 1.2 * Descending iterator iterates through all elements
1396     */
1397     public void testDescendingIterator() {
1398     LinkedBlockingDeque q = populatedDeque(SIZE);
1399     int i = 0;
1400 jsr166 1.7 Iterator it = q.descendingIterator();
1401 jsr166 1.5 while (it.hasNext()) {
1402 dl 1.2 assertTrue(q.contains(it.next()));
1403     ++i;
1404     }
1405     assertEquals(i, SIZE);
1406     assertFalse(it.hasNext());
1407     try {
1408     it.next();
1409 jsr166 1.9 shouldThrow();
1410     } catch (NoSuchElementException success) {}
1411 dl 1.2 }
1412    
1413     /**
1414     * Descending iterator ordering is reverse FIFO
1415     */
1416     public void testDescendingIteratorOrdering() {
1417     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1418 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1419     q.add(new Integer(3));
1420     q.add(new Integer(2));
1421     q.add(new Integer(1));
1422     int k = 0;
1423     for (Iterator it = q.descendingIterator(); it.hasNext();) {
1424     int i = ((Integer)(it.next())).intValue();
1425     assertEquals(++k, i);
1426     }
1427 jsr166 1.4
1428 dl 1.3 assertEquals(3, k);
1429     q.remove();
1430     q.remove();
1431     q.remove();
1432 dl 1.2 }
1433     }
1434    
1435     /**
1436     * descendingIterator.remove removes current element
1437     */
1438     public void testDescendingIteratorRemove () {
1439     final LinkedBlockingDeque q = new LinkedBlockingDeque();
1440 dl 1.3 for (int iters = 0; iters < 100; ++iters) {
1441     q.add(new Integer(3));
1442     q.add(new Integer(2));
1443     q.add(new Integer(1));
1444     Iterator it = q.descendingIterator();
1445     assertEquals(it.next(), new Integer(1));
1446     it.remove();
1447     assertEquals(it.next(), new Integer(2));
1448     it = q.descendingIterator();
1449     assertEquals(it.next(), new Integer(2));
1450     assertEquals(it.next(), new Integer(3));
1451     it.remove();
1452     assertFalse(it.hasNext());
1453     q.remove();
1454     }
1455 dl 1.2 }
1456    
1457    
1458     /**
1459 dl 1.1 * toString contains toStrings of elements
1460     */
1461     public void testToString() {
1462     LinkedBlockingDeque q = populatedDeque(SIZE);
1463     String s = q.toString();
1464     for (int i = 0; i < SIZE; ++i) {
1465     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1466     }
1467 jsr166 1.4 }
1468 dl 1.1
1469    
1470     /**
1471     * offer transfers elements across Executor tasks
1472     */
1473     public void testOfferInExecutor() {
1474     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1475     q.add(one);
1476     q.add(two);
1477     ExecutorService executor = Executors.newFixedThreadPool(2);
1478 jsr166 1.9 executor.execute(new CheckedRunnable() {
1479     public void realRun() throws InterruptedException {
1480 dl 1.1 threadAssertFalse(q.offer(three));
1481 jsr166 1.9 threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1482     threadAssertEquals(0, q.remainingCapacity());
1483     }});
1484    
1485     executor.execute(new CheckedRunnable() {
1486     public void realRun() throws InterruptedException {
1487     Thread.sleep(SMALL_DELAY_MS);
1488     threadAssertEquals(one, q.take());
1489     }});
1490 jsr166 1.4
1491 dl 1.1 joinPool(executor);
1492     }
1493    
1494     /**
1495     * poll retrieves elements across Executor threads
1496     */
1497     public void testPollInExecutor() {
1498     final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1499     ExecutorService executor = Executors.newFixedThreadPool(2);
1500 jsr166 1.9 executor.execute(new CheckedRunnable() {
1501     public void realRun() throws InterruptedException {
1502 dl 1.1 threadAssertNull(q.poll());
1503 jsr166 1.9 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1504     threadAssertTrue(q.isEmpty());
1505     }});
1506    
1507     executor.execute(new CheckedRunnable() {
1508     public void realRun() throws InterruptedException {
1509     Thread.sleep(SMALL_DELAY_MS);
1510     q.put(one);
1511     }});
1512 jsr166 1.4
1513 dl 1.1 joinPool(executor);
1514     }
1515    
1516     /**
1517     * A deserialized serialized deque has same elements in same order
1518     */
1519 jsr166 1.9 public void testSerialization() throws Exception {
1520 dl 1.1 LinkedBlockingDeque q = populatedDeque(SIZE);
1521    
1522 jsr166 1.9 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1523     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1524     out.writeObject(q);
1525     out.close();
1526    
1527     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1528     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1529     LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1530     assertEquals(q.size(), r.size());
1531     while (!q.isEmpty())
1532     assertEquals(q.remove(), r.remove());
1533 dl 1.1 }
1534    
1535     /**
1536     * drainTo(null) throws NPE
1537 jsr166 1.4 */
1538 dl 1.1 public void testDrainToNull() {
1539     LinkedBlockingDeque q = populatedDeque(SIZE);
1540     try {
1541     q.drainTo(null);
1542     shouldThrow();
1543 jsr166 1.9 } catch (NullPointerException success) {}
1544 dl 1.1 }
1545    
1546     /**
1547     * drainTo(this) throws IAE
1548 jsr166 1.4 */
1549 dl 1.1 public void testDrainToSelf() {
1550     LinkedBlockingDeque q = populatedDeque(SIZE);
1551     try {
1552     q.drainTo(q);
1553     shouldThrow();
1554 jsr166 1.10 } catch (IllegalArgumentException success) {}
1555 dl 1.1 }
1556    
1557     /**
1558     * drainTo(c) empties deque into another collection c
1559 jsr166 1.4 */
1560 dl 1.1 public void testDrainTo() {
1561     LinkedBlockingDeque q = populatedDeque(SIZE);
1562     ArrayList l = new ArrayList();
1563     q.drainTo(l);
1564     assertEquals(q.size(), 0);
1565     assertEquals(l.size(), SIZE);
1566 jsr166 1.4 for (int i = 0; i < SIZE; ++i)
1567 dl 1.1 assertEquals(l.get(i), new Integer(i));
1568     q.add(zero);
1569     q.add(one);
1570     assertFalse(q.isEmpty());
1571     assertTrue(q.contains(zero));
1572     assertTrue(q.contains(one));
1573     l.clear();
1574     q.drainTo(l);
1575     assertEquals(q.size(), 0);
1576     assertEquals(l.size(), 2);
1577 jsr166 1.4 for (int i = 0; i < 2; ++i)
1578 dl 1.1 assertEquals(l.get(i), new Integer(i));
1579     }
1580    
1581     /**
1582     * drainTo empties full deque, unblocking a waiting put.
1583 jsr166 1.4 */
1584 jsr166 1.9 public void testDrainToWithActivePut() throws InterruptedException {
1585 dl 1.1 final LinkedBlockingDeque q = populatedDeque(SIZE);
1586 jsr166 1.9 Thread t = new Thread(new CheckedRunnable() {
1587     public void realRun() throws InterruptedException {
1588     q.put(new Integer(SIZE+1));
1589     }});
1590    
1591     t.start();
1592     ArrayList l = new ArrayList();
1593     q.drainTo(l);
1594     assertTrue(l.size() >= SIZE);
1595     for (int i = 0; i < SIZE; ++i)
1596     assertEquals(l.get(i), new Integer(i));
1597     t.join();
1598     assertTrue(q.size() + l.size() >= SIZE);
1599 dl 1.1 }
1600    
1601     /**
1602     * drainTo(null, n) throws NPE
1603 jsr166 1.4 */
1604 dl 1.1 public void testDrainToNullN() {
1605     LinkedBlockingDeque q = populatedDeque(SIZE);
1606     try {
1607     q.drainTo(null, 0);
1608     shouldThrow();
1609 jsr166 1.9 } catch (NullPointerException success) {}
1610 dl 1.1 }
1611    
1612     /**
1613     * drainTo(this, n) throws IAE
1614 jsr166 1.4 */
1615 dl 1.1 public void testDrainToSelfN() {
1616     LinkedBlockingDeque q = populatedDeque(SIZE);
1617     try {
1618     q.drainTo(q, 0);
1619     shouldThrow();
1620 jsr166 1.9 } catch (IllegalArgumentException success) {}
1621 dl 1.1 }
1622    
1623     /**
1624     * drainTo(c, n) empties first max {n, size} elements of deque into c
1625 jsr166 1.4 */
1626 dl 1.1 public void testDrainToN() {
1627     LinkedBlockingDeque q = new LinkedBlockingDeque();
1628     for (int i = 0; i < SIZE + 2; ++i) {
1629 jsr166 1.5 for (int j = 0; j < SIZE; j++)
1630 dl 1.1 assertTrue(q.offer(new Integer(j)));
1631     ArrayList l = new ArrayList();
1632     q.drainTo(l, i);
1633     int k = (i < SIZE)? i : SIZE;
1634     assertEquals(l.size(), k);
1635     assertEquals(q.size(), SIZE-k);
1636 jsr166 1.4 for (int j = 0; j < k; ++j)
1637 dl 1.1 assertEquals(l.get(j), new Integer(j));
1638     while (q.poll() != null) ;
1639     }
1640     }
1641    
1642     }