21 |
|
|
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(); |
26 |
< |
if (i < j) return 1; |
27 |
< |
if (i > j) return -1; |
28 |
< |
return 0; |
24 |
> |
return ((Comparable)y).compareTo(x); |
25 |
|
} |
26 |
|
} |
27 |
|
|
96 |
|
* Queue contains all elements of collection used to initialize |
97 |
|
*/ |
98 |
|
public void testConstructor6() { |
99 |
< |
try { |
100 |
< |
Integer[] ints = new Integer[SIZE]; |
101 |
< |
for (int i = 0; i < SIZE; ++i) |
102 |
< |
ints[i] = new Integer(i); |
103 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
104 |
< |
for (int i = 0; i < SIZE; ++i) |
109 |
< |
assertEquals(ints[i], q.poll()); |
110 |
< |
} |
111 |
< |
finally {} |
99 |
> |
Integer[] ints = new Integer[SIZE]; |
100 |
> |
for (int i = 0; i < SIZE; ++i) |
101 |
> |
ints[i] = new Integer(i); |
102 |
> |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
103 |
> |
for (int i = 0; i < SIZE; ++i) |
104 |
> |
assertEquals(ints[i], q.poll()); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
|
* The comparator used in constructor is used |
109 |
|
*/ |
110 |
|
public void testConstructor7() { |
111 |
< |
try { |
112 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
113 |
< |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
114 |
< |
assertEquals(cmp, q.comparator()); |
115 |
< |
Integer[] ints = new Integer[SIZE]; |
116 |
< |
for (int i = 0; i < SIZE; ++i) |
117 |
< |
ints[i] = new Integer(i); |
118 |
< |
q.addAll(Arrays.asList(ints)); |
119 |
< |
for (int i = SIZE-1; i >= 0; --i) |
127 |
< |
assertEquals(ints[i], q.poll()); |
128 |
< |
} |
129 |
< |
finally {} |
111 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
112 |
> |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
113 |
> |
assertEquals(cmp, q.comparator()); |
114 |
> |
Integer[] ints = new Integer[SIZE]; |
115 |
> |
for (int i = 0; i < SIZE; ++i) |
116 |
> |
ints[i] = new Integer(i); |
117 |
> |
q.addAll(Arrays.asList(ints)); |
118 |
> |
for (int i = SIZE-1; i >= 0; --i) |
119 |
> |
assertEquals(ints[i], q.poll()); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
243 |
|
* Queue contains all elements of successful addAll |
244 |
|
*/ |
245 |
|
public void testAddAll5() { |
246 |
< |
try { |
247 |
< |
Integer[] empty = new Integer[0]; |
248 |
< |
Integer[] ints = new Integer[SIZE]; |
249 |
< |
for (int i = 0; i < SIZE; ++i) |
250 |
< |
ints[i] = new Integer(SIZE-1-i); |
251 |
< |
PriorityQueue q = new PriorityQueue(SIZE); |
252 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
253 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
254 |
< |
for (int i = 0; i < SIZE; ++i) |
265 |
< |
assertEquals(new Integer(i), q.poll()); |
266 |
< |
} |
267 |
< |
finally {} |
246 |
> |
Integer[] empty = new Integer[0]; |
247 |
> |
Integer[] ints = new Integer[SIZE]; |
248 |
> |
for (int i = 0; i < SIZE; ++i) |
249 |
> |
ints[i] = new Integer(SIZE-1-i); |
250 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
251 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
252 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
253 |
> |
for (int i = 0; i < SIZE; ++i) |
254 |
> |
assertEquals(new Integer(i), q.poll()); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
260 |
|
public void testPoll() { |
261 |
|
PriorityQueue q = populatedQueue(SIZE); |
262 |
|
for (int i = 0; i < SIZE; ++i) { |
263 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
263 |
> |
assertEquals(i, q.poll()); |
264 |
|
} |
265 |
|
assertNull(q.poll()); |
266 |
|
} |
271 |
|
public void testPeek() { |
272 |
|
PriorityQueue q = populatedQueue(SIZE); |
273 |
|
for (int i = 0; i < SIZE; ++i) { |
274 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
275 |
< |
q.poll(); |
274 |
> |
assertEquals(i, q.peek()); |
275 |
> |
assertEquals(i, q.poll()); |
276 |
|
assertTrue(q.peek() == null || |
277 |
< |
i != ((Integer)q.peek()).intValue()); |
277 |
> |
!q.peek().equals(i)); |
278 |
|
} |
279 |
|
assertNull(q.peek()); |
280 |
|
} |
285 |
|
public void testElement() { |
286 |
|
PriorityQueue q = populatedQueue(SIZE); |
287 |
|
for (int i = 0; i < SIZE; ++i) { |
288 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
289 |
< |
q.poll(); |
288 |
> |
assertEquals(i, q.element()); |
289 |
> |
assertEquals(i, q.poll()); |
290 |
|
} |
291 |
|
try { |
292 |
|
q.element(); |
300 |
|
public void testRemove() { |
301 |
|
PriorityQueue q = populatedQueue(SIZE); |
302 |
|
for (int i = 0; i < SIZE; ++i) { |
303 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
303 |
> |
assertEquals(i, q.remove()); |
304 |
|
} |
305 |
|
try { |
306 |
|
q.remove(); |