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 package test.java.math.BigDecimal; 24 25 /* 26 * @test 27 * @bug 6274390 7082971 28 * @summary Verify {float, double}Value methods work with condensed representation 29 * @run main FloatDoubleValueTests 30 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 FloatDoubleValueTests 31 */ 32 33 import java.math.*; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 38 // Android-changed: Replace error counting with asserts. 39 public class FloatDoubleValueTests { 40 private static final long two2the24 = 1L<<23; 41 private static final long two2the53 = 1L<<52; 42 43 // Largest long that fits exactly in a float 44 private static final long maxFltLong = (long)(Integer.MAX_VALUE & ~(0xff)); 45 46 // Largest long that fits exactly in a double 47 private static final long maxDblLong = Long.MAX_VALUE & ~(0x7ffL); 48 testDoubleValue0(long i, BigDecimal bd)49 static void testDoubleValue0(long i, BigDecimal bd) { 50 Assert.assertFalse(bd.doubleValue() != i || bd.longValue() != i, 51 "Unexpected equality failure for " + i + "\t" + bd); 52 } 53 testFloatValue0(long i, BigDecimal bd)54 static void testFloatValue0(long i, BigDecimal bd) { 55 Assert.assertFalse(bd.floatValue() != i || bd.longValue() != i, 56 "Unexpected equality failure for " + i + "\t" + bd); 57 } 58 checkFloat(BigDecimal bd, float f)59 static void checkFloat(BigDecimal bd, float f) { 60 float fbd = bd.floatValue(); 61 Assert.assertEquals(fbd, f, String.format("Bad conversion:"+ 62 "got %g (%a)\texpected %g (%a)", 63 f, f, fbd, fbd)); 64 } 65 checkDouble(BigDecimal bd, double d)66 static void checkDouble(BigDecimal bd, double d) { 67 double dbd = bd.doubleValue(); 68 69 Assert.assertEquals(dbd, d, String.format("Bad conversion:"+ 70 "got %g (%a)\texpected %g (%a)", 71 d, d, dbd, dbd)); 72 } 73 74 // Test integral values that will convert exactly to both float 75 // and double. 76 @Test testFloatDoubleValue()77 public void testFloatDoubleValue() { 78 long[] longValues = { 79 Long.MIN_VALUE, // -2^63 80 0, 81 1, 82 2, 83 84 two2the24-1, 85 two2the24, 86 two2the24+1, 87 88 maxFltLong-1, 89 maxFltLong, 90 maxFltLong+1, 91 }; 92 93 for(long i : longValues) { 94 BigDecimal bd1 = new BigDecimal(i); 95 BigDecimal bd2 = new BigDecimal(-i); 96 97 testDoubleValue0( i, bd1); 98 testDoubleValue0(-i, bd2); 99 100 testFloatValue0( i, bd1); 101 testFloatValue0(-i, bd2); 102 } 103 104 } 105 106 @Test testDoubleValue()107 public void testDoubleValue() { 108 long[] longValues = { 109 Integer.MAX_VALUE-1, 110 Integer.MAX_VALUE, 111 (long)Integer.MAX_VALUE+1, 112 113 two2the53-1, 114 two2the53, 115 two2the53+1, 116 117 maxDblLong, 118 }; 119 120 // Test integral values that will convert exactly to double 121 // but not float. 122 for(long i : longValues) { 123 BigDecimal bd1 = new BigDecimal(i); 124 BigDecimal bd2 = new BigDecimal(-i); 125 126 testDoubleValue0( i, bd1); 127 testDoubleValue0(-i, bd2); 128 129 checkFloat(bd1, (float)i); 130 checkFloat(bd2, -(float)i); 131 } 132 133 // Now check values that should not convert the same in double 134 for(long i = maxDblLong; i < Long.MAX_VALUE; i++) { 135 BigDecimal bd1 = new BigDecimal(i); 136 BigDecimal bd2 = new BigDecimal(-i); 137 checkDouble(bd1, (double)i); 138 checkDouble(bd2, -(double)i); 139 140 checkFloat(bd1, (float)i); 141 checkFloat(bd2, -(float)i); 142 } 143 144 checkDouble(new BigDecimal(Long.MIN_VALUE), (double)Long.MIN_VALUE); 145 checkDouble(new BigDecimal(Long.MAX_VALUE), (double)Long.MAX_VALUE); 146 } 147 148 @Test testFloatValue()149 public 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 @Test testFloatValue1()163 public 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 @Test testDoubleValue1()172 public 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