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 |
jsr166 |
1.21 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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 |
jsr166 |
1.23 |
import java.util.Arrays; |
11 |
|
|
import java.util.Collection; |
12 |
|
|
import java.util.Comparator; |
13 |
|
|
import java.util.Iterator; |
14 |
|
|
import java.util.NoSuchElementException; |
15 |
|
|
import java.util.PriorityQueue; |
16 |
|
|
import java.util.Queue; |
17 |
dl |
1.1 |
|
18 |
dl |
1.3 |
public class PriorityQueueTest extends JSR166TestCase { |
19 |
dl |
1.1 |
public static void main(String[] args) { |
20 |
jsr166 |
1.16 |
junit.textui.TestRunner.run(suite()); |
21 |
dl |
1.1 |
} |
22 |
|
|
public static Test suite() { |
23 |
jsr166 |
1.12 |
return new TestSuite(PriorityQueueTest.class); |
24 |
dl |
1.1 |
} |
25 |
|
|
|
26 |
jsr166 |
1.9 |
static class MyReverseComparator implements Comparator { |
27 |
dl |
1.1 |
public int compare(Object x, Object y) { |
28 |
jsr166 |
1.15 |
return ((Comparable)y).compareTo(x); |
29 |
dl |
1.1 |
} |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
/** |
33 |
jsr166 |
1.25 |
* Returns a new queue of given size containing consecutive |
34 |
dl |
1.1 |
* Integers 0 ... n. |
35 |
|
|
*/ |
36 |
jsr166 |
1.19 |
private PriorityQueue<Integer> populatedQueue(int n) { |
37 |
|
|
PriorityQueue<Integer> q = new PriorityQueue<Integer>(n); |
38 |
dl |
1.1 |
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 |
jsr166 |
1.14 |
Integer[] ints = new Integer[SIZE]; |
104 |
|
|
for (int i = 0; i < SIZE; ++i) |
105 |
|
|
ints[i] = new Integer(i); |
106 |
|
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
107 |
|
|
for (int i = 0; i < SIZE; ++i) |
108 |
|
|
assertEquals(ints[i], q.poll()); |
109 |
dl |
1.1 |
} |
110 |
|
|
|
111 |
dl |
1.5 |
/** |
112 |
dl |
1.6 |
* The comparator used in constructor is used |
113 |
dl |
1.5 |
*/ |
114 |
|
|
public void testConstructor7() { |
115 |
jsr166 |
1.14 |
MyReverseComparator cmp = new MyReverseComparator(); |
116 |
|
|
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
117 |
|
|
assertEquals(cmp, q.comparator()); |
118 |
|
|
Integer[] ints = new Integer[SIZE]; |
119 |
|
|
for (int i = 0; i < SIZE; ++i) |
120 |
|
|
ints[i] = new Integer(i); |
121 |
|
|
q.addAll(Arrays.asList(ints)); |
122 |
|
|
for (int i = SIZE-1; i >= 0; --i) |
123 |
|
|
assertEquals(ints[i], q.poll()); |
124 |
dl |
1.1 |
} |
125 |
|
|
|
126 |
dl |
1.5 |
/** |
127 |
dl |
1.6 |
* isEmpty is true before add, false after |
128 |
dl |
1.5 |
*/ |
129 |
dl |
1.1 |
public void testEmpty() { |
130 |
|
|
PriorityQueue q = new PriorityQueue(2); |
131 |
|
|
assertTrue(q.isEmpty()); |
132 |
|
|
q.add(new Integer(1)); |
133 |
|
|
assertFalse(q.isEmpty()); |
134 |
|
|
q.add(new Integer(2)); |
135 |
|
|
q.remove(); |
136 |
|
|
q.remove(); |
137 |
|
|
assertTrue(q.isEmpty()); |
138 |
|
|
} |
139 |
|
|
|
140 |
dl |
1.5 |
/** |
141 |
dl |
1.6 |
* size changes when elements added and removed |
142 |
dl |
1.5 |
*/ |
143 |
dl |
1.1 |
public void testSize() { |
144 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
145 |
|
|
for (int i = 0; i < SIZE; ++i) { |
146 |
|
|
assertEquals(SIZE-i, q.size()); |
147 |
dl |
1.1 |
q.remove(); |
148 |
|
|
} |
149 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
150 |
dl |
1.1 |
assertEquals(i, q.size()); |
151 |
|
|
q.add(new Integer(i)); |
152 |
|
|
} |
153 |
|
|
} |
154 |
|
|
|
155 |
dl |
1.5 |
/** |
156 |
dl |
1.6 |
* offer(null) throws NPE |
157 |
dl |
1.5 |
*/ |
158 |
|
|
public void testOfferNull() { |
159 |
jsr166 |
1.12 |
try { |
160 |
dl |
1.1 |
PriorityQueue q = new PriorityQueue(1); |
161 |
|
|
q.offer(null); |
162 |
dl |
1.5 |
shouldThrow(); |
163 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
164 |
dl |
1.1 |
} |
165 |
|
|
|
166 |
dl |
1.5 |
/** |
167 |
dl |
1.7 |
* add(null) throws NPE |
168 |
|
|
*/ |
169 |
|
|
public void testAddNull() { |
170 |
jsr166 |
1.12 |
try { |
171 |
dl |
1.7 |
PriorityQueue q = new PriorityQueue(1); |
172 |
|
|
q.add(null); |
173 |
|
|
shouldThrow(); |
174 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
175 |
dl |
1.7 |
} |
176 |
|
|
|
177 |
|
|
/** |
178 |
dl |
1.6 |
* Offer of comparable element succeeds |
179 |
dl |
1.5 |
*/ |
180 |
dl |
1.1 |
public void testOffer() { |
181 |
|
|
PriorityQueue q = new PriorityQueue(1); |
182 |
dl |
1.6 |
assertTrue(q.offer(zero)); |
183 |
|
|
assertTrue(q.offer(one)); |
184 |
dl |
1.1 |
} |
185 |
|
|
|
186 |
dl |
1.5 |
/** |
187 |
dl |
1.6 |
* Offer of non-Comparable throws CCE |
188 |
dl |
1.5 |
*/ |
189 |
dl |
1.1 |
public void testOfferNonComparable() { |
190 |
|
|
try { |
191 |
|
|
PriorityQueue q = new PriorityQueue(1); |
192 |
|
|
q.offer(new Object()); |
193 |
|
|
q.offer(new Object()); |
194 |
|
|
q.offer(new Object()); |
195 |
dl |
1.5 |
shouldThrow(); |
196 |
jsr166 |
1.13 |
} catch (ClassCastException success) {} |
197 |
dl |
1.1 |
} |
198 |
|
|
|
199 |
dl |
1.5 |
/** |
200 |
dl |
1.6 |
* add of comparable succeeds |
201 |
dl |
1.5 |
*/ |
202 |
|
|
public void testAdd() { |
203 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
204 |
|
|
for (int i = 0; i < SIZE; ++i) { |
205 |
dl |
1.1 |
assertEquals(i, q.size()); |
206 |
|
|
assertTrue(q.add(new Integer(i))); |
207 |
|
|
} |
208 |
|
|
} |
209 |
|
|
|
210 |
dl |
1.5 |
/** |
211 |
dl |
1.6 |
* addAll(null) throws NPE |
212 |
dl |
1.5 |
*/ |
213 |
|
|
public void testAddAll1() { |
214 |
dl |
1.1 |
try { |
215 |
|
|
PriorityQueue q = new PriorityQueue(1); |
216 |
|
|
q.addAll(null); |
217 |
dl |
1.5 |
shouldThrow(); |
218 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
219 |
dl |
1.1 |
} |
220 |
jsr166 |
1.17 |
|
221 |
dl |
1.5 |
/** |
222 |
dl |
1.6 |
* addAll of a collection with null elements throws NPE |
223 |
dl |
1.5 |
*/ |
224 |
|
|
public void testAddAll2() { |
225 |
dl |
1.1 |
try { |
226 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
227 |
|
|
Integer[] ints = new Integer[SIZE]; |
228 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
229 |
dl |
1.5 |
shouldThrow(); |
230 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
231 |
dl |
1.1 |
} |
232 |
jsr166 |
1.17 |
|
233 |
dl |
1.5 |
/** |
234 |
dl |
1.6 |
* addAll of a collection with any null elements throws NPE after |
235 |
|
|
* possibly adding some elements |
236 |
dl |
1.5 |
*/ |
237 |
|
|
public void testAddAll3() { |
238 |
dl |
1.1 |
try { |
239 |
dl |
1.3 |
PriorityQueue q = new PriorityQueue(SIZE); |
240 |
|
|
Integer[] ints = new Integer[SIZE]; |
241 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
242 |
dl |
1.1 |
ints[i] = new Integer(i); |
243 |
|
|
q.addAll(Arrays.asList(ints)); |
244 |
dl |
1.5 |
shouldThrow(); |
245 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
246 |
dl |
1.1 |
} |
247 |
|
|
|
248 |
dl |
1.5 |
/** |
249 |
dl |
1.6 |
* Queue contains all elements of successful addAll |
250 |
dl |
1.5 |
*/ |
251 |
|
|
public void testAddAll5() { |
252 |
jsr166 |
1.14 |
Integer[] empty = new Integer[0]; |
253 |
|
|
Integer[] ints = new Integer[SIZE]; |
254 |
|
|
for (int i = 0; i < SIZE; ++i) |
255 |
|
|
ints[i] = new Integer(SIZE-1-i); |
256 |
|
|
PriorityQueue q = new PriorityQueue(SIZE); |
257 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
258 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
259 |
|
|
for (int i = 0; i < SIZE; ++i) |
260 |
|
|
assertEquals(new Integer(i), q.poll()); |
261 |
dl |
1.1 |
} |
262 |
|
|
|
263 |
dl |
1.5 |
/** |
264 |
dl |
1.6 |
* poll succeeds unless empty |
265 |
dl |
1.5 |
*/ |
266 |
|
|
public void testPoll() { |
267 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
268 |
|
|
for (int i = 0; i < SIZE; ++i) { |
269 |
jsr166 |
1.15 |
assertEquals(i, q.poll()); |
270 |
dl |
1.1 |
} |
271 |
jsr166 |
1.12 |
assertNull(q.poll()); |
272 |
dl |
1.1 |
} |
273 |
|
|
|
274 |
dl |
1.5 |
/** |
275 |
dl |
1.6 |
* peek returns next element, or null if empty |
276 |
dl |
1.5 |
*/ |
277 |
|
|
public void testPeek() { |
278 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
279 |
|
|
for (int i = 0; i < SIZE; ++i) { |
280 |
jsr166 |
1.15 |
assertEquals(i, q.peek()); |
281 |
|
|
assertEquals(i, q.poll()); |
282 |
dl |
1.1 |
assertTrue(q.peek() == null || |
283 |
jsr166 |
1.15 |
!q.peek().equals(i)); |
284 |
dl |
1.1 |
} |
285 |
jsr166 |
1.12 |
assertNull(q.peek()); |
286 |
dl |
1.1 |
} |
287 |
|
|
|
288 |
dl |
1.5 |
/** |
289 |
dl |
1.6 |
* element returns next element, or throws NSEE if empty |
290 |
dl |
1.5 |
*/ |
291 |
|
|
public void testElement() { |
292 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
293 |
|
|
for (int i = 0; i < SIZE; ++i) { |
294 |
jsr166 |
1.15 |
assertEquals(i, q.element()); |
295 |
|
|
assertEquals(i, q.poll()); |
296 |
dl |
1.1 |
} |
297 |
|
|
try { |
298 |
|
|
q.element(); |
299 |
dl |
1.5 |
shouldThrow(); |
300 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
301 |
dl |
1.1 |
} |
302 |
|
|
|
303 |
dl |
1.5 |
/** |
304 |
dl |
1.6 |
* remove removes next element, or throws NSEE if empty |
305 |
dl |
1.5 |
*/ |
306 |
|
|
public void testRemove() { |
307 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
308 |
|
|
for (int i = 0; i < SIZE; ++i) { |
309 |
jsr166 |
1.15 |
assertEquals(i, q.remove()); |
310 |
dl |
1.1 |
} |
311 |
|
|
try { |
312 |
|
|
q.remove(); |
313 |
dl |
1.5 |
shouldThrow(); |
314 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
315 |
dl |
1.1 |
} |
316 |
|
|
|
317 |
dl |
1.5 |
/** |
318 |
dl |
1.6 |
* remove(x) removes x and returns true if present |
319 |
dl |
1.5 |
*/ |
320 |
|
|
public void testRemoveElement() { |
321 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
322 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
323 |
jsr166 |
1.20 |
assertTrue(q.contains(i)); |
324 |
|
|
assertTrue(q.remove(i)); |
325 |
|
|
assertFalse(q.contains(i)); |
326 |
|
|
assertTrue(q.contains(i-1)); |
327 |
dl |
1.1 |
} |
328 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
329 |
jsr166 |
1.20 |
assertTrue(q.contains(i)); |
330 |
|
|
assertTrue(q.remove(i)); |
331 |
|
|
assertFalse(q.contains(i)); |
332 |
|
|
assertFalse(q.remove(i+1)); |
333 |
|
|
assertFalse(q.contains(i+1)); |
334 |
dl |
1.1 |
} |
335 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
336 |
dl |
1.1 |
} |
337 |
jsr166 |
1.9 |
|
338 |
dl |
1.5 |
/** |
339 |
dl |
1.6 |
* contains(x) reports true when elements added but not yet removed |
340 |
dl |
1.5 |
*/ |
341 |
|
|
public void testContains() { |
342 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
343 |
|
|
for (int i = 0; i < SIZE; ++i) { |
344 |
dl |
1.1 |
assertTrue(q.contains(new Integer(i))); |
345 |
|
|
q.poll(); |
346 |
|
|
assertFalse(q.contains(new Integer(i))); |
347 |
|
|
} |
348 |
|
|
} |
349 |
|
|
|
350 |
dl |
1.5 |
/** |
351 |
dl |
1.6 |
* clear removes all elements |
352 |
dl |
1.5 |
*/ |
353 |
|
|
public void testClear() { |
354 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
355 |
dl |
1.1 |
q.clear(); |
356 |
|
|
assertTrue(q.isEmpty()); |
357 |
|
|
assertEquals(0, q.size()); |
358 |
|
|
q.add(new Integer(1)); |
359 |
|
|
assertFalse(q.isEmpty()); |
360 |
|
|
q.clear(); |
361 |
|
|
assertTrue(q.isEmpty()); |
362 |
|
|
} |
363 |
|
|
|
364 |
dl |
1.5 |
/** |
365 |
dl |
1.6 |
* containsAll(c) is true when c contains a subset of elements |
366 |
dl |
1.5 |
*/ |
367 |
|
|
public void testContainsAll() { |
368 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
369 |
|
|
PriorityQueue p = new PriorityQueue(SIZE); |
370 |
|
|
for (int i = 0; i < SIZE; ++i) { |
371 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
372 |
|
|
assertFalse(p.containsAll(q)); |
373 |
|
|
p.add(new Integer(i)); |
374 |
|
|
} |
375 |
|
|
assertTrue(p.containsAll(q)); |
376 |
|
|
} |
377 |
|
|
|
378 |
dl |
1.5 |
/** |
379 |
dl |
1.6 |
* retainAll(c) retains only those elements of c and reports true if changed |
380 |
dl |
1.5 |
*/ |
381 |
|
|
public void testRetainAll() { |
382 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
383 |
|
|
PriorityQueue p = populatedQueue(SIZE); |
384 |
|
|
for (int i = 0; i < SIZE; ++i) { |
385 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
386 |
|
|
if (i == 0) |
387 |
|
|
assertFalse(changed); |
388 |
|
|
else |
389 |
|
|
assertTrue(changed); |
390 |
|
|
|
391 |
|
|
assertTrue(q.containsAll(p)); |
392 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
393 |
dl |
1.1 |
p.remove(); |
394 |
|
|
} |
395 |
|
|
} |
396 |
|
|
|
397 |
dl |
1.5 |
/** |
398 |
dl |
1.6 |
* removeAll(c) removes only those elements of c and reports true if changed |
399 |
dl |
1.5 |
*/ |
400 |
|
|
public void testRemoveAll() { |
401 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
402 |
|
|
PriorityQueue q = populatedQueue(SIZE); |
403 |
|
|
PriorityQueue p = populatedQueue(i); |
404 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
405 |
dl |
1.3 |
assertEquals(SIZE-i, q.size()); |
406 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
407 |
|
|
Integer I = (Integer)(p.remove()); |
408 |
|
|
assertFalse(q.contains(I)); |
409 |
|
|
} |
410 |
|
|
} |
411 |
|
|
} |
412 |
|
|
|
413 |
dl |
1.5 |
/** |
414 |
dl |
1.6 |
* toArray contains all elements |
415 |
dl |
1.5 |
*/ |
416 |
|
|
public void testToArray() { |
417 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
418 |
jsr166 |
1.12 |
Object[] o = q.toArray(); |
419 |
dl |
1.1 |
Arrays.sort(o); |
420 |
jsr166 |
1.12 |
for (int i = 0; i < o.length; i++) |
421 |
jsr166 |
1.18 |
assertSame(o[i], q.poll()); |
422 |
dl |
1.1 |
} |
423 |
|
|
|
424 |
dl |
1.5 |
/** |
425 |
dl |
1.6 |
* toArray(a) contains all elements |
426 |
dl |
1.5 |
*/ |
427 |
|
|
public void testToArray2() { |
428 |
jsr166 |
1.19 |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
429 |
jsr166 |
1.12 |
Integer[] ints = new Integer[SIZE]; |
430 |
jsr166 |
1.19 |
Integer[] array = q.toArray(ints); |
431 |
|
|
assertSame(ints, array); |
432 |
dl |
1.1 |
Arrays.sort(ints); |
433 |
jsr166 |
1.10 |
for (int i = 0; i < ints.length; i++) |
434 |
jsr166 |
1.18 |
assertSame(ints[i], q.poll()); |
435 |
dl |
1.1 |
} |
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 |
jsr166 |
1.16 |
public void testIteratorRemove() { |
455 |
dl |
1.1 |
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 |
dl |
1.5 |
/** |
471 |
dl |
1.6 |
* toString contains toStrings of elements |
472 |
dl |
1.5 |
*/ |
473 |
|
|
public void testToString() { |
474 |
dl |
1.3 |
PriorityQueue q = populatedQueue(SIZE); |
475 |
dl |
1.1 |
String s = q.toString(); |
476 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
477 |
jsr166 |
1.22 |
assertTrue(s.contains(String.valueOf(i))); |
478 |
dl |
1.1 |
} |
479 |
jsr166 |
1.9 |
} |
480 |
dl |
1.1 |
|
481 |
dl |
1.5 |
/** |
482 |
jsr166 |
1.9 |
* A deserialized serialized queue has same elements |
483 |
dl |
1.5 |
*/ |
484 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
485 |
jsr166 |
1.23 |
Queue x = populatedQueue(SIZE); |
486 |
|
|
Queue y = serialClone(x); |
487 |
|
|
|
488 |
|
|
assertTrue(x != y); |
489 |
|
|
assertEquals(x.size(), y.size()); |
490 |
|
|
while (!x.isEmpty()) { |
491 |
|
|
assertFalse(y.isEmpty()); |
492 |
|
|
assertEquals(x.remove(), y.remove()); |
493 |
|
|
} |
494 |
|
|
assertTrue(y.isEmpty()); |
495 |
dl |
1.2 |
} |
496 |
dl |
1.1 |
} |