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.3 by dl, Sun Sep 14 20:42:40 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 >            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 = N-1; i >= 0; --i)
113 >            for (int i = SIZE-1; i >= 0; --i)
114                  assertEquals(ints[i], q.poll());
115          }
116          finally {}
# Line 133 | Line 129 | public class PriorityBlockingQueueTest e
129      }
130  
131      public void testRemainingCapacity(){
132 <        PriorityBlockingQueue q = fullQueue(N);
133 <        for (int i = 0; i < N; ++i) {
132 >        PriorityBlockingQueue q = populatedQueue(SIZE);
133 >        for (int i = 0; i < SIZE; ++i) {
134              assertEquals(NOCAP, q.remainingCapacity());
135 <            assertEquals(N-i, q.size());
135 >            assertEquals(SIZE-i, q.size());
136              q.remove();
137          }
138 <        for (int i = 0; i < N; ++i) {
138 >        for (int i = 0; i < SIZE; ++i) {
139              assertEquals(NOCAP, q.remainingCapacity());
140              assertEquals(i, q.size());
141              q.add(new Integer(i));
# Line 172 | Line 168 | public class PriorityBlockingQueueTest e
168      }
169  
170      public void testAdd(){
171 <        PriorityBlockingQueue q = new PriorityBlockingQueue(N);
172 <        for (int i = 0; i < N; ++i) {
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          }
# Line 189 | Line 185 | public class PriorityBlockingQueueTest e
185      }
186      public void testAddAll2(){
187          try {
188 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
189 <            Integer[] ints = new Integer[N];
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          }
# Line 198 | Line 194 | public class PriorityBlockingQueueTest e
194      }
195      public void testAddAll3(){
196          try {
197 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
198 <            Integer[] ints = new Integer[N];
199 <            for (int i = 0; i < N-1; ++i)
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");
# Line 211 | Line 207 | public class PriorityBlockingQueueTest e
207      public void testAddAll5(){
208          try {
209              Integer[] empty = new Integer[0];
210 <            Integer[] ints = new Integer[N];
211 <            for (int i = N-1; i >= 0; --i)
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(N);
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 < N; ++i)
216 >            for (int i = 0; i < SIZE; ++i)
217                  assertEquals(ints[i], q.poll());
218          }
219          finally {}
# Line 225 | Line 221 | public class PriorityBlockingQueueTest e
221  
222       public void testPutNull() {
223          try {
224 <            PriorityBlockingQueue q = new PriorityBlockingQueue(N);
224 >            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
225              q.put(null);
226              fail("put should throw NPE");
227          }
# Line 235 | Line 231 | public class PriorityBlockingQueueTest e
231  
232       public void testPut() {
233           try {
234 <             PriorityBlockingQueue q = new PriorityBlockingQueue(N);
235 <             for (int i = 0; i < N; ++i) {
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(N, q.size());
240 >             assertEquals(SIZE, q.size());
241           }
242           finally {
243          }
# Line 261 | Line 257 | public class PriorityBlockingQueueTest e
257                          ++added;
258                          q.put(new Integer(0));
259                          ++added;
260 <                        assertTrue(added == 4);
260 >                        threadAssertTrue(added == 4);
261                      } finally {
262                      }
263                  }
# Line 284 | Line 280 | public class PriorityBlockingQueueTest e
280                      try {
281                          q.put(new Integer(0));
282                          q.put(new Integer(0));
283 <                        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
284 <                        assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
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(SHORT_DELAY_MS);
291 >            Thread.sleep(SMALL_DELAY_MS);
292              t.interrupt();
293              t.join();
294          } catch (Exception e){
# Line 302 | Line 298 | public class PriorityBlockingQueueTest e
298  
299      public void testTake(){
300          try {
301 <            PriorityBlockingQueue q = fullQueue(N);
302 <            for (int i = 0; i < N; ++i) {
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){
# Line 317 | Line 313 | public class PriorityBlockingQueueTest e
313                  public void run(){
314                      try {
315                          q.take();
316 <                        fail("Should block");
316 >                        threadFail("Should block");
317                      } catch (InterruptedException success){ }                
318                  }
319              });
# Line 335 | Line 331 | public class PriorityBlockingQueueTest e
331          Thread t = new Thread(new Runnable() {
332                  public void run() {
333                      try {
334 <                        PriorityBlockingQueue q = fullQueue(N);
335 <                        for (int i = 0; i < N; ++i) {
336 <                            assertEquals(i, ((Integer)q.take()).intValue());
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 <                        fail("take should block");
339 >                        threadFail("take should block");
340                      } catch (InterruptedException success){
341                      }  
342                  }});
# Line 357 | Line 353 | public class PriorityBlockingQueueTest e
353  
354  
355      public void testPoll(){
356 <        PriorityBlockingQueue q = fullQueue(N);
357 <        for (int i = 0; i < N; ++i) {
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());
# Line 366 | Line 362 | public class PriorityBlockingQueueTest e
362  
363      public void testTimedPoll0() {
364          try {
365 <            PriorityBlockingQueue q = fullQueue(N);
366 <            for (int i = 0; i < N; ++i) {
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));
# Line 378 | Line 374 | public class PriorityBlockingQueueTest e
374  
375      public void testTimedPoll() {
376          try {
377 <            PriorityBlockingQueue q = fullQueue(N);
378 <            for (int i = 0; i < N; ++i) {
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));
# Line 392 | Line 388 | public class PriorityBlockingQueueTest e
388          Thread t = new Thread(new Runnable() {
389                  public void run() {
390                      try {
391 <                        PriorityBlockingQueue q = fullQueue(N);
392 <                        for (int i = 0; i < N; ++i) {
393 <                            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
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 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
395 >                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
396                      } catch (InterruptedException success){
397                      }  
398                  }});
# Line 416 | Line 412 | public class PriorityBlockingQueueTest e
412          Thread t = new Thread(new Runnable() {
413                  public void run(){
414                      try {
415 <                        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
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 <                        fail("Should block");
418 >                        threadFail("Should block");
419                      } catch (InterruptedException success) { }                
420                  }
421              });
422          try {
423              t.start();
424 <            Thread.sleep(SHORT_DELAY_MS * 2);
424 >            Thread.sleep(SMALL_DELAY_MS);
425              assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
426              t.interrupt();
427              t.join();
# Line 436 | Line 432 | public class PriorityBlockingQueueTest e
432  
433  
434      public void testPeek(){
435 <        PriorityBlockingQueue q = fullQueue(N);
436 <        for (int i = 0; i < N; ++i) {
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 ||
# Line 447 | Line 443 | public class PriorityBlockingQueueTest e
443      }
444  
445      public void testElement(){
446 <        PriorityBlockingQueue q = fullQueue(N);
447 <        for (int i = 0; i < N; ++i) {
446 >        PriorityBlockingQueue q = populatedQueue(SIZE);
447 >        for (int i = 0; i < SIZE; ++i) {
448              assertEquals(i, ((Integer)q.element()).intValue());
449              q.poll();
450          }
# Line 460 | Line 456 | public class PriorityBlockingQueueTest e
456      }
457  
458      public void testRemove(){
459 <        PriorityBlockingQueue q = fullQueue(N);
460 <        for (int i = 0; i < N; ++i) {
459 >        PriorityBlockingQueue q = populatedQueue(SIZE);
460 >        for (int i = 0; i < SIZE; ++i) {
461              assertEquals(i, ((Integer)q.remove()).intValue());
462          }
463          try {
# Line 472 | Line 468 | public class PriorityBlockingQueueTest e
468      }
469  
470      public void testRemoveElement(){
471 <        PriorityBlockingQueue q = fullQueue(N);
472 <        for (int i = 1; i < N; i+=2) {
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 < N; i+=2) {
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          }
# Line 484 | Line 480 | public class PriorityBlockingQueueTest e
480      }
481          
482      public void testContains(){
483 <        PriorityBlockingQueue q = fullQueue(N);
484 <        for (int i = 0; i < N; ++i) {
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)));
# Line 493 | Line 489 | public class PriorityBlockingQueueTest e
489      }
490  
491      public void testClear(){
492 <        PriorityBlockingQueue q = fullQueue(N);
492 >        PriorityBlockingQueue q = populatedQueue(SIZE);
493          q.clear();
494          assertTrue(q.isEmpty());
495          assertEquals(0, q.size());
# Line 505 | Line 501 | public class PriorityBlockingQueueTest e
501      }
502  
503      public void testContainsAll(){
504 <        PriorityBlockingQueue q = fullQueue(N);
505 <        PriorityBlockingQueue p = new PriorityBlockingQueue(N);
506 <        for (int i = 0; i < N; ++i) {
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));
# Line 516 | Line 512 | public class PriorityBlockingQueueTest e
512      }
513  
514      public void testRetainAll(){
515 <        PriorityBlockingQueue q = fullQueue(N);
516 <        PriorityBlockingQueue p = fullQueue(N);
517 <        for (int i = 0; i < N; ++i) {
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);
# Line 526 | Line 522 | public class PriorityBlockingQueueTest e
522                  assertTrue(changed);
523  
524              assertTrue(q.containsAll(p));
525 <            assertEquals(N-i, q.size());
525 >            assertEquals(SIZE-i, q.size());
526              p.remove();
527          }
528      }
529  
530      public void testRemoveAll(){
531 <        for (int i = 1; i < N; ++i) {
532 <            PriorityBlockingQueue q = fullQueue(N);
533 <            PriorityBlockingQueue p = fullQueue(i);
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(N-i, q.size());
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));
# Line 545 | Line 541 | public class PriorityBlockingQueueTest e
541      }
542  
543      public void testToArray(){
544 <        PriorityBlockingQueue q = fullQueue(N);
544 >        PriorityBlockingQueue q = populatedQueue(SIZE);
545          Object[] o = q.toArray();
546          Arrays.sort(o);
547          try {
# Line 557 | Line 553 | public class PriorityBlockingQueueTest e
553      }
554  
555      public void testToArray2(){
556 <        PriorityBlockingQueue q = fullQueue(N);
557 <        Integer[] ints = new Integer[N];
556 >        PriorityBlockingQueue q = populatedQueue(SIZE);
557 >        Integer[] ints = new Integer[SIZE];
558          ints = (Integer[])q.toArray(ints);
559          Arrays.sort(ints);
560          try {
# Line 570 | Line 566 | public class PriorityBlockingQueueTest e
566      }
567      
568      public void testIterator(){
569 <        PriorityBlockingQueue q = fullQueue(N);
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, N);
576 >        assertEquals(i, SIZE);
577      }
578  
579      public void testIteratorRemove () {
# Line 600 | Line 596 | public class PriorityBlockingQueueTest e
596  
597  
598      public void testToString(){
599 <        PriorityBlockingQueue q = fullQueue(N);
599 >        PriorityBlockingQueue q = populatedQueue(SIZE);
600          String s = q.toString();
601 <        for (int i = 0; i < N; ++i) {
601 >        for (int i = 0; i < SIZE; ++i) {
602              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
603          }
604      }        
# Line 615 | Line 611 | public class PriorityBlockingQueueTest e
611  
612          executor.execute(new Runnable() {
613              public void run() {
614 <                assertNull("poll should fail", q.poll());
614 >                threadAssertNull(q.poll());
615                  try {
616 <                    assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
617 <                    assertTrue(q.isEmpty());
616 >                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
617 >                    threadAssertTrue(q.isEmpty());
618                  }
619                  catch (InterruptedException e) {
620 <                    fail("should not be interrupted");
620 >                    threadFail("should not be interrupted");
621                  }
622              }
623          });
# Line 629 | Line 625 | public class PriorityBlockingQueueTest e
625          executor.execute(new Runnable() {
626              public void run() {
627                  try {
628 <                    Thread.sleep(MEDIUM_DELAY_MS);
628 >                    Thread.sleep(SMALL_DELAY_MS);
629                      q.put(new Integer(1));
630                  }
631                  catch (InterruptedException e) {
632 <                    fail("should not be interrupted");
632 >                    threadFail("should not be interrupted");
633                  }
634              }
635          });
636          
637 <        executor.shutdown();
637 >        joinPool(executor);
638  
639      }
640  
641      public void testSerialization() {
642 <        PriorityBlockingQueue q = fullQueue(N);
642 >        PriorityBlockingQueue q = populatedQueue(SIZE);
643          try {
644              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
645              ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines