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