5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.ArrayList; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NoSuchElementException; |
13 |
> |
import java.util.Queue; |
14 |
> |
import java.util.concurrent.BlockingDeque; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.Executors; |
18 |
> |
import java.util.concurrent.ExecutorService; |
19 |
> |
import java.util.concurrent.LinkedBlockingDeque; |
20 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
– |
import java.io.*; |
21 |
|
|
22 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
23 |
|
|
29 |
|
|
30 |
|
public static class Bounded extends BlockingQueueTest { |
31 |
|
protected BlockingQueue emptyCollection() { |
32 |
< |
return new LinkedBlockingDeque(20); |
32 |
> |
return new LinkedBlockingDeque(SIZE); |
33 |
|
} |
34 |
|
} |
35 |
|
|
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* offer(null) throws NPE |
92 |
> |
* offerFirst(null) throws NullPointerException |
93 |
|
*/ |
94 |
|
public void testOfferFirstNull() { |
95 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
96 |
|
try { |
87 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
97 |
|
q.offerFirst(null); |
98 |
|
shouldThrow(); |
99 |
|
} catch (NullPointerException success) {} |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
+ |
* offerLast(null) throws NullPointerException |
104 |
+ |
*/ |
105 |
+ |
public void testOfferLastNull() { |
106 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(); |
107 |
+ |
try { |
108 |
+ |
q.offerLast(null); |
109 |
+ |
shouldThrow(); |
110 |
+ |
} catch (NullPointerException success) {} |
111 |
+ |
} |
112 |
+ |
|
113 |
+ |
/** |
114 |
|
* OfferFirst succeeds |
115 |
|
*/ |
116 |
|
public void testOfferFirst() { |
328 |
|
} |
329 |
|
|
330 |
|
/** |
331 |
< |
* Constructor throws IAE if capacity argument nonpositive |
331 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
332 |
|
*/ |
333 |
|
public void testConstructor2() { |
334 |
|
try { |
335 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(0); |
335 |
> |
new LinkedBlockingDeque(0); |
336 |
|
shouldThrow(); |
337 |
|
} catch (IllegalArgumentException success) {} |
338 |
|
} |
339 |
|
|
340 |
|
/** |
341 |
< |
* Initializing from null Collection throws NPE |
341 |
> |
* Initializing from null Collection throws NullPointerException |
342 |
|
*/ |
343 |
|
public void testConstructor3() { |
344 |
|
try { |
345 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(null); |
345 |
> |
new LinkedBlockingDeque(null); |
346 |
|
shouldThrow(); |
347 |
|
} catch (NullPointerException success) {} |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* Initializing from Collection of null elements throws NPE |
351 |
> |
* Initializing from Collection of null elements throws NullPointerException |
352 |
|
*/ |
353 |
|
public void testConstructor4() { |
354 |
+ |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
355 |
|
try { |
356 |
< |
Integer[] ints = new Integer[SIZE]; |
336 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
356 |
> |
new LinkedBlockingDeque(elements); |
357 |
|
shouldThrow(); |
358 |
|
} catch (NullPointerException success) {} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* Initializing from Collection with some null elements throws NPE |
362 |
> |
* Initializing from Collection with some null elements throws |
363 |
> |
* NullPointerException |
364 |
|
*/ |
365 |
|
public void testConstructor5() { |
366 |
+ |
Integer[] ints = new Integer[SIZE]; |
367 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
368 |
+ |
ints[i] = i; |
369 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
370 |
|
try { |
371 |
< |
Integer[] ints = new Integer[SIZE]; |
347 |
< |
for (int i = 0; i < SIZE-1; ++i) |
348 |
< |
ints[i] = new Integer(i); |
349 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
371 |
> |
new LinkedBlockingDeque(elements); |
372 |
|
shouldThrow(); |
373 |
|
} catch (NullPointerException success) {} |
374 |
|
} |
379 |
|
public void testConstructor6() { |
380 |
|
Integer[] ints = new Integer[SIZE]; |
381 |
|
for (int i = 0; i < SIZE; ++i) |
382 |
< |
ints[i] = new Integer(i); |
382 |
> |
ints[i] = i; |
383 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); |
384 |
|
for (int i = 0; i < SIZE; ++i) |
385 |
|
assertEquals(ints[i], q.poll()); |
418 |
|
} |
419 |
|
|
420 |
|
/** |
399 |
– |
* offer(null) throws NPE |
400 |
– |
*/ |
401 |
– |
public void testOfferNull() { |
402 |
– |
try { |
403 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
404 |
– |
q.offer(null); |
405 |
– |
shouldThrow(); |
406 |
– |
} catch (NullPointerException success) {} |
407 |
– |
} |
408 |
– |
|
409 |
– |
/** |
410 |
– |
* add(null) throws NPE |
411 |
– |
*/ |
412 |
– |
public void testAddNull() { |
413 |
– |
try { |
414 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
415 |
– |
q.add(null); |
416 |
– |
shouldThrow(); |
417 |
– |
} catch (NullPointerException success) {} |
418 |
– |
} |
419 |
– |
|
420 |
– |
/** |
421 |
|
* push(null) throws NPE |
422 |
|
*/ |
423 |
|
public void testPushNull() { |
482 |
|
* add succeeds if not full; throws ISE if full |
483 |
|
*/ |
484 |
|
public void testAdd() { |
485 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
486 |
+ |
for (int i = 0; i < SIZE; ++i) |
487 |
+ |
assertTrue(q.add(new Integer(i))); |
488 |
+ |
assertEquals(0, q.remainingCapacity()); |
489 |
|
try { |
486 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
487 |
– |
for (int i = 0; i < SIZE; ++i) { |
488 |
– |
assertTrue(q.add(new Integer(i))); |
489 |
– |
} |
490 |
– |
assertEquals(0, q.remainingCapacity()); |
490 |
|
q.add(new Integer(SIZE)); |
491 |
|
shouldThrow(); |
492 |
|
} catch (IllegalStateException success) {} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
497 |
– |
* addAll(null) throws NPE |
498 |
– |
*/ |
499 |
– |
public void testAddAll1() { |
500 |
– |
try { |
501 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
502 |
– |
q.addAll(null); |
503 |
– |
shouldThrow(); |
504 |
– |
} catch (NullPointerException success) {} |
505 |
– |
} |
506 |
– |
|
507 |
– |
/** |
496 |
|
* addAll(this) throws IAE |
497 |
|
*/ |
498 |
|
public void testAddAllSelf() { |
499 |
+ |
LinkedBlockingDeque q = populatedDeque(SIZE); |
500 |
|
try { |
512 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
501 |
|
q.addAll(q); |
502 |
|
shouldThrow(); |
503 |
|
} catch (IllegalArgumentException success) {} |
504 |
|
} |
505 |
|
|
506 |
|
/** |
519 |
– |
* addAll of a collection with null elements throws NPE |
520 |
– |
*/ |
521 |
– |
public void testAddAll2() { |
522 |
– |
try { |
523 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
524 |
– |
Integer[] ints = new Integer[SIZE]; |
525 |
– |
q.addAll(Arrays.asList(ints)); |
526 |
– |
shouldThrow(); |
527 |
– |
} catch (NullPointerException success) {} |
528 |
– |
} |
529 |
– |
|
530 |
– |
/** |
507 |
|
* addAll of a collection with any null elements throws NPE after |
508 |
|
* possibly adding some elements |
509 |
|
*/ |
510 |
|
public void testAddAll3() { |
511 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
512 |
+ |
Integer[] ints = new Integer[SIZE]; |
513 |
+ |
for (int i = 0; i < SIZE-1; ++i) |
514 |
+ |
ints[i] = new Integer(i); |
515 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
516 |
|
try { |
517 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
537 |
< |
Integer[] ints = new Integer[SIZE]; |
538 |
< |
for (int i = 0; i < SIZE-1; ++i) |
539 |
< |
ints[i] = new Integer(i); |
540 |
< |
q.addAll(Arrays.asList(ints)); |
517 |
> |
q.addAll(elements); |
518 |
|
shouldThrow(); |
519 |
|
} catch (NullPointerException success) {} |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
< |
* addAll throws ISE if not enough room |
523 |
> |
* addAll throws IllegalStateException if not enough room |
524 |
|
*/ |
525 |
|
public void testAddAll4() { |
526 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1); |
527 |
+ |
Integer[] ints = new Integer[SIZE]; |
528 |
+ |
for (int i = 0; i < SIZE; ++i) |
529 |
+ |
ints[i] = new Integer(i); |
530 |
+ |
Collection<Integer> elements = Arrays.asList(ints); |
531 |
|
try { |
532 |
< |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
551 |
< |
Integer[] ints = new Integer[SIZE]; |
552 |
< |
for (int i = 0; i < SIZE; ++i) |
553 |
< |
ints[i] = new Integer(i); |
554 |
< |
q.addAll(Arrays.asList(ints)); |
532 |
> |
q.addAll(elements); |
533 |
|
shouldThrow(); |
534 |
|
} catch (IllegalStateException success) {} |
535 |
|
} |
550 |
|
} |
551 |
|
|
552 |
|
/** |
575 |
– |
* put(null) throws NPE |
576 |
– |
*/ |
577 |
– |
public void testPutNull() throws InterruptedException { |
578 |
– |
try { |
579 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
580 |
– |
q.put(null); |
581 |
– |
shouldThrow(); |
582 |
– |
} catch (NullPointerException success) {} |
583 |
– |
} |
584 |
– |
|
585 |
– |
/** |
553 |
|
* all elements successfully put are contained |
554 |
|
*/ |
555 |
|
public void testPut() throws InterruptedException { |
774 |
|
* putFirst(null) throws NPE |
775 |
|
*/ |
776 |
|
public void testPutFirstNull() throws InterruptedException { |
777 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
778 |
|
try { |
811 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
779 |
|
q.putFirst(null); |
780 |
|
shouldThrow(); |
781 |
|
} catch (NullPointerException success) {} |
1121 |
|
* putLast(null) throws NPE |
1122 |
|
*/ |
1123 |
|
public void testPutLastNull() throws InterruptedException { |
1124 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1125 |
|
try { |
1158 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1126 |
|
q.putLast(null); |
1127 |
|
shouldThrow(); |
1128 |
|
} catch (NullPointerException success) {} |
1400 |
|
} |
1401 |
|
|
1402 |
|
/** |
1436 |
– |
* remove(x) removes x and returns true if present |
1437 |
– |
*/ |
1438 |
– |
public void testRemoveElement() { |
1439 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1440 |
– |
for (int i = 1; i < SIZE; i+=2) { |
1441 |
– |
assertTrue(q.contains(i)); |
1442 |
– |
assertTrue(q.remove(i)); |
1443 |
– |
assertFalse(q.contains(i)); |
1444 |
– |
assertTrue(q.contains(i-1)); |
1445 |
– |
} |
1446 |
– |
for (int i = 0; i < SIZE; i+=2) { |
1447 |
– |
assertTrue(q.contains(i)); |
1448 |
– |
assertTrue(q.remove(i)); |
1449 |
– |
assertFalse(q.contains(i)); |
1450 |
– |
assertFalse(q.remove(i+1)); |
1451 |
– |
assertFalse(q.contains(i+1)); |
1452 |
– |
} |
1453 |
– |
assertTrue(q.isEmpty()); |
1454 |
– |
} |
1455 |
– |
|
1456 |
– |
/** |
1403 |
|
* contains(x) reports true when elements added but not yet removed |
1404 |
|
*/ |
1405 |
|
public void testContains() { |
1479 |
|
/** |
1480 |
|
* toArray contains all elements in FIFO order |
1481 |
|
*/ |
1482 |
< |
public void testToArray() throws InterruptedException{ |
1482 |
> |
public void testToArray() throws InterruptedException { |
1483 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1484 |
|
Object[] o = q.toArray(); |
1485 |
|
for (int i = 0; i < o.length; i++) |
1499 |
|
} |
1500 |
|
|
1501 |
|
/** |
1556 |
– |
* toArray(null) throws NullPointerException |
1557 |
– |
*/ |
1558 |
– |
public void testToArray_NullArg() { |
1559 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1560 |
– |
try { |
1561 |
– |
q.toArray(null); |
1562 |
– |
shouldThrow(); |
1563 |
– |
} catch (NullPointerException success) {} |
1564 |
– |
} |
1565 |
– |
|
1566 |
– |
/** |
1502 |
|
* toArray(incompatible array type) throws ArrayStoreException |
1503 |
|
*/ |
1504 |
|
public void testToArray1_BadArg() { |
1697 |
|
* A deserialized serialized deque has same elements in same order |
1698 |
|
*/ |
1699 |
|
public void testSerialization() throws Exception { |
1700 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1701 |
< |
|
1767 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1768 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1769 |
< |
out.writeObject(q); |
1770 |
< |
out.close(); |
1771 |
< |
|
1772 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1773 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1774 |
< |
LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject(); |
1775 |
< |
assertEquals(q.size(), r.size()); |
1776 |
< |
while (!q.isEmpty()) |
1777 |
< |
assertEquals(q.remove(), r.remove()); |
1778 |
< |
} |
1700 |
> |
Queue x = populatedDeque(SIZE); |
1701 |
> |
Queue y = serialClone(x); |
1702 |
|
|
1703 |
< |
/** |
1704 |
< |
* drainTo(null) throws NPE |
1705 |
< |
*/ |
1706 |
< |
public void testDrainToNull() { |
1707 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1708 |
< |
try { |
1709 |
< |
q.drainTo(null); |
1710 |
< |
shouldThrow(); |
1711 |
< |
} catch (NullPointerException success) {} |
1789 |
< |
} |
1790 |
< |
|
1791 |
< |
/** |
1792 |
< |
* drainTo(this) throws IAE |
1793 |
< |
*/ |
1794 |
< |
public void testDrainToSelf() { |
1795 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1796 |
< |
try { |
1797 |
< |
q.drainTo(q); |
1798 |
< |
shouldThrow(); |
1799 |
< |
} catch (IllegalArgumentException success) {} |
1703 |
> |
assertTrue(x != y); |
1704 |
> |
assertEquals(x.size(), y.size()); |
1705 |
> |
assertEquals(x.toString(), y.toString()); |
1706 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
1707 |
> |
while (!x.isEmpty()) { |
1708 |
> |
assertFalse(y.isEmpty()); |
1709 |
> |
assertEquals(x.remove(), y.remove()); |
1710 |
> |
} |
1711 |
> |
assertTrue(y.isEmpty()); |
1712 |
|
} |
1713 |
|
|
1714 |
|
/** |
1756 |
|
} |
1757 |
|
|
1758 |
|
/** |
1847 |
– |
* drainTo(null, n) throws NPE |
1848 |
– |
*/ |
1849 |
– |
public void testDrainToNullN() { |
1850 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1851 |
– |
try { |
1852 |
– |
q.drainTo(null, 0); |
1853 |
– |
shouldThrow(); |
1854 |
– |
} catch (NullPointerException success) {} |
1855 |
– |
} |
1856 |
– |
|
1857 |
– |
/** |
1858 |
– |
* drainTo(this, n) throws IAE |
1859 |
– |
*/ |
1860 |
– |
public void testDrainToSelfN() { |
1861 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1862 |
– |
try { |
1863 |
– |
q.drainTo(q, 0); |
1864 |
– |
shouldThrow(); |
1865 |
– |
} catch (IllegalArgumentException success) {} |
1866 |
– |
} |
1867 |
– |
|
1868 |
– |
/** |
1759 |
|
* drainTo(c, n) empties first min(n, size) elements of queue into c |
1760 |
|
*/ |
1761 |
|
public void testDrainToN() { |