ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.33
Committed: Thu Nov 18 20:21:53 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.32: +9 -3 lines
Log Message:
add more assertions to testRemoveElement

File Contents

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