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.18 by jsr166, Wed Sep 25 07:39:17 2013 UTC vs.
Revision 1.22 by jsr166, Sun Nov 13 03:36:50 2016 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;
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 50 | Line 52 | public class ThreadLocalRandomTest exten
52      }
53  
54      /**
55 +     * Repeated calls to next (only accessible via reflection) produce
56 +     * at least two distinct results, and repeated calls produce all
57 +     * possible values.
58 +     */
59 +    public void testNext() throws ReflectiveOperationException {
60 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
61 +        try {
62 +            java.lang.reflect.Method m
63 +                = ThreadLocalRandom.class.getDeclaredMethod(
64 +                    "next", new Class[] { int.class });
65 +            m.setAccessible(true);
66 +
67 +            int i;
68 +            {
69 +                int val = new java.util.Random().nextInt(4);
70 +                for (i = 0; i < NCALLS; i++) {
71 +                    int q = (int) m.invoke(rnd, new Object[] { 2 });
72 +                    if (val == q) break;
73 +                }
74 +                assertTrue(i < NCALLS);
75 +            }
76 +
77 +            {
78 +                int r = (int) m.invoke(rnd, new Object[] { 3 });
79 +                for (i = 0; i < NCALLS; i++) {
80 +                    int q = (int) m.invoke(rnd, new Object[] { 3 });
81 +                    assertTrue(q < (1<<3));
82 +                    if (r != q) break;
83 +                }
84 +                assertTrue(i < NCALLS);
85 +            }
86 +        } catch (SecurityException acceptable) {}
87 +    }
88 +
89 +    /**
90       * Repeated calls to nextInt produce at least two distinct results
91       */
92      public void testNextInt() {
# Line 116 | Line 153 | public class ThreadLocalRandomTest exten
153      }
154  
155      /**
156 <     * nextInt(negative) throws IllegalArgumentException
156 >     * nextInt(non-positive) throws IllegalArgumentException
157       */
158 <    public void testNextIntBoundedNeg() {
159 <        try {
160 <            int f = ThreadLocalRandom.current().nextInt(-17);
161 <            shouldThrow();
162 <        } catch (IllegalArgumentException success) {}
158 >    public void testNextIntBoundNonPositive() {
159 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
160 >        for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) {
161 >            try {
162 >                rnd.nextInt(bound);
163 >                shouldThrow();
164 >            } catch (IllegalArgumentException success) {}
165 >        }
166      }
167  
168      /**
169       * nextInt(least >= bound) throws IllegalArgumentException
170       */
171      public void testNextIntBadBounds() {
172 <        try {
173 <            int f = ThreadLocalRandom.current().nextInt(17, 2);
174 <            shouldThrow();
175 <        } catch (IllegalArgumentException success) {}
172 >        int[][] badBoundss = {
173 >            { 17, 2 },
174 >            { -42, -42 },
175 >            { Integer.MAX_VALUE, Integer.MIN_VALUE },
176 >        };
177 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
178 >        for (int[] badBounds : badBoundss) {
179 >            try {
180 >                rnd.nextInt(badBounds[0], badBounds[1]);
181 >                shouldThrow();
182 >            } catch (IllegalArgumentException success) {}
183 >        }
184      }
185  
186      /**
# Line 177 | Line 225 | public class ThreadLocalRandomTest exten
225      }
226  
227      /**
228 <     * nextLong(negative) throws IllegalArgumentException
228 >     * nextLong(non-positive) throws IllegalArgumentException
229       */
230 <    public void testNextLongBoundedNeg() {
231 <        try {
232 <            long f = ThreadLocalRandom.current().nextLong(-17);
233 <            shouldThrow();
234 <        } catch (IllegalArgumentException success) {}
230 >    public void testNextLongBoundNonPositive() {
231 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
232 >        for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) {
233 >            try {
234 >                rnd.nextLong(bound);
235 >                shouldThrow();
236 >            } catch (IllegalArgumentException success) {}
237 >        }
238      }
239  
240      /**
241       * nextLong(least >= bound) throws IllegalArgumentException
242       */
243      public void testNextLongBadBounds() {
244 <        try {
245 <            long f = ThreadLocalRandom.current().nextLong(17, 2);
246 <            shouldThrow();
247 <        } catch (IllegalArgumentException success) {}
244 >        long[][] badBoundss = {
245 >            { 17L, 2L },
246 >            { -42L, -42L },
247 >            { Long.MAX_VALUE, Long.MIN_VALUE },
248 >        };
249 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
250 >        for (long[] badBounds : badBoundss) {
251 >            try {
252 >                rnd.nextLong(badBounds[0], badBounds[1]);
253 >                shouldThrow();
254 >            } catch (IllegalArgumentException success) {}
255 >        }
256      }
257  
258      /**
# Line 236 | Line 295 | public class ThreadLocalRandomTest exten
295          }
296      }
297  
298 +    /**
299 +     * nextDouble(non-positive) throws IllegalArgumentException
300 +     */
301 +    public void testNextDoubleBoundNonPositive() {
302 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
303 +        double[] badBounds = {
304 +            0.0d,
305 +            -17.0d,
306 +            -Double.MIN_VALUE,
307 +            Double.NEGATIVE_INFINITY,
308 +            Double.NaN,
309 +        };
310 +        for (double bound : badBounds) {
311 +            try {
312 +                rnd.nextDouble(bound);
313 +                shouldThrow();
314 +            } catch (IllegalArgumentException success) {}
315 +        }
316 +    }
317 +
318      /**
319       * nextDouble(least, bound) returns least <= value < bound;
320       * repeated calls produce at least two distinct results

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines