ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
Revision: 1.19
Committed: Tue Dec 1 06:03:49 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.18: +7 -7 lines
Log Message:
Use MILLISECONDS.toNanos instead of multiplying by 1000*1000; use explicit assertEquals instead of assertTrue(...!= null); improve testPutWithTake

File Contents

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