1 /* 2 * Copyright (c) 2013, 2023, 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 package test.jdk.internal.math.FloatingDecimal; 25 26 import java.util.Random; 27 import jdk.internal.math.FloatingDecimal; 28 29 import jdk.test.lib.RandomFactory; 30 31 import org.junit.jupiter.api.Test; 32 33 import static org.junit.jupiter.api.Assertions.assertEquals; 34 35 /* 36 OldFloatingDecimalForTest 37 38 public class OldFloatingDecimalForTest { 39 public boolean digitsRoundedUp(); 40 public OldFloatingDecimalForTest(double); 41 public OldFloatingDecimalForTest(float); 42 public boolean decimalDigitsExact(); 43 public java.lang.String toString(); 44 public java.lang.String toJavaFormatString(); 45 public void appendTo(java.lang.Appendable); 46 public static OldFloatingDecimalForTest readJavaFormatString(java.lang.String) throws java.lang.NumberFormatException; 47 public strictfp double doubleValue(); 48 public strictfp float floatValue(); 49 } 50 51 jdk.internal.math.FloatingDecimal 52 53 public class jdk.internal.math.FloatingDecimal { 54 public jdk.internal.math.FloatingDecimal(); 55 public static java.lang.String toJavaFormatString(double); 56 public static java.lang.String toJavaFormatString(float); 57 public static void appendTo(double, java.lang.Appendable); 58 public static void appendTo(float, java.lang.Appendable); 59 public static double parseDouble(java.lang.String) throws java.lang.NumberFormatException; 60 public static float parseFloat(java.lang.String) throws java.lang.NumberFormatException; 61 public static jdk.internal.math.FloatingDecimal$AbstractD2ABuffer getD2ABuffer(double); 62 } 63 */ 64 65 /** 66 * @test 67 * @bug 7032154 68 * @summary unit tests of FloatingDecimal (use -Dseed=X to set PRANDOM seed) 69 * @modules java.base/jdk.internal.math 70 * @library .. 71 * @library /test/lib 72 * @library /java/lang/Math 73 * @build jdk.test.lib.RandomFactory 74 * @build DoubleConsts FloatConsts 75 * @run junit TestFloatingDecimal 76 * @author Brian Burkhalter 77 * @key randomness 78 */ 79 public class TestFloatingDecimal { 80 private static final int NUM_RANDOM_TESTS = 100_000; 81 82 private static final Random RANDOM = RandomFactory.getRandom(); 83 check(String test, Object expected, Object actual)84 private static int check(String test, Object expected, Object actual) { 85 int failures = 0; 86 if(!actual.equals(expected)) { 87 failures++; 88 System.err.println("Test " + test + 89 " expected " + expected + 90 " but obtained " + actual); 91 } 92 return failures; 93 } 94 95 @Test testAppendToDouble()96 public void testAppendToDouble() { 97 int failures = 0; 98 99 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 100 double[] d = new double[] { 101 RANDOM.nextLong(), 102 RANDOM.nextGaussian(), 103 RANDOM.nextDouble()*Double.MAX_VALUE 104 }; 105 for(int j = 0; j < d.length; j++) { 106 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]); 107 StringBuilder sb = new StringBuilder(); 108 ofd.appendTo(sb); 109 String oldString = sb.toString(); 110 sb = new StringBuilder(); 111 FloatingDecimal.appendTo(d[j], sb); 112 String newString = sb.toString(); 113 failures += check("testAppendToDouble", oldString, newString); 114 } 115 } 116 117 assertEquals(0, failures); 118 } 119 120 @Test testAppendToFloat()121 public void testAppendToFloat() { 122 int failures = 0; 123 124 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 125 float[] f = new float[] { 126 RANDOM.nextLong(), 127 (float)RANDOM.nextGaussian(), 128 RANDOM.nextFloat()*Float.MAX_VALUE 129 }; 130 for(int j = 0; j < f.length; j++) { 131 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]); 132 StringBuilder sb = new StringBuilder(); 133 ofd.appendTo(sb); 134 String oldString = sb.toString(); 135 sb = new StringBuilder(); 136 FloatingDecimal.appendTo(f[j], sb); 137 String newString = sb.toString(); 138 failures += check("testAppendToFloat", oldString, newString); 139 } 140 } 141 142 assertEquals(0, failures); 143 } 144 145 @Test testParseDouble()146 public void testParseDouble() { 147 int failures = 0; 148 149 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 150 double[] d = new double[] { 151 RANDOM.nextLong(), 152 RANDOM.nextGaussian(), 153 RANDOM.nextDouble()*Double.MAX_VALUE 154 }; 155 for(int j = 0; j < d.length; j++) { 156 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]); 157 String javaFormatString = ofd.toJavaFormatString(); 158 ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString); 159 double oldDouble = ofd.doubleValue(); 160 double newDouble = FloatingDecimal.parseDouble(javaFormatString); 161 failures += check("testParseDouble", oldDouble, newDouble); 162 } 163 } 164 165 assertEquals(0, failures); 166 } 167 168 @Test testParseFloat()169 public void testParseFloat() { 170 int failures = 0; 171 172 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 173 float[] f = new float[] { 174 RANDOM.nextInt(), 175 (float)RANDOM.nextGaussian(), 176 RANDOM.nextFloat()*Float.MAX_VALUE 177 }; 178 for(int j = 0; j < f.length; j++) { 179 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]); 180 String javaFormatString = ofd.toJavaFormatString(); 181 ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString); 182 float oldFloat = ofd.floatValue(); 183 float newFloat = FloatingDecimal.parseFloat(javaFormatString); 184 failures += check("testParseFloat", oldFloat, newFloat); 185 } 186 } 187 188 assertEquals(0, failures); 189 } 190 191 @Test testToJavaFormatStringDoubleFixed()192 public void testToJavaFormatStringDoubleFixed() { 193 int failures = 0; 194 195 double[] d = new double [] { 196 -5.9522650387500933e18, // dtoa() fast path 197 0.872989018674569, // dtoa() fast iterative - long 198 1.1317400099603851e308 // dtoa() slow iterative 199 }; 200 201 for(int i = 0; i < d.length; i++) { 202 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[i]); 203 failures += check("testToJavaFormatStringDoubleFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[i])); 204 } 205 206 assertEquals(0, failures); 207 } 208 209 @Test testToJavaFormatStringDoubleRandom()210 public void testToJavaFormatStringDoubleRandom() { 211 int failures = 0; 212 213 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 214 double[] d = new double[] { 215 RANDOM.nextLong(), 216 RANDOM.nextGaussian(), 217 RANDOM.nextDouble()*Double.MAX_VALUE 218 }; 219 for(int j = 0; j < d.length; j++) { 220 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]); 221 failures += check("testToJavaFormatStringDoubleRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[j])); 222 } 223 } 224 225 assertEquals(0, failures); 226 } 227 228 @Test testToJavaFormatStringFloatFixed()229 public void testToJavaFormatStringFloatFixed() { 230 int failures = 0; 231 232 float[] f = new float[] { 233 -9.8784166e8f, // dtoa() fast path 234 0.70443946f, // dtoa() fast iterative - int 235 1.8254228e37f // dtoa() slow iterative 236 }; 237 238 for(int i = 0; i < f.length; i++) { 239 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[i]); 240 failures += check("testToJavaFormatStringFloatFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[i])); 241 } 242 243 assertEquals(0, failures); 244 } 245 246 @Test testToJavaFormatStringFloatRandom()247 public void testToJavaFormatStringFloatRandom() { 248 int failures = 0; 249 250 for(int i = 0; i < NUM_RANDOM_TESTS; i++) { 251 float[] f = new float[] { 252 RANDOM.nextInt(), 253 (float)RANDOM.nextGaussian(), 254 RANDOM.nextFloat()*Float.MAX_VALUE 255 }; 256 for(int j = 0; j < f.length; j++) { 257 OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]); 258 failures += check("testToJavaFormatStringFloatRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[j])); 259 } 260 } 261 262 assertEquals(0, failures); 263 } 264 } 265