1 |
dl |
1.1 |
/* |
2 |
dl |
1.8 |
* 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 |
jsr166 |
1.9 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.*; |
11 |
|
|
import java.util.concurrent.*; |
12 |
dl |
1.2 |
import java.io.*; |
13 |
dl |
1.1 |
|
14 |
dl |
1.3 |
public class PriorityQueueTest extends JSR166TestCase { |
15 |
dl |
1.1 |
public static void main(String[] args) { |
16 |
jsr166 |
1.9 |
junit.textui.TestRunner.run (suite()); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
|
|
return new TestSuite(PriorityQueueTest.class); |
20 |
|
|
} |
21 |
|
|
|
22 |
jsr166 |
1.9 |
static class MyReverseComparator implements Comparator { |
23 |
dl |
1.1 |
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; |
29 |
|
|
} |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Create a queue of given size containing consecutive |
34 |
|
|
* Integers 0 ... n. |
35 |
|
|
*/ |
36 |
dl |
1.3 |
private PriorityQueue populatedQueue(int n) { |
37 |
dl |
1.1 |
PriorityQueue q = new PriorityQueue(n); |
38 |
|
|
assertTrue(q.isEmpty()); |
39 |
jsr166 |
1.10 |
for (int i = n-1; i >= 0; i-=2) |
40 |
dl |
1.1 |
assertTrue(q.offer(new Integer(i))); |
41 |
jsr166 |
1.10 |
for (int i = (n & 1); i < n; i+=2) |
42 |
dl |
1.1 |
assertTrue(q.offer(new Integer(i))); |
43 |
|
|
assertFalse(q.isEmpty()); |
44 |
|
|
assertEquals(n, q.size()); |
45 |
|
|
return q; |
46 |
|
|
} |
47 |
jsr166 |
1.9 |
|
48 |
dl |
1.5 |
/** |
49 |
dl |
1.6 |
* A new queue has unbounded capacity |
50 |
dl |
1.5 |
*/ |
51 |
|
|
public void testConstructor1() { |
52 |
dl |
1.3 |
assertEquals(0, new PriorityQueue(SIZE).size()); |
53 |
dl |
1.1 |
} |
54 |
|
|
|
55 |
dl |
1.5 |
/** |
56 |
dl |
1.6 |
* Constructor throws IAE if capacity argument nonpositive |
57 |
dl |
1.5 |
*/ |
58 |
|
|
public void testConstructor2() { |
59 |
dl |
1.1 |
try { |
60 |
|
|
PriorityQueue q = new PriorityQueue(0); |
61 |
dl |
1.5 |
shouldThrow(); |
62 |
dl |
1.1 |
} |
63 |
|
|
catch (IllegalArgumentException success) {} |
64 |
|
|
} |
65 |
|
|
|
66 |
dl |
1.5 |
/** |
67 |
dl |
1.6 |
* Initializing from null Collection throws NPE |
68 |
dl |
1.5 |
*/ |
69 |
dl |
1.1 |
public void testConstructor3() { |
70 |
|
|
try { |
71 |
|
|
PriorityQueue q = new PriorityQueue((Collection)null); |
72 |
dl |
1.5 |
shouldThrow(); |
73 |
dl |
1.1 |
} |
74 |
|
|
catch (NullPointerException success) {} |
75 |
|
|
} |
76 |
|
|
|
77 |
dl |
1.5 |
/** |
78 |
dl |
1.6 |
* Initializing from Collection of null elements throws NPE |
79 |
dl |
1.5 |
*/ |
80 |
|
|
public void testConstructor4() { |
81 |
dl |
1.1 |
try { |
82 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
83 |
dl |
1.1 |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
84 |
dl |
1.5 |
shouldThrow(); |
85 |
dl |
1.1 |
} |
86 |
|
|
catch (NullPointerException success) {} |
87 |
|
|
} |
88 |
|
|
|
89 |
dl |
1.5 |
/** |
90 |
dl |
1.6 |
* Initializing from Collection with some null elements throws NPE |
91 |
dl |
1.5 |
*/ |
92 |
|
|
public void testConstructor5() { |
93 |
dl |
1.1 |
try { |
94 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
95 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
96 |
dl |
1.1 |
ints[i] = new Integer(i); |
97 |
|
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
98 |
dl |
1.5 |
shouldThrow(); |
99 |
dl |
1.1 |
} |
100 |
|
|
catch (NullPointerException success) {} |
101 |
|
|
} |
102 |
|
|
|
103 |
dl |
1.5 |
/** |
104 |
dl |
1.6 |
* Queue contains all elements of collection used to initialize |
105 |
dl |
1.5 |
*/ |
106 |
|
|
public void testConstructor6() { |
107 |
dl |
1.1 |
try { |
108 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
109 |
|
|
for (int i = 0; i < SIZE; ++i) |
110 |
dl |
1.1 |
ints[i] = new Integer(i); |
111 |
|
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
112 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
113 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
114 |
|
|
} |
115 |
|
|
finally {} |
116 |
|
|
} |
117 |
|
|
|
118 |
dl |
1.5 |
/** |
119 |
dl |
1.6 |
* The comparator used in constructor is used |
120 |
dl |
1.5 |
*/ |
121 |
|
|
public void testConstructor7() { |
122 |
dl |
1.1 |
try { |
123 |
dl |
1.4 |
MyReverseComparator cmp = new MyReverseComparator(); |
124 |
|
|
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
125 |
|
|
assertEquals(cmp, q.comparator()); |
126 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
127 |
|
|
for (int i = 0; i < SIZE; ++i) |
128 |
dl |
1.1 |
ints[i] = new Integer(i); |
129 |
|
|
q.addAll(Arrays.asList(ints)); |
130 |
dl |
1.3 |
for (int i = SIZE-1; i >= 0; --i) |
131 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
132 |
|
|
} |
133 |
|
|
finally {} |
134 |
|
|
} |
135 |
|
|
|
136 |
dl |
1.5 |
/** |
137 |
dl |
1.6 |
* isEmpty is true before add, false after |
138 |
dl |
1.5 |
*/ |
139 |
dl |
1.1 |
public void testEmpty() { |
140 |
|
|
PriorityQueue q = new PriorityQueue(2); |
141 |
|
|
assertTrue(q.isEmpty()); |
142 |
|
|
q.add(new Integer(1)); |
143 |
|
|
assertFalse(q.isEmpty()); |
144 |
|
|
q.add(new Integer(2)); |
145 |
|
|
q.remove(); |
146 |
|
|
q.remove(); |
147 |
|
|
assertTrue(q.isEmpty()); |
148 |
|
|
} |
149 |
|
|
|
150 |
dl |
1.5 |
/** |
151 |
dl |
1.6 |
* size changes when elements added and removed |
152 |
dl |
1.5 |
*/ |
153 |
dl |
1.1 |
public void testSize() { |
154 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
155 |
|
|
for (int i = 0; i < SIZE; ++i) { |
156 |
|
|
assertEquals(SIZE-i, q.size()); |
157 |
dl |
1.1 |
q.remove(); |
158 |
|
|
} |
159 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
160 |
dl |
1.1 |
assertEquals(i, q.size()); |
161 |
|
|
q.add(new Integer(i)); |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
|
165 |
dl |
1.5 |
/** |
166 |
dl |
1.6 |
* offer(null) throws NPE |
167 |
dl |
1.5 |
*/ |
168 |
|
|
public void testOfferNull() { |
169 |
dl |
1.1 |
try { |
170 |
|
|
PriorityQueue q = new PriorityQueue(1); |
171 |
|
|
q.offer(null); |
172 |
dl |
1.5 |
shouldThrow(); |
173 |
jsr166 |
1.9 |
} catch (NullPointerException success) { } |
174 |
dl |
1.1 |
} |
175 |
|
|
|
176 |
dl |
1.5 |
/** |
177 |
dl |
1.7 |
* add(null) throws NPE |
178 |
|
|
*/ |
179 |
|
|
public void testAddNull() { |
180 |
|
|
try { |
181 |
|
|
PriorityQueue q = new PriorityQueue(1); |
182 |
|
|
q.add(null); |
183 |
|
|
shouldThrow(); |
184 |
jsr166 |
1.9 |
} catch (NullPointerException success) { } |
185 |
dl |
1.7 |
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
dl |
1.6 |
* Offer of comparable element succeeds |
189 |
dl |
1.5 |
*/ |
190 |
dl |
1.1 |
public void testOffer() { |
191 |
|
|
PriorityQueue q = new PriorityQueue(1); |
192 |
dl |
1.6 |
assertTrue(q.offer(zero)); |
193 |
|
|
assertTrue(q.offer(one)); |
194 |
dl |
1.1 |
} |
195 |
|
|
|
196 |
dl |
1.5 |
/** |
197 |
dl |
1.6 |
* Offer of non-Comparable throws CCE |
198 |
dl |
1.5 |
*/ |
199 |
dl |
1.1 |
public void testOfferNonComparable() { |
200 |
|
|
try { |
201 |
|
|
PriorityQueue q = new PriorityQueue(1); |
202 |
|
|
q.offer(new Object()); |
203 |
|
|
q.offer(new Object()); |
204 |
|
|
q.offer(new Object()); |
205 |
dl |
1.5 |
shouldThrow(); |
206 |
dl |
1.1 |
} |
207 |
jsr166 |
1.10 |
catch (ClassCastException success) {} |
208 |
dl |
1.1 |
} |
209 |
|
|
|
210 |
dl |
1.5 |
/** |
211 |
dl |
1.6 |
* add of comparable succeeds |
212 |
dl |
1.5 |
*/ |
213 |
|
|
public void testAdd() { |
214 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
215 |
|
|
for (int i = 0; i < SIZE; ++i) { |
216 |
dl |
1.1 |
assertEquals(i, q.size()); |
217 |
|
|
assertTrue(q.add(new Integer(i))); |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
dl |
1.5 |
/** |
222 |
dl |
1.6 |
* addAll(null) throws NPE |
223 |
dl |
1.5 |
*/ |
224 |
|
|
public void testAddAll1() { |
225 |
dl |
1.1 |
try { |
226 |
|
|
PriorityQueue q = new PriorityQueue(1); |
227 |
|
|
q.addAll(null); |
228 |
dl |
1.5 |
shouldThrow(); |
229 |
dl |
1.1 |
} |
230 |
|
|
catch (NullPointerException success) {} |
231 |
|
|
} |
232 |
dl |
1.5 |
/** |
233 |
dl |
1.6 |
* addAll of a collection with null elements throws NPE |
234 |
dl |
1.5 |
*/ |
235 |
|
|
public void testAddAll2() { |
236 |
dl |
1.1 |
try { |
237 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
238 |
|
|
Integer[] ints = new Integer[SIZE]; |
239 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
240 |
dl |
1.5 |
shouldThrow(); |
241 |
dl |
1.1 |
} |
242 |
|
|
catch (NullPointerException success) {} |
243 |
|
|
} |
244 |
dl |
1.5 |
/** |
245 |
dl |
1.6 |
* addAll of a collection with any null elements throws NPE after |
246 |
|
|
* possibly adding some elements |
247 |
dl |
1.5 |
*/ |
248 |
|
|
public void testAddAll3() { |
249 |
dl |
1.1 |
try { |
250 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
251 |
|
|
Integer[] ints = new Integer[SIZE]; |
252 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
253 |
dl |
1.1 |
ints[i] = new Integer(i); |
254 |
|
|
q.addAll(Arrays.asList(ints)); |
255 |
dl |
1.5 |
shouldThrow(); |
256 |
dl |
1.1 |
} |
257 |
|
|
catch (NullPointerException success) {} |
258 |
|
|
} |
259 |
|
|
|
260 |
dl |
1.5 |
/** |
261 |
dl |
1.6 |
* Queue contains all elements of successful addAll |
262 |
dl |
1.5 |
*/ |
263 |
|
|
public void testAddAll5() { |
264 |
dl |
1.1 |
try { |
265 |
|
|
Integer[] empty = new Integer[0]; |
266 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
267 |
|
|
for (int i = 0; i < SIZE; ++i) |
268 |
|
|
ints[i] = new Integer(SIZE-1-i); |
269 |
|
|
PriorityQueue q = new PriorityQueue(SIZE); |
270 |
dl |
1.1 |
assertFalse(q.addAll(Arrays.asList(empty))); |
271 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
272 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
273 |
dl |
1.1 |
assertEquals(new Integer(i), q.poll()); |
274 |
|
|
} |
275 |
|
|
finally {} |
276 |
|
|
} |
277 |
|
|
|
278 |
dl |
1.5 |
/** |
279 |
dl |
1.6 |
* poll succeeds unless empty |
280 |
dl |
1.5 |
*/ |
281 |
|
|
public void testPoll() { |
282 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
283 |
|
|
for (int i = 0; i < SIZE; ++i) { |
284 |
dl |
1.1 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
285 |
|
|
} |
286 |
|
|
assertNull(q.poll()); |
287 |
|
|
} |
288 |
|
|
|
289 |
dl |
1.5 |
/** |
290 |
dl |
1.6 |
* peek returns next element, or null if empty |
291 |
dl |
1.5 |
*/ |
292 |
|
|
public void testPeek() { |
293 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
294 |
|
|
for (int i = 0; i < SIZE; ++i) { |
295 |
dl |
1.1 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
296 |
|
|
q.poll(); |
297 |
|
|
assertTrue(q.peek() == null || |
298 |
|
|
i != ((Integer)q.peek()).intValue()); |
299 |
|
|
} |
300 |
|
|
assertNull(q.peek()); |
301 |
|
|
} |
302 |
|
|
|
303 |
dl |
1.5 |
/** |
304 |
dl |
1.6 |
* element returns next element, or throws NSEE if empty |
305 |
dl |
1.5 |
*/ |
306 |
|
|
public void testElement() { |
307 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
308 |
|
|
for (int i = 0; i < SIZE; ++i) { |
309 |
dl |
1.1 |
assertEquals(i, ((Integer)q.element()).intValue()); |
310 |
|
|
q.poll(); |
311 |
|
|
} |
312 |
|
|
try { |
313 |
|
|
q.element(); |
314 |
dl |
1.5 |
shouldThrow(); |
315 |
dl |
1.1 |
} |
316 |
|
|
catch (NoSuchElementException success) {} |
317 |
|
|
} |
318 |
|
|
|
319 |
dl |
1.5 |
/** |
320 |
dl |
1.6 |
* remove removes next element, or throws NSEE if empty |
321 |
dl |
1.5 |
*/ |
322 |
|
|
public void testRemove() { |
323 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
324 |
|
|
for (int i = 0; i < SIZE; ++i) { |
325 |
dl |
1.1 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
326 |
|
|
} |
327 |
|
|
try { |
328 |
|
|
q.remove(); |
329 |
dl |
1.5 |
shouldThrow(); |
330 |
dl |
1.1 |
} catch (NoSuchElementException success){ |
331 |
jsr166 |
1.9 |
} |
332 |
dl |
1.1 |
} |
333 |
|
|
|
334 |
dl |
1.5 |
/** |
335 |
dl |
1.6 |
* remove(x) removes x and returns true if present |
336 |
dl |
1.5 |
*/ |
337 |
|
|
public void testRemoveElement() { |
338 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
339 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
340 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
341 |
|
|
} |
342 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
343 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
344 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
345 |
|
|
} |
346 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
347 |
dl |
1.1 |
} |
348 |
jsr166 |
1.9 |
|
349 |
dl |
1.5 |
/** |
350 |
dl |
1.6 |
* contains(x) reports true when elements added but not yet removed |
351 |
dl |
1.5 |
*/ |
352 |
|
|
public void testContains() { |
353 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
354 |
|
|
for (int i = 0; i < SIZE; ++i) { |
355 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
356 |
|
|
q.poll(); |
357 |
|
|
assertFalse(q.contains(new Integer(i))); |
358 |
|
|
} |
359 |
|
|
} |
360 |
|
|
|
361 |
dl |
1.5 |
/** |
362 |
dl |
1.6 |
* clear removes all elements |
363 |
dl |
1.5 |
*/ |
364 |
|
|
public void testClear() { |
365 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
366 |
dl |
1.1 |
q.clear(); |
367 |
|
|
assertTrue(q.isEmpty()); |
368 |
|
|
assertEquals(0, q.size()); |
369 |
|
|
q.add(new Integer(1)); |
370 |
|
|
assertFalse(q.isEmpty()); |
371 |
|
|
q.clear(); |
372 |
|
|
assertTrue(q.isEmpty()); |
373 |
|
|
} |
374 |
|
|
|
375 |
dl |
1.5 |
/** |
376 |
dl |
1.6 |
* containsAll(c) is true when c contains a subset of elements |
377 |
dl |
1.5 |
*/ |
378 |
|
|
public void testContainsAll() { |
379 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
380 |
|
|
PriorityQueue p = new PriorityQueue(SIZE); |
381 |
|
|
for (int i = 0; i < SIZE; ++i) { |
382 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
383 |
|
|
assertFalse(p.containsAll(q)); |
384 |
|
|
p.add(new Integer(i)); |
385 |
|
|
} |
386 |
|
|
assertTrue(p.containsAll(q)); |
387 |
|
|
} |
388 |
|
|
|
389 |
dl |
1.5 |
/** |
390 |
dl |
1.6 |
* retainAll(c) retains only those elements of c and reports true if changed |
391 |
dl |
1.5 |
*/ |
392 |
|
|
public void testRetainAll() { |
393 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
394 |
|
|
PriorityQueue p = populatedQueue(SIZE); |
395 |
|
|
for (int i = 0; i < SIZE; ++i) { |
396 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
397 |
|
|
if (i == 0) |
398 |
|
|
assertFalse(changed); |
399 |
|
|
else |
400 |
|
|
assertTrue(changed); |
401 |
|
|
|
402 |
|
|
assertTrue(q.containsAll(p)); |
403 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
404 |
dl |
1.1 |
p.remove(); |
405 |
|
|
} |
406 |
|
|
} |
407 |
|
|
|
408 |
dl |
1.5 |
/** |
409 |
dl |
1.6 |
* removeAll(c) removes only those elements of c and reports true if changed |
410 |
dl |
1.5 |
*/ |
411 |
|
|
public void testRemoveAll() { |
412 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
413 |
|
|
PriorityQueue q = populatedQueue(SIZE); |
414 |
|
|
PriorityQueue p = populatedQueue(i); |
415 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
416 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
417 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
418 |
|
|
Integer I = (Integer)(p.remove()); |
419 |
|
|
assertFalse(q.contains(I)); |
420 |
|
|
} |
421 |
|
|
} |
422 |
|
|
} |
423 |
|
|
|
424 |
dl |
1.5 |
/** |
425 |
dl |
1.6 |
* toArray contains all elements |
426 |
dl |
1.5 |
*/ |
427 |
|
|
public void testToArray() { |
428 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
429 |
dl |
1.1 |
Object[] o = q.toArray(); |
430 |
|
|
Arrays.sort(o); |
431 |
jsr166 |
1.10 |
for (int i = 0; i < o.length; i++) |
432 |
dl |
1.1 |
assertEquals(o[i], q.poll()); |
433 |
|
|
} |
434 |
|
|
|
435 |
dl |
1.5 |
/** |
436 |
dl |
1.6 |
* toArray(a) contains all elements |
437 |
dl |
1.5 |
*/ |
438 |
|
|
public void testToArray2() { |
439 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
440 |
|
|
Integer[] ints = new Integer[SIZE]; |
441 |
dl |
1.1 |
ints = (Integer[])q.toArray(ints); |
442 |
|
|
Arrays.sort(ints); |
443 |
jsr166 |
1.10 |
for (int i = 0; i < ints.length; i++) |
444 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
445 |
|
|
} |
446 |
jsr166 |
1.9 |
|
447 |
dl |
1.5 |
/** |
448 |
dl |
1.6 |
* iterator iterates through all elements |
449 |
dl |
1.5 |
*/ |
450 |
|
|
public void testIterator() { |
451 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
452 |
dl |
1.1 |
int i = 0; |
453 |
|
|
Iterator it = q.iterator(); |
454 |
jsr166 |
1.10 |
while (it.hasNext()) { |
455 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
456 |
|
|
++i; |
457 |
|
|
} |
458 |
dl |
1.3 |
assertEquals(i, SIZE); |
459 |
dl |
1.1 |
} |
460 |
|
|
|
461 |
dl |
1.5 |
/** |
462 |
dl |
1.6 |
* iterator.remove removes current element |
463 |
dl |
1.5 |
*/ |
464 |
dl |
1.1 |
public void testIteratorRemove () { |
465 |
|
|
final PriorityQueue q = new PriorityQueue(3); |
466 |
|
|
q.add(new Integer(2)); |
467 |
|
|
q.add(new Integer(1)); |
468 |
|
|
q.add(new Integer(3)); |
469 |
|
|
|
470 |
|
|
Iterator it = q.iterator(); |
471 |
|
|
it.next(); |
472 |
|
|
it.remove(); |
473 |
|
|
|
474 |
|
|
it = q.iterator(); |
475 |
|
|
assertEquals(it.next(), new Integer(2)); |
476 |
|
|
assertEquals(it.next(), new Integer(3)); |
477 |
|
|
assertFalse(it.hasNext()); |
478 |
|
|
} |
479 |
|
|
|
480 |
|
|
|
481 |
dl |
1.5 |
/** |
482 |
dl |
1.6 |
* toString contains toStrings of elements |
483 |
dl |
1.5 |
*/ |
484 |
|
|
public void testToString() { |
485 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
486 |
dl |
1.1 |
String s = q.toString(); |
487 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
488 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
489 |
|
|
} |
490 |
jsr166 |
1.9 |
} |
491 |
dl |
1.1 |
|
492 |
dl |
1.5 |
/** |
493 |
jsr166 |
1.9 |
* A deserialized serialized queue has same elements |
494 |
dl |
1.5 |
*/ |
495 |
dl |
1.2 |
public void testSerialization() { |
496 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
497 |
dl |
1.2 |
try { |
498 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
499 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
500 |
|
|
out.writeObject(q); |
501 |
|
|
out.close(); |
502 |
|
|
|
503 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
504 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
505 |
|
|
PriorityQueue r = (PriorityQueue)in.readObject(); |
506 |
|
|
assertEquals(q.size(), r.size()); |
507 |
jsr166 |
1.9 |
while (!q.isEmpty()) |
508 |
dl |
1.2 |
assertEquals(q.remove(), r.remove()); |
509 |
jsr166 |
1.10 |
} catch (Exception e){ |
510 |
dl |
1.5 |
unexpectedException(); |
511 |
dl |
1.2 |
} |
512 |
|
|
} |
513 |
dl |
1.1 |
} |