ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.6
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.5: +36 -39 lines
Log Message:
improve tck javadocs; rename and add a few tests

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.6 * Offer of comparable element succeeds
177 dl 1.5 */
178 dl 1.1 public void testOffer() {
179     PriorityQueue q = new PriorityQueue(1);
180 dl 1.6 assertTrue(q.offer(zero));
181     assertTrue(q.offer(one));
182 dl 1.1 }
183    
184 dl 1.5 /**
185 dl 1.6 * Offer of non-Comparable throws CCE
186 dl 1.5 */
187 dl 1.1 public void testOfferNonComparable() {
188     try {
189     PriorityQueue q = new PriorityQueue(1);
190     q.offer(new Object());
191     q.offer(new Object());
192     q.offer(new Object());
193 dl 1.5 shouldThrow();
194 dl 1.1 }
195     catch(ClassCastException success) {}
196     }
197    
198 dl 1.5 /**
199 dl 1.6 * add of comparable succeeds
200 dl 1.5 */
201     public void testAdd() {
202 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
203     for (int i = 0; i < SIZE; ++i) {
204 dl 1.1 assertEquals(i, q.size());
205     assertTrue(q.add(new Integer(i)));
206     }
207     }
208    
209 dl 1.5 /**
210 dl 1.6 * addAll(null) throws NPE
211 dl 1.5 */
212     public void testAddAll1() {
213 dl 1.1 try {
214     PriorityQueue q = new PriorityQueue(1);
215     q.addAll(null);
216 dl 1.5 shouldThrow();
217 dl 1.1 }
218     catch (NullPointerException success) {}
219     }
220 dl 1.5 /**
221 dl 1.6 * addAll of a collection with null elements throws NPE
222 dl 1.5 */
223     public void testAddAll2() {
224 dl 1.1 try {
225 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
226     Integer[] ints = new Integer[SIZE];
227 dl 1.1 q.addAll(Arrays.asList(ints));
228 dl 1.5 shouldThrow();
229 dl 1.1 }
230     catch (NullPointerException success) {}
231     }
232 dl 1.5 /**
233 dl 1.6 * addAll of a collection with any null elements throws NPE after
234     * possibly adding some elements
235 dl 1.5 */
236     public void testAddAll3() {
237 dl 1.1 try {
238 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
239     Integer[] ints = new Integer[SIZE];
240     for (int i = 0; i < SIZE-1; ++i)
241 dl 1.1 ints[i] = new Integer(i);
242     q.addAll(Arrays.asList(ints));
243 dl 1.5 shouldThrow();
244 dl 1.1 }
245     catch (NullPointerException success) {}
246     }
247    
248 dl 1.5 /**
249 dl 1.6 * Queue contains all elements of successful addAll
250 dl 1.5 */
251     public void testAddAll5() {
252 dl 1.1 try {
253     Integer[] empty = new Integer[0];
254 dl 1.3 Integer[] ints = new Integer[SIZE];
255     for (int i = 0; i < SIZE; ++i)
256     ints[i] = new Integer(SIZE-1-i);
257     PriorityQueue q = new PriorityQueue(SIZE);
258 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
259     assertTrue(q.addAll(Arrays.asList(ints)));
260 dl 1.3 for (int i = 0; i < SIZE; ++i)
261 dl 1.1 assertEquals(new Integer(i), q.poll());
262     }
263     finally {}
264     }
265    
266 dl 1.5 /**
267 dl 1.6 * poll succeeds unless empty
268 dl 1.5 */
269     public void testPoll() {
270 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
271     for (int i = 0; i < SIZE; ++i) {
272 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
273     }
274     assertNull(q.poll());
275     }
276    
277 dl 1.5 /**
278 dl 1.6 * peek returns next element, or null if empty
279 dl 1.5 */
280     public void testPeek() {
281 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
282     for (int i = 0; i < SIZE; ++i) {
283 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
284     q.poll();
285     assertTrue(q.peek() == null ||
286     i != ((Integer)q.peek()).intValue());
287     }
288     assertNull(q.peek());
289     }
290    
291 dl 1.5 /**
292 dl 1.6 * element returns next element, or throws NSEE if empty
293 dl 1.5 */
294     public void testElement() {
295 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
296     for (int i = 0; i < SIZE; ++i) {
297 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
298     q.poll();
299     }
300     try {
301     q.element();
302 dl 1.5 shouldThrow();
303 dl 1.1 }
304     catch (NoSuchElementException success) {}
305     }
306    
307 dl 1.5 /**
308 dl 1.6 * remove removes next element, or throws NSEE if empty
309 dl 1.5 */
310     public void testRemove() {
311 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
312     for (int i = 0; i < SIZE; ++i) {
313 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
314     }
315     try {
316     q.remove();
317 dl 1.5 shouldThrow();
318 dl 1.1 } catch (NoSuchElementException success){
319     }
320     }
321    
322 dl 1.5 /**
323 dl 1.6 * remove(x) removes x and returns true if present
324 dl 1.5 */
325     public void testRemoveElement() {
326 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
327     for (int i = 1; i < SIZE; i+=2) {
328 dl 1.1 assertTrue(q.remove(new Integer(i)));
329     }
330 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
331 dl 1.1 assertTrue(q.remove(new Integer(i)));
332     assertFalse(q.remove(new Integer(i+1)));
333     }
334 dl 1.2 assertTrue(q.isEmpty());
335 dl 1.1 }
336    
337 dl 1.5 /**
338 dl 1.6 * contains(x) reports true when elements added but not yet removed
339 dl 1.5 */
340     public void testContains() {
341 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
342     for (int i = 0; i < SIZE; ++i) {
343 dl 1.1 assertTrue(q.contains(new Integer(i)));
344     q.poll();
345     assertFalse(q.contains(new Integer(i)));
346     }
347     }
348    
349 dl 1.5 /**
350 dl 1.6 * clear removes all elements
351 dl 1.5 */
352     public void testClear() {
353 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
354 dl 1.1 q.clear();
355     assertTrue(q.isEmpty());
356     assertEquals(0, q.size());
357     q.add(new Integer(1));
358     assertFalse(q.isEmpty());
359     q.clear();
360     assertTrue(q.isEmpty());
361     }
362    
363 dl 1.5 /**
364 dl 1.6 * containsAll(c) is true when c contains a subset of elements
365 dl 1.5 */
366     public void testContainsAll() {
367 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
368     PriorityQueue p = new PriorityQueue(SIZE);
369     for (int i = 0; i < SIZE; ++i) {
370 dl 1.1 assertTrue(q.containsAll(p));
371     assertFalse(p.containsAll(q));
372     p.add(new Integer(i));
373     }
374     assertTrue(p.containsAll(q));
375     }
376    
377 dl 1.5 /**
378 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
379 dl 1.5 */
380     public void testRetainAll() {
381 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
382     PriorityQueue p = populatedQueue(SIZE);
383     for (int i = 0; i < SIZE; ++i) {
384 dl 1.1 boolean changed = q.retainAll(p);
385     if (i == 0)
386     assertFalse(changed);
387     else
388     assertTrue(changed);
389    
390     assertTrue(q.containsAll(p));
391 dl 1.3 assertEquals(SIZE-i, q.size());
392 dl 1.1 p.remove();
393     }
394     }
395    
396 dl 1.5 /**
397 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
398 dl 1.5 */
399     public void testRemoveAll() {
400 dl 1.3 for (int i = 1; i < SIZE; ++i) {
401     PriorityQueue q = populatedQueue(SIZE);
402     PriorityQueue p = populatedQueue(i);
403 dl 1.1 assertTrue(q.removeAll(p));
404 dl 1.3 assertEquals(SIZE-i, q.size());
405 dl 1.1 for (int j = 0; j < i; ++j) {
406     Integer I = (Integer)(p.remove());
407     assertFalse(q.contains(I));
408     }
409     }
410     }
411    
412 dl 1.5 /**
413 dl 1.6 * toArray contains all elements
414 dl 1.5 */
415     public void testToArray() {
416 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
417 dl 1.1 Object[] o = q.toArray();
418     Arrays.sort(o);
419     for(int i = 0; i < o.length; i++)
420     assertEquals(o[i], q.poll());
421     }
422    
423 dl 1.5 /**
424 dl 1.6 * toArray(a) contains all elements
425 dl 1.5 */
426     public void testToArray2() {
427 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
428     Integer[] ints = new Integer[SIZE];
429 dl 1.1 ints = (Integer[])q.toArray(ints);
430     Arrays.sort(ints);
431     for(int i = 0; i < ints.length; i++)
432     assertEquals(ints[i], q.poll());
433     }
434    
435 dl 1.5 /**
436 dl 1.6 * iterator iterates through all elements
437 dl 1.5 */
438     public void testIterator() {
439 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
440 dl 1.1 int i = 0;
441     Iterator it = q.iterator();
442     while(it.hasNext()) {
443     assertTrue(q.contains(it.next()));
444     ++i;
445     }
446 dl 1.3 assertEquals(i, SIZE);
447 dl 1.1 }
448    
449 dl 1.5 /**
450 dl 1.6 * iterator.remove removes current element
451 dl 1.5 */
452 dl 1.1 public void testIteratorRemove () {
453     final PriorityQueue q = new PriorityQueue(3);
454     q.add(new Integer(2));
455     q.add(new Integer(1));
456     q.add(new Integer(3));
457    
458     Iterator it = q.iterator();
459     it.next();
460     it.remove();
461    
462     it = q.iterator();
463     assertEquals(it.next(), new Integer(2));
464     assertEquals(it.next(), new Integer(3));
465     assertFalse(it.hasNext());
466     }
467    
468    
469 dl 1.5 /**
470 dl 1.6 * toString contains toStrings of elements
471 dl 1.5 */
472     public void testToString() {
473 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
474 dl 1.1 String s = q.toString();
475 dl 1.3 for (int i = 0; i < SIZE; ++i) {
476 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
477     }
478     }
479    
480 dl 1.5 /**
481 dl 1.6 * A deserialized serialized queue has same elements
482 dl 1.5 */
483 dl 1.2 public void testSerialization() {
484 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
485 dl 1.2 try {
486     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
487     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
488     out.writeObject(q);
489     out.close();
490    
491     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
492     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
493     PriorityQueue r = (PriorityQueue)in.readObject();
494     assertEquals(q.size(), r.size());
495     while (!q.isEmpty())
496     assertEquals(q.remove(), r.remove());
497     } catch(Exception e){
498 dl 1.5 unexpectedException();
499 dl 1.2 }
500     }
501 dl 1.1 }