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; |
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); |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
< |
* nextInt(negative) throws IllegalArgumentException; |
131 |
> |
* nextInt(negative) throws IllegalArgumentException |
132 |
|
*/ |
133 |
|
public void testNextIntBoundedNeg() { |
134 |
|
SplittableRandom sr = new SplittableRandom(); |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
142 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
143 |
|
*/ |
144 |
|
public void testNextIntBadBounds() { |
145 |
|
SplittableRandom sr = new SplittableRandom(); |
151 |
|
|
152 |
|
/** |
153 |
|
* nextInt(bound) returns 0 <= value < bound; |
154 |
< |
* repeated calls produce at least one different result |
154 |
> |
* repeated calls produce at least two distinct results |
155 |
|
*/ |
156 |
|
public void testNextIntBounded() { |
157 |
|
SplittableRandom sr = new SplittableRandom(); |
172 |
|
|
173 |
|
/** |
174 |
|
* nextInt(least, bound) returns least <= value < bound; |
175 |
< |
* repeated calls produce at least one different result |
175 |
> |
* repeated calls produce at least two distinct results |
176 |
|
*/ |
177 |
|
public void testNextIntBounded2() { |
178 |
|
SplittableRandom sr = new SplittableRandom(); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
196 |
< |
* nextLong(negative) throws IllegalArgumentException; |
196 |
> |
* nextLong(negative) throws IllegalArgumentException |
197 |
|
*/ |
198 |
|
public void testNextLongBoundedNeg() { |
199 |
|
SplittableRandom sr = new SplittableRandom(); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
207 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
208 |
|
*/ |
209 |
|
public void testNextLongBadBounds() { |
210 |
|
SplittableRandom sr = new SplittableRandom(); |
216 |
|
|
217 |
|
/** |
218 |
|
* nextLong(bound) returns 0 <= value < bound; |
219 |
< |
* repeated calls produce at least one different result |
219 |
> |
* repeated calls produce at least two distinct results |
220 |
|
*/ |
221 |
|
public void testNextLongBounded() { |
222 |
|
SplittableRandom sr = new SplittableRandom(); |
236 |
|
|
237 |
|
/** |
238 |
|
* nextLong(least, bound) returns least <= value < bound; |
239 |
< |
* repeated calls produce at least one different result |
239 |
> |
* repeated calls produce at least two distinct results |
240 |
|
*/ |
241 |
|
public void testNextLongBounded2() { |
242 |
|
SplittableRandom sr = new SplittableRandom(); |
258 |
|
|
259 |
|
/** |
260 |
|
* nextDouble(least, bound) returns least <= value < bound; |
261 |
< |
* repeated calls produce at least one different result |
261 |
> |
* repeated calls produce at least two distinct results |
262 |
|
*/ |
263 |
|
public void testNextDoubleBounded2() { |
264 |
|
SplittableRandom sr = new SplittableRandom(); |
287 |
|
try { |
288 |
|
java.util.stream.IntStream x = r.ints(-1L); |
289 |
|
shouldThrow(); |
290 |
< |
} catch (IllegalArgumentException ok) { |
291 |
< |
} |
290 |
> |
} catch (IllegalArgumentException success) {} |
291 |
> |
try { |
292 |
> |
java.util.stream.IntStream x = r.ints(-1L, 2, 3); |
293 |
> |
shouldThrow(); |
294 |
> |
} catch (IllegalArgumentException success) {} |
295 |
|
try { |
296 |
|
java.util.stream.LongStream x = r.longs(-1L); |
297 |
|
shouldThrow(); |
298 |
< |
} catch (IllegalArgumentException ok) { |
299 |
< |
} |
298 |
> |
} catch (IllegalArgumentException success) {} |
299 |
> |
try { |
300 |
> |
java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); |
301 |
> |
shouldThrow(); |
302 |
> |
} catch (IllegalArgumentException success) {} |
303 |
|
try { |
304 |
|
java.util.stream.DoubleStream x = r.doubles(-1L); |
305 |
|
shouldThrow(); |
306 |
< |
} catch (IllegalArgumentException ok) { |
307 |
< |
} |
306 |
> |
} catch (IllegalArgumentException success) {} |
307 |
> |
try { |
308 |
> |
java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); |
309 |
> |
shouldThrow(); |
310 |
> |
} catch (IllegalArgumentException success) {} |
311 |
|
} |
312 |
|
|
313 |
|
/** |
319 |
|
try { |
320 |
|
java.util.stream.IntStream x = r.ints(2, 1); |
321 |
|
shouldThrow(); |
322 |
< |
} catch (IllegalArgumentException ok) { |
313 |
< |
} |
322 |
> |
} catch (IllegalArgumentException success) {} |
323 |
|
try { |
324 |
< |
java.util.stream.LongStream x = r.longs(1, -2); |
324 |
> |
java.util.stream.IntStream x = r.ints(10, 42, 42); |
325 |
|
shouldThrow(); |
326 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
326 |
> |
} catch (IllegalArgumentException success) {} |
327 |
|
try { |
328 |
< |
java.util.stream.DoubleStream x = r.doubles(0, 0); |
328 |
> |
java.util.stream.LongStream x = r.longs(-1L, -1L); |
329 |
|
shouldThrow(); |
330 |
< |
} catch (IllegalArgumentException ok) { |
331 |
< |
} |
330 |
> |
} catch (IllegalArgumentException success) {} |
331 |
> |
try { |
332 |
> |
java.util.stream.LongStream x = r.longs(10, 1L, -2L); |
333 |
> |
shouldThrow(); |
334 |
> |
} catch (IllegalArgumentException success) {} |
335 |
> |
try { |
336 |
> |
java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); |
337 |
> |
shouldThrow(); |
338 |
> |
} catch (IllegalArgumentException success) {} |
339 |
> |
try { |
340 |
> |
java.util.stream.DoubleStream x = r.doubles(10, .5, .4); |
341 |
> |
shouldThrow(); |
342 |
> |
} catch (IllegalArgumentException success) {} |
343 |
|
} |
344 |
|
|
345 |
|
/** |
351 |
|
long size = 0; |
352 |
|
for (int reps = 0; reps < REPS; ++reps) { |
353 |
|
counter.reset(); |
354 |
< |
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
355 |
< |
assertEquals(counter.sum(), size); |
354 |
> |
r.ints(size).parallel().forEach(x -> counter.increment()); |
355 |
> |
assertEquals(size, counter.sum()); |
356 |
|
size += 524959; |
357 |
|
} |
358 |
|
} |
366 |
|
long size = 0; |
367 |
|
for (int reps = 0; reps < REPS; ++reps) { |
368 |
|
counter.reset(); |
369 |
< |
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
370 |
< |
assertEquals(counter.sum(), size); |
369 |
> |
r.longs(size).parallel().forEach(x -> counter.increment()); |
370 |
> |
assertEquals(size, counter.sum()); |
371 |
|
size += 524959; |
372 |
|
} |
373 |
|
} |
381 |
|
long size = 0; |
382 |
|
for (int reps = 0; reps < REPS; ++reps) { |
383 |
|
counter.reset(); |
384 |
< |
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
385 |
< |
assertEquals(counter.sum(), size); |
384 |
> |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
385 |
> |
assertEquals(size, counter.sum()); |
386 |
|
size += 524959; |
387 |
|
} |
388 |
|
} |
389 |
|
|
371 |
– |
|
390 |
|
/** |
391 |
|
* Each of a parallel sized stream of bounded ints is within bounds |
392 |
|
*/ |
402 |
|
fails.getAndIncrement(); }); |
403 |
|
} |
404 |
|
} |
405 |
< |
assertEquals(fails.get(), 0); |
405 |
> |
assertEquals(0, fails.get()); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
420 |
|
fails.getAndIncrement(); }); |
421 |
|
} |
422 |
|
} |
423 |
< |
assertEquals(fails.get(), 0); |
423 |
> |
assertEquals(0, fails.get()); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
438 |
|
fails.getAndIncrement(); }); |
439 |
|
} |
440 |
|
} |
441 |
< |
assertEquals(fails.get(), 0); |
441 |
> |
assertEquals(0, fails.get()); |
442 |
|
} |
443 |
|
|
444 |
|
/** |
448 |
|
LongAdder counter = new LongAdder(); |
449 |
|
SplittableRandom r = new SplittableRandom(); |
450 |
|
long size = 100; |
451 |
< |
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
452 |
< |
assertEquals(counter.sum(), size); |
451 |
> |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
452 |
> |
assertEquals(size, counter.sum()); |
453 |
|
} |
454 |
|
|
455 |
|
/** |
459 |
|
LongAdder counter = new LongAdder(); |
460 |
|
SplittableRandom r = new SplittableRandom(); |
461 |
|
long size = 100; |
462 |
< |
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
463 |
< |
assertEquals(counter.sum(), size); |
462 |
> |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
463 |
> |
assertEquals(size, counter.sum()); |
464 |
|
} |
465 |
|
|
448 |
– |
|
466 |
|
/** |
467 |
|
* A parallel unsized stream of doubles generates at least 100 values |
468 |
|
*/ |
470 |
|
LongAdder counter = new LongAdder(); |
471 |
|
SplittableRandom r = new SplittableRandom(); |
472 |
|
long size = 100; |
473 |
< |
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
474 |
< |
assertEquals(counter.sum(), size); |
473 |
> |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
474 |
> |
assertEquals(size, counter.sum()); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
481 |
|
LongAdder counter = new LongAdder(); |
482 |
|
SplittableRandom r = new SplittableRandom(); |
483 |
|
long size = 100; |
484 |
< |
r.ints().limit(size).forEach(x -> {counter.increment();}); |
485 |
< |
assertEquals(counter.sum(), size); |
484 |
> |
r.ints().limit(size).forEach(x -> counter.increment()); |
485 |
> |
assertEquals(size, counter.sum()); |
486 |
|
} |
487 |
|
|
488 |
|
/** |
492 |
|
LongAdder counter = new LongAdder(); |
493 |
|
SplittableRandom r = new SplittableRandom(); |
494 |
|
long size = 100; |
495 |
< |
r.longs().limit(size).forEach(x -> {counter.increment();}); |
496 |
< |
assertEquals(counter.sum(), size); |
495 |
> |
r.longs().limit(size).forEach(x -> counter.increment()); |
496 |
> |
assertEquals(size, counter.sum()); |
497 |
|
} |
498 |
|
|
482 |
– |
|
499 |
|
/** |
500 |
|
* A sequential unsized stream of doubles generates at least 100 values |
501 |
|
*/ |
503 |
|
LongAdder counter = new LongAdder(); |
504 |
|
SplittableRandom r = new SplittableRandom(); |
505 |
|
long size = 100; |
506 |
< |
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
507 |
< |
assertEquals(counter.sum(), size); |
506 |
> |
r.doubles().limit(size).forEach(x -> counter.increment()); |
507 |
> |
assertEquals(size, counter.sum()); |
508 |
|
} |
509 |
|
|
494 |
– |
|
510 |
|
} |