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