1 /*
   2  * Copyright (c) 1998, 2013, 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         int failures = 0;
  67         for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
  68             double expected = Double.parseDouble(big.toString());
  69             double actual = big.doubleValue();
  70 
  71             // should be bitwise identical
  72             if (Double.doubleToRawLongBits(expected) != Double
  73                     .doubleToRawLongBits(actual)) {
  74                 System.out.println(big);
  75                 failures++;
  76             }
  77         }
  78         return failures;
  79     }
  80 
  81     public static int testFloatValue() {
  82         int failures = 0;
  83         for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
  84             float expected = Float.parseFloat(big.toString());
  85             float actual = big.floatValue();
  86 
  87             // should be bitwise identical
  88             if (Float.floatToRawIntBits(expected) != Float
  89                     .floatToRawIntBits(actual)) {
  90                 System.out.println(big + " " + expected + " " + actual);
  91                 failures++;
  92             }
  93         }
  94         return failures;
  95     }
  96 
  97     public static void main(String[] args) {
  98         int failures = testDoubleValue();
  99         failures += testFloatValue();
 100         if (failures > 0) {
 101             throw new RuntimeException("Incurred " + failures
 102                     + " failures while testing primitive conversions.");
 103         }
 104     }
 105 }