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