--- jsr166/src/test/tck/ThreadLocalRandom8Test.java 2013/08/16 07:10:20 1.2 +++ jsr166/src/test/tck/ThreadLocalRandom8Test.java 2019/02/22 19:27:47 1.13 @@ -3,29 +3,32 @@ * 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.AtomicInteger; import java.util.concurrent.atomic.LongAdder; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ThreadLocalRandom8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ThreadLocalRandom8Test.class); } // 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); // Number of replications for other checks - static final int REPS = 20; + static final int REPS = + Integer.getInteger("ThreadLocalRandom8Test.reps", 4); /** * Invoking sized ints, long, doubles, with negative sizes throws @@ -33,30 +36,14 @@ public class ThreadLocalRandom8Test exte */ public void testBadStreamSize() { ThreadLocalRandom r = ThreadLocalRandom.current(); - try { - java.util.stream.IntStream x = r.ints(-1L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.IntStream x = r.ints(-1L, 2, 3); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.LongStream x = r.longs(-1L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.DoubleStream x = r.doubles(-1L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> r.ints(-1L), + () -> r.ints(-1L, 2, 3), + () -> r.longs(-1L), + () -> r.longs(-1L, -1L, 1L), + () -> r.doubles(-1L), + () -> r.doubles(-1L, .5, .6)); } /** @@ -65,30 +52,14 @@ public class ThreadLocalRandom8Test exte */ public void testBadStreamBounds() { ThreadLocalRandom r = ThreadLocalRandom.current(); - try { - java.util.stream.IntStream x = r.ints(2, 1); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.IntStream x = r.ints(10, 42, 42); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.LongStream x = r.longs(-1L, -1L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.LongStream x = r.longs(10, 1L, -2L); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); - shouldThrow(); - } catch (IllegalArgumentException success) {} - try { - java.util.stream.DoubleStream x = r.doubles(10, .5, .4); - shouldThrow(); - } catch (IllegalArgumentException success) {} + assertThrows( + IllegalArgumentException.class, + () -> r.ints(2, 1), + () -> r.ints(10, 42, 42), + () -> r.longs(-1L, -1L), + () -> r.longs(10, 1L, -2L), + () -> r.doubles(0.0, 0.0), + () -> r.doubles(10, .5, .4)); } /** @@ -100,7 +71,7 @@ public class ThreadLocalRandom8Test exte long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.ints(size).parallel().forEach(x -> {counter.increment();}); + r.ints(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); size += 524959; } @@ -115,7 +86,7 @@ public class ThreadLocalRandom8Test exte long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.longs(size).parallel().forEach(x -> {counter.increment();}); + r.longs(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); size += 524959; } @@ -130,7 +101,7 @@ public class ThreadLocalRandom8Test exte long size = 0; for (int reps = 0; reps < REPS; ++reps) { counter.reset(); - r.doubles(size).parallel().forEach(x -> {counter.increment();}); + r.doubles(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); size += 524959; } @@ -146,9 +117,10 @@ public class ThreadLocalRandom8Test exte 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()); @@ -164,9 +136,10 @@ public class ThreadLocalRandom8Test exte 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()); @@ -182,9 +155,10 @@ public class ThreadLocalRandom8Test exte 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()); @@ -197,7 +171,7 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); + r.ints().limit(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } @@ -208,7 +182,7 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); + r.longs().limit(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } @@ -219,7 +193,7 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); + r.doubles().limit(size).parallel().forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } @@ -230,7 +204,7 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.ints().limit(size).forEach(x -> {counter.increment();}); + r.ints().limit(size).forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } @@ -241,7 +215,7 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.longs().limit(size).forEach(x -> {counter.increment();}); + r.longs().limit(size).forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } @@ -252,8 +226,24 @@ public class ThreadLocalRandom8Test exte LongAdder counter = new LongAdder(); ThreadLocalRandom r = ThreadLocalRandom.current(); long size = 100; - r.doubles().limit(size).forEach(x -> {counter.increment();}); + r.doubles().limit(size).forEach(x -> counter.increment()); assertEquals(size, counter.sum()); } + /** + * A deserialized/reserialized ThreadLocalRandom is always + * identical to ThreadLocalRandom.current() + */ + public void testSerialization() { + assertSame( + ThreadLocalRandom.current(), + serialClone(ThreadLocalRandom.current())); + // In the current implementation, there is exactly one shared instance + if (testImplementationDetails) + assertSame( + ThreadLocalRandom.current(), + java.util.concurrent.CompletableFuture.supplyAsync( + () -> serialClone(ThreadLocalRandom.current())).join()); + } + }