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()); |
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); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* pollLast succeeds unless empty |
123 |
> |
* pollLast succeeds unless empty |
124 |
|
*/ |
125 |
|
public void testPollLast() { |
126 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
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); |
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); |
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); |
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); |
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); |
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); |
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); |
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); |
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); |
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 |
|
*/ |
683 |
|
} |
684 |
|
|
685 |
|
/** |
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 |
– |
/** |
686 |
|
* Take removes existing elements until empty, then blocks interruptibly |
687 |
|
*/ |
688 |
|
public void testBlockingTake() throws InterruptedException { |
762 |
|
} |
763 |
|
|
764 |
|
/** |
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 |
– |
/** |
765 |
|
* putFirst(null) throws NPE |
766 |
|
*/ |
767 |
|
public void testPutFirstNull() throws InterruptedException { |
952 |
|
} |
953 |
|
|
954 |
|
/** |
955 |
< |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
956 |
< |
* on interruption throws |
955 |
> |
* timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds; |
956 |
> |
* on interruption throws |
957 |
|
*/ |
958 |
|
public void testTimedPollFirstWithOfferFirst() throws InterruptedException { |
959 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1164 |
|
} |
1165 |
|
|
1166 |
|
/** |
1167 |
< |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1168 |
< |
* on interruption throws |
1167 |
> |
* timed poll before a delayed offerLast fails; after offerLast succeeds; |
1168 |
> |
* on interruption throws |
1169 |
|
*/ |
1170 |
|
public void testTimedPollWithOfferLast() throws InterruptedException { |
1171 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1208 |
|
public void testRemoveElement() { |
1209 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1210 |
|
for (int i = 1; i < SIZE; i+=2) { |
1211 |
< |
assertTrue(q.remove(new Integer(i))); |
1211 |
> |
assertTrue(q.contains(i)); |
1212 |
> |
assertTrue(q.remove(i)); |
1213 |
> |
assertFalse(q.contains(i)); |
1214 |
> |
assertTrue(q.contains(i-1)); |
1215 |
|
} |
1216 |
|
for (int i = 0; i < SIZE; i+=2) { |
1217 |
< |
assertTrue(q.remove(new Integer(i))); |
1218 |
< |
assertFalse(q.remove(new Integer(i+1))); |
1217 |
> |
assertTrue(q.contains(i)); |
1218 |
> |
assertTrue(q.remove(i)); |
1219 |
> |
assertFalse(q.contains(i)); |
1220 |
> |
assertFalse(q.remove(i+1)); |
1221 |
> |
assertFalse(q.contains(i+1)); |
1222 |
|
} |
1223 |
|
assertTrue(q.isEmpty()); |
1224 |
|
} |
1301 |
|
} |
1302 |
|
|
1303 |
|
/** |
1304 |
< |
* toArray contains all elements |
1304 |
> |
* toArray contains all elements in FIFO order |
1305 |
|
*/ |
1306 |
|
public void testToArray() throws InterruptedException{ |
1307 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1308 |
|
Object[] o = q.toArray(); |
1309 |
|
for (int i = 0; i < o.length; i++) |
1310 |
< |
assertEquals(o[i], q.take()); |
1310 |
> |
assertSame(o[i], q.poll()); |
1311 |
|
} |
1312 |
|
|
1313 |
|
/** |
1314 |
< |
* toArray(a) contains all elements |
1314 |
> |
* toArray(a) contains all elements in FIFO order |
1315 |
|
*/ |
1316 |
< |
public void testToArray2() throws InterruptedException { |
1317 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1316 |
> |
public void testToArray2() { |
1317 |
> |
LinkedBlockingDeque<Integer> q = populatedDeque(SIZE); |
1318 |
|
Integer[] ints = new Integer[SIZE]; |
1319 |
< |
ints = (Integer[])q.toArray(ints); |
1319 |
> |
Integer[] array = q.toArray(ints); |
1320 |
> |
assertSame(ints, array); |
1321 |
|
for (int i = 0; i < ints.length; i++) |
1322 |
< |
assertEquals(ints[i], q.take()); |
1322 |
> |
assertSame(ints[i], q.remove()); |
1323 |
|
} |
1324 |
|
|
1325 |
|
/** |
1326 |
< |
* toArray(null) throws NPE |
1326 |
> |
* toArray(null) throws NullPointerException |
1327 |
|
*/ |
1328 |
< |
public void testToArray_BadArg() { |
1328 |
> |
public void testToArray_NullArg() { |
1329 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1330 |
|
try { |
1331 |
< |
Object o[] = q.toArray(null); |
1331 |
> |
q.toArray(null); |
1332 |
|
shouldThrow(); |
1333 |
|
} catch (NullPointerException success) {} |
1334 |
|
} |
1335 |
|
|
1336 |
|
/** |
1337 |
< |
* toArray with incompatible array type throws CCE |
1337 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
1338 |
|
*/ |
1339 |
|
public void testToArray1_BadArg() { |
1340 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1341 |
|
try { |
1342 |
< |
Object o[] = q.toArray(new String[10]); |
1342 |
> |
q.toArray(new String[10]); |
1343 |
|
shouldThrow(); |
1344 |
|
} catch (ArrayStoreException success) {} |
1345 |
|
} |
1409 |
|
|
1410 |
|
|
1411 |
|
/** |
1412 |
< |
* Descending iterator iterates through all elements |
1412 |
> |
* Descending iterator iterates through all elements |
1413 |
|
*/ |
1414 |
|
public void testDescendingIterator() { |
1415 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1428 |
|
} |
1429 |
|
|
1430 |
|
/** |
1431 |
< |
* Descending iterator ordering is reverse FIFO |
1431 |
> |
* Descending iterator ordering is reverse FIFO |
1432 |
|
*/ |
1433 |
|
public void testDescendingIteratorOrdering() { |
1434 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1637 |
|
} |
1638 |
|
|
1639 |
|
/** |
1640 |
< |
* drainTo(c, n) empties first max {n, size} elements of deque into c |
1640 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1641 |
|
*/ |
1642 |
|
public void testDrainToN() { |
1643 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
1646 |
|
assertTrue(q.offer(new Integer(j))); |
1647 |
|
ArrayList l = new ArrayList(); |
1648 |
|
q.drainTo(l, i); |
1649 |
< |
int k = (i < SIZE)? i : SIZE; |
1649 |
> |
int k = (i < SIZE) ? i : SIZE; |
1650 |
|
assertEquals(l.size(), k); |
1651 |
|
assertEquals(q.size(), SIZE-k); |
1652 |
|
for (int j = 0; j < k; ++j) |