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