--- jsr166/src/test/tck/SplittableRandomTest.java 2014/12/31 16:44:02 1.15 +++ jsr166/src/test/tck/SplittableRandomTest.java 2016/11/13 03:36:50 1.20 @@ -3,16 +3,18 @@ * 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.SplittableRandom; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.LongAdder; +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); @@ -48,6 +50,41 @@ public class SplittableRandomTest extend Integer.getInteger("SplittableRandomTest.reps", 4); /** + * Repeated calls to next (only accessible via reflection) produce + * at least two distinct results, and repeated calls produce all + * possible values. + */ + public void testNext() throws ReflectiveOperationException { + SplittableRandom rnd = new SplittableRandom(); + try { + java.lang.reflect.Method m + = SplittableRandom.class.getDeclaredMethod( + "next", new Class[] { int.class }); + m.setAccessible(true); + + int i; + { + int val = new java.util.Random().nextInt(4); + for (i = 0; i < NCALLS; i++) { + int q = (int) m.invoke(rnd, new Object[] { 2 }); + if (val == q) break; + } + assertTrue(i < NCALLS); + } + + { + int r = (int) m.invoke(rnd, new Object[] { 3 }); + for (i = 0; i < NCALLS; i++) { + int q = (int) m.invoke(rnd, new Object[] { 3 }); + assertTrue(q < (1<<3)); + if (r != q) break; + } + assertTrue(i < NCALLS); + } + } catch (SecurityException acceptable) {} + } + + /** * Repeated calls to nextInt produce at least two distinct results */ public void testNextInt() { @@ -158,6 +195,7 @@ public class SplittableRandomTest extend */ 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); @@ -227,6 +265,7 @@ public class SplittableRandomTest extend */ 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); @@ -408,9 +447,10 @@ 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(0, fails.get()); @@ -426,9 +466,10 @@ 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(0, fails.get()); @@ -444,9 +485,10 @@ 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(0, fails.get());