Path: blob/master/test/jdk/java/util/Random/RandomTest.java
41149 views
/*1* Copyright (c) 2012, 2017, 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 java.util.Random;24import java.util.concurrent.atomic.AtomicInteger;25import java.util.concurrent.atomic.LongAdder;26import java.util.function.BiConsumer;2728import org.testng.annotations.Test;2930import static org.testng.Assert.*;3132/**33* @test34* @run testng RandomTest35* @summary test methods on Random36* @key randomness37*/38@Test39public class RandomTest {4041// Note: this test was adapted from the 166 TCK ThreadLocalRandomTest test42// and modified to be a TestNG test4344/*45* Testing coverage notes:46*47* We don't test randomness properties, but only that repeated48* calls, up to NCALLS tries, produce at least one different49* result. For bounded versions, we sample various intervals50* across multiples of primes.51*/5253// max numbers of calls to detect getting stuck on one value54static final int NCALLS = 10000;5556// max sampled int bound57static final int MAX_INT_BOUND = (1 << 28);5859// max sampled long bound60static final long MAX_LONG_BOUND = (1L << 42);6162// Number of replications for other checks63static final int REPS = 20;6465/**66* Repeated calls to nextInt produce at least two distinct results67*/68public void testNextInt() {69Random r = new Random();70int f = r.nextInt();71int i = 0;72while (i < NCALLS && r.nextInt() == f)73++i;74assertTrue(i < NCALLS);75}7677/**78* Repeated calls to nextLong produce at least two distinct results79*/80public void testNextLong() {81Random r = new Random();82long f = r.nextLong();83int i = 0;84while (i < NCALLS && r.nextLong() == f)85++i;86assertTrue(i < NCALLS);87}8889/**90* Repeated calls to nextBoolean produce at least two distinct results91*/92public void testNextBoolean() {93Random r = new Random();94boolean f = r.nextBoolean();95int i = 0;96while (i < NCALLS && r.nextBoolean() == f)97++i;98assertTrue(i < NCALLS);99}100101/**102* Repeated calls to nextFloat produce at least two distinct results103*/104public void testNextFloat() {105Random r = new Random();106float f = r.nextFloat();107int i = 0;108while (i < NCALLS && r.nextFloat() == f)109++i;110assertTrue(i < NCALLS);111}112113/**114* Repeated calls to nextDouble produce at least two distinct results115*/116public void testNextDouble() {117Random r = new Random();118double f = r.nextDouble();119int i = 0;120while (i < NCALLS && r.nextDouble() == f)121++i;122assertTrue(i < NCALLS);123}124125/**126* Repeated calls to nextGaussian produce at least two distinct results127*/128public void testNextGaussian() {129Random r = new Random();130double f = r.nextGaussian();131int i = 0;132while (i < NCALLS && r.nextGaussian() == f)133++i;134assertTrue(i < NCALLS);135}136137/**138* nextInt(negative) throws IllegalArgumentException139*/140@Test(expectedExceptions = IllegalArgumentException.class)141public void testNextIntBoundedNeg() {142Random r = new Random();143int f = r.nextInt(-17);144}145146/**147* nextInt(bound) returns 0 <= value < bound; repeated calls produce at148* least two distinct results149*/150public void testNextIntBounded() {151Random r = new Random();152// sample bound space across prime number increments153for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {154int f = r.nextInt(bound);155assertTrue(0 <= f && f < bound);156int i = 0;157int j;158while (i < NCALLS &&159(j = r.nextInt(bound)) == f) {160assertTrue(0 <= j && j < bound);161++i;162}163assertTrue(i < NCALLS);164}165}166167/**168* Invoking sized ints, long, doubles, with negative sizes throws169* IllegalArgumentException170*/171public void testBadStreamSize() {172Random r = new Random();173assertThrowsIAE(() -> r.ints(-1L));174assertThrowsIAE(() -> r.ints(-1L, 2, 3));175assertThrowsIAE(() -> r.longs(-1L));176assertThrowsIAE(() -> r.longs(-1L, -1L, 1L));177assertThrowsIAE(() -> r.doubles(-1L));178assertThrowsIAE(() -> r.doubles(-1L, .5, .6));179}180181/**182* Invoking bounded ints, long, doubles, with illegal bounds throws183* IllegalArgumentException184*/185public void testBadStreamBounds() {186Random r = new Random();187assertThrowsIAE(() -> r.ints(2, 1));188assertThrowsIAE(() -> r.ints(10, 42, 42));189assertThrowsIAE(() -> r.longs(-1L, -1L));190assertThrowsIAE(() -> r.longs(10, 1L, -2L));191192testDoubleBadOriginBound((o, b) -> r.doubles(10, o, b));193}194195// An arbitrary finite double value196static final double FINITE = Math.PI;197198void testDoubleBadOriginBound(BiConsumer<Double, Double> bi) {199assertThrowsIAE(() -> bi.accept(17.0, 2.0));200assertThrowsIAE(() -> bi.accept(0.0, 0.0));201assertThrowsIAE(() -> bi.accept(Double.NaN, FINITE));202assertThrowsIAE(() -> bi.accept(FINITE, Double.NaN));203assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));204205// Returns NaN206// assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, FINITE));207// assertThrowsIAE(() -> bi.accept(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));208209assertThrowsIAE(() -> bi.accept(FINITE, Double.NEGATIVE_INFINITY));210211// Returns Double.MAX_VALUE212// assertThrowsIAE(() -> bi.accept(FINITE, Double.POSITIVE_INFINITY));213214assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));215assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, FINITE));216assertThrowsIAE(() -> bi.accept(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));217}218219private void assertThrowsIAE(ThrowingRunnable r) {220assertThrows(IllegalArgumentException.class, r);221}222223/**224* A sequential sized stream of ints generates the given number of values225*/226public void testIntsCount() {227LongAdder counter = new LongAdder();228Random r = new Random();229long size = 0;230for (int reps = 0; reps < REPS; ++reps) {231counter.reset();232r.ints(size).forEach(x -> {233counter.increment();234});235assertEquals(counter.sum(), size);236size += 524959;237}238}239240/**241* A sequential sized stream of longs generates the given number of values242*/243public void testLongsCount() {244LongAdder counter = new LongAdder();245Random r = new Random();246long size = 0;247for (int reps = 0; reps < REPS; ++reps) {248counter.reset();249r.longs(size).forEach(x -> {250counter.increment();251});252assertEquals(counter.sum(), size);253size += 524959;254}255}256257/**258* A sequential sized stream of doubles generates the given number of values259*/260public void testDoublesCount() {261LongAdder counter = new LongAdder();262Random r = new Random();263long size = 0;264for (int reps = 0; reps < REPS; ++reps) {265counter.reset();266r.doubles(size).forEach(x -> {267counter.increment();268});269assertEquals(counter.sum(), size);270size += 524959;271}272}273274/**275* Each of a sequential sized stream of bounded ints is within bounds276*/277public void testBoundedInts() {278AtomicInteger fails = new AtomicInteger(0);279Random r = new Random();280long size = 12345L;281for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {282for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {283final int lo = least, hi = bound;284r.ints(size, lo, hi).285forEach(x -> {286if (x < lo || x >= hi)287fails.getAndIncrement();288});289}290}291assertEquals(fails.get(), 0);292}293294/**295* Each of a sequential sized stream of bounded longs is within bounds296*/297public void testBoundedLongs() {298AtomicInteger fails = new AtomicInteger(0);299Random r = new Random();300long size = 123L;301for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {302for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {303final long lo = least, hi = bound;304r.longs(size, lo, hi).305forEach(x -> {306if (x < lo || x >= hi)307fails.getAndIncrement();308});309}310}311assertEquals(fails.get(), 0);312}313314/**315* Each of a sequential sized stream of bounded doubles is within bounds316*/317public void testBoundedDoubles() {318AtomicInteger fails = new AtomicInteger(0);319Random r = new Random();320long size = 456;321for (double least = 0.00011; least < 1.0e20; least *= 9) {322for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {323final double lo = least, hi = bound;324r.doubles(size, lo, hi).325forEach(x -> {326if (x < lo || x >= hi)327fails.getAndIncrement();328});329}330}331assertEquals(fails.get(), 0);332}333334/**335* A parallel unsized stream of ints generates at least 100 values336*/337public void testUnsizedIntsCount() {338LongAdder counter = new LongAdder();339Random r = new Random();340long size = 100;341r.ints().limit(size).parallel().forEach(x -> {342counter.increment();343});344assertEquals(counter.sum(), size);345}346347/**348* A parallel unsized stream of longs generates at least 100 values349*/350public void testUnsizedLongsCount() {351LongAdder counter = new LongAdder();352Random r = new Random();353long size = 100;354r.longs().limit(size).parallel().forEach(x -> {355counter.increment();356});357assertEquals(counter.sum(), size);358}359360/**361* A parallel unsized stream of doubles generates at least 100 values362*/363public void testUnsizedDoublesCount() {364LongAdder counter = new LongAdder();365Random r = new Random();366long size = 100;367r.doubles().limit(size).parallel().forEach(x -> {368counter.increment();369});370assertEquals(counter.sum(), size);371}372373/**374* A sequential unsized stream of ints generates at least 100 values375*/376public void testUnsizedIntsCountSeq() {377LongAdder counter = new LongAdder();378Random r = new Random();379long size = 100;380r.ints().limit(size).forEach(x -> {381counter.increment();382});383assertEquals(counter.sum(), size);384}385386/**387* A sequential unsized stream of longs generates at least 100 values388*/389public void testUnsizedLongsCountSeq() {390LongAdder counter = new LongAdder();391Random r = new Random();392long size = 100;393r.longs().limit(size).forEach(x -> {394counter.increment();395});396assertEquals(counter.sum(), size);397}398399/**400* A sequential unsized stream of doubles generates at least 100 values401*/402public void testUnsizedDoublesCountSeq() {403LongAdder counter = new LongAdder();404Random r = new Random();405long size = 100;406r.doubles().limit(size).forEach(x -> {407counter.increment();408});409assertEquals(counter.sum(), size);410}411412}413414415