ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 00:31:57 2003 UTC

# Line 10 | Line 10 | import java.util.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class PriorityBlockingQueueTest extends TestCase {
14 <
15 <    private static final int N = 10;
16 <    private static final long SHORT_DELAY_MS = 100;
17 <    private static final long MEDIUM_DELAY_MS = 1000;
18 <    private static final long LONG_DELAY_MS = 10000;
19 <    private static final int NOCAP = Integer.MAX_VALUE;
20 <
13 > public class PriorityBlockingQueueTest extends JSR166TestCase {
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
16      }
# Line 26 | Line 19 | public class PriorityBlockingQueueTest e
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();
# Line 41 | Line 37 | public class PriorityBlockingQueueTest e
37       * Create a queue of given size containing consecutive
38       * Integers 0 ... n.
39       */
40 <    private PriorityBlockingQueue fullQueue(int n) {
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)
# Line 55 | Line 51 | public class PriorityBlockingQueueTest e
51      }
52  
53      public void testConstructor1(){
54 <        assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity());
54 >        assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
55      }
56  
57      public void testConstructor2(){
# Line 77 | Line 73 | public class PriorityBlockingQueueTest e
73  
74      public void testConstructor4(){
75          try {
76 <            Integer[] ints = new Integer[N];
76 >            Integer[] ints = new Integer[SIZE];
77              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
78              fail("Cannot make with null elements");
79          }
# Line 86 | Line 82 | public class PriorityBlockingQueueTest e
82  
83      public void testConstructor5(){
84          try {
85 <            Integer[] ints = new Integer[N];
86 <            for (int i = 0; i < N-1; ++i)
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");
# Line 97 | Line 93 | public class PriorityBlockingQueueTest e
93  
94      public void testConstructor6(){
95          try {
96 <            Integer[] ints = new Integer[N];
97 <            for (int i = 0; i < N; ++i)
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 < N; ++i)
100 >            for (int i = 0; i < SIZE; ++i)
101                  assertEquals(ints[i], q.poll());
102          }
103          finally {}
# Line 109 | Line 105 | public class PriorityBlockingQueueTest e
105  
106      public void testConstructor7(){
107          try {
108 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator());
109 <            Integer[] ints = new Integer[N];
110 <            for (int i = 0; i < N; ++i)
108 >            MyReverseComparator cmp = new MyReverseComparator();
109 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
110 >            assertEquals(cmp, q.comparator());
111 >            Integer[] ints = new Integer[SIZE];
112 >            for (int i = 0; i < SIZE; ++i)
113                  ints[i] = new Integer(i);
114              q.addAll(Arrays.asList(ints));
115 <            for (int i = N-1; i >= 0; --i)
115 >            for (int i = SIZE-1; i >= 0; --i)
116                  assertEquals(ints[i], q.poll());
117          }
118          finally {}
# Line 133 | Line 131 | public class PriorityBlockingQueueTest e
131      }
132  
133      public void testRemainingCapacity(){
134 <        PriorityBlockingQueue q = fullQueue(N);
135 <        for (int i = 0; i < N; ++i) {
134 >        PriorityBlockingQueue q = populatedQueue(SIZE);
135 >        for (int i = 0; i < SIZE; ++i) {
136              assertEquals(NOCAP, q.remainingCapacity());
137 <            assertEquals(N-i, q.size());
137 >            assertEquals(SIZE-i, q.size());
138              q.remove();
139          }
140 <        for (int i = 0; i < N; ++i) {
140 >        for (int i = 0; i < SIZE; ++i) {
141              assertEquals(NOCAP, q.remainingCapacity());
142              assertEquals(i, q.size());
143              q.add(new Integer(i));
# Line 172 | Line 170 | public class PriorityBlockingQueueTest e
170      }
171  
172      public void testAdd(){
173 <        PriorityBlockingQueue q = new PriorityBlockingQueue(N);
174 <        for (int i = 0; i < N; ++i) {
173 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
174 >        for (int i = 0; i < SIZE; ++i) {
175              assertEquals(i, q.size());
176              assertTrue(q.add(new Integer(i)));
177          }
# Line 189 | Line 187 | public class PriorityBlockingQueueTest e
187      }
188      public void testAddAll2(){
189          try {
190 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
191 <            Integer[] ints = new Integer[N];
190 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
191 >            Integer[] ints = new Integer[SIZE];
192              q.addAll(Arrays.asList(ints));
193              fail("Cannot add null elements");
194          }
# Line 198 | Line 196 | public class PriorityBlockingQueueTest e
196      }
197      public void testAddAll3(){
198          try {
199 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
200 <            Integer[] ints = new Integer[N];
201 <            for (int i = 0; i < N-1; ++i)
199 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
200 >            Integer[] ints = new Integer[SIZE];
201 >            for (int i = 0; i < SIZE-1; ++i)
202                  ints[i] = new Integer(i);
203              q.addAll(Arrays.asList(ints));
204              fail("Cannot add null elements");
# Line 211 | Line 209 | public class PriorityBlockingQueueTest e
209      public void testAddAll5(){
210          try {
211              Integer[] empty = new Integer[0];
212 <            Integer[] ints = new Integer[N];
213 <            for (int i = N-1; i >= 0; --i)
212 >            Integer[] ints = new Integer[SIZE];
213 >            for (int i = SIZE-1; i >= 0; --i)
214                  ints[i] = new Integer(i);
215 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
215 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
216              assertFalse(q.addAll(Arrays.asList(empty)));
217              assertTrue(q.addAll(Arrays.asList(ints)));
218 <            for (int i = 0; i < N; ++i)
218 >            for (int i = 0; i < SIZE; ++i)
219                  assertEquals(ints[i], q.poll());
220          }
221          finally {}
# Line 225 | Line 223 | public class PriorityBlockingQueueTest e
223  
224       public void testPutNull() {
225          try {
226 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
226 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
227              q.put(null);
228              fail("put should throw NPE");
229          }
# Line 235 | Line 233 | public class PriorityBlockingQueueTest e
233  
234       public void testPut() {
235           try {
236 <             PriorityBlockingQueue q = new PriorityBlockingQueue(N);
237 <             for (int i = 0; i < N; ++i) {
236 >             PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
237 >             for (int i = 0; i < SIZE; ++i) {
238                   Integer I = new Integer(i);
239                   q.put(I);
240                   assertTrue(q.contains(I));
241               }
242 <             assertEquals(N, q.size());
242 >             assertEquals(SIZE, q.size());
243           }
244           finally {
245          }
# Line 261 | Line 259 | public class PriorityBlockingQueueTest e
259                          ++added;
260                          q.put(new Integer(0));
261                          ++added;
262 <                        assertTrue(added == 4);
262 >                        threadAssertTrue(added == 4);
263                      } finally {
264                      }
265                  }
# Line 284 | Line 282 | public class PriorityBlockingQueueTest e
282                      try {
283                          q.put(new Integer(0));
284                          q.put(new Integer(0));
285 <                        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
286 <                        assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
285 >                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
286 >                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
287                      } finally { }
288                  }
289              });
290          
291          try {
292              t.start();
293 <            Thread.sleep(SHORT_DELAY_MS);
293 >            Thread.sleep(SMALL_DELAY_MS);
294              t.interrupt();
295              t.join();
296          } catch (Exception e){
# Line 302 | Line 300 | public class PriorityBlockingQueueTest e
300  
301      public void testTake(){
302          try {
303 <            PriorityBlockingQueue q = fullQueue(N);
304 <            for (int i = 0; i < N; ++i) {
303 >            PriorityBlockingQueue q = populatedQueue(SIZE);
304 >            for (int i = 0; i < SIZE; ++i) {
305                  assertEquals(i, ((Integer)q.take()).intValue());
306              }
307          } catch (InterruptedException e){
# Line 317 | Line 315 | public class PriorityBlockingQueueTest e
315                  public void run(){
316                      try {
317                          q.take();
318 <                        fail("Should block");
318 >                        threadFail("Should block");
319                      } catch (InterruptedException success){ }                
320                  }
321              });
# Line 335 | Line 333 | public class PriorityBlockingQueueTest e
333          Thread t = new Thread(new Runnable() {
334                  public void run() {
335                      try {
336 <                        PriorityBlockingQueue q = fullQueue(N);
337 <                        for (int i = 0; i < N; ++i) {
338 <                            assertEquals(i, ((Integer)q.take()).intValue());
336 >                        PriorityBlockingQueue q = populatedQueue(SIZE);
337 >                        for (int i = 0; i < SIZE; ++i) {
338 >                            threadAssertEquals(i, ((Integer)q.take()).intValue());
339                          }
340                          q.take();
341 <                        fail("take should block");
341 >                        threadFail("take should block");
342                      } catch (InterruptedException success){
343                      }  
344                  }});
# Line 357 | Line 355 | public class PriorityBlockingQueueTest e
355  
356  
357      public void testPoll(){
358 <        PriorityBlockingQueue q = fullQueue(N);
359 <        for (int i = 0; i < N; ++i) {
358 >        PriorityBlockingQueue q = populatedQueue(SIZE);
359 >        for (int i = 0; i < SIZE; ++i) {
360              assertEquals(i, ((Integer)q.poll()).intValue());
361          }
362          assertNull(q.poll());
# Line 366 | Line 364 | public class PriorityBlockingQueueTest e
364  
365      public void testTimedPoll0() {
366          try {
367 <            PriorityBlockingQueue q = fullQueue(N);
368 <            for (int i = 0; i < N; ++i) {
367 >            PriorityBlockingQueue q = populatedQueue(SIZE);
368 >            for (int i = 0; i < SIZE; ++i) {
369                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
370              }
371              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
# Line 378 | Line 376 | public class PriorityBlockingQueueTest e
376  
377      public void testTimedPoll() {
378          try {
379 <            PriorityBlockingQueue q = fullQueue(N);
380 <            for (int i = 0; i < N; ++i) {
379 >            PriorityBlockingQueue q = populatedQueue(SIZE);
380 >            for (int i = 0; i < SIZE; ++i) {
381                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
382              }
383              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
# Line 392 | Line 390 | public class PriorityBlockingQueueTest e
390          Thread t = new Thread(new Runnable() {
391                  public void run() {
392                      try {
393 <                        PriorityBlockingQueue q = fullQueue(N);
394 <                        for (int i = 0; i < N; ++i) {
395 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
393 >                        PriorityBlockingQueue q = populatedQueue(SIZE);
394 >                        for (int i = 0; i < SIZE; ++i) {
395 >                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
396                          }
397 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
397 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
398                      } catch (InterruptedException success){
399                      }  
400                  }});
# Line 416 | Line 414 | public class PriorityBlockingQueueTest e
414          Thread t = new Thread(new Runnable() {
415                  public void run(){
416                      try {
417 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
417 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
418                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
419                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
420 <                        fail("Should block");
420 >                        threadFail("Should block");
421                      } catch (InterruptedException success) { }                
422                  }
423              });
424          try {
425              t.start();
426 <            Thread.sleep(SHORT_DELAY_MS * 2);
426 >            Thread.sleep(SMALL_DELAY_MS);
427              assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
428              t.interrupt();
429              t.join();
# Line 436 | Line 434 | public class PriorityBlockingQueueTest e
434  
435  
436      public void testPeek(){
437 <        PriorityBlockingQueue q = fullQueue(N);
438 <        for (int i = 0; i < N; ++i) {
437 >        PriorityBlockingQueue q = populatedQueue(SIZE);
438 >        for (int i = 0; i < SIZE; ++i) {
439              assertEquals(i, ((Integer)q.peek()).intValue());
440              q.poll();
441              assertTrue(q.peek() == null ||
# Line 447 | Line 445 | public class PriorityBlockingQueueTest e
445      }
446  
447      public void testElement(){
448 <        PriorityBlockingQueue q = fullQueue(N);
449 <        for (int i = 0; i < N; ++i) {
448 >        PriorityBlockingQueue q = populatedQueue(SIZE);
449 >        for (int i = 0; i < SIZE; ++i) {
450              assertEquals(i, ((Integer)q.element()).intValue());
451              q.poll();
452          }
# Line 460 | Line 458 | public class PriorityBlockingQueueTest e
458      }
459  
460      public void testRemove(){
461 <        PriorityBlockingQueue q = fullQueue(N);
462 <        for (int i = 0; i < N; ++i) {
461 >        PriorityBlockingQueue q = populatedQueue(SIZE);
462 >        for (int i = 0; i < SIZE; ++i) {
463              assertEquals(i, ((Integer)q.remove()).intValue());
464          }
465          try {
# Line 472 | Line 470 | public class PriorityBlockingQueueTest e
470      }
471  
472      public void testRemoveElement(){
473 <        PriorityBlockingQueue q = fullQueue(N);
474 <        for (int i = 1; i < N; i+=2) {
473 >        PriorityBlockingQueue q = populatedQueue(SIZE);
474 >        for (int i = 1; i < SIZE; i+=2) {
475              assertTrue(q.remove(new Integer(i)));
476          }
477 <        for (int i = 0; i < N; i+=2) {
477 >        for (int i = 0; i < SIZE; i+=2) {
478              assertTrue(q.remove(new Integer(i)));
479              assertFalse(q.remove(new Integer(i+1)));
480          }
# Line 484 | Line 482 | public class PriorityBlockingQueueTest e
482      }
483          
484      public void testContains(){
485 <        PriorityBlockingQueue q = fullQueue(N);
486 <        for (int i = 0; i < N; ++i) {
485 >        PriorityBlockingQueue q = populatedQueue(SIZE);
486 >        for (int i = 0; i < SIZE; ++i) {
487              assertTrue(q.contains(new Integer(i)));
488              q.poll();
489              assertFalse(q.contains(new Integer(i)));
# Line 493 | Line 491 | public class PriorityBlockingQueueTest e
491      }
492  
493      public void testClear(){
494 <        PriorityBlockingQueue q = fullQueue(N);
494 >        PriorityBlockingQueue q = populatedQueue(SIZE);
495          q.clear();
496          assertTrue(q.isEmpty());
497          assertEquals(0, q.size());
# Line 505 | Line 503 | public class PriorityBlockingQueueTest e
503      }
504  
505      public void testContainsAll(){
506 <        PriorityBlockingQueue q = fullQueue(N);
507 <        PriorityBlockingQueue p = new PriorityBlockingQueue(N);
508 <        for (int i = 0; i < N; ++i) {
506 >        PriorityBlockingQueue q = populatedQueue(SIZE);
507 >        PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
508 >        for (int i = 0; i < SIZE; ++i) {
509              assertTrue(q.containsAll(p));
510              assertFalse(p.containsAll(q));
511              p.add(new Integer(i));
# Line 516 | Line 514 | public class PriorityBlockingQueueTest e
514      }
515  
516      public void testRetainAll(){
517 <        PriorityBlockingQueue q = fullQueue(N);
518 <        PriorityBlockingQueue p = fullQueue(N);
519 <        for (int i = 0; i < N; ++i) {
517 >        PriorityBlockingQueue q = populatedQueue(SIZE);
518 >        PriorityBlockingQueue p = populatedQueue(SIZE);
519 >        for (int i = 0; i < SIZE; ++i) {
520              boolean changed = q.retainAll(p);
521              if (i == 0)
522                  assertFalse(changed);
# Line 526 | Line 524 | public class PriorityBlockingQueueTest e
524                  assertTrue(changed);
525  
526              assertTrue(q.containsAll(p));
527 <            assertEquals(N-i, q.size());
527 >            assertEquals(SIZE-i, q.size());
528              p.remove();
529          }
530      }
531  
532      public void testRemoveAll(){
533 <        for (int i = 1; i < N; ++i) {
534 <            PriorityBlockingQueue q = fullQueue(N);
535 <            PriorityBlockingQueue p = fullQueue(i);
533 >        for (int i = 1; i < SIZE; ++i) {
534 >            PriorityBlockingQueue q = populatedQueue(SIZE);
535 >            PriorityBlockingQueue p = populatedQueue(i);
536              assertTrue(q.removeAll(p));
537 <            assertEquals(N-i, q.size());
537 >            assertEquals(SIZE-i, q.size());
538              for (int j = 0; j < i; ++j) {
539                  Integer I = (Integer)(p.remove());
540                  assertFalse(q.contains(I));
# Line 545 | Line 543 | public class PriorityBlockingQueueTest e
543      }
544  
545      public void testToArray(){
546 <        PriorityBlockingQueue q = fullQueue(N);
546 >        PriorityBlockingQueue q = populatedQueue(SIZE);
547          Object[] o = q.toArray();
548          Arrays.sort(o);
549          try {
# Line 557 | Line 555 | public class PriorityBlockingQueueTest e
555      }
556  
557      public void testToArray2(){
558 <        PriorityBlockingQueue q = fullQueue(N);
559 <        Integer[] ints = new Integer[N];
558 >        PriorityBlockingQueue q = populatedQueue(SIZE);
559 >        Integer[] ints = new Integer[SIZE];
560          ints = (Integer[])q.toArray(ints);
561          Arrays.sort(ints);
562          try {
# Line 570 | Line 568 | public class PriorityBlockingQueueTest e
568      }
569      
570      public void testIterator(){
571 <        PriorityBlockingQueue q = fullQueue(N);
571 >        PriorityBlockingQueue q = populatedQueue(SIZE);
572          int i = 0;
573          Iterator it = q.iterator();
574          while(it.hasNext()) {
575              assertTrue(q.contains(it.next()));
576              ++i;
577          }
578 <        assertEquals(i, N);
578 >        assertEquals(i, SIZE);
579      }
580  
581      public void testIteratorRemove () {
# Line 600 | Line 598 | public class PriorityBlockingQueueTest e
598  
599  
600      public void testToString(){
601 <        PriorityBlockingQueue q = fullQueue(N);
601 >        PriorityBlockingQueue q = populatedQueue(SIZE);
602          String s = q.toString();
603 <        for (int i = 0; i < N; ++i) {
603 >        for (int i = 0; i < SIZE; ++i) {
604              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
605          }
606      }        
# Line 615 | Line 613 | public class PriorityBlockingQueueTest e
613  
614          executor.execute(new Runnable() {
615              public void run() {
616 <                assertNull("poll should fail", q.poll());
616 >                threadAssertNull(q.poll());
617                  try {
618 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
619 <                    assertTrue(q.isEmpty());
618 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
619 >                    threadAssertTrue(q.isEmpty());
620                  }
621                  catch (InterruptedException e) {
622 <                    fail("should not be interrupted");
622 >                    threadFail("should not be interrupted");
623                  }
624              }
625          });
# Line 629 | Line 627 | public class PriorityBlockingQueueTest e
627          executor.execute(new Runnable() {
628              public void run() {
629                  try {
630 <                    Thread.sleep(MEDIUM_DELAY_MS);
630 >                    Thread.sleep(SMALL_DELAY_MS);
631                      q.put(new Integer(1));
632                  }
633                  catch (InterruptedException e) {
634 <                    fail("should not be interrupted");
634 >                    threadFail("should not be interrupted");
635                  }
636              }
637          });
638          
639 <        executor.shutdown();
639 >        joinPool(executor);
640  
641      }
642  
643      public void testSerialization() {
644 <        PriorityBlockingQueue q = fullQueue(N);
644 >        PriorityBlockingQueue q = populatedQueue(SIZE);
645          try {
646              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
647              ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines