11 |
|
|
12 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
|
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
18 |
> |
return new TestSuite(LinkedBlockingDequeTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
/** |
25 |
|
private LinkedBlockingDeque populatedDeque(int n) { |
26 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
27 |
|
assertTrue(q.isEmpty()); |
28 |
< |
for(int i = 0; i < n; i++) |
29 |
< |
assertTrue(q.offer(new Integer(i))); |
28 |
> |
for (int i = 0; i < n; i++) |
29 |
> |
assertTrue(q.offer(new Integer(i))); |
30 |
|
assertFalse(q.isEmpty()); |
31 |
|
assertEquals(0, q.remainingCapacity()); |
32 |
< |
assertEquals(n, q.size()); |
32 |
> |
assertEquals(n, q.size()); |
33 |
|
return q; |
34 |
|
} |
35 |
|
|
66 |
|
* offer(null) throws NPE |
67 |
|
*/ |
68 |
|
public void testOfferFirstNull() { |
69 |
< |
try { |
69 |
> |
try { |
70 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
71 |
|
q.offerFirst(null); |
72 |
|
shouldThrow(); |
73 |
< |
} catch (NullPointerException success) { |
74 |
< |
} |
73 |
> |
} catch (NullPointerException success) { |
74 |
> |
} |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* OfferFirst succeeds |
78 |
> |
* OfferFirst succeeds |
79 |
|
*/ |
80 |
|
public void testOfferFirst() { |
81 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* OfferLast succeeds |
87 |
> |
* OfferLast succeeds |
88 |
|
*/ |
89 |
|
public void testOfferLast() { |
90 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
100 |
|
for (int i = 0; i < SIZE; ++i) { |
101 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
102 |
|
} |
103 |
< |
assertNull(q.pollFirst()); |
103 |
> |
assertNull(q.pollFirst()); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
111 |
|
for (int i = SIZE-1; i >= 0; --i) { |
112 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
113 |
|
} |
114 |
< |
assertNull(q.pollLast()); |
114 |
> |
assertNull(q.pollLast()); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
125 |
|
assertTrue(q.peekFirst() == null || |
126 |
|
i != ((Integer)q.peekFirst()).intValue()); |
127 |
|
} |
128 |
< |
assertNull(q.peekFirst()); |
128 |
> |
assertNull(q.peekFirst()); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
139 |
|
assertTrue(q.peek() == null || |
140 |
|
i != ((Integer)q.peek()).intValue()); |
141 |
|
} |
142 |
< |
assertNull(q.peek()); |
142 |
> |
assertNull(q.peek()); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
153 |
|
assertTrue(q.peekLast() == null || |
154 |
|
i != ((Integer)q.peekLast()).intValue()); |
155 |
|
} |
156 |
< |
assertNull(q.peekLast()); |
156 |
> |
assertNull(q.peekLast()); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
186 |
|
shouldThrow(); |
187 |
|
} |
188 |
|
catch (NoSuchElementException success) {} |
189 |
< |
assertNull(q.peekLast()); |
189 |
> |
assertNull(q.peekLast()); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
200 |
|
try { |
201 |
|
q.removeFirst(); |
202 |
|
shouldThrow(); |
203 |
< |
} catch (NoSuchElementException success){ |
204 |
< |
} |
203 |
> |
} catch (NoSuchElementException success) { |
204 |
> |
} |
205 |
|
} |
206 |
|
|
207 |
|
/** |
215 |
|
try { |
216 |
|
q.remove(); |
217 |
|
shouldThrow(); |
218 |
< |
} catch (NoSuchElementException success){ |
219 |
< |
} |
218 |
> |
} catch (NoSuchElementException success) { |
219 |
> |
} |
220 |
|
} |
221 |
|
|
222 |
|
/** |
255 |
|
public void testAddFirst() { |
256 |
|
LinkedBlockingDeque q = populatedDeque(3); |
257 |
|
q.pollLast(); |
258 |
< |
q.addFirst(four); |
259 |
< |
assertEquals(four,q.peekFirst()); |
260 |
< |
} |
258 |
> |
q.addFirst(four); |
259 |
> |
assertEquals(four,q.peekFirst()); |
260 |
> |
} |
261 |
|
|
262 |
|
/** |
263 |
|
* peekLast returns element inserted with addLast |
265 |
|
public void testAddLast() { |
266 |
|
LinkedBlockingDeque q = populatedDeque(3); |
267 |
|
q.pollLast(); |
268 |
< |
q.addLast(four); |
269 |
< |
assertEquals(four,q.peekLast()); |
270 |
< |
} |
268 |
> |
q.addLast(four); |
269 |
> |
assertEquals(four,q.peekLast()); |
270 |
> |
} |
271 |
|
|
272 |
|
|
273 |
|
/** |
378 |
|
* offer(null) throws NPE |
379 |
|
*/ |
380 |
|
public void testOfferNull() { |
381 |
< |
try { |
381 |
> |
try { |
382 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
383 |
|
q.offer(null); |
384 |
|
shouldThrow(); |
385 |
< |
} catch (NullPointerException success) { } |
385 |
> |
} catch (NullPointerException success) { } |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
|
* add(null) throws NPE |
390 |
|
*/ |
391 |
|
public void testAddNull() { |
392 |
< |
try { |
392 |
> |
try { |
393 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
394 |
|
q.add(null); |
395 |
|
shouldThrow(); |
396 |
< |
} catch (NullPointerException success) { } |
396 |
> |
} catch (NullPointerException success) { } |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
|
* push(null) throws NPE |
401 |
|
*/ |
402 |
|
public void testPushNull() { |
403 |
< |
try { |
403 |
> |
try { |
404 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
405 |
|
q.push(null); |
406 |
|
shouldThrow(); |
407 |
< |
} catch (NullPointerException success) { } |
407 |
> |
} catch (NullPointerException success) { } |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
|
* push succeeds if not full; throws ISE if full |
412 |
|
*/ |
413 |
|
public void testPush() { |
414 |
< |
try { |
414 |
> |
try { |
415 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
416 |
|
for (int i = 0; i < SIZE; ++i) { |
417 |
|
Integer I = new Integer(i); |
420 |
|
} |
421 |
|
assertEquals(0, q.remainingCapacity()); |
422 |
|
q.push(new Integer(SIZE)); |
423 |
< |
} catch (IllegalStateException success){ |
424 |
< |
} |
423 |
> |
} catch (IllegalStateException success) { |
424 |
> |
} |
425 |
|
} |
426 |
|
|
427 |
|
/** |
430 |
|
public void testPushWithPeek() { |
431 |
|
LinkedBlockingDeque q = populatedDeque(3); |
432 |
|
q.pollLast(); |
433 |
< |
q.push(four); |
434 |
< |
assertEquals(four,q.peekFirst()); |
435 |
< |
} |
433 |
> |
q.push(four); |
434 |
> |
assertEquals(four,q.peekFirst()); |
435 |
> |
} |
436 |
|
|
437 |
|
|
438 |
|
/** |
446 |
|
try { |
447 |
|
q.pop(); |
448 |
|
shouldThrow(); |
449 |
< |
} catch (NoSuchElementException success){ |
450 |
< |
} |
449 |
> |
} catch (NoSuchElementException success) { |
450 |
> |
} |
451 |
|
} |
452 |
|
|
453 |
|
|
464 |
|
* add succeeds if not full; throws ISE if full |
465 |
|
*/ |
466 |
|
public void testAdd() { |
467 |
< |
try { |
467 |
> |
try { |
468 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
469 |
|
for (int i = 0; i < SIZE; ++i) { |
470 |
|
assertTrue(q.add(new Integer(i))); |
471 |
|
} |
472 |
|
assertEquals(0, q.remainingCapacity()); |
473 |
|
q.add(new Integer(SIZE)); |
474 |
< |
} catch (IllegalStateException success){ |
475 |
< |
} |
474 |
> |
} catch (IllegalStateException success) { |
475 |
> |
} |
476 |
|
} |
477 |
|
|
478 |
|
/** |
563 |
|
* put(null) throws NPE |
564 |
|
*/ |
565 |
|
public void testPutNull() { |
566 |
< |
try { |
566 |
> |
try { |
567 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
568 |
|
q.put(null); |
569 |
|
shouldThrow(); |
570 |
< |
} |
571 |
< |
catch (NullPointerException success){ |
572 |
< |
} |
570 |
> |
} |
571 |
> |
catch (NullPointerException success) { |
572 |
> |
} |
573 |
|
catch (InterruptedException ie) { |
574 |
< |
unexpectedException(); |
574 |
> |
unexpectedException(); |
575 |
|
} |
576 |
|
} |
577 |
|
|
589 |
|
assertEquals(0, q.remainingCapacity()); |
590 |
|
} |
591 |
|
catch (InterruptedException ie) { |
592 |
< |
unexpectedException(); |
592 |
> |
unexpectedException(); |
593 |
|
} |
594 |
|
} |
595 |
|
|
608 |
|
} |
609 |
|
q.put(new Integer(SIZE)); |
610 |
|
threadShouldThrow(); |
611 |
< |
} catch (InterruptedException ie){ |
611 |
> |
} catch (InterruptedException ie) { |
612 |
|
threadAssertEquals(added, SIZE); |
613 |
< |
} |
613 |
> |
} |
614 |
|
}}); |
615 |
|
t.start(); |
616 |
< |
try { |
617 |
< |
Thread.sleep(SHORT_DELAY_MS); |
616 |
> |
try { |
617 |
> |
Thread.sleep(SHORT_DELAY_MS); |
618 |
|
t.interrupt(); |
619 |
|
t.join(); |
620 |
|
} |
621 |
|
catch (InterruptedException ie) { |
622 |
< |
unexpectedException(); |
622 |
> |
unexpectedException(); |
623 |
|
} |
624 |
|
} |
625 |
|
|
640 |
|
++added; |
641 |
|
q.put(new Object()); |
642 |
|
++added; |
643 |
< |
threadShouldThrow(); |
644 |
< |
} catch (InterruptedException e){ |
643 |
> |
threadShouldThrow(); |
644 |
> |
} catch (InterruptedException e) { |
645 |
|
threadAssertTrue(added >= 2); |
646 |
|
} |
647 |
|
} |
652 |
|
q.take(); |
653 |
|
t.interrupt(); |
654 |
|
t.join(); |
655 |
< |
} catch (Exception e){ |
655 |
> |
} catch (Exception e) { |
656 |
|
unexpectedException(); |
657 |
|
} |
658 |
|
} |
669 |
|
q.put(new Object()); |
670 |
|
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
671 |
|
q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
672 |
< |
threadShouldThrow(); |
673 |
< |
} catch (InterruptedException success){} |
672 |
> |
threadShouldThrow(); |
673 |
> |
} catch (InterruptedException success) {} |
674 |
|
} |
675 |
|
}); |
676 |
< |
|
676 |
> |
|
677 |
|
try { |
678 |
|
t.start(); |
679 |
|
Thread.sleep(SMALL_DELAY_MS); |
680 |
|
t.interrupt(); |
681 |
|
t.join(); |
682 |
< |
} catch (Exception e){ |
682 |
> |
} catch (Exception e) { |
683 |
|
unexpectedException(); |
684 |
|
} |
685 |
|
} |
688 |
|
* take retrieves elements in FIFO order |
689 |
|
*/ |
690 |
|
public void testTake() { |
691 |
< |
try { |
691 |
> |
try { |
692 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
693 |
|
for (int i = 0; i < SIZE; ++i) { |
694 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
695 |
|
} |
696 |
< |
} catch (InterruptedException e){ |
697 |
< |
unexpectedException(); |
698 |
< |
} |
696 |
> |
} catch (InterruptedException e) { |
697 |
> |
unexpectedException(); |
698 |
> |
} |
699 |
|
} |
700 |
|
|
701 |
|
/** |
707 |
|
public void run() { |
708 |
|
try { |
709 |
|
q.take(); |
710 |
< |
threadShouldThrow(); |
711 |
< |
} catch (InterruptedException success){ } |
710 |
> |
threadShouldThrow(); |
711 |
> |
} catch (InterruptedException success) { } |
712 |
|
} |
713 |
|
}); |
714 |
|
try { |
716 |
|
Thread.sleep(SHORT_DELAY_MS); |
717 |
|
t.interrupt(); |
718 |
|
t.join(); |
719 |
< |
} catch (Exception e){ |
719 |
> |
} catch (Exception e) { |
720 |
|
unexpectedException(); |
721 |
|
} |
722 |
|
} |
734 |
|
} |
735 |
|
q.take(); |
736 |
|
threadShouldThrow(); |
737 |
< |
} catch (InterruptedException success){ |
738 |
< |
} |
737 |
> |
} catch (InterruptedException success) { |
738 |
> |
} |
739 |
|
}}); |
740 |
|
t.start(); |
741 |
< |
try { |
742 |
< |
Thread.sleep(SHORT_DELAY_MS); |
741 |
> |
try { |
742 |
> |
Thread.sleep(SHORT_DELAY_MS); |
743 |
|
t.interrupt(); |
744 |
|
t.join(); |
745 |
|
} |
746 |
|
catch (InterruptedException ie) { |
747 |
< |
unexpectedException(); |
747 |
> |
unexpectedException(); |
748 |
|
} |
749 |
|
} |
750 |
|
|
757 |
|
for (int i = 0; i < SIZE; ++i) { |
758 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
759 |
|
} |
760 |
< |
assertNull(q.poll()); |
760 |
> |
assertNull(q.poll()); |
761 |
|
} |
762 |
|
|
763 |
|
/** |
770 |
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
771 |
|
} |
772 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
773 |
< |
} catch (InterruptedException e){ |
774 |
< |
unexpectedException(); |
775 |
< |
} |
773 |
> |
} catch (InterruptedException e) { |
774 |
> |
unexpectedException(); |
775 |
> |
} |
776 |
|
} |
777 |
|
|
778 |
|
/** |
785 |
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
786 |
|
} |
787 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
788 |
< |
} catch (InterruptedException e){ |
789 |
< |
unexpectedException(); |
790 |
< |
} |
788 |
> |
} catch (InterruptedException e) { |
789 |
> |
unexpectedException(); |
790 |
> |
} |
791 |
|
} |
792 |
|
|
793 |
|
/** |
803 |
|
threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
804 |
|
} |
805 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
806 |
< |
} catch (InterruptedException success){ |
807 |
< |
} |
806 |
> |
} catch (InterruptedException success) { |
807 |
> |
} |
808 |
|
}}); |
809 |
|
t.start(); |
810 |
< |
try { |
811 |
< |
Thread.sleep(SHORT_DELAY_MS); |
810 |
> |
try { |
811 |
> |
Thread.sleep(SHORT_DELAY_MS); |
812 |
|
t.interrupt(); |
813 |
|
t.join(); |
814 |
|
} |
815 |
|
catch (InterruptedException ie) { |
816 |
< |
unexpectedException(); |
816 |
> |
unexpectedException(); |
817 |
|
} |
818 |
|
} |
819 |
|
|
829 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
830 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
831 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
832 |
< |
threadShouldThrow(); |
833 |
< |
} catch (InterruptedException success) { } |
832 |
> |
threadShouldThrow(); |
833 |
> |
} catch (InterruptedException success) { } |
834 |
|
} |
835 |
|
}); |
836 |
|
try { |
839 |
|
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
840 |
|
t.interrupt(); |
841 |
|
t.join(); |
842 |
< |
} catch (Exception e){ |
842 |
> |
} catch (Exception e) { |
843 |
|
unexpectedException(); |
844 |
|
} |
845 |
< |
} |
845 |
> |
} |
846 |
|
|
847 |
|
|
848 |
|
/** |
849 |
|
* putFirst(null) throws NPE |
850 |
|
*/ |
851 |
|
public void testPutFirstNull() { |
852 |
< |
try { |
852 |
> |
try { |
853 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
854 |
|
q.putFirst(null); |
855 |
|
shouldThrow(); |
856 |
< |
} |
857 |
< |
catch (NullPointerException success){ |
858 |
< |
} |
856 |
> |
} |
857 |
> |
catch (NullPointerException success) { |
858 |
> |
} |
859 |
|
catch (InterruptedException ie) { |
860 |
< |
unexpectedException(); |
860 |
> |
unexpectedException(); |
861 |
|
} |
862 |
|
} |
863 |
|
|
875 |
|
assertEquals(0, q.remainingCapacity()); |
876 |
|
} |
877 |
|
catch (InterruptedException ie) { |
878 |
< |
unexpectedException(); |
878 |
> |
unexpectedException(); |
879 |
|
} |
880 |
|
} |
881 |
|
|
894 |
|
} |
895 |
|
q.putFirst(new Integer(SIZE)); |
896 |
|
threadShouldThrow(); |
897 |
< |
} catch (InterruptedException ie){ |
897 |
> |
} catch (InterruptedException ie) { |
898 |
|
threadAssertEquals(added, SIZE); |
899 |
< |
} |
899 |
> |
} |
900 |
|
}}); |
901 |
|
t.start(); |
902 |
< |
try { |
903 |
< |
Thread.sleep(SHORT_DELAY_MS); |
902 |
> |
try { |
903 |
> |
Thread.sleep(SHORT_DELAY_MS); |
904 |
|
t.interrupt(); |
905 |
|
t.join(); |
906 |
|
} |
907 |
|
catch (InterruptedException ie) { |
908 |
< |
unexpectedException(); |
908 |
> |
unexpectedException(); |
909 |
|
} |
910 |
|
} |
911 |
|
|
926 |
|
++added; |
927 |
|
q.putFirst(new Object()); |
928 |
|
++added; |
929 |
< |
threadShouldThrow(); |
930 |
< |
} catch (InterruptedException e){ |
929 |
> |
threadShouldThrow(); |
930 |
> |
} catch (InterruptedException e) { |
931 |
|
threadAssertTrue(added >= 2); |
932 |
|
} |
933 |
|
} |
938 |
|
q.take(); |
939 |
|
t.interrupt(); |
940 |
|
t.join(); |
941 |
< |
} catch (Exception e){ |
941 |
> |
} catch (Exception e) { |
942 |
|
unexpectedException(); |
943 |
|
} |
944 |
|
} |
955 |
|
q.putFirst(new Object()); |
956 |
|
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
957 |
|
q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
958 |
< |
threadShouldThrow(); |
959 |
< |
} catch (InterruptedException success){} |
958 |
> |
threadShouldThrow(); |
959 |
> |
} catch (InterruptedException success) {} |
960 |
|
} |
961 |
|
}); |
962 |
< |
|
962 |
> |
|
963 |
|
try { |
964 |
|
t.start(); |
965 |
|
Thread.sleep(SMALL_DELAY_MS); |
966 |
|
t.interrupt(); |
967 |
|
t.join(); |
968 |
< |
} catch (Exception e){ |
968 |
> |
} catch (Exception e) { |
969 |
|
unexpectedException(); |
970 |
|
} |
971 |
|
} |
974 |
|
* take retrieves elements in FIFO order |
975 |
|
*/ |
976 |
|
public void testTakeFirst() { |
977 |
< |
try { |
977 |
> |
try { |
978 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
979 |
|
for (int i = 0; i < SIZE; ++i) { |
980 |
|
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
981 |
|
} |
982 |
< |
} catch (InterruptedException e){ |
983 |
< |
unexpectedException(); |
984 |
< |
} |
982 |
> |
} catch (InterruptedException e) { |
983 |
> |
unexpectedException(); |
984 |
> |
} |
985 |
|
} |
986 |
|
|
987 |
|
/** |
993 |
|
public void run() { |
994 |
|
try { |
995 |
|
q.takeFirst(); |
996 |
< |
threadShouldThrow(); |
997 |
< |
} catch (InterruptedException success){ } |
996 |
> |
threadShouldThrow(); |
997 |
> |
} catch (InterruptedException success) { } |
998 |
|
} |
999 |
|
}); |
1000 |
|
try { |
1002 |
|
Thread.sleep(SHORT_DELAY_MS); |
1003 |
|
t.interrupt(); |
1004 |
|
t.join(); |
1005 |
< |
} catch (Exception e){ |
1005 |
> |
} catch (Exception e) { |
1006 |
|
unexpectedException(); |
1007 |
|
} |
1008 |
|
} |
1020 |
|
} |
1021 |
|
q.takeFirst(); |
1022 |
|
threadShouldThrow(); |
1023 |
< |
} catch (InterruptedException success){ |
1024 |
< |
} |
1023 |
> |
} catch (InterruptedException success) { |
1024 |
> |
} |
1025 |
|
}}); |
1026 |
|
t.start(); |
1027 |
< |
try { |
1028 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1027 |
> |
try { |
1028 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1029 |
|
t.interrupt(); |
1030 |
|
t.join(); |
1031 |
|
} |
1032 |
|
catch (InterruptedException ie) { |
1033 |
< |
unexpectedException(); |
1033 |
> |
unexpectedException(); |
1034 |
|
} |
1035 |
|
} |
1036 |
|
|
1045 |
|
assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue()); |
1046 |
|
} |
1047 |
|
assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS)); |
1048 |
< |
} catch (InterruptedException e){ |
1049 |
< |
unexpectedException(); |
1050 |
< |
} |
1048 |
> |
} catch (InterruptedException e) { |
1049 |
> |
unexpectedException(); |
1050 |
> |
} |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1060 |
|
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1061 |
|
} |
1062 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1063 |
< |
} catch (InterruptedException e){ |
1064 |
< |
unexpectedException(); |
1065 |
< |
} |
1063 |
> |
} catch (InterruptedException e) { |
1064 |
> |
unexpectedException(); |
1065 |
> |
} |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1078 |
|
threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1079 |
|
} |
1080 |
|
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1081 |
< |
} catch (InterruptedException success){ |
1082 |
< |
} |
1081 |
> |
} catch (InterruptedException success) { |
1082 |
> |
} |
1083 |
|
}}); |
1084 |
|
t.start(); |
1085 |
< |
try { |
1086 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1085 |
> |
try { |
1086 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1087 |
|
t.interrupt(); |
1088 |
|
t.join(); |
1089 |
|
} |
1090 |
|
catch (InterruptedException ie) { |
1091 |
< |
unexpectedException(); |
1091 |
> |
unexpectedException(); |
1092 |
|
} |
1093 |
|
} |
1094 |
|
|
1104 |
|
threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1105 |
|
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1106 |
|
q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1107 |
< |
threadShouldThrow(); |
1108 |
< |
} catch (InterruptedException success) { } |
1107 |
> |
threadShouldThrow(); |
1108 |
> |
} catch (InterruptedException success) { } |
1109 |
|
} |
1110 |
|
}); |
1111 |
|
try { |
1114 |
|
assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1115 |
|
t.interrupt(); |
1116 |
|
t.join(); |
1117 |
< |
} catch (Exception e){ |
1117 |
> |
} catch (Exception e) { |
1118 |
|
unexpectedException(); |
1119 |
|
} |
1120 |
< |
} |
1120 |
> |
} |
1121 |
|
|
1122 |
|
/** |
1123 |
|
* putLast(null) throws NPE |
1124 |
|
*/ |
1125 |
|
public void testPutLastNull() { |
1126 |
< |
try { |
1126 |
> |
try { |
1127 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1128 |
|
q.putLast(null); |
1129 |
|
shouldThrow(); |
1130 |
< |
} |
1131 |
< |
catch (NullPointerException success){ |
1132 |
< |
} |
1130 |
> |
} |
1131 |
> |
catch (NullPointerException success) { |
1132 |
> |
} |
1133 |
|
catch (InterruptedException ie) { |
1134 |
< |
unexpectedException(); |
1134 |
> |
unexpectedException(); |
1135 |
|
} |
1136 |
|
} |
1137 |
|
|
1149 |
|
assertEquals(0, q.remainingCapacity()); |
1150 |
|
} |
1151 |
|
catch (InterruptedException ie) { |
1152 |
< |
unexpectedException(); |
1152 |
> |
unexpectedException(); |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1168 |
|
} |
1169 |
|
q.putLast(new Integer(SIZE)); |
1170 |
|
threadShouldThrow(); |
1171 |
< |
} catch (InterruptedException ie){ |
1171 |
> |
} catch (InterruptedException ie) { |
1172 |
|
threadAssertEquals(added, SIZE); |
1173 |
< |
} |
1173 |
> |
} |
1174 |
|
}}); |
1175 |
|
t.start(); |
1176 |
< |
try { |
1177 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1176 |
> |
try { |
1177 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1178 |
|
t.interrupt(); |
1179 |
|
t.join(); |
1180 |
|
} |
1181 |
|
catch (InterruptedException ie) { |
1182 |
< |
unexpectedException(); |
1182 |
> |
unexpectedException(); |
1183 |
|
} |
1184 |
|
} |
1185 |
|
|
1200 |
|
++added; |
1201 |
|
q.putLast(new Object()); |
1202 |
|
++added; |
1203 |
< |
threadShouldThrow(); |
1204 |
< |
} catch (InterruptedException e){ |
1203 |
> |
threadShouldThrow(); |
1204 |
> |
} catch (InterruptedException e) { |
1205 |
|
threadAssertTrue(added >= 2); |
1206 |
|
} |
1207 |
|
} |
1212 |
|
q.take(); |
1213 |
|
t.interrupt(); |
1214 |
|
t.join(); |
1215 |
< |
} catch (Exception e){ |
1215 |
> |
} catch (Exception e) { |
1216 |
|
unexpectedException(); |
1217 |
|
} |
1218 |
|
} |
1229 |
|
q.putLast(new Object()); |
1230 |
|
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1231 |
|
q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1232 |
< |
threadShouldThrow(); |
1233 |
< |
} catch (InterruptedException success){} |
1232 |
> |
threadShouldThrow(); |
1233 |
> |
} catch (InterruptedException success) {} |
1234 |
|
} |
1235 |
|
}); |
1236 |
< |
|
1236 |
> |
|
1237 |
|
try { |
1238 |
|
t.start(); |
1239 |
|
Thread.sleep(SMALL_DELAY_MS); |
1240 |
|
t.interrupt(); |
1241 |
|
t.join(); |
1242 |
< |
} catch (Exception e){ |
1242 |
> |
} catch (Exception e) { |
1243 |
|
unexpectedException(); |
1244 |
|
} |
1245 |
|
} |
1248 |
|
* takeLast retrieves elements in FIFO order |
1249 |
|
*/ |
1250 |
|
public void testTakeLast() { |
1251 |
< |
try { |
1251 |
> |
try { |
1252 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1253 |
|
for (int i = 0; i < SIZE; ++i) { |
1254 |
|
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1255 |
|
} |
1256 |
< |
} catch (InterruptedException e){ |
1257 |
< |
unexpectedException(); |
1258 |
< |
} |
1256 |
> |
} catch (InterruptedException e) { |
1257 |
> |
unexpectedException(); |
1258 |
> |
} |
1259 |
|
} |
1260 |
|
|
1261 |
|
/** |
1267 |
|
public void run() { |
1268 |
|
try { |
1269 |
|
q.takeLast(); |
1270 |
< |
threadShouldThrow(); |
1271 |
< |
} catch (InterruptedException success){ } |
1270 |
> |
threadShouldThrow(); |
1271 |
> |
} catch (InterruptedException success) { } |
1272 |
|
} |
1273 |
|
}); |
1274 |
|
try { |
1276 |
|
Thread.sleep(SHORT_DELAY_MS); |
1277 |
|
t.interrupt(); |
1278 |
|
t.join(); |
1279 |
< |
} catch (Exception e){ |
1279 |
> |
} catch (Exception e) { |
1280 |
|
unexpectedException(); |
1281 |
|
} |
1282 |
|
} |
1294 |
|
} |
1295 |
|
q.takeLast(); |
1296 |
|
threadShouldThrow(); |
1297 |
< |
} catch (InterruptedException success){ |
1298 |
< |
} |
1297 |
> |
} catch (InterruptedException success) { |
1298 |
> |
} |
1299 |
|
}}); |
1300 |
|
t.start(); |
1301 |
< |
try { |
1302 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1301 |
> |
try { |
1302 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1303 |
|
t.interrupt(); |
1304 |
|
t.join(); |
1305 |
|
} |
1306 |
|
catch (InterruptedException ie) { |
1307 |
< |
unexpectedException(); |
1307 |
> |
unexpectedException(); |
1308 |
|
} |
1309 |
|
} |
1310 |
|
|
1319 |
|
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue()); |
1320 |
|
} |
1321 |
|
assertNull(q.pollLast(0, TimeUnit.MILLISECONDS)); |
1322 |
< |
} catch (InterruptedException e){ |
1323 |
< |
unexpectedException(); |
1324 |
< |
} |
1322 |
> |
} catch (InterruptedException e) { |
1323 |
> |
unexpectedException(); |
1324 |
> |
} |
1325 |
|
} |
1326 |
|
|
1327 |
|
/** |
1334 |
|
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1335 |
|
} |
1336 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1337 |
< |
} catch (InterruptedException e){ |
1338 |
< |
unexpectedException(); |
1339 |
< |
} |
1337 |
> |
} catch (InterruptedException e) { |
1338 |
> |
unexpectedException(); |
1339 |
> |
} |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1352 |
|
threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
1353 |
|
} |
1354 |
|
threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1355 |
< |
} catch (InterruptedException success){ |
1356 |
< |
} |
1355 |
> |
} catch (InterruptedException success) { |
1356 |
> |
} |
1357 |
|
}}); |
1358 |
|
t.start(); |
1359 |
< |
try { |
1360 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1359 |
> |
try { |
1360 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1361 |
|
t.interrupt(); |
1362 |
|
t.join(); |
1363 |
|
} |
1364 |
|
catch (InterruptedException ie) { |
1365 |
< |
unexpectedException(); |
1365 |
> |
unexpectedException(); |
1366 |
|
} |
1367 |
|
} |
1368 |
|
|
1378 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1379 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1380 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1381 |
< |
threadShouldThrow(); |
1382 |
< |
} catch (InterruptedException success) { } |
1381 |
> |
threadShouldThrow(); |
1382 |
> |
} catch (InterruptedException success) { } |
1383 |
|
} |
1384 |
|
}); |
1385 |
|
try { |
1388 |
|
assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1389 |
|
t.interrupt(); |
1390 |
|
t.join(); |
1391 |
< |
} catch (Exception e){ |
1391 |
> |
} catch (Exception e) { |
1392 |
|
unexpectedException(); |
1393 |
|
} |
1394 |
< |
} |
1394 |
> |
} |
1395 |
|
|
1396 |
|
|
1397 |
|
/** |
1424 |
|
} |
1425 |
|
assertTrue(q.isEmpty()); |
1426 |
|
} |
1427 |
< |
|
1427 |
> |
|
1428 |
|
/** |
1429 |
|
* contains(x) reports true when elements added but not yet removed |
1430 |
|
*/ |
1507 |
|
*/ |
1508 |
|
public void testToArray() { |
1509 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1510 |
< |
Object[] o = q.toArray(); |
1511 |
< |
try { |
1512 |
< |
for(int i = 0; i < o.length; i++) |
1513 |
< |
assertEquals(o[i], q.take()); |
1514 |
< |
} catch (InterruptedException e){ |
1515 |
< |
unexpectedException(); |
1516 |
< |
} |
1510 |
> |
Object[] o = q.toArray(); |
1511 |
> |
try { |
1512 |
> |
for (int i = 0; i < o.length; i++) |
1513 |
> |
assertEquals(o[i], q.take()); |
1514 |
> |
} catch (InterruptedException e) { |
1515 |
> |
unexpectedException(); |
1516 |
> |
} |
1517 |
|
} |
1518 |
|
|
1519 |
|
/** |
1521 |
|
*/ |
1522 |
|
public void testToArray2() { |
1523 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1524 |
< |
Integer[] ints = new Integer[SIZE]; |
1525 |
< |
ints = (Integer[])q.toArray(ints); |
1526 |
< |
try { |
1527 |
< |
for(int i = 0; i < ints.length; i++) |
1528 |
< |
assertEquals(ints[i], q.take()); |
1529 |
< |
} catch (InterruptedException e){ |
1530 |
< |
unexpectedException(); |
1531 |
< |
} |
1524 |
> |
Integer[] ints = new Integer[SIZE]; |
1525 |
> |
ints = (Integer[])q.toArray(ints); |
1526 |
> |
try { |
1527 |
> |
for (int i = 0; i < ints.length; i++) |
1528 |
> |
assertEquals(ints[i], q.take()); |
1529 |
> |
} catch (InterruptedException e) { |
1530 |
> |
unexpectedException(); |
1531 |
> |
} |
1532 |
|
} |
1533 |
|
|
1534 |
|
/** |
1535 |
|
* toArray(null) throws NPE |
1536 |
|
*/ |
1537 |
|
public void testToArray_BadArg() { |
1538 |
< |
try { |
1538 |
> |
try { |
1539 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1540 |
< |
Object o[] = q.toArray(null); |
1541 |
< |
shouldThrow(); |
1542 |
< |
} catch(NullPointerException success){} |
1540 |
> |
Object o[] = q.toArray(null); |
1541 |
> |
shouldThrow(); |
1542 |
> |
} catch (NullPointerException success) {} |
1543 |
|
} |
1544 |
|
|
1545 |
|
/** |
1546 |
|
* toArray with incompatible array type throws CCE |
1547 |
|
*/ |
1548 |
|
public void testToArray1_BadArg() { |
1549 |
< |
try { |
1549 |
> |
try { |
1550 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1551 |
< |
Object o[] = q.toArray(new String[10] ); |
1552 |
< |
shouldThrow(); |
1553 |
< |
} catch(ArrayStoreException success){} |
1551 |
> |
Object o[] = q.toArray(new String[10] ); |
1552 |
> |
shouldThrow(); |
1553 |
> |
} catch (ArrayStoreException success) {} |
1554 |
|
} |
1555 |
|
|
1556 |
< |
|
1556 |
> |
|
1557 |
|
/** |
1558 |
|
* iterator iterates through all elements |
1559 |
|
*/ |
1560 |
|
public void testIterator() { |
1561 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1562 |
< |
Iterator it = q.iterator(); |
1563 |
< |
try { |
1564 |
< |
while(it.hasNext()){ |
1565 |
< |
assertEquals(it.next(), q.take()); |
1566 |
< |
} |
1567 |
< |
} catch (InterruptedException e){ |
1568 |
< |
unexpectedException(); |
1569 |
< |
} |
1562 |
> |
Iterator it = q.iterator(); |
1563 |
> |
try { |
1564 |
> |
while (it.hasNext()) { |
1565 |
> |
assertEquals(it.next(), q.take()); |
1566 |
> |
} |
1567 |
> |
} catch (InterruptedException e) { |
1568 |
> |
unexpectedException(); |
1569 |
> |
} |
1570 |
|
} |
1571 |
|
|
1572 |
|
/** |
1581 |
|
Iterator it = q.iterator(); |
1582 |
|
it.next(); |
1583 |
|
it.remove(); |
1584 |
< |
|
1584 |
> |
|
1585 |
|
it = q.iterator(); |
1586 |
|
assertEquals(it.next(), one); |
1587 |
|
assertEquals(it.next(), three); |
1633 |
|
public void testDescendingIterator() { |
1634 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1635 |
|
int i = 0; |
1636 |
< |
Iterator it = q.descendingIterator(); |
1637 |
< |
while(it.hasNext()) { |
1636 |
> |
Iterator it = q.descendingIterator(); |
1637 |
> |
while (it.hasNext()) { |
1638 |
|
assertTrue(q.contains(it.next())); |
1639 |
|
++i; |
1640 |
|
} |
1642 |
|
assertFalse(it.hasNext()); |
1643 |
|
try { |
1644 |
|
it.next(); |
1645 |
< |
} catch(NoSuchElementException success) { |
1645 |
> |
} catch (NoSuchElementException success) { |
1646 |
|
} |
1647 |
|
} |
1648 |
|
|
1651 |
|
*/ |
1652 |
|
public void testDescendingIteratorOrdering() { |
1653 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1654 |
< |
q.add(new Integer(3)); |
1655 |
< |
q.add(new Integer(2)); |
1656 |
< |
q.add(new Integer(1)); |
1657 |
< |
int k = 0; |
1658 |
< |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1659 |
< |
int i = ((Integer)(it.next())).intValue(); |
1660 |
< |
assertEquals(++k, i); |
1661 |
< |
} |
1654 |
> |
for (int iters = 0; iters < 100; ++iters) { |
1655 |
> |
q.add(new Integer(3)); |
1656 |
> |
q.add(new Integer(2)); |
1657 |
> |
q.add(new Integer(1)); |
1658 |
> |
int k = 0; |
1659 |
> |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1660 |
> |
int i = ((Integer)(it.next())).intValue(); |
1661 |
> |
assertEquals(++k, i); |
1662 |
> |
} |
1663 |
|
|
1664 |
< |
assertEquals(3, k); |
1664 |
> |
assertEquals(3, k); |
1665 |
> |
q.remove(); |
1666 |
> |
q.remove(); |
1667 |
> |
q.remove(); |
1668 |
> |
} |
1669 |
|
} |
1670 |
|
|
1671 |
|
/** |
1673 |
|
*/ |
1674 |
|
public void testDescendingIteratorRemove () { |
1675 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1676 |
< |
q.add(new Integer(3)); |
1677 |
< |
q.add(new Integer(2)); |
1678 |
< |
q.add(new Integer(1)); |
1679 |
< |
Iterator it = q.descendingIterator(); |
1680 |
< |
it.next(); |
1681 |
< |
it.remove(); |
1682 |
< |
it = q.descendingIterator(); |
1683 |
< |
assertEquals(it.next(), new Integer(2)); |
1684 |
< |
assertEquals(it.next(), new Integer(3)); |
1685 |
< |
assertFalse(it.hasNext()); |
1676 |
> |
for (int iters = 0; iters < 100; ++iters) { |
1677 |
> |
q.add(new Integer(3)); |
1678 |
> |
q.add(new Integer(2)); |
1679 |
> |
q.add(new Integer(1)); |
1680 |
> |
Iterator it = q.descendingIterator(); |
1681 |
> |
assertEquals(it.next(), new Integer(1)); |
1682 |
> |
it.remove(); |
1683 |
> |
assertEquals(it.next(), new Integer(2)); |
1684 |
> |
it = q.descendingIterator(); |
1685 |
> |
assertEquals(it.next(), new Integer(2)); |
1686 |
> |
assertEquals(it.next(), new Integer(3)); |
1687 |
> |
it.remove(); |
1688 |
> |
assertFalse(it.hasNext()); |
1689 |
> |
q.remove(); |
1690 |
> |
} |
1691 |
|
} |
1692 |
|
|
1693 |
|
|
1700 |
|
for (int i = 0; i < SIZE; ++i) { |
1701 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
1702 |
|
} |
1703 |
< |
} |
1703 |
> |
} |
1704 |
|
|
1705 |
|
|
1706 |
|
/** |
1735 |
|
} |
1736 |
|
} |
1737 |
|
}); |
1738 |
< |
|
1738 |
> |
|
1739 |
|
joinPool(executor); |
1740 |
|
} |
1741 |
|
|
1769 |
|
} |
1770 |
|
} |
1771 |
|
}); |
1772 |
< |
|
1772 |
> |
|
1773 |
|
joinPool(executor); |
1774 |
|
} |
1775 |
|
|
1789 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1790 |
|
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1791 |
|
assertEquals(q.size(), r.size()); |
1792 |
< |
while (!q.isEmpty()) |
1792 |
> |
while (!q.isEmpty()) |
1793 |
|
assertEquals(q.remove(), r.remove()); |
1794 |
< |
} catch(Exception e){ |
1794 |
> |
} catch (Exception e) { |
1795 |
|
unexpectedException(); |
1796 |
|
} |
1797 |
|
} |
1798 |
|
|
1799 |
|
/** |
1800 |
|
* drainTo(null) throws NPE |
1801 |
< |
*/ |
1801 |
> |
*/ |
1802 |
|
public void testDrainToNull() { |
1803 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1804 |
|
try { |
1805 |
|
q.drainTo(null); |
1806 |
|
shouldThrow(); |
1807 |
< |
} catch(NullPointerException success) { |
1807 |
> |
} catch (NullPointerException success) { |
1808 |
|
} |
1809 |
|
} |
1810 |
|
|
1811 |
|
/** |
1812 |
|
* drainTo(this) throws IAE |
1813 |
< |
*/ |
1813 |
> |
*/ |
1814 |
|
public void testDrainToSelf() { |
1815 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1816 |
|
try { |
1817 |
|
q.drainTo(q); |
1818 |
|
shouldThrow(); |
1819 |
< |
} catch(IllegalArgumentException success) { |
1819 |
> |
} catch (IllegalArgumentException success) { |
1820 |
|
} |
1821 |
|
} |
1822 |
|
|
1823 |
|
/** |
1824 |
|
* drainTo(c) empties deque into another collection c |
1825 |
< |
*/ |
1825 |
> |
*/ |
1826 |
|
public void testDrainTo() { |
1827 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1828 |
|
ArrayList l = new ArrayList(); |
1829 |
|
q.drainTo(l); |
1830 |
|
assertEquals(q.size(), 0); |
1831 |
|
assertEquals(l.size(), SIZE); |
1832 |
< |
for (int i = 0; i < SIZE; ++i) |
1832 |
> |
for (int i = 0; i < SIZE; ++i) |
1833 |
|
assertEquals(l.get(i), new Integer(i)); |
1834 |
|
q.add(zero); |
1835 |
|
q.add(one); |
1840 |
|
q.drainTo(l); |
1841 |
|
assertEquals(q.size(), 0); |
1842 |
|
assertEquals(l.size(), 2); |
1843 |
< |
for (int i = 0; i < 2; ++i) |
1843 |
> |
for (int i = 0; i < 2; ++i) |
1844 |
|
assertEquals(l.get(i), new Integer(i)); |
1845 |
|
} |
1846 |
|
|
1847 |
|
/** |
1848 |
|
* drainTo empties full deque, unblocking a waiting put. |
1849 |
< |
*/ |
1849 |
> |
*/ |
1850 |
|
public void testDrainToWithActivePut() { |
1851 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1852 |
|
Thread t = new Thread(new Runnable() { |
1853 |
|
public void run() { |
1854 |
|
try { |
1855 |
|
q.put(new Integer(SIZE+1)); |
1856 |
< |
} catch (InterruptedException ie){ |
1856 |
> |
} catch (InterruptedException ie) { |
1857 |
|
threadUnexpectedException(); |
1858 |
|
} |
1859 |
|
} |
1863 |
|
ArrayList l = new ArrayList(); |
1864 |
|
q.drainTo(l); |
1865 |
|
assertTrue(l.size() >= SIZE); |
1866 |
< |
for (int i = 0; i < SIZE; ++i) |
1866 |
> |
for (int i = 0; i < SIZE; ++i) |
1867 |
|
assertEquals(l.get(i), new Integer(i)); |
1868 |
|
t.join(); |
1869 |
|
assertTrue(q.size() + l.size() >= SIZE); |
1870 |
< |
} catch(Exception e){ |
1870 |
> |
} catch (Exception e) { |
1871 |
|
unexpectedException(); |
1872 |
|
} |
1873 |
|
} |
1874 |
|
|
1875 |
|
/** |
1876 |
|
* drainTo(null, n) throws NPE |
1877 |
< |
*/ |
1877 |
> |
*/ |
1878 |
|
public void testDrainToNullN() { |
1879 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1880 |
|
try { |
1881 |
|
q.drainTo(null, 0); |
1882 |
|
shouldThrow(); |
1883 |
< |
} catch(NullPointerException success) { |
1883 |
> |
} catch (NullPointerException success) { |
1884 |
|
} |
1885 |
|
} |
1886 |
|
|
1887 |
|
/** |
1888 |
|
* drainTo(this, n) throws IAE |
1889 |
< |
*/ |
1889 |
> |
*/ |
1890 |
|
public void testDrainToSelfN() { |
1891 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1892 |
|
try { |
1893 |
|
q.drainTo(q, 0); |
1894 |
|
shouldThrow(); |
1895 |
< |
} catch(IllegalArgumentException success) { |
1895 |
> |
} catch (IllegalArgumentException success) { |
1896 |
|
} |
1897 |
|
} |
1898 |
|
|
1899 |
|
/** |
1900 |
|
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1901 |
< |
*/ |
1901 |
> |
*/ |
1902 |
|
public void testDrainToN() { |
1903 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1904 |
|
for (int i = 0; i < SIZE + 2; ++i) { |
1905 |
< |
for(int j = 0; j < SIZE; j++) |
1905 |
> |
for (int j = 0; j < SIZE; j++) |
1906 |
|
assertTrue(q.offer(new Integer(j))); |
1907 |
|
ArrayList l = new ArrayList(); |
1908 |
|
q.drainTo(l, i); |
1909 |
|
int k = (i < SIZE)? i : SIZE; |
1910 |
|
assertEquals(l.size(), k); |
1911 |
|
assertEquals(q.size(), SIZE-k); |
1912 |
< |
for (int j = 0; j < k; ++j) |
1912 |
> |
for (int j = 0; j < k; ++j) |
1913 |
|
assertEquals(l.get(j), new Integer(j)); |
1914 |
|
while (q.poll() != null) ; |
1915 |
|
} |