1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include John Vint |
6 |
|
*/ |
7 |
|
|
40 |
|
new Generic().testSuite()); |
41 |
|
} |
42 |
|
|
43 |
– |
void checkEmpty(BlockingQueue q) { |
44 |
– |
try { |
45 |
– |
assertTrue(q.isEmpty()); |
46 |
– |
assertEquals(0, q.size()); |
47 |
– |
assertNull(q.peek()); |
48 |
– |
assertNull(q.poll()); |
49 |
– |
assertNull(q.poll(0, MILLISECONDS)); |
50 |
– |
assertEquals(q.toString(), "[]"); |
51 |
– |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
52 |
– |
assertFalse(q.iterator().hasNext()); |
53 |
– |
try { |
54 |
– |
q.element(); |
55 |
– |
shouldThrow(); |
56 |
– |
} catch (NoSuchElementException success) {} |
57 |
– |
try { |
58 |
– |
q.iterator().next(); |
59 |
– |
shouldThrow(); |
60 |
– |
} catch (NoSuchElementException success) {} |
61 |
– |
try { |
62 |
– |
q.remove(); |
63 |
– |
shouldThrow(); |
64 |
– |
} catch (NoSuchElementException success) {} |
65 |
– |
} catch (InterruptedException ie) { |
66 |
– |
threadUnexpectedException(ie); |
67 |
– |
} |
68 |
– |
} |
69 |
– |
|
43 |
|
/** |
44 |
|
* Constructor builds new queue with size being zero and empty |
45 |
|
* being true |
421 |
|
*/ |
422 |
|
public void testRemoveElement() throws InterruptedException { |
423 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
424 |
< |
for (int i = 1; i < SIZE; i += 2) { |
424 |
> |
for (int i = 1; i < SIZE; i+=2) { |
425 |
> |
assertTrue(q.contains(i)); |
426 |
|
assertTrue(q.remove(i)); |
427 |
+ |
assertFalse(q.contains(i)); |
428 |
+ |
assertTrue(q.contains(i-1)); |
429 |
|
} |
430 |
< |
for (int i = 0; i < SIZE; i += 2) { |
430 |
> |
for (int i = 0; i < SIZE; i+=2) { |
431 |
> |
assertTrue(q.contains(i)); |
432 |
|
assertTrue(q.remove(i)); |
433 |
< |
assertFalse(q.remove(i + 1)); |
433 |
> |
assertFalse(q.contains(i)); |
434 |
> |
assertFalse(q.remove(i+1)); |
435 |
> |
assertFalse(q.contains(i+1)); |
436 |
|
} |
437 |
|
checkEmpty(q); |
438 |
|
} |
529 |
|
} |
530 |
|
|
531 |
|
/** |
532 |
< |
* toArray() contains all elements |
532 |
> |
* toArray() contains all elements in FIFO order |
533 |
|
*/ |
534 |
< |
public void testToArray() throws InterruptedException { |
534 |
> |
public void testToArray() { |
535 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
536 |
|
Object[] o = q.toArray(); |
537 |
|
for (int i = 0; i < o.length; i++) { |
538 |
< |
assertEquals(o[i], q.take()); |
538 |
> |
assertSame(o[i], q.poll()); |
539 |
|
} |
540 |
|
} |
541 |
|
|
542 |
|
/** |
543 |
< |
* toArray(a) contains all elements |
543 |
> |
* toArray(a) contains all elements in FIFO order |
544 |
|
*/ |
545 |
< |
public void testToArray2() throws InterruptedException { |
545 |
> |
public void testToArray2() { |
546 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
547 |
|
Integer[] ints = new Integer[SIZE]; |
548 |
< |
ints = q.toArray(ints); |
548 |
> |
Integer[] array = q.toArray(ints); |
549 |
> |
assertSame(ints, array); |
550 |
|
for (int i = 0; i < ints.length; i++) { |
551 |
< |
assertEquals(ints[i], q.take()); |
551 |
> |
assertSame(ints[i], q.poll()); |
552 |
|
} |
553 |
|
} |
554 |
|
|
555 |
|
/** |
556 |
|
* toArray(null) throws NullPointerException |
557 |
|
*/ |
558 |
< |
public void testToArray_BadArg() { |
558 |
> |
public void testToArray_NullArg() { |
559 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
560 |
|
try { |
561 |
< |
Object o[] = q.toArray(null); |
561 |
> |
q.toArray(null); |
562 |
|
shouldThrow(); |
563 |
|
} catch (NullPointerException success) {} |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
< |
* toArray(incompatible array type) throws CCE |
567 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
568 |
|
*/ |
569 |
|
public void testToArray1_BadArg() { |
570 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
571 |
|
try { |
572 |
< |
Object o[] = q.toArray(new String[10]); |
572 |
> |
q.toArray(new String[10]); |
573 |
|
shouldThrow(); |
574 |
|
} catch (ArrayStoreException success) {} |
575 |
|
} |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1054 |
< |
* tryTransfer waits the amount given if interrupted, and |
1055 |
< |
* throws interrupted exception |
1054 |
> |
* tryTransfer waits the amount given, and throws |
1055 |
> |
* InterruptedException when interrupted. |
1056 |
|
*/ |
1057 |
|
public void testTryTransfer5() throws InterruptedException { |
1058 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1059 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1060 |
+ |
assertTrue(q.isEmpty()); |
1061 |
|
|
1062 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
1063 |
|
public void realRun() throws InterruptedException { |
1068 |
|
shouldThrow(); |
1069 |
|
} catch (InterruptedException success) {} |
1070 |
|
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
1071 |
+ |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
1072 |
|
}}); |
1073 |
|
|
1074 |
|
threadStarted.await(); |
1075 |
+ |
while (q.isEmpty()) |
1076 |
+ |
Thread.yield(); |
1077 |
|
Thread.sleep(SHORT_DELAY_MS); |
1078 |
|
t.interrupt(); |
1079 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1123 |
|
} |
1124 |
|
|
1125 |
|
/** |
1126 |
< |
* tryTransfer attempts to enqueue into the q and fails returning |
1127 |
< |
* false not enqueueing and the successive poll is null |
1126 |
> |
* tryTransfer attempts to enqueue into the queue and fails |
1127 |
> |
* returning false not enqueueing and the successive poll is null |
1128 |
|
*/ |
1129 |
|
public void testTryTransfer8() throws InterruptedException { |
1130 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |