ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 00:31:57 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +3 -1 lines
Log Message:
Added tests

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     public void testConstructor1(){
51 dl 1.3 assertEquals(0, new PriorityQueue(SIZE).size());
52 dl 1.1 }
53    
54     public void testConstructor2(){
55     try {
56     PriorityQueue q = new PriorityQueue(0);
57     fail("Cannot make zero-sized");
58     }
59     catch (IllegalArgumentException success) {}
60     }
61    
62     public void testConstructor3() {
63    
64     try {
65     PriorityQueue q = new PriorityQueue((Collection)null);
66     fail("Cannot make from null collection");
67     }
68     catch (NullPointerException success) {}
69     }
70    
71     public void testConstructor4(){
72     try {
73 dl 1.3 Integer[] ints = new Integer[SIZE];
74 dl 1.1 PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
75     fail("Cannot make with null elements");
76     }
77     catch (NullPointerException success) {}
78     }
79    
80     public void testConstructor5(){
81     try {
82 dl 1.3 Integer[] ints = new Integer[SIZE];
83     for (int i = 0; i < SIZE-1; ++i)
84 dl 1.1 ints[i] = new Integer(i);
85     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
86     fail("Cannot make with null elements");
87     }
88     catch (NullPointerException success) {}
89     }
90    
91     public void testConstructor6(){
92     try {
93 dl 1.3 Integer[] ints = new Integer[SIZE];
94     for (int i = 0; i < SIZE; ++i)
95 dl 1.1 ints[i] = new Integer(i);
96     PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
97 dl 1.3 for (int i = 0; i < SIZE; ++i)
98 dl 1.1 assertEquals(ints[i], q.poll());
99     }
100     finally {}
101     }
102    
103     public void testConstructor7(){
104     try {
105 dl 1.4 MyReverseComparator cmp = new MyReverseComparator();
106     PriorityQueue q = new PriorityQueue(SIZE, cmp);
107     assertEquals(cmp, q.comparator());
108 dl 1.3 Integer[] ints = new Integer[SIZE];
109     for (int i = 0; i < SIZE; ++i)
110 dl 1.1 ints[i] = new Integer(i);
111     q.addAll(Arrays.asList(ints));
112 dl 1.3 for (int i = SIZE-1; i >= 0; --i)
113 dl 1.1 assertEquals(ints[i], q.poll());
114     }
115     finally {}
116     }
117    
118     public void testEmpty() {
119     PriorityQueue q = new PriorityQueue(2);
120     assertTrue(q.isEmpty());
121     q.add(new Integer(1));
122     assertFalse(q.isEmpty());
123     q.add(new Integer(2));
124     q.remove();
125     q.remove();
126     assertTrue(q.isEmpty());
127     }
128    
129     public void testSize() {
130 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
131     for (int i = 0; i < SIZE; ++i) {
132     assertEquals(SIZE-i, q.size());
133 dl 1.1 q.remove();
134     }
135 dl 1.3 for (int i = 0; i < SIZE; ++i) {
136 dl 1.1 assertEquals(i, q.size());
137     q.add(new Integer(i));
138     }
139     }
140    
141     public void testOfferNull(){
142     try {
143     PriorityQueue q = new PriorityQueue(1);
144     q.offer(null);
145     fail("should throw NPE");
146     } catch (NullPointerException success) { }
147     }
148    
149     public void testOffer() {
150     PriorityQueue q = new PriorityQueue(1);
151     assertTrue(q.offer(new Integer(0)));
152     assertTrue(q.offer(new Integer(1)));
153     }
154    
155     public void testOfferNonComparable() {
156     try {
157     PriorityQueue q = new PriorityQueue(1);
158     q.offer(new Object());
159     q.offer(new Object());
160     q.offer(new Object());
161     fail("should throw CCE");
162     }
163     catch(ClassCastException success) {}
164     }
165    
166     public void testAdd(){
167 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
168     for (int i = 0; i < SIZE; ++i) {
169 dl 1.1 assertEquals(i, q.size());
170     assertTrue(q.add(new Integer(i)));
171     }
172     }
173    
174     public void testAddAll1(){
175     try {
176     PriorityQueue q = new PriorityQueue(1);
177     q.addAll(null);
178     fail("Cannot add null collection");
179     }
180     catch (NullPointerException success) {}
181     }
182     public void testAddAll2(){
183     try {
184 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
185     Integer[] ints = new Integer[SIZE];
186 dl 1.1 q.addAll(Arrays.asList(ints));
187     fail("Cannot add null elements");
188     }
189     catch (NullPointerException success) {}
190     }
191     public void testAddAll3(){
192     try {
193 dl 1.3 PriorityQueue q = new PriorityQueue(SIZE);
194     Integer[] ints = new Integer[SIZE];
195     for (int i = 0; i < SIZE-1; ++i)
196 dl 1.1 ints[i] = new Integer(i);
197     q.addAll(Arrays.asList(ints));
198     fail("Cannot add null elements");
199     }
200     catch (NullPointerException success) {}
201     }
202    
203     public void testAddAll5(){
204     try {
205     Integer[] empty = new Integer[0];
206 dl 1.3 Integer[] ints = new Integer[SIZE];
207     for (int i = 0; i < SIZE; ++i)
208     ints[i] = new Integer(SIZE-1-i);
209     PriorityQueue q = new PriorityQueue(SIZE);
210 dl 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
211     assertTrue(q.addAll(Arrays.asList(ints)));
212 dl 1.3 for (int i = 0; i < SIZE; ++i)
213 dl 1.1 assertEquals(new Integer(i), q.poll());
214     }
215     finally {}
216     }
217    
218     public void testPoll(){
219 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
220     for (int i = 0; i < SIZE; ++i) {
221 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
222     }
223     assertNull(q.poll());
224     }
225    
226     public void testPeek(){
227 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
228     for (int i = 0; i < SIZE; ++i) {
229 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
230     q.poll();
231     assertTrue(q.peek() == null ||
232     i != ((Integer)q.peek()).intValue());
233     }
234     assertNull(q.peek());
235     }
236    
237     public void testElement(){
238 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
239     for (int i = 0; i < SIZE; ++i) {
240 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
241     q.poll();
242     }
243     try {
244     q.element();
245     fail("no such element");
246     }
247     catch (NoSuchElementException success) {}
248     }
249    
250     public void testRemove(){
251 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
252     for (int i = 0; i < SIZE; ++i) {
253 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
254     }
255     try {
256     q.remove();
257     fail("remove should throw");
258     } catch (NoSuchElementException success){
259     }
260     }
261    
262     public void testRemoveElement(){
263 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
264     for (int i = 1; i < SIZE; i+=2) {
265 dl 1.1 assertTrue(q.remove(new Integer(i)));
266     }
267 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
268 dl 1.1 assertTrue(q.remove(new Integer(i)));
269     assertFalse(q.remove(new Integer(i+1)));
270     }
271 dl 1.2 assertTrue(q.isEmpty());
272 dl 1.1 }
273    
274     public void testContains(){
275 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
276     for (int i = 0; i < SIZE; ++i) {
277 dl 1.1 assertTrue(q.contains(new Integer(i)));
278     q.poll();
279     assertFalse(q.contains(new Integer(i)));
280     }
281     }
282    
283     public void testClear(){
284 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
285 dl 1.1 q.clear();
286     assertTrue(q.isEmpty());
287     assertEquals(0, q.size());
288     q.add(new Integer(1));
289     assertFalse(q.isEmpty());
290     q.clear();
291     assertTrue(q.isEmpty());
292     }
293    
294     public void testContainsAll(){
295 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
296     PriorityQueue p = new PriorityQueue(SIZE);
297     for (int i = 0; i < SIZE; ++i) {
298 dl 1.1 assertTrue(q.containsAll(p));
299     assertFalse(p.containsAll(q));
300     p.add(new Integer(i));
301     }
302     assertTrue(p.containsAll(q));
303     }
304    
305     public void testRetainAll(){
306 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
307     PriorityQueue p = populatedQueue(SIZE);
308     for (int i = 0; i < SIZE; ++i) {
309 dl 1.1 boolean changed = q.retainAll(p);
310     if (i == 0)
311     assertFalse(changed);
312     else
313     assertTrue(changed);
314    
315     assertTrue(q.containsAll(p));
316 dl 1.3 assertEquals(SIZE-i, q.size());
317 dl 1.1 p.remove();
318     }
319     }
320    
321     public void testRemoveAll(){
322 dl 1.3 for (int i = 1; i < SIZE; ++i) {
323     PriorityQueue q = populatedQueue(SIZE);
324     PriorityQueue p = populatedQueue(i);
325 dl 1.1 assertTrue(q.removeAll(p));
326 dl 1.3 assertEquals(SIZE-i, q.size());
327 dl 1.1 for (int j = 0; j < i; ++j) {
328     Integer I = (Integer)(p.remove());
329     assertFalse(q.contains(I));
330     }
331     }
332     }
333    
334     public void testToArray(){
335 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
336 dl 1.1 Object[] o = q.toArray();
337     Arrays.sort(o);
338     for(int i = 0; i < o.length; i++)
339     assertEquals(o[i], q.poll());
340     }
341    
342     public void testToArray2(){
343 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
344     Integer[] ints = new Integer[SIZE];
345 dl 1.1 ints = (Integer[])q.toArray(ints);
346     Arrays.sort(ints);
347     for(int i = 0; i < ints.length; i++)
348     assertEquals(ints[i], q.poll());
349     }
350    
351     public void testIterator(){
352 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
353 dl 1.1 int i = 0;
354     Iterator it = q.iterator();
355     while(it.hasNext()) {
356     assertTrue(q.contains(it.next()));
357     ++i;
358     }
359 dl 1.3 assertEquals(i, SIZE);
360 dl 1.1 }
361    
362     public void testIteratorRemove () {
363    
364     final PriorityQueue q = new PriorityQueue(3);
365    
366     q.add(new Integer(2));
367     q.add(new Integer(1));
368     q.add(new Integer(3));
369    
370     Iterator it = q.iterator();
371     it.next();
372     it.remove();
373    
374     it = q.iterator();
375     assertEquals(it.next(), new Integer(2));
376     assertEquals(it.next(), new Integer(3));
377     assertFalse(it.hasNext());
378     }
379    
380    
381     public void testToString(){
382 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
383 dl 1.1 String s = q.toString();
384 dl 1.3 for (int i = 0; i < SIZE; ++i) {
385 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
386     }
387     }
388    
389 dl 1.2 public void testSerialization() {
390 dl 1.3 PriorityQueue q = populatedQueue(SIZE);
391 dl 1.2 try {
392     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
393     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
394     out.writeObject(q);
395     out.close();
396    
397     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
398     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
399     PriorityQueue r = (PriorityQueue)in.readObject();
400     assertEquals(q.size(), r.size());
401     while (!q.isEmpty())
402     assertEquals(q.remove(), r.remove());
403     } catch(Exception e){
404     fail("unexpected exception");
405     }
406     }
407 dl 1.1 }