37 |
|
} |
38 |
|
|
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run(suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
|
43 |
|
public static Test suite() { |
48 |
|
|
49 |
|
/** |
50 |
|
* Returns a new deque of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
|
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
54 |
|
LinkedBlockingDeque<Integer> q = |
59 |
|
assertFalse(q.isEmpty()); |
60 |
|
assertEquals(0, q.remainingCapacity()); |
61 |
|
assertEquals(n, q.size()); |
62 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
63 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
64 |
|
return q; |
65 |
|
} |
66 |
|
|
84 |
|
public void testSize() { |
85 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
86 |
|
for (int i = 0; i < SIZE; ++i) { |
87 |
< |
assertEquals(SIZE-i, q.size()); |
87 |
> |
assertEquals(SIZE - i, q.size()); |
88 |
|
q.removeFirst(); |
89 |
|
} |
90 |
|
for (int i = 0; i < SIZE; ++i) { |
149 |
|
*/ |
150 |
|
public void testPollLast() { |
151 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
152 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
152 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
153 |
|
assertEquals(i, q.pollLast()); |
154 |
|
} |
155 |
|
assertNull(q.pollLast()); |
188 |
|
*/ |
189 |
|
public void testPeekLast() { |
190 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
191 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
191 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
192 |
|
assertEquals(i, q.peekLast()); |
193 |
|
assertEquals(i, q.pollLast()); |
194 |
|
assertTrue(q.peekLast() == null || |
218 |
|
*/ |
219 |
|
public void testLastElement() { |
220 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
221 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
221 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
222 |
|
assertEquals(i, q.getLast()); |
223 |
|
assertEquals(i, q.pollLast()); |
224 |
|
} |
283 |
|
} |
284 |
|
for (int i = 0; i < SIZE; i += 2) { |
285 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
286 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
286 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
287 |
|
} |
288 |
|
assertTrue(q.isEmpty()); |
289 |
|
} |
298 |
|
} |
299 |
|
for (int i = 0; i < SIZE; i += 2) { |
300 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
301 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
301 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
302 |
|
} |
303 |
|
assertTrue(q.isEmpty()); |
304 |
|
} |
369 |
|
*/ |
370 |
|
public void testConstructor5() { |
371 |
|
Integer[] ints = new Integer[SIZE]; |
372 |
< |
for (int i = 0; i < SIZE-1; ++i) |
372 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
373 |
|
ints[i] = i; |
374 |
|
Collection<Integer> elements = Arrays.asList(ints); |
375 |
|
try { |
416 |
|
assertEquals(i, q.remove()); |
417 |
|
} |
418 |
|
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
419 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
420 |
|
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
421 |
|
assertTrue(q.add(i)); |
422 |
|
} |
426 |
|
* push(null) throws NPE |
427 |
|
*/ |
428 |
|
public void testPushNull() { |
429 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
430 |
|
try { |
428 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
431 |
|
q.push(null); |
432 |
|
shouldThrow(); |
433 |
|
} catch (NullPointerException success) {} |
437 |
|
* push succeeds if not full; throws ISE if full |
438 |
|
*/ |
439 |
|
public void testPush() { |
440 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
441 |
+ |
for (int i = 0; i < SIZE; ++i) { |
442 |
+ |
Integer x = new Integer(i); |
443 |
+ |
q.push(x); |
444 |
+ |
assertEquals(x, q.peek()); |
445 |
+ |
} |
446 |
+ |
assertEquals(0, q.remainingCapacity()); |
447 |
|
try { |
439 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
440 |
– |
for (int i = 0; i < SIZE; ++i) { |
441 |
– |
Integer x = new Integer(i); |
442 |
– |
q.push(x); |
443 |
– |
assertEquals(x, q.peek()); |
444 |
– |
} |
445 |
– |
assertEquals(0, q.remainingCapacity()); |
448 |
|
q.push(new Integer(SIZE)); |
449 |
|
shouldThrow(); |
450 |
|
} catch (IllegalStateException success) {} |
515 |
|
public void testAddAll3() { |
516 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
517 |
|
Integer[] ints = new Integer[SIZE]; |
518 |
< |
for (int i = 0; i < SIZE-1; ++i) |
518 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
519 |
|
ints[i] = new Integer(i); |
520 |
|
Collection<Integer> elements = Arrays.asList(ints); |
521 |
|
try { |
753 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
754 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
755 |
|
public void realRun() throws InterruptedException { |
756 |
+ |
long startTime = System.nanoTime(); |
757 |
|
for (int i = 0; i < SIZE; ++i) { |
755 |
– |
long t0 = System.nanoTime(); |
758 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
757 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
759 |
|
} |
759 |
– |
long t0 = System.nanoTime(); |
760 |
|
aboutToWait.countDown(); |
761 |
|
try { |
762 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
762 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
763 |
|
shouldThrow(); |
764 |
|
} catch (InterruptedException success) { |
765 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
765 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
766 |
|
} |
767 |
|
}}); |
768 |
|
|
769 |
|
aboutToWait.await(); |
770 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
770 |
> |
waitForThreadToEnterWaitState(t); |
771 |
|
t.interrupt(); |
772 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
772 |
> |
awaitTermination(t); |
773 |
|
checkEmpty(q); |
774 |
|
} |
775 |
|
|
1050 |
|
* returning timeout status |
1051 |
|
*/ |
1052 |
|
public void testInterruptedTimedPollFirst() throws InterruptedException { |
1053 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1054 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1055 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1056 |
|
public void realRun() throws InterruptedException { |
1057 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1057 |
> |
long startTime = System.nanoTime(); |
1058 |
|
for (int i = 0; i < SIZE; ++i) { |
1059 |
|
assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
1060 |
|
} |
1061 |
|
|
1062 |
|
Thread.currentThread().interrupt(); |
1063 |
|
try { |
1064 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1064 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1065 |
|
shouldThrow(); |
1066 |
|
} catch (InterruptedException success) {} |
1067 |
|
assertFalse(Thread.interrupted()); |
1072 |
|
shouldThrow(); |
1073 |
|
} catch (InterruptedException success) {} |
1074 |
|
assertFalse(Thread.interrupted()); |
1075 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1076 |
|
}}); |
1077 |
|
|
1078 |
|
await(pleaseInterrupt); |
1248 |
|
public void testTakeLast() throws InterruptedException { |
1249 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1250 |
|
for (int i = 0; i < SIZE; ++i) { |
1251 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1251 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1252 |
|
} |
1253 |
|
} |
1254 |
|
|
1261 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1262 |
|
public void realRun() throws InterruptedException { |
1263 |
|
for (int i = 0; i < SIZE; ++i) { |
1264 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1264 |
> |
assertEquals(SIZE - i - 1, q.takeLast()); |
1265 |
|
} |
1266 |
|
|
1267 |
|
Thread.currentThread().interrupt(); |
1291 |
|
public void testTimedPollLast0() throws InterruptedException { |
1292 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1293 |
|
for (int i = 0; i < SIZE; ++i) { |
1294 |
< |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1294 |
> |
assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS)); |
1295 |
|
} |
1296 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1297 |
|
} |
1303 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1304 |
|
for (int i = 0; i < SIZE; ++i) { |
1305 |
|
long startTime = System.nanoTime(); |
1306 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1306 |
> |
assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1307 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1308 |
|
} |
1309 |
|
long startTime = System.nanoTime(); |
1317 |
|
* returning timeout status |
1318 |
|
*/ |
1319 |
|
public void testInterruptedTimedPollLast() throws InterruptedException { |
1320 |
+ |
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1321 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
1322 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1323 |
|
public void realRun() throws InterruptedException { |
1324 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1324 |
> |
long startTime = System.nanoTime(); |
1325 |
|
for (int i = 0; i < SIZE; ++i) { |
1326 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1326 |
> |
assertEquals(SIZE - i - 1, |
1327 |
> |
q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1328 |
|
} |
1329 |
|
|
1330 |
|
Thread.currentThread().interrupt(); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (InterruptedException success) {} |
1342 |
|
assertFalse(Thread.interrupted()); |
1343 |
+ |
|
1344 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1345 |
|
}}); |
1346 |
|
|
1347 |
|
await(pleaseInterrupt); |
1348 |
|
assertThreadStaysAlive(t); |
1349 |
|
t.interrupt(); |
1350 |
|
awaitTermination(t); |
1351 |
+ |
checkEmpty(q); |
1352 |
|
} |
1353 |
|
|
1354 |
|
/** |
1381 |
|
shouldThrow(); |
1382 |
|
} catch (InterruptedException success) {} |
1383 |
|
assertFalse(Thread.interrupted()); |
1384 |
+ |
|
1385 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1386 |
|
}}); |
1387 |
|
|
1388 |
|
barrier.await(); |
1467 |
|
assertTrue(changed); |
1468 |
|
|
1469 |
|
assertTrue(q.containsAll(p)); |
1470 |
< |
assertEquals(SIZE-i, q.size()); |
1470 |
> |
assertEquals(SIZE - i, q.size()); |
1471 |
|
p.remove(); |
1472 |
|
} |
1473 |
|
} |
1480 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1481 |
|
LinkedBlockingDeque p = populatedDeque(i); |
1482 |
|
assertTrue(q.removeAll(p)); |
1483 |
< |
assertEquals(SIZE-i, q.size()); |
1483 |
> |
assertEquals(SIZE - i, q.size()); |
1484 |
|
for (int j = 0; j < i; ++j) { |
1485 |
|
Integer x = (Integer)(p.remove()); |
1486 |
|
assertFalse(q.contains(x)); |
1679 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1680 |
|
q.add(one); |
1681 |
|
q.add(two); |
1673 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1682 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1683 |
< |
executor.execute(new CheckedRunnable() { |
1684 |
< |
public void realRun() throws InterruptedException { |
1685 |
< |
assertFalse(q.offer(three)); |
1686 |
< |
threadsStarted.await(); |
1687 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1688 |
< |
assertEquals(0, q.remainingCapacity()); |
1689 |
< |
}}); |
1690 |
< |
|
1691 |
< |
executor.execute(new CheckedRunnable() { |
1692 |
< |
public void realRun() throws InterruptedException { |
1693 |
< |
threadsStarted.await(); |
1694 |
< |
assertSame(one, q.take()); |
1695 |
< |
}}); |
1696 |
< |
|
1697 |
< |
joinPool(executor); |
1683 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1684 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1685 |
> |
executor.execute(new CheckedRunnable() { |
1686 |
> |
public void realRun() throws InterruptedException { |
1687 |
> |
assertFalse(q.offer(three)); |
1688 |
> |
threadsStarted.await(); |
1689 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
1690 |
> |
assertEquals(0, q.remainingCapacity()); |
1691 |
> |
}}); |
1692 |
> |
|
1693 |
> |
executor.execute(new CheckedRunnable() { |
1694 |
> |
public void realRun() throws InterruptedException { |
1695 |
> |
threadsStarted.await(); |
1696 |
> |
assertSame(one, q.take()); |
1697 |
> |
}}); |
1698 |
> |
} |
1699 |
|
} |
1700 |
|
|
1701 |
|
/** |
1704 |
|
public void testPollInExecutor() { |
1705 |
|
final LinkedBlockingDeque q = new LinkedBlockingDeque(2); |
1706 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
1707 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
1708 |
< |
executor.execute(new CheckedRunnable() { |
1709 |
< |
public void realRun() throws InterruptedException { |
1710 |
< |
assertNull(q.poll()); |
1711 |
< |
threadsStarted.await(); |
1712 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1713 |
< |
checkEmpty(q); |
1714 |
< |
}}); |
1715 |
< |
|
1716 |
< |
executor.execute(new CheckedRunnable() { |
1717 |
< |
public void realRun() throws InterruptedException { |
1718 |
< |
threadsStarted.await(); |
1719 |
< |
q.put(one); |
1720 |
< |
}}); |
1721 |
< |
|
1722 |
< |
joinPool(executor); |
1707 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
1708 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
1709 |
> |
executor.execute(new CheckedRunnable() { |
1710 |
> |
public void realRun() throws InterruptedException { |
1711 |
> |
assertNull(q.poll()); |
1712 |
> |
threadsStarted.await(); |
1713 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
1714 |
> |
checkEmpty(q); |
1715 |
> |
}}); |
1716 |
> |
|
1717 |
> |
executor.execute(new CheckedRunnable() { |
1718 |
> |
public void realRun() throws InterruptedException { |
1719 |
> |
threadsStarted.await(); |
1720 |
> |
q.put(one); |
1721 |
> |
}}); |
1722 |
> |
} |
1723 |
|
} |
1724 |
|
|
1725 |
|
/** |
1771 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1772 |
|
Thread t = new Thread(new CheckedRunnable() { |
1773 |
|
public void realRun() throws InterruptedException { |
1774 |
< |
q.put(new Integer(SIZE+1)); |
1774 |
> |
q.put(new Integer(SIZE + 1)); |
1775 |
|
}}); |
1776 |
|
|
1777 |
|
t.start(); |
1796 |
|
q.drainTo(l, i); |
1797 |
|
int k = (i < SIZE) ? i : SIZE; |
1798 |
|
assertEquals(k, l.size()); |
1799 |
< |
assertEquals(SIZE-k, q.size()); |
1799 |
> |
assertEquals(SIZE - k, q.size()); |
1800 |
|
for (int j = 0; j < k; ++j) |
1801 |
|
assertEquals(l.get(j), new Integer(j)); |
1802 |
|
do {} while (q.poll() != null); |