--- jsr166/src/test/tck/ThreadLocalRandomTest.java 2013/08/14 12:41:18 1.15 +++ jsr166/src/test/tck/ThreadLocalRandomTest.java 2014/12/31 19:05:43 1.20 @@ -3,13 +3,13 @@ * 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.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import java.util.concurrent.atomic.LongAdder; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ThreadLocalRandomTest extends JSR166TestCase { @@ -118,23 +118,34 @@ public class ThreadLocalRandomTest exten } /** - * nextInt(negative) throws IllegalArgumentException + * nextInt(non-positive) throws IllegalArgumentException */ - public void testNextIntBoundedNeg() { - try { - int f = ThreadLocalRandom.current().nextInt(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + public void testNextIntBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) { + try { + rnd.nextInt(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** * nextInt(least >= bound) throws IllegalArgumentException */ public void testNextIntBadBounds() { - try { - int f = ThreadLocalRandom.current().nextInt(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + int[][] badBoundss = { + { 17, 2 }, + { -42, -42 }, + { Integer.MAX_VALUE, Integer.MIN_VALUE }, + }; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (int[] badBounds : badBoundss) { + try { + rnd.nextInt(badBounds[0], badBounds[1]); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** @@ -179,23 +190,34 @@ public class ThreadLocalRandomTest exten } /** - * nextLong(negative) throws IllegalArgumentException + * nextLong(non-positive) throws IllegalArgumentException */ - public void testNextLongBoundedNeg() { - try { - long f = ThreadLocalRandom.current().nextLong(-17); - shouldThrow(); - } catch (IllegalArgumentException success) {} + public void testNextLongBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) { + try { + rnd.nextLong(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** * nextLong(least >= bound) throws IllegalArgumentException */ public void testNextLongBadBounds() { - try { - long f = ThreadLocalRandom.current().nextLong(17, 2); - shouldThrow(); - } catch (IllegalArgumentException success) {} + long[][] badBoundss = { + { 17L, 2L }, + { -42L, -42L }, + { Long.MAX_VALUE, Long.MIN_VALUE }, + }; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (long[] badBounds : badBoundss) { + try { + rnd.nextLong(badBounds[0], badBounds[1]); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } } /** @@ -239,6 +261,26 @@ public class ThreadLocalRandomTest exten } /** + * nextDouble(non-positive) throws IllegalArgumentException + */ + public void testNextDoubleBoundNonPositive() { + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + double[] badBounds = { + 0.0d, + -17.0d, + -Double.MIN_VALUE, + Double.NEGATIVE_INFINITY, + Double.NaN, + }; + for (double bound : badBounds) { + try { + rnd.nextDouble(bound); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } + } + + /** * nextDouble(least, bound) returns least <= value < bound; * repeated calls produce at least two distinct results */ @@ -272,7 +314,7 @@ public class ThreadLocalRandomTest exten long firstRand = 0; ThreadLocalRandom firstThreadLocalRandom = null; - final CheckedRunnable getRandomState = new CheckedRunnable() { + Runnable getRandomState = new CheckedRunnable() { public void realRun() { ThreadLocalRandom current = ThreadLocalRandom.current(); assertSame(current, ThreadLocalRandom.current()); @@ -296,233 +338,4 @@ public class ThreadLocalRandomTest exten fail("all threads generate the same pseudo-random sequence"); } - /** - * Invoking sized ints, long, doubles, with negative sizes throws - * IllegalArgumentException - */ - 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) {} - } - - /** - * Invoking bounded ints, long, doubles, with illegal bounds throws - * IllegalArgumentException - */ - 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) {} - } - - /** - * A parallel sized stream of ints generates the given number of values - */ - public void testIntsCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 0; - for (int reps = 0; reps < REPS; ++reps) { - counter.reset(); - r.ints(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - size += 524959; - } - } - - /** - * A parallel sized stream of longs generates the given number of values - */ - public void testLongsCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 0; - for (int reps = 0; reps < REPS; ++reps) { - counter.reset(); - r.longs(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - size += 524959; - } - } - - /** - * A parallel sized stream of doubles generates the given number of values - */ - public void testDoublesCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 0; - for (int reps = 0; reps < REPS; ++reps) { - counter.reset(); - 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 - */ - public void testBoundedInts() { - AtomicInteger fails = new AtomicInteger(0); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 12345L; - 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(); }); - } - } - assertEquals(0, fails.get()); - } - - /** - * Each of a parallel sized stream of bounded longs is within bounds - */ - public void testBoundedLongs() { - AtomicInteger fails = new AtomicInteger(0); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 123L; - 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(); }); - } - } - assertEquals(0, fails.get()); - } - - /** - * Each of a parallel sized stream of bounded doubles is within bounds - */ - public void testBoundedDoubles() { - AtomicInteger fails = new AtomicInteger(0); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 456; - 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(); }); - } - } - assertEquals(0, fails.get()); - } - - /** - * A parallel unsized stream of ints generates at least 100 values - */ - public void testUnsizedIntsCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.ints().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - - /** - * A parallel unsized stream of longs generates at least 100 values - */ - public void testUnsizedLongsCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.longs().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - - /** - * A parallel unsized stream of doubles generates at least 100 values - */ - public void testUnsizedDoublesCount() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.doubles().limit(size).parallel().forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - - /** - * A sequential unsized stream of ints generates at least 100 values - */ - public void testUnsizedIntsCountSeq() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.ints().limit(size).forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - - /** - * A sequential unsized stream of longs generates at least 100 values - */ - public void testUnsizedLongsCountSeq() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.longs().limit(size).forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - - /** - * A sequential unsized stream of doubles generates at least 100 values - */ - public void testUnsizedDoublesCountSeq() { - LongAdder counter = new LongAdder(); - ThreadLocalRandom r = ThreadLocalRandom.current(); - long size = 100; - r.doubles().limit(size).forEach(x -> {counter.increment();}); - assertEquals(size, counter.sum()); - } - }