1 /*
   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.
   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 /* @test
  25  *
  26  * @bug 7131459
  27  * @summary test various situations of NumberFormat rounding when close to tie
  28  * @author Olivier Lagneau
  29  * @run main TieRoundingTest
  30  *
  31  */
  32 
  33 import java.math.BigDecimal;
  34 import java.math.BigInteger;
  35 import java.text.NumberFormat;
  36 import java.text.DecimalFormat;
  37 import java.math.RoundingMode;
  38 import java.util.Locale;
  39 
  40 public class TieRoundingTest {
  41 
  42     static int testCounter = 0;
  43     static int errorCounter = 0;
  44     static boolean allPassed = true;
  45 
  46     static void formatOutputTestDouble(NumberFormat nf,
  47                                        double doubleToTest,
  48                                        String tiePosition,
  49                                        String inputDigits,
  50                                        String expectedOutput) {
  51 
  52         int mfd = nf.getMaximumFractionDigits();
  53         RoundingMode rm = nf.getRoundingMode();
  54         String result = nf.format(doubleToTest);
  55 
  56         if (!result.equals(expectedOutput)) {
  57             System.out.println();
  58             System.out.println("========================================");
  59             System.out.println("***Error formatting double value from string : " +
  60                                inputDigits);
  61             System.out.println("NumberFormat pattern is  : " +
  62                                ((DecimalFormat ) nf).toPattern());
  63             System.out.println("Maximum number of fractional digits : " + mfd);
  64             System.out.println("Fractional rounding digit : " + (mfd + 1));
  65             System.out.println("Position of value relative to tie : " + tiePosition);
  66             System.out.println("Rounding Mode : " + rm);
  67             System.out.println("BigDecimal output : " +
  68                                new BigDecimal(doubleToTest).toString());
  69             System.out.println("FloatingDecimal output : " + doubleToTest);
  70             System.out.println(
  71                "Error. Formatted result different from expected." +
  72                "\nExpected output is : \"" + expectedOutput + "\"" +
  73                "\nFormated output is : \"" + result + "\"");
  74             System.out.println("========================================");
  75             System.out.println();
  76 
  77             errorCounter++;
  78             allPassed = false;
  79         } else {
  80             testCounter++;
  81             System.out.println("\nSuccess for double value : " + doubleToTest + " :");
  82             System.out.println(" Input digits :" + inputDigits +
  83                                ", BigDecimal value : " +
  84                                new BigDecimal(doubleToTest).toString());
  85             System.out.print(" Rounding mode: " + rm);
  86             System.out.print(", fract digits : " + mfd);
  87             System.out.print(", position : " + tiePosition + " tie");
  88             System.out.print(", result : " + result);
  89             System.out.println(", expected : " + expectedOutput);
  90         }
  91     }
  92 
  93     static void formatOutputTestLong(NumberFormat nf,
  94                                      long longToTest,
  95                                      String tiePosition,
  96                                      String inputDigits,
  97                                      String expectedOutput) {
  98 
  99         int mfd = nf.getMaximumFractionDigits();
 100         RoundingMode rm = nf.getRoundingMode();
 101         String result = nf.format(longToTest);
 102 
 103         if (!result.equals(expectedOutput)) {
 104             System.out.println();
 105             System.out.println("========================================");
 106             System.out.println("***Error formatting double value from string : " +
 107                                inputDigits);
 108             System.out.println("NumberFormat pattern is  : " +
 109                                ((DecimalFormat ) nf).toPattern());
 110             System.out.println("Maximum number of fractional digits : " + mfd);
 111             System.out.println("Fractional rounding digit : " + (mfd + 1));
 112             System.out.println("Position of value relative to tie : " + tiePosition);
 113             System.out.println("Rounding Mode : " + rm);
 114             System.out.println(
 115                "Error. Formatted result different from expected." +
 116                "\nExpected output is : \"" + expectedOutput + "\"" +
 117                "\nFormated output is : \"" + result + "\"");
 118             System.out.println("========================================");
 119             System.out.println();
 120 
 121             errorCounter++;
 122             allPassed = false;
 123         } else {
 124             testCounter++;
 125             System.out.print("Success. Long input :" + inputDigits);
 126             System.out.print(", rounding : " + rm);
 127             System.out.print(", fract digits : " + mfd);
 128             System.out.print(", tie position : " + tiePosition);
 129             System.out.println(", expected : " + expectedOutput);
 130 
 131         }
 132     }
 133 
 134     static void formatOutputTestObject(NumberFormat nf,
 135                                        Object someNumber,
 136                                        String tiePosition,
 137                                        String inputDigits,
 138                                        String expectedOutput) {
 139 
 140         int mfd = nf.getMaximumFractionDigits();
 141         RoundingMode rm = nf.getRoundingMode();
 142         String result = nf.format(someNumber);
 143 
 144         if (!result.equals(expectedOutput)) {
 145             System.out.println();
 146             System.out.println("========================================");
 147             System.out.println("***Error formatting number value from string : " +
 148                                inputDigits);
 149             System.out.println("NumberFormat pattern is  : " +
 150                                ((DecimalFormat ) nf).toPattern());
 151             System.out.println("Maximum number of fractional digits : " + mfd);
 152             System.out.println("Fractional rounding digit : " + (mfd + 1));
 153             System.out.println("Position of value relative to tie : " + tiePosition);
 154             System.out.println("Rounding Mode : " + rm);
 155             System.out.println("Number self output representation: " + someNumber);
 156             System.out.println(
 157                "Error. Formatted result different from expected." +
 158                "\nExpected output is : \"" + expectedOutput + "\"" +
 159                "\nFormated output is : \"" + result + "\"");
 160             System.out.println("========================================");
 161             System.out.println();
 162 
 163             errorCounter++;
 164             allPassed = false;
 165         } else {
 166             testCounter++;
 167             System.out.print("Success. Number input :" + inputDigits);
 168             System.out.print(", rounding : " + rm);
 169             System.out.print(", fract digits : " + mfd);
 170             System.out.print(", tie position : " + tiePosition);
 171             System.out.println(", expected : " + expectedOutput);
 172         }
 173     }
 174 
 175     public static void main(String[] args) {
 176 
 177         // Only the 3 rounding modes below may be impacted by bug 7131459.
 178         // So we do not test the other rounding modes.
 179         RoundingMode[] roundingModes = {
 180             RoundingMode.HALF_DOWN,
 181             RoundingMode.HALF_EVEN,
 182             RoundingMode.HALF_UP
 183         };
 184 
 185         // Precise the relative position of input value against its closest tie.
 186         String[] tieRelativePositions = {
 187             "below", "exact", "above",
 188             "below", "exact", "above",
 189             "below", "exact", "above",
 190             "below", "above", "above",
 191             "below", "below", "above",
 192             "below", "exact", "above"
 193         };
 194 
 195         // =============== Testing double (and thus float) value cases =========
 196 
 197         System.out.println("\n===== testing 3 digits rounding position =====");
 198         double[] values3FractDigits = {
 199             // unimpacting values close to tie, with less than 3 input fract digits
 200             1.115d, 1.125d, 1.135d,
 201             // HALF_* impacting close to tie values covering all 6 tie cases
 202             0.3115d, 0.3125d, 0.3135d,
 203             0.6865d, 0.6875d, 0.6885d,
 204             // specific HALF_UP close to tie values
 205             0.3124d, 0.3126d, 0.3128d,
 206             // specific HALF_DOWN close to tie values
 207             0.6864d, 0.6865d, 0.6868d,
 208             // unimpacting values close to tie, with more than 3 input fract digits
 209             1.46885d, 2.46875d, 1.46865d
 210         };
 211 
 212         String[] inputs3FractDigits = {
 213             "1.115d", "1.125d", "1.135d",
 214             "0.3115d", "0.3125d", "0.3135d",
 215             "0.6865d", "0.6875d", "0.6885d",
 216             "0.3124d", "0.3126d", "0.3128d",
 217             "0.6864d", "0.6865d", "0.6868d",
 218             "1.46885d", "2.46875d", "1.46865d"
 219         };
 220 
 221         String[][] expected3FractDigits = {
 222             {"1.115", "1.125", "1.135",
 223              "0.311", "0.312", "0.314",
 224              "0.686", "0.687", "0.689",
 225              "0.312", "0.313", "0.313",
 226              "0.686", "0.686", "0.687",
 227              "1.469", "2.469", "1.469"
 228             },
 229             {"1.115", "1.125", "1.135",
 230              "0.311", "0.312", "0.314",
 231              "0.686", "0.688", "0.689",
 232              "0.312", "0.313", "0.313",
 233              "0.686", "0.686", "0.687",
 234              "1.469", "2.469", "1.469"
 235             },
 236             {"1.115", "1.125", "1.135",
 237              "0.311", "0.313", "0.314",
 238              "0.686", "0.688", "0.689",
 239              "0.312", "0.313", "0.313",
 240              "0.686", "0.686", "0.687",
 241              "1.469", "2.469", "1.469"
 242             },
 243         };
 244 
 245 
 246         for (int r = 0; r < roundingModes.length; r++) {
 247             NumberFormat dfDefault = NumberFormat.getInstance(Locale.US);
 248             RoundingMode rmode = roundingModes[r];
 249             dfDefault.setRoundingMode(rmode);
 250             System.out.println("\n----- Now checking " + rmode +
 251                                " rounding mode -----");
 252 
 253             for (int i = 0; i < values3FractDigits.length; i++) {
 254                 double d = values3FractDigits[i];
 255                 String tiePosition = tieRelativePositions[i];
 256                 String input = inputs3FractDigits[i];
 257                 String expected = expected3FractDigits[r][i];
 258 
 259                 formatOutputTestDouble(dfDefault, d, tiePosition, input, expected);
 260             }
 261         }
 262 
 263         System.out.println("\n===== testing 5 digits rounding position =====");
 264         double[] values5FractDigits = {
 265             // unimpacting values close to tie, with less than 5 input fract digits
 266             1.3135d, 1.3125d, 1.3115d,
 267             // HALF_* impacting values close to tie, covering all 6 cases
 268             1.328115d, 1.328125d, 1.328135d,
 269             1.796865d, 1.796875d, 1.796885d,
 270             // specific HALF_UP close to tie values
 271             1.328124d, 1.798876d, 1.796889d,
 272             // specific HALF_DOWN close to tie values
 273             1.328114d, 1.796865d, 1.328138d,
 274             // unimpacting values close to tie, with more than 5 input fract digits
 275             1.3281149999999d, 1.75390625d, 1.7968750000001d
 276         };
 277 
 278         String[] inputs5FractDigits = {
 279             "1.3135d", "1.3125d", "1.3115d",
 280             "1.328115d", "1.328125d", "1.328135d",
 281             "1.796865d", "1.796875d", "1.796885d",
 282             "1.328124d", "1.798876d", "1.796889d",
 283             "1.328114d", "1.796865d", "1.328138d",
 284             "1.3281149999999d", "1.75390625d", "1.7968750000001d"
 285         };
 286 
 287         String[][] expected5FractDigits = {
 288             {"1.3135", "1.3125", "1.3115",
 289              "1.32811", "1.32812", "1.32814",
 290              "1.79686", "1.79687", "1.79689",
 291              "1.32812", "1.79888", "1.79689",
 292              "1.32811", "1.79686", "1.32814",
 293              "1.32811", "1.75391", "1.79688"
 294             },
 295             {"1.3135", "1.3125", "1.3115",
 296              "1.32811", "1.32812", "1.32814",
 297              "1.79686", "1.79688", "1.79689",
 298              "1.32812", "1.79888", "1.79689",
 299              "1.32811", "1.79686", "1.32814",
 300              "1.32811", "1.75391", "1.79688"
 301             },
 302             {"1.3135", "1.3125", "1.3115",
 303              "1.32811", "1.32813", "1.32814",
 304              "1.79686", "1.79688", "1.79689",
 305              "1.32812", "1.79888", "1.79689",
 306              "1.32811", "1.79686", "1.32814",
 307              "1.32811", "1.75391", "1.79688"
 308             }
 309         };
 310 
 311 
 312         for (int r = 0; r < roundingModes.length; r++) {
 313             DecimalFormat df5 = (DecimalFormat) NumberFormat.getInstance(Locale.US);
 314             RoundingMode rmode = roundingModes[r];
 315             df5.setRoundingMode(rmode);
 316             System.out.println("\n----- Now checking " + rmode +
 317                                " rounding mode -----");
 318             df5.applyPattern("#,###.#####");
 319 
 320             for (int i = 0; i < values5FractDigits.length; i++) {
 321                 double d = values5FractDigits[i];
 322                 String tiePosition = tieRelativePositions[i];
 323                 String input = inputs5FractDigits[i];
 324                 String expected = expected5FractDigits[r][i];
 325 
 326                 formatOutputTestDouble(df5, d, tiePosition, input, expected);
 327             }
 328         }
 329 
 330         // ==================== Testing long value cases ====================
 331 
 332         System.out.println("\n===== testing long values =====");
 333         long l = 123456789012345L;
 334         DecimalFormat dfLong = (DecimalFormat) NumberFormat.getInstance(Locale.US);
 335         String tiePosition = "exact";
 336         String input = "123456789012345L";
 337         String expected = "123,456,789,012,345";
 338         String result = dfLong.format(l);
 339         formatOutputTestLong(dfLong, l, tiePosition, input, expected);
 340 
 341         dfLong.applyPattern("0.###E0");
 342         expected = "1.235E14";
 343         formatOutputTestLong(dfLong, l, tiePosition, input, expected);
 344 
 345         l = 123450000000000L;
 346         input = "123450000000000L";
 347         expected = "1.234E14";
 348         formatOutputTestLong(dfLong, l, tiePosition, input, expected);
 349 
 350         l = 987750000000000L;
 351         input = "987750000000000L";
 352         expected = "9.878E14";
 353         formatOutputTestLong(dfLong, l, tiePosition, input, expected);
 354 
 355         dfLong.applyPattern("#,###.0E0");
 356         l = 987755000000000L;
 357         input = "987755000000000L";
 358         expected = "987.76E12";
 359 
 360         formatOutputTestLong(dfLong, l, tiePosition, input, expected);
 361 
 362 
 363         // ================= Testing BigInteger value cases =================
 364 
 365         System.out.println("\n===== testing BigInteger values =====");
 366         String stringValue = "12345678901234567890123456789012345";
 367         BigInteger bi = new BigInteger(stringValue);
 368         DecimalFormat dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
 369         tiePosition = "exact";
 370         input = stringValue;
 371         expected = "12,345,678,901,234,567,890,123,456,789,012,345";
 372         formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
 373 
 374         dfBig.applyPattern("0.###E0");
 375         expected = "1.235E34";
 376         formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
 377 
 378         stringValue = "12345000000000000000000000000000000";
 379         input = stringValue;
 380         bi = new BigInteger(stringValue);
 381         expected = "1.234E34";
 382         formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
 383 
 384         stringValue = "12345000000000000000000000000000001";
 385         input = stringValue;
 386         bi = new BigInteger(stringValue);
 387         expected = "1.235E34";
 388         formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
 389 
 390         stringValue = "98755000000000000000000000000000000";
 391         input = stringValue;
 392         bi = new BigInteger(stringValue);
 393         expected = "9.876E34";
 394         formatOutputTestObject(dfBig, bi, tiePosition, input, expected);
 395 
 396         dfLong.applyPattern("#,###.0E0");
 397         stringValue = "98775500000000000000000000000000000";
 398         input = stringValue;
 399         expected = "987.76E34";
 400 
 401         // =============== Testing BigDecimal value cases ================
 402 
 403         System.out.println("\n===== testing BigDecimal values =====");
 404         dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);
 405 
 406         stringValue = "0.68850000000000000088817841970012523233890533447265625";
 407         BigDecimal bd = new BigDecimal(stringValue);
 408         tiePosition = "exact";
 409         input = stringValue;
 410         expected = "0.689";
 411         formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
 412 
 413         stringValue = "0.31149999999999999911182158029987476766109466552734375";
 414         bd = new BigDecimal(stringValue);
 415         dfBig.applyPattern("#,##0.####");
 416         tiePosition = "exact";
 417         input = stringValue;
 418         expected = "0.3115";
 419         formatOutputTestObject(dfBig, bd, tiePosition, input, expected);
 420 
 421         // ==================== Printing results and exiting ===================
 422 
 423         System.out.println();
 424         System.out.println("==> " + testCounter + " tests passed successfully");
 425         System.out.println("==> " + errorCounter + " tests failed");
 426 
 427         System.out.println();
 428         if (allPassed) {
 429             System.out.println(
 430                 "Success in formating all the values with the given parameters");
 431         } else {
 432             String s = "Test failed with " + errorCounter + " formating error(s).";
 433             System.out.println(s);
 434             throw new RuntimeException(s);
 435         }
 436     }
 437 }