Path: blob/master/test/jdk/java/text/Format/NumberFormat/BigDecimalParse.java
41152 views
/*1* Copyright (c) 2003, 2016, 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/*24* @test25* @bug 4018937 800857726* @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.27* @library /java/text/testlib28* @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalParse29*/3031import java.math.BigDecimal;32import java.text.*;33import java.util.*;3435public class BigDecimalParse extends IntlTest {3637public static void main(String[] args) throws Exception {38Locale loc = Locale.getDefault();39try {40Locale.setDefault(Locale.US);41new BigDecimalParse().run(args);42} finally {43// restore the reserved locale44Locale.setDefault(loc);45}46}4748static final String nonsep_int =49"123456789012345678901234567890123456789012345678901234567890" +50"123456789012345678901234567890123456789012345678901234567890" +51"123456789012345678901234567890123456789012345678901234567890" +52"123456789012345678901234567890123456789012345678901234567890" +53"123456789012345678901234567890123456789012345678901234567890" +54"123456789012345678901234567890123456789012345678901234567890";5556static final String sep_int =57"123,456,789,012,345,678,901,234,567,890," +58"123,456,789,012,345,678,901,234,567,890," +59"123,456,789,012,345,678,901,234,567,890," +60"123,456,789,012,345,678,901,234,567,890," +61"123,456,789,012,345,678,901,234,567,890," +62"123,456,789,012,345,678,901,234,567,890," +63"123,456,789,012,345,678,901,234,567,890," +64"123,456,789,012,345,678,901,234,567,890," +65"123,456,789,012,345,678,901,234,567,890," +66"123,456,789,012,345,678,901,234,567,890," +67"123,456,789,012,345,678,901,234,567,890," +68"123,456,789,012,345,678,901,234,567,890";6970static final String nonsep_zero =71"000000000000000000000000000000000000000000000000000000000000" +72"000000000000000000000000000000000000000000000000000000000000" +73"000000000000000000000000000000000000000000000000000000000000" +74"000000000000000000000000000000000000000000000000000000000000" +75"000000000000000000000000000000000000000000000000000000000000" +76"000000000000000000000000000000000000000000000000000000000000";7778static final String sep_zero =79"000,000,000,000,000,000,000,000,000,000," +80"000,000,000,000,000,000,000,000,000,000," +81"000,000,000,000,000,000,000,000,000,000," +82"000,000,000,000,000,000,000,000,000,000," +83"000,000,000,000,000,000,000,000,000,000," +84"000,000,000,000,000,000,000,000,000,000," +85"000,000,000,000,000,000,000,000,000,000," +86"000,000,000,000,000,000,000,000,000,000," +87"000,000,000,000,000,000,000,000,000,000," +88"000,000,000,000,000,000,000,000,000,000," +89"000,000,000,000,000,000,000,000,000,000," +90"000,000,000,000,000,000,000,000,000,000";9192static final String fra =93"012345678901234567890123456789012345678901234567890123456789" +94"012345678901234567890123456789012345678901234567890123456789" +95"012345678901234567890123456789012345678901234567890123456789" +96"012345678901234567890123456789012345678901234567890123456789" +97"012345678901234567890123456789012345678901234567890123456789" +98"012345678901234567890123456789012345678901234567890123456789";99100101Number parsed = null;102ParsePosition pp;103boolean exceptionOccurred;104String msg;105DecimalFormat df;106107/**108* Test for normal big numbers which have the fraction part109*/110void test_Parse_in_DecimalFormat_BigDecimal() {111df = new DecimalFormat();112df.setParseBigDecimal(true);113114// From: 1234...7890.012...789115// To: BigDecimal 1234...7890.012...789116check(nonsep_int + "." + fra, new BigDecimal(nonsep_int + "." + fra));117118// From: -1,234...7,890.012...789119// To: BigDecimal -1234...7890.012...789120check("-" + sep_int + "." + fra,121new BigDecimal("-" + nonsep_int + "." + fra));122123// From: 000...0000.0...0124// To: BigDecimal 0E-360125check(nonsep_zero + "." + nonsep_zero,126new BigDecimal(nonsep_zero + "." + nonsep_zero));127128// From: 0.000...0000123...789E370129// To: BigDecimal 0.0123...789130check("0.0000000000" + nonsep_zero + fra + "E370",131new BigDecimal("0.0000000000" + nonsep_zero + fra + "E370"));132133// From: 0.1123...890E-360134// To: BigDecimal 1.123...890E-361135check("0.1" + nonsep_int + "E-360",136new BigDecimal("0.1" + nonsep_int + "E-360"));137138// From: 000...0000.0...0123...7890139// To: BigDecimal 1.234...890E-361140check(nonsep_zero + "." + nonsep_zero + nonsep_int,141new BigDecimal(nonsep_zero + "." + nonsep_zero + nonsep_int));142143// From: 0.123...890E360144// To: BigDecimal 123...890145check("0." + nonsep_int + "E360",146new BigDecimal("0." + nonsep_int + "E360"));147}148149/**150* Test for normal big numbers which have the fraction part with multiplier151*/152void test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier() {153df = new DecimalFormat();154df.setParseBigDecimal(true);155156// From: 250,0...0,000.000...000157// To: 1000...0000.000...000158df.setMultiplier(250000000);159check("250,000,000," + sep_zero + "." + nonsep_zero,160new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));161162// From: -250,0...0,000.000...000163// To: -1000...0000.000...000164check("-250,000,000," + sep_zero + "." + nonsep_zero,165new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));166167// From: 250,0...0,000.000...000168// To: -1000...0000.000...000169df.setMultiplier(-250000000);170check("250,000,000," + sep_zero + "." + nonsep_zero,171new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));172173// From: -250,0...0,000.000...000174// To: 1000...0000.000...000175check("-250,000,000," + sep_zero + "." + nonsep_zero,176new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));177178// Confirm that ArithmeticException is handled properly179// From: 1000.000180// To: 333.333181df.setMultiplier(3);182check("1000.000", new BigDecimal("333.333"));183184// Confirm that ArithmeticException is handled properly185// From: 10000.0000186// To: 303.0303187df.setMultiplier(33);188check("10000.0000", new BigDecimal("303.0303"));189}190191/**192* Test for division by zero (BigDecimal)193*/194void test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero() {195df = new DecimalFormat();196df.setParseBigDecimal(true);197df.setMultiplier(0);198199// From: 1000.000200// To: Double.POSITIVE_INFINITY201check("1000.000", Double.POSITIVE_INFINITY);202203// From: -1000204// To: Double.NEGATIVE_INFINITY205check("-1000", Double.NEGATIVE_INFINITY);206207// From: -0.00208// To: Double.NaN209check("-0.00", Double.NaN);210}211212/**213* Test for division by zero (Double)214*/215void test_Parse_in_DecimalFormat_Double_DivisionByZero() {216df = new DecimalFormat();217df.setParseBigDecimal(false);218df.setMultiplier(0);219220// From: 1000.000221// To: Double.POSITIVE_INFINITY222check("1000.000", Double.POSITIVE_INFINITY);223224// From: -1000.000225// To: Double.NEGATIVE_INFINITY226check("-1000.000", Double.NEGATIVE_INFINITY);227228// From: 0.0229// To: Double.NaN230check("0.0", Double.NaN);231232// From: -0.0 (Double)233// To: Double.NaN234check("-0.0", Double.NaN);235236// From: Double.NaN237// To: Double.NaN238check("\ufffd", Double.NaN);239240// From: Double.POSITIVE_INFINITY241// To: Double.NaN242check("\u221e", Double.POSITIVE_INFINITY);243244// From: Double.NEGATIVE_INFINITY245// To: Double.NaN246check("-\u221e", Double.NEGATIVE_INFINITY);247}248249/**250* Test for division by zero (Long)251*/252void test_Parse_in_DecimalFormat_Long_DivisionByZero() {253df = new DecimalFormat();254df.setParseBigDecimal(false);255df.setMultiplier(0);256257// From: 1000258// To: Double.POSITIVE_INFINITY259check("1000", Double.POSITIVE_INFINITY);260261// From: -1000262// To: Double.NEGATIVE_INFINITY263check("-1000", Double.NEGATIVE_INFINITY);264265// From: -000 (Long)266// To: Double.NaN267check("-000", Double.NaN);268}269270/**271* Test for normal big numbers which don't have the fraction part272*/273void test_Parse_in_DecimalFormat_BigInteger() {274df = new DecimalFormat();275df.setParseBigDecimal(true);276277// From: 123...890278// To: BigDecimal 123...890279check(nonsep_int + nonsep_int, new BigDecimal(nonsep_int + nonsep_int));280281// From: 123,4...7,890282// To: BigDecimal 1234...7890283check(sep_int + "," + sep_int, new BigDecimal(nonsep_int + nonsep_int));284285// From: -000...000123...890286// To: BigDecimal -123...890287check("-" + nonsep_zero + nonsep_int, new BigDecimal("-" + nonsep_int));288289// From: -000,0...0,000,123,4...7,890290// To: BigDecimal -123...890291check("-" + sep_zero + "," + sep_int, new BigDecimal("-" + nonsep_int));292}293294/**295* Test for normal big numbers which don't have the fraction part with296* multiplier297*/298void test_Parse_in_DecimalFormat_BigInteger_usingMultiplier() {299df = new DecimalFormat();300df.setParseBigDecimal(true);301302// From: 250,0...0,000303// To: 1000...0000304df.setMultiplier(250000000);305check("250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));306307// From: -250,0...0,000308// To: -1000...0000309check("-250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));310311// From: 250,0...0,000312// To: -1000...0000313df.setMultiplier(-250000000);314check("250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));315316// From: -250,0...0,000317// To: 1000...0000318check("-250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));319320// From: 250,0...0,000E-360321// To: -1000...0000.000...000322check("250,000,000," + sep_zero + "," + sep_zero + "E-360",323new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));324325// Confirm that a division which results in a irrational number is done326// properly327// From: 1000328// To: 333329df.setMultiplier(3);330check("1000", new BigDecimal("333"));331}332333/**334* Test for special numbers335* Double.NaN336* Double.POSITIVE_INFINITY337* Double.NEGATIVE_INFINITY338*/339void test_Parse_in_DecimalFormat_SpecialNumber() {340df = new DecimalFormat();341df.setParseBigDecimal(true);342343String[] numbers = {344"0", "0.0", "25", "25.0", "25.5", "\u221e", "\ufffd",345"-0", "-0.0", "-25", "-25.0", "-25.5", "-\u221e",346};347int multipliers[] = {5, -5};348Number[][] expected = {349{350new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),351new BigDecimal("5.0"), new BigDecimal("5.1"),352Double.POSITIVE_INFINITY, Double.NaN,353new BigDecimal("0"), new BigDecimal("0.0"),354new BigDecimal("-5"), new BigDecimal("-5.0"),355new BigDecimal("-5.1"),356Double.NEGATIVE_INFINITY, Double.NaN,357},358{359new BigDecimal("0"), new BigDecimal("0.0"),360new BigDecimal("-5"), new BigDecimal("-5.0"),361new BigDecimal("-5.1"),362Double.NEGATIVE_INFINITY, Double.NaN,363new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),364new BigDecimal("5.0"), new BigDecimal("5.1"),365Double.POSITIVE_INFINITY,366},367};368369for (int i = 0; i < multipliers.length; i++) {370df.setMultiplier(multipliers[i]);371for (int j = 0; j < numbers.length; j++) {372check(String.valueOf(numbers[j]), expected[i][j]);373}374}375}376377/**378* Test for special numbers379*/380void test_Parse_in_DecimalFormat_Other() {381df = new DecimalFormat();382df.setParseBigDecimal(true);383384String[] numbers = {385"-9223372036854775808", // Long.MIN_VALUE386};387int multipliers[] = {1, -1};388String[][] expected = {389{"-9223372036854775808"}, // Long.MIN_VALUE390{"9223372036854775808"}, // Long.MAX_VALUE+1 = abs(MIN_VALUE)391};392393for (int i = 0; i < multipliers.length; i++) {394df.setMultiplier(multipliers[i]);395for (int j = 0; j < numbers.length; j++) {396check(String.valueOf(numbers[j]),397new BigDecimal(expected[i][j]));398}399}400}401402static final String[] patterns = {403" {0, number} ",404" {0, number} ",405" {0, number, currency} ",406" {0, number, currency} ",407" {0, number, percent} ",408" {0, number, percent} ",409" {0, number,#,##0.###E0} ",410" {0, number,#,##0.###E0} ",411412" {0, number} ",413" {0, number} ",414" {0, number, integer} ",415" {0, number, integer} ",416" {0, number, currency} ",417" {0, number, currency} ",418" {0, number, percent} ",419" {0, number, percent} ",420" {0, number,#,##0.###E0} ",421" {0, number,#,##0.###E0} ",422};423static final String[] from = {424" 12,345,678,901,234,567,890.98765432109876543210987654321 ",425" -12,345,678,901,234,567,890.98765432109876543210987654321 ",426" $12,345,678,901,234,567,890.98765432109876543210987654321 ",427" ($12,345,678,901,234,567,890.98765432109876543210987654321) ",428" 1,234,567,890,123,456,789,098.76543210987654321098765432100% ",429" -1,234,567,890,123,456,789,098.76543210987654321098765432100% ",430" 12,345,678,901,234,567,890.98765432109876543210987654321E-20 ",431" -12,345,678,901,234,567,890.98765432109876543210987654321E-20 ",432433" 9,876,543,210,987,654,321,098,765,432,109,876,543,210 ",434" -9,876,543,210,987,654,321,098,765,432,109,876,543,210 ",435" 9,876,543,210,987,654,321,098,765,432,109,876,543,210E5 ",436" -9,876,543,210,987,654,321,098,765,432,109,876,543,210E-5 ",437" $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00 ",438" ($9,876,543,210,987,654,321,098,765,432,109,876,543,210.00) ",439" 987,654,321,098,765,432,109,876,543,210,987,654,321,012% ",440" -987,654,321,098,765,432,109,876,543,210,987,654,321,012% ",441" 98,765,432,109,876,543,210.98765432109876543210E20 ",442" -987,654,321,098,765,432,109,876,543,210,987,654,321,000,000,000,000,000,000,000E-20 ",443};444445static final String[] expected1 = { // isParseIntegerOnly() == false446"12345678901234567890.98765432109876543210987654321",447"-12345678901234567890.98765432109876543210987654321",448"12345678901234567890.98765432109876543210987654321",449"-12345678901234567890.98765432109876543210987654321",450"12345678901234567890.98765432109876543210987654321",451"-12345678901234567890.98765432109876543210987654321",452"0.1234567890123456789098765432109876543210987654321",453"-0.1234567890123456789098765432109876543210987654321",454455"9876543210987654321098765432109876543210",456"-9876543210987654321098765432109876543210",457"9.876543210987654321098765432109876543210E44",458"-98765432109876543210987654321098765.43210",459"9876543210987654321098765432109876543210.00",460"-9876543210987654321098765432109876543210.00",461"9876543210987654321098765432109876543210.12",462"-9876543210987654321098765432109876543210.12",463"9876543210987654321098765432109876543210",464"-9876543210987654321098765432109876543210.00000000000000000000",465};466static final int[] parsePosition1 = {46760, 61, 61, 63, 64, 65, 64, 65,46857, 58, 59, 61, 61, 63, 60, 61, 54, 88,469};470471/**472* Test for MessageFormat: setParseIntegerOnly(false)473*/474void test_Parse_in_MessageFormat_NotParseIntegerOnly() {475for (int i=0; i < patterns.length; i++) {476pp = new ParsePosition(0);477Object[] parsed = null;478479try {480MessageFormat mf = new MessageFormat(patterns[i]);481Format[] formats = mf.getFormats();482for (int j=0; j < formats.length; j++) {483((DecimalFormat)formats[j]).setParseBigDecimal(true);484}485486parsed = mf.parse(from[i], pp);487488if (pp.getErrorIndex() != -1) {489errln("Case" + (i+1) +490": getErrorIndex() returns wrong value. expected:-1, got:"+491pp.getErrorIndex() + " for " + from[i]);492}493if (pp.getIndex() != parsePosition1[i]) {494errln("Case" + (i+1) +495": getIndex() returns wrong value. expected:" +496parsePosition1[i] + ", got:"+ pp.getIndex() +497" for " + from[i]);498}499}500catch(Exception e) {501errln("Unexpected exception: " + e.getMessage());502}503504checkType(from[i], getType(new BigDecimal(expected1[i])),505getType((Number)parsed[0]));506checkParse(from[i], new BigDecimal(expected1[i]),507(Number)parsed[0]);508}509}510511static final String[] expected2 = { // isParseIntegerOnly() == true512"12345678901234567890",513"-12345678901234567890",514"12345678901234567890",515"-12345678901234567890",516"12345678901234567890",517"-12345678901234567890",518"0",519"0",520521"9876543210987654321098765432109876543210",522"-9876543210987654321098765432109876543210",523"9.876543210987654321098765432109876543210E44",524"-98765432109876543210987654321098765.43210",525"9876543210987654321098765432109876543210",526"-9876543210987654321098765432109876543210",527"9876543210987654321098765432109876543210.12",528"-9876543210987654321098765432109876543210.12",529"9876543210987654321098765432109876543210",530"-9876543210987654321098765432109876543210.00000000000000000000",531};532static final int[][] parsePosition2 = { // {errorIndex, index}533/*534* Should keep in mind that the expected result is different from535* DecimalFormat.parse() for some cases.536*/537{28, 0}, // parsing stopped at '.'538{29, 0}, // parsing stopped at '.'539{29, 0}, // parsing stopped at '.'540{2, 0}, // parsing stopped at '(' because cannot find ')'541{2, 0}, // parsing stopped at the first numeric542// because cannot find '%'543{2, 0}, // parsing stopped at the first numeric544// because cannot find '%'545{28, 0}, // parsing stopped at '.'546{29, 0}, // parsing stopped at '.'547548{-1, 57}, {-1, 58}, {-1, 59}, {-1, 61},549{56, 0}, // parsing stopped at '.'550// because cannot find '%'551{2, 0}, // parsing stopped at '(' because cannot find ')'552{-1, 60}, {-1, 61},553{28, 0}, // parsing stopped at '.'554{-1, 88},555};556557/**558* Test for MessageFormat: setParseIntegerOnly(true)559*/560void test_Parse_in_MessageFormat_ParseIntegerOnly() {561for (int i=0; i < patterns.length; i++) {562pp = new ParsePosition(0);563Object[] parsed = null;564565try {566MessageFormat mf = new MessageFormat(patterns[i]);567Format[] formats = mf.getFormats();568for (int j=0; j < formats.length; j++) {569((DecimalFormat)formats[j]).setParseBigDecimal(true);570((DecimalFormat)formats[j]).setParseIntegerOnly(true);571}572573parsed = mf.parse(from[i], pp);574575if (pp.getErrorIndex() != parsePosition2[i][0]) {576errln("Case" + (i+1) +577": getErrorIndex() returns wrong value. expected:" +578parsePosition2[i][0] + ", got:"+ pp.getErrorIndex() +579" for " + from[i]);580}581if (pp.getIndex() != parsePosition2[i][1]) {582errln("Case" + (i+1) +583": getIndex() returns wrong value. expected:" +584parsePosition2[i][1] + ", got:"+ pp.getIndex() +585" for " + from[i]);586}587}588catch(Exception e) {589errln("Unexpected exception: " + e.getMessage());590}591592if (parsePosition2[i][0] == -1) {593checkType(from[i], getType(new BigDecimal(expected2[i])),594getType((Number)parsed[0]));595checkParse(from[i], new BigDecimal(expected2[i]),596(Number)parsed[0]);597}598}599}600601static final String[] from3 = {602"12,345,678,901,234,567,890.98765432109876543210987654321",603"-12,345,678,901,234,567,890.98765432109876543210987654321",604"9,876,543,210,987,654,321,098,765,432,109,876,543,210",605"-9,876,543,210,987,654,321,098,765,432,109,876,543,210",606"1234556790000E-8",607};608static final String[] expected3 = {609"12345678901234567890",610"-12345678901234567890",611"9876543210987654321098765432109876543210",612"-9876543210987654321098765432109876543210",613"12345.56790000",614};615static final int[][] parsePosition3 = { // {errorIndex, index}616{-1, 26},617{-1, 27},618{-1, 53},619{-1, 54},620{-1, 16},621};622623/**624* Test for DecimalFormat: setParseIntegerOnly(true)625*/626void test_Parse_in_DecimalFormat_ParseIntegerOnly() {627DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance();628df.setParseBigDecimal(true);629630for (int i=0; i < from3.length; i++) {631pp = new ParsePosition(0);632Number parsed = null;633634try {635parsed = df.parse(from3[i], pp);636637if (pp.getErrorIndex() != parsePosition3[i][0]) {638errln("Case" + (i+1) +639": getErrorIndex() returns wrong value. expected:" +640parsePosition3[i][0] + ", got:"+ pp.getErrorIndex() +641" for " + from3[i]);642}643if (pp.getIndex() != parsePosition3[i][1]) {644errln("Case" + (i+1) +645": getIndex() returns wrong value. expected:" +646parsePosition3[i][1] + ", got:"+ pp.getIndex() +647" for " + from3[i]);648}649}650catch(Exception e) {651errln("Unexpected exception: " + e.getMessage());652}653654if (parsePosition3[i][0] == -1) {655checkType(from3[i], getType(new BigDecimal(expected3[i])),656getType(parsed));657checkParse(from3[i], new BigDecimal(expected3[i]), parsed);658}659}660}661662protected void check(String from, Number to) {663pp = new ParsePosition(0);664try {665parsed = df.parse(from, pp);666}667catch(Exception e) {668exceptionOccurred = true;669errln(e.getMessage());670}671if (!exceptionOccurred) {672checkParse(from, to, parsed);673checkType(from, getType(to), getType(parsed));674checkParsePosition(from, from.length(), pp.getIndex());675}676}677678private void checkParse(String orig, Number expected, Number got) {679if (!expected.equals(got)) {680errln("Parsing... failed." +681"\n original: " + orig +682"\n parsed: " + got +683"\n expected: " + expected + "\n");684}685}686687private void checkType(String orig, String expected, String got) {688if (!expected.equals(got)) {689errln("Parsing... unexpected Class returned." +690"\n original: " + orig +691"\n got: " + got +692"\n expected: " + expected + "\n");693}694}695696private void checkParsePosition(String orig, int expected, int got) {697if (expected != got) {698errln("Parsing... wrong ParsePosition returned." +699"\n original: " + orig +700"\n got: " + got +701"\n expected: " + expected + "\n");702}703}704705private String getType(Number number) {706return number.getClass().getName();707}708}709710711