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) { |
23 |
|
/* |
24 |
|
* Testing coverage notes: |
25 |
|
* |
26 |
< |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest |
26 |
> |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest. |
27 |
|
* |
28 |
< |
* 2. This set of tests do not check for random number generator |
29 |
< |
* quality. But we check for minimal API compliance by requiring |
30 |
< |
* that repeated calls to nextX methods, up to NCALLS tries, |
31 |
< |
* produce at least one different result. (In some possible |
32 |
< |
* universe, a "correct" implementation might fail, but the odds |
33 |
< |
* are vastly less than that of encountering a hardware failure |
34 |
< |
* while running the test.) For bounded nextX methods, we sample |
35 |
< |
* various intervals across multiples of primes. In other tests, |
36 |
< |
* we repeat under REPS different values. |
28 |
> |
* 2. These tests do not check for random number generator quality. |
29 |
> |
* But we check for minimal API compliance by requiring that |
30 |
> |
* repeated calls to nextX methods, up to NCALLS tries, produce at |
31 |
> |
* least two distinct results. (In some possible universe, a |
32 |
> |
* "correct" implementation might fail, but the odds are vastly |
33 |
> |
* less than that of encountering a hardware failure while running |
34 |
> |
* the test.) For bounded nextX methods, we sample various |
35 |
> |
* intervals across multiples of primes. In other tests, we repeat |
36 |
> |
* under REPS different values. |
37 |
|
*/ |
38 |
|
|
39 |
|
// max numbers of calls to detect getting stuck on one value |
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); |
45 |
> |
// max sampled long bound |
46 |
> |
static final long MAX_LONG_BOUND = (1L << 40); |
47 |
|
|
48 |
|
// Number of replications for other checks |
49 |
< |
static final int REPS = 20; |
49 |
> |
static final int REPS = |
50 |
> |
Integer.getInteger("SplittableRandomTest.reps", 4); |
51 |
|
|
52 |
|
/** |
53 |
< |
* Repeated calls to nextInt produce at least one different result |
53 |
> |
* Repeated calls to nextInt produce at least two distinct results |
54 |
|
*/ |
55 |
|
public void testNextInt() { |
56 |
|
SplittableRandom sr = new SplittableRandom(); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* Repeated calls to nextLong produce at least one different result |
65 |
> |
* Repeated calls to nextLong produce at least two distinct results |
66 |
|
*/ |
67 |
|
public void testNextLong() { |
68 |
|
SplittableRandom sr = new SplittableRandom(); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* Repeated calls to nextDouble produce at least one different result |
77 |
> |
* Repeated calls to nextDouble produce at least two distinct results |
78 |
|
*/ |
79 |
|
public void testNextDouble() { |
80 |
|
SplittableRandom sr = new SplittableRandom(); |
81 |
|
double f = sr.nextDouble(); |
82 |
< |
double i = 0; |
82 |
> |
int i = 0; |
83 |
|
while (i < NCALLS && sr.nextDouble() == f) |
84 |
|
++i; |
85 |
|
assertTrue(i < NCALLS); |
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) |
96 |
> |
for (int i = 0; i < REPS; ++i) |
97 |
|
assertEquals(sr1.nextLong(), sr2.nextLong()); |
98 |
|
} |
99 |
|
} |
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 |
|
/** |
145 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
145 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
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 |
|
/** |
158 |
|
* nextInt(bound) returns 0 <= value < bound; |
159 |
< |
* repeated calls produce at least one different result |
159 |
> |
* repeated calls produce at least two distinct results |
160 |
|
*/ |
161 |
|
public void testNextIntBounded() { |
162 |
|
SplittableRandom sr = new SplittableRandom(); |
177 |
|
|
178 |
|
/** |
179 |
|
* nextInt(least, bound) returns least <= value < bound; |
180 |
< |
* repeated calls produce at least one different result |
180 |
> |
* repeated calls produce at least two distinct results |
181 |
|
*/ |
182 |
|
public void testNextIntBounded2() { |
183 |
|
SplittableRandom sr = new SplittableRandom(); |
198 |
|
} |
199 |
|
|
200 |
|
/** |
201 |
< |
* nextLong(negative) throws IllegalArgumentException; |
201 |
> |
* nextLong(non-positive) throws IllegalArgumentException |
202 |
|
*/ |
203 |
< |
public void testNextLongBoundedNeg() { |
203 |
> |
public void testNextLongBoundNonPositive() { |
204 |
|
SplittableRandom sr = new SplittableRandom(); |
205 |
< |
try { |
206 |
< |
long f = sr.nextLong(-17); |
207 |
< |
shouldThrow(); |
208 |
< |
} catch (IllegalArgumentException success) {} |
205 |
> |
Runnable[] throwingActions = { |
206 |
> |
() -> sr.nextLong(-17L), |
207 |
> |
() -> sr.nextLong(0L), |
208 |
> |
() -> sr.nextLong(Long.MIN_VALUE), |
209 |
> |
}; |
210 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
211 |
|
} |
212 |
|
|
213 |
|
/** |
214 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
214 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
215 |
|
*/ |
216 |
|
public void testNextLongBadBounds() { |
217 |
|
SplittableRandom sr = new SplittableRandom(); |
218 |
< |
try { |
219 |
< |
long f = sr.nextLong(17, 2); |
220 |
< |
shouldThrow(); |
221 |
< |
} catch (IllegalArgumentException success) {} |
218 |
> |
Runnable[] throwingActions = { |
219 |
> |
() -> sr.nextLong(17L, 2L), |
220 |
> |
() -> sr.nextLong(-42L, -42L), |
221 |
> |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE), |
222 |
> |
}; |
223 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
|
* nextLong(bound) returns 0 <= value < bound; |
228 |
< |
* repeated calls produce at least one different result |
228 |
> |
* repeated calls produce at least two distinct results |
229 |
|
*/ |
230 |
|
public void testNextLongBounded() { |
231 |
|
SplittableRandom sr = new SplittableRandom(); |
245 |
|
|
246 |
|
/** |
247 |
|
* nextLong(least, bound) returns least <= value < bound; |
248 |
< |
* repeated calls produce at least one different result |
248 |
> |
* repeated calls produce at least two distinct results |
249 |
|
*/ |
250 |
|
public void testNextLongBounded2() { |
251 |
|
SplittableRandom sr = new SplittableRandom(); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
270 |
+ |
*/ |
271 |
+ |
public void testNextDoubleBoundNonPositive() { |
272 |
+ |
SplittableRandom sr = new SplittableRandom(); |
273 |
+ |
Runnable[] throwingActions = { |
274 |
+ |
() -> sr.nextDouble(-17.0d), |
275 |
+ |
() -> sr.nextDouble(0.0d), |
276 |
+ |
() -> sr.nextDouble(-Double.MIN_VALUE), |
277 |
+ |
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
278 |
+ |
() -> sr.nextDouble(Double.NaN), |
279 |
+ |
}; |
280 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
281 |
+ |
} |
282 |
+ |
|
283 |
+ |
/** |
284 |
+ |
* nextDouble(! (least < bound)) throws IllegalArgumentException |
285 |
+ |
*/ |
286 |
+ |
public void testNextDoubleBadBounds() { |
287 |
+ |
SplittableRandom sr = new SplittableRandom(); |
288 |
+ |
Runnable[] throwingActions = { |
289 |
+ |
() -> sr.nextDouble(17.0d, 2.0d), |
290 |
+ |
() -> sr.nextDouble(-42.0d, -42.0d), |
291 |
+ |
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
292 |
+ |
() -> sr.nextDouble(Double.NaN, 0.0d), |
293 |
+ |
() -> sr.nextDouble(0.0d, Double.NaN), |
294 |
+ |
}; |
295 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
// TODO: Test infinite bounds! |
299 |
+ |
//() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), |
300 |
+ |
//() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), |
301 |
+ |
|
302 |
+ |
/** |
303 |
|
* nextDouble(least, bound) returns least <= value < bound; |
304 |
< |
* repeated calls produce at least one different result |
304 |
> |
* repeated calls produce at least two distinct results |
305 |
|
*/ |
306 |
|
public void testNextDoubleBounded2() { |
307 |
|
SplittableRandom sr = new SplittableRandom(); |
327 |
|
*/ |
328 |
|
public void testBadStreamSize() { |
329 |
|
SplittableRandom r = new SplittableRandom(); |
330 |
< |
try { |
331 |
< |
java.util.stream.IntStream x = r.ints(-1L); |
332 |
< |
shouldThrow(); |
333 |
< |
} catch (IllegalArgumentException ok) { |
334 |
< |
} |
335 |
< |
try { |
336 |
< |
java.util.stream.LongStream x = r.longs(-1L); |
337 |
< |
shouldThrow(); |
338 |
< |
} catch (IllegalArgumentException ok) { |
295 |
< |
} |
296 |
< |
try { |
297 |
< |
java.util.stream.DoubleStream x = r.doubles(-1L); |
298 |
< |
shouldThrow(); |
299 |
< |
} catch (IllegalArgumentException ok) { |
300 |
< |
} |
330 |
> |
Runnable[] throwingActions = { |
331 |
> |
() -> { java.util.stream.IntStream x = r.ints(-1L); }, |
332 |
> |
() -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); }, |
333 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L); }, |
334 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); }, |
335 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L); }, |
336 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }, |
337 |
> |
}; |
338 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
344 |
|
*/ |
345 |
|
public void testBadStreamBounds() { |
346 |
|
SplittableRandom r = new SplittableRandom(); |
347 |
< |
try { |
348 |
< |
java.util.stream.IntStream x = r.ints(2, 1); |
349 |
< |
shouldThrow(); |
350 |
< |
} catch (IllegalArgumentException ok) { |
351 |
< |
} |
352 |
< |
try { |
353 |
< |
java.util.stream.LongStream x = r.longs(1, -2); |
354 |
< |
shouldThrow(); |
355 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
319 |
< |
try { |
320 |
< |
java.util.stream.DoubleStream x = r.doubles(0, 0); |
321 |
< |
shouldThrow(); |
322 |
< |
} catch (IllegalArgumentException ok) { |
323 |
< |
} |
347 |
> |
Runnable[] throwingActions = { |
348 |
> |
() -> { java.util.stream.IntStream x = r.ints(2, 1); }, |
349 |
> |
() -> { java.util.stream.IntStream x = r.ints(10, 42, 42); }, |
350 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L); }, |
351 |
> |
() -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); }, |
352 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); }, |
353 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }, |
354 |
> |
}; |
355 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
356 |
|
} |
357 |
|
|
358 |
|
/** |
364 |
|
long size = 0; |
365 |
|
for (int reps = 0; reps < REPS; ++reps) { |
366 |
|
counter.reset(); |
367 |
< |
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
368 |
< |
assertEquals(counter.sum(), size); |
367 |
> |
r.ints(size).parallel().forEach(x -> counter.increment()); |
368 |
> |
assertEquals(size, counter.sum()); |
369 |
|
size += 524959; |
370 |
|
} |
371 |
|
} |
379 |
|
long size = 0; |
380 |
|
for (int reps = 0; reps < REPS; ++reps) { |
381 |
|
counter.reset(); |
382 |
< |
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
383 |
< |
assertEquals(counter.sum(), size); |
382 |
> |
r.longs(size).parallel().forEach(x -> counter.increment()); |
383 |
> |
assertEquals(size, counter.sum()); |
384 |
|
size += 524959; |
385 |
|
} |
386 |
|
} |
394 |
|
long size = 0; |
395 |
|
for (int reps = 0; reps < REPS; ++reps) { |
396 |
|
counter.reset(); |
397 |
< |
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
398 |
< |
assertEquals(counter.sum(), size); |
397 |
> |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
398 |
> |
assertEquals(size, counter.sum()); |
399 |
|
size += 524959; |
400 |
|
} |
401 |
|
} |
402 |
|
|
371 |
– |
|
403 |
|
/** |
404 |
|
* Each of a parallel sized stream of bounded ints is within bounds |
405 |
|
*/ |
411 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
412 |
|
final int lo = least, hi = bound; |
413 |
|
r.ints(size, lo, hi).parallel(). |
414 |
< |
forEach(x -> {if (x < lo || x >= hi) |
414 |
> |
forEach(x -> {if (x < lo || x >= hi) |
415 |
|
fails.getAndIncrement(); }); |
416 |
|
} |
417 |
|
} |
418 |
< |
assertEquals(fails.get(), 0); |
418 |
> |
assertEquals(0, fails.get()); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
429 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
430 |
|
final long lo = least, hi = bound; |
431 |
|
r.longs(size, lo, hi).parallel(). |
432 |
< |
forEach(x -> {if (x < lo || x >= hi) |
432 |
> |
forEach(x -> {if (x < lo || x >= hi) |
433 |
|
fails.getAndIncrement(); }); |
434 |
|
} |
435 |
|
} |
436 |
< |
assertEquals(fails.get(), 0); |
436 |
> |
assertEquals(0, fails.get()); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
447 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
448 |
|
final double lo = least, hi = bound; |
449 |
|
r.doubles(size, lo, hi).parallel(). |
450 |
< |
forEach(x -> {if (x < lo || x >= hi) |
450 |
> |
forEach(x -> {if (x < lo || x >= hi) |
451 |
|
fails.getAndIncrement(); }); |
452 |
|
} |
453 |
|
} |
454 |
< |
assertEquals(fails.get(), 0); |
454 |
> |
assertEquals(0, fails.get()); |
455 |
|
} |
456 |
|
|
457 |
|
/** |
461 |
|
LongAdder counter = new LongAdder(); |
462 |
|
SplittableRandom r = new SplittableRandom(); |
463 |
|
long size = 100; |
464 |
< |
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
465 |
< |
assertEquals(counter.sum(), size); |
464 |
> |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
465 |
> |
assertEquals(size, counter.sum()); |
466 |
|
} |
467 |
|
|
468 |
|
/** |
472 |
|
LongAdder counter = new LongAdder(); |
473 |
|
SplittableRandom r = new SplittableRandom(); |
474 |
|
long size = 100; |
475 |
< |
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
476 |
< |
assertEquals(counter.sum(), size); |
475 |
> |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
476 |
> |
assertEquals(size, counter.sum()); |
477 |
|
} |
478 |
|
|
448 |
– |
|
479 |
|
/** |
480 |
|
* A parallel unsized stream of doubles generates at least 100 values |
481 |
|
*/ |
483 |
|
LongAdder counter = new LongAdder(); |
484 |
|
SplittableRandom r = new SplittableRandom(); |
485 |
|
long size = 100; |
486 |
< |
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
487 |
< |
assertEquals(counter.sum(), size); |
486 |
> |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
487 |
> |
assertEquals(size, counter.sum()); |
488 |
|
} |
489 |
|
|
490 |
|
/** |
494 |
|
LongAdder counter = new LongAdder(); |
495 |
|
SplittableRandom r = new SplittableRandom(); |
496 |
|
long size = 100; |
497 |
< |
r.ints().limit(size).forEach(x -> {counter.increment();}); |
498 |
< |
assertEquals(counter.sum(), size); |
497 |
> |
r.ints().limit(size).forEach(x -> counter.increment()); |
498 |
> |
assertEquals(size, counter.sum()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
505 |
|
LongAdder counter = new LongAdder(); |
506 |
|
SplittableRandom r = new SplittableRandom(); |
507 |
|
long size = 100; |
508 |
< |
r.longs().limit(size).forEach(x -> {counter.increment();}); |
509 |
< |
assertEquals(counter.sum(), size); |
508 |
> |
r.longs().limit(size).forEach(x -> counter.increment()); |
509 |
> |
assertEquals(size, counter.sum()); |
510 |
|
} |
511 |
|
|
482 |
– |
|
512 |
|
/** |
513 |
|
* A sequential unsized stream of doubles generates at least 100 values |
514 |
|
*/ |
516 |
|
LongAdder counter = new LongAdder(); |
517 |
|
SplittableRandom r = new SplittableRandom(); |
518 |
|
long size = 100; |
519 |
< |
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
520 |
< |
assertEquals(counter.sum(), size); |
519 |
> |
r.doubles().limit(size).forEach(x -> counter.increment()); |
520 |
> |
assertEquals(size, counter.sum()); |
521 |
|
} |
522 |
|
|
494 |
– |
|
523 |
|
} |