Path: blob/master/test/jdk/java/text/Format/NumberFormat/Bug4838107.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 4838107 800857726* @summary Confirm that DecimalFormat can format a number with negative exponent number correctly.27* @library /java/text/testlib28* @run main/othervm -Djava.locale.providers=COMPAT,SPI Bug483810729*/3031import java.math.*;32import java.util.*;33import java.text.*;3435public class Bug4838107 extends IntlTest {3637static DecimalFormat df;38static DecimalFormatSymbols dfs;39static boolean err = false;4041static public void main(String[] args) {42Locale defaultLoc = Locale.getDefault();43Locale.setDefault(Locale.US);4445/**46* This bug is about exponential formatting. But I added test cases for:47* - Double and BigDecimal numbers which don't have exponent parts.48* - Long and BigInteger numbers which don't support exponential49* notation.50* because there are few test cases for suffix and prefix.51* And also, I added test cases to guarantee further formatting and52* parsing using the same DecimalFormat instance will not change the53* Number's value anymore.54*/5556test_double();57test_long();58test_BigDecimal();59test_BigInteger();6061Locale.setDefault(defaultLoc);6263if (err) {64throw new RuntimeException("Wrong format with DecimalFormat");65}66}6768static void test_double() {69df = new DecimalFormat();70dfs = df.getDecimalFormatSymbols();7172/* Test with default pattern */73test(1234D, "1,234");74test(0.1234, "0.123"); // rounded75test(-1234D, "-1,234");76test(-0.1234, "-0.123"); // rounded7778test(Double.POSITIVE_INFINITY, "\u221e");79test(Double.NEGATIVE_INFINITY, "-\u221e");80test(Double.NaN, "\ufffd"); // without prefix and suffix81test(0.0, "0");82test(-0.0, "-0"); // with the minus sign8384/* Specify a pattern and the minus sign. */85prepareFormatter("<P>#.###E00<S>", 'm');86test(1234D, "<P>1.234E03<S>");87test(0.1234, "<P>1.234Em01<S>");88test(-1234D, "m<P>1.234E03<S>");89test(-0.1234, "m<P>1.234Em01<S>");9091prepareFormatter("<P>#.###E00<S>;#.###E00", 'm');92test(1234D, "<P>1.234E03<S>");93test(0.1234, "<P>1.234Em01<S>");94test(-1234D, "1.234E03");95test(-0.1234, "1.234Em01");9697prepareFormatter("#.###E00;<P>#.###E00<S>", 'm');98test(1234D, "1.234E03");99test(0.1234, "1.234Em01");100test(-1234D, "<P>1.234E03<S>");101test(-0.1234, "<P>1.234Em01<S>");102103prepareFormatter("<P>#.###E00<S>;<p>-#.###E00<s>", 'm');104test(1234D, "<P>1.234E03<S>");105test(0.1234, "<P>1.234Em01<S>");106test(-1234D, "<p>m1.234E03<s>");107test(-0.1234, "<p>m1.234Em01<s>");108109test(Double.POSITIVE_INFINITY, "<P>\u221e<S>");110test(Double.NEGATIVE_INFINITY, "<p>m\u221e<s>");111test(Double.NaN, "\ufffd"); // without prefix and suffix112test(0.0, "<P>0E00<S>");113test(-0.0, "<p>m0E00<s>"); // with the minus sign114}115116static void test_BigDecimal() {117df = new DecimalFormat();118dfs = df.getDecimalFormatSymbols();119120/* Test with default pattern */121test(new BigDecimal("123456789012345678901234567890"),122"123,456,789,012,345,678,901,234,567,890");123test(new BigDecimal("0.000000000123456789012345678901234567890"),124"0");125test(new BigDecimal("-123456789012345678901234567890"),126"-123,456,789,012,345,678,901,234,567,890");127test(new BigDecimal("-0.000000000123456789012345678901234567890"),128"-0");129130test(new BigDecimal("0"), "0");131test(new BigDecimal("-0"), "0");132133/* Specify a pattern and the minus sign. */134prepareFormatter("<P>#.####################E00<S>;<p>-#.####################E00<s>", 'm');135test(new BigDecimal("123456789012345678901234567890"),136"<P>1.23456789012345678901E29<S>");137test(new BigDecimal("0.000000000123456789012345678901234567890"),138"<P>1.23456789012345678901Em10<S>");139test(new BigDecimal("-123456789012345678901234567890"),140"<p>m1.23456789012345678901E29<s>");141test(new BigDecimal("-0.000000000123456789012345678901234567890"),142"<p>m1.23456789012345678901Em10<s>");143144test(new BigDecimal("0"), "<P>0E00<S>");145test(new BigDecimal("-0"), "<P>0E00<S>");146}147148static void test_long() {149df = new DecimalFormat();150dfs = df.getDecimalFormatSymbols();151152/* Test with default pattern */153test(123456789L, "123,456,789");154test(-123456789L, "-123,456,789");155156test(0L, "0");157test(-0L, "0");158159/* Specify a pattern and the minus sign. */160prepareFormatter("<P>#,###<S>;<p>-#,###<s>", 'm');161test(123456789L, "<P>123,456,789<S>");162test(-123456789L, "<p>m123,456,789<s>");163164test(0L, "<P>0<S>");165test(-0L, "<P>0<S>");166}167168static void test_BigInteger() {169df = new DecimalFormat();170dfs = df.getDecimalFormatSymbols();171172/* Test with default pattern */173test(new BigInteger("123456789012345678901234567890"),174"123,456,789,012,345,678,901,234,567,890");175test(new BigInteger("-123456789012345678901234567890"),176"-123,456,789,012,345,678,901,234,567,890");177178test(new BigInteger("0"), "0");179test(new BigInteger("-0"), "0");180181/* Specify a pattern and the minus sign. */182prepareFormatter("<P>#,###<S>;<p>-#,###<s>", 'm');183test(new BigInteger("123456789012345678901234567890"),184"<P>123,456,789,012,345,678,901,234,567,890<S>");185test(new BigInteger("-123456789012345678901234567890"),186"<p>m123,456,789,012,345,678,901,234,567,890<s>");187188test(new BigInteger("0"), "<P>0<S>");189test(new BigInteger("-0"), "<P>0<S>");190}191192static void prepareFormatter(String pattern, char minusSign) {193dfs = df.getDecimalFormatSymbols();194df.applyPattern(pattern);195dfs.setMinusSign(minusSign);196df.setDecimalFormatSymbols(dfs);197}198199static void test(Number num, String str) {200String formatted = df.format(num);201if (!formatted.equals(str)) {202err = true;203System.err.println(" DecimalFormat format(" +204num.getClass().getName() +205") error: \n\tnumber: " + num +206"\n\tminus sign: " + dfs.getMinusSign() +207"\n\tgot: " + formatted +208"\n\texpected: " + str);209return;210}211212if (num instanceof BigDecimal || num instanceof BigInteger) {213df.setParseBigDecimal(true);214}215Number parsed1 = null, parsed2 = null;216try {217parsed1 = df.parse(formatted);218formatted = df.format(parsed1);219parsed2 = df.parse(formatted);220if (!parsed1.equals(parsed2)) {221err = true;222System.err.println(" DecimalFormat roundtrip parse(" +223num.getClass().getName() +224") error: \n\toriginal number: " + str +225"\n\tparsed number: " + parsed1 +226" (" + parsed1.getClass().getName() + ")" +227"\n\tformatted number: " + formatted +228"\n\tre-parsed number: " + parsed2 +229" (" + parsed2.getClass().getName() + ")" +230"\n\tminus sign: " + dfs.getMinusSign());231}232}233catch (Exception e) {234err = true;235System.err.println(" DecimalFormat parse(" +236num.getClass().getName() +237") threw an Exception: " + e.getMessage() +238"\n\toriginal number: " + str +239"\n\tparsed number : " + parsed1 +240" (" + parsed1.getClass().getName() + ")" +241"\n\tformatted number: " + formatted +242"\n\tre-parsed number: " + parsed2 +243" (" + parsed2.getClass().getName() + ")" +244"\n\tminus sign: " + dfs.getMinusSign());245}246}247}248249250