6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.Collection; |
11 |
|
import java.util.Comparator; |
14 |
|
import java.util.PriorityQueue; |
15 |
|
import java.util.Queue; |
16 |
|
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
import junit.framework.TestSuite; |
19 |
+ |
|
20 |
|
public class PriorityQueueTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
main(suite(), args); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(PriorityQueueTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return PriorityQueue.class; } |
27 |
> |
public Collection emptyCollection() { return new PriorityQueue(); } |
28 |
> |
public Object makeElement(int i) { return i; } |
29 |
> |
public boolean isConcurrent() { return false; } |
30 |
> |
public boolean permitsNulls() { return false; } |
31 |
> |
} |
32 |
> |
return newTestSuite(PriorityQueueTest.class, |
33 |
> |
CollectionTest.testSuite(new Implementation())); |
34 |
|
} |
35 |
|
|
36 |
|
static class MyReverseComparator implements Comparator { |
41 |
|
|
42 |
|
/** |
43 |
|
* Returns a new queue of given size containing consecutive |
44 |
< |
* Integers 0 ... n. |
44 |
> |
* Integers 0 ... n - 1. |
45 |
|
*/ |
46 |
|
private PriorityQueue<Integer> populatedQueue(int n) { |
47 |
|
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
48 |
|
assertTrue(q.isEmpty()); |
49 |
< |
for (int i = n-1; i >= 0; i-=2) |
49 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
50 |
|
assertTrue(q.offer(new Integer(i))); |
51 |
< |
for (int i = (n & 1); i < n; i+=2) |
51 |
> |
for (int i = (n & 1); i < n; i += 2) |
52 |
|
assertTrue(q.offer(new Integer(i))); |
53 |
|
assertFalse(q.isEmpty()); |
54 |
|
assertEquals(n, q.size()); |
55 |
+ |
assertEquals((Integer) 0, q.peek()); |
56 |
|
return q; |
57 |
|
} |
58 |
|
|
68 |
|
*/ |
69 |
|
public void testConstructor2() { |
70 |
|
try { |
71 |
< |
PriorityQueue q = new PriorityQueue(0); |
71 |
> |
new PriorityQueue(0); |
72 |
|
shouldThrow(); |
73 |
|
} catch (IllegalArgumentException success) {} |
74 |
|
} |
78 |
|
*/ |
79 |
|
public void testConstructor3() { |
80 |
|
try { |
81 |
< |
PriorityQueue q = new PriorityQueue((Collection)null); |
81 |
> |
new PriorityQueue((Collection)null); |
82 |
|
shouldThrow(); |
83 |
|
} catch (NullPointerException success) {} |
84 |
|
} |
88 |
|
*/ |
89 |
|
public void testConstructor4() { |
90 |
|
try { |
91 |
< |
Integer[] ints = new Integer[SIZE]; |
81 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
91 |
> |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
92 |
|
shouldThrow(); |
93 |
|
} catch (NullPointerException success) {} |
94 |
|
} |
97 |
|
* Initializing from Collection with some null elements throws NPE |
98 |
|
*/ |
99 |
|
public void testConstructor5() { |
100 |
+ |
Integer[] ints = new Integer[SIZE]; |
101 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
102 |
+ |
ints[i] = new Integer(i); |
103 |
|
try { |
104 |
< |
Integer[] ints = new Integer[SIZE]; |
92 |
< |
for (int i = 0; i < SIZE-1; ++i) |
93 |
< |
ints[i] = new Integer(i); |
94 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
104 |
> |
new PriorityQueue(Arrays.asList(ints)); |
105 |
|
shouldThrow(); |
106 |
|
} catch (NullPointerException success) {} |
107 |
|
} |
129 |
|
for (int i = 0; i < SIZE; ++i) |
130 |
|
ints[i] = new Integer(i); |
131 |
|
q.addAll(Arrays.asList(ints)); |
132 |
< |
for (int i = SIZE-1; i >= 0; --i) |
132 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
133 |
|
assertEquals(ints[i], q.poll()); |
134 |
|
} |
135 |
|
|
153 |
|
public void testSize() { |
154 |
|
PriorityQueue q = populatedQueue(SIZE); |
155 |
|
for (int i = 0; i < SIZE; ++i) { |
156 |
< |
assertEquals(SIZE-i, q.size()); |
156 |
> |
assertEquals(SIZE - i, q.size()); |
157 |
|
q.remove(); |
158 |
|
} |
159 |
|
for (int i = 0; i < SIZE; ++i) { |
166 |
|
* offer(null) throws NPE |
167 |
|
*/ |
168 |
|
public void testOfferNull() { |
169 |
+ |
PriorityQueue q = new PriorityQueue(1); |
170 |
|
try { |
160 |
– |
PriorityQueue q = new PriorityQueue(1); |
171 |
|
q.offer(null); |
172 |
|
shouldThrow(); |
173 |
|
} catch (NullPointerException success) {} |
177 |
|
* add(null) throws NPE |
178 |
|
*/ |
179 |
|
public void testAddNull() { |
180 |
+ |
PriorityQueue q = new PriorityQueue(1); |
181 |
|
try { |
171 |
– |
PriorityQueue q = new PriorityQueue(1); |
182 |
|
q.add(null); |
183 |
|
shouldThrow(); |
184 |
|
} catch (NullPointerException success) {} |
197 |
|
* Offer of non-Comparable throws CCE |
198 |
|
*/ |
199 |
|
public void testOfferNonComparable() { |
200 |
+ |
PriorityQueue q = new PriorityQueue(1); |
201 |
|
try { |
191 |
– |
PriorityQueue q = new PriorityQueue(1); |
192 |
– |
q.offer(new Object()); |
193 |
– |
q.offer(new Object()); |
202 |
|
q.offer(new Object()); |
203 |
|
shouldThrow(); |
204 |
< |
} catch (ClassCastException success) {} |
204 |
> |
} catch (ClassCastException success) { |
205 |
> |
assertTrue(q.isEmpty()); |
206 |
> |
assertEquals(0, q.size()); |
207 |
> |
assertNull(q.poll()); |
208 |
> |
} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
223 |
|
* addAll(null) throws NPE |
224 |
|
*/ |
225 |
|
public void testAddAll1() { |
226 |
+ |
PriorityQueue q = new PriorityQueue(1); |
227 |
|
try { |
215 |
– |
PriorityQueue q = new PriorityQueue(1); |
228 |
|
q.addAll(null); |
229 |
|
shouldThrow(); |
230 |
|
} catch (NullPointerException success) {} |
234 |
|
* addAll of a collection with null elements throws NPE |
235 |
|
*/ |
236 |
|
public void testAddAll2() { |
237 |
+ |
PriorityQueue q = new PriorityQueue(SIZE); |
238 |
|
try { |
239 |
< |
PriorityQueue q = new PriorityQueue(SIZE); |
227 |
< |
Integer[] ints = new Integer[SIZE]; |
228 |
< |
q.addAll(Arrays.asList(ints)); |
239 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
240 |
|
shouldThrow(); |
241 |
|
} catch (NullPointerException success) {} |
242 |
|
} |
246 |
|
* possibly adding some elements |
247 |
|
*/ |
248 |
|
public void testAddAll3() { |
249 |
+ |
PriorityQueue q = new PriorityQueue(SIZE); |
250 |
+ |
Integer[] ints = new Integer[SIZE]; |
251 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
252 |
+ |
ints[i] = new Integer(i); |
253 |
|
try { |
239 |
– |
PriorityQueue q = new PriorityQueue(SIZE); |
240 |
– |
Integer[] ints = new Integer[SIZE]; |
241 |
– |
for (int i = 0; i < SIZE-1; ++i) |
242 |
– |
ints[i] = new Integer(i); |
254 |
|
q.addAll(Arrays.asList(ints)); |
255 |
|
shouldThrow(); |
256 |
|
} catch (NullPointerException success) {} |
263 |
|
Integer[] empty = new Integer[0]; |
264 |
|
Integer[] ints = new Integer[SIZE]; |
265 |
|
for (int i = 0; i < SIZE; ++i) |
266 |
< |
ints[i] = new Integer(SIZE-1-i); |
266 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
267 |
|
PriorityQueue q = new PriorityQueue(SIZE); |
268 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
269 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
330 |
|
*/ |
331 |
|
public void testRemoveElement() { |
332 |
|
PriorityQueue q = populatedQueue(SIZE); |
333 |
< |
for (int i = 1; i < SIZE; i+=2) { |
333 |
> |
for (int i = 1; i < SIZE; i += 2) { |
334 |
|
assertTrue(q.contains(i)); |
335 |
|
assertTrue(q.remove(i)); |
336 |
|
assertFalse(q.contains(i)); |
337 |
< |
assertTrue(q.contains(i-1)); |
337 |
> |
assertTrue(q.contains(i - 1)); |
338 |
|
} |
339 |
< |
for (int i = 0; i < SIZE; i+=2) { |
339 |
> |
for (int i = 0; i < SIZE; i += 2) { |
340 |
|
assertTrue(q.contains(i)); |
341 |
|
assertTrue(q.remove(i)); |
342 |
|
assertFalse(q.contains(i)); |
343 |
< |
assertFalse(q.remove(i+1)); |
344 |
< |
assertFalse(q.contains(i+1)); |
343 |
> |
assertFalse(q.remove(i + 1)); |
344 |
> |
assertFalse(q.contains(i + 1)); |
345 |
|
} |
346 |
|
assertTrue(q.isEmpty()); |
347 |
|
} |
400 |
|
assertTrue(changed); |
401 |
|
|
402 |
|
assertTrue(q.containsAll(p)); |
403 |
< |
assertEquals(SIZE-i, q.size()); |
403 |
> |
assertEquals(SIZE - i, q.size()); |
404 |
|
p.remove(); |
405 |
|
} |
406 |
|
} |
413 |
|
PriorityQueue q = populatedQueue(SIZE); |
414 |
|
PriorityQueue p = populatedQueue(i); |
415 |
|
assertTrue(q.removeAll(p)); |
416 |
< |
assertEquals(SIZE-i, q.size()); |
416 |
> |
assertEquals(SIZE - i, q.size()); |
417 |
|
for (int j = 0; j < i; ++j) { |
418 |
< |
Integer I = (Integer)(p.remove()); |
419 |
< |
assertFalse(q.contains(I)); |
418 |
> |
Integer x = (Integer)(p.remove()); |
419 |
> |
assertFalse(q.contains(x)); |
420 |
|
} |
421 |
|
} |
422 |
|
} |
450 |
|
*/ |
451 |
|
public void testIterator() { |
452 |
|
PriorityQueue q = populatedQueue(SIZE); |
442 |
– |
int i = 0; |
453 |
|
Iterator it = q.iterator(); |
454 |
< |
while (it.hasNext()) { |
454 |
> |
int i; |
455 |
> |
for (i = 0; it.hasNext(); i++) |
456 |
|
assertTrue(q.contains(it.next())); |
446 |
– |
++i; |
447 |
– |
} |
457 |
|
assertEquals(i, SIZE); |
458 |
+ |
assertIteratorExhausted(it); |
459 |
+ |
} |
460 |
+ |
|
461 |
+ |
/** |
462 |
+ |
* iterator of empty collection has no elements |
463 |
+ |
*/ |
464 |
+ |
public void testEmptyIterator() { |
465 |
+ |
assertIteratorExhausted(new PriorityQueue().iterator()); |
466 |
|
} |
467 |
|
|
468 |
|
/** |