# HG changeset patch # User bpb # Date 1430166913 25200 # Mon Apr 27 13:35:13 2015 -0700 # Node ID ae51bf9fceb00beb7ffe8b2f205b9115ce0a586f # Parent 76b64929271bb417f65342d4ff025cf2ecadced1 8078672: Print and allow setting by Java property seeds used to initialize Random instances in java.lang numerics tests Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used. Reviewed-by: XXX diff --git a/test/java/lang/Double/ParseHexFloatingPoint.java b/test/java/lang/Double/ParseHexFloatingPoint.java --- a/test/java/lang/Double/ParseHexFloatingPoint.java +++ b/test/java/lang/Double/ParseHexFloatingPoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,14 +23,12 @@ /* * @test - * @bug 4826774 + * @library .. + * @bug 4826774 8078672 * @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat * @author Joseph D. Darcy */ - -import java.util.regex.*; - public class ParseHexFloatingPoint { private ParseHexFloatingPoint(){} @@ -255,7 +253,7 @@ failures += significandAlignmentTests(); { - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); // Consistency check; double => hexadecimal => double // preserves the original value. for(int i = 0; i < 1000; i++) { diff --git a/test/java/lang/Integer/BitTwiddle.java b/test/java/lang/Integer/BitTwiddle.java --- a/test/java/lang/Integer/BitTwiddle.java +++ b/test/java/lang/Integer/BitTwiddle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4495754 - * @summary Basic test for int bit twiddling + * @library .. + * @bug 4495754 8078672 + * @summary Basic test for int bit twiddling (use -Dseed=X to set PRNG seed) * @author Josh Bloch */ @@ -35,7 +36,7 @@ private static final int N = 1000; // # of repetitions per test public static void main(String args[]) { - Random rnd = new Random(); + Random rnd = RandomFactory.getRandom(); if (highestOneBit(0) != 0) throw new RuntimeException("a"); diff --git a/test/java/lang/Long/BitTwiddle.java b/test/java/lang/Long/BitTwiddle.java --- a/test/java/lang/Long/BitTwiddle.java +++ b/test/java/lang/Long/BitTwiddle.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4495754 - * @summary Basic test for long bit twiddling + * @library .. + * @bug 4495754 8078672 + * @summary Basic test for long bit twiddling (use -Dseed=X to set PRNG seed) * @author Josh Bloch */ @@ -35,7 +36,7 @@ private static final int N = 1000; // # of repetitions per test public static void main(String args[]) { - Random rnd = new Random(); + Random rnd = RandomFactory.getRandom(); if (highestOneBit(0) != 0) throw new RuntimeException("a"); diff --git a/test/java/lang/Math/CubeRootTests.java b/test/java/lang/Math/CubeRootTests.java --- a/test/java/lang/Math/CubeRootTests.java +++ b/test/java/lang/Math/CubeRootTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4347132 4939441 - * @summary Tests for {Math, StrictMath}.cbrt + * @library .. + * @bug 4347132 4939441 8078672 + * @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy */ @@ -35,7 +36,7 @@ static final double NaNd = Double.NaN; // Initialize shared random number generator - static java.util.Random rand = new java.util.Random(); + static java.util.Random rand = RandomFactory.getRandom(); static int testCubeRootCase(double input, double expected) { int failures=0; diff --git a/test/java/lang/Math/HypotTests.java b/test/java/lang/Math/HypotTests.java --- a/test/java/lang/Math/HypotTests.java +++ b/test/java/lang/Math/HypotTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4851638 4939441 - * @summary Tests for {Math, StrictMath}.hypot + * @library .. + * @bug 4851638 4939441 8078672 + * @summary Tests for {Math, StrictMath}.hypot (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy */ @@ -119,7 +120,7 @@ * exponent). While the exponent of r is less than or equal * to (MAX_EXPONENT - 3), the computation should not overflow. */ - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); for(int i = 0; i < 1000; i++) { double d = rand.nextDouble(); // Scale d to have an exponent equal to MAX_EXPONENT -15 diff --git a/test/java/lang/Math/IeeeRecommendedTests.java b/test/java/lang/Math/IeeeRecommendedTests.java --- a/test/java/lang/Math/IeeeRecommendedTests.java +++ b/test/java/lang/Math/IeeeRecommendedTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4860891 4826732 4780454 4939441 4826652 - * @summary Tests for IEEE 754[R] recommended functions and similar methods + * @library .. + * @bug 4860891 4826732 4780454 4939441 4826652 8078672 + * @summary Tests for IEEE 754[R] recommended functions and similar methods (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy */ @@ -45,7 +46,7 @@ static final double Double_MAX_SUBNORMALmm = 0x0.ffffffffffffeP-1022; // Initialize shared random number generator - static java.util.Random rand = new java.util.Random(); + static java.util.Random rand = RandomFactory.getRandom(); /** * Returns a floating-point power of two in the normal range. diff --git a/test/java/lang/Math/Log1pTests.java b/test/java/lang/Math/Log1pTests.java --- a/test/java/lang/Math/Log1pTests.java +++ b/test/java/lang/Math/Log1pTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,8 +23,9 @@ /* * @test - * @bug 4851638 4939441 - * @summary Tests for {Math, StrictMath}.log1p + * @library .. + * @bug 4851638 4939441 8078672 + * @summary Tests for {Math, StrictMath}.log1p (use -Dseed=X to set PRNG seed) * @author Joseph D. Darcy */ @@ -98,7 +99,7 @@ // Construct random values with exponents ranging from -53 to // 52 and compare against HP-15C formula. - java.util.Random rand = new java.util.Random(); + java.util.Random rand = RandomFactory.getRandom(); for(int i = 0; i < 1000; i++) { double d = rand.nextDouble(); diff --git a/test/java/lang/RandomFactory.java b/test/java/lang/RandomFactory.java new file mode 100644 --- /dev/null +++ b/test/java/lang/RandomFactory.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.util.Random; + +/** + * Factory class which generates and prints to STDOUT a long-valued seed + * for use in initializing a PRNG. An instance of {@code Random} may likewise + * be obtained. + */ +public class RandomFactory { + /** + * Attempt to obtain the seed from the value of the "seed" property. + * @return The seed or {@code null} if the "seed" property was not set or + * could not be parsed. + */ + private static Long getSystemSeed() { + Long seed = null; + try { + // note that Long.valueOf(null) also throws a + // NumberFormatException so if the property is undefined this + // will still work correctly + seed = Long.valueOf(System.getProperty("seed")); + } catch (NumberFormatException e) { + // do nothing: seed is still null + } + + return seed; + } + + /** + * Obtain a seed from an independent PRNG. + * + * @return A random seed. + */ + private static long getRandomSeed() { + return new Random().nextLong(); + } + + /** + * Obtain and print to STDOUT a seed appropriate for initializing a PRNG. + * If the system property "seed" is set and has value which may be correctly + * parsed it is used, otherwise a seed is generated using an independent + * PRNG. + * + * @return The seed. + */ + public static long getSeed() { + Long seed = getSystemSeed(); + if (seed == null) { + seed = getRandomSeed(); + } + System.out.println("Seed from RandomFactory = "+seed+"L"); + return seed; + } + + /** + * Obtain and print to STDOUT a seed and use it to initialize a new PRNG + * which is returned. If the system property "seed" is set and has value + * which may be correctly parsed it is used, otherwise a seed is generated + * using an independent PRNG. + * + * @return The PRNG. + */ + public static Random getRandom() { + return new Random(getSeed()); + } +}