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.*; |
8 |
|
import java.util.*; |
9 |
|
|
10 |
– |
|
10 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
11 |
|
|
12 |
|
public static void main(String[] args) { |
13 |
< |
junit.textui.TestRunner.run (suite()); |
13 |
> |
junit.textui.TestRunner.run(suite()); |
14 |
|
} |
15 |
|
public static Test suite() { |
16 |
< |
return new TestSuite(ThreadLocalRandomTest.class); |
16 |
> |
return new TestSuite(ThreadLocalRandomTest.class); |
17 |
|
} |
18 |
|
|
19 |
|
/** |
20 |
|
* Testing coverage notes: |
21 |
< |
* |
21 |
> |
* |
22 |
|
* We don't test randomness properties, but only that repeated |
23 |
|
* calls, up to NCALLS tries, produce at least one different |
24 |
|
* result. For bounded versions, we sample various intervals |
25 |
|
* across multiples of primes. |
26 |
|
*/ |
27 |
|
|
28 |
< |
// |
28 |
> |
// |
29 |
|
static final int NCALLS = 10000; |
30 |
|
|
31 |
|
// max sampled int bound |
40 |
|
public void testSetSeed() { |
41 |
|
try { |
42 |
|
ThreadLocalRandom.current().setSeed(17); |
43 |
< |
} catch(UnsupportedOperationException success) { |
44 |
< |
} |
43 |
> |
shouldThrow(); |
44 |
> |
} catch (UnsupportedOperationException success) {} |
45 |
|
} |
46 |
|
|
47 |
|
/** |
66 |
|
assertTrue(i < NCALLS); |
67 |
|
} |
68 |
|
|
70 |
– |
|
69 |
|
/** |
70 |
|
* Repeated calls to nextBoolean produce at least one different result |
71 |
|
*/ |
110 |
|
assertTrue(i < NCALLS); |
111 |
|
} |
112 |
|
|
115 |
– |
|
113 |
|
/** |
114 |
|
* nextInt(negative) throws IllegalArgumentException; |
115 |
|
*/ |
116 |
|
public void testNextIntBoundedNeg() { |
117 |
|
try { |
118 |
< |
int f = ThreadLocalRandom.current().nextInt(-17); |
119 |
< |
} catch(IllegalArgumentException success) { |
120 |
< |
} |
118 |
> |
int f = ThreadLocalRandom.current().nextInt(-17); |
119 |
> |
shouldThrow(); |
120 |
> |
} catch (IllegalArgumentException success) {} |
121 |
|
} |
122 |
|
|
123 |
|
/** |
125 |
|
*/ |
126 |
|
public void testNextIntBadBounds() { |
127 |
|
try { |
128 |
< |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
129 |
< |
} catch(IllegalArgumentException success) { |
130 |
< |
} |
128 |
> |
int f = ThreadLocalRandom.current().nextInt(17, 2); |
129 |
> |
shouldThrow(); |
130 |
> |
} catch (IllegalArgumentException success) {} |
131 |
|
} |
132 |
|
|
136 |
– |
|
133 |
|
/** |
134 |
|
* nextInt(bound) returns 0 <= value < bound; |
135 |
|
* repeated calls produce at least one different result |
141 |
|
assertTrue(0 <= f && f < bound); |
142 |
|
int i = 0; |
143 |
|
int j; |
144 |
< |
while (i < NCALLS && |
144 |
> |
while (i < NCALLS && |
145 |
|
(j = ThreadLocalRandom.current().nextInt(bound)) == f) { |
146 |
|
assertTrue(0 <= j && j < bound); |
147 |
|
++i; |
150 |
|
} |
151 |
|
} |
152 |
|
|
157 |
– |
|
153 |
|
/** |
154 |
|
* nextInt(least, bound) returns least <= value < bound; |
155 |
|
* repeated calls produce at least one different result |
161 |
|
assertTrue(least <= f && f < bound); |
162 |
|
int i = 0; |
163 |
|
int j; |
164 |
< |
while (i < NCALLS && |
164 |
> |
while (i < NCALLS && |
165 |
|
(j = ThreadLocalRandom.current().nextInt(least, bound)) == f) { |
166 |
|
assertTrue(least <= j && j < bound); |
167 |
|
++i; |
176 |
|
*/ |
177 |
|
public void testNextLongBoundedNeg() { |
178 |
|
try { |
179 |
< |
long f = ThreadLocalRandom.current().nextLong(-17); |
180 |
< |
} catch(IllegalArgumentException success) { |
181 |
< |
} |
179 |
> |
long f = ThreadLocalRandom.current().nextLong(-17); |
180 |
> |
shouldThrow(); |
181 |
> |
} catch (IllegalArgumentException success) {} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
186 |
|
*/ |
187 |
|
public void testNextLongBadBounds() { |
188 |
|
try { |
189 |
< |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
190 |
< |
} catch(IllegalArgumentException success) { |
191 |
< |
} |
189 |
> |
long f = ThreadLocalRandom.current().nextLong(17, 2); |
190 |
> |
shouldThrow(); |
191 |
> |
} catch (IllegalArgumentException success) {} |
192 |
|
} |
193 |
|
|
194 |
|
/** |
201 |
|
assertTrue(0 <= f && f < bound); |
202 |
|
int i = 0; |
203 |
|
long j; |
204 |
< |
while (i < NCALLS && |
204 |
> |
while (i < NCALLS && |
205 |
|
(j = ThreadLocalRandom.current().nextLong(bound)) == f) { |
206 |
|
assertTrue(0 <= j && j < bound); |
207 |
|
++i; |
221 |
|
assertTrue(least <= f && f < bound); |
222 |
|
int i = 0; |
223 |
|
long j; |
224 |
< |
while (i < NCALLS && |
224 |
> |
while (i < NCALLS && |
225 |
|
(j = ThreadLocalRandom.current().nextLong(least, bound)) == f) { |
226 |
|
assertTrue(least <= j && j < bound); |
227 |
|
++i; |
231 |
|
} |
232 |
|
} |
233 |
|
|
239 |
– |
|
234 |
|
/** |
235 |
|
* nextDouble(least, bound) returns least <= value < bound; |
236 |
|
* repeated calls produce at least one different result |
242 |
|
assertTrue(least <= f && f < bound); |
243 |
|
int i = 0; |
244 |
|
double j; |
245 |
< |
while (i < NCALLS && |
245 |
> |
while (i < NCALLS && |
246 |
|
(j = ThreadLocalRandom.current().nextDouble(least, bound)) == f) { |
247 |
|
assertTrue(least <= j && j < bound); |
248 |
|
++i; |
252 |
|
} |
253 |
|
} |
254 |
|
|
261 |
– |
|
255 |
|
} |