ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.39
Committed: Tue Nov 22 16:54:27 2016 UTC (7 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +9 -1 lines
Log Message:
use CollectionImplementation

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