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