ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.45
Committed: Sun May 6 22:09:42 2018 UTC (5 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.44: +10 -2 lines
Log Message:
Add tests for PriorityQueue with Comparator

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