1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
13 |
|
|
14 |
|
public class PriorityQueueTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(PriorityQueueTest.class); |
19 |
> |
return new TestSuite(PriorityQueueTest.class); |
20 |
|
} |
21 |
|
|
22 |
< |
static class MyReverseComparator implements Comparator { |
22 |
> |
static class MyReverseComparator implements Comparator { |
23 |
|
public int compare(Object x, Object y) { |
24 |
|
int i = ((Integer)x).intValue(); |
25 |
|
int j = ((Integer)y).intValue(); |
36 |
|
private PriorityQueue populatedQueue(int n) { |
37 |
|
PriorityQueue q = new PriorityQueue(n); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
< |
for(int i = n-1; i >= 0; i-=2) |
40 |
< |
assertTrue(q.offer(new Integer(i))); |
41 |
< |
for(int i = (n & 1); i < n; i+=2) |
42 |
< |
assertTrue(q.offer(new Integer(i))); |
39 |
> |
for (int i = n-1; i >= 0; i-=2) |
40 |
> |
assertTrue(q.offer(new Integer(i))); |
41 |
> |
for (int i = (n & 1); i < n; i+=2) |
42 |
> |
assertTrue(q.offer(new Integer(i))); |
43 |
|
assertFalse(q.isEmpty()); |
44 |
< |
assertEquals(n, q.size()); |
44 |
> |
assertEquals(n, q.size()); |
45 |
|
return q; |
46 |
|
} |
47 |
< |
|
47 |
> |
|
48 |
|
/** |
49 |
|
* A new queue has unbounded capacity |
50 |
|
*/ |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* Constructor throws IAE if capacity argument nonpositive |
56 |
> |
* Constructor throws IAE if capacity argument nonpositive |
57 |
|
*/ |
58 |
|
public void testConstructor2() { |
59 |
|
try { |
60 |
|
PriorityQueue q = new PriorityQueue(0); |
61 |
|
shouldThrow(); |
62 |
< |
} |
62 |
< |
catch (IllegalArgumentException success) {} |
62 |
> |
} catch (IllegalArgumentException success) {} |
63 |
|
} |
64 |
|
|
65 |
|
/** |
69 |
|
try { |
70 |
|
PriorityQueue q = new PriorityQueue((Collection)null); |
71 |
|
shouldThrow(); |
72 |
< |
} |
73 |
< |
catch (NullPointerException success) {} |
72 |
> |
} catch (NullPointerException success) {} |
73 |
|
} |
74 |
|
|
75 |
|
/** |
80 |
|
Integer[] ints = new Integer[SIZE]; |
81 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
82 |
|
shouldThrow(); |
83 |
< |
} |
85 |
< |
catch (NullPointerException success) {} |
83 |
> |
} catch (NullPointerException success) {} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
93 |
|
ints[i] = new Integer(i); |
94 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
95 |
|
shouldThrow(); |
96 |
< |
} |
99 |
< |
catch (NullPointerException success) {} |
96 |
> |
} catch (NullPointerException success) {} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
162 |
|
* offer(null) throws NPE |
163 |
|
*/ |
164 |
|
public void testOfferNull() { |
165 |
< |
try { |
165 |
> |
try { |
166 |
|
PriorityQueue q = new PriorityQueue(1); |
167 |
|
q.offer(null); |
168 |
|
shouldThrow(); |
169 |
< |
} catch (NullPointerException success) { } |
169 |
> |
} catch (NullPointerException success) {} |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
|
* add(null) throws NPE |
174 |
|
*/ |
175 |
|
public void testAddNull() { |
176 |
< |
try { |
176 |
> |
try { |
177 |
|
PriorityQueue q = new PriorityQueue(1); |
178 |
|
q.add(null); |
179 |
|
shouldThrow(); |
180 |
< |
} catch (NullPointerException success) { } |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
|
|
183 |
|
/** |
199 |
|
q.offer(new Object()); |
200 |
|
q.offer(new Object()); |
201 |
|
shouldThrow(); |
202 |
< |
} |
206 |
< |
catch(ClassCastException success) {} |
202 |
> |
} catch (ClassCastException success) {} |
203 |
|
} |
204 |
|
|
205 |
|
/** |
221 |
|
PriorityQueue q = new PriorityQueue(1); |
222 |
|
q.addAll(null); |
223 |
|
shouldThrow(); |
224 |
< |
} |
229 |
< |
catch (NullPointerException success) {} |
224 |
> |
} catch (NullPointerException success) {} |
225 |
|
} |
226 |
|
/** |
227 |
|
* addAll of a collection with null elements throws NPE |
232 |
|
Integer[] ints = new Integer[SIZE]; |
233 |
|
q.addAll(Arrays.asList(ints)); |
234 |
|
shouldThrow(); |
235 |
< |
} |
241 |
< |
catch (NullPointerException success) {} |
235 |
> |
} catch (NullPointerException success) {} |
236 |
|
} |
237 |
|
/** |
238 |
|
* addAll of a collection with any null elements throws NPE after |
246 |
|
ints[i] = new Integer(i); |
247 |
|
q.addAll(Arrays.asList(ints)); |
248 |
|
shouldThrow(); |
249 |
< |
} |
256 |
< |
catch (NullPointerException success) {} |
249 |
> |
} catch (NullPointerException success) {} |
250 |
|
} |
251 |
|
|
252 |
|
/** |
275 |
|
for (int i = 0; i < SIZE; ++i) { |
276 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
277 |
|
} |
278 |
< |
assertNull(q.poll()); |
278 |
> |
assertNull(q.poll()); |
279 |
|
} |
280 |
|
|
281 |
|
/** |
289 |
|
assertTrue(q.peek() == null || |
290 |
|
i != ((Integer)q.peek()).intValue()); |
291 |
|
} |
292 |
< |
assertNull(q.peek()); |
292 |
> |
assertNull(q.peek()); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
304 |
|
try { |
305 |
|
q.element(); |
306 |
|
shouldThrow(); |
307 |
< |
} |
315 |
< |
catch (NoSuchElementException success) {} |
307 |
> |
} catch (NoSuchElementException success) {} |
308 |
|
} |
309 |
|
|
310 |
|
/** |
318 |
|
try { |
319 |
|
q.remove(); |
320 |
|
shouldThrow(); |
321 |
< |
} catch (NoSuchElementException success){ |
330 |
< |
} |
321 |
> |
} catch (NoSuchElementException success) {} |
322 |
|
} |
323 |
|
|
324 |
|
/** |
335 |
|
} |
336 |
|
assertTrue(q.isEmpty()); |
337 |
|
} |
338 |
< |
|
338 |
> |
|
339 |
|
/** |
340 |
|
* contains(x) reports true when elements added but not yet removed |
341 |
|
*/ |
416 |
|
*/ |
417 |
|
public void testToArray() { |
418 |
|
PriorityQueue q = populatedQueue(SIZE); |
419 |
< |
Object[] o = q.toArray(); |
419 |
> |
Object[] o = q.toArray(); |
420 |
|
Arrays.sort(o); |
421 |
< |
for(int i = 0; i < o.length; i++) |
422 |
< |
assertEquals(o[i], q.poll()); |
421 |
> |
for (int i = 0; i < o.length; i++) |
422 |
> |
assertEquals(o[i], q.poll()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
427 |
|
*/ |
428 |
|
public void testToArray2() { |
429 |
|
PriorityQueue q = populatedQueue(SIZE); |
430 |
< |
Integer[] ints = new Integer[SIZE]; |
431 |
< |
ints = (Integer[])q.toArray(ints); |
430 |
> |
Integer[] ints = new Integer[SIZE]; |
431 |
> |
ints = (Integer[])q.toArray(ints); |
432 |
|
Arrays.sort(ints); |
433 |
< |
for(int i = 0; i < ints.length; i++) |
433 |
> |
for (int i = 0; i < ints.length; i++) |
434 |
|
assertEquals(ints[i], q.poll()); |
435 |
|
} |
436 |
< |
|
436 |
> |
|
437 |
|
/** |
438 |
|
* iterator iterates through all elements |
439 |
|
*/ |
440 |
|
public void testIterator() { |
441 |
|
PriorityQueue q = populatedQueue(SIZE); |
442 |
|
int i = 0; |
443 |
< |
Iterator it = q.iterator(); |
444 |
< |
while(it.hasNext()) { |
443 |
> |
Iterator it = q.iterator(); |
444 |
> |
while (it.hasNext()) { |
445 |
|
assertTrue(q.contains(it.next())); |
446 |
|
++i; |
447 |
|
} |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
479 |
|
} |
480 |
< |
} |
480 |
> |
} |
481 |
|
|
482 |
|
/** |
483 |
< |
* A deserialized serialized queue has same elements |
483 |
> |
* A deserialized serialized queue has same elements |
484 |
|
*/ |
485 |
< |
public void testSerialization() { |
485 |
> |
public void testSerialization() throws Exception { |
486 |
|
PriorityQueue q = populatedQueue(SIZE); |
487 |
< |
try { |
488 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
489 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
490 |
< |
out.writeObject(q); |
491 |
< |
out.close(); |
492 |
< |
|
493 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
494 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
495 |
< |
PriorityQueue r = (PriorityQueue)in.readObject(); |
496 |
< |
assertEquals(q.size(), r.size()); |
497 |
< |
while (!q.isEmpty()) |
507 |
< |
assertEquals(q.remove(), r.remove()); |
508 |
< |
} catch(Exception e){ |
509 |
< |
unexpectedException(); |
510 |
< |
} |
487 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
488 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
489 |
> |
out.writeObject(q); |
490 |
> |
out.close(); |
491 |
> |
|
492 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
493 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
494 |
> |
PriorityQueue r = (PriorityQueue)in.readObject(); |
495 |
> |
assertEquals(q.size(), r.size()); |
496 |
> |
while (!q.isEmpty()) |
497 |
> |
assertEquals(q.remove(), r.remove()); |
498 |
|
} |
499 |
|
} |