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

Comparing jsr166/src/test/tck/ThreadLocalRandomTest.java (file contents):
Revision 1.16 by jsr166, Fri Aug 16 07:07:01 2013 UTC vs.
Revision 1.21 by jsr166, Sat Apr 25 04:55:31 2015 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import junit.framework.*;
7 < import java.util.*;
6 >
7   import java.util.concurrent.ThreadLocalRandom;
8   import java.util.concurrent.atomic.AtomicLong;
10 import java.util.concurrent.atomic.AtomicInteger;
9   import java.util.concurrent.atomic.AtomicReference;
10  
11 + import junit.framework.Test;
12 + import junit.framework.TestSuite;
13 +
14   public class ThreadLocalRandomTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run(suite());
17 >        main(suite(), args);
18      }
19      public static Test suite() {
20          return new TestSuite(ThreadLocalRandomTest.class);
# Line 117 | Line 118 | public class ThreadLocalRandomTest exten
118      }
119  
120      /**
121 <     * nextInt(negative) throws IllegalArgumentException
121 >     * nextInt(non-positive) throws IllegalArgumentException
122       */
123 <    public void testNextIntBoundedNeg() {
124 <        try {
125 <            int f = ThreadLocalRandom.current().nextInt(-17);
126 <            shouldThrow();
127 <        } catch (IllegalArgumentException success) {}
123 >    public void testNextIntBoundNonPositive() {
124 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
125 >        for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) {
126 >            try {
127 >                rnd.nextInt(bound);
128 >                shouldThrow();
129 >            } catch (IllegalArgumentException success) {}
130 >        }
131      }
132  
133      /**
134       * nextInt(least >= bound) throws IllegalArgumentException
135       */
136      public void testNextIntBadBounds() {
137 <        try {
138 <            int f = ThreadLocalRandom.current().nextInt(17, 2);
139 <            shouldThrow();
140 <        } catch (IllegalArgumentException success) {}
137 >        int[][] badBoundss = {
138 >            { 17, 2 },
139 >            { -42, -42 },
140 >            { Integer.MAX_VALUE, Integer.MIN_VALUE },
141 >        };
142 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
143 >        for (int[] badBounds : badBoundss) {
144 >            try {
145 >                rnd.nextInt(badBounds[0], badBounds[1]);
146 >                shouldThrow();
147 >            } catch (IllegalArgumentException success) {}
148 >        }
149      }
150  
151      /**
# Line 178 | Line 190 | public class ThreadLocalRandomTest exten
190      }
191  
192      /**
193 <     * nextLong(negative) throws IllegalArgumentException
193 >     * nextLong(non-positive) throws IllegalArgumentException
194       */
195 <    public void testNextLongBoundedNeg() {
196 <        try {
197 <            long f = ThreadLocalRandom.current().nextLong(-17);
198 <            shouldThrow();
199 <        } catch (IllegalArgumentException success) {}
195 >    public void testNextLongBoundNonPositive() {
196 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
197 >        for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) {
198 >            try {
199 >                rnd.nextLong(bound);
200 >                shouldThrow();
201 >            } catch (IllegalArgumentException success) {}
202 >        }
203      }
204  
205      /**
206       * nextLong(least >= bound) throws IllegalArgumentException
207       */
208      public void testNextLongBadBounds() {
209 <        try {
210 <            long f = ThreadLocalRandom.current().nextLong(17, 2);
211 <            shouldThrow();
212 <        } catch (IllegalArgumentException success) {}
209 >        long[][] badBoundss = {
210 >            { 17L, 2L },
211 >            { -42L, -42L },
212 >            { Long.MAX_VALUE, Long.MIN_VALUE },
213 >        };
214 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
215 >        for (long[] badBounds : badBoundss) {
216 >            try {
217 >                rnd.nextLong(badBounds[0], badBounds[1]);
218 >                shouldThrow();
219 >            } catch (IllegalArgumentException success) {}
220 >        }
221      }
222  
223      /**
# Line 238 | Line 261 | public class ThreadLocalRandomTest exten
261      }
262  
263      /**
264 +     * nextDouble(non-positive) throws IllegalArgumentException
265 +     */
266 +    public void testNextDoubleBoundNonPositive() {
267 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
268 +        double[] badBounds = {
269 +            0.0d,
270 +            -17.0d,
271 +            -Double.MIN_VALUE,
272 +            Double.NEGATIVE_INFINITY,
273 +            Double.NaN,
274 +        };
275 +        for (double bound : badBounds) {
276 +            try {
277 +                rnd.nextDouble(bound);
278 +                shouldThrow();
279 +            } catch (IllegalArgumentException success) {}
280 +        }
281 +    }
282 +
283 +    /**
284       * nextDouble(least, bound) returns least <= value < bound;
285       * repeated calls produce at least two distinct results
286       */
# Line 271 | Line 314 | public class ThreadLocalRandomTest exten
314          long firstRand = 0;
315          ThreadLocalRandom firstThreadLocalRandom = null;
316  
317 <        final CheckedRunnable getRandomState = new CheckedRunnable() {
317 >        Runnable getRandomState = new CheckedRunnable() {
318              public void realRun() {
319                  ThreadLocalRandom current = ThreadLocalRandom.current();
320                  assertSame(current, ThreadLocalRandom.current());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines