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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
14 |
+ |
|
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 |
|
public static void main(String[] args) { |
28 |
< |
junit.textui.TestRunner.run (suite()); |
28 |
> |
junit.textui.TestRunner.run(suite()); |
29 |
|
} |
30 |
|
|
31 |
|
public static Test suite() { |
32 |
< |
return new TestSuite(LinkedBlockingDequeTest.class); |
32 |
> |
return newTestSuite(LinkedBlockingDequeTest.class, |
33 |
> |
new Unbounded().testSuite(), |
34 |
> |
new Bounded().testSuite()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* Create a deque of given size containing consecutive |
39 |
|
* Integers 0 ... n. |
40 |
|
*/ |
41 |
< |
private LinkedBlockingDeque populatedDeque(int n) { |
42 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(n); |
41 |
> |
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
42 |
> |
LinkedBlockingDeque<Integer> q = |
43 |
> |
new LinkedBlockingDeque<Integer>(n); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
|
for (int i = 0; i < n; i++) |
46 |
|
assertTrue(q.offer(new Integer(i))); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* pollFirst succeeds unless empty |
112 |
> |
* pollFirst succeeds unless empty |
113 |
|
*/ |
114 |
|
public void testPollFirst() { |
115 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
117 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
117 |
> |
assertEquals(i, q.pollFirst()); |
118 |
|
} |
119 |
|
assertNull(q.pollFirst()); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* pollLast succeeds unless empty |
123 |
> |
* pollLast succeeds unless empty |
124 |
|
*/ |
125 |
|
public void testPollLast() { |
126 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
127 |
|
for (int i = SIZE-1; i >= 0; --i) { |
128 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
128 |
> |
assertEquals(i, q.pollLast()); |
129 |
|
} |
130 |
|
assertNull(q.pollLast()); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* peekFirst returns next element, or null if empty |
134 |
> |
* peekFirst returns next element, or null if empty |
135 |
|
*/ |
136 |
|
public void testPeekFirst() { |
137 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
138 |
|
for (int i = 0; i < SIZE; ++i) { |
139 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
140 |
< |
q.pollFirst(); |
139 |
> |
assertEquals(i, q.peekFirst()); |
140 |
> |
assertEquals(i, q.pollFirst()); |
141 |
|
assertTrue(q.peekFirst() == null || |
142 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
142 |
> |
!q.peekFirst().equals(i)); |
143 |
|
} |
144 |
|
assertNull(q.peekFirst()); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* peek returns next element, or null if empty |
148 |
> |
* peek returns next element, or null if empty |
149 |
|
*/ |
150 |
|
public void testPeek() { |
151 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
154 |
< |
q.pollFirst(); |
153 |
> |
assertEquals(i, q.peek()); |
154 |
> |
assertEquals(i, q.pollFirst()); |
155 |
|
assertTrue(q.peek() == null || |
156 |
< |
i != ((Integer)q.peek()).intValue()); |
156 |
> |
!q.peek().equals(i)); |
157 |
|
} |
158 |
|
assertNull(q.peek()); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* peekLast returns next element, or null if empty |
162 |
> |
* peekLast returns next element, or null if empty |
163 |
|
*/ |
164 |
|
public void testPeekLast() { |
165 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
166 |
|
for (int i = SIZE-1; i >= 0; --i) { |
167 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
168 |
< |
q.pollLast(); |
167 |
> |
assertEquals(i, q.peekLast()); |
168 |
> |
assertEquals(i, q.pollLast()); |
169 |
|
assertTrue(q.peekLast() == null || |
170 |
< |
i != ((Integer)q.peekLast()).intValue()); |
170 |
> |
!q.peekLast().equals(i)); |
171 |
|
} |
172 |
|
assertNull(q.peekLast()); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getFirst returns next getFirst, or throws NSEE if empty |
176 |
> |
* getFirst() returns first element, or throws NSEE if empty |
177 |
|
*/ |
178 |
|
public void testFirstElement() { |
179 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
181 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
182 |
< |
q.pollFirst(); |
181 |
> |
assertEquals(i, q.getFirst()); |
182 |
> |
assertEquals(i, q.pollFirst()); |
183 |
|
} |
184 |
|
try { |
185 |
|
q.getFirst(); |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
< |
* getLast returns next element, or throws NSEE if empty |
192 |
> |
* getLast() returns last element, or throws NSEE if empty |
193 |
|
*/ |
194 |
|
public void testLastElement() { |
195 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
196 |
|
for (int i = SIZE-1; i >= 0; --i) { |
197 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
198 |
< |
q.pollLast(); |
197 |
> |
assertEquals(i, q.getLast()); |
198 |
> |
assertEquals(i, q.pollLast()); |
199 |
|
} |
200 |
|
try { |
201 |
|
q.getLast(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* removeFirst removes next element, or throws NSEE if empty |
208 |
> |
* removeFirst() removes first element, or throws NSEE if empty |
209 |
|
*/ |
210 |
|
public void testRemoveFirst() { |
211 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
213 |
< |
assertEquals(i, ((Integer)q.removeFirst()).intValue()); |
213 |
> |
assertEquals(i, q.removeFirst()); |
214 |
|
} |
215 |
|
try { |
216 |
|
q.removeFirst(); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
223 |
< |
* removeLast removes last element, or throws NSEE if empty |
223 |
> |
* removeLast() removes last element, or throws NSEE if empty |
224 |
|
*/ |
225 |
|
public void testRemoveLast() { |
226 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
227 |
|
for (int i = SIZE - 1; i >= 0; --i) { |
228 |
< |
assertEquals(i, ((Integer)q.removeLast()).intValue()); |
228 |
> |
assertEquals(i, q.removeLast()); |
229 |
|
} |
230 |
|
try { |
231 |
|
q.removeLast(); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* remove removes next element, or throws NSEE if empty |
238 |
> |
* remove removes next element, or throws NSEE if empty |
239 |
|
*/ |
240 |
|
public void testRemove() { |
241 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
243 |
> |
assertEquals(i, q.remove()); |
244 |
|
} |
245 |
|
try { |
246 |
|
q.remove(); |
285 |
|
LinkedBlockingDeque q = populatedDeque(3); |
286 |
|
q.pollLast(); |
287 |
|
q.addFirst(four); |
288 |
< |
assertEquals(four,q.peekFirst()); |
288 |
> |
assertSame(four, q.peekFirst()); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
295 |
|
LinkedBlockingDeque q = populatedDeque(3); |
296 |
|
q.pollLast(); |
297 |
|
q.addLast(four); |
298 |
< |
assertEquals(four,q.peekLast()); |
298 |
> |
assertSame(four, q.peekLast()); |
299 |
|
} |
300 |
|
|
301 |
|
|
453 |
|
LinkedBlockingDeque q = populatedDeque(3); |
454 |
|
q.pollLast(); |
455 |
|
q.push(four); |
456 |
< |
assertEquals(four,q.peekFirst()); |
456 |
> |
assertSame(four, q.peekFirst()); |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
|
/** |
461 |
< |
* pop removes next element, or throws NSEE if empty |
461 |
> |
* pop removes next element, or throws NSEE if empty |
462 |
|
*/ |
463 |
|
public void testPop() { |
464 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
465 |
|
for (int i = 0; i < SIZE; ++i) { |
466 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
466 |
> |
assertEquals(i, q.pop()); |
467 |
|
} |
468 |
|
try { |
469 |
|
q.pop(); |
529 |
|
shouldThrow(); |
530 |
|
} catch (NullPointerException success) {} |
531 |
|
} |
532 |
+ |
|
533 |
|
/** |
534 |
|
* addAll of a collection with any null elements throws NPE after |
535 |
|
* possibly adding some elements |
544 |
|
shouldThrow(); |
545 |
|
} catch (NullPointerException success) {} |
546 |
|
} |
547 |
+ |
|
548 |
|
/** |
549 |
|
* addAll throws ISE if not enough room |
550 |
|
*/ |
558 |
|
shouldThrow(); |
559 |
|
} catch (IllegalStateException success) {} |
560 |
|
} |
561 |
+ |
|
562 |
|
/** |
563 |
|
* Deque contains all elements, in traversal order, of successful addAll |
564 |
|
*/ |
603 |
|
* put blocks interruptibly if full |
604 |
|
*/ |
605 |
|
public void testBlockingPut() throws InterruptedException { |
606 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
607 |
|
Thread t = new Thread(new CheckedRunnable() { |
608 |
< |
public void realRun() { |
609 |
< |
int added = 0; |
608 |
> |
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 |
|
try { |
614 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
615 |
< |
for (int i = 0; i < SIZE; ++i) { |
616 |
< |
q.put(new Integer(i)); |
594 |
< |
++added; |
595 |
< |
} |
596 |
< |
q.put(new Integer(SIZE)); |
597 |
< |
threadShouldThrow(); |
598 |
< |
} catch (InterruptedException success) { |
599 |
< |
threadAssertEquals(added, SIZE); |
600 |
< |
} |
614 |
> |
q.put(99); |
615 |
> |
shouldThrow(); |
616 |
> |
} catch (InterruptedException success) {} |
617 |
|
}}); |
618 |
|
|
619 |
|
t.start(); |
620 |
|
Thread.sleep(SHORT_DELAY_MS); |
621 |
|
t.interrupt(); |
622 |
|
t.join(); |
623 |
+ |
assertEquals(SIZE, q.size()); |
624 |
+ |
assertEquals(0, q.remainingCapacity()); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
|
* put blocks waiting for take when full |
629 |
|
*/ |
630 |
|
public void testPutWithTake() throws InterruptedException { |
631 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
631 |
> |
final int capacity = 2; |
632 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
633 |
|
Thread t = new Thread(new CheckedRunnable() { |
634 |
< |
public void realRun() { |
635 |
< |
int added = 0; |
634 |
> |
public void realRun() throws InterruptedException { |
635 |
> |
for (int i = 0; i < capacity + 1; i++) |
636 |
> |
q.put(i); |
637 |
|
try { |
638 |
< |
q.put(new Object()); |
639 |
< |
++added; |
640 |
< |
q.put(new Object()); |
621 |
< |
++added; |
622 |
< |
q.put(new Object()); |
623 |
< |
++added; |
624 |
< |
q.put(new Object()); |
625 |
< |
++added; |
626 |
< |
threadShouldThrow(); |
627 |
< |
} catch (InterruptedException success) { |
628 |
< |
threadAssertTrue(added >= 2); |
629 |
< |
} |
638 |
> |
q.put(99); |
639 |
> |
shouldThrow(); |
640 |
> |
} catch (InterruptedException success) {} |
641 |
|
}}); |
642 |
|
|
643 |
|
t.start(); |
644 |
|
Thread.sleep(SHORT_DELAY_MS); |
645 |
< |
q.take(); |
645 |
> |
assertEquals(q.remainingCapacity(), 0); |
646 |
> |
assertEquals(0, q.take()); |
647 |
> |
Thread.sleep(SHORT_DELAY_MS); |
648 |
|
t.interrupt(); |
649 |
|
t.join(); |
650 |
+ |
assertEquals(q.remainingCapacity(), 0); |
651 |
|
} |
652 |
|
|
653 |
|
/** |
655 |
|
*/ |
656 |
|
public void testTimedOffer() throws InterruptedException { |
657 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
658 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
658 |
> |
Thread t = new Thread(new CheckedRunnable() { |
659 |
|
public void realRun() throws InterruptedException { |
660 |
|
q.put(new Object()); |
661 |
|
q.put(new Object()); |
662 |
< |
threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
663 |
< |
q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
664 |
< |
}}; |
662 |
> |
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 |
|
|
669 |
|
t.start(); |
670 |
|
Thread.sleep(SMALL_DELAY_MS); |
678 |
|
public void testTake() throws InterruptedException { |
679 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
680 |
|
for (int i = 0; i < SIZE; ++i) { |
681 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
681 |
> |
assertEquals(i, q.take()); |
682 |
|
} |
683 |
|
} |
684 |
|
|
685 |
|
/** |
669 |
– |
* take blocks interruptibly when empty |
670 |
– |
*/ |
671 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
672 |
– |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
673 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
674 |
– |
public void realRun() throws InterruptedException { |
675 |
– |
q.take(); |
676 |
– |
}}; |
677 |
– |
|
678 |
– |
t.start(); |
679 |
– |
Thread.sleep(SHORT_DELAY_MS); |
680 |
– |
t.interrupt(); |
681 |
– |
t.join(); |
682 |
– |
} |
683 |
– |
|
684 |
– |
/** |
686 |
|
* Take removes existing elements until empty, then blocks interruptibly |
687 |
|
*/ |
688 |
|
public void testBlockingTake() throws InterruptedException { |
689 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
689 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
690 |
> |
Thread t = new Thread(new CheckedRunnable() { |
691 |
|
public void realRun() throws InterruptedException { |
690 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
692 |
|
for (int i = 0; i < SIZE; ++i) { |
693 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
693 |
> |
assertEquals(i, q.take()); |
694 |
|
} |
695 |
< |
q.take(); |
696 |
< |
}}; |
695 |
> |
try { |
696 |
> |
q.take(); |
697 |
> |
shouldThrow(); |
698 |
> |
} catch (InterruptedException success) {} |
699 |
> |
}}); |
700 |
|
|
701 |
|
t.start(); |
702 |
|
Thread.sleep(SHORT_DELAY_MS); |
711 |
|
public void testPoll() { |
712 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
713 |
|
for (int i = 0; i < SIZE; ++i) { |
714 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
714 |
> |
assertEquals(i, q.poll()); |
715 |
|
} |
716 |
|
assertNull(q.poll()); |
717 |
|
} |
722 |
|
public void testTimedPoll0() throws InterruptedException { |
723 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
724 |
|
for (int i = 0; i < SIZE; ++i) { |
725 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
725 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
726 |
|
} |
727 |
|
assertNull(q.poll(0, MILLISECONDS)); |
728 |
|
} |
733 |
|
public void testTimedPoll() throws InterruptedException { |
734 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
735 |
|
for (int i = 0; i < SIZE; ++i) { |
736 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
736 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
737 |
|
} |
738 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
739 |
|
} |
743 |
|
* returning timeout status |
744 |
|
*/ |
745 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
746 |
< |
Thread t = new Thread(new CheckedRunnable() { |
746 |
> |
final BlockingQueue<Integer> q = populatedDeque(SIZE); |
747 |
> |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
748 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
749 |
|
public void realRun() throws InterruptedException { |
744 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
750 |
|
for (int i = 0; i < SIZE; ++i) { |
751 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
751 |
> |
long t0 = System.nanoTime(); |
752 |
> |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
753 |
> |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
754 |
|
} |
755 |
+ |
long t0 = System.nanoTime(); |
756 |
+ |
aboutToWait.countDown(); |
757 |
|
try { |
758 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
758 |
> |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
759 |
|
shouldThrow(); |
760 |
< |
} catch (InterruptedException success) {} |
761 |
< |
}}); |
762 |
< |
|
754 |
< |
t.start(); |
755 |
< |
Thread.sleep(SHORT_DELAY_MS); |
756 |
< |
t.interrupt(); |
757 |
< |
t.join(); |
758 |
< |
} |
759 |
< |
|
760 |
< |
/** |
761 |
< |
* timed poll before a delayed offer fails; after offer succeeds; |
762 |
< |
* on interruption throws |
763 |
< |
*/ |
764 |
< |
public void testTimedPollWithOffer() throws InterruptedException { |
765 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
766 |
< |
Thread t = new Thread(new CheckedRunnable() { |
767 |
< |
public void realRun() throws InterruptedException { |
768 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
769 |
< |
assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
770 |
< |
try { |
771 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
772 |
< |
shouldThrow(); |
773 |
< |
} catch (InterruptedException success) {} |
760 |
> |
} catch (InterruptedException success) { |
761 |
> |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
762 |
> |
} |
763 |
|
}}); |
764 |
|
|
765 |
< |
t.start(); |
766 |
< |
Thread.sleep(SMALL_DELAY_MS); |
778 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
765 |
> |
aboutToWait.await(); |
766 |
> |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
767 |
|
t.interrupt(); |
768 |
< |
t.join(); |
768 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
769 |
> |
checkEmpty(q); |
770 |
|
} |
771 |
|
|
783 |
– |
|
772 |
|
/** |
773 |
|
* putFirst(null) throws NPE |
774 |
|
*/ |
775 |
< |
public void testPutFirstNull() throws InterruptedException { |
775 |
> |
public void testPutFirstNull() throws InterruptedException { |
776 |
|
try { |
777 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
|
q.putFirst(null); |
779 |
|
shouldThrow(); |
780 |
|
} catch (NullPointerException success) {} |
781 |
< |
} |
781 |
> |
} |
782 |
|
|
783 |
|
/** |
784 |
|
* all elements successfully putFirst are contained |
785 |
|
*/ |
786 |
< |
public void testPutFirst() throws InterruptedException { |
787 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
788 |
< |
for (int i = 0; i < SIZE; ++i) { |
789 |
< |
Integer I = new Integer(i); |
790 |
< |
q.putFirst(I); |
791 |
< |
assertTrue(q.contains(I)); |
792 |
< |
} |
793 |
< |
assertEquals(0, q.remainingCapacity()); |
786 |
> |
public void testPutFirst() throws InterruptedException { |
787 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
788 |
> |
for (int i = 0; i < SIZE; ++i) { |
789 |
> |
Integer I = new Integer(i); |
790 |
> |
q.putFirst(I); |
791 |
> |
assertTrue(q.contains(I)); |
792 |
> |
} |
793 |
> |
assertEquals(0, q.remainingCapacity()); |
794 |
|
} |
795 |
|
|
796 |
|
/** |
797 |
|
* putFirst blocks interruptibly if full |
798 |
|
*/ |
799 |
|
public void testBlockingPutFirst() throws InterruptedException { |
800 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
801 |
|
Thread t = new Thread(new CheckedRunnable() { |
802 |
< |
public void realRun() { |
803 |
< |
int added = 0; |
802 |
> |
public void realRun() throws InterruptedException { |
803 |
> |
for (int i = 0; i < SIZE; ++i) |
804 |
> |
q.putFirst(i); |
805 |
> |
assertEquals(SIZE, q.size()); |
806 |
> |
assertEquals(0, q.remainingCapacity()); |
807 |
|
try { |
808 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
809 |
< |
for (int i = 0; i < SIZE; ++i) { |
810 |
< |
q.putFirst(new Integer(i)); |
819 |
< |
++added; |
820 |
< |
} |
821 |
< |
q.putFirst(new Integer(SIZE)); |
822 |
< |
threadShouldThrow(); |
823 |
< |
} catch (InterruptedException success) { |
824 |
< |
threadAssertEquals(added, SIZE); |
825 |
< |
} |
808 |
> |
q.putFirst(99); |
809 |
> |
shouldThrow(); |
810 |
> |
} catch (InterruptedException success) {} |
811 |
|
}}); |
812 |
|
|
813 |
|
t.start(); |
814 |
|
Thread.sleep(SHORT_DELAY_MS); |
815 |
|
t.interrupt(); |
816 |
|
t.join(); |
817 |
+ |
assertEquals(SIZE, q.size()); |
818 |
+ |
assertEquals(0, q.remainingCapacity()); |
819 |
|
} |
820 |
|
|
821 |
|
/** |
822 |
|
* putFirst blocks waiting for take when full |
823 |
|
*/ |
824 |
|
public void testPutFirstWithTake() throws InterruptedException { |
825 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
825 |
> |
final int capacity = 2; |
826 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
827 |
|
Thread t = new Thread(new CheckedRunnable() { |
828 |
< |
public void realRun() { |
829 |
< |
int added = 0; |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
> |
for (int i = 0; i < capacity + 1; i++) |
830 |
> |
q.putFirst(i); |
831 |
|
try { |
832 |
< |
q.putFirst(new Object()); |
833 |
< |
++added; |
834 |
< |
q.putFirst(new Object()); |
846 |
< |
++added; |
847 |
< |
q.putFirst(new Object()); |
848 |
< |
++added; |
849 |
< |
q.putFirst(new Object()); |
850 |
< |
++added; |
851 |
< |
threadShouldThrow(); |
852 |
< |
} catch (InterruptedException success) { |
853 |
< |
threadAssertTrue(added >= 2); |
854 |
< |
} |
832 |
> |
q.putFirst(99); |
833 |
> |
shouldThrow(); |
834 |
> |
} catch (InterruptedException success) {} |
835 |
|
}}); |
836 |
|
|
837 |
|
t.start(); |
838 |
|
Thread.sleep(SHORT_DELAY_MS); |
839 |
< |
q.take(); |
839 |
> |
assertEquals(q.remainingCapacity(), 0); |
840 |
> |
assertEquals(capacity - 1, q.take()); |
841 |
> |
Thread.sleep(SHORT_DELAY_MS); |
842 |
|
t.interrupt(); |
843 |
|
t.join(); |
844 |
+ |
assertEquals(q.remainingCapacity(), 0); |
845 |
|
} |
846 |
|
|
847 |
|
/** |
849 |
|
*/ |
850 |
|
public void testTimedOfferFirst() throws InterruptedException { |
851 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
852 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
852 |
> |
Thread t = new Thread(new CheckedRunnable() { |
853 |
|
public void realRun() throws InterruptedException { |
854 |
|
q.putFirst(new Object()); |
855 |
|
q.putFirst(new Object()); |
856 |
< |
threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
857 |
< |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
858 |
< |
}}; |
856 |
> |
assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
857 |
> |
try { |
858 |
> |
q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); |
859 |
> |
shouldThrow(); |
860 |
> |
} catch (InterruptedException success) {} |
861 |
> |
}}); |
862 |
|
|
863 |
|
t.start(); |
864 |
|
Thread.sleep(SMALL_DELAY_MS); |
872 |
|
public void testTakeFirst() throws InterruptedException { |
873 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
874 |
|
for (int i = 0; i < SIZE; ++i) { |
875 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
875 |
> |
assertEquals(i, q.takeFirst()); |
876 |
|
} |
877 |
|
} |
878 |
|
|
896 |
|
* TakeFirst removes existing elements until empty, then blocks interruptibly |
897 |
|
*/ |
898 |
|
public void testBlockingTakeFirst() throws InterruptedException { |
899 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
899 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
900 |
> |
Thread t = new Thread(new CheckedRunnable() { |
901 |
|
public void realRun() throws InterruptedException { |
902 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
903 |
< |
for (int i = 0; i < SIZE; ++i) { |
904 |
< |
assertEquals(i, ((Integer)q.takeFirst()).intValue()); |
905 |
< |
} |
906 |
< |
q.takeFirst(); |
907 |
< |
}}; |
902 |
> |
for (int i = 0; i < SIZE; ++i) |
903 |
> |
assertEquals(i, q.takeFirst()); |
904 |
> |
try { |
905 |
> |
q.takeFirst(); |
906 |
> |
shouldThrow(); |
907 |
> |
} catch (InterruptedException success) {} |
908 |
> |
}}); |
909 |
|
|
910 |
|
t.start(); |
911 |
|
Thread.sleep(SHORT_DELAY_MS); |
920 |
|
public void testTimedPollFirst0() throws InterruptedException { |
921 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
922 |
|
for (int i = 0; i < SIZE; ++i) { |
923 |
< |
assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); |
923 |
> |
assertEquals(i, q.pollFirst(0, MILLISECONDS)); |
924 |
|
} |
925 |
|
assertNull(q.pollFirst(0, MILLISECONDS)); |
926 |
|
} |
931 |
|
public void testTimedPollFirst() throws InterruptedException { |
932 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
933 |
|
for (int i = 0; i < SIZE; ++i) { |
934 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
934 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
935 |
|
} |
936 |
|
assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
937 |
|
} |
945 |
|
public void realRun() throws InterruptedException { |
946 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
947 |
|
for (int i = 0; i < SIZE; ++i) { |
948 |
< |
assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
948 |
> |
assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); |
949 |
|
} |
950 |
|
try { |
951 |
|
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
960 |
|
} |
961 |
|
|
962 |
|
/** |
963 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
964 |
< |
* on interruption throws |
963 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
964 |
> |
* on interruption throws |
965 |
|
*/ |
966 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
967 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
985 |
|
/** |
986 |
|
* putLast(null) throws NPE |
987 |
|
*/ |
988 |
< |
public void testPutLastNull() throws InterruptedException { |
988 |
> |
public void testPutLastNull() throws InterruptedException { |
989 |
|
try { |
990 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
991 |
|
q.putLast(null); |
992 |
|
shouldThrow(); |
993 |
|
} catch (NullPointerException success) {} |
994 |
< |
} |
994 |
> |
} |
995 |
|
|
996 |
|
/** |
997 |
|
* all elements successfully putLast are contained |
998 |
|
*/ |
999 |
< |
public void testPutLast() throws InterruptedException { |
1000 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
< |
for (int i = 0; i < SIZE; ++i) { |
1002 |
< |
Integer I = new Integer(i); |
1003 |
< |
q.putLast(I); |
1004 |
< |
assertTrue(q.contains(I)); |
1005 |
< |
} |
1006 |
< |
assertEquals(0, q.remainingCapacity()); |
999 |
> |
public void testPutLast() throws InterruptedException { |
1000 |
> |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1001 |
> |
for (int i = 0; i < SIZE; ++i) { |
1002 |
> |
Integer I = new Integer(i); |
1003 |
> |
q.putLast(I); |
1004 |
> |
assertTrue(q.contains(I)); |
1005 |
> |
} |
1006 |
> |
assertEquals(0, q.remainingCapacity()); |
1007 |
|
} |
1008 |
|
|
1009 |
|
/** |
1010 |
|
* putLast blocks interruptibly if full |
1011 |
|
*/ |
1012 |
|
public void testBlockingPutLast() throws InterruptedException { |
1013 |
+ |
final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1014 |
|
Thread t = new Thread(new CheckedRunnable() { |
1015 |
< |
public void realRun() { |
1016 |
< |
int added = 0; |
1015 |
> |
public void realRun() throws InterruptedException { |
1016 |
> |
for (int i = 0; i < SIZE; ++i) |
1017 |
> |
q.putLast(i); |
1018 |
> |
assertEquals(SIZE, q.size()); |
1019 |
> |
assertEquals(0, q.remainingCapacity()); |
1020 |
|
try { |
1021 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1022 |
< |
for (int i = 0; i < SIZE; ++i) { |
1023 |
< |
q.putLast(new Integer(i)); |
1032 |
< |
++added; |
1033 |
< |
} |
1034 |
< |
q.putLast(new Integer(SIZE)); |
1035 |
< |
threadShouldThrow(); |
1036 |
< |
} catch (InterruptedException success) { |
1037 |
< |
threadAssertEquals(added, SIZE); |
1038 |
< |
} |
1021 |
> |
q.putLast(99); |
1022 |
> |
shouldThrow(); |
1023 |
> |
} catch (InterruptedException success) {} |
1024 |
|
}}); |
1025 |
|
|
1026 |
|
t.start(); |
1027 |
|
Thread.sleep(SHORT_DELAY_MS); |
1028 |
|
t.interrupt(); |
1029 |
|
t.join(); |
1030 |
+ |
assertEquals(SIZE, q.size()); |
1031 |
+ |
assertEquals(0, q.remainingCapacity()); |
1032 |
|
} |
1033 |
|
|
1034 |
|
/** |
1035 |
|
* putLast blocks waiting for take when full |
1036 |
|
*/ |
1037 |
|
public void testPutLastWithTake() throws InterruptedException { |
1038 |
< |
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1038 |
> |
final int capacity = 2; |
1039 |
> |
final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); |
1040 |
|
Thread t = new Thread(new CheckedRunnable() { |
1041 |
< |
public void realRun() { |
1042 |
< |
int added = 0; |
1041 |
> |
public void realRun() throws InterruptedException { |
1042 |
> |
for (int i = 0; i < capacity + 1; i++) |
1043 |
> |
q.putLast(i); |
1044 |
|
try { |
1045 |
< |
q.putLast(new Object()); |
1046 |
< |
++added; |
1047 |
< |
q.putLast(new Object()); |
1059 |
< |
++added; |
1060 |
< |
q.putLast(new Object()); |
1061 |
< |
++added; |
1062 |
< |
q.putLast(new Object()); |
1063 |
< |
++added; |
1064 |
< |
threadShouldThrow(); |
1065 |
< |
} catch (InterruptedException success) { |
1066 |
< |
threadAssertTrue(added >= 2); |
1067 |
< |
} |
1045 |
> |
q.putLast(99); |
1046 |
> |
shouldThrow(); |
1047 |
> |
} catch (InterruptedException success) {} |
1048 |
|
}}); |
1049 |
|
|
1050 |
|
t.start(); |
1051 |
|
Thread.sleep(SHORT_DELAY_MS); |
1052 |
< |
q.take(); |
1052 |
> |
assertEquals(q.remainingCapacity(), 0); |
1053 |
> |
assertEquals(0, q.take()); |
1054 |
> |
Thread.sleep(SHORT_DELAY_MS); |
1055 |
|
t.interrupt(); |
1056 |
|
t.join(); |
1057 |
+ |
assertEquals(q.remainingCapacity(), 0); |
1058 |
|
} |
1059 |
|
|
1060 |
|
/** |
1062 |
|
*/ |
1063 |
|
public void testTimedOfferLast() throws InterruptedException { |
1064 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1065 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1065 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1066 |
|
public void realRun() throws InterruptedException { |
1067 |
|
q.putLast(new Object()); |
1068 |
|
q.putLast(new Object()); |
1069 |
< |
threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1070 |
< |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1071 |
< |
}}; |
1069 |
> |
assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); |
1070 |
> |
try { |
1071 |
> |
q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1072 |
> |
shouldThrow(); |
1073 |
> |
} catch (InterruptedException success) {} |
1074 |
> |
}}); |
1075 |
|
|
1076 |
|
t.start(); |
1077 |
|
Thread.sleep(SMALL_DELAY_MS); |
1085 |
|
public void testTakeLast() throws InterruptedException { |
1086 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1087 |
|
for (int i = 0; i < SIZE; ++i) { |
1088 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1088 |
> |
assertEquals(SIZE-i-1, q.takeLast()); |
1089 |
|
} |
1090 |
|
} |
1091 |
|
|
1109 |
|
* TakeLast removes existing elements until empty, then blocks interruptibly |
1110 |
|
*/ |
1111 |
|
public void testBlockingTakeLast() throws InterruptedException { |
1112 |
< |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
1112 |
> |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1113 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1114 |
|
public void realRun() throws InterruptedException { |
1115 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1116 |
< |
for (int i = 0; i < SIZE; ++i) { |
1117 |
< |
assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); |
1118 |
< |
} |
1119 |
< |
q.takeLast(); |
1120 |
< |
}}; |
1115 |
> |
for (int i = 0; i < SIZE; ++i) |
1116 |
> |
assertEquals(SIZE - 1 - i, q.takeLast()); |
1117 |
> |
try { |
1118 |
> |
q.takeLast(); |
1119 |
> |
shouldThrow(); |
1120 |
> |
} catch (InterruptedException success) {} |
1121 |
> |
}}); |
1122 |
|
|
1123 |
|
t.start(); |
1124 |
|
Thread.sleep(SHORT_DELAY_MS); |
1126 |
|
t.join(); |
1127 |
|
} |
1128 |
|
|
1141 |
– |
|
1129 |
|
/** |
1130 |
|
* timed pollLast with zero timeout succeeds when non-empty, else times out |
1131 |
|
*/ |
1132 |
|
public void testTimedPollLast0() throws InterruptedException { |
1133 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1134 |
|
for (int i = 0; i < SIZE; ++i) { |
1135 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); |
1135 |
> |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1136 |
|
} |
1137 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1138 |
|
} |
1143 |
|
public void testTimedPollLast() throws InterruptedException { |
1144 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1145 |
|
for (int i = 0; i < SIZE; ++i) { |
1146 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1146 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1147 |
|
} |
1148 |
|
assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1149 |
|
} |
1157 |
|
public void realRun() throws InterruptedException { |
1158 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1159 |
|
for (int i = 0; i < SIZE; ++i) { |
1160 |
< |
assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
1160 |
> |
assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); |
1161 |
|
} |
1162 |
|
try { |
1163 |
|
q.pollLast(SMALL_DELAY_MS, MILLISECONDS); |
1172 |
|
} |
1173 |
|
|
1174 |
|
/** |
1175 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1176 |
< |
* on interruption throws |
1175 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1176 |
> |
* on interruption throws |
1177 |
|
*/ |
1178 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1179 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1201 |
|
public void testElement() { |
1202 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1203 |
|
for (int i = 0; i < SIZE; ++i) { |
1204 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
1204 |
> |
assertEquals(i, q.element()); |
1205 |
|
q.poll(); |
1206 |
|
} |
1207 |
|
try { |
1216 |
|
public void testRemoveElement() { |
1217 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1218 |
|
for (int i = 1; i < SIZE; i+=2) { |
1219 |
< |
assertTrue(q.remove(new Integer(i))); |
1219 |
> |
assertTrue(q.contains(i)); |
1220 |
> |
assertTrue(q.remove(i)); |
1221 |
> |
assertFalse(q.contains(i)); |
1222 |
> |
assertTrue(q.contains(i-1)); |
1223 |
|
} |
1224 |
|
for (int i = 0; i < SIZE; i+=2) { |
1225 |
< |
assertTrue(q.remove(new Integer(i))); |
1226 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1225 |
> |
assertTrue(q.contains(i)); |
1226 |
> |
assertTrue(q.remove(i)); |
1227 |
> |
assertFalse(q.contains(i)); |
1228 |
> |
assertFalse(q.remove(i+1)); |
1229 |
> |
assertFalse(q.contains(i+1)); |
1230 |
|
} |
1231 |
|
assertTrue(q.isEmpty()); |
1232 |
|
} |
1309 |
|
} |
1310 |
|
|
1311 |
|
/** |
1312 |
< |
* toArray contains all elements |
1312 |
> |
* toArray contains all elements in FIFO order |
1313 |
|
*/ |
1314 |
|
public void testToArray() throws InterruptedException{ |
1315 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1316 |
|
Object[] o = q.toArray(); |
1317 |
|
for (int i = 0; i < o.length; i++) |
1318 |
< |
assertEquals(o[i], q.take()); |
1318 |
> |
assertSame(o[i], q.poll()); |
1319 |
|
} |
1320 |
|
|
1321 |
|
/** |
1322 |
< |
* toArray(a) contains all elements |
1322 |
> |
* toArray(a) contains all elements in FIFO order |
1323 |
|
*/ |
1324 |
< |
public void testToArray2() throws InterruptedException { |
1325 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
> |
public void testToArray2() { |
1325 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1326 |
|
Integer[] ints = new Integer[SIZE]; |
1327 |
< |
ints = (Integer[])q.toArray(ints); |
1327 |
> |
Integer[] array = q.toArray(ints); |
1328 |
> |
assertSame(ints, array); |
1329 |
|
for (int i = 0; i < ints.length; i++) |
1330 |
< |
assertEquals(ints[i], q.take()); |
1330 |
> |
assertSame(ints[i], q.remove()); |
1331 |
|
} |
1332 |
|
|
1333 |
|
/** |
1334 |
< |
* toArray(null) throws NPE |
1334 |
> |
* toArray(null) throws NullPointerException |
1335 |
|
*/ |
1336 |
< |
public void testToArray_BadArg() { |
1336 |
> |
public void testToArray_NullArg() { |
1337 |
> |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1338 |
|
try { |
1339 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1345 |
< |
Object o[] = q.toArray(null); |
1339 |
> |
q.toArray(null); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (NullPointerException success) {} |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** |
1345 |
< |
* toArray with incompatible array type throws CCE |
1345 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1346 |
|
*/ |
1347 |
|
public void testToArray1_BadArg() { |
1348 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1349 |
|
try { |
1350 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1356 |
< |
Object o[] = q.toArray(new String[10] ); |
1350 |
> |
q.toArray(new String[10]); |
1351 |
|
shouldThrow(); |
1352 |
|
} catch (ArrayStoreException success) {} |
1353 |
|
} |
1367 |
|
/** |
1368 |
|
* iterator.remove removes current element |
1369 |
|
*/ |
1370 |
< |
public void testIteratorRemove () { |
1370 |
> |
public void testIteratorRemove() { |
1371 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1372 |
|
q.add(two); |
1373 |
|
q.add(one); |
1378 |
|
it.remove(); |
1379 |
|
|
1380 |
|
it = q.iterator(); |
1381 |
< |
assertEquals(it.next(), one); |
1382 |
< |
assertEquals(it.next(), three); |
1381 |
> |
assertSame(it.next(), one); |
1382 |
> |
assertSame(it.next(), three); |
1383 |
|
assertFalse(it.hasNext()); |
1384 |
|
} |
1385 |
|
|
1395 |
|
assertEquals(0, q.remainingCapacity()); |
1396 |
|
int k = 0; |
1397 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
1398 |
< |
int i = ((Integer)(it.next())).intValue(); |
1405 |
< |
assertEquals(++k, i); |
1398 |
> |
assertEquals(++k, it.next()); |
1399 |
|
} |
1400 |
|
assertEquals(3, k); |
1401 |
|
} |
1403 |
|
/** |
1404 |
|
* Modifications do not cause iterators to fail |
1405 |
|
*/ |
1406 |
< |
public void testWeaklyConsistentIteration () { |
1406 |
> |
public void testWeaklyConsistentIteration() { |
1407 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(3); |
1408 |
|
q.add(one); |
1409 |
|
q.add(two); |
1417 |
|
|
1418 |
|
|
1419 |
|
/** |
1420 |
< |
* Descending iterator iterates through all elements |
1420 |
> |
* Descending iterator iterates through all elements |
1421 |
|
*/ |
1422 |
|
public void testDescendingIterator() { |
1423 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1436 |
|
} |
1437 |
|
|
1438 |
|
/** |
1439 |
< |
* Descending iterator ordering is reverse FIFO |
1439 |
> |
* Descending iterator ordering is reverse FIFO |
1440 |
|
*/ |
1441 |
|
public void testDescendingIteratorOrdering() { |
1442 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1446 |
|
q.add(new Integer(1)); |
1447 |
|
int k = 0; |
1448 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
1449 |
< |
int i = ((Integer)(it.next())).intValue(); |
1457 |
< |
assertEquals(++k, i); |
1449 |
> |
assertEquals(++k, it.next()); |
1450 |
|
} |
1451 |
|
|
1452 |
|
assertEquals(3, k); |
1459 |
|
/** |
1460 |
|
* descendingIterator.remove removes current element |
1461 |
|
*/ |
1462 |
< |
public void testDescendingIteratorRemove () { |
1462 |
> |
public void testDescendingIteratorRemove() { |
1463 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1464 |
|
for (int iters = 0; iters < 100; ++iters) { |
1465 |
|
q.add(new Integer(3)); |
1501 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1502 |
|
executor.execute(new CheckedRunnable() { |
1503 |
|
public void realRun() throws InterruptedException { |
1504 |
< |
threadAssertFalse(q.offer(three)); |
1505 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1506 |
< |
threadAssertEquals(0, q.remainingCapacity()); |
1504 |
> |
assertFalse(q.offer(three)); |
1505 |
> |
assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); |
1506 |
> |
assertEquals(0, q.remainingCapacity()); |
1507 |
|
}}); |
1508 |
|
|
1509 |
|
executor.execute(new CheckedRunnable() { |
1510 |
|
public void realRun() throws InterruptedException { |
1511 |
|
Thread.sleep(SMALL_DELAY_MS); |
1512 |
< |
threadAssertEquals(one, q.take()); |
1512 |
> |
assertSame(one, q.take()); |
1513 |
|
}}); |
1514 |
|
|
1515 |
|
joinPool(executor); |
1523 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
1524 |
|
executor.execute(new CheckedRunnable() { |
1525 |
|
public void realRun() throws InterruptedException { |
1526 |
< |
threadAssertNull(q.poll()); |
1527 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1528 |
< |
threadAssertTrue(q.isEmpty()); |
1526 |
> |
assertNull(q.poll()); |
1527 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
1528 |
> |
assertTrue(q.isEmpty()); |
1529 |
|
}}); |
1530 |
|
|
1531 |
|
executor.execute(new CheckedRunnable() { |
1645 |
|
} |
1646 |
|
|
1647 |
|
/** |
1648 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1648 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1649 |
|
*/ |
1650 |
|
public void testDrainToN() { |
1651 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1654 |
|
assertTrue(q.offer(new Integer(j))); |
1655 |
|
ArrayList l = new ArrayList(); |
1656 |
|
q.drainTo(l, i); |
1657 |
< |
int k = (i < SIZE)? i : SIZE; |
1657 |
> |
int k = (i < SIZE) ? i : SIZE; |
1658 |
|
assertEquals(l.size(), k); |
1659 |
|
assertEquals(q.size(), SIZE-k); |
1660 |
|
for (int j = 0; j < k; ++j) |