--- jsr166/src/test/tck/SplittableRandomTest.java 2013/07/12 01:14:30 1.3 +++ jsr166/src/test/tck/SplittableRandomTest.java 2019/02/22 19:27:47 1.25 @@ -3,17 +3,23 @@ * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; + +import java.util.Arrays; +import java.util.List; import java.util.SplittableRandom; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; 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; public class SplittableRandomTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(SplittableRandomTest.class); @@ -22,33 +28,34 @@ public class SplittableRandomTest extend /* * Testing coverage notes: * - * 1. Many of the test methods are adapted from ThreadLocalRandomTest + * 1. Many of the test methods are adapted from ThreadLocalRandomTest. * - * 2. This set of tests do not check for random number generator - * quality. But we check for minimal API compliance by requiring - * that repeated calls to nextX methods, up to NCALLS tries, - * produce at least one different result. (In some possible - * universe, a "correct" implementation might fail, but the odds - * are vastly less than that of encountering a hardware failure - * while running the test.) For bounded nextX methods, we sample - * various intervals across multiples of primes. In other tests, - * we repeat under REPS different values. + * 2. These tests do not check for random number generator quality. + * But we check for minimal API compliance by requiring that + * repeated calls to nextX methods, up to NCALLS tries, produce at + * least two distinct results. (In some possible universe, a + * "correct" implementation might fail, but the odds are vastly + * less than that of encountering a hardware failure while running + * the test.) For bounded nextX methods, we sample various + * intervals across multiples of primes. In other tests, we repeat + * under REPS different values. */ // max numbers of calls to detect getting stuck on one value static final int NCALLS = 10000; // max sampled int bound - static final int MAX_INT_BOUND = (1 << 28); + static final int MAX_INT_BOUND = (1 << 26); - // Max sampled long bound - static final long MAX_LONG_BOUND = (1L << 42); + // max sampled long bound + static final long MAX_LONG_BOUND = (1L << 40); // Number of replications for other checks - static final int REPS = 20; + static final int REPS = + Integer.getInteger("SplittableRandomTest.reps", 4); /** - * Repeated calls to nextInt produce at least one different result + * Repeated calls to nextInt produce at least two distinct results */ public void testNextInt() { SplittableRandom sr = new SplittableRandom(); @@ -60,7 +67,7 @@ public class SplittableRandomTest extend } /** - * Repeated calls to nextLong produce at least one different result + * Repeated calls to nextLong produce at least two distinct results */ public void testNextLong() { SplittableRandom sr = new SplittableRandom(); @@ -72,12 +79,12 @@ public class SplittableRandomTest extend } /** - * Repeated calls to nextDouble produce at least one different result + * Repeated calls to nextDouble produce at least two distinct results */ public void testNextDouble() { SplittableRandom sr = new SplittableRandom(); double f = sr.nextDouble(); - double i = 0; + int i = 0; while (i < NCALLS && sr.nextDouble() == f) ++i; assertTrue(i < NCALLS); @@ -88,7 +95,7 @@ public class SplittableRandomTest extend * same values for nextLong. */ public void testSeedConstructor() { - for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { + for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) { SplittableRandom sr1 = new SplittableRandom(seed); SplittableRandom sr2 = new SplittableRandom(seed); for (int i = 0; i < REPS; ++i) @@ -127,33 +134,36 @@ public class SplittableRandomTest extend } /** - * nextInt(negative) throws IllegalArgumentException; + * nextInt(non-positive) throws IllegalArgumentException */ - public void testNextIntBoundedNeg() { + public void testNextIntBoundNonPositive() { SplittableRandom sr = new SplittableRandom(); - try { - int f = sr.nextInt(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> sr.nextInt(-17), + () -> sr.nextInt(0), + () -> sr.nextInt(Integer.MIN_VALUE)); } /** - * nextInt(least >= bound) throws IllegalArgumentException; + * nextInt(least >= bound) throws IllegalArgumentException */ public void testNextIntBadBounds() { SplittableRandom sr = new SplittableRandom(); - try { - int f = sr.nextInt(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> sr.nextInt(17, 2), + () -> sr.nextInt(-42, -42), + () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE)); } /** * nextInt(bound) returns 0 <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextIntBounded() { SplittableRandom sr = new SplittableRandom(); + for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1)); // sample bound space across prime number increments for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) { int f = sr.nextInt(bound); @@ -171,7 +181,7 @@ public class SplittableRandomTest extend /** * nextInt(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextIntBounded2() { SplittableRandom sr = new SplittableRandom(); @@ -192,33 +202,36 @@ public class SplittableRandomTest extend } /** - * nextLong(negative) throws IllegalArgumentException; + * nextLong(non-positive) throws IllegalArgumentException */ - public void testNextLongBoundedNeg() { + public void testNextLongBoundNonPositive() { SplittableRandom sr = new SplittableRandom(); - try { - long f = sr.nextLong(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> sr.nextLong(-17L), + () -> sr.nextLong(0L), + () -> sr.nextLong(Long.MIN_VALUE)); } /** - * nextLong(least >= bound) throws IllegalArgumentException; + * nextLong(least >= bound) throws IllegalArgumentException */ public void testNextLongBadBounds() { SplittableRandom sr = new SplittableRandom(); - try { - long f = sr.nextLong(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> sr.nextLong(17L, 2L), + () -> sr.nextLong(-42L, -42L), + () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE)); } /** * nextLong(bound) returns 0 <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextLongBounded() { SplittableRandom sr = new SplittableRandom(); + for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L)); for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) { long f = sr.nextLong(bound); assertTrue(0 <= f && f < bound); @@ -235,7 +248,7 @@ public class SplittableRandomTest extend /** * nextLong(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextLongBounded2() { SplittableRandom sr = new SplittableRandom(); @@ -256,8 +269,40 @@ public class SplittableRandomTest extend } /** + * nextDouble(non-positive) throws IllegalArgumentException + */ + public void testNextDoubleBoundNonPositive() { + SplittableRandom sr = new SplittableRandom(); + 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)); + } + + /** + * nextDouble(! (least < bound)) throws IllegalArgumentException + */ + public void testNextDoubleBadBounds() { + SplittableRandom sr = new SplittableRandom(); + 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)); + } + + // TODO: Test infinite bounds! + //() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d), + //() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY), + + /** * nextDouble(least, bound) returns least <= value < bound; - * repeated calls produce at least one different result + * repeated calls produce at least two distinct results */ public void testNextDoubleBounded2() { SplittableRandom sr = new SplittableRandom(); @@ -283,21 +328,14 @@ public class SplittableRandomTest extend */ public void testBadStreamSize() { SplittableRandom r = new SplittableRandom(); - try { - java.util.stream.IntStream x = r.ints(-1L); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } - try { - java.util.stream.LongStream x = r.longs(-1L); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } - try { - java.util.stream.DoubleStream x = r.doubles(-1L); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } + 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); }); } /** @@ -306,21 +344,14 @@ public class SplittableRandomTest extend */ public void testBadStreamBounds() { SplittableRandom r = new SplittableRandom(); - try { - java.util.stream.IntStream x = r.ints(2, 1); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } - try { - java.util.stream.LongStream x = r.longs(1, -2); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } - try { - java.util.stream.DoubleStream x = r.doubles(0, 0); - shouldThrow(); - } catch (IllegalArgumentException ok) { - } + 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); }); } /** @@ -332,8 +363,8 @@ public class SplittableRandomTest extend long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.ints(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.ints(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); size += 524959; } } @@ -347,8 +378,8 @@ public class SplittableRandomTest extend long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.longs(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.longs(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); size += 524959; } } @@ -362,13 +393,12 @@ public class SplittableRandomTest extend long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.doubles(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.doubles(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); size += 524959; } } - /** * Each of a parallel sized stream of bounded ints is within bounds */ @@ -379,12 +409,13 @@ public class SplittableRandomTest extend for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) { for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) { final int lo = least, hi = bound; - r.ints(size, lo, hi).parallel(). - forEach(x -> {if (x < lo || x >= hi) - fails.getAndIncrement(); }); + r.ints(size, lo, hi).parallel().forEach( + x -> { + if (x < lo || x >= hi) + fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -397,12 +428,13 @@ public class SplittableRandomTest extend for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) { for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) { final long lo = least, hi = bound; - r.longs(size, lo, hi).parallel(). - forEach(x -> {if (x < lo || x >= hi) - fails.getAndIncrement(); }); + r.longs(size, lo, hi).parallel().forEach( + x -> { + if (x < lo || x >= hi) + fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -415,12 +447,13 @@ public class SplittableRandomTest extend for (double least = 0.00011; least < 1.0e20; least *= 9) { for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) { final double lo = least, hi = bound; - r.doubles(size, lo, hi).parallel(). - forEach(x -> {if (x < lo || x >= hi) - fails.getAndIncrement(); }); + r.doubles(size, lo, hi).parallel().forEach( + x -> { + if (x < lo || x >= hi) + fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -430,8 +463,8 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.ints().limit(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); } /** @@ -441,11 +474,10 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.longs().limit(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); } - /** * A parallel unsized stream of doubles generates at least 100 values */ @@ -453,8 +485,8 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.doubles().limit(size).parallel().forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); } /** @@ -464,8 +496,8 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.ints().limit(size).forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.ints().limit(size).forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); } /** @@ -475,11 +507,10 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.longs().limit(size).forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.longs().limit(size).forEach(x -> counter.increment()); + assertEquals(size, counter.sum()); } - /** * A sequential unsized stream of doubles generates at least 100 values */ @@ -487,9 +518,68 @@ public class SplittableRandomTest extend LongAdder counter = new LongAdder(); SplittableRandom r = new SplittableRandom(); long size = 100; - r.doubles().limit(size).forEach(x -> {counter.increment();}); - assertEquals(counter.sum(), size); + r.doubles().limit(size).forEach(x -> counter.increment()); + 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) {} + } }