1 /*
   2  * Copyright (c) 2005, 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 /*
  25  * @test
  26  * @bug 6274390 7082971
  27  * @summary Verify {float, double}Value methods work with condensed representation
  28  * @run main FloatDoubleValueTests
  29  * @run main/othervm -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 FloatDoubleValueTests
  30  */
  31 import java.math.*;
  32 
  33 public class FloatDoubleValueTests {
  34     private static final long two2the24 = 1L<<23;
  35     private static final long two2the53 = 1L<<52;
  36 
  37     // Largest long that fits exactly in a float
  38     private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff));
  39 
  40     // Largest long that fits exactly in a double
  41     private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL);
  42 
  43     static void testDoubleValue0(long i, BigDecimal bd) {
  44         if (bd.doubleValue() != i ||
  45             bd.longValue()   != i)
  46             throw new RuntimeException("Unexpected equality failure for " +
  47                                        i + "\t" + bd);
  48     }
  49 
  50     static void testFloatValue0(long i, BigDecimal bd) {
  51         if (bd.floatValue() != i ||
  52             bd.longValue()   != i)
  53             throw new RuntimeException("Unexpected equality failure for " +
  54                                        i + "\t" + bd);
  55     }
  56 
  57     static void checkFloat(BigDecimal bd, float f) {
  58         float fbd = bd.floatValue();
  59         if (f != fbd ) {
  60             String message = String.format("Bad conversion:"+
  61                                            "got %g (%a)\texpected %g (%a)",
  62                                            f, f, fbd, fbd);
  63             throw new RuntimeException(message);
  64         }
  65     }
  66 
  67     static void checkDouble(BigDecimal bd, double d) {
  68         double dbd = bd.doubleValue();
  69 
  70         if (d != dbd ) {
  71             String message = String.format("Bad conversion:"+
  72                                            "got %g (%a)\texpected %g (%a)",
  73                                            d, d, dbd, dbd);
  74             throw new RuntimeException(message);
  75         }
  76     }
  77 
  78     // Test integral values that will convert exactly to both float
  79     // and double.
  80     static void testFloatDoubleValue() {
  81         long longValues[] = {
  82             Long.MIN_VALUE, // -2^63
  83             0,
  84             1,
  85             2,
  86 
  87             two2the24-1,
  88             two2the24,
  89             two2the24+1,
  90 
  91             maxFltLong-1,
  92             maxFltLong,
  93             maxFltLong+1,
  94         };
  95 
  96         for(long i : longValues) {
  97             BigDecimal bd1 = new BigDecimal(i);
  98             BigDecimal bd2 = new BigDecimal(-i);
  99 
 100             testDoubleValue0( i, bd1);
 101             testDoubleValue0(-i, bd2);
 102 
 103             testFloatValue0( i, bd1);
 104             testFloatValue0(-i, bd2);
 105         }
 106 
 107     }
 108 
 109     static void testDoubleValue() {
 110         long longValues[] = {
 111             Integer.MAX_VALUE-1,
 112             Integer.MAX_VALUE,
 113             (long)Integer.MAX_VALUE+1,
 114 
 115             two2the53-1,
 116             two2the53,
 117             two2the53+1,
 118 
 119             maxDblLong,
 120         };
 121 
 122         // Test integral values that will convert exactly to double
 123         // but not float.
 124         for(long i : longValues) {
 125             BigDecimal bd1 = new BigDecimal(i);
 126             BigDecimal bd2 = new BigDecimal(-i);
 127 
 128             testDoubleValue0( i, bd1);
 129             testDoubleValue0(-i, bd2);
 130 
 131             checkFloat(bd1, (float)i);
 132             checkFloat(bd2, -(float)i);
 133         }
 134 
 135         // Now check values that should not convert the same in double
 136         for(long i = maxDblLong; i < Long.MAX_VALUE; i++) {
 137             BigDecimal bd1 = new BigDecimal(i);
 138             BigDecimal bd2 = new BigDecimal(-i);
 139             checkDouble(bd1, (double)i);
 140             checkDouble(bd2, -(double)i);
 141 
 142             checkFloat(bd1, (float)i);
 143             checkFloat(bd2, -(float)i);
 144         }
 145 
 146         checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE);
 147         checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE);
 148     }
 149 
 150     static void testFloatValue() {
 151         // Now check values that should not convert the same in float
 152         for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) {
 153             BigDecimal bd1 = new BigDecimal(i);
 154             BigDecimal bd2 = new BigDecimal(-i);
 155             checkFloat(bd1, (float)i);
 156             checkFloat(bd2, -(float)i);
 157 
 158             testDoubleValue0( i, bd1);
 159             testDoubleValue0(-i, bd2);
 160         }
 161     }
 162 
 163     static void testFloatValue1() {
 164         checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f);
 165         checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f);
 166         checkFloat(new BigDecimal("907784232501249e-12"),907.78424f);
 167         checkFloat(new BigDecimal("7784e8"),7.7839997e11f);
 168         checkFloat(new BigDecimal("9077e-8"),9.077e-5f);
 169 
 170     }
 171 
 172     static void testDoubleValue1() {
 173         checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37);
 174         checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24);
 175         checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249);
 176         checkDouble(new BigDecimal("7784e8"), 7.784e11);
 177         checkDouble(new BigDecimal("9077e-8"), 9.077e-5);
 178 
 179     }
 180 
 181     public static void main(String[] args) throws Exception {
 182         testFloatDoubleValue();
 183         testDoubleValue();
 184         testFloatValue();
 185         testFloatValue1();
 186         testDoubleValue1();
 187     }
 188 }