15 |
|
import java.util.Queue; |
16 |
|
|
17 |
|
import junit.framework.Test; |
18 |
– |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class PriorityQueueTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
|
main(suite(), args); |
22 |
|
} |
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(PriorityQueueTest.class); |
24 |
> |
class Implementation implements CollectionImplementation { |
25 |
> |
public Class<?> klazz() { return PriorityQueue.class; } |
26 |
> |
public Collection emptyCollection() { return new PriorityQueue(); } |
27 |
> |
public Object makeElement(int i) { return i; } |
28 |
> |
public boolean isConcurrent() { return false; } |
29 |
> |
public boolean permitsNulls() { return false; } |
30 |
> |
} |
31 |
> |
class ComparatorImplementation implements CollectionImplementation { |
32 |
> |
public Class<?> klazz() { return PriorityQueue.class; } |
33 |
> |
public Collection emptyCollection() { return new PriorityQueue(new MyReverseComparator()); } |
34 |
> |
public Object makeElement(int i) { return i; } |
35 |
> |
public boolean isConcurrent() { return false; } |
36 |
> |
public boolean permitsNulls() { return false; } |
37 |
> |
} |
38 |
> |
return newTestSuite(PriorityQueueTest.class, |
39 |
> |
CollectionTest.testSuite(new Implementation()), |
40 |
> |
CollectionTest.testSuite(new ComparatorImplementation())); |
41 |
|
} |
42 |
|
|
43 |
< |
static class MyReverseComparator implements Comparator { |
43 |
> |
static class MyReverseComparator implements Comparator, java.io.Serializable { |
44 |
|
public int compare(Object x, Object y) { |
45 |
|
return ((Comparable)y).compareTo(x); |
46 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
|
* Returns a new queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
< |
private PriorityQueue<Integer> populatedQueue(int n) { |
54 |
< |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
53 |
> |
private static PriorityQueue<Integer> populatedQueue(int n) { |
54 |
> |
PriorityQueue<Integer> q = new PriorityQueue<>(n); |
55 |
|
assertTrue(q.isEmpty()); |
56 |
< |
for (int i = n-1; i >= 0; i -= 2) |
56 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
57 |
|
assertTrue(q.offer(new Integer(i))); |
58 |
|
for (int i = (n & 1); i < n; i += 2) |
59 |
|
assertTrue(q.offer(new Integer(i))); |
60 |
|
assertFalse(q.isEmpty()); |
61 |
|
assertEquals(n, q.size()); |
62 |
+ |
assertEquals((Integer) 0, q.peek()); |
63 |
|
return q; |
64 |
|
} |
65 |
|
|
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* Constructor throws IAE if capacity argument nonpositive |
74 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
75 |
|
*/ |
76 |
|
public void testConstructor2() { |
77 |
|
try { |
207 |
|
PriorityQueue q = new PriorityQueue(1); |
208 |
|
try { |
209 |
|
q.offer(new Object()); |
194 |
– |
q.offer(new Object()); |
210 |
|
shouldThrow(); |
211 |
< |
} catch (ClassCastException success) {} |
211 |
> |
} catch (ClassCastException success) { |
212 |
> |
assertTrue(q.isEmpty()); |
213 |
> |
assertEquals(0, q.size()); |
214 |
> |
assertNull(q.poll()); |
215 |
> |
} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
341 |
|
assertTrue(q.contains(i)); |
342 |
|
assertTrue(q.remove(i)); |
343 |
|
assertFalse(q.contains(i)); |
344 |
< |
assertTrue(q.contains(i-1)); |
344 |
> |
assertTrue(q.contains(i - 1)); |
345 |
|
} |
346 |
|
for (int i = 0; i < SIZE; i += 2) { |
347 |
|
assertTrue(q.contains(i)); |
348 |
|
assertTrue(q.remove(i)); |
349 |
|
assertFalse(q.contains(i)); |
350 |
< |
assertFalse(q.remove(i+1)); |
351 |
< |
assertFalse(q.contains(i+1)); |
350 |
> |
assertFalse(q.remove(i + 1)); |
351 |
> |
assertFalse(q.contains(i + 1)); |
352 |
|
} |
353 |
|
assertTrue(q.isEmpty()); |
354 |
|
} |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
< |
* A deserialized serialized queue has same elements |
506 |
> |
* A deserialized/reserialized queue has same elements |
507 |
|
*/ |
508 |
|
public void testSerialization() throws Exception { |
509 |
|
Queue x = populatedQueue(SIZE); |