]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/format/NumberFormatRegressionTest.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / format / NumberFormatRegressionTest.java
old mode 100755 (executable)
new mode 100644 (file)
index c873876..a882a3a
-//##header\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 2001-2009, International Business Machines Corporation and    *\r
- * others. All Rights Reserved.                                                *\r
- *******************************************************************************\r
- */\r
-\r
-/** \r
- * Port From:   ICU4C v1.8.1 : format : NumberFormatRegressionTest\r
- * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp\r
- **/\r
-\r
-package com.ibm.icu.dev.test.format;\r
-\r
-import com.ibm.icu.text.*;\r
-import com.ibm.icu.util.*;\r
-import java.util.Locale;\r
-import java.util.Date;\r
-import java.text.ParseException;\r
-import java.io.*;\r
-\r
-/** \r
- * Performs regression test for MessageFormat\r
- **/\r
-public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk {\r
-    \r
-    public static void main(String[] args) throws Exception{\r
-        new NumberFormatRegressionTest().run(args);\r
-    }\r
-    \r
-    /**\r
-     * alphaWorks upgrade\r
-     */\r
-    public void Test4161100() {\r
-        NumberFormat nf = NumberFormat.getInstance(Locale.US);\r
-        nf.setMinimumFractionDigits(1);\r
-        nf.setMaximumFractionDigits(1);\r
-        double a = -0.09;\r
-        String s = nf.format(a);\r
-        logln(a + " x " +\r
-              ((DecimalFormat) nf).toPattern() + " = " + s);\r
-        if (!s.equals("-0.1")) {\r
-            errln("FAIL");\r
-        }\r
-    }\r
-    \r
-    /**\r
-     * DateFormat should call setIntegerParseOnly(TRUE) on adopted\r
-     * NumberFormat objects.\r
-     */\r
-    public void TestJ691() {\r
-        \r
-        Locale loc = new Locale("fr", "CH");\r
-    \r
-        // set up the input date string & expected output\r
-        String udt = "11.10.2000";\r
-        String exp = "11.10.00";\r
-    \r
-        // create a Calendar for this locale\r
-        Calendar cal = Calendar.getInstance(loc);\r
-    \r
-        // create a NumberFormat for this locale\r
-        NumberFormat nf = NumberFormat.getInstance(loc);\r
-    \r
-        // *** Here's the key: We don't want to have to do THIS:\r
-        //nf.setParseIntegerOnly(true);\r
-    \r
-        // create the DateFormat\r
-        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);\r
-    \r
-        df.setCalendar(cal);\r
-        df.setNumberFormat(nf);\r
-    \r
-        // set parsing to lenient & parse\r
-        Date ulocdat = new Date();\r
-        df.setLenient(true);\r
-        try {\r
-            ulocdat = df.parse(udt);\r
-        } catch (java.text.ParseException pe) {\r
-            errln(pe.getMessage());\r
-        }\r
-        // format back to a string\r
-        String outString = df.format(ulocdat);\r
-    \r
-        if (!outString.equals(exp)) {\r
-            errln("FAIL: " + udt + " => " + outString);\r
-        }\r
-    }\r
-    \r
-    /**\r
-     * Test getIntegerInstance();\r
-     */\r
-    public void Test4408066() {\r
-        \r
-        NumberFormat nf1 = NumberFormat.getIntegerInstance();\r
-        NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);\r
-    \r
-        //test isParseIntegerOnly\r
-        if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {\r
-            errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");\r
-        }\r
-    \r
-        //Test format\r
-        {\r
-            double[] data = {\r
-                -3.75, -2.5, -1.5, \r
-                -1.25, 0,    1.0, \r
-                1.25,  1.5,  2.5, \r
-                3.75,  10.0, 255.5\r
-                };\r
-            String[] expected = {\r
-                "-4", "-2", "-2",\r
-                "-1", "0",  "1",\r
-                "1",  "2",  "2",\r
-                "4",  "10", "256"\r
-                };\r
-    \r
-            for (int i = 0; i < data.length; ++i) {\r
-                String result = nf1.format(data[i]);\r
-                if (!result.equals(expected[i])) {\r
-                    errln("Failed => Source: " + Double.toString(data[i]) \r
-                        + ";Formatted : " + result\r
-                        + ";but expectted: " + expected[i]);\r
-                }\r
-            }\r
-        }\r
-        //Test parse, Parsing should stop at "."\r
-        {\r
-            String data[] = {\r
-                "-3.75", "-2.5", "-1.5", \r
-                "-1.25", "0",    "1.0", \r
-                "1.25",  "1.5",  "2.5", \r
-                "3.75",  "10.0", "255.5"\r
-                };\r
-            long[] expected = {\r
-                -3, -2, -1,\r
-                -1, 0,  1,\r
-                1,  1,  2,\r
-                3,  10, 255\r
-                };\r
-            \r
-            for (int i = 0; i < data.length; ++i) {\r
-                Number n = null;\r
-                try {\r
-                    n = nf1.parse(data[i]);\r
-                } catch (ParseException e) {\r
-                    errln("Failed: " + e.getMessage());\r
-                }\r
-                if (!(n instanceof Long) || (n instanceof Integer)) {\r
-                    errln("Failed: Integer Number Format should parse string to Long/Integer");\r
-                }\r
-                if (n.longValue() != expected[i]) {\r
-                    errln("Failed=> Source: " + data[i] \r
-                        + ";result : " + n.toString()\r
-                        + ";expected :" + Long.toString(expected[i]));\r
-                }\r
-            }\r
-        }\r
-    }\r
-    \r
-    //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)\r
-    public void TestSerialization() throws IOException{\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//#else\r
-        byte[][] contents = NumberFormatSerialTestData.getContent();\r
-        double data = 1234.56;\r
-        String[] expected = {\r
-            "1,234.56", "$1,234.56", "123,456%", "1.23456E3"};\r
-        for (int i = 0; i < 4; ++i) {\r
-            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i]));\r
-            try {\r
-                NumberFormat format = (NumberFormat) ois.readObject();\r
-                String result = format.format(data);\r
-                if (result.equals(expected[i])) {\r
-                    logln("OK: Deserialized bogus NumberFormat(new version read old version)");\r
-                } else {\r
-                    errln("FAIL: the test data formats are not euqal");\r
-                }\r
-            } catch (Exception e) {\r
-                warnln("FAIL: " + e.getMessage());\r
-            }\r
-        }\r
-//#endif\r
-    }\r
-\r
-    /*\r
-     * Test case for JB#5509, strict parsing issue\r
-     */\r
-    public void TestJB5509() {\r
-        String[] data = {\r
-                "1,2",\r
-                "1.2",\r
-                "1,2.5",\r
-                "1,23.5",\r
-                "1,234.5",\r
-                "1,234",\r
-                "1,234,567",\r
-                "1,234,567.8",\r
-                "1,234,5",\r
-                "1,234,5.6",\r
-                "1,234,56.7"\r
-        };\r
-        boolean[] expected = { // false for expected parse failure\r
-                false,\r
-                true,\r
-                false,\r
-                false,\r
-                true,\r
-                true,\r
-                true,\r
-                true,\r
-                false,\r
-                false,\r
-                false,\r
-                false\r
-        };\r
-\r
-        DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));\r
-        df.setParseStrict(true);\r
-        for (int i = 0; i < data.length; i++) {\r
-            try {\r
-                df.parse(data[i]);\r
-                if (!expected[i]) {\r
-                    errln("Failed: ParseException must be thrown for string " + data[i]);\r
-                }\r
-            } catch (ParseException pe) {\r
-                if (expected[i]) {\r
-                    errln("Failed: ParseException must not be thrown for string " + data[i]);\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    /*\r
-     * Test case for ticket#5698 - parsing extremely large/small values\r
-     */\r
-    public void TestT5698() {\r
-        final String[] data = {\r
-                "12345679E66666666666666666",\r
-                "-12345679E66666666666666666",\r
-                ".1E2147483648", // exponent > max int\r
-                ".1E2147483647", // exponent == max int\r
-                ".1E-2147483648", // exponent == min int\r
-                ".1E-2147483649", // exponent < min int\r
-                "1.23E350", // value > max double\r
-                "1.23E300", // value < max double\r
-                "-1.23E350", // value < min double\r
-                "-1.23E300", // value > min double\r
-                "4.9E-324", // value = smallest non-zero double\r
-                "1.0E-325", // 0 < value < smallest non-zero positive double0\r
-                "-1.0E-325", // 0 > value > largest non-zero negative double\r
-        };\r
-        final double[] expected = {\r
-                Double.POSITIVE_INFINITY,\r
-                Double.NEGATIVE_INFINITY,\r
-                Double.POSITIVE_INFINITY,\r
-                Double.POSITIVE_INFINITY,\r
-                0.0,\r
-                0.0,\r
-                Double.POSITIVE_INFINITY,\r
-                1.23e300d,\r
-                Double.NEGATIVE_INFINITY,\r
-                -1.23e300d,\r
-                4.9e-324d,\r
-                0.0,\r
-                -0.0,\r
-        };\r
-\r
-        NumberFormat nfmt = NumberFormat.getInstance();\r
-\r
-        for (int i = 0; i < data.length; i++) {\r
-            try {\r
-                Number n = nfmt.parse(data[i]);\r
-                if (expected[i] != n.doubleValue()) {\r
-                    errln("Failed: Parsed result for " + data[i] + ": " \r
-                            + n.doubleValue() + " / expected: " + expected[i]);\r
-                }\r
-            } catch (ParseException pe) {\r
-                errln("Failed: ParseException is thrown for " + data[i]);\r
-            }\r
-        }\r
-    }\r
-    \r
-    void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) {\r
-        String myString = nf.format(myNumber);\r
-        \r
-            double aNumber;\r
-            try {\r
-                aNumber = nf.parse(myString).doubleValue();\r
-            } catch (ParseException e) {\r
-                // TODO Auto-generated catch block\r
-                errln("FAIL: " + testcase +" - failed to parse. " + e.toString());\r
-                return;\r
-            }\r
-            if(Math.abs(aNumber-myNumber)>.001) {\r
-            errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");\r
-        } else {\r
-            logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");\r
-        }\r
-    }\r
-\r
-    void checkNBSPPatternRT(String testcase, NumberFormat nf) {\r
-        checkNBSPPatternRtNum(testcase, nf, 12345.);\r
-        checkNBSPPatternRtNum(testcase, nf, -12345.);\r
-    }\r
-\r
-    public void TestNBSPInPattern() {\r
-    NumberFormat nf = null;\r
-    String testcase;\r
-    \r
-    \r
-    testcase="ar_AE UNUM_CURRENCY";\r
-    nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));\r
-    checkNBSPPatternRT(testcase, nf);\r
-    // if we don't have CLDR 1.6 data, bring out the problem anyways \r
-    \r
-    String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";\r
-    testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;\r
-    nf = new DecimalFormat();\r
-    ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN);\r
-    checkNBSPPatternRT(testcase, nf);\r
-    \r
-    }\r
-    \r
-}\r
+//##header J2SE15
+/*
+ *******************************************************************************
+ * Copyright (C) 2001-2009, International Business Machines Corporation and    *
+ * others. All Rights Reserved.                                                *
+ *******************************************************************************
+ */
+
+/** 
+ * Port From:   ICU4C v1.8.1 : format : NumberFormatRegressionTest
+ * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp
+ **/
+
+package com.ibm.icu.dev.test.format;
+
+import com.ibm.icu.text.*;
+import com.ibm.icu.util.*;
+import java.util.Locale;
+import java.util.Date;
+import java.text.ParseException;
+import java.io.*;
+
+/** 
+ * Performs regression test for MessageFormat
+ **/
+public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk {
+    
+    public static void main(String[] args) throws Exception{
+        new NumberFormatRegressionTest().run(args);
+    }
+    
+    /**
+     * alphaWorks upgrade
+     */
+    public void Test4161100() {
+        NumberFormat nf = NumberFormat.getInstance(Locale.US);
+        nf.setMinimumFractionDigits(1);
+        nf.setMaximumFractionDigits(1);
+        double a = -0.09;
+        String s = nf.format(a);
+        logln(a + " x " +
+              ((DecimalFormat) nf).toPattern() + " = " + s);
+        if (!s.equals("-0.1")) {
+            errln("FAIL");
+        }
+    }
+    
+    /**
+     * DateFormat should call setIntegerParseOnly(TRUE) on adopted
+     * NumberFormat objects.
+     */
+    public void TestJ691() {
+        
+        Locale loc = new Locale("fr", "CH");
+    
+        // set up the input date string & expected output
+        String udt = "11.10.2000";
+        String exp = "11.10.00";
+    
+        // create a Calendar for this locale
+        Calendar cal = Calendar.getInstance(loc);
+    
+        // create a NumberFormat for this locale
+        NumberFormat nf = NumberFormat.getInstance(loc);
+    
+        // *** Here's the key: We don't want to have to do THIS:
+        //nf.setParseIntegerOnly(true);
+    
+        // create the DateFormat
+        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);
+    
+        df.setCalendar(cal);
+        df.setNumberFormat(nf);
+    
+        // set parsing to lenient & parse
+        Date ulocdat = new Date();
+        df.setLenient(true);
+        try {
+            ulocdat = df.parse(udt);
+        } catch (java.text.ParseException pe) {
+            errln(pe.getMessage());
+        }
+        // format back to a string
+        String outString = df.format(ulocdat);
+    
+        if (!outString.equals(exp)) {
+            errln("FAIL: " + udt + " => " + outString);
+        }
+    }
+    
+    /**
+     * Test getIntegerInstance();
+     */
+    public void Test4408066() {
+        
+        NumberFormat nf1 = NumberFormat.getIntegerInstance();
+        NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
+    
+        //test isParseIntegerOnly
+        if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
+            errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
+        }
+    
+        //Test format
+        {
+            double[] data = {
+                -3.75, -2.5, -1.5, 
+                -1.25, 0,    1.0, 
+                1.25,  1.5,  2.5, 
+                3.75,  10.0, 255.5
+                };
+            String[] expected = {
+                "-4", "-2", "-2",
+                "-1", "0",  "1",
+                "1",  "2",  "2",
+                "4",  "10", "256"
+                };
+    
+            for (int i = 0; i < data.length; ++i) {
+                String result = nf1.format(data[i]);
+                if (!result.equals(expected[i])) {
+                    errln("Failed => Source: " + Double.toString(data[i]) 
+                        + ";Formatted : " + result
+                        + ";but expectted: " + expected[i]);
+                }
+            }
+        }
+        //Test parse, Parsing should stop at "."
+        {
+            String data[] = {
+                "-3.75", "-2.5", "-1.5", 
+                "-1.25", "0",    "1.0", 
+                "1.25",  "1.5",  "2.5", 
+                "3.75",  "10.0", "255.5"
+                };
+            long[] expected = {
+                -3, -2, -1,
+                -1, 0,  1,
+                1,  1,  2,
+                3,  10, 255
+                };
+            
+            for (int i = 0; i < data.length; ++i) {
+                Number n = null;
+                try {
+                    n = nf1.parse(data[i]);
+                } catch (ParseException e) {
+                    errln("Failed: " + e.getMessage());
+                }
+                if (!(n instanceof Long) || (n instanceof Integer)) {
+                    errln("Failed: Integer Number Format should parse string to Long/Integer");
+                }
+                if (n.longValue() != expected[i]) {
+                    errln("Failed=> Source: " + data[i] 
+                        + ";result : " + n.toString()
+                        + ";expected :" + Long.toString(expected[i]));
+                }
+            }
+        }
+    }
+    
+    //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)
+    public void TestSerialization() throws IOException{
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//#else
+        byte[][] contents = NumberFormatSerialTestData.getContent();
+        double data = 1234.56;
+        String[] expected = {
+            "1,234.56", "$1,234.56", "123,456%", "1.23456E3"};
+        for (int i = 0; i < 4; ++i) {
+            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i]));
+            try {
+                NumberFormat format = (NumberFormat) ois.readObject();
+                String result = format.format(data);
+                if (result.equals(expected[i])) {
+                    logln("OK: Deserialized bogus NumberFormat(new version read old version)");
+                } else {
+                    errln("FAIL: the test data formats are not euqal");
+                }
+            } catch (Exception e) {
+                warnln("FAIL: " + e.getMessage());
+            }
+        }
+//#endif
+    }
+
+    /*
+     * Test case for JB#5509, strict parsing issue
+     */
+    public void TestJB5509() {
+        String[] data = {
+                "1,2",
+                "1.2",
+                "1,2.5",
+                "1,23.5",
+                "1,234.5",
+                "1,234",
+                "1,234,567",
+                "1,234,567.8",
+                "1,234,5",
+                "1,234,5.6",
+                "1,234,56.7"
+        };
+        boolean[] expected = { // false for expected parse failure
+                false,
+                true,
+                false,
+                false,
+                true,
+                true,
+                true,
+                true,
+                false,
+                false,
+                false,
+                false
+        };
+
+        DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));
+        df.setParseStrict(true);
+        for (int i = 0; i < data.length; i++) {
+            try {
+                df.parse(data[i]);
+                if (!expected[i]) {
+                    errln("Failed: ParseException must be thrown for string " + data[i]);
+                }
+            } catch (ParseException pe) {
+                if (expected[i]) {
+                    errln("Failed: ParseException must not be thrown for string " + data[i]);
+                }
+            }
+        }
+    }
+
+    /*
+     * Test case for ticket#5698 - parsing extremely large/small values
+     */
+    public void TestT5698() {
+        final String[] data = {
+                "12345679E66666666666666666",
+                "-12345679E66666666666666666",
+                ".1E2147483648", // exponent > max int
+                ".1E2147483647", // exponent == max int
+                ".1E-2147483648", // exponent == min int
+                ".1E-2147483649", // exponent < min int
+                "1.23E350", // value > max double
+                "1.23E300", // value < max double
+                "-1.23E350", // value < min double
+                "-1.23E300", // value > min double
+                "4.9E-324", // value = smallest non-zero double
+                "1.0E-325", // 0 < value < smallest non-zero positive double0
+                "-1.0E-325", // 0 > value > largest non-zero negative double
+        };
+        final double[] expected = {
+                Double.POSITIVE_INFINITY,
+                Double.NEGATIVE_INFINITY,
+                Double.POSITIVE_INFINITY,
+                Double.POSITIVE_INFINITY,
+                0.0,
+                0.0,
+                Double.POSITIVE_INFINITY,
+                1.23e300d,
+                Double.NEGATIVE_INFINITY,
+                -1.23e300d,
+                4.9e-324d,
+                0.0,
+                -0.0,
+        };
+
+        NumberFormat nfmt = NumberFormat.getInstance();
+
+        for (int i = 0; i < data.length; i++) {
+            try {
+                Number n = nfmt.parse(data[i]);
+                if (expected[i] != n.doubleValue()) {
+                    errln("Failed: Parsed result for " + data[i] + ": " 
+                            + n.doubleValue() + " / expected: " + expected[i]);
+                }
+            } catch (ParseException pe) {
+                errln("Failed: ParseException is thrown for " + data[i]);
+            }
+        }
+    }
+    
+    void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) {
+        String myString = nf.format(myNumber);
+        
+            double aNumber;
+            try {
+                aNumber = nf.parse(myString).doubleValue();
+            } catch (ParseException e) {
+                // TODO Auto-generated catch block
+                errln("FAIL: " + testcase +" - failed to parse. " + e.toString());
+                return;
+            }
+            if(Math.abs(aNumber-myNumber)>.001) {
+            errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
+        } else {
+            logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
+        }
+    }
+
+    void checkNBSPPatternRT(String testcase, NumberFormat nf) {
+        checkNBSPPatternRtNum(testcase, nf, 12345.);
+        checkNBSPPatternRtNum(testcase, nf, -12345.);
+    }
+
+    public void TestNBSPInPattern() {
+    NumberFormat nf = null;
+    String testcase;
+    
+    
+    testcase="ar_AE UNUM_CURRENCY";
+    nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
+    checkNBSPPatternRT(testcase, nf);
+    // if we don't have CLDR 1.6 data, bring out the problem anyways 
+    
+    String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
+    testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
+    nf = new DecimalFormat();
+    ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN);
+    checkNBSPPatternRT(testcase, nf);
+    
+    }
+    
+}