4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.Arrays; |
7 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
8 |
> |
|
9 |
|
import java.util.ArrayList; |
10 |
+ |
import java.util.Arrays; |
11 |
|
import java.util.Collection; |
12 |
+ |
import java.util.Deque; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.Queue; |
19 |
|
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
|
import java.util.concurrent.LinkedBlockingDeque; |
22 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingDequeTest extends JSR166TestCase { |
26 |
|
|
32 |
|
|
33 |
|
public static class Bounded extends BlockingQueueTest { |
34 |
|
protected BlockingQueue emptyCollection() { |
35 |
< |
return new LinkedBlockingDeque(20); |
35 |
> |
return new LinkedBlockingDeque(SIZE); |
36 |
|
} |
37 |
|
} |
38 |
|
|
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create a deque of given size containing consecutive |
50 |
> |
* Returns a new deque of given size containing consecutive |
51 |
|
* Integers 0 ... n. |
52 |
|
*/ |
53 |
|
private LinkedBlockingDeque<Integer> populatedDeque(int n) { |
276 |
|
*/ |
277 |
|
public void testRemoveFirstOccurrence() { |
278 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
279 |
< |
for (int i = 1; i < SIZE; i+=2) { |
279 |
> |
for (int i = 1; i < SIZE; i += 2) { |
280 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
281 |
|
} |
282 |
< |
for (int i = 0; i < SIZE; i+=2) { |
282 |
> |
for (int i = 0; i < SIZE; i += 2) { |
283 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
284 |
|
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
285 |
|
} |
291 |
|
*/ |
292 |
|
public void testRemoveLastOccurrence() { |
293 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
294 |
< |
for (int i = 1; i < SIZE; i+=2) { |
294 |
> |
for (int i = 1; i < SIZE; i += 2) { |
295 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
296 |
|
} |
297 |
< |
for (int i = 0; i < SIZE; i+=2) { |
297 |
> |
for (int i = 0; i < SIZE; i += 2) { |
298 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
299 |
|
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
300 |
|
} |
407 |
|
* remainingCapacity decreases on add, increases on remove |
408 |
|
*/ |
409 |
|
public void testRemainingCapacity() { |
410 |
< |
LinkedBlockingDeque q = populatedDeque(SIZE); |
410 |
> |
BlockingQueue q = populatedDeque(SIZE); |
411 |
|
for (int i = 0; i < SIZE; ++i) { |
412 |
|
assertEquals(i, q.remainingCapacity()); |
413 |
< |
assertEquals(SIZE-i, q.size()); |
414 |
< |
q.remove(); |
413 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
414 |
> |
assertEquals(i, q.remove()); |
415 |
|
} |
416 |
|
for (int i = 0; i < SIZE; ++i) { |
417 |
|
assertEquals(SIZE-i, q.remainingCapacity()); |
418 |
< |
assertEquals(i, q.size()); |
419 |
< |
q.add(new Integer(i)); |
418 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
419 |
> |
assertTrue(q.add(i)); |
420 |
|
} |
421 |
|
} |
422 |
|
|
438 |
|
try { |
439 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
440 |
|
for (int i = 0; i < SIZE; ++i) { |
441 |
< |
Integer I = new Integer(i); |
442 |
< |
q.push(I); |
443 |
< |
assertEquals(I, q.peek()); |
441 |
> |
Integer x = new Integer(i); |
442 |
> |
q.push(x); |
443 |
> |
assertEquals(x, q.peek()); |
444 |
|
} |
445 |
|
assertEquals(0, q.remainingCapacity()); |
446 |
|
q.push(new Integer(SIZE)); |
558 |
|
public void testPut() throws InterruptedException { |
559 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
560 |
|
for (int i = 0; i < SIZE; ++i) { |
561 |
< |
Integer I = new Integer(i); |
562 |
< |
q.put(I); |
563 |
< |
assertTrue(q.contains(I)); |
561 |
> |
Integer x = new Integer(i); |
562 |
> |
q.put(x); |
563 |
> |
assertTrue(q.contains(x)); |
564 |
|
} |
565 |
|
assertEquals(0, q.remainingCapacity()); |
566 |
|
} |
625 |
|
}}); |
626 |
|
|
627 |
|
await(pleaseTake); |
628 |
< |
assertEquals(q.remainingCapacity(), 0); |
628 |
> |
assertEquals(0, q.remainingCapacity()); |
629 |
|
assertEquals(0, q.take()); |
630 |
|
|
631 |
|
await(pleaseInterrupt); |
632 |
|
assertThreadStaysAlive(t); |
633 |
|
t.interrupt(); |
634 |
|
awaitTermination(t); |
635 |
< |
assertEquals(q.remainingCapacity(), 0); |
635 |
> |
assertEquals(0, q.remainingCapacity()); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
790 |
|
public void testPutFirst() throws InterruptedException { |
791 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
792 |
|
for (int i = 0; i < SIZE; ++i) { |
793 |
< |
Integer I = new Integer(i); |
794 |
< |
q.putFirst(I); |
795 |
< |
assertTrue(q.contains(I)); |
793 |
> |
Integer x = new Integer(i); |
794 |
> |
q.putFirst(x); |
795 |
> |
assertTrue(q.contains(x)); |
796 |
|
} |
797 |
|
assertEquals(0, q.remainingCapacity()); |
798 |
|
} |
857 |
|
}}); |
858 |
|
|
859 |
|
await(pleaseTake); |
860 |
< |
assertEquals(q.remainingCapacity(), 0); |
860 |
> |
assertEquals(0, q.remainingCapacity()); |
861 |
|
assertEquals(capacity - 1, q.take()); |
862 |
|
|
863 |
|
await(pleaseInterrupt); |
864 |
|
assertThreadStaysAlive(t); |
865 |
|
t.interrupt(); |
866 |
|
awaitTermination(t); |
867 |
< |
assertEquals(q.remainingCapacity(), 0); |
867 |
> |
assertEquals(0, q.remainingCapacity()); |
868 |
|
} |
869 |
|
|
870 |
|
/** |
1067 |
|
|
1068 |
|
pleaseInterrupt.countDown(); |
1069 |
|
try { |
1070 |
< |
q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); |
1070 |
> |
q.pollFirst(LONG_DELAY_MS, MILLISECONDS); |
1071 |
|
shouldThrow(); |
1072 |
|
} catch (InterruptedException success) {} |
1073 |
|
assertFalse(Thread.interrupted()); |
1137 |
|
public void testPutLast() throws InterruptedException { |
1138 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
1139 |
|
for (int i = 0; i < SIZE; ++i) { |
1140 |
< |
Integer I = new Integer(i); |
1141 |
< |
q.putLast(I); |
1142 |
< |
assertTrue(q.contains(I)); |
1140 |
> |
Integer x = new Integer(i); |
1141 |
> |
q.putLast(x); |
1142 |
> |
assertTrue(q.contains(x)); |
1143 |
|
} |
1144 |
|
assertEquals(0, q.remainingCapacity()); |
1145 |
|
} |
1204 |
|
}}); |
1205 |
|
|
1206 |
|
await(pleaseTake); |
1207 |
< |
assertEquals(q.remainingCapacity(), 0); |
1207 |
> |
assertEquals(0, q.remainingCapacity()); |
1208 |
|
assertEquals(0, q.take()); |
1209 |
|
|
1210 |
|
await(pleaseInterrupt); |
1211 |
|
assertThreadStaysAlive(t); |
1212 |
|
t.interrupt(); |
1213 |
|
awaitTermination(t); |
1214 |
< |
assertEquals(q.remainingCapacity(), 0); |
1214 |
> |
assertEquals(0, q.remainingCapacity()); |
1215 |
|
} |
1216 |
|
|
1217 |
|
/** |
1403 |
|
} |
1404 |
|
|
1405 |
|
/** |
1403 |
– |
* remove(x) removes x and returns true if present |
1404 |
– |
*/ |
1405 |
– |
public void testRemoveElement() { |
1406 |
– |
LinkedBlockingDeque q = populatedDeque(SIZE); |
1407 |
– |
for (int i = 1; i < SIZE; i+=2) { |
1408 |
– |
assertTrue(q.contains(i)); |
1409 |
– |
assertTrue(q.remove(i)); |
1410 |
– |
assertFalse(q.contains(i)); |
1411 |
– |
assertTrue(q.contains(i-1)); |
1412 |
– |
} |
1413 |
– |
for (int i = 0; i < SIZE; i+=2) { |
1414 |
– |
assertTrue(q.contains(i)); |
1415 |
– |
assertTrue(q.remove(i)); |
1416 |
– |
assertFalse(q.contains(i)); |
1417 |
– |
assertFalse(q.remove(i+1)); |
1418 |
– |
assertFalse(q.contains(i+1)); |
1419 |
– |
} |
1420 |
– |
assertTrue(q.isEmpty()); |
1421 |
– |
} |
1422 |
– |
|
1423 |
– |
/** |
1406 |
|
* contains(x) reports true when elements added but not yet removed |
1407 |
|
*/ |
1408 |
|
public void testContains() { |
1473 |
|
assertTrue(q.removeAll(p)); |
1474 |
|
assertEquals(SIZE-i, q.size()); |
1475 |
|
for (int j = 0; j < i; ++j) { |
1476 |
< |
Integer I = (Integer)(p.remove()); |
1477 |
< |
assertFalse(q.contains(I)); |
1476 |
> |
Integer x = (Integer)(p.remove()); |
1477 |
> |
assertFalse(q.contains(x)); |
1478 |
|
} |
1479 |
|
} |
1480 |
|
} |
1482 |
|
/** |
1483 |
|
* toArray contains all elements in FIFO order |
1484 |
|
*/ |
1485 |
< |
public void testToArray() throws InterruptedException{ |
1485 |
> |
public void testToArray() throws InterruptedException { |
1486 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1487 |
|
Object[] o = q.toArray(); |
1488 |
|
for (int i = 0; i < o.length; i++) |
1518 |
|
public void testIterator() throws InterruptedException { |
1519 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1520 |
|
Iterator it = q.iterator(); |
1521 |
< |
while (it.hasNext()) { |
1521 |
> |
int i; |
1522 |
> |
for (i = 0; it.hasNext(); i++) |
1523 |
> |
assertTrue(q.contains(it.next())); |
1524 |
> |
assertEquals(i, SIZE); |
1525 |
> |
assertIteratorExhausted(it); |
1526 |
> |
|
1527 |
> |
it = q.iterator(); |
1528 |
> |
for (i = 0; it.hasNext(); i++) |
1529 |
|
assertEquals(it.next(), q.take()); |
1530 |
< |
} |
1530 |
> |
assertEquals(i, SIZE); |
1531 |
> |
assertIteratorExhausted(it); |
1532 |
> |
} |
1533 |
> |
|
1534 |
> |
/** |
1535 |
> |
* iterator of empty collection has no elements |
1536 |
> |
*/ |
1537 |
> |
public void testEmptyIterator() { |
1538 |
> |
Deque c = new LinkedBlockingDeque(); |
1539 |
> |
assertIteratorExhausted(c.iterator()); |
1540 |
> |
assertIteratorExhausted(c.descendingIterator()); |
1541 |
|
} |
1542 |
|
|
1543 |
|
/** |
1720 |
|
Queue x = populatedDeque(SIZE); |
1721 |
|
Queue y = serialClone(x); |
1722 |
|
|
1723 |
< |
assertTrue(x != y); |
1723 |
> |
assertNotSame(y, x); |
1724 |
|
assertEquals(x.size(), y.size()); |
1725 |
|
assertEquals(x.toString(), y.toString()); |
1726 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
1738 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1739 |
|
ArrayList l = new ArrayList(); |
1740 |
|
q.drainTo(l); |
1741 |
< |
assertEquals(q.size(), 0); |
1742 |
< |
assertEquals(l.size(), SIZE); |
1741 |
> |
assertEquals(0, q.size()); |
1742 |
> |
assertEquals(SIZE, l.size()); |
1743 |
|
for (int i = 0; i < SIZE; ++i) |
1744 |
|
assertEquals(l.get(i), new Integer(i)); |
1745 |
|
q.add(zero); |
1749 |
|
assertTrue(q.contains(one)); |
1750 |
|
l.clear(); |
1751 |
|
q.drainTo(l); |
1752 |
< |
assertEquals(q.size(), 0); |
1753 |
< |
assertEquals(l.size(), 2); |
1752 |
> |
assertEquals(0, q.size()); |
1753 |
> |
assertEquals(2, l.size()); |
1754 |
|
for (int i = 0; i < 2; ++i) |
1755 |
|
assertEquals(l.get(i), new Integer(i)); |
1756 |
|
} |
1786 |
|
ArrayList l = new ArrayList(); |
1787 |
|
q.drainTo(l, i); |
1788 |
|
int k = (i < SIZE) ? i : SIZE; |
1789 |
< |
assertEquals(l.size(), k); |
1790 |
< |
assertEquals(q.size(), SIZE-k); |
1789 |
> |
assertEquals(k, l.size()); |
1790 |
> |
assertEquals(SIZE-k, q.size()); |
1791 |
|
for (int j = 0; j < k; ++j) |
1792 |
|
assertEquals(l.get(j), new Integer(j)); |
1793 |
< |
while (q.poll() != null) ; |
1793 |
> |
do {} while (q.poll() != null); |
1794 |
> |
} |
1795 |
> |
} |
1796 |
> |
|
1797 |
> |
/** |
1798 |
> |
* remove(null), contains(null) always return false |
1799 |
> |
*/ |
1800 |
> |
public void testNeverContainsNull() { |
1801 |
> |
Deque<?>[] qs = { |
1802 |
> |
new LinkedBlockingDeque<Object>(), |
1803 |
> |
populatedDeque(2), |
1804 |
> |
}; |
1805 |
> |
|
1806 |
> |
for (Deque<?> q : qs) { |
1807 |
> |
assertFalse(q.contains(null)); |
1808 |
> |
assertFalse(q.remove(null)); |
1809 |
> |
assertFalse(q.removeFirstOccurrence(null)); |
1810 |
> |
assertFalse(q.removeLastOccurrence(null)); |
1811 |
|
} |
1812 |
|
} |
1813 |
|
|