< prev index next >

test/java/text/Format/NumberFormat/NumberRegression.java

Print this page




 271         if (!buffer.equals("(1\u00a0234,00)"))
 272             errln("nf : " + buffer); // Expect (1 234,00)
 273 
 274         // Erroneously prints:
 275         // 1234,00 ,
 276         // (1234,00 ,)
 277 
 278     }
 279     /**
 280      * DecimalFormat.parse returns wrong value
 281      */
 282     public void Test4068693()
 283     {
 284         Locale savedLocale = Locale.getDefault();
 285         Locale.setDefault(Locale.US);
 286         logln("----- Test Application -----");
 287         ParsePosition pos;
 288         DecimalFormat df = new DecimalFormat();
 289         Double d = (Double)df.parse("123.55456", pos=new ParsePosition(0));
 290         if (!d.toString().equals("123.55456")) {
 291             errln("Result -> " + d.doubleValue());
 292         }
 293         Locale.setDefault(savedLocale);
 294     }
 295 
 296     /* bugs 4069754, 4067878
 297      * null pointer thrown when accessing a deserialized DecimalFormat
 298      * object.
 299      */
 300     public void Test4069754()
 301     {
 302         try {
 303             myformat it = new myformat();
 304             logln(it.Now());
 305             FileOutputStream ostream = new FileOutputStream("t.tmp");
 306             ObjectOutputStream p = new ObjectOutputStream(ostream);
 307             p.writeObject(it);
 308             ostream.close();
 309             logln("Saved ok.");
 310 
 311             FileInputStream istream = new FileInputStream("t.tmp");


 377             for (int i = 17; i <= 20; i++) {
 378                 df.setMaximumFractionDigits(i);
 379                 sb = new StringBuffer("");
 380                 fp = new FieldPosition(0);
 381                 logln("  getMaximumFractionDigits() = " + i);
 382                 logln("  formated: " + df.format(d, sb, fp));
 383             }
 384         } catch (Exception foo) {
 385             errln("Bug 4090504 regression test failed. Message : " + foo.getMessage());
 386         }
 387     }
 388     /**
 389      * DecimalFormat.parse(String str, ParsePosition pp) loses precision
 390      */
 391     public void Test4095713 ()
 392     {
 393         Locale savedLocale = Locale.getDefault();
 394         Locale.setDefault(Locale.US);
 395         DecimalFormat df = new DecimalFormat();
 396         String str = "0.1234";
 397         Double d1 = new Double(str);
 398         Double d2 = (Double) df.parse(str, new ParsePosition(0));
 399         logln(d1.toString());
 400         if (d2.doubleValue() != d1.doubleValue())
 401             errln("Bug 4095713 test failed, new double value : " + d2.doubleValue());
 402         Locale.setDefault(savedLocale);
 403     }
 404 
 405     /**
 406      * DecimalFormat.parse() fails when multiplier is not set to 1
 407      */
 408     public void Test4092561 ()
 409     {
 410         Locale savedLocale = Locale.getDefault();
 411         Locale.setDefault(Locale.US);
 412         DecimalFormat df = new DecimalFormat();
 413 
 414         String str = Long.toString(Long.MIN_VALUE);
 415         logln("Long.MIN_VALUE : " + df.parse(str, new ParsePosition(0)).toString());
 416         df.setMultiplier(100);
 417         Number num = df.parse(str, new ParsePosition(0));
 418         if (num.doubleValue() != -9.223372036854776E16) {
 419             errln("Bug 4092561 test failed when multiplier is not set to 1. Expected: -9.223372036854776E16, got: " + num.doubleValue());
 420         }
 421 


 852         fmt.setMonetaryDecimalSeparator('*');
 853         currency = fmt.getCurrencySymbol();
 854         intlCurrency = fmt.getInternationalCurrencySymbol();
 855         monDecSeparator = fmt.getMonetaryDecimalSeparator();
 856         if (!currency.equals("XYZ") ||
 857             !intlCurrency.equals("ABC") ||
 858             monDecSeparator != '*') {
 859             errln("setCurrencySymbols failed.");
 860         }
 861         logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
 862     }
 863     /**
 864      * API tests for API addition request A23. FieldPosition.getBeginIndex and
 865      * FieldPosition.getEndIndex.
 866      */
 867     public void Test4062486()
 868     {
 869         DecimalFormat fmt = new DecimalFormat("#,##0.00");
 870         StringBuffer formatted = new StringBuffer();
 871         FieldPosition field = new FieldPosition(0);
 872         Double num = new Double(1234.5);
 873         fmt.format(num, formatted, field);
 874         if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
 875             errln("Format 1234.5 failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
 876         field.setBeginIndex(7);
 877         field.setEndIndex(4);
 878         if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
 879             errln("Set begin/end field indexes failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
 880     }
 881 
 882     /**
 883      * DecimalFormat.parse incorrectly works with a group separator.
 884      */
 885     public void Test4108738()
 886     {
 887 
 888         DecimalFormat df = new DecimalFormat("#,##0.###",
 889         DecimalFormatSymbols.getInstance(java.util.Locale.US));
 890         String text = "1.222,111";
 891         Number num = df.parse(text,new ParsePosition(0));
 892         if (!num.toString().equals("1.222"))


1398             String s = df.toPattern();
1399             if (!s.equals(DATA[i+1])) {
1400                 errln("FAIL: " + DATA[i] + " -> " + s + ", want " + DATA[i+1]);
1401             }
1402         }
1403     }
1404 
1405     /**
1406      * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
1407      */
1408     public void Test4179818() {
1409         String DATA[] = {
1410             // Input  Pattern  Expected output
1411             "1.2511", "#.#",   "1.3",
1412             "1.2501", "#.#",   "1.3",
1413             "0.9999", "#",     "1",
1414         };
1415         DecimalFormat fmt = new DecimalFormat("#",
1416                 DecimalFormatSymbols.getInstance(Locale.US));
1417         for (int i=0; i<DATA.length; i+=3) {
1418             double in = Double.valueOf(DATA[i]).doubleValue();
1419             String pat = DATA[i+1];
1420             String exp = DATA[i+2];
1421             fmt.applyPattern(pat);
1422             String out = fmt.format(in);
1423             if (out.equals(exp)) {
1424                 logln("Ok: " + in + " x " + pat + " = " + out);
1425             } else {
1426                 errln("FAIL: " + in + " x  " + pat + " = " + out +
1427                       ", expected " + exp);
1428             }
1429         }
1430     }
1431 
1432     public void Test4185761() throws IOException, ClassNotFoundException {
1433         /* Code used to write out the initial files, which are
1434          * then edited manually:
1435         NumberFormat nf = NumberFormat.getInstance(Locale.US);
1436         nf.setMinimumIntegerDigits(0x111); // Keep under 309
1437         nf.setMaximumIntegerDigits(0x112); // Keep under 309
1438         nf.setMinimumFractionDigits(0x113); // Keep under 340


1604                 if (!df.equals(f2)) {
1605                     errln("FAIL: Stream in/out " + avail[i] + " -> \"" + pat +
1606                           "\" -> " +
1607                           (f2 != null ? ("\""+f2.toPattern()+'"') : "null"));
1608                 }
1609 
1610             }
1611         }
1612     }
1613 
1614     /**
1615      * DecimalFormat.parse() fails for mulipliers 2^n.
1616      */
1617     public void Test4216742() throws ParseException {
1618         DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(Locale.US);
1619         long[] DATA = { Long.MIN_VALUE, Long.MAX_VALUE, -100000000L, 100000000L};
1620         for (int i=0; i<DATA.length; ++i) {
1621             String str = Long.toString(DATA[i]);
1622             for (int m = 1; m <= 100; m++) {
1623                 fmt.setMultiplier(m);
1624                 long n = ((Number) fmt.parse(str)).longValue();
1625                 if (n > 0 != DATA[i] > 0) {
1626                     errln("\"" + str + "\" parse(x " + fmt.getMultiplier() +
1627                           ") => " + n);
1628                 }
1629             }
1630         }
1631     }
1632 
1633     /**
1634      * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
1635      * digits.
1636      */
1637     public void Test4217661() {
1638         Object[] DATA = {
1639             new Double(0.001), "0",
1640             new Double(1.001), "1",
1641             new Double(0.006), "0.01",
1642             new Double(1.006), "1.01",
1643         };
1644         NumberFormat fmt = NumberFormat.getInstance(Locale.US);
1645         fmt.setMaximumFractionDigits(2);
1646         for (int i=0; i<DATA.length; i+=2) {
1647             String s = fmt.format(((Double) DATA[i]).doubleValue());
1648             if (!s.equals(DATA[i+1])) {
1649                 errln("FAIL: Got " + s + ", exp " + DATA[i+1]);
1650             }
1651         }
1652     }
1653 
1654     /**
1655      * 4243011: Formatting .5 rounds to "1" instead of "0"
1656      */
1657     public void Test4243011() {
1658         Locale savedLocale = Locale.getDefault();
1659         Locale.setDefault(Locale.US);
1660         double DATA[] = {0.5, 1.5, 2.5, 3.5, 4.5};
1661         String EXPECTED[] = {"0.", "2.", "2.", "4.", "4."};
1662 
1663         DecimalFormat format = new DecimalFormat("0.");
1664         for (int i = 0; i < DATA.length; i++) {
1665             String result = format.format(DATA[i]);
1666             if (result.equals(EXPECTED[i])) {
1667                 logln("OK: got " + result);


1786         String[] expected = {
1787                 "2%", "1%", "2%", "2%", "1%",
1788                 "0%", "0%", "1%", "1%", "1%",
1789                 "0", "2", "0.2", "0.6", "0.04",
1790                 "0.04", "0.000", "0.002",
1791         };
1792         for (int i = 0; i < input.length; i++) {
1793             DecimalFormat format = new DecimalFormat(pattern[i]);
1794             String result = format.format(input[i]);
1795             if (!result.equals(expected[i])) {
1796                 errln("FAIL: input: " + input[i] +
1797                         ", pattern: " + pattern[i] +
1798                         ", expected: " + expected[i] +
1799                         ", got: " + result);
1800             }
1801         }
1802         Locale.setDefault(savedLocale);
1803     }
1804 }
1805 

1806 class myformat implements Serializable
1807 {
1808     DateFormat _dateFormat = DateFormat.getDateInstance();
1809 
1810     public String Now()
1811     {
1812         GregorianCalendar calendar = new GregorianCalendar();
1813         Date t = calendar.getTime();
1814         String nowStr = _dateFormat.format(t);
1815         return nowStr;
1816     }
1817 }
1818 

1819 class MyNumberFormatTest extends NumberFormat {
1820     public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
1821         return new StringBuffer("");
1822     }
1823     public StringBuffer format(long number,StringBuffer toAppendTo, FieldPosition pos) {
1824         return new StringBuffer("");
1825     }
1826     public Number parse(String text, ParsePosition parsePosition) {
1827         return new Integer(0);
1828     }
1829 }


 271         if (!buffer.equals("(1\u00a0234,00)"))
 272             errln("nf : " + buffer); // Expect (1 234,00)
 273 
 274         // Erroneously prints:
 275         // 1234,00 ,
 276         // (1234,00 ,)
 277 
 278     }
 279     /**
 280      * DecimalFormat.parse returns wrong value
 281      */
 282     public void Test4068693()
 283     {
 284         Locale savedLocale = Locale.getDefault();
 285         Locale.setDefault(Locale.US);
 286         logln("----- Test Application -----");
 287         ParsePosition pos;
 288         DecimalFormat df = new DecimalFormat();
 289         Double d = (Double)df.parse("123.55456", pos=new ParsePosition(0));
 290         if (!d.toString().equals("123.55456")) {
 291             errln("Result -> " + d);
 292         }
 293         Locale.setDefault(savedLocale);
 294     }
 295 
 296     /* bugs 4069754, 4067878
 297      * null pointer thrown when accessing a deserialized DecimalFormat
 298      * object.
 299      */
 300     public void Test4069754()
 301     {
 302         try {
 303             myformat it = new myformat();
 304             logln(it.Now());
 305             FileOutputStream ostream = new FileOutputStream("t.tmp");
 306             ObjectOutputStream p = new ObjectOutputStream(ostream);
 307             p.writeObject(it);
 308             ostream.close();
 309             logln("Saved ok.");
 310 
 311             FileInputStream istream = new FileInputStream("t.tmp");


 377             for (int i = 17; i <= 20; i++) {
 378                 df.setMaximumFractionDigits(i);
 379                 sb = new StringBuffer("");
 380                 fp = new FieldPosition(0);
 381                 logln("  getMaximumFractionDigits() = " + i);
 382                 logln("  formated: " + df.format(d, sb, fp));
 383             }
 384         } catch (Exception foo) {
 385             errln("Bug 4090504 regression test failed. Message : " + foo.getMessage());
 386         }
 387     }
 388     /**
 389      * DecimalFormat.parse(String str, ParsePosition pp) loses precision
 390      */
 391     public void Test4095713 ()
 392     {
 393         Locale savedLocale = Locale.getDefault();
 394         Locale.setDefault(Locale.US);
 395         DecimalFormat df = new DecimalFormat();
 396         String str = "0.1234";
 397         Double d1 = 0.1234;
 398         Double d2 = (Double) df.parse(str, new ParsePosition(0));
 399         logln(d1.toString());
 400         if (d2.doubleValue() != d1.doubleValue())
 401             errln("Bug 4095713 test failed, new double value : " + d2);
 402         Locale.setDefault(savedLocale);
 403     }
 404 
 405     /**
 406      * DecimalFormat.parse() fails when multiplier is not set to 1
 407      */
 408     public void Test4092561 ()
 409     {
 410         Locale savedLocale = Locale.getDefault();
 411         Locale.setDefault(Locale.US);
 412         DecimalFormat df = new DecimalFormat();
 413 
 414         String str = Long.toString(Long.MIN_VALUE);
 415         logln("Long.MIN_VALUE : " + df.parse(str, new ParsePosition(0)).toString());
 416         df.setMultiplier(100);
 417         Number num = df.parse(str, new ParsePosition(0));
 418         if (num.doubleValue() != -9.223372036854776E16) {
 419             errln("Bug 4092561 test failed when multiplier is not set to 1. Expected: -9.223372036854776E16, got: " + num.doubleValue());
 420         }
 421 


 852         fmt.setMonetaryDecimalSeparator('*');
 853         currency = fmt.getCurrencySymbol();
 854         intlCurrency = fmt.getInternationalCurrencySymbol();
 855         monDecSeparator = fmt.getMonetaryDecimalSeparator();
 856         if (!currency.equals("XYZ") ||
 857             !intlCurrency.equals("ABC") ||
 858             monDecSeparator != '*') {
 859             errln("setCurrencySymbols failed.");
 860         }
 861         logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
 862     }
 863     /**
 864      * API tests for API addition request A23. FieldPosition.getBeginIndex and
 865      * FieldPosition.getEndIndex.
 866      */
 867     public void Test4062486()
 868     {
 869         DecimalFormat fmt = new DecimalFormat("#,##0.00");
 870         StringBuffer formatted = new StringBuffer();
 871         FieldPosition field = new FieldPosition(0);
 872         Double num = 1234.5;
 873         fmt.format(num, formatted, field);
 874         if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
 875             errln("Format 1234.5 failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
 876         field.setBeginIndex(7);
 877         field.setEndIndex(4);
 878         if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
 879             errln("Set begin/end field indexes failed. Begin index: " + field.getBeginIndex() + " End index: " + field.getEndIndex());
 880     }
 881 
 882     /**
 883      * DecimalFormat.parse incorrectly works with a group separator.
 884      */
 885     public void Test4108738()
 886     {
 887 
 888         DecimalFormat df = new DecimalFormat("#,##0.###",
 889         DecimalFormatSymbols.getInstance(java.util.Locale.US));
 890         String text = "1.222,111";
 891         Number num = df.parse(text,new ParsePosition(0));
 892         if (!num.toString().equals("1.222"))


1398             String s = df.toPattern();
1399             if (!s.equals(DATA[i+1])) {
1400                 errln("FAIL: " + DATA[i] + " -> " + s + ", want " + DATA[i+1]);
1401             }
1402         }
1403     }
1404 
1405     /**
1406      * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
1407      */
1408     public void Test4179818() {
1409         String DATA[] = {
1410             // Input  Pattern  Expected output
1411             "1.2511", "#.#",   "1.3",
1412             "1.2501", "#.#",   "1.3",
1413             "0.9999", "#",     "1",
1414         };
1415         DecimalFormat fmt = new DecimalFormat("#",
1416                 DecimalFormatSymbols.getInstance(Locale.US));
1417         for (int i=0; i<DATA.length; i+=3) {
1418             double in = Double.valueOf(DATA[i]);
1419             String pat = DATA[i+1];
1420             String exp = DATA[i+2];
1421             fmt.applyPattern(pat);
1422             String out = fmt.format(in);
1423             if (out.equals(exp)) {
1424                 logln("Ok: " + in + " x " + pat + " = " + out);
1425             } else {
1426                 errln("FAIL: " + in + " x  " + pat + " = " + out +
1427                       ", expected " + exp);
1428             }
1429         }
1430     }
1431 
1432     public void Test4185761() throws IOException, ClassNotFoundException {
1433         /* Code used to write out the initial files, which are
1434          * then edited manually:
1435         NumberFormat nf = NumberFormat.getInstance(Locale.US);
1436         nf.setMinimumIntegerDigits(0x111); // Keep under 309
1437         nf.setMaximumIntegerDigits(0x112); // Keep under 309
1438         nf.setMinimumFractionDigits(0x113); // Keep under 340


1604                 if (!df.equals(f2)) {
1605                     errln("FAIL: Stream in/out " + avail[i] + " -> \"" + pat +
1606                           "\" -> " +
1607                           (f2 != null ? ("\""+f2.toPattern()+'"') : "null"));
1608                 }
1609 
1610             }
1611         }
1612     }
1613 
1614     /**
1615      * DecimalFormat.parse() fails for mulipliers 2^n.
1616      */
1617     public void Test4216742() throws ParseException {
1618         DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(Locale.US);
1619         long[] DATA = { Long.MIN_VALUE, Long.MAX_VALUE, -100000000L, 100000000L};
1620         for (int i=0; i<DATA.length; ++i) {
1621             String str = Long.toString(DATA[i]);
1622             for (int m = 1; m <= 100; m++) {
1623                 fmt.setMultiplier(m);
1624                 long n = fmt.parse(str).longValue();
1625                 if (n > 0 != DATA[i] > 0) {
1626                     errln("\"" + str + "\" parse(x " + fmt.getMultiplier() +
1627                           ") => " + n);
1628                 }
1629             }
1630         }
1631     }
1632 
1633     /**
1634      * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
1635      * digits.
1636      */
1637     public void Test4217661() {
1638         Object[] DATA = {
1639             0.001, "0",
1640             1.001, "1",
1641             0.006, "0.01",
1642             1.006, "1.01",
1643         };
1644         NumberFormat fmt = NumberFormat.getInstance(Locale.US);
1645         fmt.setMaximumFractionDigits(2);
1646         for (int i=0; i<DATA.length; i+=2) {
1647             String s = fmt.format((Double) DATA[i]);
1648             if (!s.equals(DATA[i+1])) {
1649                 errln("FAIL: Got " + s + ", exp " + DATA[i+1]);
1650             }
1651         }
1652     }
1653 
1654     /**
1655      * 4243011: Formatting .5 rounds to "1" instead of "0"
1656      */
1657     public void Test4243011() {
1658         Locale savedLocale = Locale.getDefault();
1659         Locale.setDefault(Locale.US);
1660         double DATA[] = {0.5, 1.5, 2.5, 3.5, 4.5};
1661         String EXPECTED[] = {"0.", "2.", "2.", "4.", "4."};
1662 
1663         DecimalFormat format = new DecimalFormat("0.");
1664         for (int i = 0; i < DATA.length; i++) {
1665             String result = format.format(DATA[i]);
1666             if (result.equals(EXPECTED[i])) {
1667                 logln("OK: got " + result);


1786         String[] expected = {
1787                 "2%", "1%", "2%", "2%", "1%",
1788                 "0%", "0%", "1%", "1%", "1%",
1789                 "0", "2", "0.2", "0.6", "0.04",
1790                 "0.04", "0.000", "0.002",
1791         };
1792         for (int i = 0; i < input.length; i++) {
1793             DecimalFormat format = new DecimalFormat(pattern[i]);
1794             String result = format.format(input[i]);
1795             if (!result.equals(expected[i])) {
1796                 errln("FAIL: input: " + input[i] +
1797                         ", pattern: " + pattern[i] +
1798                         ", expected: " + expected[i] +
1799                         ", got: " + result);
1800             }
1801         }
1802         Locale.setDefault(savedLocale);
1803     }
1804 }
1805 
1806 @SuppressWarnings("serial")
1807 class myformat implements Serializable
1808 {
1809     DateFormat _dateFormat = DateFormat.getDateInstance();
1810 
1811     public String Now()
1812     {
1813         GregorianCalendar calendar = new GregorianCalendar();
1814         Date t = calendar.getTime();
1815         String nowStr = _dateFormat.format(t);
1816         return nowStr;
1817     }
1818 }
1819 
1820 @SuppressWarnings("serial")
1821 class MyNumberFormatTest extends NumberFormat {
1822     public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
1823         return new StringBuffer("");
1824     }
1825     public StringBuffer format(long number,StringBuffer toAppendTo, FieldPosition pos) {
1826         return new StringBuffer("");
1827     }
1828     public Number parse(String text, ParsePosition parsePosition) {
1829         return 0;
1830     }
1831 }
< prev index next >