author | dl |
Mon, 12 Sep 2016 13:14:49 -0700 | |
changeset 40817 | 4f5fb115676d |
parent 39039 | 9dea20ff2dee |
child 42322 | c3474fef4fe4 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import java.util.Arrays; |
|
37 |
import java.util.Collection; |
|
38 |
import java.util.Comparator; |
|
39 |
import java.util.Iterator; |
|
40 |
import java.util.NoSuchElementException; |
|
41 |
import java.util.PriorityQueue; |
|
42 |
import java.util.Queue; |
|
43 |
||
44 |
import junit.framework.Test; |
|
45 |
import junit.framework.TestSuite; |
|
46 |
||
47 |
public class PriorityQueueTest extends JSR166TestCase { |
|
48 |
public static void main(String[] args) { |
|
49 |
main(suite(), args); |
|
50 |
} |
|
51 |
public static Test suite() { |
|
52 |
return new TestSuite(PriorityQueueTest.class); |
|
53 |
} |
|
54 |
||
55 |
static class MyReverseComparator implements Comparator { |
|
56 |
public int compare(Object x, Object y) { |
|
57 |
return ((Comparable)y).compareTo(x); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Returns a new queue of given size containing consecutive |
|
63 |
* Integers 0 ... n. |
|
64 |
*/ |
|
65 |
private PriorityQueue<Integer> populatedQueue(int n) { |
|
66 |
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
|
67 |
assertTrue(q.isEmpty()); |
|
68 |
for (int i = n - 1; i >= 0; i -= 2) |
|
69 |
assertTrue(q.offer(new Integer(i))); |
|
70 |
for (int i = (n & 1); i < n; i += 2) |
|
71 |
assertTrue(q.offer(new Integer(i))); |
|
72 |
assertFalse(q.isEmpty()); |
|
73 |
assertEquals(n, q.size()); |
|
74 |
return q; |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* A new queue has unbounded capacity |
|
79 |
*/ |
|
80 |
public void testConstructor1() { |
|
81 |
assertEquals(0, new PriorityQueue(SIZE).size()); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Constructor throws IAE if capacity argument nonpositive |
|
86 |
*/ |
|
87 |
public void testConstructor2() { |
|
88 |
try { |
|
89 |
new PriorityQueue(0); |
|
90 |
shouldThrow(); |
|
91 |
} catch (IllegalArgumentException success) {} |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Initializing from null Collection throws NPE |
|
96 |
*/ |
|
97 |
public void testConstructor3() { |
|
98 |
try { |
|
99 |
new PriorityQueue((Collection)null); |
|
100 |
shouldThrow(); |
|
101 |
} catch (NullPointerException success) {} |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Initializing from Collection of null elements throws NPE |
|
106 |
*/ |
|
107 |
public void testConstructor4() { |
|
108 |
try { |
|
109 |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
|
110 |
shouldThrow(); |
|
111 |
} catch (NullPointerException success) {} |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Initializing from Collection with some null elements throws NPE |
|
116 |
*/ |
|
117 |
public void testConstructor5() { |
|
118 |
Integer[] ints = new Integer[SIZE]; |
|
119 |
for (int i = 0; i < SIZE - 1; ++i) |
|
120 |
ints[i] = new Integer(i); |
|
121 |
try { |
|
122 |
new PriorityQueue(Arrays.asList(ints)); |
|
123 |
shouldThrow(); |
|
124 |
} catch (NullPointerException success) {} |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Queue contains all elements of collection used to initialize |
|
129 |
*/ |
|
130 |
public void testConstructor6() { |
|
131 |
Integer[] ints = new Integer[SIZE]; |
|
132 |
for (int i = 0; i < SIZE; ++i) |
|
133 |
ints[i] = new Integer(i); |
|
134 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
|
135 |
for (int i = 0; i < SIZE; ++i) |
|
136 |
assertEquals(ints[i], q.poll()); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* The comparator used in constructor is used |
|
141 |
*/ |
|
142 |
public void testConstructor7() { |
|
143 |
MyReverseComparator cmp = new MyReverseComparator(); |
|
144 |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
|
145 |
assertEquals(cmp, q.comparator()); |
|
146 |
Integer[] ints = new Integer[SIZE]; |
|
147 |
for (int i = 0; i < SIZE; ++i) |
|
148 |
ints[i] = new Integer(i); |
|
149 |
q.addAll(Arrays.asList(ints)); |
|
150 |
for (int i = SIZE - 1; i >= 0; --i) |
|
151 |
assertEquals(ints[i], q.poll()); |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* isEmpty is true before add, false after |
|
156 |
*/ |
|
157 |
public void testEmpty() { |
|
158 |
PriorityQueue q = new PriorityQueue(2); |
|
159 |
assertTrue(q.isEmpty()); |
|
160 |
q.add(new Integer(1)); |
|
161 |
assertFalse(q.isEmpty()); |
|
162 |
q.add(new Integer(2)); |
|
163 |
q.remove(); |
|
164 |
q.remove(); |
|
165 |
assertTrue(q.isEmpty()); |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* size changes when elements added and removed |
|
170 |
*/ |
|
171 |
public void testSize() { |
|
172 |
PriorityQueue q = populatedQueue(SIZE); |
|
173 |
for (int i = 0; i < SIZE; ++i) { |
|
174 |
assertEquals(SIZE - i, q.size()); |
|
175 |
q.remove(); |
|
176 |
} |
|
177 |
for (int i = 0; i < SIZE; ++i) { |
|
178 |
assertEquals(i, q.size()); |
|
179 |
q.add(new Integer(i)); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* offer(null) throws NPE |
|
185 |
*/ |
|
186 |
public void testOfferNull() { |
|
187 |
PriorityQueue q = new PriorityQueue(1); |
|
188 |
try { |
|
189 |
q.offer(null); |
|
190 |
shouldThrow(); |
|
191 |
} catch (NullPointerException success) {} |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* add(null) throws NPE |
|
196 |
*/ |
|
197 |
public void testAddNull() { |
|
198 |
PriorityQueue q = new PriorityQueue(1); |
|
199 |
try { |
|
200 |
q.add(null); |
|
201 |
shouldThrow(); |
|
202 |
} catch (NullPointerException success) {} |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Offer of comparable element succeeds |
|
207 |
*/ |
|
208 |
public void testOffer() { |
|
209 |
PriorityQueue q = new PriorityQueue(1); |
|
210 |
assertTrue(q.offer(zero)); |
|
211 |
assertTrue(q.offer(one)); |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Offer of non-Comparable throws CCE |
|
216 |
*/ |
|
217 |
public void testOfferNonComparable() { |
|
218 |
PriorityQueue q = new PriorityQueue(1); |
|
219 |
try { |
|
220 |
q.offer(new Object()); |
|
221 |
shouldThrow(); |
|
39039
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
222 |
} catch (ClassCastException success) { |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
223 |
assertTrue(q.isEmpty()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
224 |
assertEquals(0, q.size()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
225 |
assertNull(q.poll()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
226 |
} |
35394 | 227 |
} |
228 |
||
229 |
/** |
|
230 |
* add of comparable succeeds |
|
231 |
*/ |
|
232 |
public void testAdd() { |
|
233 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
234 |
for (int i = 0; i < SIZE; ++i) { |
|
235 |
assertEquals(i, q.size()); |
|
236 |
assertTrue(q.add(new Integer(i))); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* addAll(null) throws NPE |
|
242 |
*/ |
|
243 |
public void testAddAll1() { |
|
244 |
PriorityQueue q = new PriorityQueue(1); |
|
245 |
try { |
|
246 |
q.addAll(null); |
|
247 |
shouldThrow(); |
|
248 |
} catch (NullPointerException success) {} |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* addAll of a collection with null elements throws NPE |
|
253 |
*/ |
|
254 |
public void testAddAll2() { |
|
255 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
256 |
try { |
|
257 |
q.addAll(Arrays.asList(new Integer[SIZE])); |
|
258 |
shouldThrow(); |
|
259 |
} catch (NullPointerException success) {} |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* addAll of a collection with any null elements throws NPE after |
|
264 |
* possibly adding some elements |
|
265 |
*/ |
|
266 |
public void testAddAll3() { |
|
267 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
268 |
Integer[] ints = new Integer[SIZE]; |
|
269 |
for (int i = 0; i < SIZE - 1; ++i) |
|
270 |
ints[i] = new Integer(i); |
|
271 |
try { |
|
272 |
q.addAll(Arrays.asList(ints)); |
|
273 |
shouldThrow(); |
|
274 |
} catch (NullPointerException success) {} |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Queue contains all elements of successful addAll |
|
279 |
*/ |
|
280 |
public void testAddAll5() { |
|
281 |
Integer[] empty = new Integer[0]; |
|
282 |
Integer[] ints = new Integer[SIZE]; |
|
283 |
for (int i = 0; i < SIZE; ++i) |
|
284 |
ints[i] = new Integer(SIZE - 1 - i); |
|
285 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
286 |
assertFalse(q.addAll(Arrays.asList(empty))); |
|
287 |
assertTrue(q.addAll(Arrays.asList(ints))); |
|
288 |
for (int i = 0; i < SIZE; ++i) |
|
289 |
assertEquals(new Integer(i), q.poll()); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* poll succeeds unless empty |
|
294 |
*/ |
|
295 |
public void testPoll() { |
|
296 |
PriorityQueue q = populatedQueue(SIZE); |
|
297 |
for (int i = 0; i < SIZE; ++i) { |
|
298 |
assertEquals(i, q.poll()); |
|
299 |
} |
|
300 |
assertNull(q.poll()); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* peek returns next element, or null if empty |
|
305 |
*/ |
|
306 |
public void testPeek() { |
|
307 |
PriorityQueue q = populatedQueue(SIZE); |
|
308 |
for (int i = 0; i < SIZE; ++i) { |
|
309 |
assertEquals(i, q.peek()); |
|
310 |
assertEquals(i, q.poll()); |
|
311 |
assertTrue(q.peek() == null || |
|
312 |
!q.peek().equals(i)); |
|
313 |
} |
|
314 |
assertNull(q.peek()); |
|
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* element returns next element, or throws NSEE if empty |
|
319 |
*/ |
|
320 |
public void testElement() { |
|
321 |
PriorityQueue q = populatedQueue(SIZE); |
|
322 |
for (int i = 0; i < SIZE; ++i) { |
|
323 |
assertEquals(i, q.element()); |
|
324 |
assertEquals(i, q.poll()); |
|
325 |
} |
|
326 |
try { |
|
327 |
q.element(); |
|
328 |
shouldThrow(); |
|
329 |
} catch (NoSuchElementException success) {} |
|
330 |
} |
|
331 |
||
332 |
/** |
|
333 |
* remove removes next element, or throws NSEE if empty |
|
334 |
*/ |
|
335 |
public void testRemove() { |
|
336 |
PriorityQueue q = populatedQueue(SIZE); |
|
337 |
for (int i = 0; i < SIZE; ++i) { |
|
338 |
assertEquals(i, q.remove()); |
|
339 |
} |
|
340 |
try { |
|
341 |
q.remove(); |
|
342 |
shouldThrow(); |
|
343 |
} catch (NoSuchElementException success) {} |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* remove(x) removes x and returns true if present |
|
348 |
*/ |
|
349 |
public void testRemoveElement() { |
|
350 |
PriorityQueue q = populatedQueue(SIZE); |
|
351 |
for (int i = 1; i < SIZE; i += 2) { |
|
352 |
assertTrue(q.contains(i)); |
|
353 |
assertTrue(q.remove(i)); |
|
354 |
assertFalse(q.contains(i)); |
|
355 |
assertTrue(q.contains(i - 1)); |
|
356 |
} |
|
357 |
for (int i = 0; i < SIZE; i += 2) { |
|
358 |
assertTrue(q.contains(i)); |
|
359 |
assertTrue(q.remove(i)); |
|
360 |
assertFalse(q.contains(i)); |
|
361 |
assertFalse(q.remove(i + 1)); |
|
362 |
assertFalse(q.contains(i + 1)); |
|
363 |
} |
|
364 |
assertTrue(q.isEmpty()); |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* contains(x) reports true when elements added but not yet removed |
|
369 |
*/ |
|
370 |
public void testContains() { |
|
371 |
PriorityQueue q = populatedQueue(SIZE); |
|
372 |
for (int i = 0; i < SIZE; ++i) { |
|
373 |
assertTrue(q.contains(new Integer(i))); |
|
374 |
q.poll(); |
|
375 |
assertFalse(q.contains(new Integer(i))); |
|
376 |
} |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* clear removes all elements |
|
381 |
*/ |
|
382 |
public void testClear() { |
|
383 |
PriorityQueue q = populatedQueue(SIZE); |
|
384 |
q.clear(); |
|
385 |
assertTrue(q.isEmpty()); |
|
386 |
assertEquals(0, q.size()); |
|
387 |
q.add(new Integer(1)); |
|
388 |
assertFalse(q.isEmpty()); |
|
389 |
q.clear(); |
|
390 |
assertTrue(q.isEmpty()); |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* containsAll(c) is true when c contains a subset of elements |
|
395 |
*/ |
|
396 |
public void testContainsAll() { |
|
397 |
PriorityQueue q = populatedQueue(SIZE); |
|
398 |
PriorityQueue p = new PriorityQueue(SIZE); |
|
399 |
for (int i = 0; i < SIZE; ++i) { |
|
400 |
assertTrue(q.containsAll(p)); |
|
401 |
assertFalse(p.containsAll(q)); |
|
402 |
p.add(new Integer(i)); |
|
403 |
} |
|
404 |
assertTrue(p.containsAll(q)); |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* retainAll(c) retains only those elements of c and reports true if changed |
|
409 |
*/ |
|
410 |
public void testRetainAll() { |
|
411 |
PriorityQueue q = populatedQueue(SIZE); |
|
412 |
PriorityQueue p = populatedQueue(SIZE); |
|
413 |
for (int i = 0; i < SIZE; ++i) { |
|
414 |
boolean changed = q.retainAll(p); |
|
415 |
if (i == 0) |
|
416 |
assertFalse(changed); |
|
417 |
else |
|
418 |
assertTrue(changed); |
|
419 |
||
420 |
assertTrue(q.containsAll(p)); |
|
421 |
assertEquals(SIZE - i, q.size()); |
|
422 |
p.remove(); |
|
423 |
} |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* removeAll(c) removes only those elements of c and reports true if changed |
|
428 |
*/ |
|
429 |
public void testRemoveAll() { |
|
430 |
for (int i = 1; i < SIZE; ++i) { |
|
431 |
PriorityQueue q = populatedQueue(SIZE); |
|
432 |
PriorityQueue p = populatedQueue(i); |
|
433 |
assertTrue(q.removeAll(p)); |
|
434 |
assertEquals(SIZE - i, q.size()); |
|
435 |
for (int j = 0; j < i; ++j) { |
|
436 |
Integer x = (Integer)(p.remove()); |
|
437 |
assertFalse(q.contains(x)); |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* toArray contains all elements |
|
444 |
*/ |
|
445 |
public void testToArray() { |
|
446 |
PriorityQueue q = populatedQueue(SIZE); |
|
447 |
Object[] o = q.toArray(); |
|
448 |
Arrays.sort(o); |
|
449 |
for (int i = 0; i < o.length; i++) |
|
450 |
assertSame(o[i], q.poll()); |
|
451 |
} |
|
452 |
||
453 |
/** |
|
454 |
* toArray(a) contains all elements |
|
455 |
*/ |
|
456 |
public void testToArray2() { |
|
457 |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
|
458 |
Integer[] ints = new Integer[SIZE]; |
|
459 |
Integer[] array = q.toArray(ints); |
|
460 |
assertSame(ints, array); |
|
461 |
Arrays.sort(ints); |
|
462 |
for (int i = 0; i < ints.length; i++) |
|
463 |
assertSame(ints[i], q.poll()); |
|
464 |
} |
|
465 |
||
466 |
/** |
|
467 |
* iterator iterates through all elements |
|
468 |
*/ |
|
469 |
public void testIterator() { |
|
470 |
PriorityQueue q = populatedQueue(SIZE); |
|
471 |
Iterator it = q.iterator(); |
|
472 |
int i; |
|
473 |
for (i = 0; it.hasNext(); i++) |
|
474 |
assertTrue(q.contains(it.next())); |
|
475 |
assertEquals(i, SIZE); |
|
476 |
assertIteratorExhausted(it); |
|
477 |
} |
|
478 |
||
479 |
/** |
|
480 |
* iterator of empty collection has no elements |
|
481 |
*/ |
|
482 |
public void testEmptyIterator() { |
|
483 |
assertIteratorExhausted(new PriorityQueue().iterator()); |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* iterator.remove removes current element |
|
488 |
*/ |
|
489 |
public void testIteratorRemove() { |
|
490 |
final PriorityQueue q = new PriorityQueue(3); |
|
491 |
q.add(new Integer(2)); |
|
492 |
q.add(new Integer(1)); |
|
493 |
q.add(new Integer(3)); |
|
494 |
||
495 |
Iterator it = q.iterator(); |
|
496 |
it.next(); |
|
497 |
it.remove(); |
|
498 |
||
499 |
it = q.iterator(); |
|
500 |
assertEquals(it.next(), new Integer(2)); |
|
501 |
assertEquals(it.next(), new Integer(3)); |
|
502 |
assertFalse(it.hasNext()); |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* toString contains toStrings of elements |
|
507 |
*/ |
|
508 |
public void testToString() { |
|
509 |
PriorityQueue q = populatedQueue(SIZE); |
|
510 |
String s = q.toString(); |
|
511 |
for (int i = 0; i < SIZE; ++i) { |
|
512 |
assertTrue(s.contains(String.valueOf(i))); |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
/** |
|
517 |
* A deserialized serialized queue has same elements |
|
518 |
*/ |
|
519 |
public void testSerialization() throws Exception { |
|
520 |
Queue x = populatedQueue(SIZE); |
|
521 |
Queue y = serialClone(x); |
|
522 |
||
523 |
assertNotSame(x, y); |
|
524 |
assertEquals(x.size(), y.size()); |
|
525 |
while (!x.isEmpty()) { |
|
526 |
assertFalse(y.isEmpty()); |
|
527 |
assertEquals(x.remove(), y.remove()); |
|
528 |
} |
|
529 |
assertTrue(y.isEmpty()); |
|
530 |
} |
|
531 |
} |