1 /*
   2  * Copyright (c) 2005, 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
  27  * @summary Verify {float, double}Value methods work with condensed representation
  28  * @run main FloatDoubleValueTests
  29  * @run main/othervm -XX:+AggressiveOpts 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             0,
  83             1,
  84             2,
  85 
  86             two2the24-1,
  87             two2the24,
  88             two2the24+1,
  89 
  90             maxFltLong-1,
  91             maxFltLong,
  92             maxFltLong+1,
  93         };
  94 
  95         for(long i : longValues) {
  96             BigDecimal bd1 = new BigDecimal(i);
  97             BigDecimal bd2 = new BigDecimal(-i);
  98 
  99             testDoubleValue0( i, bd1);
 100             testDoubleValue0(-i, bd2);
 101 
 102             testFloatValue0( i, bd1);
 103             testFloatValue0(-i, bd2);
 104         }
 105 
 106     }
 107 
 108     static void testDoubleValue() {
 109         long longValues[] = {
 110             Integer.MAX_VALUE-1,
 111             Integer.MAX_VALUE,
 112             (long)Integer.MAX_VALUE+1,
 113 
 114             two2the53-1,
 115             two2the53,
 116             two2the53+1,
 117 
 118             maxDblLong,
 119         };
 120 
 121         // Test integral values that will convert exactly to double
 122         // but not float.
 123         for(long i : longValues) {
 124             BigDecimal bd1 = new BigDecimal(i);
 125             BigDecimal bd2 = new BigDecimal(-i);
 126 
 127             testDoubleValue0( i, bd1);
 128             testDoubleValue0(-i, bd2);
 129 
 130             checkFloat(bd1, (float)i);
 131             checkFloat(bd2, -(float)i);
 132         }
 133 
 134         // Now check values that should not convert the same in double
 135         for(long i = maxDblLong; i < Long.MAX_VALUE; i++) {
 136             BigDecimal bd1 = new BigDecimal(i);
 137             BigDecimal bd2 = new BigDecimal(-i);
 138             checkDouble(bd1, (double)i);
 139             checkDouble(bd2, -(double)i);
 140 
 141             checkFloat(bd1, (float)i);
 142             checkFloat(bd2, -(float)i);
 143         }
 144 
 145         checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE);
 146         checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE);
 147     }
 148 
 149     static void testFloatValue() {
 150         // Now check values that should not convert the same in float
 151         for(long i = maxFltLong; i <= Integer.MAX_VALUE; i++) {
 152             BigDecimal bd1 = new BigDecimal(i);
 153             BigDecimal bd2 = new BigDecimal(-i);
 154             checkFloat(bd1, (float)i);
 155             checkFloat(bd2, -(float)i);
 156 
 157             testDoubleValue0( i, bd1);
 158             testDoubleValue0(-i, bd2);
 159         }
 160     }
 161 
 162     static void testFloatValue1() {
 163         checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f);
 164         checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f);
 165         checkFloat(new BigDecimal("907784232501249e-12"),907.78424f);
 166         checkFloat(new BigDecimal("7784e8"),7.7839997e11f);
 167         checkFloat(new BigDecimal("9077e-8"),9.077e-5f);
 168 
 169     }
 170 
 171     static void testDoubleValue1() {
 172         checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37);
 173         checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24);
 174         checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249);
 175         checkDouble(new BigDecimal("7784e8"), 7.784e11);
 176         checkDouble(new BigDecimal("9077e-8"), 9.077e-5);
 177 
 178     }
 179 
 180     public static void main(String[] args) throws Exception {
 181         testFloatDoubleValue();
 182         testDoubleValue();
 183         testFloatValue();
 184         testFloatValue1();
 185         testDoubleValue1();
 186     }
 187 }