11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class PriorityQueueTest extends JSR166TestCase { |
14 |
– |
|
14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
18 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(PriorityQueueTest.class); |
19 |
|
} |
28 |
|
} |
29 |
|
} |
30 |
|
|
33 |
– |
|
31 |
|
/** |
32 |
|
* Create a queue of given size containing consecutive |
33 |
|
* Integers 0 ... n. |
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* |
48 |
> |
* A new queue has unbounded capacity |
49 |
|
*/ |
50 |
|
public void testConstructor1() { |
51 |
|
assertEquals(0, new PriorityQueue(SIZE).size()); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
< |
* |
55 |
> |
* Constructor throws IAE if capacity argument nonpositive |
56 |
|
*/ |
57 |
|
public void testConstructor2() { |
58 |
|
try { |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* |
66 |
> |
* Initializing from null Collection throws NPE |
67 |
|
*/ |
68 |
|
public void testConstructor3() { |
72 |
– |
|
69 |
|
try { |
70 |
|
PriorityQueue q = new PriorityQueue((Collection)null); |
71 |
|
shouldThrow(); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* |
77 |
> |
* Initializing from Collection of null elements throws NPE |
78 |
|
*/ |
79 |
|
public void testConstructor4() { |
80 |
|
try { |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* |
89 |
> |
* Initializing from Collection with some null elements throws NPE |
90 |
|
*/ |
91 |
|
public void testConstructor5() { |
92 |
|
try { |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* |
103 |
> |
* Queue contains all elements of collection used to initialize |
104 |
|
*/ |
105 |
|
public void testConstructor6() { |
106 |
|
try { |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* |
118 |
> |
* The comparator used in constructor is used |
119 |
|
*/ |
120 |
|
public void testConstructor7() { |
121 |
|
try { |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* |
136 |
> |
* isEmpty is true before add, false after |
137 |
|
*/ |
138 |
|
public void testEmpty() { |
139 |
|
PriorityQueue q = new PriorityQueue(2); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* |
150 |
> |
* size changes when elements added and removed |
151 |
|
*/ |
152 |
|
public void testSize() { |
153 |
|
PriorityQueue q = populatedQueue(SIZE); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* |
165 |
> |
* offer(null) throws NPE |
166 |
|
*/ |
167 |
|
public void testOfferNull() { |
168 |
|
try { |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* |
176 |
> |
* add(null) throws NPE |
177 |
> |
*/ |
178 |
> |
public void testAddNull() { |
179 |
> |
try { |
180 |
> |
PriorityQueue q = new PriorityQueue(1); |
181 |
> |
q.add(null); |
182 |
> |
shouldThrow(); |
183 |
> |
} catch (NullPointerException success) { } |
184 |
> |
} |
185 |
> |
|
186 |
> |
/** |
187 |
> |
* Offer of comparable element succeeds |
188 |
|
*/ |
189 |
|
public void testOffer() { |
190 |
|
PriorityQueue q = new PriorityQueue(1); |
191 |
< |
assertTrue(q.offer(new Integer(0))); |
192 |
< |
assertTrue(q.offer(new Integer(1))); |
191 |
> |
assertTrue(q.offer(zero)); |
192 |
> |
assertTrue(q.offer(one)); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* |
196 |
> |
* Offer of non-Comparable throws CCE |
197 |
|
*/ |
198 |
|
public void testOfferNonComparable() { |
199 |
|
try { |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
< |
* |
210 |
> |
* add of comparable succeeds |
211 |
|
*/ |
212 |
|
public void testAdd() { |
213 |
|
PriorityQueue q = new PriorityQueue(SIZE); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* |
221 |
> |
* addAll(null) throws NPE |
222 |
|
*/ |
223 |
|
public void testAddAll1() { |
224 |
|
try { |
229 |
|
catch (NullPointerException success) {} |
230 |
|
} |
231 |
|
/** |
232 |
< |
* |
232 |
> |
* addAll of a collection with null elements throws NPE |
233 |
|
*/ |
234 |
|
public void testAddAll2() { |
235 |
|
try { |
241 |
|
catch (NullPointerException success) {} |
242 |
|
} |
243 |
|
/** |
244 |
< |
* |
244 |
> |
* addAll of a collection with any null elements throws NPE after |
245 |
> |
* possibly adding some elements |
246 |
|
*/ |
247 |
|
public void testAddAll3() { |
248 |
|
try { |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* |
260 |
> |
* Queue contains all elements of successful addAll |
261 |
|
*/ |
262 |
|
public void testAddAll5() { |
263 |
|
try { |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
< |
* |
278 |
> |
* poll succeeds unless empty |
279 |
|
*/ |
280 |
|
public void testPoll() { |
281 |
|
PriorityQueue q = populatedQueue(SIZE); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* |
289 |
> |
* peek returns next element, or null if empty |
290 |
|
*/ |
291 |
|
public void testPeek() { |
292 |
|
PriorityQueue q = populatedQueue(SIZE); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* |
303 |
> |
* element returns next element, or throws NSEE if empty |
304 |
|
*/ |
305 |
|
public void testElement() { |
306 |
|
PriorityQueue q = populatedQueue(SIZE); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
< |
* |
319 |
> |
* remove removes next element, or throws NSEE if empty |
320 |
|
*/ |
321 |
|
public void testRemove() { |
322 |
|
PriorityQueue q = populatedQueue(SIZE); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* |
334 |
> |
* remove(x) removes x and returns true if present |
335 |
|
*/ |
336 |
|
public void testRemoveElement() { |
337 |
|
PriorityQueue q = populatedQueue(SIZE); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* |
349 |
> |
* contains(x) reports true when elements added but not yet removed |
350 |
|
*/ |
351 |
|
public void testContains() { |
352 |
|
PriorityQueue q = populatedQueue(SIZE); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* |
361 |
> |
* clear removes all elements |
362 |
|
*/ |
363 |
|
public void testClear() { |
364 |
|
PriorityQueue q = populatedQueue(SIZE); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* |
375 |
> |
* containsAll(c) is true when c contains a subset of elements |
376 |
|
*/ |
377 |
|
public void testContainsAll() { |
378 |
|
PriorityQueue q = populatedQueue(SIZE); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
< |
* |
389 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
390 |
|
*/ |
391 |
|
public void testRetainAll() { |
392 |
|
PriorityQueue q = populatedQueue(SIZE); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
< |
* |
408 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
409 |
|
*/ |
410 |
|
public void testRemoveAll() { |
411 |
|
for (int i = 1; i < SIZE; ++i) { |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* |
424 |
> |
* toArray contains all elements |
425 |
|
*/ |
426 |
|
public void testToArray() { |
427 |
|
PriorityQueue q = populatedQueue(SIZE); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* |
435 |
> |
* toArray(a) contains all elements |
436 |
|
*/ |
437 |
|
public void testToArray2() { |
438 |
|
PriorityQueue q = populatedQueue(SIZE); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
< |
* |
447 |
> |
* iterator iterates through all elements |
448 |
|
*/ |
449 |
|
public void testIterator() { |
450 |
|
PriorityQueue q = populatedQueue(SIZE); |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* |
461 |
> |
* iterator.remove removes current element |
462 |
|
*/ |
463 |
|
public void testIteratorRemove () { |
464 |
|
final PriorityQueue q = new PriorityQueue(3); |
478 |
|
|
479 |
|
|
480 |
|
/** |
481 |
< |
* |
481 |
> |
* toString contains toStrings of elements |
482 |
|
*/ |
483 |
|
public void testToString() { |
484 |
|
PriorityQueue q = populatedQueue(SIZE); |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
< |
* |
492 |
> |
* A deserialized serialized queue has same elements |
493 |
|
*/ |
494 |
|
public void testSerialization() { |
495 |
|
PriorityQueue q = populatedQueue(SIZE); |