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.5 by jsr166, Sat Nov 21 21:02:14 2009 UTC vs.
Revision 1.22 by jsr166, Sun Nov 13 03:36:50 2016 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 import junit.framework.*;
7 import java.util.concurrent.*;
8 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);
21      }
22  
23 <    /**
23 >    /*
24       * Testing coverage notes:
25       *
26       * We don't test randomness properties, but only that repeated
# Line 26 | Line 29 | public class ThreadLocalRandomTest exten
29       * across multiples of primes.
30       */
31  
32 <    //
32 >    // max numbers of calls to detect getting stuck on one value
33      static final int NCALLS = 10000;
34  
35      // max sampled int bound
36      static final int MAX_INT_BOUND = (1 << 28);
37  
38 <    // Max sampled long bound
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 46 | Line 52 | public class ThreadLocalRandomTest exten
52      }
53  
54      /**
55 <     * Repeated calls to nextInt produce at least one different result
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() {
93          int f = ThreadLocalRandom.current().nextInt();
# Line 57 | Line 98 | public class ThreadLocalRandomTest exten
98      }
99  
100      /**
101 <     * Repeated calls to nextLong produce at least one different result
101 >     * Repeated calls to nextLong produce at least two distinct results
102       */
103      public void testNextLong() {
104          long f = ThreadLocalRandom.current().nextLong();
# Line 67 | Line 108 | public class ThreadLocalRandomTest exten
108          assertTrue(i < NCALLS);
109      }
110  
70
111      /**
112 <     * Repeated calls to nextBoolean produce at least one different result
112 >     * Repeated calls to nextBoolean produce at least two distinct results
113       */
114      public void testNextBoolean() {
115          boolean f = ThreadLocalRandom.current().nextBoolean();
# Line 80 | Line 120 | public class ThreadLocalRandomTest exten
120      }
121  
122      /**
123 <     * Repeated calls to nextFloat produce at least one different result
123 >     * Repeated calls to nextFloat produce at least two distinct results
124       */
125      public void testNextFloat() {
126          float f = ThreadLocalRandom.current().nextFloat();
# Line 91 | Line 131 | public class ThreadLocalRandomTest exten
131      }
132  
133      /**
134 <     * Repeated calls to nextDouble produce at least one different result
134 >     * Repeated calls to nextDouble produce at least two distinct results
135       */
136      public void testNextDouble() {
137          double f = ThreadLocalRandom.current().nextDouble();
138 <        double i = 0;
138 >        int i = 0;
139          while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
140              ++i;
141          assertTrue(i < NCALLS);
142      }
143  
144      /**
145 <     * Repeated calls to nextGaussian produce at least one different result
145 >     * Repeated calls to nextGaussian produce at least two distinct results
146       */
147      public void testNextGaussian() {
148          double f = ThreadLocalRandom.current().nextGaussian();
# Line 112 | Line 152 | public class ThreadLocalRandomTest exten
152          assertTrue(i < NCALLS);
153      }
154  
115
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;
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  
136
186      /**
187       * nextInt(bound) returns 0 <= value < bound;
188 <     * repeated calls produce at least one different result
188 >     * repeated calls produce at least two distinct results
189       */
190      public void testNextIntBounded() {
191          // sample bound space across prime number increments
# Line 154 | Line 203 | public class ThreadLocalRandomTest exten
203          }
204      }
205  
157
206      /**
207       * nextInt(least, bound) returns least <= value < bound;
208 <     * repeated calls produce at least one different result
208 >     * repeated calls produce at least two distinct results
209       */
210      public void testNextIntBounded2() {
211          for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
# 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;
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      /**
259       * nextLong(bound) returns 0 <= value < bound;
260 <     * repeated calls produce at least one different result
260 >     * repeated calls produce at least two distinct results
261       */
262      public void testNextLongBounded() {
263          for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
# Line 217 | Line 276 | public class ThreadLocalRandomTest exten
276  
277      /**
278       * nextLong(least, bound) returns least <= value < bound;
279 <     * repeated calls produce at least one different result
279 >     * repeated calls produce at least two distinct results
280       */
281      public void testNextLongBounded2() {
282          for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
# 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 one different result
320 >     * repeated calls produce at least two distinct results
321       */
322      public void testNextDoubleBounded2() {
323          for (double least = 0.0001; least < 1.0e20; least *= 8) {
# Line 258 | Line 336 | public class ThreadLocalRandomTest exten
336          }
337      }
338  
339 +    /**
340 +     * Different threads produce different pseudo-random sequences
341 +     */
342 +    public void testDifferentSequences() {
343 +        // Don't use main thread's ThreadLocalRandom - it is likely to
344 +        // be polluted by previous tests.
345 +        final AtomicReference<ThreadLocalRandom> threadLocalRandom =
346 +            new AtomicReference<ThreadLocalRandom>();
347 +        final AtomicLong rand = new AtomicLong();
348 +
349 +        long firstRand = 0;
350 +        ThreadLocalRandom firstThreadLocalRandom = null;
351 +
352 +        Runnable getRandomState = new CheckedRunnable() {
353 +            public void realRun() {
354 +                ThreadLocalRandom current = ThreadLocalRandom.current();
355 +                assertSame(current, ThreadLocalRandom.current());
356 +                // test bug: the following is not guaranteed and not true in JDK8
357 +                //                assertNotSame(current, threadLocalRandom.get());
358 +                rand.set(current.nextLong());
359 +                threadLocalRandom.set(current);
360 +            }};
361 +
362 +        Thread first = newStartedThread(getRandomState);
363 +        awaitTermination(first);
364 +        firstRand = rand.get();
365 +        firstThreadLocalRandom = threadLocalRandom.get();
366 +
367 +        for (int i = 0; i < NCALLS; i++) {
368 +            Thread t = newStartedThread(getRandomState);
369 +            awaitTermination(t);
370 +            if (firstRand != rand.get())
371 +                return;
372 +        }
373 +        fail("all threads generate the same pseudo-random sequence");
374 +    }
375  
376   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines