3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
< |
import junit.framework.*; |
7 |
< |
import java.util.*; |
6 |
> |
|
7 |
> |
import java.util.Arrays; |
8 |
> |
import java.util.List; |
9 |
|
import java.util.SplittableRandom; |
10 |
|
import java.util.concurrent.atomic.AtomicInteger; |
10 |
– |
import java.util.concurrent.atomic.AtomicLong; |
11 |
|
import java.util.concurrent.atomic.LongAdder; |
12 |
+ |
import java.lang.reflect.Method; |
13 |
+ |
import java.util.function.Predicate; |
14 |
+ |
import java.util.stream.Collectors; |
15 |
+ |
import java.util.stream.DoubleStream; |
16 |
+ |
import java.util.stream.IntStream; |
17 |
+ |
import java.util.stream.LongStream; |
18 |
+ |
|
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
|
|
22 |
|
public class SplittableRandomTest extends JSR166TestCase { |
23 |
|
|
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
public static Test suite() { |
28 |
|
return new TestSuite(SplittableRandomTest.class); |
31 |
|
/* |
32 |
|
* Testing coverage notes: |
33 |
|
* |
34 |
< |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest |
34 |
> |
* 1. Many of the test methods are adapted from ThreadLocalRandomTest. |
35 |
|
* |
36 |
< |
* 2. This set of tests do not check for random number generator |
37 |
< |
* quality. But we check for minimal API compliance by requiring |
38 |
< |
* that repeated calls to nextX methods, up to NCALLS tries, |
39 |
< |
* produce at least one different result. (In some possible |
40 |
< |
* universe, a "correct" implementation might fail, but the odds |
41 |
< |
* are vastly less than that of encountering a hardware failure |
42 |
< |
* while running the test.) For bounded nextX methods, we sample |
43 |
< |
* various intervals across multiples of primes. In other tests, |
44 |
< |
* we repeat under REPS different values. |
36 |
> |
* 2. These tests do not check for random number generator quality. |
37 |
> |
* But we check for minimal API compliance by requiring that |
38 |
> |
* repeated calls to nextX methods, up to NCALLS tries, produce at |
39 |
> |
* least two distinct results. (In some possible universe, a |
40 |
> |
* "correct" implementation might fail, but the odds are vastly |
41 |
> |
* less than that of encountering a hardware failure while running |
42 |
> |
* the test.) For bounded nextX methods, we sample various |
43 |
> |
* intervals across multiples of primes. In other tests, we repeat |
44 |
> |
* under REPS different values. |
45 |
|
*/ |
46 |
|
|
47 |
|
// max numbers of calls to detect getting stuck on one value |
48 |
|
static final int NCALLS = 10000; |
49 |
|
|
50 |
|
// max sampled int bound |
51 |
< |
static final int MAX_INT_BOUND = (1 << 28); |
51 |
> |
static final int MAX_INT_BOUND = (1 << 26); |
52 |
|
|
53 |
< |
// Max sampled long bound |
54 |
< |
static final long MAX_LONG_BOUND = (1L << 42); |
53 |
> |
// max sampled long bound |
54 |
> |
static final long MAX_LONG_BOUND = (1L << 40); |
55 |
|
|
56 |
|
// Number of replications for other checks |
57 |
< |
static final int REPS = 20; |
57 |
> |
static final int REPS = |
58 |
> |
Integer.getInteger("SplittableRandomTest.reps", 4); |
59 |
|
|
60 |
|
/** |
61 |
< |
* Repeated calls to nextInt produce at least one different result |
61 |
> |
* Repeated calls to nextInt produce at least two distinct results |
62 |
|
*/ |
63 |
|
public void testNextInt() { |
64 |
|
SplittableRandom sr = new SplittableRandom(); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Repeated calls to nextLong produce at least one different result |
73 |
> |
* Repeated calls to nextLong produce at least two distinct results |
74 |
|
*/ |
75 |
|
public void testNextLong() { |
76 |
|
SplittableRandom sr = new SplittableRandom(); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Repeated calls to nextDouble produce at least one different result |
85 |
> |
* Repeated calls to nextDouble produce at least two distinct results |
86 |
|
*/ |
87 |
|
public void testNextDouble() { |
88 |
|
SplittableRandom sr = new SplittableRandom(); |
89 |
|
double f = sr.nextDouble(); |
90 |
< |
double i = 0; |
90 |
> |
int i = 0; |
91 |
|
while (i < NCALLS && sr.nextDouble() == f) |
92 |
|
++i; |
93 |
|
assertTrue(i < NCALLS); |
98 |
|
* same values for nextLong. |
99 |
|
*/ |
100 |
|
public void testSeedConstructor() { |
101 |
< |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
101 |
> |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
102 |
|
SplittableRandom sr1 = new SplittableRandom(seed); |
103 |
|
SplittableRandom sr2 = new SplittableRandom(seed); |
104 |
< |
for (int i = 0; i < REPS; ++i) |
104 |
> |
for (int i = 0; i < REPS; ++i) |
105 |
|
assertEquals(sr1.nextLong(), sr2.nextLong()); |
106 |
|
} |
107 |
|
} |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* nextInt(negative) throws IllegalArgumentException; |
140 |
> |
* nextInt(non-positive) throws IllegalArgumentException |
141 |
|
*/ |
142 |
< |
public void testNextIntBoundedNeg() { |
142 |
> |
public void testNextIntBoundNonPositive() { |
143 |
|
SplittableRandom sr = new SplittableRandom(); |
144 |
< |
try { |
145 |
< |
int f = sr.nextInt(-17); |
146 |
< |
shouldThrow(); |
147 |
< |
} catch (IllegalArgumentException success) {} |
144 |
> |
assertThrows( |
145 |
> |
IllegalArgumentException.class, |
146 |
> |
() -> sr.nextInt(-17), |
147 |
> |
() -> sr.nextInt(0), |
148 |
> |
() -> sr.nextInt(Integer.MIN_VALUE)); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
< |
* nextInt(least >= bound) throws IllegalArgumentException; |
152 |
> |
* nextInt(least >= bound) throws IllegalArgumentException |
153 |
|
*/ |
154 |
|
public void testNextIntBadBounds() { |
155 |
|
SplittableRandom sr = new SplittableRandom(); |
156 |
< |
try { |
157 |
< |
int f = sr.nextInt(17, 2); |
158 |
< |
shouldThrow(); |
159 |
< |
} catch (IllegalArgumentException success) {} |
156 |
> |
assertThrows( |
157 |
> |
IllegalArgumentException.class, |
158 |
> |
() -> sr.nextInt(17, 2), |
159 |
> |
() -> sr.nextInt(-42, -42), |
160 |
> |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE)); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* nextInt(bound) returns 0 <= value < bound; |
165 |
< |
* repeated calls produce at least one different result |
165 |
> |
* repeated calls produce at least two distinct results |
166 |
|
*/ |
167 |
|
public void testNextIntBounded() { |
168 |
|
SplittableRandom sr = new SplittableRandom(); |
169 |
+ |
for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1)); |
170 |
|
// sample bound space across prime number increments |
171 |
|
for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { |
172 |
|
int f = sr.nextInt(bound); |
184 |
|
|
185 |
|
/** |
186 |
|
* nextInt(least, bound) returns least <= value < bound; |
187 |
< |
* repeated calls produce at least one different result |
187 |
> |
* repeated calls produce at least two distinct results |
188 |
|
*/ |
189 |
|
public void testNextIntBounded2() { |
190 |
|
SplittableRandom sr = new SplittableRandom(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
< |
* nextLong(negative) throws IllegalArgumentException; |
208 |
> |
* nextLong(non-positive) throws IllegalArgumentException |
209 |
|
*/ |
210 |
< |
public void testNextLongBoundedNeg() { |
210 |
> |
public void testNextLongBoundNonPositive() { |
211 |
|
SplittableRandom sr = new SplittableRandom(); |
212 |
< |
try { |
213 |
< |
long f = sr.nextLong(-17); |
214 |
< |
shouldThrow(); |
215 |
< |
} catch (IllegalArgumentException success) {} |
212 |
> |
assertThrows( |
213 |
> |
IllegalArgumentException.class, |
214 |
> |
() -> sr.nextLong(-17L), |
215 |
> |
() -> sr.nextLong(0L), |
216 |
> |
() -> sr.nextLong(Long.MIN_VALUE)); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* nextLong(least >= bound) throws IllegalArgumentException; |
220 |
> |
* nextLong(least >= bound) throws IllegalArgumentException |
221 |
|
*/ |
222 |
|
public void testNextLongBadBounds() { |
223 |
|
SplittableRandom sr = new SplittableRandom(); |
224 |
< |
try { |
225 |
< |
long f = sr.nextLong(17, 2); |
226 |
< |
shouldThrow(); |
227 |
< |
} catch (IllegalArgumentException success) {} |
224 |
> |
assertThrows( |
225 |
> |
IllegalArgumentException.class, |
226 |
> |
() -> sr.nextLong(17L, 2L), |
227 |
> |
() -> sr.nextLong(-42L, -42L), |
228 |
> |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE)); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
|
* nextLong(bound) returns 0 <= value < bound; |
233 |
< |
* repeated calls produce at least one different result |
233 |
> |
* repeated calls produce at least two distinct results |
234 |
|
*/ |
235 |
|
public void testNextLongBounded() { |
236 |
|
SplittableRandom sr = new SplittableRandom(); |
237 |
+ |
for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L)); |
238 |
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
239 |
|
long f = sr.nextLong(bound); |
240 |
|
assertTrue(0 <= f && f < bound); |
251 |
|
|
252 |
|
/** |
253 |
|
* nextLong(least, bound) returns least <= value < bound; |
254 |
< |
* repeated calls produce at least one different result |
254 |
> |
* repeated calls produce at least two distinct results |
255 |
|
*/ |
256 |
|
public void testNextLongBounded2() { |
257 |
|
SplittableRandom sr = new SplittableRandom(); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
276 |
+ |
*/ |
277 |
+ |
public void testNextDoubleBoundNonPositive() { |
278 |
+ |
SplittableRandom sr = new SplittableRandom(); |
279 |
+ |
assertThrows( |
280 |
+ |
IllegalArgumentException.class, |
281 |
+ |
() -> sr.nextDouble(-17.0d), |
282 |
+ |
() -> sr.nextDouble(0.0d), |
283 |
+ |
() -> sr.nextDouble(-Double.MIN_VALUE), |
284 |
+ |
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
285 |
+ |
() -> sr.nextDouble(Double.NaN)); |
286 |
+ |
} |
287 |
+ |
|
288 |
+ |
/** |
289 |
+ |
* nextDouble(! (least < bound)) throws IllegalArgumentException |
290 |
+ |
*/ |
291 |
+ |
public void testNextDoubleBadBounds() { |
292 |
+ |
SplittableRandom sr = new SplittableRandom(); |
293 |
+ |
assertThrows( |
294 |
+ |
IllegalArgumentException.class, |
295 |
+ |
() -> sr.nextDouble(17.0d, 2.0d), |
296 |
+ |
() -> sr.nextDouble(-42.0d, -42.0d), |
297 |
+ |
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
298 |
+ |
() -> sr.nextDouble(Double.NaN, 0.0d), |
299 |
+ |
() -> sr.nextDouble(0.0d, Double.NaN)); |
300 |
+ |
} |
301 |
+ |
|
302 |
+ |
// TODO: Test infinite bounds! |
303 |
+ |
//() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), |
304 |
+ |
//() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), |
305 |
+ |
|
306 |
+ |
/** |
307 |
|
* nextDouble(least, bound) returns least <= value < bound; |
308 |
< |
* repeated calls produce at least one different result |
308 |
> |
* repeated calls produce at least two distinct results |
309 |
|
*/ |
310 |
|
public void testNextDoubleBounded2() { |
311 |
|
SplittableRandom sr = new SplittableRandom(); |
331 |
|
*/ |
332 |
|
public void testBadStreamSize() { |
333 |
|
SplittableRandom r = new SplittableRandom(); |
334 |
< |
try { |
335 |
< |
java.util.stream.IntStream x = r.ints(-1L); |
336 |
< |
shouldThrow(); |
337 |
< |
} catch (IllegalArgumentException ok) { |
338 |
< |
} |
339 |
< |
try { |
340 |
< |
java.util.stream.LongStream x = r.longs(-1L); |
341 |
< |
shouldThrow(); |
294 |
< |
} catch (IllegalArgumentException ok) { |
295 |
< |
} |
296 |
< |
try { |
297 |
< |
java.util.stream.DoubleStream x = r.doubles(-1L); |
298 |
< |
shouldThrow(); |
299 |
< |
} catch (IllegalArgumentException ok) { |
300 |
< |
} |
334 |
> |
assertThrows( |
335 |
> |
IllegalArgumentException.class, |
336 |
> |
() -> { IntStream unused = r.ints(-1L); }, |
337 |
> |
() -> { IntStream unused = r.ints(-1L, 2, 3); }, |
338 |
> |
() -> { LongStream unused = r.longs(-1L); }, |
339 |
> |
() -> { LongStream unused = r.longs(-1L, -1L, 1L); }, |
340 |
> |
() -> { DoubleStream unused = r.doubles(-1L); }, |
341 |
> |
() -> { DoubleStream unused = r.doubles(-1L, .5, .6); }); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
347 |
|
*/ |
348 |
|
public void testBadStreamBounds() { |
349 |
|
SplittableRandom r = new SplittableRandom(); |
350 |
< |
try { |
351 |
< |
java.util.stream.IntStream x = r.ints(2, 1); |
352 |
< |
shouldThrow(); |
353 |
< |
} catch (IllegalArgumentException ok) { |
354 |
< |
} |
355 |
< |
try { |
356 |
< |
java.util.stream.LongStream x = r.longs(1, -2); |
357 |
< |
shouldThrow(); |
317 |
< |
} catch (IllegalArgumentException ok) { |
318 |
< |
} |
319 |
< |
try { |
320 |
< |
java.util.stream.DoubleStream x = r.doubles(0, 0); |
321 |
< |
shouldThrow(); |
322 |
< |
} catch (IllegalArgumentException ok) { |
323 |
< |
} |
350 |
> |
assertThrows( |
351 |
> |
IllegalArgumentException.class, |
352 |
> |
() -> { IntStream unused = r.ints(2, 1); }, |
353 |
> |
() -> { IntStream unused = r.ints(10, 42, 42); }, |
354 |
> |
() -> { LongStream unused = r.longs(-1L, -1L); }, |
355 |
> |
() -> { LongStream unused = r.longs(10, 1L, -2L); }, |
356 |
> |
() -> { DoubleStream unused = r.doubles(0.0, 0.0); }, |
357 |
> |
() -> { DoubleStream unused = r.doubles(10, .5, .4); }); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
366 |
|
long size = 0; |
367 |
|
for (int reps = 0; reps < REPS; ++reps) { |
368 |
|
counter.reset(); |
369 |
< |
r.ints(size).parallel().forEach(x -> {counter.increment();}); |
370 |
< |
assertEquals(counter.sum(), size); |
369 |
> |
r.ints(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.longs(size).parallel().forEach(x -> {counter.increment();}); |
385 |
< |
assertEquals(counter.sum(), size); |
384 |
> |
r.longs(size).parallel().forEach(x -> counter.increment()); |
385 |
> |
assertEquals(size, counter.sum()); |
386 |
|
size += 524959; |
387 |
|
} |
388 |
|
} |
396 |
|
long size = 0; |
397 |
|
for (int reps = 0; reps < REPS; ++reps) { |
398 |
|
counter.reset(); |
399 |
< |
r.doubles(size).parallel().forEach(x -> {counter.increment();}); |
400 |
< |
assertEquals(counter.sum(), size); |
399 |
> |
r.doubles(size).parallel().forEach(x -> counter.increment()); |
400 |
> |
assertEquals(size, counter.sum()); |
401 |
|
size += 524959; |
402 |
|
} |
403 |
|
} |
412 |
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
413 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
414 |
|
final int lo = least, hi = bound; |
415 |
< |
r.ints(size, lo, hi).parallel(). |
416 |
< |
forEach(x -> {if (x < lo || x >= hi) |
417 |
< |
fails.getAndIncrement(); }); |
415 |
> |
r.ints(size, lo, hi).parallel().forEach( |
416 |
> |
x -> { |
417 |
> |
if (x < lo || x >= hi) |
418 |
> |
fails.getAndIncrement(); }); |
419 |
|
} |
420 |
|
} |
421 |
< |
assertEquals(fails.get(), 0); |
421 |
> |
assertEquals(0, fails.get()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
431 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
432 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
433 |
|
final long lo = least, hi = bound; |
434 |
< |
r.longs(size, lo, hi).parallel(). |
435 |
< |
forEach(x -> {if (x < lo || x >= hi) |
436 |
< |
fails.getAndIncrement(); }); |
434 |
> |
r.longs(size, lo, hi).parallel().forEach( |
435 |
> |
x -> { |
436 |
> |
if (x < lo || x >= hi) |
437 |
> |
fails.getAndIncrement(); }); |
438 |
|
} |
439 |
|
} |
440 |
< |
assertEquals(fails.get(), 0); |
440 |
> |
assertEquals(0, fails.get()); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
450 |
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
451 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
452 |
|
final double lo = least, hi = bound; |
453 |
< |
r.doubles(size, lo, hi).parallel(). |
454 |
< |
forEach(x -> {if (x < lo || x >= hi) |
455 |
< |
fails.getAndIncrement(); }); |
453 |
> |
r.doubles(size, lo, hi).parallel().forEach( |
454 |
> |
x -> { |
455 |
> |
if (x < lo || x >= hi) |
456 |
> |
fails.getAndIncrement(); }); |
457 |
|
} |
458 |
|
} |
459 |
< |
assertEquals(fails.get(), 0); |
459 |
> |
assertEquals(0, fails.get()); |
460 |
> |
} |
461 |
> |
|
462 |
> |
/** |
463 |
> |
* A parallel unsized stream of ints generates at least 100 values |
464 |
> |
*/ |
465 |
> |
public void testUnsizedIntsCount() { |
466 |
> |
LongAdder counter = new LongAdder(); |
467 |
> |
SplittableRandom r = new SplittableRandom(); |
468 |
> |
long size = 100; |
469 |
> |
r.ints().limit(size).parallel().forEach(x -> counter.increment()); |
470 |
> |
assertEquals(size, counter.sum()); |
471 |
> |
} |
472 |
> |
|
473 |
> |
/** |
474 |
> |
* A parallel unsized stream of longs generates at least 100 values |
475 |
> |
*/ |
476 |
> |
public void testUnsizedLongsCount() { |
477 |
> |
LongAdder counter = new LongAdder(); |
478 |
> |
SplittableRandom r = new SplittableRandom(); |
479 |
> |
long size = 100; |
480 |
> |
r.longs().limit(size).parallel().forEach(x -> counter.increment()); |
481 |
> |
assertEquals(size, counter.sum()); |
482 |
> |
} |
483 |
> |
|
484 |
> |
/** |
485 |
> |
* A parallel unsized stream of doubles generates at least 100 values |
486 |
> |
*/ |
487 |
> |
public void testUnsizedDoublesCount() { |
488 |
> |
LongAdder counter = new LongAdder(); |
489 |
> |
SplittableRandom r = new SplittableRandom(); |
490 |
> |
long size = 100; |
491 |
> |
r.doubles().limit(size).parallel().forEach(x -> counter.increment()); |
492 |
> |
assertEquals(size, counter.sum()); |
493 |
> |
} |
494 |
> |
|
495 |
> |
/** |
496 |
> |
* A sequential unsized stream of ints generates at least 100 values |
497 |
> |
*/ |
498 |
> |
public void testUnsizedIntsCountSeq() { |
499 |
> |
LongAdder counter = new LongAdder(); |
500 |
> |
SplittableRandom r = new SplittableRandom(); |
501 |
> |
long size = 100; |
502 |
> |
r.ints().limit(size).forEach(x -> counter.increment()); |
503 |
> |
assertEquals(size, counter.sum()); |
504 |
> |
} |
505 |
> |
|
506 |
> |
/** |
507 |
> |
* A sequential unsized stream of longs generates at least 100 values |
508 |
> |
*/ |
509 |
> |
public void testUnsizedLongsCountSeq() { |
510 |
> |
LongAdder counter = new LongAdder(); |
511 |
> |
SplittableRandom r = new SplittableRandom(); |
512 |
> |
long size = 100; |
513 |
> |
r.longs().limit(size).forEach(x -> counter.increment()); |
514 |
> |
assertEquals(size, counter.sum()); |
515 |
> |
} |
516 |
> |
|
517 |
> |
/** |
518 |
> |
* A sequential unsized stream of doubles generates at least 100 values |
519 |
> |
*/ |
520 |
> |
public void testUnsizedDoublesCountSeq() { |
521 |
> |
LongAdder counter = new LongAdder(); |
522 |
> |
SplittableRandom r = new SplittableRandom(); |
523 |
> |
long size = 100; |
524 |
> |
r.doubles().limit(size).forEach(x -> counter.increment()); |
525 |
> |
assertEquals(size, counter.sum()); |
526 |
> |
} |
527 |
> |
|
528 |
> |
/** |
529 |
> |
* SplittableRandom should implement most of Random's public methods |
530 |
> |
*/ |
531 |
> |
public void testShouldImplementMostRandomMethods() throws Throwable { |
532 |
> |
Predicate<Method> wasForgotten = method -> { |
533 |
> |
String name = method.getName(); |
534 |
> |
// some methods deliberately not implemented |
535 |
> |
if (name.equals("setSeed")) return false; |
536 |
> |
if (name.equals("nextFloat")) return false; |
537 |
> |
if (name.equals("nextGaussian")) return false; |
538 |
> |
try { |
539 |
> |
SplittableRandom.class.getMethod( |
540 |
> |
method.getName(), method.getParameterTypes()); |
541 |
> |
} catch (ReflectiveOperationException ex) { |
542 |
> |
return true; |
543 |
> |
} |
544 |
> |
return false; |
545 |
> |
}; |
546 |
> |
List<Method> forgotten = |
547 |
> |
Arrays.stream(java.util.Random.class.getMethods()) |
548 |
> |
.filter(wasForgotten) |
549 |
> |
.collect(Collectors.toList()); |
550 |
> |
if (!forgotten.isEmpty()) |
551 |
> |
throw new AssertionError("Please implement: " + forgotten); |
552 |
> |
} |
553 |
> |
|
554 |
> |
/** |
555 |
> |
* Repeated calls to nextBytes produce at least values of different signs for every byte |
556 |
> |
*/ |
557 |
> |
public void testNextBytes() { |
558 |
> |
SplittableRandom sr = new SplittableRandom(); |
559 |
> |
int n = sr.nextInt(1, 20); |
560 |
> |
byte[] bytes = new byte[n]; |
561 |
> |
outer: |
562 |
> |
for (int i = 0; i < n; i++) { |
563 |
> |
for (int tries = NCALLS; tries-->0; ) { |
564 |
> |
byte before = bytes[i]; |
565 |
> |
sr.nextBytes(bytes); |
566 |
> |
byte after = bytes[i]; |
567 |
> |
if (after * before < 0) |
568 |
> |
continue outer; |
569 |
> |
} |
570 |
> |
fail("not enough variation in random bytes"); |
571 |
> |
} |
572 |
> |
} |
573 |
> |
|
574 |
> |
/** |
575 |
> |
* Filling an empty array with random bytes succeeds without effect. |
576 |
> |
*/ |
577 |
> |
public void testNextBytes_emptyArray() { |
578 |
> |
new SplittableRandom().nextBytes(new byte[0]); |
579 |
> |
} |
580 |
> |
|
581 |
> |
public void testNextBytes_nullArray() { |
582 |
> |
try { |
583 |
> |
new SplittableRandom().nextBytes(null); |
584 |
> |
shouldThrow(); |
585 |
> |
} catch (NullPointerException success) {} |
586 |
|
} |
587 |
|
|
588 |
|
} |