13 |
|
import java.util.Comparator; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
16 |
+ |
import java.util.Queue; |
17 |
|
import java.util.concurrent.PriorityBlockingQueue; |
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; |
22 |
– |
import java.io.*; |
23 |
|
|
24 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
25 |
|
|
31 |
|
|
32 |
|
public static class InitialCapacity extends BlockingQueueTest { |
33 |
|
protected BlockingQueue emptyCollection() { |
34 |
< |
return new PriorityBlockingQueue(20); |
34 |
> |
return new PriorityBlockingQueue(SIZE); |
35 |
|
} |
36 |
|
} |
37 |
|
|
459 |
|
} |
460 |
|
|
461 |
|
/** |
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 |
– |
/** |
462 |
|
* contains(x) reports true when elements added but not yet removed |
463 |
|
*/ |
464 |
|
public void testContains() { |
641 |
|
* A deserialized serialized queue has same elements |
642 |
|
*/ |
643 |
|
public void testSerialization() throws Exception { |
644 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
645 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
646 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
647 |
< |
out.writeObject(q); |
648 |
< |
out.close(); |
649 |
< |
|
650 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
651 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
652 |
< |
PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject(); |
653 |
< |
assertEquals(q.size(), r.size()); |
675 |
< |
while (!q.isEmpty()) |
676 |
< |
assertEquals(q.remove(), r.remove()); |
644 |
> |
Queue x = populatedQueue(SIZE); |
645 |
> |
Queue y = serialClone(x); |
646 |
> |
|
647 |
> |
assertTrue(x != y); |
648 |
> |
assertEquals(x.size(), y.size()); |
649 |
> |
while (!x.isEmpty()) { |
650 |
> |
assertFalse(y.isEmpty()); |
651 |
> |
assertEquals(x.remove(), y.remove()); |
652 |
> |
} |
653 |
> |
assertTrue(y.isEmpty()); |
654 |
|
} |
655 |
|
|
656 |
|
/** |
660 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
661 |
|
ArrayList l = new ArrayList(); |
662 |
|
q.drainTo(l); |
663 |
< |
assertEquals(q.size(), 0); |
664 |
< |
assertEquals(l.size(), SIZE); |
663 |
> |
assertEquals(0, q.size()); |
664 |
> |
assertEquals(SIZE, l.size()); |
665 |
|
for (int i = 0; i < SIZE; ++i) |
666 |
|
assertEquals(l.get(i), new Integer(i)); |
667 |
|
q.add(zero); |
671 |
|
assertTrue(q.contains(one)); |
672 |
|
l.clear(); |
673 |
|
q.drainTo(l); |
674 |
< |
assertEquals(q.size(), 0); |
675 |
< |
assertEquals(l.size(), 2); |
674 |
> |
assertEquals(0, q.size()); |
675 |
> |
assertEquals(2, l.size()); |
676 |
|
for (int i = 0; i < 2; ++i) |
677 |
|
assertEquals(l.get(i), new Integer(i)); |
678 |
|
} |
708 |
|
ArrayList l = new ArrayList(); |
709 |
|
q.drainTo(l, i); |
710 |
|
int k = (i < SIZE) ? i : SIZE; |
711 |
< |
assertEquals(l.size(), k); |
712 |
< |
assertEquals(q.size(), SIZE-k); |
711 |
> |
assertEquals(k, l.size()); |
712 |
> |
assertEquals(SIZE-k, q.size()); |
713 |
|
for (int j = 0; j < k; ++j) |
714 |
|
assertEquals(l.get(j), new Integer(j)); |
715 |
|
while (q.poll() != null) ; |