ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.38
Committed: Sun Oct 16 20:44:18 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.37: +2 -1 lines
Log Message:
improve populatedFoo methods

File Contents

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