ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.15
Committed: Sun Nov 22 18:57:17 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +8 -12 lines
Log Message:
use autoboxing judiciously for readability

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