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); |
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.LongStream x = r.longs(-1L); |
292 |
|
shouldThrow(); |
293 |
< |
} catch (IllegalArgumentException ok) { |
295 |
< |
} |
293 |
> |
} catch (IllegalArgumentException success) {} |
294 |
|
try { |
295 |
|
java.util.stream.DoubleStream x = r.doubles(-1L); |
296 |
|
shouldThrow(); |
297 |
< |
} catch (IllegalArgumentException ok) { |
300 |
< |
} |
297 |
> |
} catch (IllegalArgumentException success) {} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
306 |
|
try { |
307 |
|
java.util.stream.IntStream x = r.ints(2, 1); |
308 |
|
shouldThrow(); |
309 |
< |
} catch (IllegalArgumentException ok) { |
313 |
< |
} |
309 |
> |
} catch (IllegalArgumentException success) {} |
310 |
|
try { |
311 |
|
java.util.stream.LongStream x = r.longs(1, -2); |
312 |
|
shouldThrow(); |
313 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
313 |
> |
} catch (IllegalArgumentException success) {} |
314 |
|
try { |
315 |
|
java.util.stream.DoubleStream x = r.doubles(0, 0); |
316 |
|
shouldThrow(); |
317 |
< |
} catch (IllegalArgumentException ok) { |
323 |
< |
} |
317 |
> |
} catch (IllegalArgumentException success) {} |
318 |
|
} |
319 |
|
|
320 |
|
/** |
327 |
|
for (int reps = 0; reps < REPS; ++reps) { |
328 |
|
counter.reset(); |
329 |
|
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
330 |
< |
assertEquals(counter.sum(), size); |
330 |
> |
assertEquals(size, counter.sum()); |
331 |
|
size += 524959; |
332 |
|
} |
333 |
|
} |
342 |
|
for (int reps = 0; reps < REPS; ++reps) { |
343 |
|
counter.reset(); |
344 |
|
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
345 |
< |
assertEquals(counter.sum(), size); |
345 |
> |
assertEquals(size, counter.sum()); |
346 |
|
size += 524959; |
347 |
|
} |
348 |
|
} |
357 |
|
for (int reps = 0; reps < REPS; ++reps) { |
358 |
|
counter.reset(); |
359 |
|
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
360 |
< |
assertEquals(counter.sum(), size); |
360 |
> |
assertEquals(size, counter.sum()); |
361 |
|
size += 524959; |
362 |
|
} |
363 |
|
} |
364 |
|
|
371 |
– |
|
365 |
|
/** |
366 |
|
* Each of a parallel sized stream of bounded ints is within bounds |
367 |
|
*/ |
377 |
|
fails.getAndIncrement(); }); |
378 |
|
} |
379 |
|
} |
380 |
< |
assertEquals(fails.get(), 0); |
380 |
> |
assertEquals(0, fails.get()); |
381 |
|
} |
382 |
|
|
383 |
|
/** |
395 |
|
fails.getAndIncrement(); }); |
396 |
|
} |
397 |
|
} |
398 |
< |
assertEquals(fails.get(), 0); |
398 |
> |
assertEquals(0, fails.get()); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
413 |
|
fails.getAndIncrement(); }); |
414 |
|
} |
415 |
|
} |
416 |
< |
assertEquals(fails.get(), 0); |
416 |
> |
assertEquals(0, fails.get()); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
424 |
|
SplittableRandom r = new SplittableRandom(); |
425 |
|
long size = 100; |
426 |
|
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
427 |
< |
assertEquals(counter.sum(), size); |
427 |
> |
assertEquals(size, counter.sum()); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
435 |
|
SplittableRandom r = new SplittableRandom(); |
436 |
|
long size = 100; |
437 |
|
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
438 |
< |
assertEquals(counter.sum(), size); |
438 |
> |
assertEquals(size, counter.sum()); |
439 |
|
} |
440 |
|
|
448 |
– |
|
441 |
|
/** |
442 |
|
* A parallel unsized stream of doubles generates at least 100 values |
443 |
|
*/ |
446 |
|
SplittableRandom r = new SplittableRandom(); |
447 |
|
long size = 100; |
448 |
|
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
449 |
< |
assertEquals(counter.sum(), size); |
449 |
> |
assertEquals(size, counter.sum()); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
457 |
|
SplittableRandom r = new SplittableRandom(); |
458 |
|
long size = 100; |
459 |
|
r.ints().limit(size).forEach(x -> {counter.increment();}); |
460 |
< |
assertEquals(counter.sum(), size); |
460 |
> |
assertEquals(size, counter.sum()); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
468 |
|
SplittableRandom r = new SplittableRandom(); |
469 |
|
long size = 100; |
470 |
|
r.longs().limit(size).forEach(x -> {counter.increment();}); |
471 |
< |
assertEquals(counter.sum(), size); |
471 |
> |
assertEquals(size, counter.sum()); |
472 |
|
} |
473 |
|
|
482 |
– |
|
474 |
|
/** |
475 |
|
* A sequential unsized stream of doubles generates at least 100 values |
476 |
|
*/ |
479 |
|
SplittableRandom r = new SplittableRandom(); |
480 |
|
long size = 100; |
481 |
|
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
482 |
< |
assertEquals(counter.sum(), size); |
482 |
> |
assertEquals(size, counter.sum()); |
483 |
|
} |
484 |
|
|
494 |
– |
|
485 |
|
} |