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 |
|
|
18 |
|
return new TestSuite(ThreadLocalRandomTest.class); |
19 |
|
} |
20 |
|
|
21 |
< |
/** |
21 |
> |
/* |
22 |
|
* Testing coverage notes: |
23 |
|
* |
24 |
|
* We don't test randomness properties, but only that repeated |
27 |
|
* across multiples of primes. |
28 |
|
*/ |
29 |
|
|
30 |
< |
// |
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 |
36 |
> |
// max sampled long bound |
37 |
|
static final long MAX_LONG_BOUND = (1L << 42); |
38 |
|
|
39 |
|
/** |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Repeated calls to nextInt produce at least one different result |
50 |
> |
* Repeated calls to nextInt produce at least two distinct results |
51 |
|
*/ |
52 |
|
public void testNextInt() { |
53 |
|
int f = ThreadLocalRandom.current().nextInt(); |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* Repeated calls to nextLong produce at least one different result |
61 |
> |
* Repeated calls to nextLong produce at least two distinct results |
62 |
|
*/ |
63 |
|
public void testNextLong() { |
64 |
|
long f = ThreadLocalRandom.current().nextLong(); |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* Repeated calls to nextBoolean produce at least one different result |
72 |
> |
* Repeated calls to nextBoolean produce at least two distinct results |
73 |
|
*/ |
74 |
|
public void testNextBoolean() { |
75 |
|
boolean f = ThreadLocalRandom.current().nextBoolean(); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* Repeated calls to nextFloat produce at least one different result |
83 |
> |
* Repeated calls to nextFloat produce at least two distinct results |
84 |
|
*/ |
85 |
|
public void testNextFloat() { |
86 |
|
float f = ThreadLocalRandom.current().nextFloat(); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
< |
* Repeated calls to nextDouble produce at least one different result |
94 |
> |
* Repeated calls to nextDouble produce at least two distinct results |
95 |
|
*/ |
96 |
|
public void testNextDouble() { |
97 |
|
double f = ThreadLocalRandom.current().nextDouble(); |
98 |
< |
double i = 0; |
98 |
> |
int i = 0; |
99 |
|
while (i < NCALLS && ThreadLocalRandom.current().nextDouble() == f) |
100 |
|
++i; |
101 |
|
assertTrue(i < NCALLS); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* Repeated calls to nextGaussian produce at least one different result |
105 |
> |
* Repeated calls to nextGaussian produce at least two distinct results |
106 |
|
*/ |
107 |
|
public void testNextGaussian() { |
108 |
|
double f = ThreadLocalRandom.current().nextGaussian(); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* nextInt(negative) throws IllegalArgumentException; |
116 |
> |
* nextInt(negative) throws IllegalArgumentException |
117 |
|
*/ |
118 |
|
public void testNextIntBoundedNeg() { |
119 |
|
try { |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
126 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
127 |
|
*/ |
128 |
|
public void testNextIntBadBounds() { |
129 |
|
try { |
134 |
|
|
135 |
|
/** |
136 |
|
* nextInt(bound) returns 0 <= value < bound; |
137 |
< |
* repeated calls produce at least one different result |
137 |
> |
* repeated calls produce at least two distinct results |
138 |
|
*/ |
139 |
|
public void testNextIntBounded() { |
140 |
|
// sample bound space across prime number increments |
154 |
|
|
155 |
|
/** |
156 |
|
* nextInt(least, bound) returns least <= value < bound; |
157 |
< |
* repeated calls produce at least one different result |
157 |
> |
* repeated calls produce at least two distinct results |
158 |
|
*/ |
159 |
|
public void testNextIntBounded2() { |
160 |
|
for (int least = -15485863; least < MAX_INT_BOUND; least += 524959) { |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* nextLong(negative) throws IllegalArgumentException; |
177 |
> |
* nextLong(negative) throws IllegalArgumentException |
178 |
|
*/ |
179 |
|
public void testNextLongBoundedNeg() { |
180 |
|
try { |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
187 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
188 |
|
*/ |
189 |
|
public void testNextLongBadBounds() { |
190 |
|
try { |
195 |
|
|
196 |
|
/** |
197 |
|
* nextLong(bound) returns 0 <= value < bound; |
198 |
< |
* repeated calls produce at least one different result |
198 |
> |
* repeated calls produce at least two distinct results |
199 |
|
*/ |
200 |
|
public void testNextLongBounded() { |
201 |
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
214 |
|
|
215 |
|
/** |
216 |
|
* nextLong(least, bound) returns least <= value < bound; |
217 |
< |
* repeated calls produce at least one different result |
217 |
> |
* repeated calls produce at least two distinct results |
218 |
|
*/ |
219 |
|
public void testNextLongBounded2() { |
220 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 982451653L) { |
235 |
|
|
236 |
|
/** |
237 |
|
* nextDouble(least, bound) returns least <= value < bound; |
238 |
< |
* repeated calls produce at least one different result |
238 |
> |
* repeated calls produce at least two distinct results |
239 |
|
*/ |
240 |
|
public void testNextDoubleBounded2() { |
241 |
|
for (double least = 0.0001; least < 1.0e20; least *= 8) { |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
+ |
/** |
258 |
+ |
* Different threads produce different pseudo-random sequences |
259 |
+ |
*/ |
260 |
+ |
public void testDifferentSequences() { |
261 |
+ |
// Don't use main thread's ThreadLocalRandom - it is likely to |
262 |
+ |
// be polluted by previous tests. |
263 |
+ |
final AtomicReference<ThreadLocalRandom> threadLocalRandom = |
264 |
+ |
new AtomicReference<ThreadLocalRandom>(); |
265 |
+ |
final AtomicLong rand = new AtomicLong(); |
266 |
+ |
|
267 |
+ |
long firstRand = 0; |
268 |
+ |
ThreadLocalRandom firstThreadLocalRandom = null; |
269 |
+ |
|
270 |
+ |
final CheckedRunnable getRandomState = new CheckedRunnable() { |
271 |
+ |
public void realRun() { |
272 |
+ |
ThreadLocalRandom current = ThreadLocalRandom.current(); |
273 |
+ |
assertSame(current, ThreadLocalRandom.current()); |
274 |
+ |
// test bug: the following is not guaranteed and not true in JDK8 |
275 |
+ |
// assertNotSame(current, threadLocalRandom.get()); |
276 |
+ |
rand.set(current.nextLong()); |
277 |
+ |
threadLocalRandom.set(current); |
278 |
+ |
}}; |
279 |
+ |
|
280 |
+ |
Thread first = newStartedThread(getRandomState); |
281 |
+ |
awaitTermination(first); |
282 |
+ |
firstRand = rand.get(); |
283 |
+ |
firstThreadLocalRandom = threadLocalRandom.get(); |
284 |
+ |
|
285 |
+ |
for (int i = 0; i < NCALLS; i++) { |
286 |
+ |
Thread t = newStartedThread(getRandomState); |
287 |
+ |
awaitTermination(t); |
288 |
+ |
if (firstRand != rand.get()) |
289 |
+ |
return; |
290 |
+ |
} |
291 |
+ |
fail("all threads generate the same pseudo-random sequence"); |
292 |
+ |
} |
293 |
+ |
|
294 |
|
} |