From: Peter Edberg <pedberg@unicode.org>
Date: Fri, 16 Sep 2022 22:09:56 +0000 (-0700)
Subject: ICU-20512 ICU4J: just add test of parse with empty curr symbol, code already works
X-Git-Tag: release-72-rc~15
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e646ea23e9988fff220ae8c135adf577dfe4c65c;p=icu

ICU-20512 ICU4J: just add test of parse with empty curr symbol, code already works
---

diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java
index 3351f8d6ea9..f15baaa482d 100644
--- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java
+++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java
@@ -6966,4 +6966,94 @@ public class NumberFormatTest extends TestFmwk {
             assertEquals("Via applyPattern: field position end", 3, fp.getEndIndex());
         }
     }
+
+    @Test
+    public void TestParseWithEmptyCurr() {
+        DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(new ULocale("en_US"), NumberFormat.CURRENCYSTYLE);
+        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
+        dfs.setCurrencySymbol("");
+        df.setDecimalFormatSymbols(dfs);
+ 
+        String textToParse = "3";    
+        ParsePosition ppos = new ParsePosition(0);
+
+        Number num = df.parse(textToParse, ppos);
+        if (ppos.getIndex() != 1 || num.doubleValue() != 3.0) {
+            errln("parse with empty curr symbol, expect pos 1, value 3.0; get "
+                                + ppos.getIndex() + ", " + num.doubleValue());
+        }
+
+        ppos.setIndex(0);
+        CurrencyAmount currAmt = df.parseCurrency(textToParse, ppos);
+        if (currAmt != null) {
+            errln("parseCurrency with empty curr symbol, expect null but succeeds");
+        }
+
+        //                        "¤#,##0.00" "¤ #,##0.00" "#,##0.00 ¤" "#,##,##0.00¤"
+        final String[] locales = {"en_US",    "nb_NO",     "cs_CZ",     "bn_BD"};
+        for (String locale: locales) {
+            df = (DecimalFormat)NumberFormat.getInstance(new ULocale(locale), NumberFormat.CURRENCYSTYLE);
+            dfs = df.getDecimalFormatSymbols();
+            dfs.setCurrencySymbol("");
+            df.setDecimalFormatSymbols(dfs);
+
+            final double posValToUse = 37.0;
+            final double negValToUse = -3.0;
+
+            textToParse = df.format(posValToUse);
+            int expectParseLen = textToParse.length();
+            if (textToParse.endsWith("\u00A0") || textToParse.endsWith("\u202F")) { // NBSP, NNBSP
+                expectParseLen--;
+            }
+            ppos.setIndex(0);
+            num = df.parse(textToParse, ppos);
+            if (ppos.getIndex() != expectParseLen || num.doubleValue() != posValToUse) {
+                errln("locale " + locale + ", parse with empty curr symbol, expect pos, value "
+                                + expectParseLen + ", " + posValToUse + ";  get "
+                                + ppos.getIndex() + ", " + num.doubleValue());
+            }
+
+            textToParse = df.format(negValToUse);
+            expectParseLen = textToParse.length();
+            if (textToParse.endsWith("\u00A0") || textToParse.endsWith("\u202F")) { // NBSP, NNBSP
+                expectParseLen--;
+            }
+            ppos.setIndex(0);
+            num = df.parse(textToParse, ppos);
+            if (ppos.getIndex() != expectParseLen || num.doubleValue() != negValToUse) {
+                errln("locale " + locale + ", parse with empty curr symbol, expect pos, value "
+                                + expectParseLen + ", " + negValToUse + ";  get "
+                                + ppos.getIndex() + ", " + num.doubleValue());
+            }
+
+            df.applyPattern("#,##0.00¤");
+
+            textToParse = df.format(posValToUse);
+            expectParseLen = textToParse.length();
+            if (textToParse.endsWith("\u00A0") || textToParse.endsWith("\u202F")) { // NBSP, NNBSP
+                expectParseLen--;
+            }
+            ppos.setIndex(0);
+            num = df.parse(textToParse, ppos);
+            if (ppos.getIndex() != expectParseLen || num.doubleValue() != posValToUse) {
+                errln("locale " + locale + "with \"#,##0.00¤\", parse with empty curr symbol, expect pos, value "
+                                + expectParseLen + ", " + posValToUse + ";  get "
+                                + ppos.getIndex() + ", " + num.doubleValue());
+            }
+
+            textToParse = df.format(negValToUse);
+            expectParseLen = textToParse.length();
+            if (textToParse.endsWith("\u00A0") || textToParse.endsWith("\u202F")) { // NBSP, NNBSP
+                expectParseLen--;
+            }
+            ppos.setIndex(0);
+            num = df.parse(textToParse, ppos);
+            if (ppos.getIndex() != expectParseLen || num.doubleValue() != negValToUse) {
+                errln("locale " + locale + "with \"#,##0.00¤\", parse with empty curr symbol, expect pos, value "
+                                + expectParseLen + ", " + negValToUse + ";  get "
+                                + ppos.getIndex() + ", " + num.doubleValue());
+            }
+
+        }
+    }
 }