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 |
jsr166 |
1.25 |
// Inhibit "An illegal reflective access operation has occurred" |
61 |
|
|
if (!testImplementationDetails) return; |
62 |
|
|
|
63 |
jsr166 |
1.22 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
64 |
jsr166 |
1.23 |
final java.lang.reflect.Method m; |
65 |
jsr166 |
1.22 |
try { |
66 |
jsr166 |
1.29 |
m = ThreadLocalRandom.class.getDeclaredMethod("next", int.class); |
67 |
jsr166 |
1.22 |
m.setAccessible(true); |
68 |
jsr166 |
1.24 |
} catch (SecurityException acceptable) { |
69 |
|
|
// Security manager may deny access |
70 |
jsr166 |
1.23 |
return; |
71 |
jsr166 |
1.24 |
} catch (Exception ex) { |
72 |
|
|
// jdk9 module system may deny access |
73 |
|
|
if (ex.getClass().getSimpleName() |
74 |
|
|
.equals("InaccessibleObjectException")) |
75 |
|
|
return; |
76 |
|
|
throw ex; |
77 |
jsr166 |
1.23 |
} |
78 |
jsr166 |
1.22 |
|
79 |
jsr166 |
1.23 |
int i; |
80 |
|
|
{ |
81 |
|
|
int val = new java.util.Random().nextInt(4); |
82 |
|
|
for (i = 0; i < NCALLS; i++) { |
83 |
|
|
int q = (int) m.invoke(rnd, new Object[] { 2 }); |
84 |
|
|
if (val == q) break; |
85 |
jsr166 |
1.22 |
} |
86 |
jsr166 |
1.23 |
assertTrue(i < NCALLS); |
87 |
|
|
} |
88 |
jsr166 |
1.22 |
|
89 |
jsr166 |
1.23 |
{ |
90 |
|
|
int r = (int) m.invoke(rnd, new Object[] { 3 }); |
91 |
|
|
for (i = 0; i < NCALLS; i++) { |
92 |
|
|
int q = (int) m.invoke(rnd, new Object[] { 3 }); |
93 |
|
|
assertTrue(q < (1<<3)); |
94 |
|
|
if (r != q) break; |
95 |
jsr166 |
1.22 |
} |
96 |
jsr166 |
1.23 |
assertTrue(i < NCALLS); |
97 |
|
|
} |
98 |
jsr166 |
1.22 |
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
jsr166 |
1.14 |
* Repeated calls to nextInt produce at least two distinct results |
102 |
dl |
1.1 |
*/ |
103 |
|
|
public void testNextInt() { |
104 |
|
|
int f = ThreadLocalRandom.current().nextInt(); |
105 |
|
|
int i = 0; |
106 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f) |
107 |
|
|
++i; |
108 |
|
|
assertTrue(i < NCALLS); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
jsr166 |
1.14 |
* Repeated calls to nextLong produce at least two distinct results |
113 |
dl |
1.1 |
*/ |
114 |
|
|
public void testNextLong() { |
115 |
|
|
long f = ThreadLocalRandom.current().nextLong(); |
116 |
|
|
int i = 0; |
117 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f) |
118 |
|
|
++i; |
119 |
|
|
assertTrue(i < NCALLS); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
jsr166 |
1.14 |
* Repeated calls to nextBoolean produce at least two distinct results |
124 |
dl |
1.1 |
*/ |
125 |
|
|
public void testNextBoolean() { |
126 |
|
|
boolean f = ThreadLocalRandom.current().nextBoolean(); |
127 |
|
|
int i = 0; |
128 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f) |
129 |
|
|
++i; |
130 |
|
|
assertTrue(i < NCALLS); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
jsr166 |
1.14 |
* Repeated calls to nextFloat produce at least two distinct results |
135 |
dl |
1.1 |
*/ |
136 |
|
|
public void testNextFloat() { |
137 |
|
|
float f = ThreadLocalRandom.current().nextFloat(); |
138 |
|
|
int i = 0; |
139 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f) |
140 |
|
|
++i; |
141 |
|
|
assertTrue(i < NCALLS); |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
/** |
145 |
jsr166 |
1.14 |
* Repeated calls to nextDouble produce at least two distinct results |
146 |
dl |
1.1 |
*/ |
147 |
|
|
public void testNextDouble() { |
148 |
|
|
double f = ThreadLocalRandom.current().nextDouble(); |
149 |
jsr166 |
1.14 |
int i = 0; |
150 |
dl |
1.1 |
while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) |
151 |
|
|
++i; |
152 |
|
|
assertTrue(i < NCALLS); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
/** |
156 |
jsr166 |
1.14 |
* Repeated calls to nextGaussian produce at least two distinct results |
157 |
dl |
1.1 |
*/ |
158 |
|
|
public void testNextGaussian() { |
159 |
|
|
double f = ThreadLocalRandom.current().nextGaussian(); |
160 |
|
|
int i = 0; |
161 |
|
|
while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f) |
162 |
|
|
++i; |
163 |
|
|
assertTrue(i < NCALLS); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
/** |
167 |
jsr166 |
1.19 |
* nextInt(non-positive) throws IllegalArgumentException |
168 |
dl |
1.1 |
*/ |
169 |
jsr166 |
1.19 |
public void testNextIntBoundNonPositive() { |
170 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
171 |
|
|
for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { |
172 |
|
|
try { |
173 |
|
|
rnd.nextInt(bound); |
174 |
|
|
shouldThrow(); |
175 |
|
|
} catch (IllegalArgumentException success) {} |
176 |
|
|
} |
177 |
dl |
1.1 |
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
jsr166 |
1.14 |
* nextInt(least >= bound) throws IllegalArgumentException |
181 |
dl |
1.1 |
*/ |
182 |
|
|
public void testNextIntBadBounds() { |
183 |
jsr166 |
1.19 |
int[][] badBoundss = { |
184 |
|
|
{ 17, 2 }, |
185 |
|
|
{ -42, -42 }, |
186 |
|
|
{ Integer.MAX_VALUE, Integer.MIN_VALUE }, |
187 |
|
|
}; |
188 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
189 |
|
|
for (int[] badBounds : badBoundss) { |
190 |
|
|
try { |
191 |
|
|
rnd.nextInt(badBounds[0], badBounds[1]); |
192 |
|
|
shouldThrow(); |
193 |
|
|
} catch (IllegalArgumentException success) {} |
194 |
|
|
} |
195 |
dl |
1.1 |
} |
196 |
|
|
|
197 |
|
|
/** |
198 |
|
|
* nextInt(bound) returns 0 <= value < bound; |
199 |
jsr166 |
1.14 |
* repeated calls produce at least two distinct results |
200 |
dl |
1.1 |
*/ |
201 |
|
|
public void testNextIntBounded() { |
202 |
|
|
// sample bound space across prime number increments |
203 |
|
|
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
204 |
|
|
int f = ThreadLocalRandom.current().nextInt(bound); |
205 |
|
|
assertTrue(0 <= f && f < bound); |
206 |
|
|
int i = 0; |
207 |
|
|
int j; |
208 |
jsr166 |
1.2 |
while (i < NCALLS && |
209 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
210 |
|
|
assertTrue(0 <= j && j < bound); |
211 |
|
|
++i; |
212 |
|
|
} |
213 |
|
|
assertTrue(i < NCALLS); |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
/** |
218 |
|
|
* nextInt(least, bound) returns least <= value < bound; |
219 |
jsr166 |
1.14 |
* repeated calls produce at least two distinct results |
220 |
dl |
1.1 |
*/ |
221 |
|
|
public void testNextIntBounded2() { |
222 |
|
|
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
223 |
|
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { |
224 |
|
|
int f = ThreadLocalRandom.current().nextInt(least, bound); |
225 |
|
|
assertTrue(least <= f && f < bound); |
226 |
|
|
int i = 0; |
227 |
|
|
int j; |
228 |
jsr166 |
1.2 |
while (i < NCALLS && |
229 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
230 |
|
|
assertTrue(least <= j && j < bound); |
231 |
|
|
++i; |
232 |
|
|
} |
233 |
|
|
assertTrue(i < NCALLS); |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
jsr166 |
1.19 |
* nextLong(non-positive) throws IllegalArgumentException |
240 |
dl |
1.1 |
*/ |
241 |
jsr166 |
1.19 |
public void testNextLongBoundNonPositive() { |
242 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
243 |
|
|
for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { |
244 |
|
|
try { |
245 |
|
|
rnd.nextLong(bound); |
246 |
|
|
shouldThrow(); |
247 |
|
|
} catch (IllegalArgumentException success) {} |
248 |
|
|
} |
249 |
dl |
1.1 |
} |
250 |
|
|
|
251 |
|
|
/** |
252 |
jsr166 |
1.14 |
* nextLong(least >= bound) throws IllegalArgumentException |
253 |
dl |
1.1 |
*/ |
254 |
|
|
public void testNextLongBadBounds() { |
255 |
jsr166 |
1.19 |
long[][] badBoundss = { |
256 |
|
|
{ 17L, 2L }, |
257 |
|
|
{ -42L, -42L }, |
258 |
|
|
{ Long.MAX_VALUE, Long.MIN_VALUE }, |
259 |
|
|
}; |
260 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
261 |
|
|
for (long[] badBounds : badBoundss) { |
262 |
|
|
try { |
263 |
|
|
rnd.nextLong(badBounds[0], badBounds[1]); |
264 |
|
|
shouldThrow(); |
265 |
|
|
} catch (IllegalArgumentException success) {} |
266 |
|
|
} |
267 |
dl |
1.1 |
} |
268 |
|
|
|
269 |
|
|
/** |
270 |
|
|
* nextLong(bound) returns 0 <= value < bound; |
271 |
jsr166 |
1.14 |
* repeated calls produce at least two distinct results |
272 |
dl |
1.1 |
*/ |
273 |
|
|
public void testNextLongBounded() { |
274 |
|
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
275 |
|
|
long f = ThreadLocalRandom.current().nextLong(bound); |
276 |
|
|
assertTrue(0 <= f && f < bound); |
277 |
|
|
int i = 0; |
278 |
|
|
long j; |
279 |
jsr166 |
1.2 |
while (i < NCALLS && |
280 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
281 |
|
|
assertTrue(0 <= j && j < bound); |
282 |
|
|
++i; |
283 |
|
|
} |
284 |
|
|
assertTrue(i < NCALLS); |
285 |
|
|
} |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
/** |
289 |
|
|
* nextLong(least, bound) returns least <= value < bound; |
290 |
jsr166 |
1.14 |
* repeated calls produce at least two distinct results |
291 |
dl |
1.1 |
*/ |
292 |
|
|
public void testNextLongBounded2() { |
293 |
|
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
294 |
|
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
295 |
|
|
long f = ThreadLocalRandom.current().nextLong(least, bound); |
296 |
|
|
assertTrue(least <= f && f < bound); |
297 |
|
|
int i = 0; |
298 |
|
|
long j; |
299 |
jsr166 |
1.2 |
while (i < NCALLS && |
300 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
301 |
|
|
assertTrue(least <= j && j < bound); |
302 |
|
|
++i; |
303 |
|
|
} |
304 |
|
|
assertTrue(i < NCALLS); |
305 |
|
|
} |
306 |
|
|
} |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
/** |
310 |
jsr166 |
1.19 |
* nextDouble(non-positive) throws IllegalArgumentException |
311 |
|
|
*/ |
312 |
|
|
public void testNextDoubleBoundNonPositive() { |
313 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
314 |
|
|
double[] badBounds = { |
315 |
|
|
0.0d, |
316 |
|
|
-17.0d, |
317 |
|
|
-Double.MIN_VALUE, |
318 |
|
|
Double.NEGATIVE_INFINITY, |
319 |
|
|
Double.NaN, |
320 |
|
|
}; |
321 |
|
|
for (double bound : badBounds) { |
322 |
|
|
try { |
323 |
|
|
rnd.nextDouble(bound); |
324 |
|
|
shouldThrow(); |
325 |
|
|
} catch (IllegalArgumentException success) {} |
326 |
|
|
} |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
dl |
1.1 |
* nextDouble(least, bound) returns least <= value < bound; |
331 |
jsr166 |
1.14 |
* repeated calls produce at least two distinct results |
332 |
dl |
1.1 |
*/ |
333 |
|
|
public void testNextDoubleBounded2() { |
334 |
|
|
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
335 |
|
|
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { |
336 |
|
|
double f = ThreadLocalRandom.current().nextDouble(least, bound); |
337 |
|
|
assertTrue(least <= f && f < bound); |
338 |
|
|
int i = 0; |
339 |
|
|
double j; |
340 |
jsr166 |
1.2 |
while (i < NCALLS && |
341 |
dl |
1.1 |
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
342 |
|
|
assertTrue(least <= j && j < bound); |
343 |
|
|
++i; |
344 |
|
|
} |
345 |
|
|
assertTrue(i < NCALLS); |
346 |
|
|
} |
347 |
|
|
} |
348 |
|
|
} |
349 |
|
|
|
350 |
jsr166 |
1.11 |
/** |
351 |
|
|
* Different threads produce different pseudo-random sequences |
352 |
|
|
*/ |
353 |
|
|
public void testDifferentSequences() { |
354 |
|
|
// Don't use main thread's ThreadLocalRandom - it is likely to |
355 |
|
|
// be polluted by previous tests. |
356 |
|
|
final AtomicReference<ThreadLocalRandom> threadLocalRandom = |
357 |
jsr166 |
1.28 |
new AtomicReference<>(); |
358 |
jsr166 |
1.11 |
final AtomicLong rand = new AtomicLong(); |
359 |
|
|
|
360 |
|
|
long firstRand = 0; |
361 |
|
|
ThreadLocalRandom firstThreadLocalRandom = null; |
362 |
|
|
|
363 |
jsr166 |
1.18 |
Runnable getRandomState = new CheckedRunnable() { |
364 |
jsr166 |
1.11 |
public void realRun() { |
365 |
|
|
ThreadLocalRandom current = ThreadLocalRandom.current(); |
366 |
|
|
assertSame(current, ThreadLocalRandom.current()); |
367 |
dl |
1.12 |
// test bug: the following is not guaranteed and not true in JDK8 |
368 |
|
|
// assertNotSame(current, threadLocalRandom.get()); |
369 |
jsr166 |
1.11 |
rand.set(current.nextLong()); |
370 |
|
|
threadLocalRandom.set(current); |
371 |
|
|
}}; |
372 |
|
|
|
373 |
|
|
Thread first = newStartedThread(getRandomState); |
374 |
|
|
awaitTermination(first); |
375 |
|
|
firstRand = rand.get(); |
376 |
|
|
firstThreadLocalRandom = threadLocalRandom.get(); |
377 |
|
|
|
378 |
|
|
for (int i = 0; i < NCALLS; i++) { |
379 |
|
|
Thread t = newStartedThread(getRandomState); |
380 |
|
|
awaitTermination(t); |
381 |
|
|
if (firstRand != rand.get()) |
382 |
|
|
return; |
383 |
|
|
} |
384 |
|
|
fail("all threads generate the same pseudo-random sequence"); |
385 |
|
|
} |
386 |
|
|
|
387 |
jsr166 |
1.26 |
/** |
388 |
|
|
* Repeated calls to nextBytes produce at least values of different signs for every byte |
389 |
|
|
*/ |
390 |
|
|
public void testNextBytes() { |
391 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
392 |
jsr166 |
1.27 |
int n = rnd.nextInt(1, 20); |
393 |
jsr166 |
1.26 |
byte[] bytes = new byte[n]; |
394 |
|
|
outer: |
395 |
|
|
for (int i = 0; i < n; i++) { |
396 |
|
|
for (int tries = NCALLS; tries-->0; ) { |
397 |
|
|
byte before = bytes[i]; |
398 |
|
|
rnd.nextBytes(bytes); |
399 |
|
|
byte after = bytes[i]; |
400 |
|
|
if (after * before < 0) |
401 |
|
|
continue outer; |
402 |
|
|
} |
403 |
|
|
fail("not enough variation in random bytes"); |
404 |
|
|
} |
405 |
|
|
} |
406 |
|
|
|
407 |
jsr166 |
1.27 |
/** |
408 |
|
|
* Filling an empty array with random bytes succeeds without effect. |
409 |
|
|
*/ |
410 |
|
|
public void testNextBytes_emptyArray() { |
411 |
|
|
ThreadLocalRandom.current().nextBytes(new byte[0]); |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
public void testNextBytes_nullArray() { |
415 |
|
|
try { |
416 |
|
|
ThreadLocalRandom.current().nextBytes(null); |
417 |
|
|
shouldThrow(); |
418 |
|
|
} catch (NullPointerException success) {} |
419 |
|
|
} |
420 |
|
|
|
421 |
dl |
1.1 |
} |