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

Comparing jsr166/src/test/tck/DelayQueueTest.java (file contents):
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.12 by dl, Fri Jun 10 18:13:27 2005 UTC

# Line 1 | Line 1
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.
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.
7   */
8  
9   import junit.framework.*;
# Line 21 | Line 22 | public class DelayQueueTest extends JSR1
22      private static final int NOCAP = Integer.MAX_VALUE;
23  
24      /**
25 <     * A delayed implmentation for testing.
25 >     * A delayed implementation for testing.
26       * Most  tests use Pseudodelays, where delays are all elapsed
27       * (so, no blocking solely for delays) but are still ordered
28       */
29      static class PDelay implements Delayed {
30          int pseudodelay;
31          PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
32 <        public int compareTo(Object y) {
32 >        public int compareTo(PDelay y) {
33              int i = pseudodelay;
34              int j = ((PDelay)y).pseudodelay;
35              if (i < j) return -1;
# Line 36 | Line 37 | public class DelayQueueTest extends JSR1
37              return 0;
38          }
39  
40 <        public int compareTo(PDelay y) {
40 >        public int compareTo(Delayed y) {
41              int i = pseudodelay;
42              int j = ((PDelay)y).pseudodelay;
43              if (i < j) return -1;
# Line 73 | Line 74 | public class DelayQueueTest extends JSR1
74          NanoDelay(long i) {
75              trigger = System.nanoTime() + i;
76          }
77 <        public int compareTo(Object y) {
77 >        public int compareTo(NanoDelay y) {
78              long i = trigger;
79              long j = ((NanoDelay)y).trigger;
80              if (i < j) return -1;
# Line 81 | Line 82 | public class DelayQueueTest extends JSR1
82              return 0;
83          }
84  
85 <        public int compareTo(NanoDelay y) {
85 >        public int compareTo(Delayed y) {
86              long i = trigger;
87              long j = ((NanoDelay)y).trigger;
88              if (i < j) return -1;
# Line 232 | Line 233 | public class DelayQueueTest extends JSR1
233      }
234  
235      /**
236 +     * add(null) throws NPE
237 +     */
238 +    public void testAddNull() {
239 +        try {
240 +            DelayQueue q = new DelayQueue();
241 +            q.add(null);
242 +            shouldThrow();
243 +        } catch (NullPointerException success) { }  
244 +    }
245 +
246 +    /**
247       * offer non-null succeeds
248       */
249      public void testOffer() {
# Line 262 | Line 274 | public class DelayQueueTest extends JSR1
274          }
275          catch (NullPointerException success) {}
276      }
277 +
278 +
279 +    /**
280 +     * addAll(this) throws IAE
281 +     */
282 +    public void testAddAllSelf() {
283 +        try {
284 +            DelayQueue q = populatedQueue(SIZE);
285 +            q.addAll(q);
286 +            shouldThrow();
287 +        }
288 +        catch (IllegalArgumentException success) {}
289 +    }
290 +
291      /**
292       * addAll of a collection with null elements throws NPE
293       */
# Line 566 | Line 592 | public class DelayQueueTest extends JSR1
592          for (int i = 0; i < SIZE; ++i) {
593              assertEquals(new PDelay(i), ((PDelay)q.peek()));
594              q.poll();
595 <            assertTrue(q.peek() == null ||
596 <                       i != ((PDelay)q.peek()).intValue());
595 >            if (q.isEmpty())
596 >                assertNull(q.peek());
597 >            else
598 >                assertTrue(i != ((PDelay)q.peek()).intValue());
599          }
600          assertNull(q.peek());
601      }
# Line 639 | Line 667 | public class DelayQueueTest extends JSR1
667          assertTrue(q.isEmpty());
668          assertEquals(0, q.size());
669          assertEquals(NOCAP, q.remainingCapacity());
670 <        q.add(new PDelay(1));
670 >        PDelay x = new PDelay(1);
671 >        q.add(x);
672          assertFalse(q.isEmpty());
673 +        assertTrue(q.contains(x));
674          q.clear();
675          assertTrue(q.isEmpty());
676      }
# Line 724 | Line 754 | public class DelayQueueTest extends JSR1
754              unexpectedException();
755          }    
756      }
757 +
758 +
759 +    /**
760 +     * toArray(null) throws NPE
761 +     */
762 +    public void testToArray_BadArg() {
763 +        try {
764 +            DelayQueue q = populatedQueue(SIZE);
765 +            Object o[] = q.toArray(null);
766 +            shouldThrow();
767 +        } catch(NullPointerException success){}
768 +    }
769 +
770 +    /**
771 +     * toArray with incompatible array type throws CCE
772 +     */
773 +    public void testToArray1_BadArg() {
774 +        try {
775 +            DelayQueue q = populatedQueue(SIZE);
776 +            Object o[] = q.toArray(new String[10] );
777 +            shouldThrow();
778 +        } catch(ArrayStoreException  success){}
779 +    }
780      
781      /**
782       * iterator iterates through all elements
# Line 804 | Line 857 | public class DelayQueueTest extends JSR1
857  
858  
859      /**
860 <     * Dekayed actions do not occur until their delay elapses
860 >     * Delayed actions do not occur until their delay elapses
861       */
862      public void testDelay() {
863          DelayQueue q = new DelayQueue();
# Line 832 | Line 885 | public class DelayQueueTest extends JSR1
885          }
886      }
887  
888 +    /**
889 +     * peek of a non-empty queue returns non-null even if not expired
890 +     */
891 +    public void testPeekDelayed() {
892 +        DelayQueue q = new DelayQueue();
893 +        q.add(new NanoDelay(Long.MAX_VALUE));
894 +        assert(q.peek() != null);
895 +    }
896 +
897 +    /**
898 +     * poll of a non-empty queue returns null if no expired elements.
899 +     */
900 +    public void testPollDelayed() {
901 +        DelayQueue q = new DelayQueue();
902 +        q.add(new NanoDelay(Long.MAX_VALUE));
903 +        assertNull(q.poll());
904 +    }
905 +
906 +    /**
907 +     * drainTo(null) throws NPE
908 +     */
909 +    public void testDrainToNull() {
910 +        DelayQueue q = populatedQueue(SIZE);
911 +        try {
912 +            q.drainTo(null);
913 +            shouldThrow();
914 +        } catch(NullPointerException success) {
915 +        }
916 +    }
917 +
918 +    /**
919 +     * drainTo(this) throws IAE
920 +     */
921 +    public void testDrainToSelf() {
922 +        DelayQueue q = populatedQueue(SIZE);
923 +        try {
924 +            q.drainTo(q);
925 +            shouldThrow();
926 +        } catch(IllegalArgumentException success) {
927 +        }
928 +    }
929 +
930 +    /**
931 +     * drainTo(c) empties queue into another collection c
932 +     */
933 +    public void testDrainTo() {
934 +        DelayQueue q = new DelayQueue();
935 +        PDelay[] elems = new PDelay[SIZE];
936 +        for (int i = 0; i < SIZE; ++i) {
937 +            elems[i] = new PDelay(i);
938 +            q.add(elems[i]);
939 +        }
940 +        ArrayList l = new ArrayList();
941 +        q.drainTo(l);
942 +        assertEquals(q.size(), 0);
943 +        for (int i = 0; i < SIZE; ++i)
944 +            assertEquals(l.get(i), elems[i]);
945 +        q.add(elems[0]);
946 +        q.add(elems[1]);
947 +        assertFalse(q.isEmpty());
948 +        assertTrue(q.contains(elems[0]));
949 +        assertTrue(q.contains(elems[1]));
950 +        l.clear();
951 +        q.drainTo(l);
952 +        assertEquals(q.size(), 0);
953 +        assertEquals(l.size(), 2);
954 +        for (int i = 0; i < 2; ++i)
955 +            assertEquals(l.get(i), elems[i]);
956 +    }
957 +
958 +    /**
959 +     * drainTo empties queue
960 +     */
961 +    public void testDrainToWithActivePut() {
962 +        final DelayQueue q = populatedQueue(SIZE);
963 +        Thread t = new Thread(new Runnable() {
964 +                public void run() {
965 +                    q.put(new PDelay(SIZE+1));
966 +                }
967 +            });
968 +        try {
969 +            t.start();
970 +            ArrayList l = new ArrayList();
971 +            q.drainTo(l);
972 +            assertTrue(l.size() >= SIZE);
973 +            t.join();
974 +            assertTrue(q.size() + l.size() >= SIZE);
975 +        } catch(Exception e){
976 +            unexpectedException();
977 +        }
978 +    }
979 +
980 +    /**
981 +     * drainTo(null, n) throws NPE
982 +     */
983 +    public void testDrainToNullN() {
984 +        DelayQueue q = populatedQueue(SIZE);
985 +        try {
986 +            q.drainTo(null, 0);
987 +            shouldThrow();
988 +        } catch(NullPointerException success) {
989 +        }
990 +    }
991 +
992 +    /**
993 +     * drainTo(this, n) throws IAE
994 +     */
995 +    public void testDrainToSelfN() {
996 +        DelayQueue q = populatedQueue(SIZE);
997 +        try {
998 +            q.drainTo(q, 0);
999 +            shouldThrow();
1000 +        } catch(IllegalArgumentException success) {
1001 +        }
1002 +    }
1003 +
1004 +    /**
1005 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1006 +     */
1007 +    public void testDrainToN() {
1008 +        for (int i = 0; i < SIZE + 2; ++i) {
1009 +            DelayQueue q = populatedQueue(SIZE);
1010 +            ArrayList l = new ArrayList();
1011 +            q.drainTo(l, i);
1012 +            int k = (i < SIZE)? i : SIZE;
1013 +            assertEquals(q.size(), SIZE-k);
1014 +            assertEquals(l.size(), k);
1015 +        }
1016 +    }
1017 +
1018 +
1019   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines