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.Comparator; |
15 |
|
import java.util.Iterator; |
16 |
|
import java.util.NoSuchElementException; |
17 |
< |
import java.util.concurrent.PriorityBlockingQueue; |
17 |
> |
import java.util.Queue; |
18 |
|
import java.util.concurrent.BlockingQueue; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
23 |
< |
import java.io.*; |
22 |
> |
import java.util.concurrent.PriorityBlockingQueue; |
23 |
> |
|
24 |
> |
import junit.framework.Test; |
25 |
|
|
26 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
27 |
|
|
33 |
|
|
34 |
|
public static class InitialCapacity extends BlockingQueueTest { |
35 |
|
protected BlockingQueue emptyCollection() { |
36 |
< |
return new PriorityBlockingQueue(20); |
36 |
> |
return new PriorityBlockingQueue(SIZE); |
37 |
|
} |
38 |
|
} |
39 |
|
|
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* Create a queue of given size containing consecutive |
60 |
> |
* Returns a new queue of given size containing consecutive |
61 |
|
* Integers 0 ... n. |
62 |
|
*/ |
63 |
|
private PriorityBlockingQueue<Integer> populatedQueue(int n) { |
64 |
|
PriorityBlockingQueue<Integer> q = |
65 |
|
new PriorityBlockingQueue<Integer>(n); |
66 |
|
assertTrue(q.isEmpty()); |
67 |
< |
for (int i = n-1; i >= 0; i-=2) |
67 |
> |
for (int i = n-1; i >= 0; i -= 2) |
68 |
|
assertTrue(q.offer(new Integer(i))); |
69 |
< |
for (int i = (n & 1); i < n; i+=2) |
69 |
> |
for (int i = (n & 1); i < n; i += 2) |
70 |
|
assertTrue(q.offer(new Integer(i))); |
71 |
|
assertFalse(q.isEmpty()); |
72 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
266 |
|
public void testPut() { |
267 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
268 |
|
for (int i = 0; i < SIZE; ++i) { |
269 |
< |
Integer I = new Integer(i); |
270 |
< |
q.put(I); |
271 |
< |
assertTrue(q.contains(I)); |
269 |
> |
Integer x = new Integer(i); |
270 |
> |
q.put(x); |
271 |
> |
assertTrue(q.contains(x)); |
272 |
|
} |
273 |
|
assertEquals(SIZE, q.size()); |
274 |
|
} |
461 |
|
} |
462 |
|
|
463 |
|
/** |
462 |
– |
* remove(x) removes x and returns true if present |
463 |
– |
*/ |
464 |
– |
public void testRemoveElement() { |
465 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
466 |
– |
for (int i = 1; i < SIZE; i+=2) { |
467 |
– |
assertTrue(q.contains(i)); |
468 |
– |
assertTrue(q.remove(i)); |
469 |
– |
assertFalse(q.contains(i)); |
470 |
– |
assertTrue(q.contains(i-1)); |
471 |
– |
} |
472 |
– |
for (int i = 0; i < SIZE; i+=2) { |
473 |
– |
assertTrue(q.contains(i)); |
474 |
– |
assertTrue(q.remove(i)); |
475 |
– |
assertFalse(q.contains(i)); |
476 |
– |
assertFalse(q.remove(i+1)); |
477 |
– |
assertFalse(q.contains(i+1)); |
478 |
– |
} |
479 |
– |
assertTrue(q.isEmpty()); |
480 |
– |
} |
481 |
– |
|
482 |
– |
/** |
464 |
|
* contains(x) reports true when elements added but not yet removed |
465 |
|
*/ |
466 |
|
public void testContains() { |
530 |
|
assertTrue(q.removeAll(p)); |
531 |
|
assertEquals(SIZE-i, q.size()); |
532 |
|
for (int j = 0; j < i; ++j) { |
533 |
< |
Integer I = (Integer)(p.remove()); |
534 |
< |
assertFalse(q.contains(I)); |
533 |
> |
Integer x = (Integer)(p.remove()); |
534 |
> |
assertFalse(q.contains(x)); |
535 |
|
} |
536 |
|
} |
537 |
|
} |
643 |
|
* A deserialized serialized queue has same elements |
644 |
|
*/ |
645 |
|
public void testSerialization() throws Exception { |
646 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
648 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
649 |
< |
out.writeObject(q); |
650 |
< |
out.close(); |
651 |
< |
|
652 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
653 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
654 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
655 |
< |
assertEquals(q.size(), r.size()); |
675 |
< |
while (!q.isEmpty()) |
676 |
< |
assertEquals(q.remove(), r.remove()); |
646 |
> |
Queue x = populatedQueue(SIZE); |
647 |
> |
Queue y = serialClone(x); |
648 |
> |
|
649 |
> |
assertNotSame(x, y); |
650 |
> |
assertEquals(x.size(), y.size()); |
651 |
> |
while (!x.isEmpty()) { |
652 |
> |
assertFalse(y.isEmpty()); |
653 |
> |
assertEquals(x.remove(), y.remove()); |
654 |
> |
} |
655 |
> |
assertTrue(y.isEmpty()); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
662 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
663 |
|
ArrayList l = new ArrayList(); |
664 |
|
q.drainTo(l); |
665 |
< |
assertEquals(q.size(), 0); |
666 |
< |
assertEquals(l.size(), SIZE); |
665 |
> |
assertEquals(0, q.size()); |
666 |
> |
assertEquals(SIZE, l.size()); |
667 |
|
for (int i = 0; i < SIZE; ++i) |
668 |
|
assertEquals(l.get(i), new Integer(i)); |
669 |
|
q.add(zero); |
673 |
|
assertTrue(q.contains(one)); |
674 |
|
l.clear(); |
675 |
|
q.drainTo(l); |
676 |
< |
assertEquals(q.size(), 0); |
677 |
< |
assertEquals(l.size(), 2); |
676 |
> |
assertEquals(0, q.size()); |
677 |
> |
assertEquals(2, l.size()); |
678 |
|
for (int i = 0; i < 2; ++i) |
679 |
|
assertEquals(l.get(i), new Integer(i)); |
680 |
|
} |
710 |
|
ArrayList l = new ArrayList(); |
711 |
|
q.drainTo(l, i); |
712 |
|
int k = (i < SIZE) ? i : SIZE; |
713 |
< |
assertEquals(l.size(), k); |
714 |
< |
assertEquals(q.size(), SIZE-k); |
713 |
> |
assertEquals(k, l.size()); |
714 |
> |
assertEquals(SIZE-k, q.size()); |
715 |
|
for (int j = 0; j < k; ++j) |
716 |
|
assertEquals(l.get(j), new Integer(j)); |
717 |
< |
while (q.poll() != null) ; |
717 |
> |
do {} while (q.poll() != null); |
718 |
> |
} |
719 |
> |
} |
720 |
> |
|
721 |
> |
/** |
722 |
> |
* remove(null), contains(null) always return false |
723 |
> |
*/ |
724 |
> |
public void testNeverContainsNull() { |
725 |
> |
Collection<?>[] qs = { |
726 |
> |
new PriorityBlockingQueue<Object>(), |
727 |
> |
populatedQueue(2), |
728 |
> |
}; |
729 |
> |
|
730 |
> |
for (Collection<?> q : qs) { |
731 |
> |
assertFalse(q.contains(null)); |
732 |
> |
assertFalse(q.remove(null)); |
733 |
|
} |
734 |
|
} |
735 |
|
|