1 /* 2 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package test.java.util.Random; 25 26 import java.math.BigInteger; 27 import java.security.SecureRandom; 28 import java.util.concurrent.ThreadLocalRandom; 29 import java.util.random.RandomGenerator; 30 import java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator; 31 import java.util.random.RandomGenerator.JumpableGenerator; 32 import java.util.random.RandomGenerator.LeapableGenerator; 33 import java.util.random.RandomGenerator.SplittableGenerator; 34 import java.util.random.RandomGenerator.StreamableGenerator; 35 import java.util.random.RandomGeneratorFactory; 36 import java.util.stream.DoubleStream; 37 import java.util.stream.IntStream; 38 import java.util.stream.LongStream; 39 import java.util.stream.Stream; 40 41 /** 42 * @test 43 * @summary Ensure that all implementations of RandomGenerator supply required methods. 44 * @bug 8248862 45 * @run main RandomTestCoverage 46 * @key randomness 47 */ 48 49 50 public class RandomTestCoverage { coverRandomGenerator(RandomGenerator rng)51 static void coverRandomGenerator(RandomGenerator rng) { 52 boolean bool = rng.nextBoolean(); 53 byte[] bytes = new byte[8]; 54 rng.nextBytes(bytes); 55 56 int i1 = rng.nextInt(); 57 int i2 = rng.nextInt(10); 58 int i3 = rng.nextInt(5, 10); 59 60 long l1 = rng.nextLong(); 61 long l2 = rng.nextLong(10L); 62 long l3 = rng.nextLong(5L, 10L); 63 64 float f1 = rng.nextFloat(); 65 float f2 = rng.nextFloat(1.0f); 66 float f3 = rng.nextFloat(0.5f, 1.0f); 67 68 double d1 = rng.nextDouble(); 69 double d2 = rng.nextDouble(1.0); 70 double d3 = rng.nextDouble(0.5, 1.0); 71 72 double exp = rng.nextExponential(); 73 double gauss1 = rng.nextGaussian(); 74 double gauss2 = rng.nextGaussian(0.5, 2.0); 75 76 IntStream intStream1 = rng.ints(); 77 IntStream intStream2 = rng.ints(5, 10); 78 IntStream intStream3 = rng.ints(5L); 79 IntStream intStream4 = rng.ints(5L, 5, 10); 80 81 LongStream longStream1 = rng.longs(); 82 LongStream longStream2 = rng.longs(5L, 10L); 83 LongStream longStream3 = rng.longs(5L); 84 LongStream longStream4 = rng.longs(5L, 5L, 10L); 85 86 DoubleStream doubleStream1 = rng.doubles(); 87 DoubleStream doubleStream2 = rng.doubles(0.5, 1.0); 88 DoubleStream doubleStream3 = rng.doubles(5); 89 DoubleStream doubleStream4 = rng.doubles(5, 0.5, 1.0); 90 } 91 checkPredicates(RandomGeneratorFactory factory)92 static void checkPredicates(RandomGeneratorFactory factory) { 93 RandomGenerator rng = factory.create(); 94 if (rng instanceof ArbitrarilyJumpableGenerator != factory.isArbitrarilyJumpable()) { 95 throw new RuntimeException("isArbitrarilyJumpable failing"); 96 } 97 if (rng instanceof JumpableGenerator != factory.isJumpable()) { 98 throw new RuntimeException("isJumpable failing"); 99 } 100 if (rng instanceof LeapableGenerator != factory.isLeapable()) { 101 throw new RuntimeException("isLeapable failing"); 102 } 103 if (rng instanceof SplittableGenerator != factory.isSplittable()) { 104 throw new RuntimeException("isArbitrarilyJumpable failing"); 105 } 106 if (rng instanceof StreamableGenerator != factory.isStreamable()) { 107 throw new RuntimeException("isArbitrarilyJumpable failing"); 108 } 109 } 110 coverStreamable(StreamableGenerator rng)111 static void coverStreamable(StreamableGenerator rng) { 112 Stream<RandomGenerator> rngs1 = rng.rngs(); 113 Stream<RandomGenerator> rngs2 = rng.rngs(5L); 114 } 115 coverSplittable(SplittableGenerator rng)116 static void coverSplittable(SplittableGenerator rng) { 117 SplittableGenerator s1 = rng.split(); 118 SplittableGenerator s2 = rng.split(rng); 119 Stream<SplittableGenerator> s3 = rng.splits(); 120 Stream<SplittableGenerator> s4 = rng.splits(5L); 121 Stream<SplittableGenerator> s5 = rng.splits(rng); 122 Stream<SplittableGenerator> s6 = rng.splits(5L, rng); 123 } 124 coverJumpable(JumpableGenerator rng)125 static void coverJumpable(JumpableGenerator rng) { 126 JumpableGenerator j1 = rng.copy(); 127 rng.jump(); 128 RandomGenerator j2 = rng.copyAndJump(); 129 double d = rng.jumpDistance(); 130 Stream<RandomGenerator> j3 = rng.jumps(); 131 Stream<RandomGenerator> j4 = rng.jumps(5L); 132 } 133 coverLeapable(LeapableGenerator rng)134 static void coverLeapable(LeapableGenerator rng) { 135 LeapableGenerator l1 = rng.copy(); 136 rng.leap(); 137 JumpableGenerator l2 = rng.copyAndLeap(); 138 double d = rng.leapDistance(); 139 Stream<JumpableGenerator> l3 = rng.leaps(); 140 Stream<JumpableGenerator> l4 = rng.leaps(5L); 141 } 142 coverArbitrarilyJumpable(ArbitrarilyJumpableGenerator rng)143 static void coverArbitrarilyJumpable(ArbitrarilyJumpableGenerator rng) { 144 ArbitrarilyJumpableGenerator a1 = rng.copy(); 145 rng.jump(); 146 rng.leap(); 147 rng.jump(1.2345); 148 rng.jumpPowerOfTwo(4); 149 RandomGenerator a2 = rng.copyAndJump(); 150 RandomGenerator a3 = rng.copyAndJump(1.2345); 151 Stream<ArbitrarilyJumpableGenerator> a4 = rng.jumps(1.2345); 152 Stream<ArbitrarilyJumpableGenerator> a5 = rng.jumps(5L, 1.2345); 153 154 } 155 coverOf(String name)156 static void coverOf(String name) { 157 coverRandomGenerator(RandomGenerator.of(name)); 158 coverFactory(RandomGeneratorFactory.of(name)); 159 } 160 coverFactory(RandomGeneratorFactory factory)161 static void coverFactory(RandomGeneratorFactory factory) { 162 String name = factory.name(); 163 String group = factory.group(); 164 int stateBits = factory.stateBits(); 165 int equidistribution = factory.equidistribution(); 166 BigInteger period = factory.period(); 167 boolean isStatistical = factory.isStatistical(); 168 boolean isStochastic = factory.isStochastic(); 169 boolean isHardware = factory.isHardware(); 170 boolean isArbitrarilyJumpable = factory.isArbitrarilyJumpable(); 171 boolean isJumpable = factory.isJumpable(); 172 boolean isLeapable = factory.isLeapable(); 173 boolean isSplittable = factory.isSplittable(); 174 175 coverRandomGenerator(factory.create()); 176 coverRandomGenerator(factory.create(12345L)); 177 coverRandomGenerator(factory.create(new byte[] {1, 2, 3, 4, 5, 6, 7, 8})); 178 // Android-added: call one more method. 179 checkPredicates(factory); 180 } 181 coverDefaults()182 static void coverDefaults() { 183 RandomGeneratorFactory<RandomGenerator> factory = 184 RandomGeneratorFactory.getDefault(); 185 RandomGenerator rng = RandomGenerator.getDefault(); 186 } 187 main(String[] args)188 public static void main(String[] args) throws Throwable { 189 RandomGeneratorFactory.all() 190 .forEach(factory -> { 191 coverFactory(factory); 192 coverOf(factory.name()); 193 }); 194 RandomGeneratorFactory.all() 195 .filter(f -> f.isStreamable()) 196 .forEach(factory -> { 197 coverStreamable((StreamableGenerator)factory.create()); 198 }); 199 RandomGeneratorFactory.all() 200 .filter(f -> f.isSplittable()) 201 .forEach(factory -> { 202 coverSplittable((SplittableGenerator)factory.create()); 203 }); 204 RandomGeneratorFactory.all() 205 .filter(f -> f.isJumpable()) 206 .forEach(factory -> { 207 coverJumpable((JumpableGenerator)factory.create()); 208 }); 209 RandomGeneratorFactory.all() 210 .filter(f -> f.isLeapable()) 211 .forEach(factory -> { 212 coverLeapable((LeapableGenerator)factory.create()); 213 }); 214 RandomGeneratorFactory.all() 215 .filter(f -> f.isArbitrarilyJumpable()) 216 .forEach(factory -> { 217 coverArbitrarilyJumpable((ArbitrarilyJumpableGenerator)factory.create()); 218 }); 219 220 coverRandomGenerator(new SecureRandom()); 221 coverRandomGenerator(ThreadLocalRandom.current()); 222 } 223 } 224