1 /*
   2  * Copyright (c) 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 /*
  24  * @test
  25  * @bug 8177552
  26  * @summary Checks CNF.parse() when parseBigDecimal is set to true
  27  * @modules jdk.localedata
  28  * @run testng/othervm TestParseBigDecimal
  29  */
  30 
  31 import org.testng.annotations.DataProvider;
  32 import org.testng.annotations.Test;
  33 
  34 import java.math.BigDecimal;
  35 import java.text.CompactNumberFormat;
  36 import java.text.NumberFormat;
  37 import java.text.ParseException;
  38 import java.util.Locale;
  39 
  40 public class TestParseBigDecimal {
  41 
  42     private static final CompactNumberFormat FORMAT_DZ_LONG = (CompactNumberFormat) NumberFormat
  43             .getCompactNumberInstance(new Locale("dz"), NumberFormat.Style.LONG);
  44 
  45     private static final CompactNumberFormat FORMAT_EN_US_SHORT = (CompactNumberFormat) NumberFormat
  46             .getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
  47 
  48     private static final CompactNumberFormat FORMAT_EN_LONG = (CompactNumberFormat) NumberFormat
  49             .getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
  50 
  51     private static final CompactNumberFormat FORMAT_HI_IN_LONG = (CompactNumberFormat) NumberFormat
  52             .getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.LONG);
  53 
  54     private static final CompactNumberFormat FORMAT_JA_JP_SHORT = (CompactNumberFormat) NumberFormat
  55             .getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
  56 
  57     private static final CompactNumberFormat FORMAT_IT_SHORT = (CompactNumberFormat) NumberFormat
  58             .getCompactNumberInstance(new Locale("it"), NumberFormat.Style.SHORT);
  59 
  60     private static final CompactNumberFormat FORMAT_SW_LONG = (CompactNumberFormat) NumberFormat
  61             .getCompactNumberInstance(new Locale("sw"), NumberFormat.Style.LONG);
  62 
  63     private static final CompactNumberFormat FORMAT_SE_SHORT = (CompactNumberFormat) NumberFormat
  64             .getCompactNumberInstance(new Locale("se"), NumberFormat.Style.SHORT);
  65 
  66     TestParseBigDecimal() {
  67         FORMAT_DZ_LONG.setParseBigDecimal(true);
  68         FORMAT_EN_US_SHORT.setParseBigDecimal(true);
  69         FORMAT_EN_LONG.setParseBigDecimal(true);
  70         FORMAT_HI_IN_LONG.setParseBigDecimal(true);
  71         FORMAT_JA_JP_SHORT.setParseBigDecimal(true);
  72         FORMAT_IT_SHORT.setParseBigDecimal(true);
  73         FORMAT_SW_LONG.setParseBigDecimal(true);
  74         FORMAT_SE_SHORT.setParseBigDecimal(true);
  75     }
  76 
  77     @DataProvider(name = "parse")
  78     Object[][] compactParseData() {
  79         return new Object[][]{
  80                 // compact number format instance, string to parse, parsed number
  81                 {FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
  82                         + "\u0F42 \u0F21", new BigDecimal("1000")},
  83                 {FORMAT_DZ_LONG, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
  84                         + "\u0F42 \u0F23", new BigDecimal("-3000")},
  85                 {FORMAT_DZ_LONG, "\u0F51\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62"
  86                         + "\u0F0B\u0F66\u0F0B\u0F61\u0F0B \u0F21"
  87                         + "\u0F22\u0F23\u0F24\u0F25\u0F27", new BigDecimal("12345700000000000000")},
  88                 {FORMAT_DZ_LONG, "-\u0F51\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62"
  89                         + "\u0F0B\u0F66\u0F0B\u0F61\u0F0B \u0F21"
  90                         + "\u0F22\u0F23\u0F24\u0F25\u0F27", new BigDecimal("-12345700000000000000")},
  91                 {FORMAT_EN_US_SHORT, "-0.0", new BigDecimal("-0.0")},
  92                 {FORMAT_EN_US_SHORT, "0", new BigDecimal("0")},
  93                 {FORMAT_EN_US_SHORT, "499", new BigDecimal("499")},
  94                 {FORMAT_EN_US_SHORT, "-499", new BigDecimal("-499")},
  95                 {FORMAT_EN_US_SHORT, "499.89", new BigDecimal("499.89")},
  96                 {FORMAT_EN_US_SHORT, "-499.89", new BigDecimal("-499.89")},
  97                 {FORMAT_EN_US_SHORT, "1K", new BigDecimal("1000")},
  98                 {FORMAT_EN_US_SHORT, "-1K", new BigDecimal("-1000")},
  99                 {FORMAT_EN_US_SHORT, "3K", new BigDecimal("3000")},
 100                 {FORMAT_EN_US_SHORT, "-3K", new BigDecimal("-3000")},
 101                 {FORMAT_EN_US_SHORT, "17K", new BigDecimal("17000")},
 102                 {FORMAT_EN_US_SHORT, "-17K", new BigDecimal("-17000")},
 103                 {FORMAT_EN_US_SHORT, "12345678901234567890",
 104                         new BigDecimal("12345678901234567890")},
 105                 {FORMAT_EN_US_SHORT, "12345679T", new BigDecimal("12345679000000000000")},
 106                 {FORMAT_EN_US_SHORT, "-12345679T", new BigDecimal("-12345679000000000000")},
 107                 {FORMAT_EN_US_SHORT, "599.01K", new BigDecimal("599010.00")},
 108                 {FORMAT_EN_US_SHORT, "-599.01K", new BigDecimal("-599010.00")},
 109                 {FORMAT_EN_US_SHORT, "599444444.90T", new BigDecimal("599444444900000000000.00")},
 110                 {FORMAT_EN_US_SHORT, "-599444444.90T", new BigDecimal("-599444444900000000000.00")},
 111                 {FORMAT_EN_US_SHORT, "123456789012345.5678K",
 112                         new BigDecimal("123456789012345567.8000")},
 113                 {FORMAT_EN_US_SHORT, "17.000K", new BigDecimal("17000.000")},
 114                 {FORMAT_EN_US_SHORT, "123.56678K", new BigDecimal("123566.78000")},
 115                 {FORMAT_EN_US_SHORT, "-123.56678K", new BigDecimal("-123566.78000")},
 116                 {FORMAT_EN_LONG, "999", new BigDecimal("999")},
 117                 {FORMAT_EN_LONG, "1 thousand", new BigDecimal("1000")},
 118                 {FORMAT_EN_LONG, "3 thousand", new BigDecimal("3000")},
 119                 {FORMAT_EN_LONG, "12345679 trillion", new BigDecimal("12345679000000000000")},
 120                 {FORMAT_HI_IN_LONG, "999", new BigDecimal("999")},
 121                 {FORMAT_HI_IN_LONG, "-999", new BigDecimal("-999")},
 122                 {FORMAT_HI_IN_LONG, "1 \u0939\u091C\u093C\u093E\u0930", new BigDecimal("1000")},
 123                 {FORMAT_HI_IN_LONG, "-1 \u0939\u091C\u093C\u093E\u0930", new BigDecimal("-1000")},
 124                 {FORMAT_HI_IN_LONG, "3 \u0939\u091C\u093C\u093E\u0930", new BigDecimal("3000")},
 125                 {FORMAT_HI_IN_LONG, "12345679 \u0916\u0930\u092C", new BigDecimal("1234567900000000000")},
 126                 {FORMAT_HI_IN_LONG, "-12345679 \u0916\u0930\u092C", new BigDecimal("-1234567900000000000")},
 127                 {FORMAT_JA_JP_SHORT, "-99", new BigDecimal("-99")},
 128                 {FORMAT_JA_JP_SHORT, "1\u4E07", new BigDecimal("10000")},
 129                 {FORMAT_JA_JP_SHORT, "30\u4E07", new BigDecimal("300000")},
 130                 {FORMAT_JA_JP_SHORT, "-30\u4E07", new BigDecimal("-300000")},
 131                 {FORMAT_JA_JP_SHORT, "12345679\u5146", new BigDecimal("12345679000000000000")},
 132                 {FORMAT_JA_JP_SHORT, "-12345679\u5146", new BigDecimal("-12345679000000000000")},
 133                 {FORMAT_IT_SHORT, "-99", new BigDecimal("-99")},
 134                 {FORMAT_IT_SHORT, "1\u00a0Mln", new BigDecimal("1000000")},
 135                 {FORMAT_IT_SHORT, "30\u00a0Mln", new BigDecimal("30000000")},
 136                 {FORMAT_IT_SHORT, "-30\u00a0Mln", new BigDecimal("-30000000")},
 137                 {FORMAT_IT_SHORT, "12345679\u00a0Bln", new BigDecimal("12345679000000000000")},
 138                 {FORMAT_IT_SHORT, "-12345679\u00a0Bln", new BigDecimal("-12345679000000000000")},
 139                 {FORMAT_SW_LONG, "-0.0", new BigDecimal("-0.0")},
 140                 {FORMAT_SW_LONG, "499", new BigDecimal("499")},
 141                 {FORMAT_SW_LONG, "elfu 1", new BigDecimal("1000")},
 142                 {FORMAT_SW_LONG, "elfu 3", new BigDecimal("3000")},
 143                 {FORMAT_SW_LONG, "elfu 17", new BigDecimal("17000")},
 144                 {FORMAT_SW_LONG, "elfu -3", new BigDecimal("-3000")},
 145                 {FORMAT_SW_LONG, "499", new BigDecimal("499")},
 146                 {FORMAT_SW_LONG, "-499", new BigDecimal("-499")},
 147                 {FORMAT_SW_LONG, "elfu 1", new BigDecimal("1000")},
 148                 {FORMAT_SW_LONG, "elfu 3", new BigDecimal("3000")},
 149                 {FORMAT_SW_LONG, "elfu -3", new BigDecimal("-3000")},
 150                 {FORMAT_SW_LONG, "elfu 17", new BigDecimal("17000")},
 151                 {FORMAT_SW_LONG, "trilioni 12345679", new BigDecimal("12345679000000000000")},
 152                 {FORMAT_SW_LONG, "trilioni -12345679", new BigDecimal("-12345679000000000000")},
 153                 {FORMAT_SW_LONG, "elfu 599.01", new BigDecimal("599010.00")},
 154                 {FORMAT_SW_LONG, "elfu -599.01", new BigDecimal("-599010.00")},
 155                 {FORMAT_SE_SHORT, "999", new BigDecimal("999")},
 156                 {FORMAT_SE_SHORT, "8\u00a0mn", new BigDecimal("8000000")},
 157                 {FORMAT_SE_SHORT, "8\u00a0dt", new BigDecimal("8000")},
 158                 {FORMAT_SE_SHORT, "12345679\u00a0bn", new BigDecimal("12345679000000000000")},
 159                 {FORMAT_SE_SHORT, "12345679,89\u00a0bn", new BigDecimal("12345679890000000000.00")},
 160                 {FORMAT_SE_SHORT, "\u2212999", new BigDecimal("-999")},
 161                 {FORMAT_SE_SHORT, "\u22128\u00a0mn", new BigDecimal("-8000000")},
 162                 {FORMAT_SE_SHORT, "\u22128\u00a0dt", new BigDecimal("-8000")},
 163                 {FORMAT_SE_SHORT, "\u221212345679\u00a0bn", new BigDecimal("-12345679000000000000")},
 164                 {FORMAT_SE_SHORT, "\u221212345679,89\u00a0bn", new BigDecimal("-12345679890000000000.00")},};
 165     }
 166 
 167     @Test(dataProvider = "parse")
 168     public void testParse(NumberFormat cnf, String parseString,
 169                           Number expected) throws ParseException {
 170         CompactFormatAndParse.testParse(cnf, parseString, expected, null, BigDecimal.class);
 171     }
 172 }
 173