ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadLocalRandomTest.java
Revision: 1.22
Committed: Sun Nov 13 03:36:50 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.21: +35 -0 lines
Log Message:
revise (uselessly inaccessible) next(int) method javadoc, implementation and tests

File Contents

# User Rev Content
1 dl 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 jsr166 1.8 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6 jsr166 1.20
7 jsr166 1.10 import java.util.concurrent.ThreadLocalRandom;
8 jsr166 1.11 import java.util.concurrent.atomic.AtomicLong;
9     import java.util.concurrent.atomic.AtomicReference;
10 dl 1.1
11 jsr166 1.20 import junit.framework.Test;
12     import junit.framework.TestSuite;
13    
14 dl 1.1 public class ThreadLocalRandomTest extends JSR166TestCase {
15    
16     public static void main(String[] args) {
17 jsr166 1.21 main(suite(), args);
18 dl 1.1 }
19     public static Test suite() {
20 jsr166 1.4 return new TestSuite(ThreadLocalRandomTest.class);
21 dl 1.1 }
22    
23 jsr166 1.13 /*
24 dl 1.1 * Testing coverage notes:
25 jsr166 1.2 *
26 dl 1.1 * We don't test randomness properties, but only that repeated
27     * calls, up to NCALLS tries, produce at least one different
28     * result. For bounded versions, we sample various intervals
29     * across multiples of primes.
30     */
31    
32 jsr166 1.14 // max numbers of calls to detect getting stuck on one value
33 dl 1.1 static final int NCALLS = 10000;
34    
35     // max sampled int bound
36     static final int MAX_INT_BOUND = (1 << 28);
37    
38 jsr166 1.14 // max sampled long bound
39 dl 1.1 static final long MAX_LONG_BOUND = (1L << 42);
40    
41 dl 1.15 // Number of replications for other checks
42     static final int REPS = 20;
43    
44 dl 1.1 /**
45     * setSeed throws UnsupportedOperationException
46     */
47     public void testSetSeed() {
48     try {
49     ThreadLocalRandom.current().setSeed(17);
50 jsr166 1.3 shouldThrow();
51 jsr166 1.5 } catch (UnsupportedOperationException success) {}
52 dl 1.1 }
53    
54     /**
55 jsr166 1.22 * 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 jsr166 1.14 * Repeated calls to nextInt produce at least two distinct results
91 dl 1.1 */
92     public void testNextInt() {
93     int f = ThreadLocalRandom.current().nextInt();
94     int i = 0;
95     while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f)
96     ++i;
97     assertTrue(i < NCALLS);
98     }
99    
100     /**
101 jsr166 1.14 * Repeated calls to nextLong produce at least two distinct results
102 dl 1.1 */
103     public void testNextLong() {
104     long f = ThreadLocalRandom.current().nextLong();
105     int i = 0;
106     while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f)
107     ++i;
108     assertTrue(i < NCALLS);
109     }
110    
111     /**
112 jsr166 1.14 * Repeated calls to nextBoolean produce at least two distinct results
113 dl 1.1 */
114     public void testNextBoolean() {
115     boolean f = ThreadLocalRandom.current().nextBoolean();
116     int i = 0;
117     while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f)
118     ++i;
119     assertTrue(i < NCALLS);
120     }
121    
122     /**
123 jsr166 1.14 * Repeated calls to nextFloat produce at least two distinct results
124 dl 1.1 */
125     public void testNextFloat() {
126     float f = ThreadLocalRandom.current().nextFloat();
127     int i = 0;
128     while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f)
129     ++i;
130     assertTrue(i < NCALLS);
131     }
132    
133     /**
134 jsr166 1.14 * Repeated calls to nextDouble produce at least two distinct results
135 dl 1.1 */
136     public void testNextDouble() {
137     double f = ThreadLocalRandom.current().nextDouble();
138 jsr166 1.14 int i = 0;
139 dl 1.1 while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f)
140     ++i;
141     assertTrue(i < NCALLS);
142     }
143    
144     /**
145 jsr166 1.14 * Repeated calls to nextGaussian produce at least two distinct results
146 dl 1.1 */
147     public void testNextGaussian() {
148     double f = ThreadLocalRandom.current().nextGaussian();
149     int i = 0;
150     while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f)
151     ++i;
152     assertTrue(i < NCALLS);
153     }
154    
155     /**
156 jsr166 1.19 * nextInt(non-positive) throws IllegalArgumentException
157 dl 1.1 */
158 jsr166 1.19 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 dl 1.1 }
167    
168     /**
169 jsr166 1.14 * nextInt(least >= bound) throws IllegalArgumentException
170 dl 1.1 */
171     public void testNextIntBadBounds() {
172 jsr166 1.19 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 dl 1.1 }
185    
186     /**
187     * nextInt(bound) returns 0 <= value < bound;
188 jsr166 1.14 * repeated calls produce at least two distinct results
189 dl 1.1 */
190     public void testNextIntBounded() {
191     // sample bound space across prime number increments
192     for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
193     int f = ThreadLocalRandom.current().nextInt(bound);
194     assertTrue(0 <= f && f < bound);
195     int i = 0;
196     int j;
197 jsr166 1.2 while (i < NCALLS &&
198 dl 1.1 (j = ThreadLocalRandom.current().nextInt(bound)) == f) {
199     assertTrue(0 <= j && j < bound);
200     ++i;
201     }
202     assertTrue(i < NCALLS);
203     }
204     }
205    
206     /**
207     * nextInt(least, bound) returns least <= value < bound;
208 jsr166 1.14 * repeated calls produce at least two distinct results
209 dl 1.1 */
210     public void testNextIntBounded2() {
211     for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) {
212     for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) {
213     int f = ThreadLocalRandom.current().nextInt(least, bound);
214     assertTrue(least <= f && f < bound);
215     int i = 0;
216     int j;
217 jsr166 1.2 while (i < NCALLS &&
218 dl 1.1 (j = ThreadLocalRandom.current().nextInt(least, bound)) == f) {
219     assertTrue(least <= j && j < bound);
220     ++i;
221     }
222     assertTrue(i < NCALLS);
223     }
224     }
225     }
226    
227     /**
228 jsr166 1.19 * nextLong(non-positive) throws IllegalArgumentException
229 dl 1.1 */
230 jsr166 1.19 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 dl 1.1 }
239    
240     /**
241 jsr166 1.14 * nextLong(least >= bound) throws IllegalArgumentException
242 dl 1.1 */
243     public void testNextLongBadBounds() {
244 jsr166 1.19 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 dl 1.1 }
257    
258     /**
259     * nextLong(bound) returns 0 <= value < bound;
260 jsr166 1.14 * repeated calls produce at least two distinct results
261 dl 1.1 */
262     public void testNextLongBounded() {
263     for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
264     long f = ThreadLocalRandom.current().nextLong(bound);
265     assertTrue(0 <= f && f < bound);
266     int i = 0;
267     long j;
268 jsr166 1.2 while (i < NCALLS &&
269 dl 1.1 (j = ThreadLocalRandom.current().nextLong(bound)) == f) {
270     assertTrue(0 <= j && j < bound);
271     ++i;
272     }
273     assertTrue(i < NCALLS);
274     }
275     }
276    
277     /**
278     * nextLong(least, bound) returns least <= value < bound;
279 jsr166 1.14 * repeated calls produce at least two distinct results
280 dl 1.1 */
281     public void testNextLongBounded2() {
282     for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) {
283     for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
284     long f = ThreadLocalRandom.current().nextLong(least, bound);
285     assertTrue(least <= f && f < bound);
286     int i = 0;
287     long j;
288 jsr166 1.2 while (i < NCALLS &&
289 dl 1.1 (j = ThreadLocalRandom.current().nextLong(least, bound)) == f) {
290     assertTrue(least <= j && j < bound);
291     ++i;
292     }
293     assertTrue(i < NCALLS);
294     }
295     }
296     }
297    
298     /**
299 jsr166 1.19 * 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 dl 1.1 * nextDouble(least, bound) returns least <= value < bound;
320 jsr166 1.14 * repeated calls produce at least two distinct results
321 dl 1.1 */
322     public void testNextDoubleBounded2() {
323     for (double least = 0.0001; least < 1.0e20; least *= 8) {
324     for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
325     double f = ThreadLocalRandom.current().nextDouble(least, bound);
326     assertTrue(least <= f && f < bound);
327     int i = 0;
328     double j;
329 jsr166 1.2 while (i < NCALLS &&
330 dl 1.1 (j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) {
331     assertTrue(least <= j && j < bound);
332     ++i;
333     }
334     assertTrue(i < NCALLS);
335     }
336     }
337     }
338    
339 jsr166 1.11 /**
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 jsr166 1.18 Runnable getRandomState = new CheckedRunnable() {
353 jsr166 1.11 public void realRun() {
354     ThreadLocalRandom current = ThreadLocalRandom.current();
355     assertSame(current, ThreadLocalRandom.current());
356 dl 1.12 // test bug: the following is not guaranteed and not true in JDK8
357     // assertNotSame(current, threadLocalRandom.get());
358 jsr166 1.11 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 dl 1.1 }