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(); |
284 |
|
*/ |
285 |
|
public void testBadStreamSize() { |
286 |
|
SplittableRandom r = new SplittableRandom(); |
287 |
< |
try { |
288 |
< |
java.util.stream.IntStream x = r.ints(-1L); |
289 |
< |
shouldThrow(); |
290 |
< |
} catch (IllegalArgumentException ok) { |
291 |
< |
} |
292 |
< |
try { |
293 |
< |
java.util.stream.LongStream x = r.longs(-1L); |
294 |
< |
shouldThrow(); |
295 |
< |
} catch (IllegalArgumentException ok) { |
295 |
< |
} |
296 |
< |
try { |
297 |
< |
java.util.stream.DoubleStream x = r.doubles(-1L); |
298 |
< |
shouldThrow(); |
299 |
< |
} catch (IllegalArgumentException ok) { |
300 |
< |
} |
287 |
> |
Runnable[] throwingActions = { |
288 |
> |
() -> { java.util.stream.IntStream x = r.ints(-1L); }, |
289 |
> |
() -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); }, |
290 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L); }, |
291 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); }, |
292 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L); }, |
293 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }, |
294 |
> |
}; |
295 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
301 |
|
*/ |
302 |
|
public void testBadStreamBounds() { |
303 |
|
SplittableRandom r = new SplittableRandom(); |
304 |
< |
try { |
305 |
< |
java.util.stream.IntStream x = r.ints(2, 1); |
306 |
< |
shouldThrow(); |
307 |
< |
} catch (IllegalArgumentException ok) { |
308 |
< |
} |
309 |
< |
try { |
310 |
< |
java.util.stream.LongStream x = r.longs(1, -2); |
311 |
< |
shouldThrow(); |
312 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
319 |
< |
try { |
320 |
< |
java.util.stream.DoubleStream x = r.doubles(0, 0); |
321 |
< |
shouldThrow(); |
322 |
< |
} catch (IllegalArgumentException ok) { |
323 |
< |
} |
304 |
> |
Runnable[] throwingActions = { |
305 |
> |
() -> { java.util.stream.IntStream x = r.ints(2, 1); }, |
306 |
> |
() -> { java.util.stream.IntStream x = r.ints(10, 42, 42); }, |
307 |
> |
() -> { java.util.stream.LongStream x = r.longs(-1L, -1L); }, |
308 |
> |
() -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); }, |
309 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); }, |
310 |
> |
() -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }, |
311 |
> |
}; |
312 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
313 |
|
} |
314 |
|
|
315 |
|
/** |
321 |
|
long size = 0; |
322 |
|
for (int reps = 0; reps < REPS; ++reps) { |
323 |
|
counter.reset(); |
324 |
< |
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
325 |
< |
assertEquals(counter.sum(), size); |
324 |
> |
r.ints(size).parallel().forEach(x -> counter.increment()); |
325 |
> |
assertEquals(size, counter.sum()); |
326 |
|
size += 524959; |
327 |
|
} |
328 |
|
} |
336 |
|
long size = 0; |
337 |
|
for (int reps = 0; reps < REPS; ++reps) { |
338 |
|
counter.reset(); |
339 |
< |
r.longs(size).parallel().forEach(x -> {counter.increment();}); |
340 |
< |
assertEquals(counter.sum(), size); |
339 |
> |
r.longs(size).parallel().forEach(x -> counter.increment()); |
340 |
> |
assertEquals(size, counter.sum()); |
341 |
|
size += 524959; |
342 |
|
} |
343 |
|
} |
351 |
|
long size = 0; |
352 |
|
for (int reps = 0; reps < REPS; ++reps) { |
353 |
|
counter.reset(); |
354 |
< |
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
355 |
< |
assertEquals(counter.sum(), size); |
354 |
> |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
355 |
> |
assertEquals(size, counter.sum()); |
356 |
|
size += 524959; |
357 |
|
} |
358 |
|
} |
359 |
|
|
371 |
– |
|
360 |
|
/** |
361 |
|
* Each of a parallel sized stream of bounded ints is within bounds |
362 |
|
*/ |
372 |
|
fails.getAndIncrement(); }); |
373 |
|
} |
374 |
|
} |
375 |
< |
assertEquals(fails.get(), 0); |
375 |
> |
assertEquals(0, fails.get()); |
376 |
|
} |
377 |
|
|
378 |
|
/** |
390 |
|
fails.getAndIncrement(); }); |
391 |
|
} |
392 |
|
} |
393 |
< |
assertEquals(fails.get(), 0); |
393 |
> |
assertEquals(0, fails.get()); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
408 |
|
fails.getAndIncrement(); }); |
409 |
|
} |
410 |
|
} |
411 |
< |
assertEquals(fails.get(), 0); |
411 |
> |
assertEquals(0, fails.get()); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
418 |
|
LongAdder counter = new LongAdder(); |
419 |
|
SplittableRandom r = new SplittableRandom(); |
420 |
|
long size = 100; |
421 |
< |
r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); |
422 |
< |
assertEquals(counter.sum(), size); |
421 |
> |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
422 |
> |
assertEquals(size, counter.sum()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
429 |
|
LongAdder counter = new LongAdder(); |
430 |
|
SplittableRandom r = new SplittableRandom(); |
431 |
|
long size = 100; |
432 |
< |
r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); |
433 |
< |
assertEquals(counter.sum(), size); |
432 |
> |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
433 |
> |
assertEquals(size, counter.sum()); |
434 |
|
} |
435 |
|
|
448 |
– |
|
436 |
|
/** |
437 |
|
* A parallel unsized stream of doubles generates at least 100 values |
438 |
|
*/ |
440 |
|
LongAdder counter = new LongAdder(); |
441 |
|
SplittableRandom r = new SplittableRandom(); |
442 |
|
long size = 100; |
443 |
< |
r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); |
444 |
< |
assertEquals(counter.sum(), size); |
443 |
> |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
444 |
> |
assertEquals(size, counter.sum()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
451 |
|
LongAdder counter = new LongAdder(); |
452 |
|
SplittableRandom r = new SplittableRandom(); |
453 |
|
long size = 100; |
454 |
< |
r.ints().limit(size).forEach(x -> {counter.increment();}); |
455 |
< |
assertEquals(counter.sum(), size); |
454 |
> |
r.ints().limit(size).forEach(x -> counter.increment()); |
455 |
> |
assertEquals(size, counter.sum()); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
462 |
|
LongAdder counter = new LongAdder(); |
463 |
|
SplittableRandom r = new SplittableRandom(); |
464 |
|
long size = 100; |
465 |
< |
r.longs().limit(size).forEach(x -> {counter.increment();}); |
466 |
< |
assertEquals(counter.sum(), size); |
465 |
> |
r.longs().limit(size).forEach(x -> counter.increment()); |
466 |
> |
assertEquals(size, counter.sum()); |
467 |
|
} |
468 |
|
|
482 |
– |
|
469 |
|
/** |
470 |
|
* A sequential unsized stream of doubles generates at least 100 values |
471 |
|
*/ |
473 |
|
LongAdder counter = new LongAdder(); |
474 |
|
SplittableRandom r = new SplittableRandom(); |
475 |
|
long size = 100; |
476 |
< |
r.doubles().limit(size).forEach(x -> {counter.increment();}); |
477 |
< |
assertEquals(counter.sum(), size); |
476 |
> |
r.doubles().limit(size).forEach(x -> counter.increment()); |
477 |
> |
assertEquals(size, counter.sum()); |
478 |
|
} |
479 |
|
|
494 |
– |
|
480 |
|
} |