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 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
61 |
+ |
final java.lang.reflect.Method m; |
62 |
+ |
try { |
63 |
+ |
m = ThreadLocalRandom.class.getDeclaredMethod( |
64 |
+ |
"next", new Class[] { int.class }); |
65 |
+ |
m.setAccessible(true); |
66 |
+ |
} catch (SecurityException acceptable) { |
67 |
+ |
// Security manager may deny access |
68 |
+ |
return; |
69 |
+ |
} catch (Exception ex) { |
70 |
+ |
// jdk9 module system may deny access |
71 |
+ |
if (ex.getClass().getSimpleName() |
72 |
+ |
.equals("InaccessibleObjectException")) |
73 |
+ |
return; |
74 |
+ |
throw ex; |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
int i; |
78 |
+ |
{ |
79 |
+ |
int val = new java.util.Random().nextInt(4); |
80 |
+ |
for (i = 0; i < NCALLS; i++) { |
81 |
+ |
int q = (int) m.invoke(rnd, new Object[] { 2 }); |
82 |
+ |
if (val == q) break; |
83 |
+ |
} |
84 |
+ |
assertTrue(i < NCALLS); |
85 |
+ |
} |
86 |
+ |
|
87 |
+ |
{ |
88 |
+ |
int r = (int) m.invoke(rnd, new Object[] { 3 }); |
89 |
+ |
for (i = 0; i < NCALLS; i++) { |
90 |
+ |
int q = (int) m.invoke(rnd, new Object[] { 3 }); |
91 |
+ |
assertTrue(q < (1<<3)); |
92 |
+ |
if (r != q) break; |
93 |
+ |
} |
94 |
+ |
assertTrue(i < NCALLS); |
95 |
+ |
} |
96 |
+ |
} |
97 |
+ |
|
98 |
+ |
/** |
99 |
|
* Repeated calls to nextInt produce at least two distinct results |
100 |
|
*/ |
101 |
|
public void testNextInt() { |