ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +97 -101 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 PriorityBlockingQueueTest extends JSR166TestCase {
14 public static void main(String[] args) {
15 junit.textui.TestRunner.run (suite());
16 }
17
18 public static Test suite() {
19 return new TestSuite(PriorityBlockingQueueTest.class);
20 }
21
22 private static final int NOCAP = Integer.MAX_VALUE;
23
24 /** Sample Comparator */
25 static class MyReverseComparator implements Comparator {
26 public int compare(Object x, Object y) {
27 int i = ((Integer)x).intValue();
28 int j = ((Integer)y).intValue();
29 if (i < j) return 1;
30 if (i > j) return -1;
31 return 0;
32 }
33 }
34
35
36 /**
37 * Create a queue of given size containing consecutive
38 * Integers 0 ... n.
39 */
40 private PriorityBlockingQueue populatedQueue(int n) {
41 PriorityBlockingQueue q = new PriorityBlockingQueue(n);
42 assertTrue(q.isEmpty());
43 for(int i = n-1; i >= 0; i-=2)
44 assertTrue(q.offer(new Integer(i)));
45 for(int i = (n & 1); i < n; i+=2)
46 assertTrue(q.offer(new Integer(i)));
47 assertFalse(q.isEmpty());
48 assertEquals(NOCAP, q.remainingCapacity());
49 assertEquals(n, q.size());
50 return q;
51 }
52
53 public void testConstructor1(){
54 assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
55 }
56
57 public void testConstructor2(){
58 try {
59 PriorityBlockingQueue q = new PriorityBlockingQueue(0);
60 fail("Cannot make zero-sized");
61 }
62 catch (IllegalArgumentException success) {}
63 }
64
65 public void testConstructor3(){
66
67 try {
68 PriorityBlockingQueue q = new PriorityBlockingQueue(null);
69 fail("Cannot make from null collection");
70 }
71 catch (NullPointerException success) {}
72 }
73
74 public void testConstructor4(){
75 try {
76 Integer[] ints = new Integer[SIZE];
77 PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
78 fail("Cannot make with null elements");
79 }
80 catch (NullPointerException success) {}
81 }
82
83 public void testConstructor5(){
84 try {
85 Integer[] ints = new Integer[SIZE];
86 for (int i = 0; i < SIZE-1; ++i)
87 ints[i] = new Integer(i);
88 PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
89 fail("Cannot make with null elements");
90 }
91 catch (NullPointerException success) {}
92 }
93
94 public void testConstructor6(){
95 try {
96 Integer[] ints = new Integer[SIZE];
97 for (int i = 0; i < SIZE; ++i)
98 ints[i] = new Integer(i);
99 PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
100 for (int i = 0; i < SIZE; ++i)
101 assertEquals(ints[i], q.poll());
102 }
103 finally {}
104 }
105
106 public void testConstructor7(){
107 try {
108 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, new MyReverseComparator());
109 Integer[] ints = new Integer[SIZE];
110 for (int i = 0; i < SIZE; ++i)
111 ints[i] = new Integer(i);
112 q.addAll(Arrays.asList(ints));
113 for (int i = SIZE-1; i >= 0; --i)
114 assertEquals(ints[i], q.poll());
115 }
116 finally {}
117 }
118
119 public void testEmpty() {
120 PriorityBlockingQueue q = new PriorityBlockingQueue(2);
121 assertTrue(q.isEmpty());
122 assertEquals(NOCAP, q.remainingCapacity());
123 q.add(new Integer(1));
124 assertFalse(q.isEmpty());
125 q.add(new Integer(2));
126 q.remove();
127 q.remove();
128 assertTrue(q.isEmpty());
129 }
130
131 public void testRemainingCapacity(){
132 PriorityBlockingQueue q = populatedQueue(SIZE);
133 for (int i = 0; i < SIZE; ++i) {
134 assertEquals(NOCAP, q.remainingCapacity());
135 assertEquals(SIZE-i, q.size());
136 q.remove();
137 }
138 for (int i = 0; i < SIZE; ++i) {
139 assertEquals(NOCAP, q.remainingCapacity());
140 assertEquals(i, q.size());
141 q.add(new Integer(i));
142 }
143 }
144
145 public void testOfferNull(){
146 try {
147 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
148 q.offer(null);
149 fail("should throw NPE");
150 } catch (NullPointerException success) { }
151 }
152
153 public void testOffer() {
154 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
155 assertTrue(q.offer(new Integer(0)));
156 assertTrue(q.offer(new Integer(1)));
157 }
158
159 public void testOfferNonComparable() {
160 try {
161 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
162 q.offer(new Object());
163 q.offer(new Object());
164 q.offer(new Object());
165 fail("should throw CCE");
166 }
167 catch(ClassCastException success) {}
168 }
169
170 public void testAdd(){
171 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
172 for (int i = 0; i < SIZE; ++i) {
173 assertEquals(i, q.size());
174 assertTrue(q.add(new Integer(i)));
175 }
176 }
177
178 public void testAddAll1(){
179 try {
180 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
181 q.addAll(null);
182 fail("Cannot add null collection");
183 }
184 catch (NullPointerException success) {}
185 }
186 public void testAddAll2(){
187 try {
188 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
189 Integer[] ints = new Integer[SIZE];
190 q.addAll(Arrays.asList(ints));
191 fail("Cannot add null elements");
192 }
193 catch (NullPointerException success) {}
194 }
195 public void testAddAll3(){
196 try {
197 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
198 Integer[] ints = new Integer[SIZE];
199 for (int i = 0; i < SIZE-1; ++i)
200 ints[i] = new Integer(i);
201 q.addAll(Arrays.asList(ints));
202 fail("Cannot add null elements");
203 }
204 catch (NullPointerException success) {}
205 }
206
207 public void testAddAll5(){
208 try {
209 Integer[] empty = new Integer[0];
210 Integer[] ints = new Integer[SIZE];
211 for (int i = SIZE-1; i >= 0; --i)
212 ints[i] = new Integer(i);
213 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
214 assertFalse(q.addAll(Arrays.asList(empty)));
215 assertTrue(q.addAll(Arrays.asList(ints)));
216 for (int i = 0; i < SIZE; ++i)
217 assertEquals(ints[i], q.poll());
218 }
219 finally {}
220 }
221
222 public void testPutNull() {
223 try {
224 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
225 q.put(null);
226 fail("put should throw NPE");
227 }
228 catch (NullPointerException success){
229 }
230 }
231
232 public void testPut() {
233 try {
234 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
235 for (int i = 0; i < SIZE; ++i) {
236 Integer I = new Integer(i);
237 q.put(I);
238 assertTrue(q.contains(I));
239 }
240 assertEquals(SIZE, q.size());
241 }
242 finally {
243 }
244 }
245
246 public void testPutWithTake() {
247 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
248 Thread t = new Thread(new Runnable() {
249 public void run(){
250 int added = 0;
251 try {
252 q.put(new Integer(0));
253 ++added;
254 q.put(new Integer(0));
255 ++added;
256 q.put(new Integer(0));
257 ++added;
258 q.put(new Integer(0));
259 ++added;
260 threadAssertTrue(added == 4);
261 } finally {
262 }
263 }
264 });
265 try {
266 t.start();
267 Thread.sleep(SHORT_DELAY_MS);
268 q.take();
269 t.interrupt();
270 t.join();
271 } catch (Exception e){
272 fail("Unexpected exception");
273 }
274 }
275
276 public void testTimedOffer() {
277 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
278 Thread t = new Thread(new Runnable() {
279 public void run(){
280 try {
281 q.put(new Integer(0));
282 q.put(new Integer(0));
283 threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
284 threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
285 } finally { }
286 }
287 });
288
289 try {
290 t.start();
291 Thread.sleep(SMALL_DELAY_MS);
292 t.interrupt();
293 t.join();
294 } catch (Exception e){
295 fail("Unexpected exception");
296 }
297 }
298
299 public void testTake(){
300 try {
301 PriorityBlockingQueue q = populatedQueue(SIZE);
302 for (int i = 0; i < SIZE; ++i) {
303 assertEquals(i, ((Integer)q.take()).intValue());
304 }
305 } catch (InterruptedException e){
306 fail("Unexpected exception");
307 }
308 }
309
310 public void testTakeFromEmpty() {
311 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
312 Thread t = new Thread(new Runnable() {
313 public void run(){
314 try {
315 q.take();
316 threadFail("Should block");
317 } catch (InterruptedException success){ }
318 }
319 });
320 try {
321 t.start();
322 Thread.sleep(SHORT_DELAY_MS);
323 t.interrupt();
324 t.join();
325 } catch (Exception e){
326 fail("Unexpected exception");
327 }
328 }
329
330 public void testBlockingTake(){
331 Thread t = new Thread(new Runnable() {
332 public void run() {
333 try {
334 PriorityBlockingQueue q = populatedQueue(SIZE);
335 for (int i = 0; i < SIZE; ++i) {
336 threadAssertEquals(i, ((Integer)q.take()).intValue());
337 }
338 q.take();
339 threadFail("take should block");
340 } catch (InterruptedException success){
341 }
342 }});
343 t.start();
344 try {
345 Thread.sleep(SHORT_DELAY_MS);
346 t.interrupt();
347 t.join();
348 }
349 catch (InterruptedException ie) {
350 fail("Unexpected exception");
351 }
352 }
353
354
355 public void testPoll(){
356 PriorityBlockingQueue q = populatedQueue(SIZE);
357 for (int i = 0; i < SIZE; ++i) {
358 assertEquals(i, ((Integer)q.poll()).intValue());
359 }
360 assertNull(q.poll());
361 }
362
363 public void testTimedPoll0() {
364 try {
365 PriorityBlockingQueue q = populatedQueue(SIZE);
366 for (int i = 0; i < SIZE; ++i) {
367 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
368 }
369 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
370 } catch (InterruptedException e){
371 fail("Unexpected exception");
372 }
373 }
374
375 public void testTimedPoll() {
376 try {
377 PriorityBlockingQueue q = populatedQueue(SIZE);
378 for (int i = 0; i < SIZE; ++i) {
379 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
380 }
381 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
382 } catch (InterruptedException e){
383 fail("Unexpected exception");
384 }
385 }
386
387 public void testInterruptedTimedPoll(){
388 Thread t = new Thread(new Runnable() {
389 public void run() {
390 try {
391 PriorityBlockingQueue q = populatedQueue(SIZE);
392 for (int i = 0; i < SIZE; ++i) {
393 threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
394 }
395 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396 } catch (InterruptedException success){
397 }
398 }});
399 t.start();
400 try {
401 Thread.sleep(SHORT_DELAY_MS);
402 t.interrupt();
403 t.join();
404 }
405 catch (InterruptedException ie) {
406 fail("Unexpected exception");
407 }
408 }
409
410 public void testTimedPollWithOffer(){
411 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
412 Thread t = new Thread(new Runnable() {
413 public void run(){
414 try {
415 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
416 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
417 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
418 threadFail("Should block");
419 } catch (InterruptedException success) { }
420 }
421 });
422 try {
423 t.start();
424 Thread.sleep(SMALL_DELAY_MS);
425 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
426 t.interrupt();
427 t.join();
428 } catch (Exception e){
429 fail("Unexpected exception");
430 }
431 }
432
433
434 public void testPeek(){
435 PriorityBlockingQueue q = populatedQueue(SIZE);
436 for (int i = 0; i < SIZE; ++i) {
437 assertEquals(i, ((Integer)q.peek()).intValue());
438 q.poll();
439 assertTrue(q.peek() == null ||
440 i != ((Integer)q.peek()).intValue());
441 }
442 assertNull(q.peek());
443 }
444
445 public void testElement(){
446 PriorityBlockingQueue q = populatedQueue(SIZE);
447 for (int i = 0; i < SIZE; ++i) {
448 assertEquals(i, ((Integer)q.element()).intValue());
449 q.poll();
450 }
451 try {
452 q.element();
453 fail("no such element");
454 }
455 catch (NoSuchElementException success) {}
456 }
457
458 public void testRemove(){
459 PriorityBlockingQueue q = populatedQueue(SIZE);
460 for (int i = 0; i < SIZE; ++i) {
461 assertEquals(i, ((Integer)q.remove()).intValue());
462 }
463 try {
464 q.remove();
465 fail("remove should throw");
466 } catch (NoSuchElementException success){
467 }
468 }
469
470 public void testRemoveElement(){
471 PriorityBlockingQueue q = populatedQueue(SIZE);
472 for (int i = 1; i < SIZE; i+=2) {
473 assertTrue(q.remove(new Integer(i)));
474 }
475 for (int i = 0; i < SIZE; i+=2) {
476 assertTrue(q.remove(new Integer(i)));
477 assertFalse(q.remove(new Integer(i+1)));
478 }
479 assertTrue(q.isEmpty());
480 }
481
482 public void testContains(){
483 PriorityBlockingQueue q = populatedQueue(SIZE);
484 for (int i = 0; i < SIZE; ++i) {
485 assertTrue(q.contains(new Integer(i)));
486 q.poll();
487 assertFalse(q.contains(new Integer(i)));
488 }
489 }
490
491 public void testClear(){
492 PriorityBlockingQueue q = populatedQueue(SIZE);
493 q.clear();
494 assertTrue(q.isEmpty());
495 assertEquals(0, q.size());
496 assertEquals(NOCAP, q.remainingCapacity());
497 q.add(new Integer(1));
498 assertFalse(q.isEmpty());
499 q.clear();
500 assertTrue(q.isEmpty());
501 }
502
503 public void testContainsAll(){
504 PriorityBlockingQueue q = populatedQueue(SIZE);
505 PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
506 for (int i = 0; i < SIZE; ++i) {
507 assertTrue(q.containsAll(p));
508 assertFalse(p.containsAll(q));
509 p.add(new Integer(i));
510 }
511 assertTrue(p.containsAll(q));
512 }
513
514 public void testRetainAll(){
515 PriorityBlockingQueue q = populatedQueue(SIZE);
516 PriorityBlockingQueue p = populatedQueue(SIZE);
517 for (int i = 0; i < SIZE; ++i) {
518 boolean changed = q.retainAll(p);
519 if (i == 0)
520 assertFalse(changed);
521 else
522 assertTrue(changed);
523
524 assertTrue(q.containsAll(p));
525 assertEquals(SIZE-i, q.size());
526 p.remove();
527 }
528 }
529
530 public void testRemoveAll(){
531 for (int i = 1; i < SIZE; ++i) {
532 PriorityBlockingQueue q = populatedQueue(SIZE);
533 PriorityBlockingQueue p = populatedQueue(i);
534 assertTrue(q.removeAll(p));
535 assertEquals(SIZE-i, q.size());
536 for (int j = 0; j < i; ++j) {
537 Integer I = (Integer)(p.remove());
538 assertFalse(q.contains(I));
539 }
540 }
541 }
542
543 public void testToArray(){
544 PriorityBlockingQueue q = populatedQueue(SIZE);
545 Object[] o = q.toArray();
546 Arrays.sort(o);
547 try {
548 for(int i = 0; i < o.length; i++)
549 assertEquals(o[i], q.take());
550 } catch (InterruptedException e){
551 fail("Unexpected exception");
552 }
553 }
554
555 public void testToArray2(){
556 PriorityBlockingQueue q = populatedQueue(SIZE);
557 Integer[] ints = new Integer[SIZE];
558 ints = (Integer[])q.toArray(ints);
559 Arrays.sort(ints);
560 try {
561 for(int i = 0; i < ints.length; i++)
562 assertEquals(ints[i], q.take());
563 } catch (InterruptedException e){
564 fail("Unexpected exception");
565 }
566 }
567
568 public void testIterator(){
569 PriorityBlockingQueue q = populatedQueue(SIZE);
570 int i = 0;
571 Iterator it = q.iterator();
572 while(it.hasNext()) {
573 assertTrue(q.contains(it.next()));
574 ++i;
575 }
576 assertEquals(i, SIZE);
577 }
578
579 public void testIteratorRemove () {
580
581 final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
582
583 q.add(new Integer(2));
584 q.add(new Integer(1));
585 q.add(new Integer(3));
586
587 Iterator it = q.iterator();
588 it.next();
589 it.remove();
590
591 it = q.iterator();
592 assertEquals(it.next(), new Integer(2));
593 assertEquals(it.next(), new Integer(3));
594 assertFalse(it.hasNext());
595 }
596
597
598 public void testToString(){
599 PriorityBlockingQueue q = populatedQueue(SIZE);
600 String s = q.toString();
601 for (int i = 0; i < SIZE; ++i) {
602 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
603 }
604 }
605
606 public void testPollInExecutor() {
607
608 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
609
610 ExecutorService executor = Executors.newFixedThreadPool(2);
611
612 executor.execute(new Runnable() {
613 public void run() {
614 threadAssertNull(q.poll());
615 try {
616 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
617 threadAssertTrue(q.isEmpty());
618 }
619 catch (InterruptedException e) {
620 threadFail("should not be interrupted");
621 }
622 }
623 });
624
625 executor.execute(new Runnable() {
626 public void run() {
627 try {
628 Thread.sleep(SMALL_DELAY_MS);
629 q.put(new Integer(1));
630 }
631 catch (InterruptedException e) {
632 threadFail("should not be interrupted");
633 }
634 }
635 });
636
637 joinPool(executor);
638
639 }
640
641 public void testSerialization() {
642 PriorityBlockingQueue q = populatedQueue(SIZE);
643 try {
644 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
645 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
646 out.writeObject(q);
647 out.close();
648
649 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
650 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
651 PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
652 assertEquals(q.size(), r.size());
653 while (!q.isEmpty())
654 assertEquals(q.remove(), r.remove());
655 } catch(Exception e){
656 fail("unexpected exception");
657 }
658 }
659
660 }