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

# Content
1 /*
2 * 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 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12 import java.io.*;
13
14 public class PriorityQueueTest extends JSR166TestCase {
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18 public static Test suite() {
19 return new TestSuite(PriorityQueueTest.class);
20 }
21
22 static class MyReverseComparator implements Comparator {
23 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 private PriorityQueue populatedQueue(int n) {
37 PriorityQueue q = new PriorityQueue(n);
38 assertTrue(q.isEmpty());
39 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 assertFalse(q.isEmpty());
44 assertEquals(n, q.size());
45 return q;
46 }
47
48 /**
49 * A new queue has unbounded capacity
50 */
51 public void testConstructor1() {
52 assertEquals(0, new PriorityQueue(SIZE).size());
53 }
54
55 /**
56 * Constructor throws IAE if capacity argument nonpositive
57 */
58 public void testConstructor2() {
59 try {
60 PriorityQueue q = new PriorityQueue(0);
61 shouldThrow();
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 } catch (NullPointerException success) {}
73 }
74
75 /**
76 * Initializing from Collection of null elements throws NPE
77 */
78 public void testConstructor4() {
79 try {
80 Integer[] ints = new Integer[SIZE];
81 PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
82 shouldThrow();
83 } catch (NullPointerException success) {}
84 }
85
86 /**
87 * Initializing from Collection with some null elements throws NPE
88 */
89 public void testConstructor5() {
90 try {
91 Integer[] ints = new Integer[SIZE];
92 for (int i = 0; i < SIZE-1; ++i)
93 ints[i] = new Integer(i);
94 PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
95 shouldThrow();
96 } catch (NullPointerException success) {}
97 }
98
99 /**
100 * Queue contains all elements of collection used to initialize
101 */
102 public void testConstructor6() {
103 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 }
110
111 /**
112 * The comparator used in constructor is used
113 */
114 public void testConstructor7() {
115 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 }
125
126 /**
127 * isEmpty is true before add, false after
128 */
129 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 /**
141 * size changes when elements added and removed
142 */
143 public void testSize() {
144 PriorityQueue q = populatedQueue(SIZE);
145 for (int i = 0; i < SIZE; ++i) {
146 assertEquals(SIZE-i, q.size());
147 q.remove();
148 }
149 for (int i = 0; i < SIZE; ++i) {
150 assertEquals(i, q.size());
151 q.add(new Integer(i));
152 }
153 }
154
155 /**
156 * offer(null) throws NPE
157 */
158 public void testOfferNull() {
159 try {
160 PriorityQueue q = new PriorityQueue(1);
161 q.offer(null);
162 shouldThrow();
163 } catch (NullPointerException success) {}
164 }
165
166 /**
167 * add(null) throws NPE
168 */
169 public void testAddNull() {
170 try {
171 PriorityQueue q = new PriorityQueue(1);
172 q.add(null);
173 shouldThrow();
174 } catch (NullPointerException success) {}
175 }
176
177 /**
178 * Offer of comparable element succeeds
179 */
180 public void testOffer() {
181 PriorityQueue q = new PriorityQueue(1);
182 assertTrue(q.offer(zero));
183 assertTrue(q.offer(one));
184 }
185
186 /**
187 * Offer of non-Comparable throws CCE
188 */
189 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 shouldThrow();
196 } catch (ClassCastException success) {}
197 }
198
199 /**
200 * add of comparable succeeds
201 */
202 public void testAdd() {
203 PriorityQueue q = new PriorityQueue(SIZE);
204 for (int i = 0; i < SIZE; ++i) {
205 assertEquals(i, q.size());
206 assertTrue(q.add(new Integer(i)));
207 }
208 }
209
210 /**
211 * addAll(null) throws NPE
212 */
213 public void testAddAll1() {
214 try {
215 PriorityQueue q = new PriorityQueue(1);
216 q.addAll(null);
217 shouldThrow();
218 } catch (NullPointerException success) {}
219 }
220 /**
221 * addAll of a collection with null elements throws NPE
222 */
223 public void testAddAll2() {
224 try {
225 PriorityQueue q = new PriorityQueue(SIZE);
226 Integer[] ints = new Integer[SIZE];
227 q.addAll(Arrays.asList(ints));
228 shouldThrow();
229 } catch (NullPointerException success) {}
230 }
231 /**
232 * addAll of a collection with any null elements throws NPE after
233 * possibly adding some elements
234 */
235 public void testAddAll3() {
236 try {
237 PriorityQueue q = new PriorityQueue(SIZE);
238 Integer[] ints = new Integer[SIZE];
239 for (int i = 0; i < SIZE-1; ++i)
240 ints[i] = new Integer(i);
241 q.addAll(Arrays.asList(ints));
242 shouldThrow();
243 } catch (NullPointerException success) {}
244 }
245
246 /**
247 * Queue contains all elements of successful addAll
248 */
249 public void testAddAll5() {
250 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 }
260
261 /**
262 * poll succeeds unless empty
263 */
264 public void testPoll() {
265 PriorityQueue q = populatedQueue(SIZE);
266 for (int i = 0; i < SIZE; ++i) {
267 assertEquals(i, ((Integer)q.poll()).intValue());
268 }
269 assertNull(q.poll());
270 }
271
272 /**
273 * peek returns next element, or null if empty
274 */
275 public void testPeek() {
276 PriorityQueue q = populatedQueue(SIZE);
277 for (int i = 0; i < SIZE; ++i) {
278 assertEquals(i, ((Integer)q.peek()).intValue());
279 q.poll();
280 assertTrue(q.peek() == null ||
281 i != ((Integer)q.peek()).intValue());
282 }
283 assertNull(q.peek());
284 }
285
286 /**
287 * element returns next element, or throws NSEE if empty
288 */
289 public void testElement() {
290 PriorityQueue q = populatedQueue(SIZE);
291 for (int i = 0; i < SIZE; ++i) {
292 assertEquals(i, ((Integer)q.element()).intValue());
293 q.poll();
294 }
295 try {
296 q.element();
297 shouldThrow();
298 } catch (NoSuchElementException success) {}
299 }
300
301 /**
302 * remove removes next element, or throws NSEE if empty
303 */
304 public void testRemove() {
305 PriorityQueue q = populatedQueue(SIZE);
306 for (int i = 0; i < SIZE; ++i) {
307 assertEquals(i, ((Integer)q.remove()).intValue());
308 }
309 try {
310 q.remove();
311 shouldThrow();
312 } catch (NoSuchElementException success) {}
313 }
314
315 /**
316 * remove(x) removes x and returns true if present
317 */
318 public void testRemoveElement() {
319 PriorityQueue q = populatedQueue(SIZE);
320 for (int i = 1; i < SIZE; i+=2) {
321 assertTrue(q.remove(new Integer(i)));
322 }
323 for (int i = 0; i < SIZE; i+=2) {
324 assertTrue(q.remove(new Integer(i)));
325 assertFalse(q.remove(new Integer(i+1)));
326 }
327 assertTrue(q.isEmpty());
328 }
329
330 /**
331 * contains(x) reports true when elements added but not yet removed
332 */
333 public void testContains() {
334 PriorityQueue q = populatedQueue(SIZE);
335 for (int i = 0; i < SIZE; ++i) {
336 assertTrue(q.contains(new Integer(i)));
337 q.poll();
338 assertFalse(q.contains(new Integer(i)));
339 }
340 }
341
342 /**
343 * clear removes all elements
344 */
345 public void testClear() {
346 PriorityQueue q = populatedQueue(SIZE);
347 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 /**
357 * containsAll(c) is true when c contains a subset of elements
358 */
359 public void testContainsAll() {
360 PriorityQueue q = populatedQueue(SIZE);
361 PriorityQueue p = new PriorityQueue(SIZE);
362 for (int i = 0; i < SIZE; ++i) {
363 assertTrue(q.containsAll(p));
364 assertFalse(p.containsAll(q));
365 p.add(new Integer(i));
366 }
367 assertTrue(p.containsAll(q));
368 }
369
370 /**
371 * retainAll(c) retains only those elements of c and reports true if changed
372 */
373 public void testRetainAll() {
374 PriorityQueue q = populatedQueue(SIZE);
375 PriorityQueue p = populatedQueue(SIZE);
376 for (int i = 0; i < SIZE; ++i) {
377 boolean changed = q.retainAll(p);
378 if (i == 0)
379 assertFalse(changed);
380 else
381 assertTrue(changed);
382
383 assertTrue(q.containsAll(p));
384 assertEquals(SIZE-i, q.size());
385 p.remove();
386 }
387 }
388
389 /**
390 * removeAll(c) removes only those elements of c and reports true if changed
391 */
392 public void testRemoveAll() {
393 for (int i = 1; i < SIZE; ++i) {
394 PriorityQueue q = populatedQueue(SIZE);
395 PriorityQueue p = populatedQueue(i);
396 assertTrue(q.removeAll(p));
397 assertEquals(SIZE-i, q.size());
398 for (int j = 0; j < i; ++j) {
399 Integer I = (Integer)(p.remove());
400 assertFalse(q.contains(I));
401 }
402 }
403 }
404
405 /**
406 * toArray contains all elements
407 */
408 public void testToArray() {
409 PriorityQueue q = populatedQueue(SIZE);
410 Object[] o = q.toArray();
411 Arrays.sort(o);
412 for (int i = 0; i < o.length; i++)
413 assertEquals(o[i], q.poll());
414 }
415
416 /**
417 * toArray(a) contains all elements
418 */
419 public void testToArray2() {
420 PriorityQueue q = populatedQueue(SIZE);
421 Integer[] ints = new Integer[SIZE];
422 ints = (Integer[])q.toArray(ints);
423 Arrays.sort(ints);
424 for (int i = 0; i < ints.length; i++)
425 assertEquals(ints[i], q.poll());
426 }
427
428 /**
429 * iterator iterates through all elements
430 */
431 public void testIterator() {
432 PriorityQueue q = populatedQueue(SIZE);
433 int i = 0;
434 Iterator it = q.iterator();
435 while (it.hasNext()) {
436 assertTrue(q.contains(it.next()));
437 ++i;
438 }
439 assertEquals(i, SIZE);
440 }
441
442 /**
443 * iterator.remove removes current element
444 */
445 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 /**
463 * toString contains toStrings of elements
464 */
465 public void testToString() {
466 PriorityQueue q = populatedQueue(SIZE);
467 String s = q.toString();
468 for (int i = 0; i < SIZE; ++i) {
469 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
470 }
471 }
472
473 /**
474 * A deserialized serialized queue has same elements
475 */
476 public void testSerialization() throws Exception {
477 PriorityQueue q = populatedQueue(SIZE);
478 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 }
490 }