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