ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.2: +62 -67 lines
Log Message:
New base class JSR166TestCase

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