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.ArrayList; |
9 |
> |
import java.util.List; |
10 |
|
import java.util.SplittableRandom; |
11 |
|
import java.util.concurrent.atomic.AtomicInteger; |
10 |
– |
import java.util.concurrent.atomic.AtomicLong; |
12 |
|
import java.util.concurrent.atomic.LongAdder; |
13 |
+ |
import java.lang.reflect.Method; |
14 |
+ |
import java.util.function.Predicate; |
15 |
+ |
import java.util.stream.Collectors; |
16 |
+ |
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class SplittableRandomTest extends JSR166TestCase { |
21 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
|
return new TestSuite(SplittableRandomTest.class); |
46 |
|
static final int NCALLS = 10000; |
47 |
|
|
48 |
|
// max sampled int bound |
49 |
< |
static final int MAX_INT_BOUND = (1 << 28); |
49 |
> |
static final int MAX_INT_BOUND = (1 << 26); |
50 |
|
|
51 |
|
// max sampled long bound |
52 |
< |
static final long MAX_LONG_BOUND = (1L << 42); |
52 |
> |
static final long MAX_LONG_BOUND = (1L << 40); |
53 |
|
|
54 |
|
// Number of replications for other checks |
55 |
|
static final int REPS = |
96 |
|
* same values for nextLong. |
97 |
|
*/ |
98 |
|
public void testSeedConstructor() { |
99 |
< |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
99 |
> |
for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { |
100 |
|
SplittableRandom sr1 = new SplittableRandom(seed); |
101 |
|
SplittableRandom sr2 = new SplittableRandom(seed); |
102 |
|
for (int i = 0; i < REPS; ++i) |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* nextInt(negative) throws IllegalArgumentException |
138 |
> |
* nextInt(non-positive) throws IllegalArgumentException |
139 |
|
*/ |
140 |
< |
public void testNextIntBoundedNeg() { |
140 |
> |
public void testNextIntBoundNonPositive() { |
141 |
|
SplittableRandom sr = new SplittableRandom(); |
142 |
< |
try { |
143 |
< |
int f = sr.nextInt(-17); |
144 |
< |
shouldThrow(); |
145 |
< |
} catch (IllegalArgumentException success) {} |
142 |
> |
Runnable[] throwingActions = { |
143 |
> |
() -> sr.nextInt(-17), |
144 |
> |
() -> sr.nextInt(0), |
145 |
> |
() -> sr.nextInt(Integer.MIN_VALUE), |
146 |
> |
}; |
147 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
152 |
|
*/ |
153 |
|
public void testNextIntBadBounds() { |
154 |
|
SplittableRandom sr = new SplittableRandom(); |
155 |
< |
try { |
156 |
< |
int f = sr.nextInt(17, 2); |
157 |
< |
shouldThrow(); |
158 |
< |
} catch (IllegalArgumentException success) {} |
155 |
> |
Runnable[] throwingActions = { |
156 |
> |
() -> sr.nextInt(17, 2), |
157 |
> |
() -> sr.nextInt(-42, -42), |
158 |
> |
() -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE), |
159 |
> |
}; |
160 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
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); |
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 |
> |
Runnable[] throwingActions = { |
213 |
> |
() -> sr.nextLong(-17L), |
214 |
> |
() -> sr.nextLong(0L), |
215 |
> |
() -> sr.nextLong(Long.MIN_VALUE), |
216 |
> |
}; |
217 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
222 |
|
*/ |
223 |
|
public void testNextLongBadBounds() { |
224 |
|
SplittableRandom sr = new SplittableRandom(); |
225 |
< |
try { |
226 |
< |
long f = sr.nextLong(17, 2); |
227 |
< |
shouldThrow(); |
228 |
< |
} catch (IllegalArgumentException success) {} |
225 |
> |
Runnable[] throwingActions = { |
226 |
> |
() -> sr.nextLong(17L, 2L), |
227 |
> |
() -> sr.nextLong(-42L, -42L), |
228 |
> |
() -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE), |
229 |
> |
}; |
230 |
> |
assertThrows(IllegalArgumentException.class, throwingActions); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
236 |
|
*/ |
237 |
|
public void testNextLongBounded() { |
238 |
|
SplittableRandom sr = new SplittableRandom(); |
239 |
+ |
for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L)); |
240 |
|
for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { |
241 |
|
long f = sr.nextLong(bound); |
242 |
|
assertTrue(0 <= f && f < bound); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
277 |
+ |
* nextDouble(non-positive) throws IllegalArgumentException |
278 |
+ |
*/ |
279 |
+ |
public void testNextDoubleBoundNonPositive() { |
280 |
+ |
SplittableRandom sr = new SplittableRandom(); |
281 |
+ |
Runnable[] throwingActions = { |
282 |
+ |
() -> sr.nextDouble(-17.0d), |
283 |
+ |
() -> sr.nextDouble(0.0d), |
284 |
+ |
() -> sr.nextDouble(-Double.MIN_VALUE), |
285 |
+ |
() -> sr.nextDouble(Double.NEGATIVE_INFINITY), |
286 |
+ |
() -> sr.nextDouble(Double.NaN), |
287 |
+ |
}; |
288 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
289 |
+ |
} |
290 |
+ |
|
291 |
+ |
/** |
292 |
+ |
* nextDouble(! (least < bound)) throws IllegalArgumentException |
293 |
+ |
*/ |
294 |
+ |
public void testNextDoubleBadBounds() { |
295 |
+ |
SplittableRandom sr = new SplittableRandom(); |
296 |
+ |
Runnable[] throwingActions = { |
297 |
+ |
() -> sr.nextDouble(17.0d, 2.0d), |
298 |
+ |
() -> sr.nextDouble(-42.0d, -42.0d), |
299 |
+ |
() -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), |
300 |
+ |
() -> sr.nextDouble(Double.NaN, 0.0d), |
301 |
+ |
() -> sr.nextDouble(0.0d, Double.NaN), |
302 |
+ |
}; |
303 |
+ |
assertThrows(IllegalArgumentException.class, throwingActions); |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
// TODO: Test infinite bounds! |
307 |
+ |
//() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), |
308 |
+ |
//() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), |
309 |
+ |
|
310 |
+ |
/** |
311 |
|
* nextDouble(least, bound) returns least <= value < bound; |
312 |
|
* repeated calls produce at least two distinct results |
313 |
|
*/ |
418 |
|
for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { |
419 |
|
for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { |
420 |
|
final int lo = least, hi = bound; |
421 |
< |
r.ints(size, lo, hi).parallel(). |
422 |
< |
forEach(x -> {if (x < lo || x >= hi) |
423 |
< |
fails.getAndIncrement(); }); |
421 |
> |
r.ints(size, lo, hi).parallel().forEach( |
422 |
> |
x -> { |
423 |
> |
if (x < lo || x >= hi) |
424 |
> |
fails.getAndIncrement(); }); |
425 |
|
} |
426 |
|
} |
427 |
|
assertEquals(0, fails.get()); |
437 |
|
for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { |
438 |
|
for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { |
439 |
|
final long lo = least, hi = bound; |
440 |
< |
r.longs(size, lo, hi).parallel(). |
441 |
< |
forEach(x -> {if (x < lo || x >= hi) |
442 |
< |
fails.getAndIncrement(); }); |
440 |
> |
r.longs(size, lo, hi).parallel().forEach( |
441 |
> |
x -> { |
442 |
> |
if (x < lo || x >= hi) |
443 |
> |
fails.getAndIncrement(); }); |
444 |
|
} |
445 |
|
} |
446 |
|
assertEquals(0, fails.get()); |
456 |
|
for (double least = 0.00011; least < 1.0e20; least *= 9) { |
457 |
|
for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { |
458 |
|
final double lo = least, hi = bound; |
459 |
< |
r.doubles(size, lo, hi).parallel(). |
460 |
< |
forEach(x -> {if (x < lo || x >= hi) |
461 |
< |
fails.getAndIncrement(); }); |
459 |
> |
r.doubles(size, lo, hi).parallel().forEach( |
460 |
> |
x -> { |
461 |
> |
if (x < lo || x >= hi) |
462 |
> |
fails.getAndIncrement(); }); |
463 |
|
} |
464 |
|
} |
465 |
|
assertEquals(0, fails.get()); |
531 |
|
assertEquals(size, counter.sum()); |
532 |
|
} |
533 |
|
|
534 |
+ |
/** |
535 |
+ |
* SplittableRandom should implement most of Random's public methods |
536 |
+ |
*/ |
537 |
+ |
public void testShouldImplementMostRandomMethods() throws Throwable { |
538 |
+ |
Predicate<Method> wasForgotten = method -> { |
539 |
+ |
String name = method.getName(); |
540 |
+ |
// some methods deliberately not implemented |
541 |
+ |
if (name.equals("setSeed")) return false; |
542 |
+ |
if (name.equals("nextFloat")) return false; |
543 |
+ |
if (name.equals("nextGaussian")) return false; |
544 |
+ |
try { |
545 |
+ |
SplittableRandom.class.getMethod( |
546 |
+ |
method.getName(), method.getParameterTypes()); |
547 |
+ |
} catch (ReflectiveOperationException ex) { |
548 |
+ |
return true; |
549 |
+ |
} |
550 |
+ |
return false; |
551 |
+ |
}; |
552 |
+ |
List<Method> forgotten = |
553 |
+ |
Arrays.stream(java.util.Random.class.getMethods()) |
554 |
+ |
.filter(wasForgotten) |
555 |
+ |
.collect(Collectors.toList()); |
556 |
+ |
if (!forgotten.isEmpty()) |
557 |
+ |
throw new AssertionError("Please implement: " + forgotten); |
558 |
+ |
} |
559 |
+ |
|
560 |
+ |
/** |
561 |
+ |
* Repeated calls to nextBytes produce at least values of different signs for every byte |
562 |
+ |
*/ |
563 |
+ |
public void testNextBytes() { |
564 |
+ |
SplittableRandom sr = new SplittableRandom(); |
565 |
+ |
int n = sr.nextInt(20); |
566 |
+ |
byte[] bytes = new byte[n]; |
567 |
+ |
outer: |
568 |
+ |
for (int i = 0; i < n; i++) { |
569 |
+ |
for (int tries = NCALLS; tries-->0; ) { |
570 |
+ |
byte before = bytes[i]; |
571 |
+ |
sr.nextBytes(bytes); |
572 |
+ |
byte after = bytes[i]; |
573 |
+ |
if (after * before < 0) |
574 |
+ |
continue outer; |
575 |
+ |
} |
576 |
+ |
fail("not enough variation in random bytes"); |
577 |
+ |
} |
578 |
+ |
} |
579 |
+ |
|
580 |
|
} |