6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.Arrays; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
|
11 |
|
import java.util.ArrayList; |
12 |
+ |
import java.util.Arrays; |
13 |
|
import java.util.Collection; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
16 |
|
import java.util.Queue; |
17 |
|
import java.util.concurrent.BlockingQueue; |
18 |
|
import java.util.concurrent.CountDownLatch; |
18 |
– |
import java.util.concurrent.LinkedBlockingQueue; |
19 |
|
import java.util.concurrent.Executors; |
20 |
|
import java.util.concurrent.ExecutorService; |
21 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
22 |
> |
|
23 |
> |
import junit.framework.Test; |
24 |
|
|
25 |
|
public class LinkedBlockingQueueTest extends JSR166TestCase { |
26 |
|
|
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() { |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
|
Integer[] ints = new Integer[SIZE]; |
111 |
< |
for (int i = 0; i < SIZE-1; ++i) |
111 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
112 |
|
ints[i] = new Integer(i); |
113 |
|
Collection<Integer> elements = Arrays.asList(ints); |
114 |
|
try { |
148 |
|
* remainingCapacity decreases on add, increases on remove |
149 |
|
*/ |
150 |
|
public void testRemainingCapacity() { |
151 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
151 |
> |
BlockingQueue q = populatedQueue(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
|
assertEquals(i, q.remainingCapacity()); |
154 |
< |
assertEquals(SIZE-i, q.size()); |
155 |
< |
q.remove(); |
154 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
155 |
> |
assertEquals(i, q.remove()); |
156 |
|
} |
157 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
< |
assertEquals(SIZE-i, q.remainingCapacity()); |
159 |
< |
assertEquals(i, q.size()); |
160 |
< |
q.add(new Integer(i)); |
158 |
> |
assertEquals(SIZE - i, q.remainingCapacity()); |
159 |
> |
assertEquals(SIZE, q.size() + q.remainingCapacity()); |
160 |
> |
assertTrue(q.add(i)); |
161 |
|
} |
162 |
|
} |
163 |
|
|
202 |
|
public void testAddAll3() { |
203 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
204 |
|
Integer[] ints = new Integer[SIZE]; |
205 |
< |
for (int i = 0; i < SIZE-1; ++i) |
205 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
206 |
|
ints[i] = new Integer(i); |
207 |
|
Collection<Integer> elements = Arrays.asList(ints); |
208 |
|
try { |
247 |
|
public void testPut() throws InterruptedException { |
248 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); |
249 |
|
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
Integer I = new Integer(i); |
251 |
< |
q.put(I); |
252 |
< |
assertTrue(q.contains(I)); |
250 |
> |
Integer x = new Integer(i); |
251 |
> |
q.put(x); |
252 |
> |
assertTrue(q.contains(x)); |
253 |
|
} |
254 |
|
assertEquals(0, q.remainingCapacity()); |
255 |
|
} |
515 |
|
assertTrue(q.remove(new Integer(1))); |
516 |
|
assertTrue(q.remove(new Integer(2))); |
517 |
|
assertTrue(q.add(new Integer(3))); |
518 |
< |
assertTrue(q.take() != null); |
518 |
> |
assertNotNull(q.take()); |
519 |
|
} |
520 |
|
|
521 |
|
/** |
574 |
|
assertTrue(changed); |
575 |
|
|
576 |
|
assertTrue(q.containsAll(p)); |
577 |
< |
assertEquals(SIZE-i, q.size()); |
577 |
> |
assertEquals(SIZE - i, q.size()); |
578 |
|
p.remove(); |
579 |
|
} |
580 |
|
} |
587 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
588 |
|
LinkedBlockingQueue p = populatedQueue(i); |
589 |
|
assertTrue(q.removeAll(p)); |
590 |
< |
assertEquals(SIZE-i, q.size()); |
590 |
> |
assertEquals(SIZE - i, q.size()); |
591 |
|
for (int j = 0; j < i; ++j) { |
592 |
< |
Integer I = (Integer)(p.remove()); |
593 |
< |
assertFalse(q.contains(I)); |
592 |
> |
Integer x = (Integer)(p.remove()); |
593 |
> |
assertFalse(q.contains(x)); |
594 |
|
} |
595 |
|
} |
596 |
|
} |
634 |
|
public void testIterator() throws InterruptedException { |
635 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
636 |
|
Iterator it = q.iterator(); |
637 |
< |
while (it.hasNext()) { |
637 |
> |
int i; |
638 |
> |
for (i = 0; it.hasNext(); i++) |
639 |
> |
assertTrue(q.contains(it.next())); |
640 |
> |
assertEquals(i, SIZE); |
641 |
> |
assertIteratorExhausted(it); |
642 |
> |
|
643 |
> |
it = q.iterator(); |
644 |
> |
for (i = 0; it.hasNext(); i++) |
645 |
|
assertEquals(it.next(), q.take()); |
646 |
< |
} |
646 |
> |
assertEquals(i, SIZE); |
647 |
> |
assertIteratorExhausted(it); |
648 |
> |
} |
649 |
> |
|
650 |
> |
/** |
651 |
> |
* iterator of empty collection has no elements |
652 |
> |
*/ |
653 |
> |
public void testEmptyIterator() { |
654 |
> |
assertIteratorExhausted(new LinkedBlockingQueue().iterator()); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
772 |
|
Queue x = populatedQueue(SIZE); |
773 |
|
Queue y = serialClone(x); |
774 |
|
|
775 |
< |
assertTrue(x != y); |
775 |
> |
assertNotSame(x, y); |
776 |
|
assertEquals(x.size(), y.size()); |
777 |
|
assertEquals(x.toString(), y.toString()); |
778 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
814 |
|
final LinkedBlockingQueue q = populatedQueue(SIZE); |
815 |
|
Thread t = new Thread(new CheckedRunnable() { |
816 |
|
public void realRun() throws InterruptedException { |
817 |
< |
q.put(new Integer(SIZE+1)); |
817 |
> |
q.put(new Integer(SIZE + 1)); |
818 |
|
}}); |
819 |
|
|
820 |
|
t.start(); |
839 |
|
q.drainTo(l, i); |
840 |
|
int k = (i < SIZE) ? i : SIZE; |
841 |
|
assertEquals(k, l.size()); |
842 |
< |
assertEquals(SIZE-k, q.size()); |
842 |
> |
assertEquals(SIZE - k, q.size()); |
843 |
|
for (int j = 0; j < k; ++j) |
844 |
|
assertEquals(l.get(j), new Integer(j)); |
845 |
< |
while (q.poll() != null) ; |
845 |
> |
do {} while (q.poll() != null); |
846 |
> |
} |
847 |
> |
} |
848 |
> |
|
849 |
> |
/** |
850 |
> |
* remove(null), contains(null) always return false |
851 |
> |
*/ |
852 |
> |
public void testNeverContainsNull() { |
853 |
> |
Collection<?>[] qs = { |
854 |
> |
new LinkedBlockingQueue<Object>(), |
855 |
> |
populatedQueue(2), |
856 |
> |
}; |
857 |
> |
|
858 |
> |
for (Collection<?> q : qs) { |
859 |
> |
assertFalse(q.contains(null)); |
860 |
> |
assertFalse(q.remove(null)); |
861 |
|
} |
862 |
|
} |
863 |
|
|