4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
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; |
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; |
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); |
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); |
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); |
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 |
|
} |