Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/FloatingDecimal/TestFloatingDecimal.java
38839 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Random;24import sun.misc.FloatingDecimal;2526/*27OldFloatingDecimalForTest2829public class OldFloatingDecimalForTest {30public boolean digitsRoundedUp();31public OldFloatingDecimalForTest(double);32public OldFloatingDecimalForTest(float);33public boolean decimalDigitsExact();34public java.lang.String toString();35public java.lang.String toJavaFormatString();36public void appendTo(java.lang.Appendable);37public static OldFloatingDecimalForTest readJavaFormatString(java.lang.String) throws java.lang.NumberFormatException;38public strictfp double doubleValue();39public strictfp float floatValue();40}4142sun.misc.FloatingDecimal4344public class sun.misc.FloatingDecimal {45public sun.misc.FloatingDecimal();46public static java.lang.String toJavaFormatString(double);47public static java.lang.String toJavaFormatString(float);48public static void appendTo(double, java.lang.Appendable);49public static void appendTo(float, java.lang.Appendable);50public static double parseDouble(java.lang.String) throws java.lang.NumberFormatException;51public static float parseFloat(java.lang.String) throws java.lang.NumberFormatException;52public static sun.misc.FloatingDecimal$AbstractD2ABuffer getD2ABuffer(double);53}54*/5556/**57* @test58* @bug 703215459* @summary unit tests of sun.misc.FloatingDecimal60* @author Brian Burkhalter61* @key randomness62*/63public class TestFloatingDecimal {64private static enum ResultType {65RESULT_EXCEPTION,66RESULT_PRINT67}6869private static final ResultType RESULT_TYPE = ResultType.RESULT_PRINT;70private static final int NUM_RANDOM_TESTS = 100000;7172private static final Random RANDOM = new Random();7374private static void result(String message) {75switch (RESULT_TYPE) {76case RESULT_EXCEPTION:77throw new RuntimeException(message);78case RESULT_PRINT:79System.err.println(message);80break;81default:82assert false;83}84}8586private static int check(String test, Object expected, Object actual) {87int failures = 0;88if(!actual.equals(expected)) {89failures++;90result("Test "+test+" expected "+expected+" but obtained "+actual);91}92return failures;93}9495private static int testAppendToDouble() {96System.out.println(" testAppendToDouble");97int failures = 0;9899for(int i = 0; i < NUM_RANDOM_TESTS; i++) {100double[] d = new double[] {101RANDOM.nextLong(),102RANDOM.nextGaussian(),103RANDOM.nextDouble()*Double.MAX_VALUE104};105for(int j = 0; j < d.length; j++) {106OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);107StringBuilder sb = new StringBuilder();108ofd.appendTo(sb);109String oldString = sb.toString();110sb = new StringBuilder();111FloatingDecimal.appendTo(d[j], sb);112String newString = sb.toString();113failures += check("testAppendToDouble", oldString, newString);114}115}116117return failures;118}119120private static int testAppendToFloat() {121System.out.println(" testAppendToFloat");122int failures = 0;123124for(int i = 0; i < NUM_RANDOM_TESTS; i++) {125float[] f = new float[] {126RANDOM.nextLong(),127(float)RANDOM.nextGaussian(),128RANDOM.nextFloat()*Float.MAX_VALUE129};130for(int j = 0; j < f.length; j++) {131OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);132StringBuilder sb = new StringBuilder();133ofd.appendTo(sb);134String oldString = sb.toString();135sb = new StringBuilder();136FloatingDecimal.appendTo(f[j], sb);137String newString = sb.toString();138failures += check("testAppendToFloat", oldString, newString);139}140}141142return failures;143}144145private static int testAppendTo() {146System.out.println("testAppendTo");147int failures = 0;148149failures += testAppendToDouble();150failures += testAppendToFloat();151152return failures;153}154155private static int testParseDouble() {156System.out.println(" testParseDouble");157int failures = 0;158159for(int i = 0; i < NUM_RANDOM_TESTS; i++) {160double[] d = new double[] {161RANDOM.nextLong(),162RANDOM.nextGaussian(),163RANDOM.nextDouble()*Double.MAX_VALUE164};165for(int j = 0; j < d.length; j++) {166OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);167String javaFormatString = ofd.toJavaFormatString();168ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString);169double oldDouble = ofd.doubleValue();170double newDouble = FloatingDecimal.parseDouble(javaFormatString);171failures += check("testParseDouble", oldDouble, newDouble);172}173}174175return failures;176}177178private static int testParseFloat() {179System.out.println(" testParseFloat");180int failures = 0;181182for(int i = 0; i < NUM_RANDOM_TESTS; i++) {183float[] f = new float[] {184RANDOM.nextInt(),185(float)RANDOM.nextGaussian(),186RANDOM.nextFloat()*Float.MAX_VALUE187};188for(int j = 0; j < f.length; j++) {189OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);190String javaFormatString = ofd.toJavaFormatString();191ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString);192float oldFloat = ofd.floatValue();193float newFloat = FloatingDecimal.parseFloat(javaFormatString);194failures += check("testParseFloat", oldFloat, newFloat);195}196}197198return failures;199}200201private static int testParse() {202System.out.println("testParse");203int failures = 0;204205failures += testParseDouble();206failures += testParseFloat();207208return failures;209}210211private static int testToJavaFormatStringDoubleFixed() {212System.out.println(" testToJavaFormatStringDoubleFixed");213int failures = 0;214215double[] d = new double [] {216-5.9522650387500933e18, // dtoa() fast path2170.872989018674569, // dtoa() fast iterative - long2181.1317400099603851e308 // dtoa() slow iterative219};220221for(int i = 0; i < d.length; i++) {222OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[i]);223failures += check("testToJavaFormatStringDoubleFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[i]));224}225226return failures;227}228229private static int testToJavaFormatStringDoubleRandom() {230System.out.println(" testToJavaFormatStringDoubleRandom");231int failures = 0;232233for(int i = 0; i < NUM_RANDOM_TESTS; i++) {234double[] d = new double[] {235RANDOM.nextLong(),236RANDOM.nextGaussian(),237RANDOM.nextDouble()*Double.MAX_VALUE238};239for(int j = 0; j < d.length; j++) {240OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);241failures += check("testToJavaFormatStringDoubleRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[j]));242}243}244245return failures;246}247248private static int testToJavaFormatStringDouble() {249System.out.println(" testToJavaFormatStringDouble");250int failures = 0;251failures += testToJavaFormatStringDoubleFixed();252failures += testToJavaFormatStringDoubleRandom();253return failures;254}255256private static int testToJavaFormatStringFloatFixed() {257System.out.println(" testToJavaFormatStringFloatFixed");258int failures = 0;259260float[] f = new float[] {261-9.8784166e8f, // dtoa() fast path2620.70443946f, // dtoa() fast iterative - int2631.8254228e37f // dtoa() slow iterative264};265266for(int i = 0; i < f.length; i++) {267OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[i]);268failures += check("testToJavaFormatStringFloatFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[i]));269}270271return failures;272}273274private static int testToJavaFormatStringFloatRandom() {275System.out.println(" testToJavaFormatStringFloatRandom");276int failures = 0;277278for(int i = 0; i < NUM_RANDOM_TESTS; i++) {279float[] f = new float[] {280RANDOM.nextInt(),281(float)RANDOM.nextGaussian(),282RANDOM.nextFloat()*Float.MAX_VALUE283};284for(int j = 0; j < f.length; j++) {285OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);286failures += check("testToJavaFormatStringFloatRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[j]));287}288}289290return failures;291}292293private static int testToJavaFormatStringFloat() {294System.out.println(" testToJavaFormatStringFloat");295int failures = 0;296297failures += testToJavaFormatStringFloatFixed();298failures += testToJavaFormatStringFloatRandom();299300return failures;301}302303private static int testToJavaFormatString() {304System.out.println("testToJavaFormatString");305int failures = 0;306307failures += testToJavaFormatStringDouble();308failures += testToJavaFormatStringFloat();309310return failures;311}312313public static void main(String[] args) {314int failures = 0;315316failures += testAppendTo();317failures += testParse();318failures += testToJavaFormatString();319320if (failures != 0) {321throw new RuntimeException("" + failures + " failures while testing FloatingDecimal");322}323}324}325326327