Path: blob/master/test/jdk/java/math/BigDecimal/ToPlainStringTests.java
41152 views
/*1* Copyright (c) 2004, 2018, 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 498487226* @summary Basic tests of toPlainString method27* @run main ToPlainStringTests28* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 ToPlainStringTests29* @author Joseph D. Darcy30*/3132import java.math.*;3334public class ToPlainStringTests {35public static void main(String argv[]) {36String [][] testCases = {37{"0", "0"},38{"1", "1"},39{"10", "10"},40{"2e1", "20"},41{"3e2", "300"},42{"4e3", "4000"},43{"5e4", "50000"},44{"6e5", "600000"},45{"7e6", "7000000"},46{"8e7", "80000000"},47{"9e8", "900000000"},48{"1e9", "1000000000"},4950{".0", "0.0"},51{".1", "0.1"},52{".10", "0.10"},53{"1e-1", "0.1"},54{"1e-1", "0.1"},55{"2e-2", "0.02"},56{"3e-3", "0.003"},57{"4e-4", "0.0004"},58{"5e-5", "0.00005"},59{"6e-6", "0.000006"},60{"7e-7", "0.0000007"},61{"8e-8", "0.00000008"},62{"9e-9", "0.000000009"},63{"9000e-12", "0.000000009000"},6465{"9000e-22", "0.0000000000000000009000"},66{"12345678901234567890", "12345678901234567890"},67{"12345678901234567890e22", "123456789012345678900000000000000000000000"},68{"12345678901234567890e-22", "0.0012345678901234567890"},69};7071int errors = 0;72for(String[] testCase: testCases) {73BigDecimal bd = new BigDecimal(testCase[0]);74String s;7576if (!(s=bd.toPlainString()).equals(testCase[1])) {77errors++;78System.err.println("Unexpected plain result ``" +79s + "'' from BigDecimal " +80bd);81}82bd = new BigDecimal("-"+testCase[0]);83if (bd.signum()!=0 && !(s=(bd.toPlainString())).equals("-"+testCase[1])) {84errors++;85System.err.println("Unexpected plain result ``" +86s + "'' from BigDecimal " +87bd);88}89}9091if(errors > 0)92throw new RuntimeException(errors + " errors during run.");93}94}959697