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.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Comparator; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.PriorityQueue; |
16 |
> |
import java.util.Queue; |
17 |
|
|
18 |
|
public class PriorityQueueTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* Create a queue of given size containing consecutive |
33 |
> |
* Creates a queue of given size containing consecutive |
34 |
|
* Integers 0 ... n. |
35 |
|
*/ |
36 |
< |
private PriorityQueue populatedQueue(int n) { |
37 |
< |
PriorityQueue q = new PriorityQueue(n); |
36 |
> |
private PriorityQueue<Integer> populatedQueue(int n) { |
37 |
> |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
|
for (int i = n-1; i >= 0; i-=2) |
40 |
|
assertTrue(q.offer(new Integer(i))); |
320 |
|
public void testRemoveElement() { |
321 |
|
PriorityQueue q = populatedQueue(SIZE); |
322 |
|
for (int i = 1; i < SIZE; i+=2) { |
323 |
< |
assertTrue(q.remove(new Integer(i))); |
323 |
> |
assertTrue(q.contains(i)); |
324 |
> |
assertTrue(q.remove(i)); |
325 |
> |
assertFalse(q.contains(i)); |
326 |
> |
assertTrue(q.contains(i-1)); |
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))); |
329 |
> |
assertTrue(q.contains(i)); |
330 |
> |
assertTrue(q.remove(i)); |
331 |
> |
assertFalse(q.contains(i)); |
332 |
> |
assertFalse(q.remove(i+1)); |
333 |
> |
assertFalse(q.contains(i+1)); |
334 |
|
} |
335 |
|
assertTrue(q.isEmpty()); |
336 |
|
} |
418 |
|
Object[] o = q.toArray(); |
419 |
|
Arrays.sort(o); |
420 |
|
for (int i = 0; i < o.length; i++) |
421 |
< |
assertEquals(o[i], q.poll()); |
421 |
> |
assertSame(o[i], q.poll()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
|
* toArray(a) contains all elements |
426 |
|
*/ |
427 |
|
public void testToArray2() { |
428 |
< |
PriorityQueue q = populatedQueue(SIZE); |
428 |
> |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
429 |
|
Integer[] ints = new Integer[SIZE]; |
430 |
< |
ints = (Integer[])q.toArray(ints); |
430 |
> |
Integer[] array = q.toArray(ints); |
431 |
> |
assertSame(ints, array); |
432 |
|
Arrays.sort(ints); |
433 |
|
for (int i = 0; i < ints.length; i++) |
434 |
< |
assertEquals(ints[i], q.poll()); |
434 |
> |
assertSame(ints[i], q.poll()); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
467 |
|
assertFalse(it.hasNext()); |
468 |
|
} |
469 |
|
|
459 |
– |
|
470 |
|
/** |
471 |
|
* toString contains toStrings of elements |
472 |
|
*/ |
474 |
|
PriorityQueue q = populatedQueue(SIZE); |
475 |
|
String s = q.toString(); |
476 |
|
for (int i = 0; i < SIZE; ++i) { |
477 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
477 |
> |
assertTrue(s.contains(String.valueOf(i))); |
478 |
|
} |
479 |
|
} |
480 |
|
|
482 |
|
* A deserialized serialized queue has same elements |
483 |
|
*/ |
484 |
|
public void testSerialization() throws Exception { |
485 |
< |
PriorityQueue q = populatedQueue(SIZE); |
486 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
487 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
488 |
< |
out.writeObject(q); |
489 |
< |
out.close(); |
490 |
< |
|
491 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
492 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
493 |
< |
PriorityQueue r = (PriorityQueue)in.readObject(); |
494 |
< |
assertEquals(q.size(), r.size()); |
485 |
< |
while (!q.isEmpty()) |
486 |
< |
assertEquals(q.remove(), r.remove()); |
485 |
> |
Queue x = populatedQueue(SIZE); |
486 |
> |
Queue y = serialClone(x); |
487 |
> |
|
488 |
> |
assertTrue(x != y); |
489 |
> |
assertEquals(x.size(), y.size()); |
490 |
> |
while (!x.isEmpty()) { |
491 |
> |
assertFalse(y.isEmpty()); |
492 |
> |
assertEquals(x.remove(), y.remove()); |
493 |
> |
} |
494 |
> |
assertTrue(y.isEmpty()); |
495 |
|
} |
496 |
|
} |