1 /* 2 * Copyright (c) 2022, 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 /* 25 * @test 26 * @summary Verify nextDouble stays within range 27 * @bug 8280550 8280950 8281183 28 */ 29 30 package test.java.util.Random; 31 32 import java.util.SplittableRandom; 33 import java.util.random.RandomGenerator; 34 35 public class RandomNextDoubleBoundary { main(String... args)36 public static void main(String... args) { 37 negativeBounds(); 38 positiveBounds(); 39 } 40 negativeBounds()41 private static void negativeBounds() { 42 // Both bounds are negative 43 double lowerBound = -1.0000000000000002; 44 double upperBound = -1.0; 45 var sr = new SplittableRandom(42L); 46 var r = sr.nextDouble(lowerBound, upperBound); 47 48 if (r >= upperBound) { 49 System.err.println("r = " + r + "\t" + Double.toHexString(r)); 50 System.err.println("ub = " + upperBound + "\t" + Double.toHexString(upperBound)); 51 throw new RuntimeException("Greater than upper bound"); 52 } 53 54 if (r < lowerBound) { 55 System.err.println("r = " + r + "\t" + Double.toHexString(r)); 56 System.err.println("lb = " + lowerBound + "\t" + Double.toHexString(lowerBound)); 57 throw new RuntimeException("Less than lower bound"); 58 } 59 } 60 positiveBounds()61 private static void positiveBounds() { 62 double[][] originAndBounds = {{10, 100}, 63 {12345, 123456}, 64 {5432167.234, 54321678.1238}}; 65 for (double[] originAndBound : originAndBounds) { 66 nextDoublesWithRange(originAndBound[0], originAndBound[1]); 67 } 68 } 69 nextDoublesWithRange(double origin, double bound)70 public static void nextDoublesWithRange(double origin, double bound) { 71 RandomGenerator rg = new RandomGenerator() { 72 @Override 73 public double nextDouble() { 74 return Double.MAX_VALUE; 75 } 76 77 @Override 78 public long nextLong() { 79 return 0; 80 } 81 }; 82 double value = rg.nextDouble(origin, bound); 83 84 if (bound > 0) { 85 value = rg.nextDouble(bound); // Equivalent to nextDouble(0.0, bound) 86 assertTrue(value >= 0.0); 87 assertTrue(value < bound); 88 } 89 } 90 91 public static void assertTrue(boolean condition) { 92 if (!condition) { 93 throw new AssertionError(); 94 } 95 } 96 } 97