ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.14
Committed: Sat Nov 21 10:29:50 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +24 -33 lines
Log Message:
improve exception handling

File Contents

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