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.*; |
7 |
|
import java.util.*; |
8 |
< |
|
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()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
< |
return new TestSuite(ThreadLocalRandomTest.class); |
18 |
> |
return new TestSuite(ThreadLocalRandomTest.class); |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
|
* Testing coverage notes: |
23 |
< |
* |
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 |
< |
// |
30 |
> |
// |
31 |
|
static final int NCALLS = 10000; |
32 |
|
|
33 |
|
// max sampled int bound |
42 |
|
public void testSetSeed() { |
43 |
|
try { |
44 |
|
ThreadLocalRandom.current().setSeed(17); |
45 |
< |
} catch(UnsupportedOperationException success) { |
46 |
< |
} |
45 |
> |
shouldThrow(); |
46 |
> |
} catch (UnsupportedOperationException success) {} |
47 |
|
} |
48 |
|
|
49 |
|
/** |
68 |
|
assertTrue(i < NCALLS); |
69 |
|
} |
70 |
|
|
70 |
– |
|
71 |
|
/** |
72 |
|
* Repeated calls to nextBoolean produce at least one different result |
73 |
|
*/ |
112 |
|
assertTrue(i < NCALLS); |
113 |
|
} |
114 |
|
|
115 |
– |
|
115 |
|
/** |
116 |
|
* nextInt(negative) throws IllegalArgumentException; |
117 |
|
*/ |
118 |
|
public void testNextIntBoundedNeg() { |
119 |
|
try { |
120 |
< |
int f = ThreadLocalRandom.current().nextInt(-17); |
121 |
< |
} catch(IllegalArgumentException success) { |
122 |
< |
} |
120 |
> |
int f = ThreadLocalRandom.current().nextInt(-17); |
121 |
> |
shouldThrow(); |
122 |
> |
} catch (IllegalArgumentException success) {} |
123 |
|
} |
124 |
|
|
125 |
|
/** |
127 |
|
*/ |
128 |
|
public void testNextIntBadBounds() { |
129 |
|
try { |
130 |
< |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
131 |
< |
} catch(IllegalArgumentException success) { |
132 |
< |
} |
130 |
> |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
131 |
> |
shouldThrow(); |
132 |
> |
} catch (IllegalArgumentException success) {} |
133 |
|
} |
134 |
|
|
136 |
– |
|
135 |
|
/** |
136 |
|
* nextInt(bound) returns 0 <= value < bound; |
137 |
|
* repeated calls produce at least one different result |
143 |
|
assertTrue(0 <= f && f < bound); |
144 |
|
int i = 0; |
145 |
|
int j; |
146 |
< |
while (i < NCALLS && |
146 |
> |
while (i < NCALLS && |
147 |
|
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
148 |
|
assertTrue(0 <= j && j < bound); |
149 |
|
++i; |
152 |
|
} |
153 |
|
} |
154 |
|
|
157 |
– |
|
155 |
|
/** |
156 |
|
* nextInt(least, bound) returns least <= value < bound; |
157 |
|
* repeated calls produce at least one different result |
163 |
|
assertTrue(least <= f && f < bound); |
164 |
|
int i = 0; |
165 |
|
int j; |
166 |
< |
while (i < NCALLS && |
166 |
> |
while (i < NCALLS && |
167 |
|
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
168 |
|
assertTrue(least <= j && j < bound); |
169 |
|
++i; |
178 |
|
*/ |
179 |
|
public void testNextLongBoundedNeg() { |
180 |
|
try { |
181 |
< |
long f = ThreadLocalRandom.current().nextLong(-17); |
182 |
< |
} catch(IllegalArgumentException success) { |
183 |
< |
} |
181 |
> |
long f = ThreadLocalRandom.current().nextLong(-17); |
182 |
> |
shouldThrow(); |
183 |
> |
} catch (IllegalArgumentException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
188 |
|
*/ |
189 |
|
public void testNextLongBadBounds() { |
190 |
|
try { |
191 |
< |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
192 |
< |
} catch(IllegalArgumentException success) { |
193 |
< |
} |
191 |
> |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
192 |
> |
shouldThrow(); |
193 |
> |
} catch (IllegalArgumentException success) {} |
194 |
|
} |
195 |
|
|
196 |
|
/** |
203 |
|
assertTrue(0 <= f && f < bound); |
204 |
|
int i = 0; |
205 |
|
long j; |
206 |
< |
while (i < NCALLS && |
206 |
> |
while (i < NCALLS && |
207 |
|
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
208 |
|
assertTrue(0 <= j && j < bound); |
209 |
|
++i; |
223 |
|
assertTrue(least <= f && f < bound); |
224 |
|
int i = 0; |
225 |
|
long j; |
226 |
< |
while (i < NCALLS && |
226 |
> |
while (i < NCALLS && |
227 |
|
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
228 |
|
assertTrue(least <= j && j < bound); |
229 |
|
++i; |
233 |
|
} |
234 |
|
} |
235 |
|
|
239 |
– |
|
236 |
|
/** |
237 |
|
* nextDouble(least, bound) returns least <= value < bound; |
238 |
|
* repeated calls produce at least one different result |
244 |
|
assertTrue(least <= f && f < bound); |
245 |
|
int i = 0; |
246 |
|
double j; |
247 |
< |
while (i < NCALLS && |
247 |
> |
while (i < NCALLS && |
248 |
|
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
249 |
|
assertTrue(least <= j && j < bound); |
250 |
|
++i; |
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 |
+ |
assertNotSame(current, threadLocalRandom.get()); |
275 |
+ |
rand.set(current.nextLong()); |
276 |
+ |
threadLocalRandom.set(current); |
277 |
+ |
}}; |
278 |
+ |
|
279 |
+ |
Thread first = newStartedThread(getRandomState); |
280 |
+ |
awaitTermination(first); |
281 |
+ |
firstRand = rand.get(); |
282 |
+ |
firstThreadLocalRandom = threadLocalRandom.get(); |
283 |
+ |
|
284 |
+ |
for (int i = 0; i < NCALLS; i++) { |
285 |
+ |
Thread t = newStartedThread(getRandomState); |
286 |
+ |
awaitTermination(t); |
287 |
+ |
if (firstRand != rand.get()) |
288 |
+ |
return; |
289 |
+ |
} |
290 |
+ |
fail("all threads generate the same pseudo-random sequence"); |
291 |
+ |
} |
292 |
|
|
293 |
|
} |