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.SplittableRandom; |
8 |
|
import java.util.concurrent.atomic.AtomicInteger; |
10 |
– |
import java.util.concurrent.atomic.AtomicLong; |
9 |
|
import java.util.concurrent.atomic.LongAdder; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
13 |
+ |
|
14 |
|
public class SplittableRandomTest 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(SplittableRandomTest.class); |
40 |
|
static final int NCALLS = 10000; |
41 |
|
|
42 |
|
// max sampled int bound |
43 |
< |
static final int MAX_INT_BOUND = (1 << 28); |
43 |
> |
static final int MAX_INT_BOUND = (1 << 26); |
44 |
|
|
45 |
|
// max sampled long bound |
46 |
< |
static final long MAX_LONG_BOUND = (1L << 42); |
46 |
> |
static final long MAX_LONG_BOUND = (1L << 40); |
47 |
|
|
48 |
|
// Number of replications for other checks |
49 |
|
static final int REPS = |
90 |
|
* same values for nextLong. |
91 |
|
*/ |
92 |
|
public void testSeedConstructor() { |
93 |
< |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
93 |
> |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
94 |
|
SplittableRandom sr1 = new SplittableRandom(seed); |
95 |
|
SplittableRandom sr2 = new SplittableRandom(seed); |
96 |
|
for (int i = 0; i < REPS; ++i) |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* nextInt(negative) throws IllegalArgumentException |
132 |
> |
* nextInt(non-positive) throws IllegalArgumentException |
133 |
|
*/ |
134 |
< |
public void testNextIntBoundedNeg() { |
134 |
> |
public void testNextIntBoundNonPositive() { |
135 |
|
SplittableRandom sr = new SplittableRandom(); |
136 |
< |
try { |
137 |
< |
int f = sr.nextInt(-17); |
138 |
< |
shouldThrow(); |
139 |
< |
} catch (IllegalArgumentException success) {} |
136 |
> |
Runnable[] throwingActions = { |
137 |
> |
() -> sr.nextInt(-17), |
138 |
> |
() -> sr.nextInt(0), |
139 |
> |
() -> sr.nextInt(Integer.MIN_VALUE), |
140 |
> |
}; |
141 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
146 |
|
*/ |
147 |
|
public void testNextIntBadBounds() { |
148 |
|
SplittableRandom sr = new SplittableRandom(); |
149 |
< |
try { |
150 |
< |
int f = sr.nextInt(17, 2); |
151 |
< |
shouldThrow(); |
152 |
< |
} catch (IllegalArgumentException success) {} |
149 |
> |
Runnable[] throwingActions = { |
150 |
> |
() -> sr.nextInt(17, 2), |
151 |
> |
() -> sr.nextInt(-42, -42), |
152 |
> |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE), |
153 |
> |
}; |
154 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
160 |
|
*/ |
161 |
|
public void testNextIntBounded() { |
162 |
|
SplittableRandom sr = new SplittableRandom(); |
163 |
+ |
for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1)); |
164 |
|
// sample bound space across prime number increments |
165 |
|
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
166 |
|
int f = sr.nextInt(bound); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* nextLong(negative) throws IllegalArgumentException |
202 |
> |
* nextLong(non-positive) throws IllegalArgumentException |
203 |
|
*/ |
204 |
< |
public void testNextLongBoundedNeg() { |
204 |
> |
public void testNextLongBoundNonPositive() { |
205 |
|
SplittableRandom sr = new SplittableRandom(); |
206 |
< |
try { |
207 |
< |
long f = sr.nextLong(-17); |
208 |
< |
shouldThrow(); |
209 |
< |
} catch (IllegalArgumentException success) {} |
206 |
> |
Runnable[] throwingActions = { |
207 |
> |
() -> sr.nextLong(-17L), |
208 |
> |
() -> sr.nextLong(0L), |
209 |
> |
() -> sr.nextLong(Long.MIN_VALUE), |
210 |
> |
}; |
211 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
212 |
|
} |
213 |
|
|
214 |
|
/** |
216 |
|
*/ |
217 |
|
public void testNextLongBadBounds() { |
218 |
|
SplittableRandom sr = new SplittableRandom(); |
219 |
< |
try { |
220 |
< |
long f = sr.nextLong(17, 2); |
221 |
< |
shouldThrow(); |
222 |
< |
} catch (IllegalArgumentException success) {} |
219 |
> |
Runnable[] throwingActions = { |
220 |
> |
() -> sr.nextLong(17L, 2L), |
221 |
> |
() -> sr.nextLong(-42L, -42L), |
222 |
> |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE), |
223 |
> |
}; |
224 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
230 |
|
*/ |
231 |
|
public void testNextLongBounded() { |
232 |
|
SplittableRandom sr = new SplittableRandom(); |
233 |
+ |
for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L)); |
234 |
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
235 |
|
long f = sr.nextLong(bound); |
236 |
|
assertTrue(0 <= f && f < bound); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
272 |
+ |
*/ |
273 |
+ |
public void testNextDoubleBoundNonPositive() { |
274 |
+ |
SplittableRandom sr = new SplittableRandom(); |
275 |
+ |
Runnable[] throwingActions = { |
276 |
+ |
() -> sr.nextDouble(-17.0d), |
277 |
+ |
() -> sr.nextDouble(0.0d), |
278 |
+ |
() -> sr.nextDouble(-Double.MIN_VALUE), |
279 |
+ |
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
280 |
+ |
() -> sr.nextDouble(Double.NaN), |
281 |
+ |
}; |
282 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
283 |
+ |
} |
284 |
+ |
|
285 |
+ |
/** |
286 |
+ |
* nextDouble(! (least < bound)) throws IllegalArgumentException |
287 |
+ |
*/ |
288 |
+ |
public void testNextDoubleBadBounds() { |
289 |
+ |
SplittableRandom sr = new SplittableRandom(); |
290 |
+ |
Runnable[] throwingActions = { |
291 |
+ |
() -> sr.nextDouble(17.0d, 2.0d), |
292 |
+ |
() -> sr.nextDouble(-42.0d, -42.0d), |
293 |
+ |
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
294 |
+ |
() -> sr.nextDouble(Double.NaN, 0.0d), |
295 |
+ |
() -> sr.nextDouble(0.0d, Double.NaN), |
296 |
+ |
}; |
297 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
298 |
+ |
} |
299 |
+ |
|
300 |
+ |
// TODO: Test infinite bounds! |
301 |
+ |
//() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), |
302 |
+ |
//() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), |
303 |
+ |
|
304 |
+ |
/** |
305 |
|
* nextDouble(least, bound) returns least <= value < bound; |
306 |
|
* repeated calls produce at least two distinct results |
307 |
|
*/ |
412 |
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
413 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
414 |
|
final int lo = least, hi = bound; |
415 |
< |
r.ints(size, lo, hi).parallel(). |
416 |
< |
forEach(x -> {if (x < lo || x >= hi) |
417 |
< |
fails.getAndIncrement(); }); |
415 |
> |
r.ints(size, lo, hi).parallel().forEach( |
416 |
> |
x -> { |
417 |
> |
if (x < lo || x >= hi) |
418 |
> |
fails.getAndIncrement(); }); |
419 |
|
} |
420 |
|
} |
421 |
|
assertEquals(0, fails.get()); |
431 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
432 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
433 |
|
final long lo = least, hi = bound; |
434 |
< |
r.longs(size, lo, hi).parallel(). |
435 |
< |
forEach(x -> {if (x < lo || x >= hi) |
436 |
< |
fails.getAndIncrement(); }); |
434 |
> |
r.longs(size, lo, hi).parallel().forEach( |
435 |
> |
x -> { |
436 |
> |
if (x < lo || x >= hi) |
437 |
> |
fails.getAndIncrement(); }); |
438 |
|
} |
439 |
|
} |
440 |
|
assertEquals(0, fails.get()); |
450 |
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
451 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
452 |
|
final double lo = least, hi = bound; |
453 |
< |
r.doubles(size, lo, hi).parallel(). |
454 |
< |
forEach(x -> {if (x < lo || x >= hi) |
455 |
< |
fails.getAndIncrement(); }); |
453 |
> |
r.doubles(size, lo, hi).parallel().forEach( |
454 |
> |
x -> { |
455 |
> |
if (x < lo || x >= hi) |
456 |
> |
fails.getAndIncrement(); }); |
457 |
|
} |
458 |
|
} |
459 |
|
assertEquals(0, fails.get()); |