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.53 by jsr166, Mon May 30 22:43:20 2011 UTC vs.
Revision 1.65 by jsr166, Wed Dec 31 20:09:08 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.Arrays;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Iterator;
15   import java.util.NoSuchElementException;
# Line 19 | Line 20 | import java.util.concurrent.DelayQueue;
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22   import java.util.concurrent.TimeUnit;
23 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 >
24 > import junit.framework.Test;
25  
26   public class DelayQueueTest extends JSR166TestCase {
27  
# Line 50 | Line 52 | public class DelayQueueTest extends JSR1
52       */
53      static class PDelay implements Delayed {
54          int pseudodelay;
55 <        PDelay(int i) { pseudodelay = Integer.MIN_VALUE + i; }
56 <        public int compareTo(PDelay y) {
57 <            int i = pseudodelay;
58 <            int j = y.pseudodelay;
59 <            if (i < j) return -1;
58 <            if (i > j) return 1;
59 <            return 0;
55 >        PDelay(int i) { pseudodelay = i; }
56 >        public int compareTo(PDelay other) {
57 >            int a = this.pseudodelay;
58 >            int b = other.pseudodelay;
59 >            return (a < b) ? -1 : (a > b) ? 1 : 0;
60          }
61
61          public int compareTo(Delayed y) {
62              return compareTo((PDelay)y);
63          }
65
64          public boolean equals(Object other) {
65 <            return equals((PDelay)other);
66 <        }
69 <        public boolean equals(PDelay other) {
70 <            return other.pseudodelay == pseudodelay;
65 >            return (other instanceof PDelay) &&
66 >                this.pseudodelay == ((PDelay)other).pseudodelay;
67          }
68 <
68 >        // suppress [overrides] javac warning
69 >        public int hashCode() { return pseudodelay; }
70          public long getDelay(TimeUnit ignore) {
71 <            return pseudodelay;
75 <        }
76 <        public int intValue() {
77 <            return pseudodelay;
71 >            return Integer.MIN_VALUE + pseudodelay;
72          }
79
73          public String toString() {
74              return String.valueOf(pseudodelay);
75          }
# Line 109 | Line 102 | public class DelayQueueTest extends JSR1
102              return other.trigger == trigger;
103          }
104  
105 +        // suppress [overrides] javac warning
106 +        public int hashCode() { return (int) trigger; }
107 +
108          public long getDelay(TimeUnit unit) {
109              long n = trigger - System.nanoTime();
110              return unit.convert(n, TimeUnit.NANOSECONDS);
# Line 124 | Line 120 | public class DelayQueueTest extends JSR1
120      }
121  
122      /**
123 <     * Create a queue of given size containing consecutive
123 >     * Returns a new queue of given size containing consecutive
124       * PDelays 0 ... n.
125       */
126      private DelayQueue<PDelay> populatedQueue(int n) {
127          DelayQueue<PDelay> q = new DelayQueue<PDelay>();
128          assertTrue(q.isEmpty());
129 <        for (int i = n-1; i >= 0; i-=2)
129 >        for (int i = n-1; i >= 0; i -= 2)
130              assertTrue(q.offer(new PDelay(i)));
131 <        for (int i = (n & 1); i < n; i+=2)
131 >        for (int i = (n & 1); i < n; i += 2)
132              assertTrue(q.offer(new PDelay(i)));
133          assertFalse(q.isEmpty());
134          assertEquals(NOCAP, q.remainingCapacity());
# Line 494 | Line 490 | public class DelayQueueTest extends JSR1
490      }
491  
492      /**
497     * remove(x) removes x and returns true if present
498     */
499    public void testRemoveElement() {
500        DelayQueue q = populatedQueue(SIZE);
501        for (int i = 1; i < SIZE; i+=2) {
502            assertTrue(q.remove(new PDelay(i)));
503        }
504        for (int i = 0; i < SIZE; i+=2) {
505            assertTrue(q.remove(new PDelay(i)));
506            assertFalse(q.remove(new PDelay(i+1)));
507        }
508        assertTrue(q.isEmpty());
509    }
510
511    /**
493       * contains(x) reports true when elements added but not yet removed
494       */
495      public void testContains() {
# Line 647 | Line 628 | public class DelayQueueTest extends JSR1
628          it.next();
629          it.remove();
630          it = q.iterator();
631 <        assertEquals(it.next(), new PDelay(2));
632 <        assertEquals(it.next(), new PDelay(3));
631 >        assertEquals(new PDelay(2), it.next());
632 >        assertEquals(new PDelay(3), it.next());
633          assertFalse(it.hasNext());
634      }
635  
# Line 658 | Line 639 | public class DelayQueueTest extends JSR1
639      public void testToString() {
640          DelayQueue q = populatedQueue(SIZE);
641          String s = q.toString();
642 <        for (int i = 0; i < SIZE; ++i) {
643 <            assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i)));
663 <        }
642 >        for (Object e : q)
643 >            assertTrue(s.contains(e.toString()));
644      }
645  
646      /**
# Line 674 | Line 654 | public class DelayQueueTest extends JSR1
654              public void realRun() throws InterruptedException {
655                  assertNull(q.poll());
656                  threadsStarted.await();
657 <                assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS));
657 >                assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
658                  checkEmpty(q);
659              }});
660  
# Line 746 | Line 726 | public class DelayQueueTest extends JSR1
726          }
727          ArrayList l = new ArrayList();
728          q.drainTo(l);
729 <        assertEquals(q.size(), 0);
729 >        assertEquals(0, q.size());
730          for (int i = 0; i < SIZE; ++i)
731 <            assertEquals(l.get(i), elems[i]);
731 >            assertEquals(elems[i], l.get(i));
732          q.add(elems[0]);
733          q.add(elems[1]);
734          assertFalse(q.isEmpty());
# Line 756 | Line 736 | public class DelayQueueTest extends JSR1
736          assertTrue(q.contains(elems[1]));
737          l.clear();
738          q.drainTo(l);
739 <        assertEquals(q.size(), 0);
740 <        assertEquals(l.size(), 2);
739 >        assertEquals(0, q.size());
740 >        assertEquals(2, l.size());
741          for (int i = 0; i < 2; ++i)
742 <            assertEquals(l.get(i), elems[i]);
742 >            assertEquals(elems[i], l.get(i));
743      }
744  
745      /**
# Line 789 | Line 769 | public class DelayQueueTest extends JSR1
769              ArrayList l = new ArrayList();
770              q.drainTo(l, i);
771              int k = (i < SIZE) ? i : SIZE;
772 <            assertEquals(q.size(), SIZE-k);
773 <            assertEquals(l.size(), k);
772 >            assertEquals(SIZE-k, q.size());
773 >            assertEquals(k, l.size());
774          }
775      }
776  
777 +    /**
778 +     * remove(null), contains(null) always return false
779 +     */
780 +    public void testNeverContainsNull() {
781 +        Collection<?> q = populatedQueue(SIZE);
782 +        assertFalse(q.contains(null));
783 +        assertFalse(q.remove(null));
784 +    }
785   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines