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 |
41 |
|
// max sampled int bound |
42 |
|
static final int MAX_INT_BOUND = (1 << 28); |
43 |
|
|
44 |
< |
// Max sampled long bound |
44 |
> |
// max sampled long bound |
45 |
|
static final long MAX_LONG_BOUND = (1L << 42); |
46 |
|
|
47 |
|
// Number of replications for other checks |
48 |
|
static final int REPS = 20; |
49 |
|
|
50 |
|
/** |
51 |
< |
* Repeated calls to nextInt produce at least one different result |
51 |
> |
* Repeated calls to nextInt produce at least two distinct results |
52 |
|
*/ |
53 |
|
public void testNextInt() { |
54 |
|
SplittableRandom sr = new SplittableRandom(); |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* Repeated calls to nextLong produce at least one different result |
63 |
> |
* Repeated calls to nextLong produce at least two distinct results |
64 |
|
*/ |
65 |
|
public void testNextLong() { |
66 |
|
SplittableRandom sr = new SplittableRandom(); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* Repeated calls to nextDouble produce at least one different result |
75 |
> |
* Repeated calls to nextDouble produce at least two distinct results |
76 |
|
*/ |
77 |
|
public void testNextDouble() { |
78 |
|
SplittableRandom sr = new SplittableRandom(); |
79 |
|
double f = sr.nextDouble(); |
80 |
< |
double i = 0; |
80 |
> |
int i = 0; |
81 |
|
while (i < NCALLS && sr.nextDouble() == f) |
82 |
|
++i; |
83 |
|
assertTrue(i < NCALLS); |
91 |
|
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
92 |
|
SplittableRandom sr1 = new SplittableRandom(seed); |
93 |
|
SplittableRandom sr2 = new SplittableRandom(seed); |
94 |
< |
for (int i = 0; i < REPS; ++i) |
94 |
> |
for (int i = 0; i < REPS; ++i) |
95 |
|
assertEquals(sr1.nextLong(), sr2.nextLong()); |
96 |
|
} |
97 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
< |
* nextInt(negative) throws IllegalArgumentException; |
130 |
> |
* nextInt(negative) throws IllegalArgumentException |
131 |
|
*/ |
132 |
|
public void testNextIntBoundedNeg() { |
133 |
|
SplittableRandom sr = new SplittableRandom(); |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
141 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
142 |
|
*/ |
143 |
|
public void testNextIntBadBounds() { |
144 |
|
SplittableRandom sr = new SplittableRandom(); |
150 |
|
|
151 |
|
/** |
152 |
|
* nextInt(bound) returns 0 <= value < bound; |
153 |
< |
* repeated calls produce at least one different result |
153 |
> |
* repeated calls produce at least two distinct results |
154 |
|
*/ |
155 |
|
public void testNextIntBounded() { |
156 |
|
SplittableRandom sr = new SplittableRandom(); |
171 |
|
|
172 |
|
/** |
173 |
|
* nextInt(least, bound) returns least <= value < bound; |
174 |
< |
* repeated calls produce at least one different result |
174 |
> |
* repeated calls produce at least two distinct results |
175 |
|
*/ |
176 |
|
public void testNextIntBounded2() { |
177 |
|
SplittableRandom sr = new SplittableRandom(); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
< |
* nextLong(negative) throws IllegalArgumentException; |
195 |
> |
* nextLong(negative) throws IllegalArgumentException |
196 |
|
*/ |
197 |
|
public void testNextLongBoundedNeg() { |
198 |
|
SplittableRandom sr = new SplittableRandom(); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
206 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
207 |
|
*/ |
208 |
|
public void testNextLongBadBounds() { |
209 |
|
SplittableRandom sr = new SplittableRandom(); |
215 |
|
|
216 |
|
/** |
217 |
|
* nextLong(bound) returns 0 <= value < bound; |
218 |
< |
* repeated calls produce at least one different result |
218 |
> |
* repeated calls produce at least two distinct results |
219 |
|
*/ |
220 |
|
public void testNextLongBounded() { |
221 |
|
SplittableRandom sr = new SplittableRandom(); |
235 |
|
|
236 |
|
/** |
237 |
|
* nextLong(least, bound) returns least <= value < bound; |
238 |
< |
* repeated calls produce at least one different result |
238 |
> |
* repeated calls produce at least two distinct results |
239 |
|
*/ |
240 |
|
public void testNextLongBounded2() { |
241 |
|
SplittableRandom sr = new SplittableRandom(); |
257 |
|
|
258 |
|
/** |
259 |
|
* nextDouble(least, bound) returns least <= value < bound; |
260 |
< |
* repeated calls produce at least one different result |
260 |
> |
* repeated calls produce at least two distinct results |
261 |
|
*/ |
262 |
|
public void testNextDoubleBounded2() { |
263 |
|
SplittableRandom sr = new SplittableRandom(); |
286 |
|
try { |
287 |
|
java.util.stream.IntStream x = r.ints(-1L); |
288 |
|
shouldThrow(); |
289 |
< |
} catch (IllegalArgumentException ok) { |
290 |
< |
} |
289 |
> |
} catch (IllegalArgumentException success) {} |
290 |
> |
try { |
291 |
> |
java.util.stream.IntStream x = r.ints(-1L, 2, 3); |
292 |
> |
shouldThrow(); |
293 |
> |
} catch (IllegalArgumentException success) {} |
294 |
|
try { |
295 |
|
java.util.stream.LongStream x = r.longs(-1L); |
296 |
|
shouldThrow(); |
297 |
< |
} catch (IllegalArgumentException ok) { |
298 |
< |
} |
297 |
> |
} catch (IllegalArgumentException success) {} |
298 |
> |
try { |
299 |
> |
java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); |
300 |
> |
shouldThrow(); |
301 |
> |
} catch (IllegalArgumentException success) {} |
302 |
|
try { |
303 |
|
java.util.stream.DoubleStream x = r.doubles(-1L); |
304 |
|
shouldThrow(); |
305 |
< |
} catch (IllegalArgumentException ok) { |
306 |
< |
} |
305 |
> |
} catch (IllegalArgumentException success) {} |
306 |
> |
try { |
307 |
> |
java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); |
308 |
> |
shouldThrow(); |
309 |
> |
} catch (IllegalArgumentException success) {} |
310 |
|
} |
311 |
|
|
312 |
|
/** |
318 |
|
try { |
319 |
|
java.util.stream.IntStream x = r.ints(2, 1); |
320 |
|
shouldThrow(); |
321 |
< |
} catch (IllegalArgumentException ok) { |
313 |
< |
} |
321 |
> |
} catch (IllegalArgumentException success) {} |
322 |
|
try { |
323 |
< |
java.util.stream.LongStream x = r.longs(1, -2); |
323 |
> |
java.util.stream.IntStream x = r.ints(10, 42, 42); |
324 |
|
shouldThrow(); |
325 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
325 |
> |
} catch (IllegalArgumentException success) {} |
326 |
|
try { |
327 |
< |
java.util.stream.DoubleStream x = r.doubles(0, 0); |
327 |
> |
java.util.stream.LongStream x = r.longs(-1L, -1L); |
328 |
|
shouldThrow(); |
329 |
< |
} catch (IllegalArgumentException ok) { |
330 |
< |
} |
329 |
> |
} catch (IllegalArgumentException success) {} |
330 |
> |
try { |
331 |
> |
java.util.stream.LongStream x = r.longs(10, 1L, -2L); |
332 |
> |
shouldThrow(); |
333 |
> |
} catch (IllegalArgumentException success) {} |
334 |
> |
try { |
335 |
> |
java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); |
336 |
> |
shouldThrow(); |
337 |
> |
} catch (IllegalArgumentException success) {} |
338 |
> |
try { |
339 |
> |
java.util.stream.DoubleStream x = r.doubles(10, .5, .4); |
340 |
> |
shouldThrow(); |
341 |
> |
} catch (IllegalArgumentException success) {} |
342 |
|
} |
343 |
|
|
344 |
|
/** |
351 |
|
for (int reps = 0; reps < REPS; ++reps) { |
352 |
|
counter.reset(); |
353 |
|
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
354 |
< |
assertEquals(counter.sum(), size); |
354 |
> |
assertEquals(size, counter.sum()); |
355 |
|
size += 524959; |
356 |
|
} |
357 |
|
} |
366 |
|
for (int reps = 0; reps < REPS; ++reps) { |
367 |
|
counter.reset(); |
368 |
|
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
369 |
< |
assertEquals(counter.sum(), size); |
369 |
> |
assertEquals(size, counter.sum()); |
370 |
|
size += 524959; |
371 |
|
} |
372 |
|
} |
381 |
|
for (int reps = 0; reps < REPS; ++reps) { |
382 |
|
counter.reset(); |
383 |
|
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
384 |
< |
assertEquals(counter.sum(), size); |
384 |
> |
assertEquals(size, counter.sum()); |
385 |
|
size += 524959; |
386 |
|
} |
387 |
|
} |
397 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
398 |
|
final int lo = least, hi = bound; |
399 |
|
r.ints(size, lo, hi).parallel(). |
400 |
< |
forEach(x -> {if (x < lo || x >= hi) |
400 |
> |
forEach(x -> {if (x < lo || x >= hi) |
401 |
|
fails.getAndIncrement(); }); |
402 |
|
} |
403 |
|
} |
404 |
< |
assertEquals(fails.get(), 0); |
404 |
> |
assertEquals(0, fails.get()); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
415 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
416 |
|
final long lo = least, hi = bound; |
417 |
|
r.longs(size, lo, hi).parallel(). |
418 |
< |
forEach(x -> {if (x < lo || x >= hi) |
418 |
> |
forEach(x -> {if (x < lo || x >= hi) |
419 |
|
fails.getAndIncrement(); }); |
420 |
|
} |
421 |
|
} |
422 |
< |
assertEquals(fails.get(), 0); |
422 |
> |
assertEquals(0, fails.get()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
433 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
434 |
|
final double lo = least, hi = bound; |
435 |
|
r.doubles(size, lo, hi).parallel(). |
436 |
< |
forEach(x -> {if (x < lo || x >= hi) |
436 |
> |
forEach(x -> {if (x < lo || x >= hi) |
437 |
|
fails.getAndIncrement(); }); |
438 |
|
} |
439 |
|
} |
440 |
< |
assertEquals(fails.get(), 0); |
440 |
> |
assertEquals(0, fails.get()); |
441 |
> |
} |
442 |
> |
|
443 |
> |
/** |
444 |
> |
* A parallel unsized stream of ints generates at least 100 values |
445 |
> |
*/ |
446 |
> |
public void testUnsizedIntsCount() { |
447 |
> |
LongAdder counter = new LongAdder(); |
448 |
> |
SplittableRandom r = new SplittableRandom(); |
449 |
> |
long size = 100; |
450 |
> |
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
451 |
> |
assertEquals(size, counter.sum()); |
452 |
> |
} |
453 |
> |
|
454 |
> |
/** |
455 |
> |
* A parallel unsized stream of longs generates at least 100 values |
456 |
> |
*/ |
457 |
> |
public void testUnsizedLongsCount() { |
458 |
> |
LongAdder counter = new LongAdder(); |
459 |
> |
SplittableRandom r = new SplittableRandom(); |
460 |
> |
long size = 100; |
461 |
> |
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
462 |
> |
assertEquals(size, counter.sum()); |
463 |
> |
} |
464 |
> |
|
465 |
> |
/** |
466 |
> |
* A parallel unsized stream of doubles generates at least 100 values |
467 |
> |
*/ |
468 |
> |
public void testUnsizedDoublesCount() { |
469 |
> |
LongAdder counter = new LongAdder(); |
470 |
> |
SplittableRandom r = new SplittableRandom(); |
471 |
> |
long size = 100; |
472 |
> |
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
473 |
> |
assertEquals(size, counter.sum()); |
474 |
> |
} |
475 |
> |
|
476 |
> |
/** |
477 |
> |
* A sequential unsized stream of ints generates at least 100 values |
478 |
> |
*/ |
479 |
> |
public void testUnsizedIntsCountSeq() { |
480 |
> |
LongAdder counter = new LongAdder(); |
481 |
> |
SplittableRandom r = new SplittableRandom(); |
482 |
> |
long size = 100; |
483 |
> |
r.ints().limit(size).forEach(x -> {counter.increment();}); |
484 |
> |
assertEquals(size, counter.sum()); |
485 |
> |
} |
486 |
> |
|
487 |
> |
/** |
488 |
> |
* A sequential unsized stream of longs generates at least 100 values |
489 |
> |
*/ |
490 |
> |
public void testUnsizedLongsCountSeq() { |
491 |
> |
LongAdder counter = new LongAdder(); |
492 |
> |
SplittableRandom r = new SplittableRandom(); |
493 |
> |
long size = 100; |
494 |
> |
r.longs().limit(size).forEach(x -> {counter.increment();}); |
495 |
> |
assertEquals(size, counter.sum()); |
496 |
> |
} |
497 |
> |
|
498 |
> |
/** |
499 |
> |
* A sequential unsized stream of doubles generates at least 100 values |
500 |
> |
*/ |
501 |
> |
public void testUnsizedDoublesCountSeq() { |
502 |
> |
LongAdder counter = new LongAdder(); |
503 |
> |
SplittableRandom r = new SplittableRandom(); |
504 |
> |
long size = 100; |
505 |
> |
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
506 |
> |
assertEquals(size, counter.sum()); |
507 |
|
} |
508 |
|
|
509 |
|
} |