--- jsr166/src/test/tck/SplittableRandomTest.java 2013/07/11 23:06:47 1.2 +++ jsr166/src/test/tck/SplittableRandomTest.java 2013/09/24 06:35:35 1.9 @@ -22,17 +22,17 @@ 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 @@ -41,14 +41,15 @@ public class SplittableRandomTest extend // max sampled int bound static final int MAX_INT_BOUND = (1 << 28); - // Max sampled long bound + // max sampled long bound static final long MAX_LONG_BOUND = (1L << 42); // 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 +61,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 +73,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); @@ -91,7 +92,7 @@ public class SplittableRandomTest extend 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) + for (int i = 0; i < REPS; ++i) assertEquals(sr1.nextLong(), sr2.nextLong()); } } @@ -127,18 +128,20 @@ public class SplittableRandomTest extend } /** - * nextInt(negative) throws IllegalArgumentException; + * nextInt(non-positive) throws IllegalArgumentException */ - public void testNextIntBoundedNeg() { + public void testNextIntNonPositive() { SplittableRandom sr = new SplittableRandom(); - try { - int f = sr.nextInt(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + Runnable[] throwingActions = { + () -> sr.nextInt(-17), + () -> sr.nextInt(0), + () -> sr.nextInt(Integer.MIN_VALUE), + }; + assertThrows(IllegalArgumentException.class, throwingActions); } /** - * nextInt(least >= bound) throws IllegalArgumentException; + * nextInt(least >= bound) throws IllegalArgumentException */ public void testNextIntBadBounds() { SplittableRandom sr = new SplittableRandom(); @@ -150,7 +153,7 @@ public class SplittableRandomTest extend /** * 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(); @@ -171,7 +174,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,18 +195,20 @@ public class SplittableRandomTest extend } /** - * nextLong(negative) throws IllegalArgumentException; + * nextLong(non-positive) throws IllegalArgumentException */ - public void testNextLongBoundedNeg() { + public void testNextLongNonPositive() { SplittableRandom sr = new SplittableRandom(); - try { - long f = sr.nextLong(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + Runnable[] throwingActions = { + () -> sr.nextLong(-17L), + () -> sr.nextLong(0L), + () -> sr.nextLong(Long.MIN_VALUE), + }; + assertThrows(IllegalArgumentException.class, throwingActions); } /** - * nextLong(least >= bound) throws IllegalArgumentException; + * nextLong(least >= bound) throws IllegalArgumentException */ public void testNextLongBadBounds() { SplittableRandom sr = new SplittableRandom(); @@ -215,7 +220,7 @@ public class SplittableRandomTest extend /** * 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(); @@ -235,7 +240,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 +261,23 @@ public class SplittableRandomTest extend } /** + * nextDouble(non-positive) throws IllegalArgumentException + */ + public void testNextDoubleNonPositive() { + SplittableRandom sr = new SplittableRandom(); + Runnable[] throwingActions = { + () -> 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); + } + + /** * 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 +303,15 @@ 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) { - } + Runnable[] throwingActions = { + () -> { 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); } /** @@ -306,21 +320,15 @@ 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) { - } + Runnable[] throwingActions = { + () -> { 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); } /** @@ -332,8 +340,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 +355,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 +370,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 */ @@ -380,11 +387,11 @@ public class SplittableRandomTest extend 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) + forEach(x -> {if (x < lo || x >= hi) fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -398,11 +405,11 @@ public class SplittableRandomTest extend 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) + forEach(x -> {if (x < lo || x >= hi) fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -416,11 +423,11 @@ public class SplittableRandomTest extend 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) + forEach(x -> {if (x < lo || x >= hi) fails.getAndIncrement(); }); } } - assertEquals(fails.get(), 0); + assertEquals(0, fails.get()); } /** @@ -430,8 +437,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 +448,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 +459,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 +470,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 +481,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 +492,8 @@ 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()); } - }