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.9 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.14 by jsr166, Mon Nov 16 05:30:08 2009 UTC

# Line 2 | Line 2
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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 13 | Line 13 | import java.io.*;
13  
14   public class PriorityBlockingQueueTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run (suite());
17      }
18      public static Test suite() {
19          return new TestSuite(PriorityBlockingQueueTest.class);
# Line 22 | Line 22 | public class PriorityBlockingQueueTest e
22      private static final int NOCAP = Integer.MAX_VALUE;
23  
24      /** Sample Comparator */
25 <    static class MyReverseComparator implements 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();
# Line 39 | Line 39 | public class PriorityBlockingQueueTest e
39      private PriorityBlockingQueue populatedQueue(int n) {
40          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
41          assertTrue(q.isEmpty());
42 <        for(int i = n-1; i >= 0; i-=2)
42 >        for (int i = n-1; i >= 0; i-=2)
43              assertTrue(q.offer(new Integer(i)));
44 <        for(int i = (n & 1); i < n; i+=2)
44 >        for (int i = (n & 1); i < n; i+=2)
45              assertTrue(q.offer(new Integer(i)));
46          assertFalse(q.isEmpty());
47          assertEquals(NOCAP, q.remainingCapacity());
48          assertEquals(n, q.size());
49          return q;
50      }
51 <
51 >
52      /**
53       * A new queue has unbounded capacity
54       */
# Line 178 | Line 178 | public class PriorityBlockingQueueTest e
178              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
179              q.offer(null);
180              shouldThrow();
181 <        } catch (NullPointerException success) { }  
181 >        } catch (NullPointerException success) { }
182      }
183  
184      /**
# Line 189 | Line 189 | public class PriorityBlockingQueueTest e
189              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
190              q.add(null);
191              shouldThrow();
192 <        } catch (NullPointerException success) { }  
192 >        } catch (NullPointerException success) { }
193      }
194  
195      /**
# Line 212 | Line 212 | public class PriorityBlockingQueueTest e
212              q.offer(new Object());
213              shouldThrow();
214          }
215 <        catch(ClassCastException success) {}
215 >        catch (ClassCastException success) {}
216      }
217  
218      /**
# Line 304 | Line 304 | public class PriorityBlockingQueueTest e
304              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
305              q.put(null);
306              shouldThrow();
307 <        }
308 <        catch (NullPointerException success){
309 <        }  
307 >        }
308 >        catch (NullPointerException success) {
309 >        }
310       }
311  
312      /**
# Line 354 | Line 354 | public class PriorityBlockingQueueTest e
354              q.take();
355              t.interrupt();
356              t.join();
357 <        } catch (Exception e){
357 >        } catch (Exception e) {
358              unexpectedException();
359          }
360      }
# Line 374 | Line 374 | public class PriorityBlockingQueueTest e
374                      } finally { }
375                  }
376              });
377 <        
377 >
378          try {
379              t.start();
380              Thread.sleep(SMALL_DELAY_MS);
381              t.interrupt();
382              t.join();
383 <        } catch (Exception e){
383 >        } catch (Exception e) {
384              unexpectedException();
385          }
386      }
# Line 394 | Line 394 | public class PriorityBlockingQueueTest e
394              for (int i = 0; i < SIZE; ++i) {
395                  assertEquals(i, ((Integer)q.take()).intValue());
396              }
397 <        } catch (InterruptedException e){
397 >        } catch (InterruptedException e) {
398              unexpectedException();
399 <        }  
399 >        }
400      }
401  
402      /**
# Line 409 | Line 409 | public class PriorityBlockingQueueTest e
409                      try {
410                          q.take();
411                          threadShouldThrow();
412 <                    } catch (InterruptedException success){ }                
412 >                    } catch (InterruptedException success) { }
413                  }
414              });
415          try {
# Line 417 | Line 417 | public class PriorityBlockingQueueTest e
417              Thread.sleep(SHORT_DELAY_MS);
418              t.interrupt();
419              t.join();
420 <        } catch (Exception e){
420 >        } catch (Exception e) {
421              unexpectedException();
422          }
423      }
# Line 435 | Line 435 | public class PriorityBlockingQueueTest e
435                          }
436                          q.take();
437                          threadShouldThrow();
438 <                    } catch (InterruptedException success){
439 <                    }  
438 >                    } catch (InterruptedException success) {
439 >                    }
440                  }});
441          t.start();
442 <        try {
443 <           Thread.sleep(SHORT_DELAY_MS);
442 >        try {
443 >           Thread.sleep(SHORT_DELAY_MS);
444             t.interrupt();
445             t.join();
446          }
# Line 471 | Line 471 | public class PriorityBlockingQueueTest e
471                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
472              }
473              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
474 <        } catch (InterruptedException e){
474 >        } catch (InterruptedException e) {
475              unexpectedException();
476 <        }  
476 >        }
477      }
478  
479      /**
# Line 486 | Line 486 | public class PriorityBlockingQueueTest e
486                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
487              }
488              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
489 <        } catch (InterruptedException e){
489 >        } catch (InterruptedException e) {
490              unexpectedException();
491 <        }  
491 >        }
492      }
493  
494      /**
# Line 504 | Line 504 | public class PriorityBlockingQueueTest e
504                              threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
505                          }
506                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
507 <                    } catch (InterruptedException success){
508 <                    }  
507 >                    } catch (InterruptedException success) {
508 >                    }
509                  }});
510          t.start();
511 <        try {
512 <           Thread.sleep(SHORT_DELAY_MS);
511 >        try {
512 >           Thread.sleep(SHORT_DELAY_MS);
513             t.interrupt();
514             t.join();
515          }
# Line 531 | Line 531 | public class PriorityBlockingQueueTest e
531                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
532                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
533                          threadShouldThrow();
534 <                    } catch (InterruptedException success) { }                
534 >                    } catch (InterruptedException success) { }
535                  }
536              });
537          try {
# Line 540 | Line 540 | public class PriorityBlockingQueueTest e
540              assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
541              t.interrupt();
542              t.join();
543 <        } catch (Exception e){
543 >        } catch (Exception e) {
544              unexpectedException();
545          }
546 <    }  
546 >    }
547  
548  
549      /**
# Line 587 | Line 587 | public class PriorityBlockingQueueTest e
587          try {
588              q.remove();
589              shouldThrow();
590 <        } catch (NoSuchElementException success){
591 <        }  
590 >        } catch (NoSuchElementException success) {
591 >        }
592      }
593  
594      /**
# Line 605 | Line 605 | public class PriorityBlockingQueueTest e
605          }
606          assertTrue(q.isEmpty());
607      }
608 <        
608 >
609      /**
610       * contains(x) reports true when elements added but not yet removed
611       */
# Line 626 | Line 626 | public class PriorityBlockingQueueTest e
626          q.clear();
627          assertTrue(q.isEmpty());
628          assertEquals(0, q.size());
629 <        assertEquals(NOCAP, q.remainingCapacity());
630 <        q.add(new Integer(1));
629 >        q.add(one);
630          assertFalse(q.isEmpty());
631 +        assertTrue(q.contains(one));
632          q.clear();
633          assertTrue(q.isEmpty());
634      }
# Line 690 | Line 690 | public class PriorityBlockingQueueTest e
690          Object[] o = q.toArray();
691          Arrays.sort(o);
692          try {
693 <        for(int i = 0; i < o.length; i++)
693 >        for (int i = 0; i < o.length; i++)
694              assertEquals(o[i], q.take());
695 <        } catch (InterruptedException e){
695 >        } catch (InterruptedException e) {
696              unexpectedException();
697 <        }    
697 >        }
698      }
699  
700      /**
# Line 706 | Line 706 | public class PriorityBlockingQueueTest e
706          ints = (Integer[])q.toArray(ints);
707          Arrays.sort(ints);
708          try {
709 <            for(int i = 0; i < ints.length; i++)
709 >            for (int i = 0; i < ints.length; i++)
710                  assertEquals(ints[i], q.take());
711 <        } catch (InterruptedException e){
711 >        } catch (InterruptedException e) {
712              unexpectedException();
713 <        }    
713 >        }
714      }
715  
716      /**
# Line 721 | Line 721 | public class PriorityBlockingQueueTest e
721              PriorityBlockingQueue q = populatedQueue(SIZE);
722              Object o[] = q.toArray(null);
723              shouldThrow();
724 <        } catch(NullPointerException success){}
724 >        } catch (NullPointerException success) {}
725      }
726  
727      /**
# Line 732 | Line 732 | public class PriorityBlockingQueueTest e
732              PriorityBlockingQueue q = populatedQueue(SIZE);
733              Object o[] = q.toArray(new String[10] );
734              shouldThrow();
735 <        } catch(ArrayStoreException  success){}
735 >        } catch (ArrayStoreException  success) {}
736      }
737 <    
737 >
738      /**
739       * iterator iterates through all elements
740       */
# Line 742 | Line 742 | public class PriorityBlockingQueueTest e
742          PriorityBlockingQueue q = populatedQueue(SIZE);
743          int i = 0;
744          Iterator it = q.iterator();
745 <        while(it.hasNext()) {
745 >        while (it.hasNext()) {
746              assertTrue(q.contains(it.next()));
747              ++i;
748          }
# Line 778 | Line 778 | public class PriorityBlockingQueueTest e
778          for (int i = 0; i < SIZE; ++i) {
779              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
780          }
781 <    }        
781 >    }
782  
783      /**
784       * offer transfers elements across Executor tasks
# Line 810 | Line 810 | public class PriorityBlockingQueueTest e
810                  }
811              }
812          });
813 <        
813 >
814          joinPool(executor);
815      }
816  
817      /**
818 <     * A deserialized serialized queue has same elements
818 >     * A deserialized serialized queue has same elements
819       */
820      public void testSerialization() {
821          PriorityBlockingQueue q = populatedQueue(SIZE);
# Line 829 | Line 829 | public class PriorityBlockingQueueTest e
829              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
830              PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
831              assertEquals(q.size(), r.size());
832 <            while (!q.isEmpty())
832 >            while (!q.isEmpty())
833                  assertEquals(q.remove(), r.remove());
834 <        } catch(Exception e){
834 >        } catch (Exception e) {
835              unexpectedException();
836          }
837      }
838  
839      /**
840       * drainTo(null) throws NPE
841 <     */
841 >     */
842      public void testDrainToNull() {
843          PriorityBlockingQueue q = populatedQueue(SIZE);
844          try {
845              q.drainTo(null);
846              shouldThrow();
847 <        } catch(NullPointerException success) {
847 >        } catch (NullPointerException success) {
848          }
849      }
850  
851      /**
852       * drainTo(this) throws IAE
853 <     */
853 >     */
854      public void testDrainToSelf() {
855          PriorityBlockingQueue q = populatedQueue(SIZE);
856          try {
857              q.drainTo(q);
858              shouldThrow();
859 <        } catch(IllegalArgumentException success) {
859 >        } catch (IllegalArgumentException success) {
860          }
861      }
862  
863      /**
864       * drainTo(c) empties queue into another collection c
865 <     */
865 >     */
866      public void testDrainTo() {
867          PriorityBlockingQueue q = populatedQueue(SIZE);
868          ArrayList l = new ArrayList();
869          q.drainTo(l);
870          assertEquals(q.size(), 0);
871          assertEquals(l.size(), SIZE);
872 <        for (int i = 0; i < SIZE; ++i)
872 >        for (int i = 0; i < SIZE; ++i)
873 >            assertEquals(l.get(i), new Integer(i));
874 >        q.add(zero);
875 >        q.add(one);
876 >        assertFalse(q.isEmpty());
877 >        assertTrue(q.contains(zero));
878 >        assertTrue(q.contains(one));
879 >        l.clear();
880 >        q.drainTo(l);
881 >        assertEquals(q.size(), 0);
882 >        assertEquals(l.size(), 2);
883 >        for (int i = 0; i < 2; ++i)
884              assertEquals(l.get(i), new Integer(i));
885      }
886  
887      /**
888       * drainTo empties queue
889 <     */
889 >     */
890      public void testDrainToWithActivePut() {
891          final PriorityBlockingQueue q = populatedQueue(SIZE);
892          Thread t = new Thread(new Runnable() {
# Line 888 | Line 899 | public class PriorityBlockingQueueTest e
899              ArrayList l = new ArrayList();
900              q.drainTo(l);
901              assertTrue(l.size() >= SIZE);
902 <            for (int i = 0; i < SIZE; ++i)
902 >            for (int i = 0; i < SIZE; ++i)
903                  assertEquals(l.get(i), new Integer(i));
904              t.join();
905 <            assertTrue(q.size() + l.size() == SIZE+1);
906 <        } catch(Exception e){
905 >            assertTrue(q.size() + l.size() >= SIZE);
906 >        } catch (Exception e) {
907              unexpectedException();
908          }
909      }
910  
911      /**
912       * drainTo(null, n) throws NPE
913 <     */
913 >     */
914      public void testDrainToNullN() {
915          PriorityBlockingQueue q = populatedQueue(SIZE);
916          try {
917              q.drainTo(null, 0);
918              shouldThrow();
919 <        } catch(NullPointerException success) {
919 >        } catch (NullPointerException success) {
920          }
921      }
922  
923      /**
924       * drainTo(this, n) throws IAE
925 <     */
925 >     */
926      public void testDrainToSelfN() {
927          PriorityBlockingQueue q = populatedQueue(SIZE);
928          try {
929              q.drainTo(q, 0);
930              shouldThrow();
931 <        } catch(IllegalArgumentException success) {
931 >        } catch (IllegalArgumentException success) {
932          }
933      }
934  
935      /**
936       * drainTo(c, n) empties first max {n, size} elements of queue into c
937 <     */
937 >     */
938      public void testDrainToN() {
939 +        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
940          for (int i = 0; i < SIZE + 2; ++i) {
941 <            PriorityBlockingQueue q = populatedQueue(SIZE);
941 >            for (int j = 0; j < SIZE; j++)
942 >                assertTrue(q.offer(new Integer(j)));
943              ArrayList l = new ArrayList();
944              q.drainTo(l, i);
945              int k = (i < SIZE)? i : SIZE;
933            assertEquals(q.size(), SIZE-k);
946              assertEquals(l.size(), k);
947 <            for (int j = 0; j < k; ++j)
948 <                assertTrue(l.contains(new Integer(j)));
947 >            assertEquals(q.size(), SIZE-k);
948 >            for (int j = 0; j < k; ++j)
949 >                assertEquals(l.get(j), new Integer(j));
950 >            while (q.poll() != null) ;
951          }
952      }
953  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines