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 |
> |
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); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Create a queue of given size containing consecutive |
36 |
< |
* Integers 0 ... n. |
35 |
> |
* Returns a new queue of given size containing consecutive |
36 |
> |
* Integers 0 ... n - 1. |
37 |
|
*/ |
38 |
< |
private PriorityQueue populatedQueue(int n) { |
39 |
< |
PriorityQueue q = new PriorityQueue(n); |
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) |
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) |
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 |
+ |
assertEquals((Integer) 0, q.peek()); |
48 |
|
return q; |
49 |
|
} |
50 |
|
|
60 |
|
*/ |
61 |
|
public void testConstructor2() { |
62 |
|
try { |
63 |
< |
PriorityQueue q = new PriorityQueue(0); |
63 |
> |
new PriorityQueue(0); |
64 |
|
shouldThrow(); |
65 |
|
} catch (IllegalArgumentException success) {} |
66 |
|
} |
70 |
|
*/ |
71 |
|
public void testConstructor3() { |
72 |
|
try { |
73 |
< |
PriorityQueue q = new PriorityQueue((Collection)null); |
73 |
> |
new PriorityQueue((Collection)null); |
74 |
|
shouldThrow(); |
75 |
|
} catch (NullPointerException success) {} |
76 |
|
} |
80 |
|
*/ |
81 |
|
public void testConstructor4() { |
82 |
|
try { |
83 |
< |
Integer[] ints = new Integer[SIZE]; |
77 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
83 |
> |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
84 |
|
shouldThrow(); |
85 |
|
} catch (NullPointerException success) {} |
86 |
|
} |
89 |
|
* Initializing from Collection with some null elements throws NPE |
90 |
|
*/ |
91 |
|
public void testConstructor5() { |
92 |
+ |
Integer[] ints = new Integer[SIZE]; |
93 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
94 |
+ |
ints[i] = new Integer(i); |
95 |
|
try { |
96 |
< |
Integer[] ints = new Integer[SIZE]; |
88 |
< |
for (int i = 0; i < SIZE-1; ++i) |
89 |
< |
ints[i] = new Integer(i); |
90 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
96 |
> |
new PriorityQueue(Arrays.asList(ints)); |
97 |
|
shouldThrow(); |
98 |
|
} catch (NullPointerException success) {} |
99 |
|
} |
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) |
124 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
125 |
|
assertEquals(ints[i], q.poll()); |
126 |
|
} |
127 |
|
|
145 |
|
public void testSize() { |
146 |
|
PriorityQueue q = populatedQueue(SIZE); |
147 |
|
for (int i = 0; i < SIZE; ++i) { |
148 |
< |
assertEquals(SIZE-i, q.size()); |
148 |
> |
assertEquals(SIZE - i, q.size()); |
149 |
|
q.remove(); |
150 |
|
} |
151 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
|
* offer(null) throws NPE |
159 |
|
*/ |
160 |
|
public void testOfferNull() { |
161 |
+ |
PriorityQueue q = new PriorityQueue(1); |
162 |
|
try { |
156 |
– |
PriorityQueue q = new PriorityQueue(1); |
163 |
|
q.offer(null); |
164 |
|
shouldThrow(); |
165 |
|
} catch (NullPointerException success) {} |
169 |
|
* add(null) throws NPE |
170 |
|
*/ |
171 |
|
public void testAddNull() { |
172 |
+ |
PriorityQueue q = new PriorityQueue(1); |
173 |
|
try { |
167 |
– |
PriorityQueue q = new PriorityQueue(1); |
174 |
|
q.add(null); |
175 |
|
shouldThrow(); |
176 |
|
} catch (NullPointerException success) {} |
189 |
|
* Offer of non-Comparable throws CCE |
190 |
|
*/ |
191 |
|
public void testOfferNonComparable() { |
192 |
+ |
PriorityQueue q = new PriorityQueue(1); |
193 |
|
try { |
187 |
– |
PriorityQueue q = new PriorityQueue(1); |
188 |
– |
q.offer(new Object()); |
189 |
– |
q.offer(new Object()); |
194 |
|
q.offer(new Object()); |
195 |
|
shouldThrow(); |
196 |
< |
} catch (ClassCastException success) {} |
196 |
> |
} catch (ClassCastException success) { |
197 |
> |
assertTrue(q.isEmpty()); |
198 |
> |
assertEquals(0, q.size()); |
199 |
> |
assertNull(q.poll()); |
200 |
> |
} |
201 |
|
} |
202 |
|
|
203 |
|
/** |
215 |
|
* addAll(null) throws NPE |
216 |
|
*/ |
217 |
|
public void testAddAll1() { |
218 |
+ |
PriorityQueue q = new PriorityQueue(1); |
219 |
|
try { |
211 |
– |
PriorityQueue q = new PriorityQueue(1); |
220 |
|
q.addAll(null); |
221 |
|
shouldThrow(); |
222 |
|
} catch (NullPointerException success) {} |
226 |
|
* addAll of a collection with null elements throws NPE |
227 |
|
*/ |
228 |
|
public void testAddAll2() { |
229 |
+ |
PriorityQueue q = new PriorityQueue(SIZE); |
230 |
|
try { |
231 |
< |
PriorityQueue q = new PriorityQueue(SIZE); |
223 |
< |
Integer[] ints = new Integer[SIZE]; |
224 |
< |
q.addAll(Arrays.asList(ints)); |
231 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
232 |
|
shouldThrow(); |
233 |
|
} catch (NullPointerException success) {} |
234 |
|
} |
238 |
|
* possibly adding some elements |
239 |
|
*/ |
240 |
|
public void testAddAll3() { |
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 |
|
try { |
235 |
– |
PriorityQueue q = new PriorityQueue(SIZE); |
236 |
– |
Integer[] ints = new Integer[SIZE]; |
237 |
– |
for (int i = 0; i < SIZE-1; ++i) |
238 |
– |
ints[i] = new Integer(i); |
246 |
|
q.addAll(Arrays.asList(ints)); |
247 |
|
shouldThrow(); |
248 |
|
} catch (NullPointerException success) {} |
255 |
|
Integer[] empty = new Integer[0]; |
256 |
|
Integer[] ints = new Integer[SIZE]; |
257 |
|
for (int i = 0; i < SIZE; ++i) |
258 |
< |
ints[i] = new Integer(SIZE-1-i); |
258 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
259 |
|
PriorityQueue q = new PriorityQueue(SIZE); |
260 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
261 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
322 |
|
*/ |
323 |
|
public void testRemoveElement() { |
324 |
|
PriorityQueue q = populatedQueue(SIZE); |
325 |
< |
for (int i = 1; i < SIZE; i+=2) { |
326 |
< |
assertTrue(q.remove(new Integer(i))); |
327 |
< |
} |
328 |
< |
for (int i = 0; i < SIZE; i+=2) { |
329 |
< |
assertTrue(q.remove(new Integer(i))); |
330 |
< |
assertFalse(q.remove(new Integer(i+1))); |
325 |
> |
for (int i = 1; i < SIZE; i += 2) { |
326 |
> |
assertTrue(q.contains(i)); |
327 |
> |
assertTrue(q.remove(i)); |
328 |
> |
assertFalse(q.contains(i)); |
329 |
> |
assertTrue(q.contains(i - 1)); |
330 |
> |
} |
331 |
> |
for (int i = 0; i < SIZE; i += 2) { |
332 |
> |
assertTrue(q.contains(i)); |
333 |
> |
assertTrue(q.remove(i)); |
334 |
> |
assertFalse(q.contains(i)); |
335 |
> |
assertFalse(q.remove(i + 1)); |
336 |
> |
assertFalse(q.contains(i + 1)); |
337 |
|
} |
338 |
|
assertTrue(q.isEmpty()); |
339 |
|
} |
392 |
|
assertTrue(changed); |
393 |
|
|
394 |
|
assertTrue(q.containsAll(p)); |
395 |
< |
assertEquals(SIZE-i, q.size()); |
395 |
> |
assertEquals(SIZE - i, q.size()); |
396 |
|
p.remove(); |
397 |
|
} |
398 |
|
} |
405 |
|
PriorityQueue q = populatedQueue(SIZE); |
406 |
|
PriorityQueue p = populatedQueue(i); |
407 |
|
assertTrue(q.removeAll(p)); |
408 |
< |
assertEquals(SIZE-i, q.size()); |
408 |
> |
assertEquals(SIZE - i, q.size()); |
409 |
|
for (int j = 0; j < i; ++j) { |
410 |
< |
Integer I = (Integer)(p.remove()); |
411 |
< |
assertFalse(q.contains(I)); |
410 |
> |
Integer x = (Integer)(p.remove()); |
411 |
> |
assertFalse(q.contains(x)); |
412 |
|
} |
413 |
|
} |
414 |
|
} |
428 |
|
* toArray(a) contains all elements |
429 |
|
*/ |
430 |
|
public void testToArray2() { |
431 |
< |
PriorityQueue q = populatedQueue(SIZE); |
431 |
> |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
432 |
|
Integer[] ints = new Integer[SIZE]; |
433 |
< |
assertSame(ints, q.toArray(ints)); |
433 |
> |
Integer[] array = q.toArray(ints); |
434 |
> |
assertSame(ints, array); |
435 |
|
Arrays.sort(ints); |
436 |
|
for (int i = 0; i < ints.length; i++) |
437 |
|
assertSame(ints[i], q.poll()); |
442 |
|
*/ |
443 |
|
public void testIterator() { |
444 |
|
PriorityQueue q = populatedQueue(SIZE); |
431 |
– |
int i = 0; |
445 |
|
Iterator it = q.iterator(); |
446 |
< |
while (it.hasNext()) { |
446 |
> |
int i; |
447 |
> |
for (i = 0; it.hasNext(); i++) |
448 |
|
assertTrue(q.contains(it.next())); |
435 |
– |
++i; |
436 |
– |
} |
449 |
|
assertEquals(i, SIZE); |
450 |
+ |
assertIteratorExhausted(it); |
451 |
+ |
} |
452 |
+ |
|
453 |
+ |
/** |
454 |
+ |
* iterator of empty collection has no elements |
455 |
+ |
*/ |
456 |
+ |
public void testEmptyIterator() { |
457 |
+ |
assertIteratorExhausted(new PriorityQueue().iterator()); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
476 |
|
assertFalse(it.hasNext()); |
477 |
|
} |
478 |
|
|
459 |
– |
|
479 |
|
/** |
480 |
|
* toString contains toStrings of elements |
481 |
|
*/ |
483 |
|
PriorityQueue q = populatedQueue(SIZE); |
484 |
|
String s = q.toString(); |
485 |
|
for (int i = 0; i < SIZE; ++i) { |
486 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
486 |
> |
assertTrue(s.contains(String.valueOf(i))); |
487 |
|
} |
488 |
|
} |
489 |
|
|
491 |
|
* A deserialized serialized queue has same elements |
492 |
|
*/ |
493 |
|
public void testSerialization() throws Exception { |
494 |
< |
PriorityQueue q = populatedQueue(SIZE); |
495 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
496 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
497 |
< |
out.writeObject(q); |
498 |
< |
out.close(); |
499 |
< |
|
500 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
501 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
502 |
< |
PriorityQueue r = (PriorityQueue)in.readObject(); |
503 |
< |
assertEquals(q.size(), r.size()); |
485 |
< |
while (!q.isEmpty()) |
486 |
< |
assertEquals(q.remove(), r.remove()); |
494 |
> |
Queue x = populatedQueue(SIZE); |
495 |
> |
Queue y = serialClone(x); |
496 |
> |
|
497 |
> |
assertNotSame(x, y); |
498 |
> |
assertEquals(x.size(), y.size()); |
499 |
> |
while (!x.isEmpty()) { |
500 |
> |
assertFalse(y.isEmpty()); |
501 |
> |
assertEquals(x.remove(), y.remove()); |
502 |
> |
} |
503 |
> |
assertTrue(y.isEmpty()); |
504 |
|
} |
505 |
|
} |