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() { |
34 |
> |
return new PriorityQueue(new MyReverseComparator()); |
35 |
> |
} |
36 |
> |
public Object makeElement(int i) { return i; } |
37 |
> |
public boolean isConcurrent() { return false; } |
38 |
> |
public boolean permitsNulls() { return false; } |
39 |
> |
} |
40 |
> |
return newTestSuite( |
41 |
> |
PriorityQueueTest.class, |
42 |
> |
CollectionTest.testSuite(new Implementation()), |
43 |
> |
CollectionTest.testSuite(new ComparatorImplementation())); |
44 |
|
} |
45 |
|
|
46 |
< |
static class MyReverseComparator implements Comparator { |
46 |
> |
static class MyReverseComparator implements Comparator, java.io.Serializable { |
47 |
|
public int compare(Object x, Object y) { |
48 |
|
return ((Comparable)y).compareTo(x); |
49 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
|
* Returns a new queue of given size containing consecutive |
54 |
< |
* Integers 0 ... n. |
54 |
> |
* Integers 0 ... n - 1. |
55 |
|
*/ |
56 |
< |
private PriorityQueue<Integer> populatedQueue(int n) { |
57 |
< |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
56 |
> |
private static PriorityQueue<Integer> populatedQueue(int n) { |
57 |
> |
PriorityQueue<Integer> q = new PriorityQueue<>(n); |
58 |
|
assertTrue(q.isEmpty()); |
59 |
< |
for (int i = n-1; i >= 0; i -= 2) |
59 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
60 |
|
assertTrue(q.offer(new Integer(i))); |
61 |
|
for (int i = (n & 1); i < n; i += 2) |
62 |
|
assertTrue(q.offer(new Integer(i))); |
63 |
|
assertFalse(q.isEmpty()); |
64 |
|
assertEquals(n, q.size()); |
65 |
+ |
assertEquals((Integer) 0, q.peek()); |
66 |
|
return q; |
67 |
|
} |
68 |
|
|
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* Constructor throws IAE if capacity argument nonpositive |
77 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
78 |
|
*/ |
79 |
|
public void testConstructor2() { |
80 |
|
try { |
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 |
|
try { |
114 |
|
new PriorityQueue(Arrays.asList(ints)); |
139 |
|
for (int i = 0; i < SIZE; ++i) |
140 |
|
ints[i] = new Integer(i); |
141 |
|
q.addAll(Arrays.asList(ints)); |
142 |
< |
for (int i = SIZE-1; i >= 0; --i) |
142 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
143 |
|
assertEquals(ints[i], q.poll()); |
144 |
|
} |
145 |
|
|
163 |
|
public void testSize() { |
164 |
|
PriorityQueue q = populatedQueue(SIZE); |
165 |
|
for (int i = 0; i < SIZE; ++i) { |
166 |
< |
assertEquals(SIZE-i, q.size()); |
166 |
> |
assertEquals(SIZE - i, q.size()); |
167 |
|
q.remove(); |
168 |
|
} |
169 |
|
for (int i = 0; i < SIZE; ++i) { |
210 |
|
PriorityQueue q = new PriorityQueue(1); |
211 |
|
try { |
212 |
|
q.offer(new Object()); |
194 |
– |
q.offer(new Object()); |
213 |
|
shouldThrow(); |
214 |
< |
} catch (ClassCastException success) {} |
214 |
> |
} catch (ClassCastException success) { |
215 |
> |
assertTrue(q.isEmpty()); |
216 |
> |
assertEquals(0, q.size()); |
217 |
> |
assertNull(q.poll()); |
218 |
> |
} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
258 |
|
public void testAddAll3() { |
259 |
|
PriorityQueue q = new PriorityQueue(SIZE); |
260 |
|
Integer[] ints = new Integer[SIZE]; |
261 |
< |
for (int i = 0; i < SIZE-1; ++i) |
261 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
262 |
|
ints[i] = new Integer(i); |
263 |
|
try { |
264 |
|
q.addAll(Arrays.asList(ints)); |
273 |
|
Integer[] empty = new Integer[0]; |
274 |
|
Integer[] ints = new Integer[SIZE]; |
275 |
|
for (int i = 0; i < SIZE; ++i) |
276 |
< |
ints[i] = new Integer(SIZE-1-i); |
276 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
277 |
|
PriorityQueue q = new PriorityQueue(SIZE); |
278 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
279 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
344 |
|
assertTrue(q.contains(i)); |
345 |
|
assertTrue(q.remove(i)); |
346 |
|
assertFalse(q.contains(i)); |
347 |
< |
assertTrue(q.contains(i-1)); |
347 |
> |
assertTrue(q.contains(i - 1)); |
348 |
|
} |
349 |
|
for (int i = 0; i < SIZE; i += 2) { |
350 |
|
assertTrue(q.contains(i)); |
351 |
|
assertTrue(q.remove(i)); |
352 |
|
assertFalse(q.contains(i)); |
353 |
< |
assertFalse(q.remove(i+1)); |
354 |
< |
assertFalse(q.contains(i+1)); |
353 |
> |
assertFalse(q.remove(i + 1)); |
354 |
> |
assertFalse(q.contains(i + 1)); |
355 |
|
} |
356 |
|
assertTrue(q.isEmpty()); |
357 |
|
} |
410 |
|
assertTrue(changed); |
411 |
|
|
412 |
|
assertTrue(q.containsAll(p)); |
413 |
< |
assertEquals(SIZE-i, q.size()); |
413 |
> |
assertEquals(SIZE - i, q.size()); |
414 |
|
p.remove(); |
415 |
|
} |
416 |
|
} |
423 |
|
PriorityQueue q = populatedQueue(SIZE); |
424 |
|
PriorityQueue p = populatedQueue(i); |
425 |
|
assertTrue(q.removeAll(p)); |
426 |
< |
assertEquals(SIZE-i, q.size()); |
426 |
> |
assertEquals(SIZE - i, q.size()); |
427 |
|
for (int j = 0; j < i; ++j) { |
428 |
|
Integer x = (Integer)(p.remove()); |
429 |
|
assertFalse(q.contains(x)); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* A deserialized serialized queue has same elements |
509 |
> |
* A deserialized/reserialized queue has same elements |
510 |
|
*/ |
511 |
|
public void testSerialization() throws Exception { |
512 |
|
Queue x = populatedQueue(SIZE); |