ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.7
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.6: +11 -0 lines
Log Message:
Added tests and documentation

File Contents

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