1 /*
   2  * Copyright (c) 1998, 2018, 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 import static java.math.BigInteger.ONE;
  25 
  26 import java.math.BigInteger;
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.Collections;
  30 import java.util.List;
  31 import java.util.Random;
  32 
  33 /**
  34  * @test
  35  * @bug 7131192
  36  * @summary This test ensures that BigInteger.floatValue() and
  37  *          BigInteger.doubleValue() behave correctly.
  38  * @author Louis Wasserman
  39  */
  40 public class PrimitiveConversionTests {
  41     static final List<BigInteger> ALL_BIGINTEGER_CANDIDATES;
  42 
  43     static {
  44         List<BigInteger> samples = new ArrayList<>();
  45         // Now add values near 2^N for lots of values of N.
  46         for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33,
  47                 34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511,
  48                 512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT,
  49                 Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) {
  50             BigInteger x = ONE.shiftLeft(exponent);
  51             for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) {
  52                 samples.add(y);
  53                 samples.add(y.negate());
  54             }
  55         }
  56 
  57         Random rng = new Random(1234567);
  58         for (int i = 0; i < 2000; i++) {
  59             samples.add(new BigInteger(rng.nextInt(2000), rng));
  60         }
  61 
  62         ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples);
  63     }
  64 
  65     public static int testDoubleValue() {
  66         System.out.println("--- testDoubleValue ---");
  67         int failures = 0;
  68         for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
  69             double expected = Double.parseDouble(big.toString());
  70             double actual = big.doubleValue();
  71 
  72             // should be bitwise identical
  73             if (Double.doubleToRawLongBits(expected) != Double
  74                     .doubleToRawLongBits(actual)) {
  75                 System.out.format("big: %s, expected: %f, actual: %f%n",
  76                     big, expected, actual);
  77                 failures++;
  78             }
  79         }
  80         return failures;
  81     }
  82 
  83     public static int testFloatValue() {
  84         System.out.println("--- testFloatValue ---");
  85         int failures = 0;
  86         for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
  87             float expected = Float.parseFloat(big.toString());
  88             float actual = big.floatValue();
  89 
  90             // should be bitwise identical
  91             if (Float.floatToRawIntBits(expected) != Float
  92                     .floatToRawIntBits(actual)) {
  93                 System.out.format("big: %s, expected: %f, actual: %f%n",
  94                     big, expected, actual);
  95                 failures++;
  96             }
  97         }
  98         return failures;
  99     }
 100 
 101     public static void main(String[] args) {
 102         int failures = testDoubleValue();
 103         failures += testFloatValue();
 104         if (failures > 0) {
 105             throw new RuntimeException("Incurred " + failures
 106                     + " failures while testing primitive conversions.");
 107         }
 108     }
 109 }
--- EOF ---