ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.47
Committed: Mon May 28 21:19:50 2018 UTC (5 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.46: +9 -6 lines
Log Message:
improve toArray tests

File Contents

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