author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
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 |
||
46 |
public class PriorityQueueTest extends JSR166TestCase { |
|
47 |
public static void main(String[] args) { |
|
48 |
main(suite(), args); |
|
49 |
} |
|
50 |
public static Test suite() { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
51 |
class Implementation implements CollectionImplementation { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
52 |
public Class<?> klazz() { return PriorityQueue.class; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
53 |
public Collection emptyCollection() { return new PriorityQueue(); } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
54 |
public Object makeElement(int i) { return i; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
55 |
public boolean isConcurrent() { return false; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
56 |
public boolean permitsNulls() { return false; } |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
57 |
} |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
58 |
return newTestSuite(PriorityQueueTest.class, |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
59 |
CollectionTest.testSuite(new Implementation())); |
35394 | 60 |
} |
61 |
||
62 |
static class MyReverseComparator implements Comparator { |
|
63 |
public int compare(Object x, Object y) { |
|
64 |
return ((Comparable)y).compareTo(x); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Returns a new queue of given size containing consecutive |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
70 |
* Integers 0 ... n - 1. |
35394 | 71 |
*/ |
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
72 |
private static PriorityQueue<Integer> populatedQueue(int n) { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
73 |
PriorityQueue<Integer> q = new PriorityQueue<>(n); |
35394 | 74 |
assertTrue(q.isEmpty()); |
75 |
for (int i = n - 1; i >= 0; i -= 2) |
|
76 |
assertTrue(q.offer(new Integer(i))); |
|
77 |
for (int i = (n & 1); i < n; i += 2) |
|
78 |
assertTrue(q.offer(new Integer(i))); |
|
79 |
assertFalse(q.isEmpty()); |
|
80 |
assertEquals(n, q.size()); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
39039
diff
changeset
|
81 |
assertEquals((Integer) 0, q.peek()); |
35394 | 82 |
return q; |
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* A new queue has unbounded capacity |
|
87 |
*/ |
|
88 |
public void testConstructor1() { |
|
89 |
assertEquals(0, new PriorityQueue(SIZE).size()); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
93 |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
35394 | 94 |
*/ |
95 |
public void testConstructor2() { |
|
96 |
try { |
|
97 |
new PriorityQueue(0); |
|
98 |
shouldThrow(); |
|
99 |
} catch (IllegalArgumentException success) {} |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Initializing from null Collection throws NPE |
|
104 |
*/ |
|
105 |
public void testConstructor3() { |
|
106 |
try { |
|
107 |
new PriorityQueue((Collection)null); |
|
108 |
shouldThrow(); |
|
109 |
} catch (NullPointerException success) {} |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Initializing from Collection of null elements throws NPE |
|
114 |
*/ |
|
115 |
public void testConstructor4() { |
|
116 |
try { |
|
117 |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
|
118 |
shouldThrow(); |
|
119 |
} catch (NullPointerException success) {} |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Initializing from Collection with some null elements throws NPE |
|
124 |
*/ |
|
125 |
public void testConstructor5() { |
|
126 |
Integer[] ints = new Integer[SIZE]; |
|
127 |
for (int i = 0; i < SIZE - 1; ++i) |
|
128 |
ints[i] = new Integer(i); |
|
129 |
try { |
|
130 |
new PriorityQueue(Arrays.asList(ints)); |
|
131 |
shouldThrow(); |
|
132 |
} catch (NullPointerException success) {} |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Queue contains all elements of collection used to initialize |
|
137 |
*/ |
|
138 |
public void testConstructor6() { |
|
139 |
Integer[] ints = new Integer[SIZE]; |
|
140 |
for (int i = 0; i < SIZE; ++i) |
|
141 |
ints[i] = new Integer(i); |
|
142 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
|
143 |
for (int i = 0; i < SIZE; ++i) |
|
144 |
assertEquals(ints[i], q.poll()); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* The comparator used in constructor is used |
|
149 |
*/ |
|
150 |
public void testConstructor7() { |
|
151 |
MyReverseComparator cmp = new MyReverseComparator(); |
|
152 |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
|
153 |
assertEquals(cmp, q.comparator()); |
|
154 |
Integer[] ints = new Integer[SIZE]; |
|
155 |
for (int i = 0; i < SIZE; ++i) |
|
156 |
ints[i] = new Integer(i); |
|
157 |
q.addAll(Arrays.asList(ints)); |
|
158 |
for (int i = SIZE - 1; i >= 0; --i) |
|
159 |
assertEquals(ints[i], q.poll()); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* isEmpty is true before add, false after |
|
164 |
*/ |
|
165 |
public void testEmpty() { |
|
166 |
PriorityQueue q = new PriorityQueue(2); |
|
167 |
assertTrue(q.isEmpty()); |
|
168 |
q.add(new Integer(1)); |
|
169 |
assertFalse(q.isEmpty()); |
|
170 |
q.add(new Integer(2)); |
|
171 |
q.remove(); |
|
172 |
q.remove(); |
|
173 |
assertTrue(q.isEmpty()); |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* size changes when elements added and removed |
|
178 |
*/ |
|
179 |
public void testSize() { |
|
180 |
PriorityQueue q = populatedQueue(SIZE); |
|
181 |
for (int i = 0; i < SIZE; ++i) { |
|
182 |
assertEquals(SIZE - i, q.size()); |
|
183 |
q.remove(); |
|
184 |
} |
|
185 |
for (int i = 0; i < SIZE; ++i) { |
|
186 |
assertEquals(i, q.size()); |
|
187 |
q.add(new Integer(i)); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* offer(null) throws NPE |
|
193 |
*/ |
|
194 |
public void testOfferNull() { |
|
195 |
PriorityQueue q = new PriorityQueue(1); |
|
196 |
try { |
|
197 |
q.offer(null); |
|
198 |
shouldThrow(); |
|
199 |
} catch (NullPointerException success) {} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* add(null) throws NPE |
|
204 |
*/ |
|
205 |
public void testAddNull() { |
|
206 |
PriorityQueue q = new PriorityQueue(1); |
|
207 |
try { |
|
208 |
q.add(null); |
|
209 |
shouldThrow(); |
|
210 |
} catch (NullPointerException success) {} |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Offer of comparable element succeeds |
|
215 |
*/ |
|
216 |
public void testOffer() { |
|
217 |
PriorityQueue q = new PriorityQueue(1); |
|
218 |
assertTrue(q.offer(zero)); |
|
219 |
assertTrue(q.offer(one)); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Offer of non-Comparable throws CCE |
|
224 |
*/ |
|
225 |
public void testOfferNonComparable() { |
|
226 |
PriorityQueue q = new PriorityQueue(1); |
|
227 |
try { |
|
228 |
q.offer(new Object()); |
|
229 |
shouldThrow(); |
|
39039
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
230 |
} catch (ClassCastException success) { |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
231 |
assertTrue(q.isEmpty()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
232 |
assertEquals(0, q.size()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
233 |
assertNull(q.poll()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
234 |
} |
35394 | 235 |
} |
236 |
||
237 |
/** |
|
238 |
* add of comparable succeeds |
|
239 |
*/ |
|
240 |
public void testAdd() { |
|
241 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
242 |
for (int i = 0; i < SIZE; ++i) { |
|
243 |
assertEquals(i, q.size()); |
|
244 |
assertTrue(q.add(new Integer(i))); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* addAll(null) throws NPE |
|
250 |
*/ |
|
251 |
public void testAddAll1() { |
|
252 |
PriorityQueue q = new PriorityQueue(1); |
|
253 |
try { |
|
254 |
q.addAll(null); |
|
255 |
shouldThrow(); |
|
256 |
} catch (NullPointerException success) {} |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* addAll of a collection with null elements throws NPE |
|
261 |
*/ |
|
262 |
public void testAddAll2() { |
|
263 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
264 |
try { |
|
265 |
q.addAll(Arrays.asList(new Integer[SIZE])); |
|
266 |
shouldThrow(); |
|
267 |
} catch (NullPointerException success) {} |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* addAll of a collection with any null elements throws NPE after |
|
272 |
* possibly adding some elements |
|
273 |
*/ |
|
274 |
public void testAddAll3() { |
|
275 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
276 |
Integer[] ints = new Integer[SIZE]; |
|
277 |
for (int i = 0; i < SIZE - 1; ++i) |
|
278 |
ints[i] = new Integer(i); |
|
279 |
try { |
|
280 |
q.addAll(Arrays.asList(ints)); |
|
281 |
shouldThrow(); |
|
282 |
} catch (NullPointerException success) {} |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Queue contains all elements of successful addAll |
|
287 |
*/ |
|
288 |
public void testAddAll5() { |
|
289 |
Integer[] empty = new Integer[0]; |
|
290 |
Integer[] ints = new Integer[SIZE]; |
|
291 |
for (int i = 0; i < SIZE; ++i) |
|
292 |
ints[i] = new Integer(SIZE - 1 - i); |
|
293 |
PriorityQueue q = new PriorityQueue(SIZE); |
|
294 |
assertFalse(q.addAll(Arrays.asList(empty))); |
|
295 |
assertTrue(q.addAll(Arrays.asList(ints))); |
|
296 |
for (int i = 0; i < SIZE; ++i) |
|
297 |
assertEquals(new Integer(i), q.poll()); |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* poll succeeds unless empty |
|
302 |
*/ |
|
303 |
public void testPoll() { |
|
304 |
PriorityQueue q = populatedQueue(SIZE); |
|
305 |
for (int i = 0; i < SIZE; ++i) { |
|
306 |
assertEquals(i, q.poll()); |
|
307 |
} |
|
308 |
assertNull(q.poll()); |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* peek returns next element, or null if empty |
|
313 |
*/ |
|
314 |
public void testPeek() { |
|
315 |
PriorityQueue q = populatedQueue(SIZE); |
|
316 |
for (int i = 0; i < SIZE; ++i) { |
|
317 |
assertEquals(i, q.peek()); |
|
318 |
assertEquals(i, q.poll()); |
|
319 |
assertTrue(q.peek() == null || |
|
320 |
!q.peek().equals(i)); |
|
321 |
} |
|
322 |
assertNull(q.peek()); |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* element returns next element, or throws NSEE if empty |
|
327 |
*/ |
|
328 |
public void testElement() { |
|
329 |
PriorityQueue q = populatedQueue(SIZE); |
|
330 |
for (int i = 0; i < SIZE; ++i) { |
|
331 |
assertEquals(i, q.element()); |
|
332 |
assertEquals(i, q.poll()); |
|
333 |
} |
|
334 |
try { |
|
335 |
q.element(); |
|
336 |
shouldThrow(); |
|
337 |
} catch (NoSuchElementException success) {} |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* remove removes next element, or throws NSEE if empty |
|
342 |
*/ |
|
343 |
public void testRemove() { |
|
344 |
PriorityQueue q = populatedQueue(SIZE); |
|
345 |
for (int i = 0; i < SIZE; ++i) { |
|
346 |
assertEquals(i, q.remove()); |
|
347 |
} |
|
348 |
try { |
|
349 |
q.remove(); |
|
350 |
shouldThrow(); |
|
351 |
} catch (NoSuchElementException success) {} |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* remove(x) removes x and returns true if present |
|
356 |
*/ |
|
357 |
public void testRemoveElement() { |
|
358 |
PriorityQueue q = populatedQueue(SIZE); |
|
359 |
for (int i = 1; i < SIZE; i += 2) { |
|
360 |
assertTrue(q.contains(i)); |
|
361 |
assertTrue(q.remove(i)); |
|
362 |
assertFalse(q.contains(i)); |
|
363 |
assertTrue(q.contains(i - 1)); |
|
364 |
} |
|
365 |
for (int i = 0; i < SIZE; i += 2) { |
|
366 |
assertTrue(q.contains(i)); |
|
367 |
assertTrue(q.remove(i)); |
|
368 |
assertFalse(q.contains(i)); |
|
369 |
assertFalse(q.remove(i + 1)); |
|
370 |
assertFalse(q.contains(i + 1)); |
|
371 |
} |
|
372 |
assertTrue(q.isEmpty()); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* contains(x) reports true when elements added but not yet removed |
|
377 |
*/ |
|
378 |
public void testContains() { |
|
379 |
PriorityQueue q = populatedQueue(SIZE); |
|
380 |
for (int i = 0; i < SIZE; ++i) { |
|
381 |
assertTrue(q.contains(new Integer(i))); |
|
382 |
q.poll(); |
|
383 |
assertFalse(q.contains(new Integer(i))); |
|
384 |
} |
|
385 |
} |
|
386 |
||
387 |
/** |
|
388 |
* clear removes all elements |
|
389 |
*/ |
|
390 |
public void testClear() { |
|
391 |
PriorityQueue q = populatedQueue(SIZE); |
|
392 |
q.clear(); |
|
393 |
assertTrue(q.isEmpty()); |
|
394 |
assertEquals(0, q.size()); |
|
395 |
q.add(new Integer(1)); |
|
396 |
assertFalse(q.isEmpty()); |
|
397 |
q.clear(); |
|
398 |
assertTrue(q.isEmpty()); |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* containsAll(c) is true when c contains a subset of elements |
|
403 |
*/ |
|
404 |
public void testContainsAll() { |
|
405 |
PriorityQueue q = populatedQueue(SIZE); |
|
406 |
PriorityQueue p = new PriorityQueue(SIZE); |
|
407 |
for (int i = 0; i < SIZE; ++i) { |
|
408 |
assertTrue(q.containsAll(p)); |
|
409 |
assertFalse(p.containsAll(q)); |
|
410 |
p.add(new Integer(i)); |
|
411 |
} |
|
412 |
assertTrue(p.containsAll(q)); |
|
413 |
} |
|
414 |
||
415 |
/** |
|
416 |
* retainAll(c) retains only those elements of c and reports true if changed |
|
417 |
*/ |
|
418 |
public void testRetainAll() { |
|
419 |
PriorityQueue q = populatedQueue(SIZE); |
|
420 |
PriorityQueue p = populatedQueue(SIZE); |
|
421 |
for (int i = 0; i < SIZE; ++i) { |
|
422 |
boolean changed = q.retainAll(p); |
|
423 |
if (i == 0) |
|
424 |
assertFalse(changed); |
|
425 |
else |
|
426 |
assertTrue(changed); |
|
427 |
||
428 |
assertTrue(q.containsAll(p)); |
|
429 |
assertEquals(SIZE - i, q.size()); |
|
430 |
p.remove(); |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
/** |
|
435 |
* removeAll(c) removes only those elements of c and reports true if changed |
|
436 |
*/ |
|
437 |
public void testRemoveAll() { |
|
438 |
for (int i = 1; i < SIZE; ++i) { |
|
439 |
PriorityQueue q = populatedQueue(SIZE); |
|
440 |
PriorityQueue p = populatedQueue(i); |
|
441 |
assertTrue(q.removeAll(p)); |
|
442 |
assertEquals(SIZE - i, q.size()); |
|
443 |
for (int j = 0; j < i; ++j) { |
|
444 |
Integer x = (Integer)(p.remove()); |
|
445 |
assertFalse(q.contains(x)); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* toArray contains all elements |
|
452 |
*/ |
|
453 |
public void testToArray() { |
|
454 |
PriorityQueue q = populatedQueue(SIZE); |
|
455 |
Object[] o = q.toArray(); |
|
456 |
Arrays.sort(o); |
|
457 |
for (int i = 0; i < o.length; i++) |
|
458 |
assertSame(o[i], q.poll()); |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* toArray(a) contains all elements |
|
463 |
*/ |
|
464 |
public void testToArray2() { |
|
465 |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
|
466 |
Integer[] ints = new Integer[SIZE]; |
|
467 |
Integer[] array = q.toArray(ints); |
|
468 |
assertSame(ints, array); |
|
469 |
Arrays.sort(ints); |
|
470 |
for (int i = 0; i < ints.length; i++) |
|
471 |
assertSame(ints[i], q.poll()); |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* iterator iterates through all elements |
|
476 |
*/ |
|
477 |
public void testIterator() { |
|
478 |
PriorityQueue q = populatedQueue(SIZE); |
|
479 |
Iterator it = q.iterator(); |
|
480 |
int i; |
|
481 |
for (i = 0; it.hasNext(); i++) |
|
482 |
assertTrue(q.contains(it.next())); |
|
483 |
assertEquals(i, SIZE); |
|
484 |
assertIteratorExhausted(it); |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* iterator of empty collection has no elements |
|
489 |
*/ |
|
490 |
public void testEmptyIterator() { |
|
491 |
assertIteratorExhausted(new PriorityQueue().iterator()); |
|
492 |
} |
|
493 |
||
494 |
/** |
|
495 |
* iterator.remove removes current element |
|
496 |
*/ |
|
497 |
public void testIteratorRemove() { |
|
498 |
final PriorityQueue q = new PriorityQueue(3); |
|
499 |
q.add(new Integer(2)); |
|
500 |
q.add(new Integer(1)); |
|
501 |
q.add(new Integer(3)); |
|
502 |
||
503 |
Iterator it = q.iterator(); |
|
504 |
it.next(); |
|
505 |
it.remove(); |
|
506 |
||
507 |
it = q.iterator(); |
|
508 |
assertEquals(it.next(), new Integer(2)); |
|
509 |
assertEquals(it.next(), new Integer(3)); |
|
510 |
assertFalse(it.hasNext()); |
|
511 |
} |
|
512 |
||
513 |
/** |
|
514 |
* toString contains toStrings of elements |
|
515 |
*/ |
|
516 |
public void testToString() { |
|
517 |
PriorityQueue q = populatedQueue(SIZE); |
|
518 |
String s = q.toString(); |
|
519 |
for (int i = 0; i < SIZE; ++i) { |
|
520 |
assertTrue(s.contains(String.valueOf(i))); |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
/** |
|
46146
b3e220a04d3f
8185099: Miscellaneous changes imported from jsr166 CVS 2017-08
dl
parents:
45937
diff
changeset
|
525 |
* A deserialized/reserialized queue has same elements |
35394 | 526 |
*/ |
527 |
public void testSerialization() throws Exception { |
|
528 |
Queue x = populatedQueue(SIZE); |
|
529 |
Queue y = serialClone(x); |
|
530 |
||
531 |
assertNotSame(x, y); |
|
532 |
assertEquals(x.size(), y.size()); |
|
533 |
while (!x.isEmpty()) { |
|
534 |
assertFalse(y.isEmpty()); |
|
535 |
assertEquals(x.remove(), y.remove()); |
|
536 |
} |
|
537 |
assertTrue(y.isEmpty()); |
|
538 |
} |
|
539 |
} |