test/java/text/Format/DecimalFormat/TieRoundingTest.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2013, 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  */


 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", "exact", "above"
 191         };
 192 
 193         // =============== Testing double (and thus float) value cases =========
 194 
 195         System.out.println("\n===== testing 3 digits rounding position =====");
 196         double[] values3FractDigits = {
 197             // unimpacting values close to tie, with less than 3 input fract digits
 198             1.115d, 1.125d, 1.135d,
 199             // impacting close to tie values covering all 6 cases
 200             0.3115d, 0.3125d, 0.3135d,
 201             0.6865d, 0.6875d, 0.6885d,




 202             // unimpacting values close to tie, with more than 3 input fract digits
 203             1.46885d, 2.46875d, 1.46865d
 204         };
 205 
 206         String[] inputs3FractDigits = {
 207             "1.115d", "1.125d", "1.135d",
 208             "0.3115d", "0.3125d", "0.3135d",
 209             "0.6865d", "0.6875d", "0.6885d",


 210             "1.46885d", "2.46875d", "1.46865d"
 211         };
 212 
 213         String[][] expected3FractDigits = {
 214             {"1.115", "1.125", "1.135",
 215              "0.311", "0.312", "0.314",
 216              "0.686", "0.687", "0.689",


 217              "1.469", "2.469", "1.469"
 218             },
 219             {"1.115", "1.125", "1.135",
 220              "0.311", "0.312", "0.314",
 221              "0.686", "0.688", "0.689",


 222              "1.469", "2.469", "1.469"
 223             },
 224             {"1.115", "1.125", "1.135",
 225              "0.311", "0.313", "0.314",
 226              "0.686", "0.688", "0.689",


 227              "1.469", "2.469", "1.469"
 228             },
 229         };
 230 
 231 
 232         for (int r = 0; r < roundingModes.length; r++) {
 233             NumberFormat dfDefault = NumberFormat.getInstance(Locale.US);
 234             RoundingMode rmode = roundingModes[r];
 235             dfDefault.setRoundingMode(rmode);
 236             System.out.println("\n----- Now checking " + rmode +
 237                                " rounding mode -----");
 238 
 239             for (int i = 0; i < values3FractDigits.length; i++) {
 240                 double d = values3FractDigits[i];
 241                 String tiePosition = tieRelativePositions[i];
 242                 String input = inputs3FractDigits[i];
 243                 String expected = expected3FractDigits[r][i];
 244 
 245                 formatOutputTestDouble(dfDefault, d, tiePosition, input, expected);
 246             }
 247         }
 248 
 249         System.out.println("\n===== testing 5 digits rounding position =====");
 250         double[] values5FractDigits = {
 251             // unimpacting values close to tie, with less than 5 input fract digits
 252             1.3135d, 1.3125d, 1.3115d,
 253             // impacting values close to tie, covering all 6 cases
 254             1.328115d, 1.328125d, 1.328135d,
 255             1.796865d, 1.796875d, 1.796885d,




 256             // unimpacting values close to tie, with more than 5 input fract digits
 257             1.3281149999999d, 1.75390625d, 1.7968750000001d
 258         };
 259 
 260         String[] inputs5FractDigits = {
 261             "1.3135d", "1.3125d", "1.3115d",
 262             "1.328115d", "1.328125d", "1.328135d",
 263             "1.796865d", "1.796875d", "1.796885d",


 264             "1.3281149999999d", "1.75390625d", "1.7968750000001d"
 265         };
 266 
 267         String[][] expected5FractDigits = {
 268             {"1.3135", "1.3125", "1.3115",
 269              "1.32811", "1.32812", "1.32814",
 270              "1.79686", "1.79687", "1.79689",


 271              "1.32811", "1.75391", "1.79688"
 272             },
 273             {"1.3135", "1.3125", "1.3115",
 274              "1.32811", "1.32812", "1.32814",
 275              "1.79686", "1.79688", "1.79689",


 276              "1.32811", "1.75391", "1.79688"
 277             },
 278             {"1.3135", "1.3125", "1.3115",
 279              "1.32811", "1.32813", "1.32814",
 280              "1.79686", "1.79688", "1.79689",


 281              "1.32811", "1.75391", "1.79688"
 282             }
 283         };
 284 
 285 
 286         for (int r = 0; r < roundingModes.length; r++) {
 287             DecimalFormat df5 = (DecimalFormat) NumberFormat.getInstance(Locale.US);
 288             RoundingMode rmode = roundingModes[r];
 289             df5.setRoundingMode(rmode);
 290             System.out.println("\n----- Now checking " + rmode +
 291                                " rounding mode -----");
 292             df5.applyPattern("#,###.#####");
 293 
 294             for (int i = 0; i < values5FractDigits.length; i++) {
 295                 double d = values5FractDigits[i];
 296                 String tiePosition = tieRelativePositions[i];
 297                 String input = inputs5FractDigits[i];
 298                 String expected = expected5FractDigits[r][i];
 299 
 300                 formatOutputTestDouble(df5, d, tiePosition, input, expected);


   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  */


 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);