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() { |
82 |
|
public void testSize() { |
83 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
84 |
|
for (int i = 0; i < SIZE; ++i) { |
85 |
< |
assertEquals(SIZE-i, q.size()); |
85 |
> |
assertEquals(SIZE - i, q.size()); |
86 |
|
q.removeFirst(); |
87 |
|
} |
88 |
|
for (int i = 0; i < SIZE; ++i) { |
147 |
|
*/ |
148 |
|
public void testPollLast() { |
149 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
150 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
150 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
151 |
|
assertEquals(i, q.pollLast()); |
152 |
|
} |
153 |
|
assertNull(q.pollLast()); |
186 |
|
*/ |
187 |
|
public void testPeekLast() { |
188 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
189 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
189 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
190 |
|
assertEquals(i, q.peekLast()); |
191 |
|
assertEquals(i, q.pollLast()); |
192 |
|
assertTrue(q.peekLast() == null || |
216 |
|
*/ |
217 |
|
public void testLastElement() { |
218 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
219 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
219 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
220 |
|
assertEquals(i, q.getLast()); |
221 |
|
assertEquals(i, q.pollLast()); |
222 |
|
} |
367 |
|
*/ |
368 |
|
public void testConstructor5() { |
369 |
|
Integer[] ints = new Integer[SIZE]; |
370 |
< |
for (int i = 0; i < SIZE-1; ++i) |
370 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
371 |
|
ints[i] = i; |
372 |
|
Collection<Integer> elements = Arrays.asList(ints); |
373 |
|
try { |
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)); |
417 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
418 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
419 |
> |
assertTrue(q.add(i)); |
420 |
|
} |
421 |
|
} |
422 |
|
|
424 |
|
* push(null) throws NPE |
425 |
|
*/ |
426 |
|
public void testPushNull() { |
427 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
428 |
|
try { |
428 |
– |
LinkedBlockingDeque q = new LinkedBlockingDeque(1); |
429 |
|
q.push(null); |
430 |
|
shouldThrow(); |
431 |
|
} catch (NullPointerException success) {} |
435 |
|
* push succeeds if not full; throws ISE if full |
436 |
|
*/ |
437 |
|
public void testPush() { |
438 |
+ |
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
439 |
+ |
for (int i = 0; i < SIZE; ++i) { |
440 |
+ |
Integer x = new Integer(i); |
441 |
+ |
q.push(x); |
442 |
+ |
assertEquals(x, q.peek()); |
443 |
+ |
} |
444 |
+ |
assertEquals(0, q.remainingCapacity()); |
445 |
|
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()); |
444 |
– |
} |
445 |
– |
assertEquals(0, q.remainingCapacity()); |
446 |
|
q.push(new Integer(SIZE)); |
447 |
|
shouldThrow(); |
448 |
|
} catch (IllegalStateException success) {} |
513 |
|
public void testAddAll3() { |
514 |
|
LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); |
515 |
|
Integer[] ints = new Integer[SIZE]; |
516 |
< |
for (int i = 0; i < SIZE-1; ++i) |
516 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
517 |
|
ints[i] = new Integer(i); |
518 |
|
Collection<Integer> elements = Arrays.asList(ints); |
519 |
|
try { |
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 |
|
} |
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 |
|
} |
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 |
|
} |
1246 |
|
public void testTakeLast() throws InterruptedException { |
1247 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1248 |
|
for (int i = 0; i < SIZE; ++i) { |
1249 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1249 |
> |
assertEquals(SIZE - i-1, q.takeLast()); |
1250 |
|
} |
1251 |
|
} |
1252 |
|
|
1259 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1260 |
|
public void realRun() throws InterruptedException { |
1261 |
|
for (int i = 0; i < SIZE; ++i) { |
1262 |
< |
assertEquals(SIZE-i-1, q.takeLast()); |
1262 |
> |
assertEquals(SIZE - i-1, q.takeLast()); |
1263 |
|
} |
1264 |
|
|
1265 |
|
Thread.currentThread().interrupt(); |
1289 |
|
public void testTimedPollLast0() throws InterruptedException { |
1290 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1291 |
|
for (int i = 0; i < SIZE; ++i) { |
1292 |
< |
assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); |
1292 |
> |
assertEquals(SIZE - i-1, q.pollLast(0, MILLISECONDS)); |
1293 |
|
} |
1294 |
|
assertNull(q.pollLast(0, MILLISECONDS)); |
1295 |
|
} |
1301 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1302 |
|
for (int i = 0; i < SIZE; ++i) { |
1303 |
|
long startTime = System.nanoTime(); |
1304 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1304 |
> |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1305 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1306 |
|
} |
1307 |
|
long startTime = System.nanoTime(); |
1320 |
|
public void realRun() throws InterruptedException { |
1321 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1322 |
|
for (int i = 0; i < SIZE; ++i) { |
1323 |
< |
assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1323 |
> |
assertEquals(SIZE - i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
1324 |
|
} |
1325 |
|
|
1326 |
|
Thread.currentThread().interrupt(); |
1458 |
|
assertTrue(changed); |
1459 |
|
|
1460 |
|
assertTrue(q.containsAll(p)); |
1461 |
< |
assertEquals(SIZE-i, q.size()); |
1461 |
> |
assertEquals(SIZE - i, q.size()); |
1462 |
|
p.remove(); |
1463 |
|
} |
1464 |
|
} |
1471 |
|
LinkedBlockingDeque q = populatedDeque(SIZE); |
1472 |
|
LinkedBlockingDeque p = populatedDeque(i); |
1473 |
|
assertTrue(q.removeAll(p)); |
1474 |
< |
assertEquals(SIZE-i, q.size()); |
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 |
|
} |
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 |
|
/** |
1762 |
|
final LinkedBlockingDeque q = populatedDeque(SIZE); |
1763 |
|
Thread t = new Thread(new CheckedRunnable() { |
1764 |
|
public void realRun() throws InterruptedException { |
1765 |
< |
q.put(new Integer(SIZE+1)); |
1765 |
> |
q.put(new Integer(SIZE + 1)); |
1766 |
|
}}); |
1767 |
|
|
1768 |
|
t.start(); |
1787 |
|
q.drainTo(l, i); |
1788 |
|
int k = (i < SIZE) ? i : SIZE; |
1789 |
|
assertEquals(k, l.size()); |
1790 |
< |
assertEquals(SIZE-k, q.size()); |
1790 |
> |
assertEquals(SIZE - k, q.size()); |
1791 |
|
for (int j = 0; j < k; ++j) |
1792 |
|
assertEquals(l.get(j), new Integer(j)); |
1793 |
|
do {} while (q.poll() != null); |