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/licenses/publicdomain |
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 junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
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 |
|
|
19 |
|
public class PriorityQueueTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
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 |
< |
int i = ((Integer)x).intValue(); |
25 |
< |
int j = ((Integer)y).intValue(); |
26 |
< |
if (i < j) return 1; |
27 |
< |
if (i > j) return -1; |
28 |
< |
return 0; |
48 |
> |
return ((Comparable)y).compareTo(x); |
49 |
|
} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Create a queue of given size containing consecutive |
54 |
< |
* Integers 0 ... n. |
53 |
> |
* Returns a new queue of given size containing consecutive |
54 |
> |
* Integers 0 ... n - 1. |
55 |
|
*/ |
56 |
< |
private PriorityQueue populatedQueue(int n) { |
57 |
< |
PriorityQueue q = new PriorityQueue(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) |
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 { |
81 |
< |
PriorityQueue q = new PriorityQueue(0); |
81 |
> |
new PriorityQueue(0); |
82 |
|
shouldThrow(); |
83 |
|
} catch (IllegalArgumentException success) {} |
84 |
|
} |
88 |
|
*/ |
89 |
|
public void testConstructor3() { |
90 |
|
try { |
91 |
< |
PriorityQueue q = new PriorityQueue((Collection)null); |
91 |
> |
new PriorityQueue((Collection)null); |
92 |
|
shouldThrow(); |
93 |
|
} catch (NullPointerException success) {} |
94 |
|
} |
98 |
|
*/ |
99 |
|
public void testConstructor4() { |
100 |
|
try { |
101 |
< |
Integer[] ints = new Integer[SIZE]; |
81 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
101 |
> |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
102 |
|
shouldThrow(); |
103 |
|
} catch (NullPointerException success) {} |
104 |
|
} |
107 |
|
* Initializing from Collection with some null elements throws NPE |
108 |
|
*/ |
109 |
|
public void testConstructor5() { |
110 |
+ |
Integer[] ints = new Integer[SIZE]; |
111 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
112 |
+ |
ints[i] = new Integer(i); |
113 |
|
try { |
114 |
< |
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)); |
114 |
> |
new PriorityQueue(Arrays.asList(ints)); |
115 |
|
shouldThrow(); |
116 |
|
} catch (NullPointerException success) {} |
117 |
|
} |
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) { |
176 |
|
* offer(null) throws NPE |
177 |
|
*/ |
178 |
|
public void testOfferNull() { |
179 |
+ |
PriorityQueue q = new PriorityQueue(1); |
180 |
|
try { |
160 |
– |
PriorityQueue q = new PriorityQueue(1); |
181 |
|
q.offer(null); |
182 |
|
shouldThrow(); |
183 |
|
} catch (NullPointerException success) {} |
187 |
|
* add(null) throws NPE |
188 |
|
*/ |
189 |
|
public void testAddNull() { |
190 |
+ |
PriorityQueue q = new PriorityQueue(1); |
191 |
|
try { |
171 |
– |
PriorityQueue q = new PriorityQueue(1); |
192 |
|
q.add(null); |
193 |
|
shouldThrow(); |
194 |
|
} catch (NullPointerException success) {} |
207 |
|
* Offer of non-Comparable throws CCE |
208 |
|
*/ |
209 |
|
public void testOfferNonComparable() { |
210 |
+ |
PriorityQueue q = new PriorityQueue(1); |
211 |
|
try { |
191 |
– |
PriorityQueue q = new PriorityQueue(1); |
192 |
– |
q.offer(new Object()); |
193 |
– |
q.offer(new Object()); |
212 |
|
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 |
|
/** |
233 |
|
* addAll(null) throws NPE |
234 |
|
*/ |
235 |
|
public void testAddAll1() { |
236 |
+ |
PriorityQueue q = new PriorityQueue(1); |
237 |
|
try { |
215 |
– |
PriorityQueue q = new PriorityQueue(1); |
238 |
|
q.addAll(null); |
239 |
|
shouldThrow(); |
240 |
|
} catch (NullPointerException success) {} |
241 |
|
} |
242 |
+ |
|
243 |
|
/** |
244 |
|
* addAll of a collection with null elements throws NPE |
245 |
|
*/ |
246 |
|
public void testAddAll2() { |
247 |
+ |
PriorityQueue q = new PriorityQueue(SIZE); |
248 |
|
try { |
249 |
< |
PriorityQueue q = new PriorityQueue(SIZE); |
226 |
< |
Integer[] ints = new Integer[SIZE]; |
227 |
< |
q.addAll(Arrays.asList(ints)); |
249 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
250 |
|
shouldThrow(); |
251 |
|
} catch (NullPointerException success) {} |
252 |
|
} |
253 |
+ |
|
254 |
|
/** |
255 |
|
* addAll of a collection with any null elements throws NPE after |
256 |
|
* possibly adding some elements |
257 |
|
*/ |
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) |
262 |
+ |
ints[i] = new Integer(i); |
263 |
|
try { |
237 |
– |
PriorityQueue q = new PriorityQueue(SIZE); |
238 |
– |
Integer[] ints = new Integer[SIZE]; |
239 |
– |
for (int i = 0; i < SIZE-1; ++i) |
240 |
– |
ints[i] = new Integer(i); |
264 |
|
q.addAll(Arrays.asList(ints)); |
265 |
|
shouldThrow(); |
266 |
|
} catch (NullPointerException success) {} |
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))); |
287 |
|
public void testPoll() { |
288 |
|
PriorityQueue q = populatedQueue(SIZE); |
289 |
|
for (int i = 0; i < SIZE; ++i) { |
290 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
290 |
> |
assertEquals(i, q.poll()); |
291 |
|
} |
292 |
|
assertNull(q.poll()); |
293 |
|
} |
298 |
|
public void testPeek() { |
299 |
|
PriorityQueue q = populatedQueue(SIZE); |
300 |
|
for (int i = 0; i < SIZE; ++i) { |
301 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
302 |
< |
q.poll(); |
301 |
> |
assertEquals(i, q.peek()); |
302 |
> |
assertEquals(i, q.poll()); |
303 |
|
assertTrue(q.peek() == null || |
304 |
< |
i != ((Integer)q.peek()).intValue()); |
304 |
> |
!q.peek().equals(i)); |
305 |
|
} |
306 |
|
assertNull(q.peek()); |
307 |
|
} |
312 |
|
public void testElement() { |
313 |
|
PriorityQueue q = populatedQueue(SIZE); |
314 |
|
for (int i = 0; i < SIZE; ++i) { |
315 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
316 |
< |
q.poll(); |
315 |
> |
assertEquals(i, q.element()); |
316 |
> |
assertEquals(i, q.poll()); |
317 |
|
} |
318 |
|
try { |
319 |
|
q.element(); |
327 |
|
public void testRemove() { |
328 |
|
PriorityQueue q = populatedQueue(SIZE); |
329 |
|
for (int i = 0; i < SIZE; ++i) { |
330 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
330 |
> |
assertEquals(i, q.remove()); |
331 |
|
} |
332 |
|
try { |
333 |
|
q.remove(); |
340 |
|
*/ |
341 |
|
public void testRemoveElement() { |
342 |
|
PriorityQueue q = populatedQueue(SIZE); |
343 |
< |
for (int i = 1; i < SIZE; i+=2) { |
344 |
< |
assertTrue(q.remove(new Integer(i))); |
345 |
< |
} |
346 |
< |
for (int i = 0; i < SIZE; i+=2) { |
347 |
< |
assertTrue(q.remove(new Integer(i))); |
348 |
< |
assertFalse(q.remove(new Integer(i+1))); |
343 |
> |
for (int i = 1; i < SIZE; i += 2) { |
344 |
> |
assertTrue(q.contains(i)); |
345 |
> |
assertTrue(q.remove(i)); |
346 |
> |
assertFalse(q.contains(i)); |
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)); |
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 I = (Integer)(p.remove()); |
429 |
< |
assertFalse(q.contains(I)); |
428 |
> |
Integer x = (Integer)(p.remove()); |
429 |
> |
assertFalse(q.contains(x)); |
430 |
|
} |
431 |
|
} |
432 |
|
} |
436 |
|
*/ |
437 |
|
public void testToArray() { |
438 |
|
PriorityQueue q = populatedQueue(SIZE); |
439 |
< |
Object[] o = q.toArray(); |
440 |
< |
Arrays.sort(o); |
441 |
< |
for (int i = 0; i < o.length; i++) |
442 |
< |
assertEquals(o[i], q.poll()); |
439 |
> |
Object[] a = q.toArray(); |
440 |
> |
assertSame(Object[].class, a.getClass()); |
441 |
> |
Arrays.sort(a); |
442 |
> |
for (Object o : a) |
443 |
> |
assertSame(o, q.poll()); |
444 |
> |
assertTrue(q.isEmpty()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* toArray(a) contains all elements |
449 |
|
*/ |
450 |
|
public void testToArray2() { |
451 |
< |
PriorityQueue q = populatedQueue(SIZE); |
451 |
> |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
452 |
|
Integer[] ints = new Integer[SIZE]; |
453 |
< |
ints = (Integer[])q.toArray(ints); |
453 |
> |
Integer[] array = q.toArray(ints); |
454 |
> |
assertSame(ints, array); |
455 |
|
Arrays.sort(ints); |
456 |
< |
for (int i = 0; i < ints.length; i++) |
457 |
< |
assertEquals(ints[i], q.poll()); |
456 |
> |
for (Integer o : ints) |
457 |
> |
assertSame(o, q.poll()); |
458 |
> |
assertTrue(q.isEmpty()); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
463 |
|
*/ |
464 |
|
public void testIterator() { |
465 |
|
PriorityQueue q = populatedQueue(SIZE); |
433 |
– |
int i = 0; |
466 |
|
Iterator it = q.iterator(); |
467 |
< |
while (it.hasNext()) { |
467 |
> |
int i; |
468 |
> |
for (i = 0; it.hasNext(); i++) |
469 |
|
assertTrue(q.contains(it.next())); |
437 |
– |
++i; |
438 |
– |
} |
470 |
|
assertEquals(i, SIZE); |
471 |
+ |
assertIteratorExhausted(it); |
472 |
+ |
} |
473 |
+ |
|
474 |
+ |
/** |
475 |
+ |
* iterator of empty collection has no elements |
476 |
+ |
*/ |
477 |
+ |
public void testEmptyIterator() { |
478 |
+ |
assertIteratorExhausted(new PriorityQueue().iterator()); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
|
* iterator.remove removes current element |
483 |
|
*/ |
484 |
< |
public void testIteratorRemove () { |
484 |
> |
public void testIteratorRemove() { |
485 |
|
final PriorityQueue q = new PriorityQueue(3); |
486 |
|
q.add(new Integer(2)); |
487 |
|
q.add(new Integer(1)); |
497 |
|
assertFalse(it.hasNext()); |
498 |
|
} |
499 |
|
|
461 |
– |
|
500 |
|
/** |
501 |
|
* toString contains toStrings of elements |
502 |
|
*/ |
504 |
|
PriorityQueue q = populatedQueue(SIZE); |
505 |
|
String s = q.toString(); |
506 |
|
for (int i = 0; i < SIZE; ++i) { |
507 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
507 |
> |
assertTrue(s.contains(String.valueOf(i))); |
508 |
|
} |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* A deserialized serialized queue has same elements |
512 |
> |
* A deserialized/reserialized queue has same elements |
513 |
|
*/ |
514 |
|
public void testSerialization() throws Exception { |
515 |
< |
PriorityQueue q = populatedQueue(SIZE); |
516 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
517 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
518 |
< |
out.writeObject(q); |
519 |
< |
out.close(); |
520 |
< |
|
521 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
522 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
523 |
< |
PriorityQueue r = (PriorityQueue)in.readObject(); |
524 |
< |
assertEquals(q.size(), r.size()); |
487 |
< |
while (!q.isEmpty()) |
488 |
< |
assertEquals(q.remove(), r.remove()); |
515 |
> |
Queue x = populatedQueue(SIZE); |
516 |
> |
Queue y = serialClone(x); |
517 |
> |
|
518 |
> |
assertNotSame(x, y); |
519 |
> |
assertEquals(x.size(), y.size()); |
520 |
> |
while (!x.isEmpty()) { |
521 |
> |
assertFalse(y.isEmpty()); |
522 |
> |
assertEquals(x.remove(), y.remove()); |
523 |
> |
} |
524 |
> |
assertTrue(y.isEmpty()); |
525 |
|
} |
526 |
|
} |