Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Random/RandomTest.java
38811 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.Assert;24import org.testng.annotations.Test;2526import java.util.Random;27import java.util.concurrent.atomic.AtomicInteger;28import java.util.concurrent.atomic.LongAdder;29import java.util.function.BiConsumer;3031import static org.testng.Assert.*;3233/**34* @test35* @run testng RandomTest36* @summary test methods on Random37* @key randomness38*/39@Test40public class RandomTest {4142// Note: this test was adapted from the 166 TCK ThreadLocalRandomTest test43// and modified to be a TestNG test4445/*46* Testing coverage notes:47*48* We don't test randomness properties, but only that repeated49* calls, up to NCALLS tries, produce at least one different50* result. For bounded versions, we sample various intervals51* across multiples of primes.52*/5354// max numbers of calls to detect getting stuck on one value55static final int NCALLS = 10000;5657// max sampled int bound58static final int MAX_INT_BOUND = (1 << 28);5960// max sampled long bound61static final long MAX_LONG_BOUND = (1L << 42);6263// Number of replications for other checks64static final int REPS = 20;6566/**67* Repeated calls to nextInt produce at least two distinct results68*/69public void testNextInt() {70Random r = new Random();71int f = r.nextInt();72int i = 0;73while (i < NCALLS && r.nextInt() == f)74++i;75assertTrue(i < NCALLS);76}7778/**79* Repeated calls to nextLong produce at least two distinct results80*/81public void testNextLong() {82Random r = new Random();83long f = r.nextLong();84int i = 0;85while (i < NCALLS && r.nextLong() == f)86++i;87assertTrue(i < NCALLS);88}8990/**91* Repeated calls to nextBoolean produce at least two distinct results92*/93public void testNextBoolean() {94Random r = new Random();95boolean f = r.nextBoolean();96int i = 0;97while (i < NCALLS && r.nextBoolean() == f)98++i;99assertTrue(i < NCALLS);100}101102/**103* Repeated calls to nextFloat produce at least two distinct results104*/105public void testNextFloat() {106Random r = new Random();107float f = r.nextFloat();108int i = 0;109while (i < NCALLS && r.nextFloat() == f)110++i;111assertTrue(i < NCALLS);112}113114/**115* Repeated calls to nextDouble produce at least two distinct results116*/117public void testNextDouble() {118Random r = new Random();119double f = r.nextDouble();120int i = 0;121while (i < NCALLS && r.nextDouble() == f)122++i;123assertTrue(i < NCALLS);124}125126/**127* Repeated calls to nextGaussian produce at least two distinct results128*/129public void testNextGaussian() {130Random r = new Random();131double f = r.nextGaussian();132int i = 0;133while (i < NCALLS && r.nextGaussian() == f)134++i;135assertTrue(i < NCALLS);136}137138/**139* nextInt(negative) throws IllegalArgumentException140*/141@Test(expectedExceptions = IllegalArgumentException.class)142public void testNextIntBoundedNeg() {143Random r = new Random();144int f = r.nextInt(-17);145}146147/**148* nextInt(bound) returns 0 <= value < bound; repeated calls produce at149* least two distinct results150*/151public void testNextIntBounded() {152Random r = new Random();153// sample bound space across prime number increments154for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {155int f = r.nextInt(bound);156assertTrue(0 <= f && f < bound);157int i = 0;158int j;159while (i < NCALLS &&160(j = r.nextInt(bound)) == f) {161assertTrue(0 <= j && j < bound);162++i;163}164assertTrue(i < NCALLS);165}166}167168/**169* Invoking sized ints, long, doubles, with negative sizes throws170* IllegalArgumentException171*/172public void testBadStreamSize() {173Random r = new Random();174executeAndCatchIAE(() -> r.ints(-1L));175executeAndCatchIAE(() -> r.ints(-1L, 2, 3));176executeAndCatchIAE(() -> r.longs(-1L));177executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L));178executeAndCatchIAE(() -> r.doubles(-1L));179executeAndCatchIAE(() -> r.doubles(-1L, .5, .6));180}181182/**183* Invoking bounded ints, long, doubles, with illegal bounds throws184* IllegalArgumentException185*/186public void testBadStreamBounds() {187Random r = new Random();188executeAndCatchIAE(() -> r.ints(2, 1));189executeAndCatchIAE(() -> r.ints(10, 42, 42));190executeAndCatchIAE(() -> r.longs(-1L, -1L));191executeAndCatchIAE(() -> r.longs(10, 1L, -2L));192193testDoubleBadOriginBound((o, b) -> r.doubles(10, o, b));194}195196// An arbitrary finite double value197static final double FINITE = Math.PI;198199void testDoubleBadOriginBound(BiConsumer<Double, Double> bi) {200executeAndCatchIAE(() -> bi.accept(17.0, 2.0));201executeAndCatchIAE(() -> bi.accept(0.0, 0.0));202executeAndCatchIAE(() -> bi.accept(Double.NaN, FINITE));203executeAndCatchIAE(() -> bi.accept(FINITE, Double.NaN));204executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));205206// Returns NaN207// executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, FINITE));208// executeAndCatchIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));209210executeAndCatchIAE(() -> bi.accept(FINITE, Double.NEGATIVE_INFINITY));211212// Returns Double.MAX_VALUE213// executeAndCatchIAE(() -> bi.accept(FINITE, Double.POSITIVE_INFINITY));214215executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));216executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, FINITE));217executeAndCatchIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));218}219220private void executeAndCatchIAE(Runnable r) {221executeAndCatch(IllegalArgumentException.class, r);222}223224private void executeAndCatch(Class<? extends Exception> expected, Runnable r) {225Exception caught = null;226try {227r.run();228}229catch (Exception e) {230caught = e;231}232233assertNotNull(caught,234String.format("No Exception was thrown, expected an Exception of %s to be thrown",235expected.getName()));236Assert.assertTrue(expected.isInstance(caught),237String.format("Exception thrown %s not an instance of %s",238caught.getClass().getName(), expected.getName()));239}240241/**242* A sequential sized stream of ints generates the given number of values243*/244public void testIntsCount() {245LongAdder counter = new LongAdder();246Random r = new Random();247long size = 0;248for (int reps = 0; reps < REPS; ++reps) {249counter.reset();250r.ints(size).forEach(x -> {251counter.increment();252});253assertEquals(counter.sum(), size);254size += 524959;255}256}257258/**259* A sequential sized stream of longs generates the given number of values260*/261public void testLongsCount() {262LongAdder counter = new LongAdder();263Random r = new Random();264long size = 0;265for (int reps = 0; reps < REPS; ++reps) {266counter.reset();267r.longs(size).forEach(x -> {268counter.increment();269});270assertEquals(counter.sum(), size);271size += 524959;272}273}274275/**276* A sequential sized stream of doubles generates the given number of values277*/278public void testDoublesCount() {279LongAdder counter = new LongAdder();280Random r = new Random();281long size = 0;282for (int reps = 0; reps < REPS; ++reps) {283counter.reset();284r.doubles(size).forEach(x -> {285counter.increment();286});287assertEquals(counter.sum(), size);288size += 524959;289}290}291292/**293* Each of a sequential sized stream of bounded ints is within bounds294*/295public void testBoundedInts() {296AtomicInteger fails = new AtomicInteger(0);297Random r = new Random();298long size = 12345L;299for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {300for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {301final int lo = least, hi = bound;302r.ints(size, lo, hi).303forEach(x -> {304if (x < lo || x >= hi)305fails.getAndIncrement();306});307}308}309assertEquals(fails.get(), 0);310}311312/**313* Each of a sequential sized stream of bounded longs is within bounds314*/315public void testBoundedLongs() {316AtomicInteger fails = new AtomicInteger(0);317Random r = new Random();318long size = 123L;319for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {320for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {321final long lo = least, hi = bound;322r.longs(size, lo, hi).323forEach(x -> {324if (x < lo || x >= hi)325fails.getAndIncrement();326});327}328}329assertEquals(fails.get(), 0);330}331332/**333* Each of a sequential sized stream of bounded doubles is within bounds334*/335public void testBoundedDoubles() {336AtomicInteger fails = new AtomicInteger(0);337Random r = new Random();338long size = 456;339for (double least = 0.00011; least < 1.0e20; least *= 9) {340for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {341final double lo = least, hi = bound;342r.doubles(size, lo, hi).343forEach(x -> {344if (x < lo || x >= hi)345fails.getAndIncrement();346});347}348}349assertEquals(fails.get(), 0);350}351352/**353* A parallel unsized stream of ints generates at least 100 values354*/355public void testUnsizedIntsCount() {356LongAdder counter = new LongAdder();357Random r = new Random();358long size = 100;359r.ints().limit(size).parallel().forEach(x -> {360counter.increment();361});362assertEquals(counter.sum(), size);363}364365/**366* A parallel unsized stream of longs generates at least 100 values367*/368public void testUnsizedLongsCount() {369LongAdder counter = new LongAdder();370Random r = new Random();371long size = 100;372r.longs().limit(size).parallel().forEach(x -> {373counter.increment();374});375assertEquals(counter.sum(), size);376}377378/**379* A parallel unsized stream of doubles generates at least 100 values380*/381public void testUnsizedDoublesCount() {382LongAdder counter = new LongAdder();383Random r = new Random();384long size = 100;385r.doubles().limit(size).parallel().forEach(x -> {386counter.increment();387});388assertEquals(counter.sum(), size);389}390391/**392* A sequential unsized stream of ints generates at least 100 values393*/394public void testUnsizedIntsCountSeq() {395LongAdder counter = new LongAdder();396Random r = new Random();397long size = 100;398r.ints().limit(size).forEach(x -> {399counter.increment();400});401assertEquals(counter.sum(), size);402}403404/**405* A sequential unsized stream of longs generates at least 100 values406*/407public void testUnsizedLongsCountSeq() {408LongAdder counter = new LongAdder();409Random r = new Random();410long size = 100;411r.longs().limit(size).forEach(x -> {412counter.increment();413});414assertEquals(counter.sum(), size);415}416417/**418* A sequential unsized stream of doubles generates at least 100 values419*/420public void testUnsizedDoublesCountSeq() {421LongAdder counter = new LongAdder();422Random r = new Random();423long size = 100;424r.doubles().limit(size).forEach(x -> {425counter.increment();426});427assertEquals(counter.sum(), size);428}429430}431432433