--- jsr166/src/test/tck/SplittableRandomTest.java 2015/09/20 21:16:08 1.19 +++ jsr166/src/test/tck/SplittableRandomTest.java 2019/02/22 19:27:47 1.25 @@ -4,9 +4,14 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ +import java.util.Arrays; +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; @@ -133,12 +138,11 @@ public class SplittableRandomTest extend */ public void testNextIntBoundNonPositive() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextInt(-17), () -> sr.nextInt(0), - () -> sr.nextInt(Integer.MIN_VALUE), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextInt(Integer.MIN_VALUE)); } /** @@ -146,12 +150,11 @@ public class SplittableRandomTest extend */ public void testNextIntBadBounds() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextInt(17, 2), () -> sr.nextInt(-42, -42), - () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE)); } /** @@ -203,12 +206,11 @@ public class SplittableRandomTest extend */ public void testNextLongBoundNonPositive() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextLong(-17L), () -> sr.nextLong(0L), - () -> sr.nextLong(Long.MIN_VALUE), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextLong(Long.MIN_VALUE)); } /** @@ -216,12 +218,11 @@ public class SplittableRandomTest extend */ public void testNextLongBadBounds() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextLong(17L, 2L), () -> sr.nextLong(-42L, -42L), - () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE)); } /** @@ -272,14 +273,13 @@ public class SplittableRandomTest extend */ public void testNextDoubleBoundNonPositive() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextDouble(-17.0d), () -> sr.nextDouble(0.0d), () -> sr.nextDouble(-Double.MIN_VALUE), () -> sr.nextDouble(Double.NEGATIVE_INFINITY), - () -> sr.nextDouble(Double.NaN), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextDouble(Double.NaN)); } /** @@ -287,14 +287,13 @@ public class SplittableRandomTest extend */ public void testNextDoubleBadBounds() { SplittableRandom sr = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> sr.nextDouble(17.0d, 2.0d), () -> sr.nextDouble(-42.0d, -42.0d), () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), () -> sr.nextDouble(Double.NaN, 0.0d), - () -> sr.nextDouble(0.0d, Double.NaN), - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> sr.nextDouble(0.0d, Double.NaN)); } // TODO: Test infinite bounds! @@ -329,15 +328,14 @@ public class SplittableRandomTest extend */ public void testBadStreamSize() { SplittableRandom r = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> { java.util.stream.IntStream x = r.ints(-1L); }, () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); }, () -> { java.util.stream.LongStream x = r.longs(-1L); }, () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); }, () -> { java.util.stream.DoubleStream x = r.doubles(-1L); }, - () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }, - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); }); } /** @@ -346,15 +344,14 @@ public class SplittableRandomTest extend */ public void testBadStreamBounds() { SplittableRandom r = new SplittableRandom(); - Runnable[] throwingActions = { + assertThrows( + IllegalArgumentException.class, () -> { java.util.stream.IntStream x = r.ints(2, 1); }, () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); }, () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); }, () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); }, () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); }, - () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }, - }; - assertThrows(IllegalArgumentException.class, throwingActions); + () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); }); } /** @@ -525,4 +522,64 @@ 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(1, 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"); + } + } + + /** + * Filling an empty array with random bytes succeeds without effect. + */ + public void testNextBytes_emptyArray() { + new SplittableRandom().nextBytes(new byte[0]); + } + + public void testNextBytes_nullArray() { + try { + new SplittableRandom().nextBytes(null); + shouldThrow(); + } catch (NullPointerException success) {} + } + }