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.14 by jsr166, Sun Jul 14 16:55:01 2013 UTC vs.
Revision 1.24 by jsr166, Sat Dec 10 12:39:15 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 36 | Line 38 | public class ThreadLocalRandomTest exten
38      // max sampled long bound
39      static final long MAX_LONG_BOUND = (1L << 42);
40  
41 +    // Number of replications for other checks
42 +    static final int REPS = 20;
43 +
44      /**
45       * setSeed throws UnsupportedOperationException
46       */
# Line 47 | 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 +        final java.lang.reflect.Method m;
62 +        try {
63 +            m = ThreadLocalRandom.class.getDeclaredMethod(
64 +                    "next", new Class[] { int.class });
65 +            m.setAccessible(true);
66 +        } catch (SecurityException acceptable) {
67 +            // Security manager may deny access
68 +            return;
69 +        } catch (Exception ex) {
70 +            // jdk9 module system may deny access
71 +            if (ex.getClass().getSimpleName()
72 +                .equals("InaccessibleObjectException"))
73 +                return;
74 +            throw ex;
75 +        }
76 +
77 +        int i;
78 +        {
79 +            int val = new java.util.Random().nextInt(4);
80 +            for (i = 0; i < NCALLS; i++) {
81 +                int q = (int) m.invoke(rnd, new Object[] { 2 });
82 +                if (val == q) break;
83 +            }
84 +            assertTrue(i < NCALLS);
85 +        }
86 +
87 +        {
88 +            int r = (int) m.invoke(rnd, new Object[] { 3 });
89 +            for (i = 0; i < NCALLS; i++) {
90 +                int q = (int) m.invoke(rnd, new Object[] { 3 });
91 +                assertTrue(q < (1<<3));
92 +                if (r != q) break;
93 +            }
94 +            assertTrue(i < NCALLS);
95 +        }
96 +    }
97 +
98 +    /**
99       * Repeated calls to nextInt produce at least two distinct results
100       */
101      public void testNextInt() {
# Line 113 | Line 162 | public class ThreadLocalRandomTest exten
162      }
163  
164      /**
165 <     * nextInt(negative) throws IllegalArgumentException
165 >     * nextInt(non-positive) throws IllegalArgumentException
166       */
167 <    public void testNextIntBoundedNeg() {
168 <        try {
169 <            int f = ThreadLocalRandom.current().nextInt(-17);
170 <            shouldThrow();
171 <        } catch (IllegalArgumentException success) {}
167 >    public void testNextIntBoundNonPositive() {
168 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
169 >        for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) {
170 >            try {
171 >                rnd.nextInt(bound);
172 >                shouldThrow();
173 >            } catch (IllegalArgumentException success) {}
174 >        }
175      }
176  
177      /**
178       * nextInt(least >= bound) throws IllegalArgumentException
179       */
180      public void testNextIntBadBounds() {
181 <        try {
182 <            int f = ThreadLocalRandom.current().nextInt(17, 2);
183 <            shouldThrow();
184 <        } catch (IllegalArgumentException success) {}
181 >        int[][] badBoundss = {
182 >            { 17, 2 },
183 >            { -42, -42 },
184 >            { Integer.MAX_VALUE, Integer.MIN_VALUE },
185 >        };
186 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
187 >        for (int[] badBounds : badBoundss) {
188 >            try {
189 >                rnd.nextInt(badBounds[0], badBounds[1]);
190 >                shouldThrow();
191 >            } catch (IllegalArgumentException success) {}
192 >        }
193      }
194  
195      /**
# Line 174 | Line 234 | public class ThreadLocalRandomTest exten
234      }
235  
236      /**
237 <     * nextLong(negative) throws IllegalArgumentException
237 >     * nextLong(non-positive) throws IllegalArgumentException
238       */
239 <    public void testNextLongBoundedNeg() {
240 <        try {
241 <            long f = ThreadLocalRandom.current().nextLong(-17);
242 <            shouldThrow();
243 <        } catch (IllegalArgumentException success) {}
239 >    public void testNextLongBoundNonPositive() {
240 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
241 >        for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) {
242 >            try {
243 >                rnd.nextLong(bound);
244 >                shouldThrow();
245 >            } catch (IllegalArgumentException success) {}
246 >        }
247      }
248  
249      /**
250       * nextLong(least >= bound) throws IllegalArgumentException
251       */
252      public void testNextLongBadBounds() {
253 <        try {
254 <            long f = ThreadLocalRandom.current().nextLong(17, 2);
255 <            shouldThrow();
256 <        } catch (IllegalArgumentException success) {}
253 >        long[][] badBoundss = {
254 >            { 17L, 2L },
255 >            { -42L, -42L },
256 >            { Long.MAX_VALUE, Long.MIN_VALUE },
257 >        };
258 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
259 >        for (long[] badBounds : badBoundss) {
260 >            try {
261 >                rnd.nextLong(badBounds[0], badBounds[1]);
262 >                shouldThrow();
263 >            } catch (IllegalArgumentException success) {}
264 >        }
265      }
266  
267      /**
# Line 234 | Line 305 | public class ThreadLocalRandomTest exten
305      }
306  
307      /**
308 +     * nextDouble(non-positive) throws IllegalArgumentException
309 +     */
310 +    public void testNextDoubleBoundNonPositive() {
311 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
312 +        double[] badBounds = {
313 +            0.0d,
314 +            -17.0d,
315 +            -Double.MIN_VALUE,
316 +            Double.NEGATIVE_INFINITY,
317 +            Double.NaN,
318 +        };
319 +        for (double bound : badBounds) {
320 +            try {
321 +                rnd.nextDouble(bound);
322 +                shouldThrow();
323 +            } catch (IllegalArgumentException success) {}
324 +        }
325 +    }
326 +
327 +    /**
328       * nextDouble(least, bound) returns least <= value < bound;
329       * repeated calls produce at least two distinct results
330       */
# Line 267 | Line 358 | public class ThreadLocalRandomTest exten
358          long firstRand = 0;
359          ThreadLocalRandom firstThreadLocalRandom = null;
360  
361 <        final CheckedRunnable getRandomState = new CheckedRunnable() {
361 >        Runnable getRandomState = new CheckedRunnable() {
362              public void realRun() {
363                  ThreadLocalRandom current = ThreadLocalRandom.current();
364                  assertSame(current, ThreadLocalRandom.current());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines