--- jsr166/src/test/tck/ThreadLocalRandomTest.java 2013/09/27 20:22:26 1.19 +++ jsr166/src/test/tck/ThreadLocalRandomTest.java 2016/12/10 12:39:15 1.24 @@ -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.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ThreadLocalRandomTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ThreadLocalRandomTest.class); @@ -50,6 +52,50 @@ public class ThreadLocalRandomTest exten } /** + * 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 { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final java.lang.reflect.Method m; + try { + m = ThreadLocalRandom.class.getDeclaredMethod( + "next", new Class[] { int.class }); + m.setAccessible(true); + } catch (SecurityException acceptable) { + // Security manager may deny access + return; + } catch (Exception ex) { + // jdk9 module system may deny access + if (ex.getClass().getSimpleName() + .equals("InaccessibleObjectException")) + return; + throw ex; + } + + 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); + } + } + + /** * Repeated calls to nextInt produce at least two distinct results */ public void testNextInt() {