ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.22
Committed: Wed Aug 25 01:44:48 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +6 -4 lines
Log Message:
whitespace

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