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.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.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 |
|
} |
576 |
|
*/ |
577 |
|
public void testIterator() { |
578 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
598 |
– |
int i = 0; |
579 |
|
Iterator it = q.iterator(); |
580 |
< |
while (it.hasNext()) { |
580 |
> |
int i; |
581 |
> |
for (i = 0; it.hasNext(); i++) |
582 |
|
assertTrue(q.contains(it.next())); |
602 |
– |
++i; |
603 |
– |
} |
583 |
|
assertEquals(i, SIZE); |
584 |
+ |
assertIteratorExhausted(it); |
585 |
+ |
} |
586 |
+ |
|
587 |
+ |
/** |
588 |
+ |
* iterator of empty collection has no elements |
589 |
+ |
*/ |
590 |
+ |
public void testEmptyIterator() { |
591 |
+ |
assertIteratorExhausted(new PriorityBlockingQueue().iterator()); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
652 |
|
Queue x = populatedQueue(SIZE); |
653 |
|
Queue y = serialClone(x); |
654 |
|
|
655 |
< |
assertTrue(x != y); |
655 |
> |
assertNotSame(x, y); |
656 |
|
assertEquals(x.size(), y.size()); |
657 |
|
while (!x.isEmpty()) { |
658 |
|
assertFalse(y.isEmpty()); |
668 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
669 |
|
ArrayList l = new ArrayList(); |
670 |
|
q.drainTo(l); |
671 |
< |
assertEquals(q.size(), 0); |
672 |
< |
assertEquals(l.size(), SIZE); |
671 |
> |
assertEquals(0, q.size()); |
672 |
> |
assertEquals(SIZE, l.size()); |
673 |
|
for (int i = 0; i < SIZE; ++i) |
674 |
|
assertEquals(l.get(i), new Integer(i)); |
675 |
|
q.add(zero); |
679 |
|
assertTrue(q.contains(one)); |
680 |
|
l.clear(); |
681 |
|
q.drainTo(l); |
682 |
< |
assertEquals(q.size(), 0); |
683 |
< |
assertEquals(l.size(), 2); |
682 |
> |
assertEquals(0, q.size()); |
683 |
> |
assertEquals(2, l.size()); |
684 |
|
for (int i = 0; i < 2; ++i) |
685 |
|
assertEquals(l.get(i), new Integer(i)); |
686 |
|
} |
716 |
|
ArrayList l = new ArrayList(); |
717 |
|
q.drainTo(l, i); |
718 |
|
int k = (i < SIZE) ? i : SIZE; |
719 |
< |
assertEquals(l.size(), k); |
720 |
< |
assertEquals(q.size(), SIZE-k); |
719 |
> |
assertEquals(k, l.size()); |
720 |
> |
assertEquals(SIZE-k, q.size()); |
721 |
|
for (int j = 0; j < k; ++j) |
722 |
|
assertEquals(l.get(j), new Integer(j)); |
723 |
< |
while (q.poll() != null) ; |
723 |
> |
do {} while (q.poll() != null); |
724 |
> |
} |
725 |
> |
} |
726 |
> |
|
727 |
> |
/** |
728 |
> |
* remove(null), contains(null) always return false |
729 |
> |
*/ |
730 |
> |
public void testNeverContainsNull() { |
731 |
> |
Collection<?>[] qs = { |
732 |
> |
new PriorityBlockingQueue<Object>(), |
733 |
> |
populatedQueue(2), |
734 |
> |
}; |
735 |
> |
|
736 |
> |
for (Collection<?> q : qs) { |
737 |
> |
assertFalse(q.contains(null)); |
738 |
> |
assertFalse(q.remove(null)); |
739 |
|
} |
740 |
|
} |
741 |
|
|