test/java/math/BigDecimal/StringConstructor.java

Print this page
rev 11823 : 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


   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  * @library ..
  27  * @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460


  28  * @summary Tests the BigDecimal string constructor (use -Dseed=X to set PRNG seed).
  29  * @key randomness
  30  */
  31 
  32 import java.math.*;

  33 
  34 public class StringConstructor {
  35 
  36     private static RandomSeed rndSeed = new RandomSeed(false);
  37 
  38     public static void main(String[] args) throws Exception {
  39         System.out.println("Random number generator seed = " + rndSeed.getSeed());
  40 
  41         constructWithError("");
  42         constructWithError("+");
  43         constructWithError("-");
  44         constructWithError("+e");
  45         constructWithError("-e");
  46         constructWithError("e+");
  47         constructWithError("1.-0");
  48         constructWithError(".-123");
  49         constructWithError("-");
  50         constructWithError("--1.1");
  51         constructWithError("-+1.1");
  52         constructWithError("+-1.1");
  53         constructWithError("1-.1");
  54         constructWithError("1+.1");
  55         constructWithError("1.111+1");
  56         constructWithError("1.111-1");
  57         constructWithError("11.e+");
  58         constructWithError("11.e-");
  59         constructWithError("11.e+-");
  60         constructWithError("11.e-+");
  61         constructWithError("11.e-+1");
  62         constructWithError("11.e+-1");
  63 
  64         // Range checks
  65         constructWithError("1e"+Integer.MIN_VALUE);
  66         constructWithError("10e"+Integer.MIN_VALUE);
  67         constructWithError("0.01e"+Integer.MIN_VALUE);
  68         constructWithError("1e"+((long)Integer.MIN_VALUE-1));
  69         constructWithError("1e"+((long)Integer.MAX_VALUE + 1));
  70 
  71         leadingExponentZeroTest();
  72         nonAsciiZeroTest();
  73 
  74         // Roundtrip tests

  75         for (int i=0; i<100; i++) {
  76             int size = rndSeed.getRandom().nextInt(100) + 1;
  77             BigInteger bi = new BigInteger(size, rndSeed.getRandom());
  78             if (rndSeed.getRandom().nextBoolean())
  79                 bi = bi.negate();
  80             int decimalLength = bi.toString().length();
  81             int scale = rndSeed.getRandom().nextInt(decimalLength);
  82             BigDecimal bd = new BigDecimal(bi, scale);
  83             String bdString = bd.toString();
  84             // System.err.println("bi" + bi.toString() + "\tscale " + scale);
  85             // System.err.println("bd string: " + bdString);
  86             BigDecimal bdDoppel = new BigDecimal(bdString);
  87             if (!bd.equals(bdDoppel)) {
  88                 System.err.println("bd string: scale: " + bd.scale() +
  89                                    "\t" + bdString);
  90                 System.err.println("bd doppel: scale: " + bdDoppel.scale() +
  91                                    "\t" + bdDoppel.toString());
  92                 throw new RuntimeException("String constructor failure.");
  93             }
  94         }
  95     }
  96 
  97 
  98     /*
  99      * Verify precision is set properly if the significand has
 100      * non-ASCII leading zeros.
 101      */




   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  * @library /lib/testlibrary/
  27  * @build jdk.testlibrary.*
  28  * @run main StringConstructor
  29  * @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460 8078672
  30  * @summary Tests the BigDecimal string constructor (use -Dseed=X to set PRNG seed).
  31  * @key randomness
  32  */
  33 
  34 import java.math.*;
  35 import java.util.Random;
  36 
  37 public class StringConstructor {
  38 


  39     public static void main(String[] args) throws Exception {


  40         constructWithError("");
  41         constructWithError("+");
  42         constructWithError("-");
  43         constructWithError("+e");
  44         constructWithError("-e");
  45         constructWithError("e+");
  46         constructWithError("1.-0");
  47         constructWithError(".-123");
  48         constructWithError("-");
  49         constructWithError("--1.1");
  50         constructWithError("-+1.1");
  51         constructWithError("+-1.1");
  52         constructWithError("1-.1");
  53         constructWithError("1+.1");
  54         constructWithError("1.111+1");
  55         constructWithError("1.111-1");
  56         constructWithError("11.e+");
  57         constructWithError("11.e-");
  58         constructWithError("11.e+-");
  59         constructWithError("11.e-+");
  60         constructWithError("11.e-+1");
  61         constructWithError("11.e+-1");
  62 
  63         // Range checks
  64         constructWithError("1e"+Integer.MIN_VALUE);
  65         constructWithError("10e"+Integer.MIN_VALUE);
  66         constructWithError("0.01e"+Integer.MIN_VALUE);
  67         constructWithError("1e"+((long)Integer.MIN_VALUE-1));
  68         constructWithError("1e"+((long)Integer.MAX_VALUE + 1));
  69 
  70         leadingExponentZeroTest();
  71         nonAsciiZeroTest();
  72 
  73         // Roundtrip tests
  74         Random random = RandomFactory.getRandom();
  75         for (int i=0; i<100; i++) {
  76             int size = random.nextInt(100) + 1;
  77             BigInteger bi = new BigInteger(size, random);
  78             if (random.nextBoolean())
  79                 bi = bi.negate();
  80             int decimalLength = bi.toString().length();
  81             int scale = random.nextInt(decimalLength);
  82             BigDecimal bd = new BigDecimal(bi, scale);
  83             String bdString = bd.toString();
  84             // System.err.println("bi" + bi.toString() + "\tscale " + scale);
  85             // System.err.println("bd string: " + bdString);
  86             BigDecimal bdDoppel = new BigDecimal(bdString);
  87             if (!bd.equals(bdDoppel)) {
  88                 System.err.println("bd string: scale: " + bd.scale() +
  89                                    "\t" + bdString);
  90                 System.err.println("bd doppel: scale: " + bdDoppel.scale() +
  91                                    "\t" + bdDoppel.toString());
  92                 throw new RuntimeException("String constructor failure.");
  93             }
  94         }
  95     }
  96 
  97 
  98     /*
  99      * Verify precision is set properly if the significand has
 100      * non-ASCII leading zeros.
 101      */