Path: blob/master/test/jdk/java/util/Random/RandomTestCoverage.java
41149 views
/*1* Copyright (c) 2021, 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.math.BigInteger;24import java.security.SecureRandom;25import java.util.concurrent.ThreadLocalRandom;26import java.util.random.RandomGenerator;27import java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator;28import java.util.random.RandomGenerator.JumpableGenerator;29import java.util.random.RandomGenerator.LeapableGenerator;30import java.util.random.RandomGenerator.SplittableGenerator;31import java.util.random.RandomGenerator.StreamableGenerator;32import java.util.random.RandomGeneratorFactory;33import java.util.stream.DoubleStream;34import java.util.stream.IntStream;35import java.util.stream.LongStream;36import java.util.stream.Stream;3738/**39* @test40* @summary Ensure that all implementations of RandomGenerator supply required methods.41* @bug 824886242* @run main RandomTestCoverage43* @key randomness44*/454647public class RandomTestCoverage {48static void coverRandomGenerator(RandomGenerator rng) {49boolean bool = rng.nextBoolean();50byte[] bytes = new byte[8];51rng.nextBytes(bytes);5253int i1 = rng.nextInt();54int i2 = rng.nextInt(10);55int i3 = rng.nextInt(5, 10);5657long l1 = rng.nextLong();58long l2 = rng.nextLong(10L);59long l3 = rng.nextLong(5L, 10L);6061float f1 = rng.nextFloat();62float f2 = rng.nextFloat(1.0f);63float f3 = rng.nextFloat(0.5f, 1.0f);6465double d1 = rng.nextDouble();66double d2 = rng.nextDouble(1.0);67double d3 = rng.nextDouble(0.5, 1.0);6869double exp = rng.nextExponential();70double gauss1 = rng.nextGaussian();71double gauss2 = rng.nextGaussian(0.5, 2.0);7273IntStream intStream1 = rng.ints();74IntStream intStream2 = rng.ints(5, 10);75IntStream intStream3 = rng.ints(5L);76IntStream intStream4 = rng.ints(5L, 5, 10);7778LongStream longStream1 = rng.longs();79LongStream longStream2 = rng.longs(5L, 10L);80LongStream longStream3 = rng.longs(5L);81LongStream longStream4 = rng.longs(5L, 5L, 10L);8283DoubleStream doubleStream1 = rng.doubles();84DoubleStream doubleStream2 = rng.doubles(0.5, 1.0);85DoubleStream doubleStream3 = rng.doubles(5);86DoubleStream doubleStream4 = rng.doubles(5, 0.5, 1.0);87}8889static void checkPredicates(RandomGeneratorFactory factory) {90RandomGenerator rng = factory.create();91if (rng instanceof ArbitrarilyJumpableGenerator != factory.isArbitrarilyJumpable()) {92throw new RuntimeException("isArbitrarilyJumpable failing");93}94if (rng instanceof JumpableGenerator != factory.isJumpable()) {95throw new RuntimeException("isJumpable failing");96}97if (rng instanceof LeapableGenerator != factory.isLeapable()) {98throw new RuntimeException("isLeapable failing");99}100if (rng instanceof SplittableGenerator != factory.isSplittable()) {101throw new RuntimeException("isArbitrarilyJumpable failing");102}103if (rng instanceof StreamableGenerator != factory.isStreamable()) {104throw new RuntimeException("isArbitrarilyJumpable failing");105}106}107108static void coverStreamable(StreamableGenerator rng) {109Stream<RandomGenerator> rngs1 = rng.rngs();110Stream<RandomGenerator> rngs2 = rng.rngs(5L);111}112113static void coverSplittable(SplittableGenerator rng) {114SplittableGenerator s1 = rng.split();115SplittableGenerator s2 = rng.split(rng);116Stream<SplittableGenerator> s3 = rng.splits();117Stream<SplittableGenerator> s4 = rng.splits(5L);118Stream<SplittableGenerator> s5 = rng.splits(rng);119Stream<SplittableGenerator> s6 = rng.splits(5L, rng);120}121122static void coverJumpable(JumpableGenerator rng) {123JumpableGenerator j1 = rng.copy();124rng.jump();125RandomGenerator j2 = rng.copyAndJump();126double d = rng.jumpDistance();127Stream<RandomGenerator> j3 = rng.jumps();128Stream<RandomGenerator> j4 = rng.jumps(5L);129}130131static void coverLeapable(LeapableGenerator rng) {132LeapableGenerator l1 = rng.copy();133rng.leap();134JumpableGenerator l2 = rng.copyAndLeap();135double d = rng.leapDistance();136Stream<JumpableGenerator> l3 = rng.leaps();137Stream<JumpableGenerator> l4 = rng.leaps(5L);138}139140static void coverArbitrarilyJumpable(ArbitrarilyJumpableGenerator rng) {141ArbitrarilyJumpableGenerator a1 = rng.copy();142rng.jump();143rng.leap();144rng.jump(1.2345);145rng.jumpPowerOfTwo(4);146RandomGenerator a2 = rng.copyAndJump();147RandomGenerator a3 = rng.copyAndJump(1.2345);148Stream<ArbitrarilyJumpableGenerator> a4 = rng.jumps(1.2345);149Stream<ArbitrarilyJumpableGenerator> a5 = rng.jumps(5L, 1.2345);150151}152153static void coverOf(String name) {154coverRandomGenerator(RandomGenerator.of(name));155coverFactory(RandomGeneratorFactory.of(name));156}157158static void coverFactory(RandomGeneratorFactory factory) {159String name = factory.name();160String group = factory.group();161int stateBits = factory.stateBits();162int equidistribution = factory.equidistribution();163BigInteger period = factory.period();164boolean isStatistical = factory.isStatistical();165boolean isStochastic = factory.isStochastic();166boolean isHardware = factory.isHardware();167boolean isArbitrarilyJumpable = factory.isArbitrarilyJumpable();168boolean isJumpable = factory.isJumpable();169boolean isLeapable = factory.isLeapable();170boolean isSplittable = factory.isSplittable();171172coverRandomGenerator(factory.create());173coverRandomGenerator(factory.create(12345L));174coverRandomGenerator(factory.create(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}));175}176177static void coverDefaults() {178RandomGeneratorFactory<RandomGenerator> factory =179RandomGeneratorFactory.getDefault();180RandomGenerator rng = RandomGenerator.getDefault();181}182183public static void main(String[] args) throws Throwable {184RandomGeneratorFactory.all()185.forEach(factory -> {186coverFactory(factory);187coverOf(factory.name());188});189RandomGeneratorFactory.all()190.filter(f -> f.isStreamable())191.forEach(factory -> {192coverStreamable((StreamableGenerator)factory.create());193});194RandomGeneratorFactory.all()195.filter(f -> f.isSplittable())196.forEach(factory -> {197coverSplittable((SplittableGenerator)factory.create());198});199RandomGeneratorFactory.all()200.filter(f -> f.isJumpable())201.forEach(factory -> {202coverJumpable((JumpableGenerator)factory.create());203});204RandomGeneratorFactory.all()205.filter(f -> f.isLeapable())206.forEach(factory -> {207coverLeapable((LeapableGenerator)factory.create());208});209RandomGeneratorFactory.all()210.filter(f -> f.isArbitrarilyJumpable())211.forEach(factory -> {212coverArbitrarilyJumpable((ArbitrarilyJumpableGenerator)factory.create());213});214215coverRandomGenerator(new SecureRandom());216coverRandomGenerator(ThreadLocalRandom.current());217}218}219220221