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.*; |
11 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
12 |
|
|
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(ThreadLocalRandomTest.class); |
17 |
> |
return new TestSuite(ThreadLocalRandomTest.class); |
18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
|
* Testing coverage notes: |
22 |
< |
* |
22 |
> |
* |
23 |
|
* We don't test randomness properties, but only that repeated |
24 |
|
* calls, up to NCALLS tries, produce at least one different |
25 |
|
* result. For bounded versions, we sample various intervals |
26 |
|
* across multiples of primes. |
27 |
|
*/ |
28 |
|
|
29 |
< |
// |
29 |
> |
// |
30 |
|
static final int NCALLS = 10000; |
31 |
|
|
32 |
|
// max sampled int bound |
41 |
|
public void testSetSeed() { |
42 |
|
try { |
43 |
|
ThreadLocalRandom.current().setSeed(17); |
44 |
< |
} catch(UnsupportedOperationException success) { |
45 |
< |
} |
44 |
> |
shouldThrow(); |
45 |
> |
} catch (UnsupportedOperationException success) {} |
46 |
|
} |
47 |
|
|
48 |
|
/** |
118 |
|
*/ |
119 |
|
public void testNextIntBoundedNeg() { |
120 |
|
try { |
121 |
< |
int f = ThreadLocalRandom.current().nextInt(-17); |
122 |
< |
} catch(IllegalArgumentException success) { |
123 |
< |
} |
121 |
> |
int f = ThreadLocalRandom.current().nextInt(-17); |
122 |
> |
shouldThrow(); |
123 |
> |
} catch (IllegalArgumentException success) {} |
124 |
|
} |
125 |
|
|
126 |
|
/** |
128 |
|
*/ |
129 |
|
public void testNextIntBadBounds() { |
130 |
|
try { |
131 |
< |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
132 |
< |
} catch(IllegalArgumentException success) { |
133 |
< |
} |
131 |
> |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
132 |
> |
shouldThrow(); |
133 |
> |
} catch (IllegalArgumentException success) {} |
134 |
|
} |
135 |
|
|
136 |
|
|
145 |
|
assertTrue(0 <= f && f < bound); |
146 |
|
int i = 0; |
147 |
|
int j; |
148 |
< |
while (i < NCALLS && |
148 |
> |
while (i < NCALLS && |
149 |
|
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
150 |
|
assertTrue(0 <= j && j < bound); |
151 |
|
++i; |
166 |
|
assertTrue(least <= f && f < bound); |
167 |
|
int i = 0; |
168 |
|
int j; |
169 |
< |
while (i < NCALLS && |
169 |
> |
while (i < NCALLS && |
170 |
|
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
171 |
|
assertTrue(least <= j && j < bound); |
172 |
|
++i; |
181 |
|
*/ |
182 |
|
public void testNextLongBoundedNeg() { |
183 |
|
try { |
184 |
< |
long f = ThreadLocalRandom.current().nextLong(-17); |
185 |
< |
} catch(IllegalArgumentException success) { |
186 |
< |
} |
184 |
> |
long f = ThreadLocalRandom.current().nextLong(-17); |
185 |
> |
shouldThrow(); |
186 |
> |
} catch (IllegalArgumentException success) {} |
187 |
|
} |
188 |
|
|
189 |
|
/** |
191 |
|
*/ |
192 |
|
public void testNextLongBadBounds() { |
193 |
|
try { |
194 |
< |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
195 |
< |
} catch(IllegalArgumentException success) { |
196 |
< |
} |
194 |
> |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
195 |
> |
shouldThrow(); |
196 |
> |
} catch (IllegalArgumentException success) {} |
197 |
|
} |
198 |
|
|
199 |
|
/** |
206 |
|
assertTrue(0 <= f && f < bound); |
207 |
|
int i = 0; |
208 |
|
long j; |
209 |
< |
while (i < NCALLS && |
209 |
> |
while (i < NCALLS && |
210 |
|
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
211 |
|
assertTrue(0 <= j && j < bound); |
212 |
|
++i; |
226 |
|
assertTrue(least <= f && f < bound); |
227 |
|
int i = 0; |
228 |
|
long j; |
229 |
< |
while (i < NCALLS && |
229 |
> |
while (i < NCALLS && |
230 |
|
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
231 |
|
assertTrue(least <= j && j < bound); |
232 |
|
++i; |
248 |
|
assertTrue(least <= f && f < bound); |
249 |
|
int i = 0; |
250 |
|
double j; |
251 |
< |
while (i < NCALLS && |
251 |
> |
while (i < NCALLS && |
252 |
|
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
253 |
|
assertTrue(least <= j && j < bound); |
254 |
|
++i; |