1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import java.util.Arrays; |
10 |
import java.util.Collection; |
11 |
import java.util.Comparator; |
12 |
import java.util.Iterator; |
13 |
import java.util.NoSuchElementException; |
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()); |
23 |
} |
24 |
public static Test suite() { |
25 |
return new TestSuite(PriorityQueueTest.class); |
26 |
} |
27 |
|
28 |
static class MyReverseComparator implements Comparator { |
29 |
public int compare(Object x, Object y) { |
30 |
return ((Comparable)y).compareTo(x); |
31 |
} |
32 |
} |
33 |
|
34 |
/** |
35 |
* Returns a new queue of given size containing consecutive |
36 |
* Integers 0 ... n. |
37 |
*/ |
38 |
private PriorityQueue<Integer> populatedQueue(int n) { |
39 |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
40 |
assertTrue(q.isEmpty()); |
41 |
for (int i = n-1; i >= 0; i-=2) |
42 |
assertTrue(q.offer(new Integer(i))); |
43 |
for (int i = (n & 1); i < n; i+=2) |
44 |
assertTrue(q.offer(new Integer(i))); |
45 |
assertFalse(q.isEmpty()); |
46 |
assertEquals(n, q.size()); |
47 |
return q; |
48 |
} |
49 |
|
50 |
/** |
51 |
* A new queue has unbounded capacity |
52 |
*/ |
53 |
public void testConstructor1() { |
54 |
assertEquals(0, new PriorityQueue(SIZE).size()); |
55 |
} |
56 |
|
57 |
/** |
58 |
* Constructor throws IAE if capacity argument nonpositive |
59 |
*/ |
60 |
public void testConstructor2() { |
61 |
try { |
62 |
PriorityQueue q = new PriorityQueue(0); |
63 |
shouldThrow(); |
64 |
} catch (IllegalArgumentException success) {} |
65 |
} |
66 |
|
67 |
/** |
68 |
* Initializing from null Collection throws NPE |
69 |
*/ |
70 |
public void testConstructor3() { |
71 |
try { |
72 |
PriorityQueue q = new PriorityQueue((Collection)null); |
73 |
shouldThrow(); |
74 |
} catch (NullPointerException success) {} |
75 |
} |
76 |
|
77 |
/** |
78 |
* Initializing from Collection of null elements throws NPE |
79 |
*/ |
80 |
public void testConstructor4() { |
81 |
try { |
82 |
Integer[] ints = new Integer[SIZE]; |
83 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
84 |
shouldThrow(); |
85 |
} catch (NullPointerException success) {} |
86 |
} |
87 |
|
88 |
/** |
89 |
* Initializing from Collection with some null elements throws NPE |
90 |
*/ |
91 |
public void testConstructor5() { |
92 |
try { |
93 |
Integer[] ints = new Integer[SIZE]; |
94 |
for (int i = 0; i < SIZE-1; ++i) |
95 |
ints[i] = new Integer(i); |
96 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
97 |
shouldThrow(); |
98 |
} catch (NullPointerException success) {} |
99 |
} |
100 |
|
101 |
/** |
102 |
* Queue contains all elements of collection used to initialize |
103 |
*/ |
104 |
public void testConstructor6() { |
105 |
Integer[] ints = new Integer[SIZE]; |
106 |
for (int i = 0; i < SIZE; ++i) |
107 |
ints[i] = new Integer(i); |
108 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
109 |
for (int i = 0; i < SIZE; ++i) |
110 |
assertEquals(ints[i], q.poll()); |
111 |
} |
112 |
|
113 |
/** |
114 |
* The comparator used in constructor is used |
115 |
*/ |
116 |
public void testConstructor7() { |
117 |
MyReverseComparator cmp = new MyReverseComparator(); |
118 |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
119 |
assertEquals(cmp, q.comparator()); |
120 |
Integer[] ints = new Integer[SIZE]; |
121 |
for (int i = 0; i < SIZE; ++i) |
122 |
ints[i] = new Integer(i); |
123 |
q.addAll(Arrays.asList(ints)); |
124 |
for (int i = SIZE-1; i >= 0; --i) |
125 |
assertEquals(ints[i], q.poll()); |
126 |
} |
127 |
|
128 |
/** |
129 |
* isEmpty is true before add, false after |
130 |
*/ |
131 |
public void testEmpty() { |
132 |
PriorityQueue q = new PriorityQueue(2); |
133 |
assertTrue(q.isEmpty()); |
134 |
q.add(new Integer(1)); |
135 |
assertFalse(q.isEmpty()); |
136 |
q.add(new Integer(2)); |
137 |
q.remove(); |
138 |
q.remove(); |
139 |
assertTrue(q.isEmpty()); |
140 |
} |
141 |
|
142 |
/** |
143 |
* size changes when elements added and removed |
144 |
*/ |
145 |
public void testSize() { |
146 |
PriorityQueue q = populatedQueue(SIZE); |
147 |
for (int i = 0; i < SIZE; ++i) { |
148 |
assertEquals(SIZE-i, q.size()); |
149 |
q.remove(); |
150 |
} |
151 |
for (int i = 0; i < SIZE; ++i) { |
152 |
assertEquals(i, q.size()); |
153 |
q.add(new Integer(i)); |
154 |
} |
155 |
} |
156 |
|
157 |
/** |
158 |
* offer(null) throws NPE |
159 |
*/ |
160 |
public void testOfferNull() { |
161 |
try { |
162 |
PriorityQueue q = new PriorityQueue(1); |
163 |
q.offer(null); |
164 |
shouldThrow(); |
165 |
} catch (NullPointerException success) {} |
166 |
} |
167 |
|
168 |
/** |
169 |
* add(null) throws NPE |
170 |
*/ |
171 |
public void testAddNull() { |
172 |
try { |
173 |
PriorityQueue q = new PriorityQueue(1); |
174 |
q.add(null); |
175 |
shouldThrow(); |
176 |
} catch (NullPointerException success) {} |
177 |
} |
178 |
|
179 |
/** |
180 |
* Offer of comparable element succeeds |
181 |
*/ |
182 |
public void testOffer() { |
183 |
PriorityQueue q = new PriorityQueue(1); |
184 |
assertTrue(q.offer(zero)); |
185 |
assertTrue(q.offer(one)); |
186 |
} |
187 |
|
188 |
/** |
189 |
* Offer of non-Comparable throws CCE |
190 |
*/ |
191 |
public void testOfferNonComparable() { |
192 |
try { |
193 |
PriorityQueue q = new PriorityQueue(1); |
194 |
q.offer(new Object()); |
195 |
q.offer(new Object()); |
196 |
q.offer(new Object()); |
197 |
shouldThrow(); |
198 |
} catch (ClassCastException success) {} |
199 |
} |
200 |
|
201 |
/** |
202 |
* add of comparable succeeds |
203 |
*/ |
204 |
public void testAdd() { |
205 |
PriorityQueue q = new PriorityQueue(SIZE); |
206 |
for (int i = 0; i < SIZE; ++i) { |
207 |
assertEquals(i, q.size()); |
208 |
assertTrue(q.add(new Integer(i))); |
209 |
} |
210 |
} |
211 |
|
212 |
/** |
213 |
* addAll(null) throws NPE |
214 |
*/ |
215 |
public void testAddAll1() { |
216 |
try { |
217 |
PriorityQueue q = new PriorityQueue(1); |
218 |
q.addAll(null); |
219 |
shouldThrow(); |
220 |
} catch (NullPointerException success) {} |
221 |
} |
222 |
|
223 |
/** |
224 |
* addAll of a collection with null elements throws NPE |
225 |
*/ |
226 |
public void testAddAll2() { |
227 |
try { |
228 |
PriorityQueue q = new PriorityQueue(SIZE); |
229 |
Integer[] ints = new Integer[SIZE]; |
230 |
q.addAll(Arrays.asList(ints)); |
231 |
shouldThrow(); |
232 |
} catch (NullPointerException success) {} |
233 |
} |
234 |
|
235 |
/** |
236 |
* addAll of a collection with any null elements throws NPE after |
237 |
* possibly adding some elements |
238 |
*/ |
239 |
public void testAddAll3() { |
240 |
try { |
241 |
PriorityQueue q = new PriorityQueue(SIZE); |
242 |
Integer[] ints = new Integer[SIZE]; |
243 |
for (int i = 0; i < SIZE-1; ++i) |
244 |
ints[i] = new Integer(i); |
245 |
q.addAll(Arrays.asList(ints)); |
246 |
shouldThrow(); |
247 |
} catch (NullPointerException success) {} |
248 |
} |
249 |
|
250 |
/** |
251 |
* Queue contains all elements of successful addAll |
252 |
*/ |
253 |
public void testAddAll5() { |
254 |
Integer[] empty = new Integer[0]; |
255 |
Integer[] ints = new Integer[SIZE]; |
256 |
for (int i = 0; i < SIZE; ++i) |
257 |
ints[i] = new Integer(SIZE-1-i); |
258 |
PriorityQueue q = new PriorityQueue(SIZE); |
259 |
assertFalse(q.addAll(Arrays.asList(empty))); |
260 |
assertTrue(q.addAll(Arrays.asList(ints))); |
261 |
for (int i = 0; i < SIZE; ++i) |
262 |
assertEquals(new Integer(i), q.poll()); |
263 |
} |
264 |
|
265 |
/** |
266 |
* poll succeeds unless empty |
267 |
*/ |
268 |
public void testPoll() { |
269 |
PriorityQueue q = populatedQueue(SIZE); |
270 |
for (int i = 0; i < SIZE; ++i) { |
271 |
assertEquals(i, q.poll()); |
272 |
} |
273 |
assertNull(q.poll()); |
274 |
} |
275 |
|
276 |
/** |
277 |
* peek returns next element, or null if empty |
278 |
*/ |
279 |
public void testPeek() { |
280 |
PriorityQueue q = populatedQueue(SIZE); |
281 |
for (int i = 0; i < SIZE; ++i) { |
282 |
assertEquals(i, q.peek()); |
283 |
assertEquals(i, q.poll()); |
284 |
assertTrue(q.peek() == null || |
285 |
!q.peek().equals(i)); |
286 |
} |
287 |
assertNull(q.peek()); |
288 |
} |
289 |
|
290 |
/** |
291 |
* element returns next element, or throws NSEE if empty |
292 |
*/ |
293 |
public void testElement() { |
294 |
PriorityQueue q = populatedQueue(SIZE); |
295 |
for (int i = 0; i < SIZE; ++i) { |
296 |
assertEquals(i, q.element()); |
297 |
assertEquals(i, q.poll()); |
298 |
} |
299 |
try { |
300 |
q.element(); |
301 |
shouldThrow(); |
302 |
} catch (NoSuchElementException success) {} |
303 |
} |
304 |
|
305 |
/** |
306 |
* remove removes next element, or throws NSEE if empty |
307 |
*/ |
308 |
public void testRemove() { |
309 |
PriorityQueue q = populatedQueue(SIZE); |
310 |
for (int i = 0; i < SIZE; ++i) { |
311 |
assertEquals(i, q.remove()); |
312 |
} |
313 |
try { |
314 |
q.remove(); |
315 |
shouldThrow(); |
316 |
} catch (NoSuchElementException success) {} |
317 |
} |
318 |
|
319 |
/** |
320 |
* remove(x) removes x and returns true if present |
321 |
*/ |
322 |
public void testRemoveElement() { |
323 |
PriorityQueue q = populatedQueue(SIZE); |
324 |
for (int i = 1; i < SIZE; i+=2) { |
325 |
assertTrue(q.contains(i)); |
326 |
assertTrue(q.remove(i)); |
327 |
assertFalse(q.contains(i)); |
328 |
assertTrue(q.contains(i-1)); |
329 |
} |
330 |
for (int i = 0; i < SIZE; i+=2) { |
331 |
assertTrue(q.contains(i)); |
332 |
assertTrue(q.remove(i)); |
333 |
assertFalse(q.contains(i)); |
334 |
assertFalse(q.remove(i+1)); |
335 |
assertFalse(q.contains(i+1)); |
336 |
} |
337 |
assertTrue(q.isEmpty()); |
338 |
} |
339 |
|
340 |
/** |
341 |
* contains(x) reports true when elements added but not yet removed |
342 |
*/ |
343 |
public void testContains() { |
344 |
PriorityQueue q = populatedQueue(SIZE); |
345 |
for (int i = 0; i < SIZE; ++i) { |
346 |
assertTrue(q.contains(new Integer(i))); |
347 |
q.poll(); |
348 |
assertFalse(q.contains(new Integer(i))); |
349 |
} |
350 |
} |
351 |
|
352 |
/** |
353 |
* clear removes all elements |
354 |
*/ |
355 |
public void testClear() { |
356 |
PriorityQueue q = populatedQueue(SIZE); |
357 |
q.clear(); |
358 |
assertTrue(q.isEmpty()); |
359 |
assertEquals(0, q.size()); |
360 |
q.add(new Integer(1)); |
361 |
assertFalse(q.isEmpty()); |
362 |
q.clear(); |
363 |
assertTrue(q.isEmpty()); |
364 |
} |
365 |
|
366 |
/** |
367 |
* containsAll(c) is true when c contains a subset of elements |
368 |
*/ |
369 |
public void testContainsAll() { |
370 |
PriorityQueue q = populatedQueue(SIZE); |
371 |
PriorityQueue p = new PriorityQueue(SIZE); |
372 |
for (int i = 0; i < SIZE; ++i) { |
373 |
assertTrue(q.containsAll(p)); |
374 |
assertFalse(p.containsAll(q)); |
375 |
p.add(new Integer(i)); |
376 |
} |
377 |
assertTrue(p.containsAll(q)); |
378 |
} |
379 |
|
380 |
/** |
381 |
* retainAll(c) retains only those elements of c and reports true if changed |
382 |
*/ |
383 |
public void testRetainAll() { |
384 |
PriorityQueue q = populatedQueue(SIZE); |
385 |
PriorityQueue p = populatedQueue(SIZE); |
386 |
for (int i = 0; i < SIZE; ++i) { |
387 |
boolean changed = q.retainAll(p); |
388 |
if (i == 0) |
389 |
assertFalse(changed); |
390 |
else |
391 |
assertTrue(changed); |
392 |
|
393 |
assertTrue(q.containsAll(p)); |
394 |
assertEquals(SIZE-i, q.size()); |
395 |
p.remove(); |
396 |
} |
397 |
} |
398 |
|
399 |
/** |
400 |
* removeAll(c) removes only those elements of c and reports true if changed |
401 |
*/ |
402 |
public void testRemoveAll() { |
403 |
for (int i = 1; i < SIZE; ++i) { |
404 |
PriorityQueue q = populatedQueue(SIZE); |
405 |
PriorityQueue p = populatedQueue(i); |
406 |
assertTrue(q.removeAll(p)); |
407 |
assertEquals(SIZE-i, q.size()); |
408 |
for (int j = 0; j < i; ++j) { |
409 |
Integer I = (Integer)(p.remove()); |
410 |
assertFalse(q.contains(I)); |
411 |
} |
412 |
} |
413 |
} |
414 |
|
415 |
/** |
416 |
* toArray contains all elements |
417 |
*/ |
418 |
public void testToArray() { |
419 |
PriorityQueue q = populatedQueue(SIZE); |
420 |
Object[] o = q.toArray(); |
421 |
Arrays.sort(o); |
422 |
for (int i = 0; i < o.length; i++) |
423 |
assertSame(o[i], q.poll()); |
424 |
} |
425 |
|
426 |
/** |
427 |
* toArray(a) contains all elements |
428 |
*/ |
429 |
public void testToArray2() { |
430 |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
431 |
Integer[] ints = new Integer[SIZE]; |
432 |
Integer[] array = q.toArray(ints); |
433 |
assertSame(ints, array); |
434 |
Arrays.sort(ints); |
435 |
for (int i = 0; i < ints.length; i++) |
436 |
assertSame(ints[i], q.poll()); |
437 |
} |
438 |
|
439 |
/** |
440 |
* iterator iterates through all elements |
441 |
*/ |
442 |
public void testIterator() { |
443 |
PriorityQueue q = populatedQueue(SIZE); |
444 |
int i = 0; |
445 |
Iterator it = q.iterator(); |
446 |
while (it.hasNext()) { |
447 |
assertTrue(q.contains(it.next())); |
448 |
++i; |
449 |
} |
450 |
assertEquals(i, SIZE); |
451 |
} |
452 |
|
453 |
/** |
454 |
* iterator.remove removes current element |
455 |
*/ |
456 |
public void testIteratorRemove() { |
457 |
final PriorityQueue q = new PriorityQueue(3); |
458 |
q.add(new Integer(2)); |
459 |
q.add(new Integer(1)); |
460 |
q.add(new Integer(3)); |
461 |
|
462 |
Iterator it = q.iterator(); |
463 |
it.next(); |
464 |
it.remove(); |
465 |
|
466 |
it = q.iterator(); |
467 |
assertEquals(it.next(), new Integer(2)); |
468 |
assertEquals(it.next(), new Integer(3)); |
469 |
assertFalse(it.hasNext()); |
470 |
} |
471 |
|
472 |
/** |
473 |
* toString contains toStrings of elements |
474 |
*/ |
475 |
public void testToString() { |
476 |
PriorityQueue q = populatedQueue(SIZE); |
477 |
String s = q.toString(); |
478 |
for (int i = 0; i < SIZE; ++i) { |
479 |
assertTrue(s.contains(String.valueOf(i))); |
480 |
} |
481 |
} |
482 |
|
483 |
/** |
484 |
* A deserialized serialized queue has same elements |
485 |
*/ |
486 |
public void testSerialization() throws Exception { |
487 |
Queue x = populatedQueue(SIZE); |
488 |
Queue y = serialClone(x); |
489 |
|
490 |
assertNotSame(x, y); |
491 |
assertEquals(x.size(), y.size()); |
492 |
while (!x.isEmpty()) { |
493 |
assertFalse(y.isEmpty()); |
494 |
assertEquals(x.remove(), y.remove()); |
495 |
} |
496 |
assertTrue(y.isEmpty()); |
497 |
} |
498 |
} |