jdk/test/java/text/Format/DecimalFormat/TieRoundingTest.java
changeset 27048 e60960edecfb
parent 23010 6dadb192ad81
equal deleted inserted replaced
27047:05b228626582 27048:e60960edecfb
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  *
    25  *
    26  * @bug 7131459
    26  * @bug 7131459 8039915
    27  * @summary test various situations of NumberFormat rounding when close to tie
    27  * @summary test various situations of NumberFormat rounding when close to tie
    28  * @author Olivier Lagneau
    28  * @author Olivier Lagneau
    29  * @run main TieRoundingTest
    29  * @run main TieRoundingTest
    30  *
    30  *
    31  */
    31  */
    54         String result = nf.format(doubleToTest);
    54         String result = nf.format(doubleToTest);
    55 
    55 
    56         if (!result.equals(expectedOutput)) {
    56         if (!result.equals(expectedOutput)) {
    57             System.out.println();
    57             System.out.println();
    58             System.out.println("========================================");
    58             System.out.println("========================================");
    59             System.out.println("***Error formatting double value from string : " +
    59             System.out.println("***Failure : error formatting value from string : " +
    60                                inputDigits);
    60                                inputDigits);
    61             System.out.println("NumberFormat pattern is  : " +
    61             System.out.println("NumberFormat pattern is  : " +
    62                                ((DecimalFormat ) nf).toPattern());
    62                                ((DecimalFormat ) nf).toPattern());
    63             System.out.println("Maximum number of fractional digits : " + mfd);
    63             System.out.println("Maximum number of fractional digits : " + mfd);
    64             System.out.println("Fractional rounding digit : " + (mfd + 1));
    64             System.out.println("Fractional rounding digit : " + (mfd + 1));
   101         String result = nf.format(longToTest);
   101         String result = nf.format(longToTest);
   102 
   102 
   103         if (!result.equals(expectedOutput)) {
   103         if (!result.equals(expectedOutput)) {
   104             System.out.println();
   104             System.out.println();
   105             System.out.println("========================================");
   105             System.out.println("========================================");
   106             System.out.println("***Error formatting double value from string : " +
   106             System.out.println("***Failure : error formatting value from string : " +
   107                                inputDigits);
   107                                inputDigits);
   108             System.out.println("NumberFormat pattern is  : " +
   108             System.out.println("NumberFormat pattern is  : " +
   109                                ((DecimalFormat ) nf).toPattern());
   109                                ((DecimalFormat ) nf).toPattern());
   110             System.out.println("Maximum number of fractional digits : " + mfd);
   110             System.out.println("Maximum number of fractional digits : " + mfd);
   111             System.out.println("Fractional rounding digit : " + (mfd + 1));
   111             System.out.println("Fractional rounding digit : " + (mfd + 1));
   142         String result = nf.format(someNumber);
   142         String result = nf.format(someNumber);
   143 
   143 
   144         if (!result.equals(expectedOutput)) {
   144         if (!result.equals(expectedOutput)) {
   145             System.out.println();
   145             System.out.println();
   146             System.out.println("========================================");
   146             System.out.println("========================================");
   147             System.out.println("***Error formatting number value from string : " +
   147             System.out.println("***Failure : error formatting value from string : " +
   148                                inputDigits);
   148                                inputDigits);
   149             System.out.println("NumberFormat pattern is  : " +
   149             System.out.println("NumberFormat pattern is  : " +
   150                                ((DecimalFormat ) nf).toPattern());
   150                                ((DecimalFormat ) nf).toPattern());
   151             System.out.println("Maximum number of fractional digits : " + mfd);
   151             System.out.println("Maximum number of fractional digits : " + mfd);
   152             System.out.println("Fractional rounding digit : " + (mfd + 1));
   152             System.out.println("Fractional rounding digit : " + (mfd + 1));
   172         }
   172         }
   173     }
   173     }
   174 
   174 
   175     public static void main(String[] args) {
   175     public static void main(String[] args) {
   176 
   176 
   177         // Only the 3 rounding modes below may be impacted by bug 7131459.
   177         // The 3 HALF_* rounding modes are impacted by bugs 7131459, 8039915.
   178         // So we do not test the other rounding modes.
   178         // So we do not test the other rounding modes.
   179         RoundingMode[] roundingModes = {
   179         RoundingMode[] roundingModes = {
   180             RoundingMode.HALF_DOWN,
   180             RoundingMode.HALF_DOWN,
   181             RoundingMode.HALF_EVEN,
   181             RoundingMode.HALF_EVEN,
   182             RoundingMode.HALF_UP
   182             RoundingMode.HALF_UP
   183         };
   183         };
   184 
   184 
   185         // Precise the relative position of input value against its closest tie.
   185         // Precise the relative position of input value against its closest tie.
       
   186         // The double values tested below for 3 and 5 fractional digits must follow
       
   187         // this scheme (position toward tie).
   186         String[] tieRelativePositions = {
   188         String[] tieRelativePositions = {
   187             "below", "exact", "above",
   189             "below", "exact", "above",
   188             "below", "exact", "above",
   190             "below", "exact", "above",
   189             "below", "exact", "above",
   191             "below", "exact", "above",
       
   192             "below", "above", "above",
       
   193             "below", "below", "above",
   190             "below", "exact", "above"
   194             "below", "exact", "above"
   191         };
   195         };
   192 
   196 
   193         // =============== Testing double (and thus float) value cases =========
   197         // =============== Testing double (and thus float) value cases =========
   194 
   198 
   195         System.out.println("\n===== testing 3 digits rounding position =====");
   199         System.out.println("\n===== testing 3 digits rounding position =====");
   196         double[] values3FractDigits = {
   200         double[] values3FractDigits = {
   197             // unimpacting values close to tie, with less than 3 input fract digits
   201             // unimpacting values close to tie, with less than 3 input fract digits
   198             1.115d, 1.125d, 1.135d,
   202             1.115d, 1.125d, 1.135d,
   199             // impacting close to tie values covering all 6 cases
   203             // HALF_* impacting close to tie values covering all 6 tie cases
   200             0.3115d, 0.3125d, 0.3135d,
   204             0.3115d, 0.3125d, 0.3135d,
   201             0.6865d, 0.6875d, 0.6885d,
   205             0.6865d, 0.6875d, 0.6885d,
       
   206             // specific HALF_UP close to tie values
       
   207             0.3124d, 0.3126d, 0.3128d,
       
   208             // specific HALF_DOWN close to tie values
       
   209             0.6864d, 0.6865d, 0.6868d,
   202             // unimpacting values close to tie, with more than 3 input fract digits
   210             // unimpacting values close to tie, with more than 3 input fract digits
   203             1.46885d, 2.46875d, 1.46865d
   211             1.46885d, 2.46875d, 1.46865d
   204         };
   212         };
   205 
   213 
   206         String[] inputs3FractDigits = {
   214         String[] inputs3FractDigits = {
   207             "1.115d", "1.125d", "1.135d",
   215             "1.115d", "1.125d", "1.135d",
   208             "0.3115d", "0.3125d", "0.3135d",
   216             "0.3115d", "0.3125d", "0.3135d",
   209             "0.6865d", "0.6875d", "0.6885d",
   217             "0.6865d", "0.6875d", "0.6885d",
       
   218             "0.3124d", "0.3126d", "0.3128d",
       
   219             "0.6864d", "0.6865d", "0.6868d",
   210             "1.46885d", "2.46875d", "1.46865d"
   220             "1.46885d", "2.46875d", "1.46865d"
   211         };
   221         };
   212 
   222 
   213         String[][] expected3FractDigits = {
   223         String[][] expected3FractDigits = {
   214             {"1.115", "1.125", "1.135",
   224             {"1.115", "1.125", "1.135",
   215              "0.311", "0.312", "0.314",
   225              "0.311", "0.312", "0.314",
   216              "0.686", "0.687", "0.689",
   226              "0.686", "0.687", "0.689",
       
   227              "0.312", "0.313", "0.313",
       
   228              "0.686", "0.686", "0.687",
   217              "1.469", "2.469", "1.469"
   229              "1.469", "2.469", "1.469"
   218             },
   230             },
   219             {"1.115", "1.125", "1.135",
   231             {"1.115", "1.125", "1.135",
   220              "0.311", "0.312", "0.314",
   232              "0.311", "0.312", "0.314",
   221              "0.686", "0.688", "0.689",
   233              "0.686", "0.688", "0.689",
       
   234              "0.312", "0.313", "0.313",
       
   235              "0.686", "0.686", "0.687",
   222              "1.469", "2.469", "1.469"
   236              "1.469", "2.469", "1.469"
   223             },
   237             },
   224             {"1.115", "1.125", "1.135",
   238             {"1.115", "1.125", "1.135",
   225              "0.311", "0.313", "0.314",
   239              "0.311", "0.313", "0.314",
   226              "0.686", "0.688", "0.689",
   240              "0.686", "0.688", "0.689",
       
   241              "0.312", "0.313", "0.313",
       
   242              "0.686", "0.686", "0.687",
   227              "1.469", "2.469", "1.469"
   243              "1.469", "2.469", "1.469"
   228             },
   244             },
   229         };
   245         };
   230 
   246 
   231 
   247 
   248 
   264 
   249         System.out.println("\n===== testing 5 digits rounding position =====");
   265         System.out.println("\n===== testing 5 digits rounding position =====");
   250         double[] values5FractDigits = {
   266         double[] values5FractDigits = {
   251             // unimpacting values close to tie, with less than 5 input fract digits
   267             // unimpacting values close to tie, with less than 5 input fract digits
   252             1.3135d, 1.3125d, 1.3115d,
   268             1.3135d, 1.3125d, 1.3115d,
   253             // impacting values close to tie, covering all 6 cases
   269             // HALF_* impacting values close to tie, covering all 6 cases
   254             1.328115d, 1.328125d, 1.328135d,
   270             1.328115d, 1.328125d, 1.328135d,
   255             1.796865d, 1.796875d, 1.796885d,
   271             1.796865d, 1.796875d, 1.796885d,
       
   272             // specific HALF_UP close to tie values
       
   273             1.328124d, 1.798876d, 1.796889d,
       
   274             // specific HALF_DOWN close to tie values
       
   275             1.328114d, 1.796865d, 1.328138d,
   256             // unimpacting values close to tie, with more than 5 input fract digits
   276             // unimpacting values close to tie, with more than 5 input fract digits
   257             1.3281149999999d, 1.75390625d, 1.7968750000001d
   277             1.3281149999999d, 1.75390625d, 1.7968750000001d
   258         };
   278         };
   259 
   279 
   260         String[] inputs5FractDigits = {
   280         String[] inputs5FractDigits = {
   261             "1.3135d", "1.3125d", "1.3115d",
   281             "1.3135d", "1.3125d", "1.3115d",
   262             "1.328115d", "1.328125d", "1.328135d",
   282             "1.328115d", "1.328125d", "1.328135d",
   263             "1.796865d", "1.796875d", "1.796885d",
   283             "1.796865d", "1.796875d", "1.796885d",
       
   284             "1.328124d", "1.798876d", "1.796889d",
       
   285             "1.328114d", "1.796865d", "1.328138d",
   264             "1.3281149999999d", "1.75390625d", "1.7968750000001d"
   286             "1.3281149999999d", "1.75390625d", "1.7968750000001d"
   265         };
   287         };
   266 
   288 
   267         String[][] expected5FractDigits = {
   289         String[][] expected5FractDigits = {
   268             {"1.3135", "1.3125", "1.3115",
   290             {"1.3135", "1.3125", "1.3115",
   269              "1.32811", "1.32812", "1.32814",
   291              "1.32811", "1.32812", "1.32814",
   270              "1.79686", "1.79687", "1.79689",
   292              "1.79686", "1.79687", "1.79689",
       
   293              "1.32812", "1.79888", "1.79689",
       
   294              "1.32811", "1.79686", "1.32814",
   271              "1.32811", "1.75391", "1.79688"
   295              "1.32811", "1.75391", "1.79688"
   272             },
   296             },
   273             {"1.3135", "1.3125", "1.3115",
   297             {"1.3135", "1.3125", "1.3115",
   274              "1.32811", "1.32812", "1.32814",
   298              "1.32811", "1.32812", "1.32814",
   275              "1.79686", "1.79688", "1.79689",
   299              "1.79686", "1.79688", "1.79689",
       
   300              "1.32812", "1.79888", "1.79689",
       
   301              "1.32811", "1.79686", "1.32814",
   276              "1.32811", "1.75391", "1.79688"
   302              "1.32811", "1.75391", "1.79688"
   277             },
   303             },
   278             {"1.3135", "1.3125", "1.3115",
   304             {"1.3135", "1.3125", "1.3115",
   279              "1.32811", "1.32813", "1.32814",
   305              "1.32811", "1.32813", "1.32814",
   280              "1.79686", "1.79688", "1.79689",
   306              "1.79686", "1.79688", "1.79689",
       
   307              "1.32812", "1.79888", "1.79689",
       
   308              "1.32811", "1.79686", "1.32814",
   281              "1.32811", "1.75391", "1.79688"
   309              "1.32811", "1.75391", "1.79688"
   282             }
   310             }
   283         };
   311         };
   284 
   312 
   285 
   313