--- jsr166/src/test/tck/SplittableRandomTest.java 2016/11/17 22:09:52 1.21 +++ jsr166/src/test/tck/SplittableRandomTest.java 2017/10/03 22:27:04 1.22 @@ -4,9 +4,15 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ +import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; import java.util.SplittableRandom; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; +import java.lang.reflect.Method; +import java.util.function.Predicate; +import java.util.stream.Collectors; import junit.framework.Test; import junit.framework.TestSuite; @@ -525,4 +531,50 @@ public class SplittableRandomTest extend assertEquals(size, counter.sum()); } + /** + * SplittableRandom should implement most of Random's public methods + */ + public void testShouldImplementMostRandomMethods() throws Throwable { + Predicate wasForgotten = method -> { + String name = method.getName(); + // some methods deliberately not implemented + if (name.equals("setSeed")) return false; + if (name.equals("nextFloat")) return false; + if (name.equals("nextGaussian")) return false; + try { + SplittableRandom.class.getMethod( + method.getName(), method.getParameterTypes()); + } catch (ReflectiveOperationException ex) { + return true; + } + return false; + }; + List forgotten = + Arrays.stream(java.util.Random.class.getMethods()) + .filter(wasForgotten) + .collect(Collectors.toList()); + if (!forgotten.isEmpty()) + throw new AssertionError("Please implement: " + forgotten); + } + + /** + * Repeated calls to nextBytes produce at least values of different signs for every byte + */ + public void testNextBytes() { + SplittableRandom sr = new SplittableRandom(); + int n = sr.nextInt(20); + byte[] bytes = new byte[n]; + outer: + for (int i = 0; i < n; i++) { + for (int tries = NCALLS; tries-->0; ) { + byte before = bytes[i]; + sr.nextBytes(bytes); + byte after = bytes[i]; + if (after * before < 0) + continue outer; + } + fail("not enough variation in random bytes"); + } + } + }