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

# User Rev Content
1 dl 1.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 dl 1.2 import java.io.*;
12 dl 1.1
13 dl 1.3 public class PriorityQueueTest extends JSR166TestCase {
14 dl 1.1 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 dl 1.3 private PriorityQueue populatedQueue(int n) {
36 dl 1.1 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 dl 1.5 /**
48 dl 1.6 * A new queue has unbounded capacity
49 dl 1.5 */
50     public void testConstructor1() {
51 dl 1.3 assertEquals(0, new PriorityQueue(SIZE).size());
52 dl 1.1 }
53    
54 dl 1.5 /**
55 dl 1.6 * Constructor throws IAE if capacity argument nonpositive
56 dl 1.5 */
57     public void testConstructor2() {
58 dl 1.1 try {
59     PriorityQueue q = new PriorityQueue(0);
60 dl 1.5 shouldThrow();
61 dl 1.1 }
62     catch (IllegalArgumentException success) {}
63     }
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 dl 1.1 }
73     catch (NullPointerException success) {}
74     }
75    
76 dl 1.5 /**
77 dl 1.6 * Initializing from Collection of null elements throws NPE
78 dl 1.5 */
79     public void testConstructor4() {
80 dl 1.1 try {
81 dl 1.3 Integer[] ints = new Integer[SIZE];
82 dl 1.1 PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
83 dl 1.5 shouldThrow();
84 dl 1.1 }
85     catch (NullPointerException success) {}
86     }
87    
88 dl 1.5 /**
89 dl 1.6 * Initializing from Collection with some null elements throws NPE
90 dl 1.5 */
91     public void testConstructor5() {
92 dl 1.1 try {
93 dl 1.3 Integer[] ints = new Integer[SIZE];
94     for (int i = 0; i < SIZE-1; ++i)
95 dl 1.1 ints[i] = new Integer(i);
96     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
97 dl 1.5 shouldThrow();
98 dl 1.1 }
99     catch (NullPointerException success) {}
100     }
101    
102 dl 1.5 /**
103 dl 1.6 * Queue contains all elements of collection used to initialize
104 dl 1.5 */
105     public void testConstructor6() {
106 dl 1.1 try {
107 dl 1.3 Integer[] ints = new Integer[SIZE];
108     for (int i = 0; i < SIZE; ++i)
109 dl 1.1 ints[i] = new Integer(i);
110     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
111 dl 1.3 for (int i = 0; i < SIZE; ++i)
112 dl 1.1 assertEquals(ints[i], q.poll());
113     }
114     finally {}
115     }
116    
117 dl 1.5 /**
118 dl 1.6 * The comparator used in constructor is used
119 dl 1.5 */
120     public void testConstructor7() {
121 dl 1.1 try {
122 dl 1.4 MyReverseComparator cmp = new MyReverseComparator();
123     PriorityQueue q = new PriorityQueue(SIZE, cmp);
124     assertEquals(cmp, q.comparator());
125 dl 1.3 Integer[] ints = new Integer[SIZE];
126     for (int i = 0; i < SIZE; ++i)
127 dl 1.1 ints[i] = new Integer(i);
128     q.addAll(Arrays.asList(ints));
129 dl 1.3 for (int i = SIZE-1; i >= 0; --i)
130 dl 1.1 assertEquals(ints[i], q.poll());
131     }
132     finally {}
133     }
134    
135 dl 1.5 /**
136 dl 1.6 * isEmpty is true before add, false after
137 dl 1.5 */
138 dl 1.1 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 dl 1.5 /**
150 dl 1.6 * size changes when elements added and removed
151 dl 1.5 */
152 dl 1.1 public void testSize() {
153 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
154     for (int i = 0; i < SIZE; ++i) {
155     assertEquals(SIZE-i, q.size());
156 dl 1.1 q.remove();
157     }
158 dl 1.3 for (int i = 0; i < SIZE; ++i) {
159 dl 1.1 assertEquals(i, q.size());
160     q.add(new Integer(i));
161     }
162     }
163    
164 dl 1.5 /**
165 dl 1.6 * offer(null) throws NPE
166 dl 1.5 */
167     public void testOfferNull() {
168 dl 1.1 try {
169     PriorityQueue q = new PriorityQueue(1);
170     q.offer(null);
171 dl 1.5 shouldThrow();
172 dl 1.1 } catch (NullPointerException success) { }
173     }
174    
175 dl 1.5 /**
176 dl 1.7 * 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 dl 1.6 * Offer of comparable element succeeds
188 dl 1.5 */
189 dl 1.1 public void testOffer() {
190     PriorityQueue q = new PriorityQueue(1);
191 dl 1.6 assertTrue(q.offer(zero));
192     assertTrue(q.offer(one));
193 dl 1.1 }
194    
195 dl 1.5 /**
196 dl 1.6 * Offer of non-Comparable throws CCE
197 dl 1.5 */
198 dl 1.1 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 dl 1.5 shouldThrow();
205 dl 1.1 }
206     catch(ClassCastException success) {}
207     }
208    
209 dl 1.5 /**
210 dl 1.6 * add of comparable succeeds
211 dl 1.5 */
212     public void testAdd() {
213 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
214     for (int i = 0; i < SIZE; ++i) {
215 dl 1.1 assertEquals(i, q.size());
216     assertTrue(q.add(new Integer(i)));
217     }
218     }
219    
220 dl 1.5 /**
221 dl 1.6 * addAll(null) throws NPE
222 dl 1.5 */
223     public void testAddAll1() {
224 dl 1.1 try {
225     PriorityQueue q = new PriorityQueue(1);
226     q.addAll(null);
227 dl 1.5 shouldThrow();
228 dl 1.1 }
229     catch (NullPointerException success) {}
230     }
231 dl 1.5 /**
232 dl 1.6 * addAll of a collection with null elements throws NPE
233 dl 1.5 */
234     public void testAddAll2() {
235 dl 1.1 try {
236 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
237     Integer[] ints = new Integer[SIZE];
238 dl 1.1 q.addAll(Arrays.asList(ints));
239 dl 1.5 shouldThrow();
240 dl 1.1 }
241     catch (NullPointerException success) {}
242     }
243 dl 1.5 /**
244 dl 1.6 * addAll of a collection with any null elements throws NPE after
245     * possibly adding some elements
246 dl 1.5 */
247     public void testAddAll3() {
248 dl 1.1 try {
249 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
250     Integer[] ints = new Integer[SIZE];
251     for (int i = 0; i < SIZE-1; ++i)
252 dl 1.1 ints[i] = new Integer(i);
253     q.addAll(Arrays.asList(ints));
254 dl 1.5 shouldThrow();
255 dl 1.1 }
256     catch (NullPointerException success) {}
257     }
258    
259 dl 1.5 /**
260 dl 1.6 * Queue contains all elements of successful addAll
261 dl 1.5 */
262     public void testAddAll5() {
263 dl 1.1 try {
264     Integer[] empty = new Integer[0];
265 dl 1.3 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 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
270     assertTrue(q.addAll(Arrays.asList(ints)));
271 dl 1.3 for (int i = 0; i < SIZE; ++i)
272 dl 1.1 assertEquals(new Integer(i), q.poll());
273     }
274     finally {}
275     }
276    
277 dl 1.5 /**
278 dl 1.6 * poll succeeds unless empty
279 dl 1.5 */
280     public void testPoll() {
281 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
282     for (int i = 0; i < SIZE; ++i) {
283 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
284     }
285     assertNull(q.poll());
286     }
287    
288 dl 1.5 /**
289 dl 1.6 * peek returns next element, or null if empty
290 dl 1.5 */
291     public void testPeek() {
292 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
293     for (int i = 0; i < SIZE; ++i) {
294 dl 1.1 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 dl 1.5 /**
303 dl 1.6 * element returns next element, or throws NSEE if empty
304 dl 1.5 */
305     public void testElement() {
306 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
307     for (int i = 0; i < SIZE; ++i) {
308 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
309     q.poll();
310     }
311     try {
312     q.element();
313 dl 1.5 shouldThrow();
314 dl 1.1 }
315     catch (NoSuchElementException success) {}
316     }
317    
318 dl 1.5 /**
319 dl 1.6 * remove removes next element, or throws NSEE if empty
320 dl 1.5 */
321     public void testRemove() {
322 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
323     for (int i = 0; i < SIZE; ++i) {
324 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
325     }
326     try {
327     q.remove();
328 dl 1.5 shouldThrow();
329 dl 1.1 } catch (NoSuchElementException success){
330     }
331     }
332    
333 dl 1.5 /**
334 dl 1.6 * remove(x) removes x and returns true if present
335 dl 1.5 */
336     public void testRemoveElement() {
337 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
338     for (int i = 1; i < SIZE; i+=2) {
339 dl 1.1 assertTrue(q.remove(new Integer(i)));
340     }
341 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
342 dl 1.1 assertTrue(q.remove(new Integer(i)));
343     assertFalse(q.remove(new Integer(i+1)));
344     }
345 dl 1.2 assertTrue(q.isEmpty());
346 dl 1.1 }
347    
348 dl 1.5 /**
349 dl 1.6 * contains(x) reports true when elements added but not yet removed
350 dl 1.5 */
351     public void testContains() {
352 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
353     for (int i = 0; i < SIZE; ++i) {
354 dl 1.1 assertTrue(q.contains(new Integer(i)));
355     q.poll();
356     assertFalse(q.contains(new Integer(i)));
357     }
358     }
359    
360 dl 1.5 /**
361 dl 1.6 * clear removes all elements
362 dl 1.5 */
363     public void testClear() {
364 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
365 dl 1.1 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 dl 1.5 /**
375 dl 1.6 * containsAll(c) is true when c contains a subset of elements
376 dl 1.5 */
377     public void testContainsAll() {
378 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
379     PriorityQueue p = new PriorityQueue(SIZE);
380     for (int i = 0; i < SIZE; ++i) {
381 dl 1.1 assertTrue(q.containsAll(p));
382     assertFalse(p.containsAll(q));
383     p.add(new Integer(i));
384     }
385     assertTrue(p.containsAll(q));
386     }
387    
388 dl 1.5 /**
389 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
390 dl 1.5 */
391     public void testRetainAll() {
392 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
393     PriorityQueue p = populatedQueue(SIZE);
394     for (int i = 0; i < SIZE; ++i) {
395 dl 1.1 boolean changed = q.retainAll(p);
396     if (i == 0)
397     assertFalse(changed);
398     else
399     assertTrue(changed);
400    
401     assertTrue(q.containsAll(p));
402 dl 1.3 assertEquals(SIZE-i, q.size());
403 dl 1.1 p.remove();
404     }
405     }
406    
407 dl 1.5 /**
408 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
409 dl 1.5 */
410     public void testRemoveAll() {
411 dl 1.3 for (int i = 1; i < SIZE; ++i) {
412     PriorityQueue q = populatedQueue(SIZE);
413     PriorityQueue p = populatedQueue(i);
414 dl 1.1 assertTrue(q.removeAll(p));
415 dl 1.3 assertEquals(SIZE-i, q.size());
416 dl 1.1 for (int j = 0; j < i; ++j) {
417     Integer I = (Integer)(p.remove());
418     assertFalse(q.contains(I));
419     }
420     }
421     }
422    
423 dl 1.5 /**
424 dl 1.6 * toArray contains all elements
425 dl 1.5 */
426     public void testToArray() {
427 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
428 dl 1.1 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 dl 1.5 /**
435 dl 1.6 * toArray(a) contains all elements
436 dl 1.5 */
437     public void testToArray2() {
438 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
439     Integer[] ints = new Integer[SIZE];
440 dl 1.1 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 dl 1.5 /**
447 dl 1.6 * iterator iterates through all elements
448 dl 1.5 */
449     public void testIterator() {
450 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
451 dl 1.1 int i = 0;
452     Iterator it = q.iterator();
453     while(it.hasNext()) {
454     assertTrue(q.contains(it.next()));
455     ++i;
456     }
457 dl 1.3 assertEquals(i, SIZE);
458 dl 1.1 }
459    
460 dl 1.5 /**
461 dl 1.6 * iterator.remove removes current element
462 dl 1.5 */
463 dl 1.1 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 dl 1.5 /**
481 dl 1.6 * toString contains toStrings of elements
482 dl 1.5 */
483     public void testToString() {
484 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
485 dl 1.1 String s = q.toString();
486 dl 1.3 for (int i = 0; i < SIZE; ++i) {
487 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
488     }
489     }
490    
491 dl 1.5 /**
492 dl 1.6 * A deserialized serialized queue has same elements
493 dl 1.5 */
494 dl 1.2 public void testSerialization() {
495 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
496 dl 1.2 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 dl 1.5 unexpectedException();
510 dl 1.2 }
511     }
512 dl 1.1 }