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

# 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
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 dl 1.3 private PriorityQueue populatedQueue(int n) {
39 dl 1.1 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 dl 1.5 /**
51     *
52     */
53     public void testConstructor1() {
54 dl 1.3 assertEquals(0, new PriorityQueue(SIZE).size());
55 dl 1.1 }
56    
57 dl 1.5 /**
58     *
59     */
60     public void testConstructor2() {
61 dl 1.1 try {
62     PriorityQueue q = new PriorityQueue(0);
63 dl 1.5 shouldThrow();
64 dl 1.1 }
65     catch (IllegalArgumentException success) {}
66     }
67    
68 dl 1.5 /**
69     *
70     */
71 dl 1.1 public void testConstructor3() {
72    
73     try {
74     PriorityQueue q = new PriorityQueue((Collection)null);
75 dl 1.5 shouldThrow();
76 dl 1.1 }
77     catch (NullPointerException success) {}
78     }
79    
80 dl 1.5 /**
81     *
82     */
83     public void testConstructor4() {
84 dl 1.1 try {
85 dl 1.3 Integer[] ints = new Integer[SIZE];
86 dl 1.1 PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
87 dl 1.5 shouldThrow();
88 dl 1.1 }
89     catch (NullPointerException success) {}
90     }
91    
92 dl 1.5 /**
93     *
94     */
95     public void testConstructor5() {
96 dl 1.1 try {
97 dl 1.3 Integer[] ints = new Integer[SIZE];
98     for (int i = 0; i < SIZE-1; ++i)
99 dl 1.1 ints[i] = new Integer(i);
100     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
101 dl 1.5 shouldThrow();
102 dl 1.1 }
103     catch (NullPointerException success) {}
104     }
105    
106 dl 1.5 /**
107     *
108     */
109     public void testConstructor6() {
110 dl 1.1 try {
111 dl 1.3 Integer[] ints = new Integer[SIZE];
112     for (int i = 0; i < SIZE; ++i)
113 dl 1.1 ints[i] = new Integer(i);
114     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
115 dl 1.3 for (int i = 0; i < SIZE; ++i)
116 dl 1.1 assertEquals(ints[i], q.poll());
117     }
118     finally {}
119     }
120    
121 dl 1.5 /**
122     *
123     */
124     public void testConstructor7() {
125 dl 1.1 try {
126 dl 1.4 MyReverseComparator cmp = new MyReverseComparator();
127     PriorityQueue q = new PriorityQueue(SIZE, cmp);
128     assertEquals(cmp, q.comparator());
129 dl 1.3 Integer[] ints = new Integer[SIZE];
130     for (int i = 0; i < SIZE; ++i)
131 dl 1.1 ints[i] = new Integer(i);
132     q.addAll(Arrays.asList(ints));
133 dl 1.3 for (int i = SIZE-1; i >= 0; --i)
134 dl 1.1 assertEquals(ints[i], q.poll());
135     }
136     finally {}
137     }
138    
139 dl 1.5 /**
140     *
141     */
142 dl 1.1 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 dl 1.5 /**
154     *
155     */
156 dl 1.1 public void testSize() {
157 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
158     for (int i = 0; i < SIZE; ++i) {
159     assertEquals(SIZE-i, q.size());
160 dl 1.1 q.remove();
161     }
162 dl 1.3 for (int i = 0; i < SIZE; ++i) {
163 dl 1.1 assertEquals(i, q.size());
164     q.add(new Integer(i));
165     }
166     }
167    
168 dl 1.5 /**
169     *
170     */
171     public void testOfferNull() {
172 dl 1.1 try {
173     PriorityQueue q = new PriorityQueue(1);
174     q.offer(null);
175 dl 1.5 shouldThrow();
176 dl 1.1 } catch (NullPointerException success) { }
177     }
178    
179 dl 1.5 /**
180     *
181     */
182 dl 1.1 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 dl 1.5 /**
189     *
190     */
191 dl 1.1 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 dl 1.5 shouldThrow();
198 dl 1.1 }
199     catch(ClassCastException success) {}
200     }
201    
202 dl 1.5 /**
203     *
204     */
205     public void testAdd() {
206 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
207     for (int i = 0; i < SIZE; ++i) {
208 dl 1.1 assertEquals(i, q.size());
209     assertTrue(q.add(new Integer(i)));
210     }
211     }
212    
213 dl 1.5 /**
214     *
215     */
216     public void testAddAll1() {
217 dl 1.1 try {
218     PriorityQueue q = new PriorityQueue(1);
219     q.addAll(null);
220 dl 1.5 shouldThrow();
221 dl 1.1 }
222     catch (NullPointerException success) {}
223     }
224 dl 1.5 /**
225     *
226     */
227     public void testAddAll2() {
228 dl 1.1 try {
229 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
230     Integer[] ints = new Integer[SIZE];
231 dl 1.1 q.addAll(Arrays.asList(ints));
232 dl 1.5 shouldThrow();
233 dl 1.1 }
234     catch (NullPointerException success) {}
235     }
236 dl 1.5 /**
237     *
238     */
239     public void testAddAll3() {
240 dl 1.1 try {
241 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
242     Integer[] ints = new Integer[SIZE];
243     for (int i = 0; i < SIZE-1; ++i)
244 dl 1.1 ints[i] = new Integer(i);
245     q.addAll(Arrays.asList(ints));
246 dl 1.5 shouldThrow();
247 dl 1.1 }
248     catch (NullPointerException success) {}
249     }
250    
251 dl 1.5 /**
252     *
253     */
254     public void testAddAll5() {
255 dl 1.1 try {
256     Integer[] empty = new Integer[0];
257 dl 1.3 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 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
262     assertTrue(q.addAll(Arrays.asList(ints)));
263 dl 1.3 for (int i = 0; i < SIZE; ++i)
264 dl 1.1 assertEquals(new Integer(i), q.poll());
265     }
266     finally {}
267     }
268    
269 dl 1.5 /**
270     *
271     */
272     public void testPoll() {
273 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
274     for (int i = 0; i < SIZE; ++i) {
275 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
276     }
277     assertNull(q.poll());
278     }
279    
280 dl 1.5 /**
281     *
282     */
283     public void testPeek() {
284 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
285     for (int i = 0; i < SIZE; ++i) {
286 dl 1.1 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 dl 1.5 /**
295     *
296     */
297     public void testElement() {
298 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
299     for (int i = 0; i < SIZE; ++i) {
300 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
301     q.poll();
302     }
303     try {
304     q.element();
305 dl 1.5 shouldThrow();
306 dl 1.1 }
307     catch (NoSuchElementException success) {}
308     }
309    
310 dl 1.5 /**
311     *
312     */
313     public void testRemove() {
314 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
315     for (int i = 0; i < SIZE; ++i) {
316 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
317     }
318     try {
319     q.remove();
320 dl 1.5 shouldThrow();
321 dl 1.1 } catch (NoSuchElementException success){
322     }
323     }
324    
325 dl 1.5 /**
326     *
327     */
328     public void testRemoveElement() {
329 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
330     for (int i = 1; i < SIZE; i+=2) {
331 dl 1.1 assertTrue(q.remove(new Integer(i)));
332     }
333 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
334 dl 1.1 assertTrue(q.remove(new Integer(i)));
335     assertFalse(q.remove(new Integer(i+1)));
336     }
337 dl 1.2 assertTrue(q.isEmpty());
338 dl 1.1 }
339    
340 dl 1.5 /**
341     *
342     */
343     public void testContains() {
344 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
345     for (int i = 0; i < SIZE; ++i) {
346 dl 1.1 assertTrue(q.contains(new Integer(i)));
347     q.poll();
348     assertFalse(q.contains(new Integer(i)));
349     }
350     }
351    
352 dl 1.5 /**
353     *
354     */
355     public void testClear() {
356 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
357 dl 1.1 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 dl 1.5 /**
367     *
368     */
369     public void testContainsAll() {
370 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
371     PriorityQueue p = new PriorityQueue(SIZE);
372     for (int i = 0; i < SIZE; ++i) {
373 dl 1.1 assertTrue(q.containsAll(p));
374     assertFalse(p.containsAll(q));
375     p.add(new Integer(i));
376     }
377     assertTrue(p.containsAll(q));
378     }
379    
380 dl 1.5 /**
381     *
382     */
383     public void testRetainAll() {
384 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
385     PriorityQueue p = populatedQueue(SIZE);
386     for (int i = 0; i < SIZE; ++i) {
387 dl 1.1 boolean changed = q.retainAll(p);
388     if (i == 0)
389     assertFalse(changed);
390     else
391     assertTrue(changed);
392    
393     assertTrue(q.containsAll(p));
394 dl 1.3 assertEquals(SIZE-i, q.size());
395 dl 1.1 p.remove();
396     }
397     }
398    
399 dl 1.5 /**
400     *
401     */
402     public void testRemoveAll() {
403 dl 1.3 for (int i = 1; i < SIZE; ++i) {
404     PriorityQueue q = populatedQueue(SIZE);
405     PriorityQueue p = populatedQueue(i);
406 dl 1.1 assertTrue(q.removeAll(p));
407 dl 1.3 assertEquals(SIZE-i, q.size());
408 dl 1.1 for (int j = 0; j < i; ++j) {
409     Integer I = (Integer)(p.remove());
410     assertFalse(q.contains(I));
411     }
412     }
413     }
414    
415 dl 1.5 /**
416     *
417     */
418     public void testToArray() {
419 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
420 dl 1.1 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 dl 1.5 /**
427     *
428     */
429     public void testToArray2() {
430 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
431     Integer[] ints = new Integer[SIZE];
432 dl 1.1 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 dl 1.5 /**
439     *
440     */
441     public void testIterator() {
442 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
443 dl 1.1 int i = 0;
444     Iterator it = q.iterator();
445     while(it.hasNext()) {
446     assertTrue(q.contains(it.next()));
447     ++i;
448     }
449 dl 1.3 assertEquals(i, SIZE);
450 dl 1.1 }
451    
452 dl 1.5 /**
453     *
454     */
455 dl 1.1 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 dl 1.5 /**
473     *
474     */
475     public void testToString() {
476 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
477 dl 1.1 String s = q.toString();
478 dl 1.3 for (int i = 0; i < SIZE; ++i) {
479 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
480     }
481     }
482    
483 dl 1.5 /**
484     *
485     */
486 dl 1.2 public void testSerialization() {
487 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
488 dl 1.2 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 dl 1.5 unexpectedException();
502 dl 1.2 }
503     }
504 dl 1.1 }