ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +137 -40 lines
Log Message:
Documentation scaffolding

File Contents

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