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.*; |
6 |
> |
|
7 |
|
import java.util.concurrent.ThreadLocalRandom; |
8 |
|
import java.util.concurrent.atomic.AtomicLong; |
9 |
|
import java.util.concurrent.atomic.AtomicReference; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
13 |
+ |
|
14 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadLocalRandomTest.class); |
52 |
|
} |
53 |
|
|
54 |
|
/** |
55 |
+ |
* 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 |
+ |
// Inhibit "An illegal reflective access operation has occurred" |
61 |
+ |
if (!testImplementationDetails) return; |
62 |
+ |
|
63 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
64 |
+ |
final java.lang.reflect.Method m; |
65 |
+ |
try { |
66 |
+ |
m = ThreadLocalRandom.class.getDeclaredMethod( |
67 |
+ |
"next", new Class[] { int.class }); |
68 |
+ |
m.setAccessible(true); |
69 |
+ |
} catch (SecurityException acceptable) { |
70 |
+ |
// Security manager may deny access |
71 |
+ |
return; |
72 |
+ |
} catch (Exception ex) { |
73 |
+ |
// jdk9 module system may deny access |
74 |
+ |
if (ex.getClass().getSimpleName() |
75 |
+ |
.equals("InaccessibleObjectException")) |
76 |
+ |
return; |
77 |
+ |
throw ex; |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
int i; |
81 |
+ |
{ |
82 |
+ |
int val = new java.util.Random().nextInt(4); |
83 |
+ |
for (i = 0; i < NCALLS; i++) { |
84 |
+ |
int q = (int) m.invoke(rnd, new Object[] { 2 }); |
85 |
+ |
if (val == q) break; |
86 |
+ |
} |
87 |
+ |
assertTrue(i < NCALLS); |
88 |
+ |
} |
89 |
+ |
|
90 |
+ |
{ |
91 |
+ |
int r = (int) m.invoke(rnd, new Object[] { 3 }); |
92 |
+ |
for (i = 0; i < NCALLS; i++) { |
93 |
+ |
int q = (int) m.invoke(rnd, new Object[] { 3 }); |
94 |
+ |
assertTrue(q < (1<<3)); |
95 |
+ |
if (r != q) break; |
96 |
+ |
} |
97 |
+ |
assertTrue(i < NCALLS); |
98 |
+ |
} |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
/** |
102 |
|
* Repeated calls to nextInt produce at least two distinct results |
103 |
|
*/ |
104 |
|
public void testNextInt() { |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* nextInt(negative) throws IllegalArgumentException |
168 |
> |
* nextInt(non-positive) throws IllegalArgumentException |
169 |
|
*/ |
170 |
< |
public void testNextIntBoundedNeg() { |
171 |
< |
try { |
172 |
< |
int f = ThreadLocalRandom.current().nextInt(-17); |
173 |
< |
shouldThrow(); |
174 |
< |
} catch (IllegalArgumentException success) {} |
170 |
> |
public void testNextIntBoundNonPositive() { |
171 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
172 |
> |
for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { |
173 |
> |
try { |
174 |
> |
rnd.nextInt(bound); |
175 |
> |
shouldThrow(); |
176 |
> |
} catch (IllegalArgumentException success) {} |
177 |
> |
} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
|
* nextInt(least >= bound) throws IllegalArgumentException |
182 |
|
*/ |
183 |
|
public void testNextIntBadBounds() { |
184 |
< |
try { |
185 |
< |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
186 |
< |
shouldThrow(); |
187 |
< |
} catch (IllegalArgumentException success) {} |
184 |
> |
int[][] badBoundss = { |
185 |
> |
{ 17, 2 }, |
186 |
> |
{ -42, -42 }, |
187 |
> |
{ Integer.MAX_VALUE, Integer.MIN_VALUE }, |
188 |
> |
}; |
189 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
190 |
> |
for (int[] badBounds : badBoundss) { |
191 |
> |
try { |
192 |
> |
rnd.nextInt(badBounds[0], badBounds[1]); |
193 |
> |
shouldThrow(); |
194 |
> |
} catch (IllegalArgumentException success) {} |
195 |
> |
} |
196 |
|
} |
197 |
|
|
198 |
|
/** |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* nextLong(negative) throws IllegalArgumentException |
240 |
> |
* nextLong(non-positive) throws IllegalArgumentException |
241 |
|
*/ |
242 |
< |
public void testNextLongBoundedNeg() { |
243 |
< |
try { |
244 |
< |
long f = ThreadLocalRandom.current().nextLong(-17); |
245 |
< |
shouldThrow(); |
246 |
< |
} catch (IllegalArgumentException success) {} |
242 |
> |
public void testNextLongBoundNonPositive() { |
243 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
244 |
> |
for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { |
245 |
> |
try { |
246 |
> |
rnd.nextLong(bound); |
247 |
> |
shouldThrow(); |
248 |
> |
} catch (IllegalArgumentException success) {} |
249 |
> |
} |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
|
* nextLong(least >= bound) throws IllegalArgumentException |
254 |
|
*/ |
255 |
|
public void testNextLongBadBounds() { |
256 |
< |
try { |
257 |
< |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
258 |
< |
shouldThrow(); |
259 |
< |
} catch (IllegalArgumentException success) {} |
256 |
> |
long[][] badBoundss = { |
257 |
> |
{ 17L, 2L }, |
258 |
> |
{ -42L, -42L }, |
259 |
> |
{ Long.MAX_VALUE, Long.MIN_VALUE }, |
260 |
> |
}; |
261 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
262 |
> |
for (long[] badBounds : badBoundss) { |
263 |
> |
try { |
264 |
> |
rnd.nextLong(badBounds[0], badBounds[1]); |
265 |
> |
shouldThrow(); |
266 |
> |
} catch (IllegalArgumentException success) {} |
267 |
> |
} |
268 |
|
} |
269 |
|
|
270 |
|
/** |
307 |
|
} |
308 |
|
} |
309 |
|
|
310 |
+ |
/** |
311 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
312 |
+ |
*/ |
313 |
+ |
public void testNextDoubleBoundNonPositive() { |
314 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
315 |
+ |
double[] badBounds = { |
316 |
+ |
0.0d, |
317 |
+ |
-17.0d, |
318 |
+ |
-Double.MIN_VALUE, |
319 |
+ |
Double.NEGATIVE_INFINITY, |
320 |
+ |
Double.NaN, |
321 |
+ |
}; |
322 |
+ |
for (double bound : badBounds) { |
323 |
+ |
try { |
324 |
+ |
rnd.nextDouble(bound); |
325 |
+ |
shouldThrow(); |
326 |
+ |
} catch (IllegalArgumentException success) {} |
327 |
+ |
} |
328 |
+ |
} |
329 |
+ |
|
330 |
|
/** |
331 |
|
* nextDouble(least, bound) returns least <= value < bound; |
332 |
|
* repeated calls produce at least two distinct results |