Path: blob/master/test/jdk/java/text/Format/DecimalFormat/TieRoundingTest.java
41152 views
/*1* Copyright (c) 2012, 2014, 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*/2223/* @test24*25* @bug 7131459 803991526* @summary test various situations of NumberFormat rounding when close to tie27* @author Olivier Lagneau28* @run main TieRoundingTest29*30*/3132import java.math.BigDecimal;33import java.math.BigInteger;34import java.text.NumberFormat;35import java.text.DecimalFormat;36import java.math.RoundingMode;37import java.util.Locale;3839public class TieRoundingTest {4041static int testCounter = 0;42static int errorCounter = 0;43static boolean allPassed = true;4445static void formatOutputTestDouble(NumberFormat nf,46double doubleToTest,47String tiePosition,48String inputDigits,49String expectedOutput) {5051int mfd = nf.getMaximumFractionDigits();52RoundingMode rm = nf.getRoundingMode();53String result = nf.format(doubleToTest);5455if (!result.equals(expectedOutput)) {56System.out.println();57System.out.println("========================================");58System.out.println("***Failure : error formatting value from string : " +59inputDigits);60System.out.println("NumberFormat pattern is : " +61((DecimalFormat ) nf).toPattern());62System.out.println("Maximum number of fractional digits : " + mfd);63System.out.println("Fractional rounding digit : " + (mfd + 1));64System.out.println("Position of value relative to tie : " + tiePosition);65System.out.println("Rounding Mode : " + rm);66System.out.println("BigDecimal output : " +67new BigDecimal(doubleToTest).toString());68System.out.println("FloatingDecimal output : " + doubleToTest);69System.out.println(70"Error. Formatted result different from expected." +71"\nExpected output is : \"" + expectedOutput + "\"" +72"\nFormated output is : \"" + result + "\"");73System.out.println("========================================");74System.out.println();7576errorCounter++;77allPassed = false;78} else {79testCounter++;80System.out.println("\nSuccess for double value : " + doubleToTest + " :");81System.out.println(" Input digits :" + inputDigits +82", BigDecimal value : " +83new BigDecimal(doubleToTest).toString());84System.out.print(" Rounding mode: " + rm);85System.out.print(", fract digits : " + mfd);86System.out.print(", position : " + tiePosition + " tie");87System.out.print(", result : " + result);88System.out.println(", expected : " + expectedOutput);89}90}9192static void formatOutputTestLong(NumberFormat nf,93long longToTest,94String tiePosition,95String inputDigits,96String expectedOutput) {9798int mfd = nf.getMaximumFractionDigits();99RoundingMode rm = nf.getRoundingMode();100String result = nf.format(longToTest);101102if (!result.equals(expectedOutput)) {103System.out.println();104System.out.println("========================================");105System.out.println("***Failure : error formatting value from string : " +106inputDigits);107System.out.println("NumberFormat pattern is : " +108((DecimalFormat ) nf).toPattern());109System.out.println("Maximum number of fractional digits : " + mfd);110System.out.println("Fractional rounding digit : " + (mfd + 1));111System.out.println("Position of value relative to tie : " + tiePosition);112System.out.println("Rounding Mode : " + rm);113System.out.println(114"Error. Formatted result different from expected." +115"\nExpected output is : \"" + expectedOutput + "\"" +116"\nFormated output is : \"" + result + "\"");117System.out.println("========================================");118System.out.println();119120errorCounter++;121allPassed = false;122} else {123testCounter++;124System.out.print("Success. Long input :" + inputDigits);125System.out.print(", rounding : " + rm);126System.out.print(", fract digits : " + mfd);127System.out.print(", tie position : " + tiePosition);128System.out.println(", expected : " + expectedOutput);129130}131}132133static void formatOutputTestObject(NumberFormat nf,134Object someNumber,135String tiePosition,136String inputDigits,137String expectedOutput) {138139int mfd = nf.getMaximumFractionDigits();140RoundingMode rm = nf.getRoundingMode();141String result = nf.format(someNumber);142143if (!result.equals(expectedOutput)) {144System.out.println();145System.out.println("========================================");146System.out.println("***Failure : error formatting value from string : " +147inputDigits);148System.out.println("NumberFormat pattern is : " +149((DecimalFormat ) nf).toPattern());150System.out.println("Maximum number of fractional digits : " + mfd);151System.out.println("Fractional rounding digit : " + (mfd + 1));152System.out.println("Position of value relative to tie : " + tiePosition);153System.out.println("Rounding Mode : " + rm);154System.out.println("Number self output representation: " + someNumber);155System.out.println(156"Error. Formatted result different from expected." +157"\nExpected output is : \"" + expectedOutput + "\"" +158"\nFormated output is : \"" + result + "\"");159System.out.println("========================================");160System.out.println();161162errorCounter++;163allPassed = false;164} else {165testCounter++;166System.out.print("Success. Number input :" + inputDigits);167System.out.print(", rounding : " + rm);168System.out.print(", fract digits : " + mfd);169System.out.print(", tie position : " + tiePosition);170System.out.println(", expected : " + expectedOutput);171}172}173174public static void main(String[] args) {175176// The 3 HALF_* rounding modes are impacted by bugs 7131459, 8039915.177// So we do not test the other rounding modes.178RoundingMode[] roundingModes = {179RoundingMode.HALF_DOWN,180RoundingMode.HALF_EVEN,181RoundingMode.HALF_UP182};183184// Precise the relative position of input value against its closest tie.185// The double values tested below for 3 and 5 fractional digits must follow186// this scheme (position toward tie).187String[] tieRelativePositions = {188"below", "exact", "above",189"below", "exact", "above",190"below", "exact", "above",191"below", "above", "above",192"below", "below", "above",193"below", "exact", "above"194};195196// =============== Testing double (and thus float) value cases =========197198System.out.println("\n===== testing 3 digits rounding position =====");199double[] values3FractDigits = {200// unimpacting values close to tie, with less than 3 input fract digits2011.115d, 1.125d, 1.135d,202// HALF_* impacting close to tie values covering all 6 tie cases2030.3115d, 0.3125d, 0.3135d,2040.6865d, 0.6875d, 0.6885d,205// specific HALF_UP close to tie values2060.3124d, 0.3126d, 0.3128d,207// specific HALF_DOWN close to tie values2080.6864d, 0.6865d, 0.6868d,209// unimpacting values close to tie, with more than 3 input fract digits2101.46885d, 2.46875d, 1.46865d211};212213String[] inputs3FractDigits = {214"1.115d", "1.125d", "1.135d",215"0.3115d", "0.3125d", "0.3135d",216"0.6865d", "0.6875d", "0.6885d",217"0.3124d", "0.3126d", "0.3128d",218"0.6864d", "0.6865d", "0.6868d",219"1.46885d", "2.46875d", "1.46865d"220};221222String[][] expected3FractDigits = {223{"1.115", "1.125", "1.135",224"0.311", "0.312", "0.314",225"0.686", "0.687", "0.689",226"0.312", "0.313", "0.313",227"0.686", "0.686", "0.687",228"1.469", "2.469", "1.469"229},230{"1.115", "1.125", "1.135",231"0.311", "0.312", "0.314",232"0.686", "0.688", "0.689",233"0.312", "0.313", "0.313",234"0.686", "0.686", "0.687",235"1.469", "2.469", "1.469"236},237{"1.115", "1.125", "1.135",238"0.311", "0.313", "0.314",239"0.686", "0.688", "0.689",240"0.312", "0.313", "0.313",241"0.686", "0.686", "0.687",242"1.469", "2.469", "1.469"243},244};245246247for (int r = 0; r < roundingModes.length; r++) {248NumberFormat dfDefault = NumberFormat.getInstance(Locale.US);249RoundingMode rmode = roundingModes[r];250dfDefault.setRoundingMode(rmode);251System.out.println("\n----- Now checking " + rmode +252" rounding mode -----");253254for (int i = 0; i < values3FractDigits.length; i++) {255double d = values3FractDigits[i];256String tiePosition = tieRelativePositions[i];257String input = inputs3FractDigits[i];258String expected = expected3FractDigits[r][i];259260formatOutputTestDouble(dfDefault, d, tiePosition, input, expected);261}262}263264System.out.println("\n===== testing 5 digits rounding position =====");265double[] values5FractDigits = {266// unimpacting values close to tie, with less than 5 input fract digits2671.3135d, 1.3125d, 1.3115d,268// HALF_* impacting values close to tie, covering all 6 cases2691.328115d, 1.328125d, 1.328135d,2701.796865d, 1.796875d, 1.796885d,271// specific HALF_UP close to tie values2721.328124d, 1.798876d, 1.796889d,273// specific HALF_DOWN close to tie values2741.328114d, 1.796865d, 1.328138d,275// unimpacting values close to tie, with more than 5 input fract digits2761.3281149999999d, 1.75390625d, 1.7968750000001d277};278279String[] inputs5FractDigits = {280"1.3135d", "1.3125d", "1.3115d",281"1.328115d", "1.328125d", "1.328135d",282"1.796865d", "1.796875d", "1.796885d",283"1.328124d", "1.798876d", "1.796889d",284"1.328114d", "1.796865d", "1.328138d",285"1.3281149999999d", "1.75390625d", "1.7968750000001d"286};287288String[][] expected5FractDigits = {289{"1.3135", "1.3125", "1.3115",290"1.32811", "1.32812", "1.32814",291"1.79686", "1.79687", "1.79689",292"1.32812", "1.79888", "1.79689",293"1.32811", "1.79686", "1.32814",294"1.32811", "1.75391", "1.79688"295},296{"1.3135", "1.3125", "1.3115",297"1.32811", "1.32812", "1.32814",298"1.79686", "1.79688", "1.79689",299"1.32812", "1.79888", "1.79689",300"1.32811", "1.79686", "1.32814",301"1.32811", "1.75391", "1.79688"302},303{"1.3135", "1.3125", "1.3115",304"1.32811", "1.32813", "1.32814",305"1.79686", "1.79688", "1.79689",306"1.32812", "1.79888", "1.79689",307"1.32811", "1.79686", "1.32814",308"1.32811", "1.75391", "1.79688"309}310};311312313for (int r = 0; r < roundingModes.length; r++) {314DecimalFormat df5 = (DecimalFormat) NumberFormat.getInstance(Locale.US);315RoundingMode rmode = roundingModes[r];316df5.setRoundingMode(rmode);317System.out.println("\n----- Now checking " + rmode +318" rounding mode -----");319df5.applyPattern("#,###.#####");320321for (int i = 0; i < values5FractDigits.length; i++) {322double d = values5FractDigits[i];323String tiePosition = tieRelativePositions[i];324String input = inputs5FractDigits[i];325String expected = expected5FractDigits[r][i];326327formatOutputTestDouble(df5, d, tiePosition, input, expected);328}329}330331// ==================== Testing long value cases ====================332333System.out.println("\n===== testing long values =====");334long l = 123456789012345L;335DecimalFormat dfLong = (DecimalFormat) NumberFormat.getInstance(Locale.US);336String tiePosition = "exact";337String input = "123456789012345L";338String expected = "123,456,789,012,345";339String result = dfLong.format(l);340formatOutputTestLong(dfLong, l, tiePosition, input, expected);341342dfLong.applyPattern("0.###E0");343expected = "1.235E14";344formatOutputTestLong(dfLong, l, tiePosition, input, expected);345346l = 123450000000000L;347input = "123450000000000L";348expected = "1.234E14";349formatOutputTestLong(dfLong, l, tiePosition, input, expected);350351l = 987750000000000L;352input = "987750000000000L";353expected = "9.878E14";354formatOutputTestLong(dfLong, l, tiePosition, input, expected);355356dfLong.applyPattern("#,###.0E0");357l = 987755000000000L;358input = "987755000000000L";359expected = "987.76E12";360361formatOutputTestLong(dfLong, l, tiePosition, input, expected);362363364// ================= Testing BigInteger value cases =================365366System.out.println("\n===== testing BigInteger values =====");367String stringValue = "12345678901234567890123456789012345";368BigInteger bi = new BigInteger(stringValue);369DecimalFormat dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);370tiePosition = "exact";371input = stringValue;372expected = "12,345,678,901,234,567,890,123,456,789,012,345";373formatOutputTestObject(dfBig, bi, tiePosition, input, expected);374375dfBig.applyPattern("0.###E0");376expected = "1.235E34";377formatOutputTestObject(dfBig, bi, tiePosition, input, expected);378379stringValue = "12345000000000000000000000000000000";380input = stringValue;381bi = new BigInteger(stringValue);382expected = "1.234E34";383formatOutputTestObject(dfBig, bi, tiePosition, input, expected);384385stringValue = "12345000000000000000000000000000001";386input = stringValue;387bi = new BigInteger(stringValue);388expected = "1.235E34";389formatOutputTestObject(dfBig, bi, tiePosition, input, expected);390391stringValue = "98755000000000000000000000000000000";392input = stringValue;393bi = new BigInteger(stringValue);394expected = "9.876E34";395formatOutputTestObject(dfBig, bi, tiePosition, input, expected);396397dfLong.applyPattern("#,###.0E0");398stringValue = "98775500000000000000000000000000000";399input = stringValue;400expected = "987.76E34";401402// =============== Testing BigDecimal value cases ================403404System.out.println("\n===== testing BigDecimal values =====");405dfBig = (DecimalFormat) NumberFormat.getInstance(Locale.US);406407stringValue = "0.68850000000000000088817841970012523233890533447265625";408BigDecimal bd = new BigDecimal(stringValue);409tiePosition = "exact";410input = stringValue;411expected = "0.689";412formatOutputTestObject(dfBig, bd, tiePosition, input, expected);413414stringValue = "0.31149999999999999911182158029987476766109466552734375";415bd = new BigDecimal(stringValue);416dfBig.applyPattern("#,##0.####");417tiePosition = "exact";418input = stringValue;419expected = "0.3115";420formatOutputTestObject(dfBig, bd, tiePosition, input, expected);421422// ==================== Printing results and exiting ===================423424System.out.println();425System.out.println("==> " + testCounter + " tests passed successfully");426System.out.println("==> " + errorCounter + " tests failed");427428System.out.println();429if (allPassed) {430System.out.println(431"Success in formating all the values with the given parameters");432} else {433String s = "Test failed with " + errorCounter + " formating error(s).";434System.out.println(s);435throw new RuntimeException(s);436}437}438}439440441