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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
import junit.framework.*; |
7 |
import java.util.*; |
8 |
import java.util.concurrent.ThreadLocalRandom; |
9 |
import java.util.concurrent.atomic.AtomicLong; |
10 |
import java.util.concurrent.atomic.AtomicReference; |
11 |
|
12 |
public class ThreadLocalRandomTest extends JSR166TestCase { |
13 |
|
14 |
public static void main(String[] args) { |
15 |
junit.textui.TestRunner.run(suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(ThreadLocalRandomTest.class); |
19 |
} |
20 |
|
21 |
/* |
22 |
* Testing coverage notes: |
23 |
* |
24 |
* We don't test randomness properties, but only that repeated |
25 |
* calls, up to NCALLS tries, produce at least one different |
26 |
* result. For bounded versions, we sample various intervals |
27 |
* across multiples of primes. |
28 |
*/ |
29 |
|
30 |
// max numbers of calls to detect getting stuck on one value |
31 |
static final int NCALLS = 10000; |
32 |
|
33 |
// max sampled int bound |
34 |
static final int MAX_INT_BOUND = (1 << 28); |
35 |
|
36 |
// max sampled long bound |
37 |
static final long MAX_LONG_BOUND = (1L << 42); |
38 |
|
39 |
// Number of replications for other checks |
40 |
static final int REPS = 20; |
41 |
|
42 |
/** |
43 |
* setSeed throws UnsupportedOperationException |
44 |
*/ |
45 |
public void testSetSeed() { |
46 |
try { |
47 |
ThreadLocalRandom.current().setSeed(17); |
48 |
shouldThrow(); |
49 |
} catch (UnsupportedOperationException success) {} |
50 |
} |
51 |
|
52 |
/** |
53 |
* Repeated calls to nextInt produce at least two distinct results |
54 |
*/ |
55 |
public void testNextInt() { |
56 |
int f = ThreadLocalRandom.current().nextInt(); |
57 |
int i = 0; |
58 |
while (i < NCALLS && ThreadLocalRandom.current().nextInt() == f) |
59 |
++i; |
60 |
assertTrue(i < NCALLS); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Repeated calls to nextLong produce at least two distinct results |
65 |
*/ |
66 |
public void testNextLong() { |
67 |
long f = ThreadLocalRandom.current().nextLong(); |
68 |
int i = 0; |
69 |
while (i < NCALLS && ThreadLocalRandom.current().nextLong() == f) |
70 |
++i; |
71 |
assertTrue(i < NCALLS); |
72 |
} |
73 |
|
74 |
/** |
75 |
* Repeated calls to nextBoolean produce at least two distinct results |
76 |
*/ |
77 |
public void testNextBoolean() { |
78 |
boolean f = ThreadLocalRandom.current().nextBoolean(); |
79 |
int i = 0; |
80 |
while (i < NCALLS && ThreadLocalRandom.current().nextBoolean() == f) |
81 |
++i; |
82 |
assertTrue(i < NCALLS); |
83 |
} |
84 |
|
85 |
/** |
86 |
* Repeated calls to nextFloat produce at least two distinct results |
87 |
*/ |
88 |
public void testNextFloat() { |
89 |
float f = ThreadLocalRandom.current().nextFloat(); |
90 |
int i = 0; |
91 |
while (i < NCALLS && ThreadLocalRandom.current().nextFloat() == f) |
92 |
++i; |
93 |
assertTrue(i < NCALLS); |
94 |
} |
95 |
|
96 |
/** |
97 |
* Repeated calls to nextDouble produce at least two distinct results |
98 |
*/ |
99 |
public void testNextDouble() { |
100 |
double f = ThreadLocalRandom.current().nextDouble(); |
101 |
int i = 0; |
102 |
while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) |
103 |
++i; |
104 |
assertTrue(i < NCALLS); |
105 |
} |
106 |
|
107 |
/** |
108 |
* Repeated calls to nextGaussian produce at least two distinct results |
109 |
*/ |
110 |
public void testNextGaussian() { |
111 |
double f = ThreadLocalRandom.current().nextGaussian(); |
112 |
int i = 0; |
113 |
while (i < NCALLS && ThreadLocalRandom.current().nextGaussian() == f) |
114 |
++i; |
115 |
assertTrue(i < NCALLS); |
116 |
} |
117 |
|
118 |
/** |
119 |
* nextInt(non-positive) throws IllegalArgumentException |
120 |
*/ |
121 |
public void testNextIntBoundNonPositive() { |
122 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
123 |
for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { |
124 |
try { |
125 |
rnd.nextInt(bound); |
126 |
shouldThrow(); |
127 |
} catch (IllegalArgumentException success) {} |
128 |
} |
129 |
} |
130 |
|
131 |
/** |
132 |
* nextInt(least >= bound) throws IllegalArgumentException |
133 |
*/ |
134 |
public void testNextIntBadBounds() { |
135 |
int[][] badBoundss = { |
136 |
{ 17, 2 }, |
137 |
{ -42, -42 }, |
138 |
{ Integer.MAX_VALUE, Integer.MIN_VALUE }, |
139 |
}; |
140 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
141 |
for (int[] badBounds : badBoundss) { |
142 |
try { |
143 |
rnd.nextInt(badBounds[0], badBounds[1]); |
144 |
shouldThrow(); |
145 |
} catch (IllegalArgumentException success) {} |
146 |
} |
147 |
} |
148 |
|
149 |
/** |
150 |
* nextInt(bound) returns 0 <= value < bound; |
151 |
* repeated calls produce at least two distinct results |
152 |
*/ |
153 |
public void testNextIntBounded() { |
154 |
// sample bound space across prime number increments |
155 |
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
156 |
int f = ThreadLocalRandom.current().nextInt(bound); |
157 |
assertTrue(0 <= f && f < bound); |
158 |
int i = 0; |
159 |
int j; |
160 |
while (i < NCALLS && |
161 |
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
162 |
assertTrue(0 <= j && j < bound); |
163 |
++i; |
164 |
} |
165 |
assertTrue(i < NCALLS); |
166 |
} |
167 |
} |
168 |
|
169 |
/** |
170 |
* nextInt(least, bound) returns least <= value < bound; |
171 |
* repeated calls produce at least two distinct results |
172 |
*/ |
173 |
public void testNextIntBounded2() { |
174 |
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
175 |
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 49979687) { |
176 |
int f = ThreadLocalRandom.current().nextInt(least, bound); |
177 |
assertTrue(least <= f && f < bound); |
178 |
int i = 0; |
179 |
int j; |
180 |
while (i < NCALLS && |
181 |
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
182 |
assertTrue(least <= j && j < bound); |
183 |
++i; |
184 |
} |
185 |
assertTrue(i < NCALLS); |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
/** |
191 |
* nextLong(non-positive) throws IllegalArgumentException |
192 |
*/ |
193 |
public void testNextLongBoundNonPositive() { |
194 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
195 |
for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { |
196 |
try { |
197 |
rnd.nextLong(bound); |
198 |
shouldThrow(); |
199 |
} catch (IllegalArgumentException success) {} |
200 |
} |
201 |
} |
202 |
|
203 |
/** |
204 |
* nextLong(least >= bound) throws IllegalArgumentException |
205 |
*/ |
206 |
public void testNextLongBadBounds() { |
207 |
long[][] badBoundss = { |
208 |
{ 17L, 2L }, |
209 |
{ -42L, -42L }, |
210 |
{ Long.MAX_VALUE, Long.MIN_VALUE }, |
211 |
}; |
212 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
213 |
for (long[] badBounds : badBoundss) { |
214 |
try { |
215 |
rnd.nextLong(badBounds[0], badBounds[1]); |
216 |
shouldThrow(); |
217 |
} catch (IllegalArgumentException success) {} |
218 |
} |
219 |
} |
220 |
|
221 |
/** |
222 |
* nextLong(bound) returns 0 <= value < bound; |
223 |
* repeated calls produce at least two distinct results |
224 |
*/ |
225 |
public void testNextLongBounded() { |
226 |
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
227 |
long f = ThreadLocalRandom.current().nextLong(bound); |
228 |
assertTrue(0 <= f && f < bound); |
229 |
int i = 0; |
230 |
long j; |
231 |
while (i < NCALLS && |
232 |
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
233 |
assertTrue(0 <= j && j < bound); |
234 |
++i; |
235 |
} |
236 |
assertTrue(i < NCALLS); |
237 |
} |
238 |
} |
239 |
|
240 |
/** |
241 |
* nextLong(least, bound) returns least <= value < bound; |
242 |
* repeated calls produce at least two distinct results |
243 |
*/ |
244 |
public void testNextLongBounded2() { |
245 |
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
246 |
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
247 |
long f = ThreadLocalRandom.current().nextLong(least, bound); |
248 |
assertTrue(least <= f && f < bound); |
249 |
int i = 0; |
250 |
long j; |
251 |
while (i < NCALLS && |
252 |
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
253 |
assertTrue(least <= j && j < bound); |
254 |
++i; |
255 |
} |
256 |
assertTrue(i < NCALLS); |
257 |
} |
258 |
} |
259 |
} |
260 |
|
261 |
/** |
262 |
* nextDouble(non-positive) throws IllegalArgumentException |
263 |
*/ |
264 |
public void testNextDoubleBoundNonPositive() { |
265 |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
266 |
double[] badBounds = { |
267 |
0.0d, |
268 |
-17.0d, |
269 |
-Double.MIN_VALUE, |
270 |
Double.NEGATIVE_INFINITY, |
271 |
Double.NaN, |
272 |
}; |
273 |
for (double bound : badBounds) { |
274 |
try { |
275 |
rnd.nextDouble(bound); |
276 |
shouldThrow(); |
277 |
} catch (IllegalArgumentException success) {} |
278 |
} |
279 |
} |
280 |
|
281 |
/** |
282 |
* nextDouble(least, bound) returns least <= value < bound; |
283 |
* repeated calls produce at least two distinct results |
284 |
*/ |
285 |
public void testNextDoubleBounded2() { |
286 |
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
287 |
for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) { |
288 |
double f = ThreadLocalRandom.current().nextDouble(least, bound); |
289 |
assertTrue(least <= f && f < bound); |
290 |
int i = 0; |
291 |
double j; |
292 |
while (i < NCALLS && |
293 |
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
294 |
assertTrue(least <= j && j < bound); |
295 |
++i; |
296 |
} |
297 |
assertTrue(i < NCALLS); |
298 |
} |
299 |
} |
300 |
} |
301 |
|
302 |
/** |
303 |
* Different threads produce different pseudo-random sequences |
304 |
*/ |
305 |
public void testDifferentSequences() { |
306 |
// Don't use main thread's ThreadLocalRandom - it is likely to |
307 |
// be polluted by previous tests. |
308 |
final AtomicReference<ThreadLocalRandom> threadLocalRandom = |
309 |
new AtomicReference<ThreadLocalRandom>(); |
310 |
final AtomicLong rand = new AtomicLong(); |
311 |
|
312 |
long firstRand = 0; |
313 |
ThreadLocalRandom firstThreadLocalRandom = null; |
314 |
|
315 |
Runnable getRandomState = new CheckedRunnable() { |
316 |
public void realRun() { |
317 |
ThreadLocalRandom current = ThreadLocalRandom.current(); |
318 |
assertSame(current, ThreadLocalRandom.current()); |
319 |
// test bug: the following is not guaranteed and not true in JDK8 |
320 |
// assertNotSame(current, threadLocalRandom.get()); |
321 |
rand.set(current.nextLong()); |
322 |
threadLocalRandom.set(current); |
323 |
}}; |
324 |
|
325 |
Thread first = newStartedThread(getRandomState); |
326 |
awaitTermination(first); |
327 |
firstRand = rand.get(); |
328 |
firstThreadLocalRandom = threadLocalRandom.get(); |
329 |
|
330 |
for (int i = 0; i < NCALLS; i++) { |
331 |
Thread t = newStartedThread(getRandomState); |
332 |
awaitTermination(t); |
333 |
if (firstRand != rand.get()) |
334 |
return; |
335 |
} |
336 |
fail("all threads generate the same pseudo-random sequence"); |
337 |
} |
338 |
|
339 |
} |