--- jsr166/src/test/tck/ThreadLocalRandomTest.java 2016/11/13 03:36:50 1.22 +++ jsr166/src/test/tck/ThreadLocalRandomTest.java 2018/01/28 16:41:13 1.29 @@ -57,33 +57,44 @@ public class ThreadLocalRandomTest exten * possible values. */ public void testNext() throws ReflectiveOperationException { + // Inhibit "An illegal reflective access operation has occurred" + if (!testImplementationDetails) return; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final java.lang.reflect.Method m; try { - java.lang.reflect.Method m - = ThreadLocalRandom.class.getDeclaredMethod( - "next", new Class[] { int.class }); + m = ThreadLocalRandom.class.getDeclaredMethod("next", 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 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); + { + 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; } - } catch (SecurityException acceptable) {} + assertTrue(i < NCALLS); + } } /** @@ -343,7 +354,7 @@ public class ThreadLocalRandomTest exten // Don't use main thread's ThreadLocalRandom - it is likely to // be polluted by previous tests. final AtomicReference threadLocalRandom = - new AtomicReference(); + new AtomicReference<>(); final AtomicLong rand = new AtomicLong(); long firstRand = 0; @@ -373,4 +384,38 @@ public class ThreadLocalRandomTest exten fail("all threads generate the same pseudo-random sequence"); } + /** + * Repeated calls to nextBytes produce at least values of different signs for every byte + */ + public void testNextBytes() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + int n = rnd.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]; + rnd.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() { + ThreadLocalRandom.current().nextBytes(new byte[0]); + } + + public void testNextBytes_nullArray() { + try { + ThreadLocalRandom.current().nextBytes(null); + shouldThrow(); + } catch (NullPointerException success) {} + } + }