• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2004, 2018, 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  */
23 package test.java.math.BigDecimal;
24 
25 /*
26  * @test
27  * @bug 4984872
28  * @summary Basic tests of toPlainString method
29  * @run main ToPlainStringTests
30  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+EliminateAutoBox -XX:AutoBoxCacheMax=20000 ToPlainStringTests
31  * @author Joseph D. Darcy
32  */
33 
34 import java.math.*;
35 
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38 
39 // Android-changed: Replace error counting with asserts.
40 public class ToPlainStringTests {
41 
42     @Test
testToPlainString()43     public void testToPlainString() {
44         String [][] testCases = {
45             {"0",                       "0"},
46             {"1",                       "1"},
47             {"10",                      "10"},
48             {"2e1",                     "20"},
49             {"3e2",                     "300"},
50             {"4e3",                     "4000"},
51             {"5e4",                     "50000"},
52             {"6e5",                     "600000"},
53             {"7e6",                     "7000000"},
54             {"8e7",                     "80000000"},
55             {"9e8",                     "900000000"},
56             {"1e9",                     "1000000000"},
57 
58             {".0",                      "0.0"},
59             {".1",                      "0.1"},
60             {".10",                     "0.10"},
61             {"1e-1",                    "0.1"},
62             {"1e-1",                    "0.1"},
63             {"2e-2",                    "0.02"},
64             {"3e-3",                    "0.003"},
65             {"4e-4",                    "0.0004"},
66             {"5e-5",                    "0.00005"},
67             {"6e-6",                    "0.000006"},
68             {"7e-7",                    "0.0000007"},
69             {"8e-8",                    "0.00000008"},
70             {"9e-9",                    "0.000000009"},
71             {"9000e-12",                "0.000000009000"},
72 
73             {"9000e-22",                 "0.0000000000000000009000"},
74             {"12345678901234567890",     "12345678901234567890"},
75             {"12345678901234567890e22",  "123456789012345678900000000000000000000000"},
76             {"12345678901234567890e-22", "0.0012345678901234567890"},
77         };
78 
79         for(String[] testCase: testCases) {
80             BigDecimal bd = new BigDecimal(testCase[0]);
81             String s;
82 
83             s = bd.toPlainString();
84             Assert.assertEquals(s, testCase[1],
85                 "Unexpected plain result ``" + s + "'' from BigDecimal " + bd);
86 
87             bd = new BigDecimal("-"+testCase[0]);
88             s = bd.toPlainString();
89             Assert.assertFalse(bd.signum()!=0 && !s.equals("-"+testCase[1]),
90                 "Unexpected plain result ``" + s + "'' from BigDecimal " + bd);
91         }
92     }
93 }
94