home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / numrgts.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  75.2 KB  |  2,309 lines

  1. /*
  2. ********************************************************************
  3. * COPYRIGHT: 
  4. * (C) Copyright International Business Machines Corporation, 1998
  5. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  6. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  7. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  8. *
  9. ********************************************************************
  10. */
  11.  
  12. #include "numrgts.h"
  13.  
  14. #include <stdio.h> // sprintf
  15. #include <float.h> // DBL_MIN, DBL_MAX
  16. #include <limits.h> // LONG_MIN, LONG_MAX
  17.  
  18. #include "dcfmtsym.h"
  19. #include "decimfmt.h"
  20. #include "locid.h"
  21. #include "resbund.h"
  22.  
  23.  
  24. // *****************************************************************************
  25. // class NumberFormatRegressionTest
  26. // *****************************************************************************
  27.  
  28. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  29.  
  30. void 
  31. NumberFormatRegressionTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  32. {
  33.     // if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
  34.     switch (index) {
  35.         CASE(0,Test4075713)
  36.         CASE(1,Test4074620)
  37.         CASE(2,Test4088161)
  38.         CASE(3,Test4087245)
  39.         CASE(4,Test4087535)
  40.         CASE(5,Test4088503)
  41.         CASE(6,Test4066646)
  42.         CASE(7,Test4059870)
  43.         CASE(8,Test4083018)
  44.         CASE(9,Test4071492)
  45.         CASE(10,Test4086575)
  46.         CASE(11,Test4068693)
  47.         CASE(12,Test4069754)
  48.         CASE(13,Test4087251)
  49.         CASE(14,Test4090489)
  50.         CASE(15,Test4090504)
  51.         CASE(16,Test4095713)
  52.         CASE(17,Test4092561)
  53.         CASE(18,Test4092480)
  54.         CASE(19,Test4087244)
  55.         CASE(20,Test4070798)
  56.         CASE(21,Test4071005)
  57.         CASE(22,Test4071014)
  58.         CASE(23,Test4071859)
  59.         CASE(24,Test4093610)
  60.         CASE(25,Test4098741)
  61.         CASE(26,Test4074454)
  62.         CASE(27,Test4099404)
  63.         CASE(28,Test4101481)
  64.         CASE(29,Test4052223)
  65.         CASE(30,Test4061302)
  66.         CASE(31,Test4062486)
  67.         CASE(32,Test4108738)
  68.         CASE(33,Test4106658)
  69.         CASE(34,Test4106662)
  70.         CASE(35,Test4114639)
  71.         CASE(36,Test4106664)
  72.         CASE(37,Test4106667)
  73.         CASE(38,Test4110936)
  74.         CASE(39,Test4122840)
  75.         CASE(40,Test4125885)
  76.         CASE(41,Test4134034)
  77.         CASE(42,Test4134300)
  78.         CASE(43,Test4140009)
  79.         CASE(44,Test4141750)
  80.         CASE(45,Test4145457)
  81.         CASE(46,Test4147295)
  82.         CASE(47,Test4147706)
  83.         CASE(48,Test4162198)
  84.         CASE(49,Test4162852)
  85.         CASE(50,Test4167494)
  86.         CASE(51,Test4170798)
  87.         CASE(52,Test4176114)
  88.         CASE(53,Test4179818)
  89.         CASE(54,Test4212072)
  90.         CASE(55,Test4216742)
  91.         CASE(56,Test4217661)
  92.         CASE(57,Test4161100)
  93.         CASE(58,Test4243011)
  94.         CASE(59,Test4243108)
  95.  
  96.         default: name = ""; break;
  97.     }
  98. }
  99.  
  100. bool_t 
  101. NumberFormatRegressionTest::failure(UErrorCode status, const UnicodeString& msg)
  102. {
  103.     if(U_FAILURE(status)) {
  104.         errln(UnicodeString("FAIL: ") + msg + " failed, error " + errorName(status));
  105.         return TRUE;
  106.     }
  107.  
  108.     return FALSE;
  109. }
  110.  
  111. /**
  112.  * Convert Java-style strings with \u Unicode escapes into UnicodeString objects
  113.  */
  114. static UnicodeString str(const char *input)
  115. {
  116.   static const UnicodeString digitString1("0123456789ABCDEF");
  117.   static const UnicodeString digitString2("0123456789abcdef");
  118.   
  119.   UnicodeString result(input);
  120.   int index = 0;
  121.   
  122.   while ((index = result.indexOf("\\u")) != -1)
  123.     {
  124.       if (index + 6 <= result.size())
  125.     {
  126.       UChar c = 0;
  127.       for (int i = index + 2; i < index + 6; i++) {
  128.         UTextOffset value = digitString1.indexOf(result[i]);
  129.         
  130.         if (value == -1) {
  131.           value = digitString2.indexOf(result[i]);
  132.         }
  133.         c = (UChar)(c * 16 + value);
  134.       }
  135.       UnicodeString replace;
  136.       replace += c;
  137.       result.replace(index, 6, replace);
  138.     }
  139.       index += 1;
  140.     }
  141.  
  142.   //cout << "Converted |" << input << "| to ";
  143.   //print(result,"");
  144.  
  145.   return result;
  146. }
  147.  
  148. /* @bug 4075713
  149.  * NumberFormat.equals comparing with null should always return false.
  150.  */
  151. // {sfb} kind of silly in C++, just checking for new success
  152. void NumberFormatRegressionTest::Test4075713()
  153. {
  154.     //try {
  155.         MyNumberFormatTest *tmp = new MyNumberFormatTest();
  156.         if(tmp != NULL)
  157.             logln("NumberFormat.equals passed");
  158.     /*} catch (NullPointerException e) {
  159.         errln("(new MyNumberFormatTest()).equals(null) throws unexpected exception");
  160.     }*/
  161.     
  162.     delete tmp;
  163. }
  164.  
  165. /* @bug 4074620
  166.  * NumberFormat.equals comparing two obj equal even the setGroupingUsed
  167.  * flag is different.
  168.  */
  169. void NumberFormatRegressionTest::Test4074620() 
  170. {
  171.  
  172.     MyNumberFormatTest *nf1 = new MyNumberFormatTest();
  173.     MyNumberFormatTest *nf2 = new MyNumberFormatTest();
  174.  
  175.     nf1->setGroupingUsed(FALSE);
  176.     nf2->setGroupingUsed(TRUE);
  177.  
  178.     if(nf1 == nf2) 
  179.         errln("Test for bug 4074620 failed");
  180.     else 
  181.         logln("Test for bug 4074620 passed.");
  182.     
  183.     delete nf1;
  184.     delete nf2;
  185. }
  186.  
  187.  
  188. /* @bug 4088161
  189.  * DecimalFormat.format() incorrectly uses maxFractionDigits setting.
  190.  */
  191.  
  192. void NumberFormatRegressionTest::Test4088161 ()
  193. {
  194.     UErrorCode status = U_ZERO_ERROR;
  195.     DecimalFormat *df = new DecimalFormat(status);
  196.     failure(status, "new DecimalFormat");
  197.     double d = 100;
  198.     df->setMinimumFractionDigits(0);
  199.     df->setMaximumFractionDigits(16);
  200.     UnicodeString sBuf1;
  201.     FieldPosition fp1(0);
  202.     logln(UnicodeString("d = ") + d);
  203.     logln("maxFractionDigits = " + df->getMaximumFractionDigits());
  204.     
  205.     logln(" format(d) = '" + df->format(d, sBuf1, fp1) + "'");
  206.     df->setMaximumFractionDigits(17);
  207.     UnicodeString sBuf2;
  208.     FieldPosition fp2(0);
  209.     logln("maxFractionDigits = " + df->getMaximumFractionDigits());
  210.     sBuf2 = df->format(d, sBuf2, fp2);
  211.     if(sBuf2 != "100")
  212.         errln(" format(d) = '" + sBuf2 + "'");
  213.  
  214.     delete df;
  215. }
  216.  
  217. /* @bug 4087245
  218.  * DecimalFormatSymbols should be cloned in the ctor DecimalFormat.
  219.  * DecimalFormat(String, DecimalFormatSymbols).
  220.  */
  221. void NumberFormatRegressionTest::Test4087245 ()
  222. {
  223.     UErrorCode status = U_ZERO_ERROR;
  224.     DecimalFormatSymbols *symbols = new DecimalFormatSymbols(status);
  225.     failure(status, "new DecimalFormatSymbols");
  226.     // {sfb} One note about this test: if you pass in a pointer
  227.     // to the symbols, they are adopted and this test will fail,
  228.     // even though that is the correct behavior.  To test the cloning
  229.     // of the symbols, it is necessary to pass in a reference to the symbols
  230.     DecimalFormat *df = new DecimalFormat("#,##0.0", *symbols, status);
  231.     failure(status, "new DecimalFormat with symbols");
  232.     int32_t n = 123;
  233.     UnicodeString buf1;
  234.     UnicodeString buf2;
  235.     FieldPosition pos(FieldPosition::DONT_CARE);
  236.     logln(UnicodeString("format(") + n + ") = " + 
  237.         df->format(n, buf1, pos));
  238.     symbols->setDecimalSeparator(UChar('p')); // change value of field
  239.     logln(UnicodeString("format(") + n + ") = " +
  240.         df->format(n, buf2, pos));
  241.     if(buf1 != buf2)
  242.         errln("Test for bug 4087245 failed");
  243.  
  244.     delete df;
  245. }
  246.  
  247. /* @bug 4087535
  248.  * DecimalFormat.format() incorrectly formats 0.0
  249.  */
  250. void NumberFormatRegressionTest::Test4087535 ()
  251. {
  252.     UErrorCode status = U_ZERO_ERROR;
  253.     DecimalFormat *df = new DecimalFormat(status);
  254.     failure(status, "new DecimalFormat");
  255.     df->setMinimumIntegerDigits(0);
  256.  
  257.     double n = 0;
  258.     UnicodeString buffer;
  259.     FieldPosition pos(FieldPosition::DONT_CARE);
  260.     buffer = df->format(n, buffer, pos);
  261.     if (buffer.size() == 0)
  262.         errln(/*n + */": '" + buffer + "'");
  263.     n = 0.1;
  264.     buffer = df->format(n, buffer, pos);
  265.     if (buffer.size() == 0)
  266.         errln(/*n + */": '" + buffer + "'");
  267.  
  268.     delete df;
  269. }
  270.  
  271. /* @bug 4088503
  272.  * DecimalFormat.format fails when groupingSize is set to 0.
  273.  */
  274. // {sfb} how do I tell if this worked? --> FieldPosition doesn't change ??
  275. void NumberFormatRegressionTest::Test4088503 ()
  276. {
  277.     UErrorCode status = U_ZERO_ERROR;
  278.     DecimalFormat *df = new DecimalFormat(status);
  279.     failure(status, "new DecimalFormat");
  280.     df->setGroupingSize(0);
  281.     UnicodeString sBuf;
  282.     FieldPosition fp(FieldPosition::DONT_CARE);
  283.     //try {
  284.         logln(df->format((int32_t)123, sBuf, fp));
  285.         //if(fp == FieldPosition(0))
  286.         //    errln("Test for bug 4088503 failed.");
  287.     /*} catch (Exception foo) {
  288.         errln("Test for bug 4088503 failed.");
  289.     }*/
  290.  
  291. }
  292. /* @bug 4066646
  293.  * NumberFormat.getCurrencyInstance is wrong.
  294.  */
  295. void NumberFormatRegressionTest::Test4066646 () 
  296. {
  297.     assignFloatValue(2.04f);
  298.     assignFloatValue(2.03f);
  299.     assignFloatValue(2.02f);
  300.     assignFloatValue(0.0f);
  301. }
  302.  
  303. float 
  304. NumberFormatRegressionTest::assignFloatValue(float returnfloat)
  305. {
  306.     logln(UnicodeString(" VALUE ") + returnfloat);
  307.     UErrorCode status = U_ZERO_ERROR;
  308.     NumberFormat *nfcommon =  NumberFormat::createCurrencyInstance(Locale::US, status);
  309.     failure(status, "NumberFormat::createCurrencyInstance");
  310.     nfcommon->setGroupingUsed(FALSE);
  311.  
  312.     UnicodeString stringValue;
  313.     stringValue = nfcommon->format(returnfloat, stringValue);
  314.     logln(" DISPLAYVALUE " + stringValue);
  315.     Formattable result;
  316.     nfcommon->parse(stringValue, result, status);
  317.     failure(status, "nfcommon->parse");
  318.     float floatResult = (float) (result.getType() == Formattable::kDouble 
  319.                                         ? result.getDouble() : result.getLong());
  320.     if( icu_fabs(floatResult - returnfloat) > 0.0001)
  321.     //String stringValue = nfcommon.format(returnfloat).substring(1);
  322.     //if (Float.valueOf(stringValue).floatValue() != returnfloat)
  323.         errln(UnicodeString("FAIL: expected ") + returnfloat + ", got " + floatResult + " (" + stringValue+")");
  324.     
  325.     delete nfcommon;
  326.     return returnfloat;
  327. } // End Of assignFloatValue()
  328.  
  329. /* @bug 4059870
  330.  * DecimalFormat throws exception when parsing "0"
  331.  */
  332. void NumberFormatRegressionTest::Test4059870() 
  333. {
  334.     UErrorCode status = U_ZERO_ERROR;
  335.     DecimalFormat *format = new DecimalFormat("00", status);
  336.     failure(status, "new Decimalformat");
  337.     //try {
  338.         Formattable result;
  339.         UnicodeString str;
  340.         format->parse(UnicodeString("0"), result, status);
  341.         failure(status, "format->parse");
  342.         
  343.     /*} 
  344.     catch (Exception e) { 
  345.         errln("Test for bug 4059870 failed : " + e); 
  346.     }*/
  347.  
  348.     delete format;
  349. }
  350. /* @bug 4083018
  351.  * DecimalFormatSymbol.equals should always return false when
  352.  * comparing with null.
  353.  */
  354. // {sfb} this is silly in C++
  355. void NumberFormatRegressionTest::Test4083018 ()
  356. {
  357.     UErrorCode status = U_ZERO_ERROR;
  358.     DecimalFormatSymbols *dfs = new DecimalFormatSymbols(status);
  359.     failure(status, "new DecimalFormatSymbols");
  360.     //try {
  361.         if (dfs != NULL)
  362.             logln("Test Passed!");
  363.         else
  364.             errln("Test for bug 4083018 failed");
  365.     /*} catch (Exception foo) {
  366.         errln("Test for bug 4083018 failed => Message : " + foo.getMessage());
  367.     }*/
  368.  
  369.     delete dfs;
  370. }
  371.  
  372. /* @bug 4071492
  373.  * DecimalFormat does not round up correctly.
  374.  */
  375. void NumberFormatRegressionTest::Test4071492 ()
  376. {
  377.     double x = 0.00159999;
  378.     UErrorCode status = U_ZERO_ERROR;
  379.     NumberFormat *nf = NumberFormat::createInstance(status);
  380.     failure(status, "NumberFormat::createInstance");
  381.     nf->setMaximumFractionDigits(4);
  382.     UnicodeString out;
  383.     FieldPosition pos(FieldPosition::DONT_CARE);
  384.     out = nf->format(x, out, pos);
  385.     logln("0.00159999 formats with 4 fractional digits to " + out);
  386.     UnicodeString expected("0.0016");
  387.     if (out != expected)
  388.         errln("FAIL: Expected " + expected);
  389.  
  390.     delete nf;
  391. }
  392.  
  393. /* @bug 4086575
  394.  * A space as a group separator for localized pattern causes
  395.  * wrong format.  WorkAround : use non-breaking space.
  396.  */
  397. void NumberFormatRegressionTest::Test4086575() 
  398. {
  399.     UErrorCode status = U_ZERO_ERROR;
  400.     NumberFormat *nf1 = NumberFormat::createInstance(Locale::FRANCE, status);
  401.     failure(status, "NumberFormat::createInstance");
  402.     
  403.     // C++ workaround to make sure cast works
  404.     // Wouldn't dynamic_cast<DecimalFormat*> be great?
  405.     if(nf1->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  406.         errln("NumberFormat::createInstance returned incorrect type.");
  407.         return;
  408.     }
  409.  
  410.     DecimalFormat *nf = (DecimalFormat*) nf1;
  411.     UnicodeString temp;
  412.     logln("nf toPattern1: " + nf->toPattern(temp));
  413.     logln("nf toLocPattern1: " + nf->toLocalizedPattern(temp));
  414.     
  415.     // No group separator
  416.     logln("...applyLocalizedPattern ###,00;(###,00) ");
  417.     nf->applyLocalizedPattern(UnicodeString("###,00;(###,00)"), status);
  418.     failure(status, "nf->applyLocalizedPattern");
  419.     logln("nf toPattern2: " + nf->toPattern(temp));
  420.     logln("nf toLocPattern2: " + nf->toLocalizedPattern(temp));
  421.  
  422.     FieldPosition pos(FieldPosition::DONT_CARE);
  423.     logln("nf: " + nf->format((int32_t)1234, temp, pos)); // 1234,00
  424.     logln("nf: " + nf->format((int32_t)-1234, temp, pos)); // (1234,00)
  425.  
  426.     // Space as group separator
  427.  
  428.     logln("...applyLocalizedPattern # ###,00;(# ###,00) ");
  429.     // nbsp = \u00a0 = ' '
  430.     //nf->applyLocalizedPattern("#\u00a0###,00;(#\u00a0###,00)");
  431.     UChar patChars[] = {
  432.              '#', 0x00a0, '#', '#', '#', ',', '0', '0', ';', 
  433.         '(', '#', 0x00a0, '#', '#', '#', ',', '0', '0', ')'
  434.     };
  435.     UnicodeString pat(patChars, 19, 19);
  436.     nf->applyLocalizedPattern(pat, status);
  437.     failure(status, "nf->applyLocalizedPattern");
  438.     logln("nf toPattern2: " + nf->toPattern(temp));
  439.     logln("nf toLocPattern2: " + nf->toLocalizedPattern(temp));
  440.     UnicodeString buffer;
  441.     buffer = nf->format((int32_t)1234, buffer, pos);
  442.     //if (buffer != UnicodeString("1\u00a0234,00"))
  443.     UChar c[] = {
  444.         '1', 0x00a0, '2', '3', '4', ',', '0', '0'
  445.     };
  446.     UnicodeString cc(c, 8, 8);
  447.     if (buffer != cc)
  448.         errln("nf : " + buffer); // Expect 1 234,00
  449.     
  450.     buffer.remove();
  451.     buffer = nf->format((int32_t)-1234, buffer, pos);
  452.     UChar c1[] = {
  453.         '(', '1', 0x00a0, '2', '3', '4', ',', '0', '0', ')'
  454.     };
  455.     UnicodeString cc1(c1, 10, 10);
  456.     if (buffer != cc1)
  457.         errln("nf : " + buffer); // Expect (1 234,00)
  458.  
  459.     // Erroneously prints:
  460.     // 1234,00 ,
  461.     // (1234,00 ,)
  462.  
  463.     delete nf1;
  464. }
  465. /* @bug 4068693
  466.  * DecimalFormat.parse returns wrong value
  467.  */
  468. // {sfb} slightly converted into a round-trip test, since in C++
  469. // there is no Double.toString()
  470. void NumberFormatRegressionTest::Test4068693()
  471. {
  472.     logln("----- Test Application -----");
  473.     ParsePosition pos(0);
  474.     UErrorCode status = U_ZERO_ERROR;
  475.     DecimalFormat *df = new DecimalFormat(status);
  476.     failure(status, "new DecimalFormat");
  477.     Formattable d;
  478.     //Double d = (Double)df.parse("123.55456", pos=new ParsePosition(0));
  479.     df->parse(UnicodeString("123.55456"), d, pos);
  480.     //if (!d.toString().equals("123.55456")) {
  481.     UnicodeString dstr;
  482.     df->setMaximumFractionDigits(999);
  483.     df->setMaximumIntegerDigits(999);
  484.     FieldPosition fp(FieldPosition::DONT_CARE);
  485.     dstr = df->format(d.getDouble(), dstr, fp);
  486.     if (dstr != UnicodeString("123.55456")) {
  487.         errln(UnicodeString("Result -> ") + d.getDouble());
  488.     }
  489.  
  490.     delete df;
  491. }
  492.  
  493. /* @bug 4069754, 4067878
  494.  * null pointer thrown when accessing a deserialized DecimalFormat
  495.  * object.
  496.  */
  497. // {sfb} doesn't apply in C++
  498. void NumberFormatRegressionTest::Test4069754()
  499. {
  500. /*    try {
  501.         myformat it = new myformat();
  502.         logln(it.Now());
  503.         FileOutputStream ostream = new FileOutputStream("t.tmp");
  504.         ObjectOutputStream p = new ObjectOutputStream(ostream);
  505.         p.writeObject(it);
  506.         ostream.close();
  507.         logln("Saved ok.");
  508.  
  509.         FileInputStream istream = new FileInputStream("t.tmp");
  510.         ObjectInputStream p2 = new ObjectInputStream(istream);
  511.         myformat it2 = (myformat)p2.readObject();
  512.         logln(it2.Now());
  513.         istream.close();
  514.         logln("Loaded ok.");
  515.     } catch (Exception foo) {
  516.         errln("Test for bug 4069754 or 4057878 failed => Exception: " + foo.getMessage());
  517.     }
  518. */}
  519.  
  520. /* @bug 4087251
  521.  * DecimalFormat.applyPattern(String) allows illegal patterns
  522.  */
  523. void NumberFormatRegressionTest::Test4087251 ()
  524. {
  525.     UErrorCode status = U_ZERO_ERROR;
  526.     DecimalFormat *df = new DecimalFormat(status);
  527.     failure(status, "new DecimalFormat");
  528.     //try {
  529.         df->applyPattern(UnicodeString("#.#.#"), status);
  530.         if( ! U_FAILURE(status))
  531.             errln("df->applyPattern with illegal pattern didn't fail");
  532.         UnicodeString temp;
  533.         logln("toPattern() returns \"" + df->toPattern(temp) + "\"");
  534.         //errln("applyPattern(\"#.#.#\") doesn't throw IllegalArgumentException");
  535.     /*} catch (IllegalArgumentException e) {
  536.         logln("Caught Illegal Argument Error !");
  537.     }*/
  538.     // Second test; added 5/11/98 when reported to fail on 1.2b3
  539.     //try {
  540.         df->applyPattern("#0.0#0#0", status);
  541.         if( ! U_FAILURE(status))
  542.             errln("df->applyPattern with illegal pattern didn't fail");
  543.         logln("toPattern() returns \"" + df->toPattern(temp) + "\"");
  544.         //errln("applyPattern(\"#0.0#0#0\") doesn't throw IllegalArgumentException");
  545.     /*} catch (IllegalArgumentException e) {
  546.         logln("Ok - IllegalArgumentException for #0.0#0#0");
  547.     }*/
  548.  
  549.     delete df;
  550. }
  551.  
  552. /* @bug 4090489
  553.  * DecimalFormat.format() loses precision
  554.  */
  555. void NumberFormatRegressionTest::Test4090489 ()
  556. {
  557. // {sfb} sprintf doesn't correctly handle the double, so there is nothing
  558. // that NumberFormat can do.  For some reason, it does not format the last 1.
  559.  
  560. /*    UErrorCode status = U_ZERO_ERROR;
  561.     DecimalFormat *df = new DecimalFormat(status);
  562.     failure(status, "new DecimalFormat");
  563.     df->setMinimumFractionDigits(10);
  564.     df->setMaximumFractionDigits(999);
  565.     df->setGroupingUsed(FALSE);
  566.     double d = 1.000000000000001E7;
  567.     //BigDecimal bd = new BigDecimal(d);
  568.     UnicodeString sb;
  569.     FieldPosition fp(0);
  570.     logln(UnicodeString("d = ") + d);
  571.     //logln("BigDecimal.toString():  " + bd.toString());
  572.     df->format(d, sb, fp);
  573.     if (sb != "10000000.0000000100") {
  574.         errln("DecimalFormat.format(): " + sb);
  575.     }
  576. */
  577. }
  578.  
  579. /* @bug 4090504
  580.  * DecimalFormat.format() loses precision
  581.  */
  582. void NumberFormatRegressionTest::Test4090504 ()
  583. {
  584.     double d = 1;
  585.     logln(UnicodeString("d = ") + d);
  586.     UErrorCode status = U_ZERO_ERROR;
  587.     DecimalFormat *df = new DecimalFormat(status);
  588.     failure(status, "new DecimalFormat");
  589.     UnicodeString sb;
  590.     FieldPosition fp(FieldPosition::DONT_CARE);
  591.     //try {
  592.         for (int i = 17; i <= 20; i++) {
  593.             df->setMaximumFractionDigits(i);
  594.             //sb = new StringBuffer("");
  595.             fp.setField(0);
  596.             logln("  getMaximumFractionDigits() = " + i);
  597.             logln("  formated: " + df->format(d, sb, fp));
  598.         }
  599.     /*} catch (Exception foo) {
  600.         errln("Bug 4090504 regression test failed. Message : " + foo.getMessage());
  601.     }*/
  602.  
  603.     delete df;
  604. }
  605. /* @bug 4095713
  606.  * DecimalFormat.parse(String str, ParsePosition pp) loses precision
  607.  */
  608. void NumberFormatRegressionTest::Test4095713 ()
  609. {
  610.     UErrorCode status = U_ZERO_ERROR;
  611.     DecimalFormat *df = new DecimalFormat(status);
  612.     failure(status, "new DecimalFOrmat");
  613.     UnicodeString str("0.1234");
  614.     double d1 = 0.1234;
  615.     //Double d1 = new Double(str);
  616.     //Double d2 = (Double) df.parse(str, new ParsePosition(0));
  617.     Formattable d2;
  618.     ParsePosition pp(0);
  619.     df->parse(str, d2, pp);
  620.     logln(UnicodeString("") + d1);
  621.     if (d2.getDouble() != d1)
  622.         errln(UnicodeString("Bug 4095713 test failed, new double value : ") + d2.getDouble());
  623. }
  624.  
  625. /* @bug 4092561
  626.  * DecimalFormat.parse() fails when multiplier is not set to 1
  627.  */
  628. // {sfb} not sure what to do with this one
  629. void NumberFormatRegressionTest::Test4092561 ()
  630. {
  631.     UErrorCode status = U_ZERO_ERROR;
  632.     DecimalFormat *df = new DecimalFormat(status);
  633.     failure(status, "new DecimalFormat");
  634.  
  635.     // {sfb} going to cheat here and use sprintf ??
  636.  
  637.     /*UnicodeString str = Long.toString(Long.MIN_VALUE);
  638.     logln("Long.MIN_VALUE : " + df.parse(str, new ParsePosition(0)).toString());
  639.     df.setMultiplier(100);
  640.     Number num = df.parse(str, new ParsePosition(0));
  641.     if (num.doubleValue() != -9.223372036854776E16)
  642.         errln("Bug 4092561 test failed when multiplier is set to not 1.");
  643. */
  644.     delete df;
  645. }
  646.  
  647. /* @bug 4092480
  648.  * DecimalFormat: Negative format ignored.
  649.  */
  650. void NumberFormatRegressionTest::Test4092480 ()
  651. {
  652.     UErrorCode status = U_ZERO_ERROR;
  653.     DecimalFormat *dfFoo = new DecimalFormat(UnicodeString("000"), status);
  654.     failure(status, "new DecimalFormat");
  655.  
  656.     //try {
  657.         dfFoo->applyPattern("0000;-000", status);
  658.         failure(status, "dfFoo->applyPattern");
  659.         UnicodeString temp;
  660.         if (dfFoo->toPattern(temp) != UnicodeString("#0000"))
  661.             errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
  662.         FieldPosition pos(FieldPosition::DONT_CARE);
  663.         logln(dfFoo->format((int32_t)42, temp, pos));
  664.         logln(dfFoo->format((int32_t)-42, temp, pos));
  665.         dfFoo->applyPattern("000;-000", status);
  666.         failure(status, "dfFoo->applyPattern");
  667.         if (dfFoo->toPattern(temp) != UnicodeString("#000"))
  668.             errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
  669.         logln(dfFoo->format((int32_t)42,temp, pos));
  670.         logln(dfFoo->format((int32_t)-42, temp, pos));
  671.  
  672.         dfFoo->applyPattern("000;-0000", status);
  673.         failure(status, "dfFoo->applyPattern");
  674.         if (dfFoo->toPattern(temp) != UnicodeString("#000"))
  675.             errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
  676.         logln(dfFoo->format((int32_t)42, temp, pos));
  677.         logln(dfFoo->format((int32_t)-42, temp, pos));
  678.  
  679.         dfFoo->applyPattern("0000;-000", status);
  680.         failure(status, "dfFoo->applyPattern");
  681.         if (dfFoo->toPattern(temp) != UnicodeString("#0000"))
  682.             errln("dfFoo.toPattern : " + dfFoo->toPattern(temp));
  683.         logln(dfFoo->format((int32_t)42, temp, pos));
  684.         logln(dfFoo->format((int32_t)-42, temp, pos));
  685.     /*} catch (Exception foo) {
  686.         errln("Message " + foo.getMessage());
  687.     }*/
  688.  
  689.     delete dfFoo;
  690. }
  691. /* @bug 4087244
  692.  * NumberFormat.getCurrencyInstance() produces format that uses
  693.  * decimal separator instead of monetary decimal separator.
  694.  *
  695.  * Rewrote this test not to depend on the actual pattern.  Pattern should
  696.  * never contain the monetary separator!  Decimal separator in pattern is
  697.  * interpreted as monetary separator if currency symbol is seen!
  698.  */
  699. void NumberFormatRegressionTest::Test4087244 () {
  700.     Locale *de = new Locale(UnicodeString("pt"), UnicodeString("PT"));
  701.     UErrorCode status = U_ZERO_ERROR;
  702.     NumberFormat *nf = NumberFormat::createCurrencyInstance(*de, status);
  703.     if (nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  704.         errln("expected DecimalFormat!");
  705.         return;
  706.     }
  707.     DecimalFormat *df = (DecimalFormat*) nf;
  708.     const DecimalFormatSymbols *sym = df->getDecimalFormatSymbols();
  709.     UChar decSep = sym->getDecimalSeparator();
  710.     UChar monSep = sym->getMonetaryDecimalSeparator();
  711.     UChar zero = sym->getZeroDigit();
  712.     if (decSep == monSep) {
  713.         errln("ERROR in test: want decimal sep != monetary sep");
  714.         return;
  715.     }
  716.     df->setMinimumIntegerDigits(1);
  717.     df->setMinimumFractionDigits(2);
  718.     UnicodeString str;
  719.     FieldPosition pos;
  720.     df->format(1.23, str, pos);
  721.     UnicodeString monStr("1x23"); monStr[(UTextOffset)1] = monSep;
  722.     UnicodeString decStr("1x23"); decStr[(UTextOffset)1] = decSep;
  723.     if (str.indexOf(monStr) >= 0 && str.indexOf(decStr) < 0) {
  724.         logln(UnicodeString("OK: 1.23 -> \"") + str + "\" contains \"" +
  725.               monStr + "\" and not \"" + decStr + '"');
  726.     } else {
  727.         errln(UnicodeString("FAIL: 1.23 -> \"") + str + "\", should contain \"" +
  728.               monStr +
  729.               "\" and not \"" + decStr + '"');
  730.     }
  731.     delete de;
  732.     delete nf;
  733. }
  734. /* @bug 4070798
  735.  * Number format data rounding errors for locale FR
  736.  */
  737. void NumberFormatRegressionTest::Test4070798 () 
  738. {
  739.     NumberFormat *formatter;
  740.     UnicodeString tempString;
  741.     
  742.     /* User error :
  743.     String expectedDefault = "-5\u00a0789,987";
  744.     String expectedCurrency = "5\u00a0789,98 F";
  745.     String expectedPercent = "-578\u00a0998%";
  746.     */
  747.     UChar chars1 [] = {
  748.         '-', '5', 0x00a0, '7', '8', '9', ',', '9', '8', '8'
  749.     };
  750.     UChar chars2 [] = {
  751.         '5', 0x00a0, '7', '8', '9', ',', '9', '9', ' ', 'F'
  752.     };
  753.     UChar chars3 [] = {
  754.         '-', '5', '7', '8', 0x00a0, '9', '9', '9', '%'
  755.     };
  756.     UnicodeString expectedDefault(chars1, 10, 10);
  757.     UnicodeString expectedCurrency(chars2, 10, 10);
  758.     UnicodeString expectedPercent(chars3, 9, 9);
  759.  
  760.     UErrorCode status = U_ZERO_ERROR;
  761.     formatter = NumberFormat::createInstance(Locale::FRANCE, status);
  762.     failure(status, "NumberFormat::createNumberInstance");
  763.     tempString = formatter->format (-5789.9876, tempString);
  764.  
  765.     if (tempString == expectedDefault) {
  766.         logln ("Bug 4070798 default test passed.");
  767.     } else {
  768.         errln(UnicodeString("Failed:") +
  769.         " Expected " + expectedDefault +
  770.         " Received " + tempString );
  771.     }
  772.     delete formatter;
  773.  
  774.     formatter = NumberFormat::createCurrencyInstance(Locale::FRANCE, status);
  775.     failure(status, "NumberFormat::createCurrencyInstance");
  776.     tempString.remove();
  777.     tempString = formatter->format( 5789.9876, tempString );
  778.  
  779.     if (tempString == expectedCurrency) {
  780.         logln ("Bug 4070798 currency test passed.");
  781.     } else {
  782.         errln(UnicodeString("Failed:") +
  783.         " Expected " + expectedCurrency +
  784.         " Received " + tempString );
  785.     }
  786.     delete formatter;
  787.  
  788.     formatter = NumberFormat::createPercentInstance(Locale::FRANCE, status);
  789.     failure(status, "NumberFormat::createPercentInstance");
  790.     tempString.remove();
  791.     tempString = formatter->format (-5789.9876, tempString);
  792.  
  793.     if (tempString == expectedPercent) {
  794.         logln ("Bug 4070798 percentage test passed.");
  795.     } else {
  796.         errln(UnicodeString("Failed:") +
  797.         " Expected " + expectedPercent +
  798.         " Received " + tempString );
  799.     }
  800.  
  801.     delete formatter;
  802. }
  803. /* @bug 4071005
  804.  * Data rounding errors for French (Canada) locale
  805.  */
  806. void NumberFormatRegressionTest::Test4071005 () 
  807. {
  808.     NumberFormat *formatter;
  809.     UnicodeString tempString;
  810.     /* User error :
  811.     String expectedDefault = "-5\u00a0789,987";
  812.     String expectedCurrency = "5\u00a0789,98 $";
  813.     String expectedPercent = "-578\u00a0998%";
  814.     */
  815.     UChar chars1 [] = {
  816.         '-', '5', 0x00a0, '7', '8', '9', ',', '9', '8', '8'
  817.     };
  818.     UChar chars2 [] = {
  819.         '5', 0x00a0, '7', '8', '9', ',', '9', '9', ' ', '$'
  820.     };
  821.     UChar chars3 [] = {
  822.         '-', '5', '7', '8', 0x00a0, '9', '9', '9', '%'
  823.     };
  824.     UnicodeString expectedDefault(chars1, 10, 10);
  825.     UnicodeString expectedCurrency(chars2, 10, 10);
  826.     UnicodeString expectedPercent(chars3, 9, 9);
  827.  
  828.     UErrorCode status = U_ZERO_ERROR;
  829.     formatter = NumberFormat::createInstance(Locale::CANADA_FRENCH, status);
  830.     failure(status, "NumberFormat::createNumberInstance");
  831.     tempString = formatter->format (-5789.9876, tempString);
  832.  
  833.     if (tempString == expectedDefault) {
  834.         logln ("Bug 4071005 default test passed.");
  835.     } else {
  836.         errln(UnicodeString("Failed:") +
  837.         " Expected " + expectedDefault +
  838.         " Received " + tempString );
  839.     }
  840.     delete formatter;
  841.  
  842.     formatter = NumberFormat::createCurrencyInstance(Locale::CANADA_FRENCH, status);
  843.     failure(status, "NumberFormat::createCurrencyInstance");
  844.     tempString.remove();
  845.     tempString = formatter->format( 5789.9876, tempString );
  846.  
  847.     if (tempString == expectedCurrency) {
  848.         logln ("Bug 4071005 currency test assed.");
  849.     } else {
  850.         errln(UnicodeString("Failed:") +
  851.         " Expected " + expectedCurrency +
  852.         " Received " + tempString );
  853.     }
  854.     delete formatter;
  855.  
  856.     formatter = NumberFormat::createPercentInstance(Locale::CANADA_FRENCH, status);
  857.     failure(status, "NumberFormat::createPercentInstance");
  858.     tempString.remove();
  859.     tempString = formatter->format (-5789.9876, tempString);
  860.  
  861.     if (tempString == expectedPercent) {
  862.         logln ("Bug 4071005 percentage test passed.");
  863.     } else {
  864.         errln(UnicodeString("Failed:") +
  865.         " Expected " + expectedPercent +
  866.         " Received " + tempString );
  867.     }
  868.  
  869.     delete formatter;
  870. }
  871.  
  872. /* @bug 4071014
  873.  * Data rounding errors for German (Germany) locale
  874.  */
  875. void NumberFormatRegressionTest::Test4071014 () 
  876. {
  877.     NumberFormat *formatter;
  878.     UnicodeString tempString;
  879.     /* user error :
  880.     String expectedDefault = "-5.789,987";
  881.     String expectedCurrency = "5.789,98 DM";
  882.     String expectedPercent = "-578.998%";
  883.     */
  884.     UnicodeString expectedDefault("-5.789,988");
  885.     UnicodeString expectedCurrency("5.789,99 DM");
  886.     UnicodeString expectedPercent("-578.999%");
  887.  
  888.     UErrorCode status = U_ZERO_ERROR;
  889.     formatter = NumberFormat::createInstance(Locale::GERMANY, status);
  890.     failure(status, "NumberFormat::createNumberInstance");
  891.     tempString.remove();
  892.     tempString = formatter->format (-5789.9876, tempString);
  893.  
  894.     if (tempString == expectedDefault) {
  895.         logln ("Bug 4071014 default test passed.");
  896.     } else {
  897.         errln(UnicodeString("Failed:") +
  898.         " Expected " + expectedDefault +
  899.         " Received " + tempString );
  900.     }
  901.     delete formatter;
  902.  
  903.     formatter = NumberFormat::createCurrencyInstance(Locale::GERMANY, status);
  904.     failure(status, "NumberFormat::createCurrencyInstance");
  905.     tempString.remove();
  906.     tempString = formatter->format( 5789.9876, tempString );
  907.  
  908.     if (tempString == expectedCurrency) {
  909.         logln ("Bug 4071014 currency test assed.");
  910.     } else {
  911.         errln(UnicodeString("Failed:") +
  912.         " Expected " + expectedCurrency +
  913.         " Received " + tempString );
  914.     }
  915.     delete formatter;
  916.  
  917.     formatter = NumberFormat::createPercentInstance(Locale::GERMANY, status);
  918.     failure(status, "NumberFormat::createPercentInstance");
  919.     tempString.remove();
  920.     tempString = formatter->format (-5789.9876, tempString);
  921.  
  922.     if (tempString == expectedPercent) {
  923.         logln ("Bug 4071014 percentage test passed.");
  924.     } else {
  925.         errln(UnicodeString("Failed:") +
  926.         " Expected " + expectedPercent +
  927.         " Received " + tempString );
  928.     }
  929.  
  930.     delete formatter;
  931. }
  932. /* @bug 4071859
  933.  * Data rounding errors for Italian locale number formats
  934.  */
  935. void NumberFormatRegressionTest::Test4071859 () 
  936. {
  937.     NumberFormat *formatter;
  938.     UnicodeString tempString;
  939.     /* user error :
  940.     String expectedDefault = "-5.789,987";
  941.     String expectedCurrency = "-L. 5.789,98";
  942.     String expectedPercent = "-578.998%";
  943.     */
  944.     UnicodeString expectedDefault("-5.789,988");
  945.     UnicodeString expectedCurrency("-L. 5.790");
  946.     UnicodeString expectedPercent("-578.999%");
  947.  
  948.     UErrorCode status = U_ZERO_ERROR;
  949.     formatter = NumberFormat::createInstance(Locale::ITALY, status);
  950.     failure(status, "NumberFormat::createNumberInstance");
  951.     tempString = formatter->format (-5789.9876, tempString);
  952.  
  953.     if (tempString == expectedDefault) {
  954.         logln ("Bug 4071859 default test passed.");
  955.     } else {
  956.         errln(UnicodeString("Failed:") +
  957.         " Expected " + expectedDefault +
  958.         " Received " + tempString );
  959.     }
  960.     delete formatter;
  961.  
  962.     formatter = NumberFormat::createCurrencyInstance(Locale::ITALY, status);
  963.     failure(status, "NumberFormat::createCurrencyInstance");
  964.     tempString.remove();
  965.     tempString = formatter->format( -5789.9876, tempString );
  966.  
  967.     if (tempString == expectedCurrency) {
  968.         logln ("Bug 4071859 currency test assed.");
  969.     } else {
  970.         errln(UnicodeString("Failed:") +
  971.         " Expected " + expectedCurrency +
  972.         " Received " + tempString );
  973.     }
  974.     delete formatter;
  975.  
  976.     formatter = NumberFormat::createPercentInstance(Locale::ITALY, status);
  977.     failure(status, "NumberFormat::createPercentInstance");
  978.     tempString.remove();
  979.     tempString = formatter->format (-5789.9876, tempString);
  980.  
  981.     if (tempString == expectedPercent) {
  982.         logln ("Bug 4071859 percentage test passed.");
  983.     } else {
  984.         errln(UnicodeString("Failed:") +
  985.         " Expected " + expectedPercent +
  986.         " Received " + tempString );
  987.     }
  988.  
  989.     delete formatter;
  990. }
  991. /* @bug 4071859
  992.  * Test rounding for nearest even.
  993.  */
  994. void NumberFormatRegressionTest::Test4093610()
  995. {
  996.     UErrorCode status = U_ZERO_ERROR;
  997.     DecimalFormat *df = new DecimalFormat("#0.#", status);
  998.     failure(status, "new DecimalFormat");
  999.     UnicodeString s("12.4");
  1000.     roundingTest(df, 12.35, s);
  1001.     roundingTest(df, 12.45, s);
  1002.     s = "12.5";
  1003.     roundingTest(df, 12.452,s);
  1004.     s = "12.6";
  1005.     roundingTest(df, 12.55, s);
  1006.     roundingTest(df, 12.65, s);
  1007.     s = "12.7";
  1008.     roundingTest(df, 12.652,s);
  1009.     s = "12.8";
  1010.     roundingTest(df, 12.75, s);
  1011.     roundingTest(df, 12.752,s);
  1012.     roundingTest(df, 12.85, s);
  1013.     s = "12.9";
  1014.     roundingTest(df, 12.852,s);
  1015.     s = "13";
  1016.     roundingTest(df, 12.95, s);
  1017.     roundingTest(df, 12.952,s);
  1018.  
  1019.     delete df;
  1020. }
  1021.  
  1022. void NumberFormatRegressionTest::roundingTest(DecimalFormat *df, double x, UnicodeString& expected)
  1023. {
  1024.     UnicodeString out;
  1025.     FieldPosition pos(FieldPosition::DONT_CARE);
  1026.     out = df->format(x, out, pos);
  1027.     logln(UnicodeString("") + x + " formats with 1 fractional digits to " + out);
  1028.     if (out != expected) 
  1029.         errln("FAIL: Expected " + expected);
  1030. }
  1031. /* @bug 4098741
  1032.  * Tests the setMaximumFractionDigits limit.
  1033.  */
  1034. void NumberFormatRegressionTest::Test4098741()
  1035. {
  1036.     //try {
  1037.     UErrorCode status = U_ZERO_ERROR;
  1038.     NumberFormat *fmt = NumberFormat::createPercentInstance(status);
  1039.         fmt->setMaximumFractionDigits(20);
  1040.         UnicodeString temp;
  1041.         logln(fmt->format(.001, temp));
  1042.     /*} catch (Exception foo) {
  1043.         errln("Bug 4098471 failed with exception thrown : " + foo.getMessage());
  1044.     }*/
  1045.     delete fmt;
  1046. }
  1047. /* @bug 4074454
  1048.  * Tests illegal pattern exception.
  1049.  * Fix comment : HShih A31 Part1 will not be fixed and javadoc needs to be updated.
  1050.  * Part2 has been fixed.
  1051.  */
  1052. void NumberFormatRegressionTest::Test4074454()
  1053. {
  1054.     //try {
  1055.     UErrorCode status = U_ZERO_ERROR;  
  1056.     DecimalFormat *fmt = new DecimalFormat("#,#00.00;-#.#", status);
  1057.     failure(status, "new DecimalFormat");
  1058.       logln("Inconsistent negative pattern is fine.");
  1059.         DecimalFormat *newFmt = new DecimalFormat("#,#00.00 p''ieces;-#,#00.00 p''ieces", status);
  1060.         failure(status, "new DecimalFormat");
  1061.         UnicodeString tempString;
  1062.         FieldPosition pos(FieldPosition::DONT_CARE);
  1063.         tempString = newFmt->format(3456.78, tempString, pos);
  1064.         if (tempString != UnicodeString("3,456.78 p'ieces"))
  1065.             errln("Failed!  3456.78 p'ieces expected, but got : " + tempString);
  1066.     /*} catch (Exception foo) {
  1067.         errln("An exception was thrown for any inconsistent negative pattern.");
  1068.     }*/
  1069.  
  1070.     delete fmt;
  1071.     delete newFmt;
  1072. }
  1073. /* @bug 4099404
  1074.  * Tests all different comments.
  1075.  * Response to some comments :
  1076.  * [1] DecimalFormat.parse API documentation is more than just one line.
  1077.  * This is not a reproducable doc error in 116 source code.
  1078.  * [2] See updated javadoc.
  1079.  * [3] Fixed.
  1080.  * [4] NumberFormat.parse(String, ParsePosition) : If parsing fails,
  1081.  * a null object will be returned.  The unchanged parse position also
  1082.  * reflects an error.
  1083.  * NumberFormat.parse(String) : If parsing fails, an ParseException
  1084.  * will be thrown.
  1085.  * See updated javadoc for more details.
  1086.  * [5] See updated javadoc.
  1087.  * [6] See updated javadoc.
  1088.  * [7] This is a correct behavior if the DateFormat object is linient.
  1089.  * Otherwise, an IllegalArgumentException will be thrown when formatting
  1090.  * "January 35".  See GregorianCalendar class javadoc for more details.
  1091.  */
  1092. void NumberFormatRegressionTest::Test4099404()
  1093. {
  1094.     //try {
  1095.         UErrorCode status = U_ZERO_ERROR;
  1096.         DecimalFormat *fmt = new DecimalFormat(UnicodeString("000.0#0"), status);
  1097.         if(! U_FAILURE(status))
  1098.             errln(UnicodeString("Bug 4099404 failed applying illegal pattern \"000.0#0\""));
  1099.     /*} catch (Exception foo) {
  1100.         logln("Bug 4099404 pattern \"000.0#0\" passed");
  1101.     }*/
  1102.     delete fmt;
  1103.     fmt = 0;
  1104.         //try {
  1105.         fmt = new DecimalFormat(UnicodeString("0#0.000"), status);
  1106.         if( !U_FAILURE(status))
  1107.            errln("Bug 4099404 failed applying illegal pattern \"0#0.000\"");
  1108.     /*} catch (Exception foo) {
  1109.         logln("Bug 4099404 pattern \"0#0.000\" passed");
  1110.     }*/
  1111.  
  1112.     delete fmt;
  1113. }
  1114. /* @bug 4101481
  1115.  * DecimalFormat.applyPattern doesn't set minimum integer digits
  1116.  */
  1117. void NumberFormatRegressionTest::Test4101481()
  1118. {
  1119.     UErrorCode status = U_ZERO_ERROR;
  1120.     DecimalFormat *sdf = new DecimalFormat(UnicodeString("#,##0"), status);
  1121.     failure(status, "new DecimalFormat");
  1122.     if (sdf->getMinimumIntegerDigits() != 1)
  1123.         errln("Minimum integer digits : " + sdf->getMinimumIntegerDigits());
  1124. }
  1125. /* @bug 4052223 (API addition request A27)
  1126.  * Tests ParsePosition.setErrorPosition() and ParsePosition.getErrorPosition().
  1127.  */
  1128. void NumberFormatRegressionTest::Test4052223()
  1129. {
  1130.     //try {
  1131.     UErrorCode status = U_ZERO_ERROR;
  1132.         DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,#00.00"), status);
  1133.         failure(status, "new DecimalFormat");
  1134.         Formattable num;
  1135.         fmt->parse(UnicodeString("abc3"), num, status);
  1136.         if(! U_FAILURE(status))
  1137.             errln(UnicodeString("Bug 4052223 failed : can't parse string \"a\".  Got ") /*+ num*/);
  1138.     /*} catch (ParseException foo) {
  1139.         logln("Caught expected ParseException : " + foo.getMessage() + " at index : " + foo.getErrorOffset());
  1140.     }*/
  1141.     delete fmt;
  1142. }
  1143. /* @bug 4061302
  1144.  * API tests for API addition request A9.
  1145.  */
  1146. void NumberFormatRegressionTest::Test4061302()
  1147. {
  1148.     UErrorCode status = U_ZERO_ERROR;
  1149.     DecimalFormatSymbols *fmt = new DecimalFormatSymbols(status);
  1150.     failure(status, "new DecimalFormatSymbols");
  1151.     UnicodeString currency;
  1152.     currency= fmt->getCurrencySymbol(currency);
  1153.     UnicodeString intlCurrency;
  1154.     intlCurrency = fmt->getInternationalCurrencySymbol(intlCurrency);
  1155.     UChar monDecSeparator = fmt->getMonetaryDecimalSeparator();
  1156.     if (currency == UnicodeString("") ||
  1157.         intlCurrency == UnicodeString("") ||
  1158.         monDecSeparator == 0x0000) {
  1159.         errln("getCurrencySymbols failed, got empty string.");
  1160.     }
  1161.     logln("Before set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
  1162.     fmt->setCurrencySymbol(UnicodeString("XYZ"));
  1163.     fmt->setInternationalCurrencySymbol(UnicodeString("ABC"));
  1164.     fmt->setMonetaryDecimalSeparator(0x002A/*'*'*/);
  1165.     currency = fmt->getCurrencySymbol(currency);
  1166.     intlCurrency = fmt->getInternationalCurrencySymbol(intlCurrency);
  1167.     monDecSeparator = fmt->getMonetaryDecimalSeparator();
  1168.     if (currency != UnicodeString("XYZ") ||
  1169.         intlCurrency != UnicodeString("ABC") ||
  1170.         monDecSeparator != 0x002A/*'*'*/) {
  1171.         errln("setCurrencySymbols failed.");
  1172.     }
  1173.     logln("After set ==> Currency : " + currency + " Intl Currency : " + intlCurrency + " Monetary Decimal Separator : " + monDecSeparator);
  1174.  
  1175.     delete fmt;
  1176. }
  1177. /* @bug 4062486
  1178.  * API tests for API addition request A23. FieldPosition.getBeginIndex and
  1179.  * FieldPosition.getEndIndex.
  1180.  */
  1181. void NumberFormatRegressionTest::Test4062486()
  1182. {
  1183.     UErrorCode status = U_ZERO_ERROR;
  1184.     DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,##0.00"), status);
  1185.     failure(status, "new DecimalFormat");
  1186.     UnicodeString formatted;
  1187.     FieldPosition field(0);
  1188.     double num = 1234.5;
  1189.     fmt->format(num, formatted, field);
  1190.     if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
  1191.         errln(UnicodeString("Format 1234.5 failed. Begin index: ") /*+ field.getBeginIndex() + " End index: " + field.getEndIndex()*/);
  1192.     field.setBeginIndex(7);
  1193.     field.setEndIndex(4);
  1194.     if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
  1195.         errln("Set begin/end field indexes failed. Begin index: " /*+ field.getBeginIndex() + " End index: " + field.getEndIndex()*/);
  1196.  
  1197.     delete fmt;
  1198. }
  1199.  
  1200. /* @bug 4108738
  1201.  * DecimalFormat.parse incorrectly works with a group separator.
  1202.  */
  1203. void NumberFormatRegressionTest::Test4108738()
  1204. {
  1205.     UErrorCode status = U_ZERO_ERROR;
  1206.     DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::US, status);
  1207.     failure(status, "new DecimalFormatSymbols");
  1208.     DecimalFormat *df = new DecimalFormat("#,##0.###", syms, status);
  1209.     failure(status, "new DecimalFormat");
  1210.     UnicodeString text("1.222,111");
  1211.     Formattable num;
  1212.     ParsePosition pp(0);
  1213.     df->parse(text, num, pp);
  1214.     
  1215.     // {sfb} how to do this (again) ?
  1216.     // shouldn't just be another round-trip test, should it?
  1217.     UnicodeString temp;
  1218.     FieldPosition pos(FieldPosition::DONT_CARE);
  1219.     temp = df->format(num.getDouble(), temp, pos);
  1220.     //if (!num.toString().equals("1.222"))
  1221.     if (temp != UnicodeString("1.222"))
  1222.         //errln("\"" + text + "\"  is parsed as " + num);
  1223.         errln("\"" + text + "\"  is parsed as " + temp);
  1224.     text = UnicodeString("1.222x111");
  1225.     pp = ParsePosition(0);
  1226.     df->parse(text, num, pp);
  1227.     temp.remove();
  1228.     temp = df->format(num.getDouble(), temp, pos);
  1229.     //if (!num.toString().equals("1.222"))
  1230.     if (temp != UnicodeString("1.222"))
  1231.         errln("\"" + text + "\"  is parsed as " + temp);
  1232.  
  1233.     delete df;
  1234. }
  1235.  
  1236. /* @bug 4106658
  1237.  * DecimalFormat.format() incorrectly formats negative doubles.
  1238.  */
  1239. void NumberFormatRegressionTest::Test4106658()
  1240. {
  1241.     UErrorCode status = U_ZERO_ERROR;
  1242.     DecimalFormat *df = new DecimalFormat(status); // Corrected; see 4147706
  1243.     failure(status, "new DecimalFormat");
  1244. #if defined(WIN32) || defined(HPUX)
  1245.     double d1 = 0.0 * -1;
  1246. #else
  1247.     double d1 = 0.0; d1 *= -1;
  1248. #endif
  1249.     double d2 = -0.0001;
  1250.     UnicodeString buffer;
  1251.     UnicodeString temp;
  1252.     logln("pattern: \"" + df->toPattern(temp) + "\"");
  1253.     FieldPosition pos(FieldPosition::DONT_CARE);
  1254.     df->format(d1, buffer, pos);
  1255.     if (buffer != UnicodeString("-0")) // Corrected; see 4147706
  1256.         errln(UnicodeString("") + d1 + "      is formatted as " + buffer);
  1257.     buffer.remove();
  1258.     df->format(d2, buffer, pos);
  1259.     if (buffer != UnicodeString("-0")) // Corrected; see 4147706
  1260.         errln(UnicodeString("") + d2 + "      is formatted as " + buffer);
  1261.  
  1262.     delete df;
  1263. }
  1264.  
  1265. /* @bug 4106662
  1266.  * DecimalFormat.parse returns 0 if string parameter is incorrect.
  1267.  */
  1268. void NumberFormatRegressionTest::Test4106662()
  1269. {
  1270.     UErrorCode status = U_ZERO_ERROR;
  1271.     DecimalFormat *df = new DecimalFormat(status);
  1272.     failure(status, "new DecimalFormat");
  1273.     UnicodeString text("x");
  1274.     ParsePosition pos1(0), pos2(0);
  1275.  
  1276.     UnicodeString temp;
  1277.     logln("pattern: \"" + df->toPattern(temp) + "\"");
  1278.     Formattable num;
  1279.     df->parse(text, num, pos1);
  1280.     if (pos1 == ParsePosition(0)/*num != null*/) {
  1281.         errln(UnicodeString("Test Failed: \"") + text + "\" is parsed as " /*+ num*/);
  1282.     }
  1283.     delete df;
  1284.     df = new DecimalFormat(UnicodeString("$###.00"), status);
  1285.     failure(status, "new DecimalFormat");
  1286.     df->parse(UnicodeString("$"), num, pos2);
  1287.     if (pos2 == ParsePosition(0) /*num != null*/){
  1288.         errln(UnicodeString("Test Failed: \"$\" is parsed as ") /*+ num*/);
  1289.     }
  1290.  
  1291.     delete df;
  1292. }
  1293.  
  1294. /* @bug 4114639 (duplicate of 4106662)
  1295.  * NumberFormat.parse doesn't return null
  1296.  */
  1297. void NumberFormatRegressionTest::Test4114639()
  1298. {
  1299.     UErrorCode status = U_ZERO_ERROR;
  1300.     NumberFormat *format = NumberFormat::createInstance(status);
  1301.     failure(status, "NumberFormat::createInstance");
  1302.     UnicodeString text("time 10:x");
  1303.     ParsePosition pos(8);
  1304.     Formattable result;
  1305.     format->parse(text, result, pos);
  1306.     if (/*result != null*/pos.getErrorIndex() != 8) 
  1307.         errln(UnicodeString("Should return null but got : ") /*+ result*/); // Should be null; it isn't
  1308.  
  1309.     delete format;
  1310. }
  1311.  
  1312. /* @bug 4106664 (FIX)
  1313.  * DecimalFormat.format(long n) fails if n * multiplier > MAX_LONG.
  1314.  */
  1315. void NumberFormatRegressionTest::Test4106664()
  1316. {
  1317.     UErrorCode status = U_ZERO_ERROR;
  1318.     DecimalFormat *df = new DecimalFormat(status);
  1319.     failure(status, "new DecimalFormat");
  1320.     // {sfb} long in java is 64 bits
  1321.     /*long*/double n = 1234567890123456.0;
  1322.     /*int*/int32_t m = 12345678;
  1323.     // {sfb} will this work?
  1324.     //BigInteger bigN = BigInteger.valueOf(n);
  1325.     //bigN = bigN.multiply(BigInteger.valueOf(m));
  1326.     double bigN = n * m;
  1327.     df->setMultiplier(m);
  1328.     df->setGroupingUsed(FALSE);
  1329.     UnicodeString temp;
  1330.     FieldPosition pos(FieldPosition::DONT_CARE);
  1331.     logln("formated: " +
  1332.         df->format(n, temp, pos));
  1333.     
  1334.     char buf [128];
  1335.     sprintf(buf, "%g", bigN);
  1336.     //logln("expected: " + bigN.toString());
  1337.     logln(UnicodeString("expected: ") + buf);
  1338.  
  1339.     delete df;
  1340. }
  1341. /* @bug 4106667 (duplicate of 4106658)
  1342.  * DecimalFormat.format incorrectly formats -0.0.
  1343.  */
  1344. void NumberFormatRegressionTest::Test4106667()
  1345. {
  1346.     UErrorCode status = U_ZERO_ERROR;
  1347.     DecimalFormat *df = new DecimalFormat(status);
  1348.     failure(status, "new DecimalFormat");
  1349.     UChar foo [] = { 0x002B };
  1350.     UnicodeString bar(foo, 1, 1);
  1351.     df->setPositivePrefix(/*"+"*/bar);
  1352. #if defined(WIN32) || defined(HPUX)
  1353.     double d = 0.0 * -1;
  1354. #else
  1355.     double d = 0.0; d *= -1;
  1356. #endif
  1357.     UnicodeString temp;
  1358.     logln("pattern: \"" + df->toPattern(temp) + "\"");
  1359.     UnicodeString buffer;
  1360.     FieldPosition pos(FieldPosition::DONT_CARE);
  1361.     df->format(d, buffer, pos);
  1362.     if (buffer != UnicodeString("-0")) // Corrected; see 4147706
  1363.         errln(/*d + */UnicodeString("  is formatted as ") + buffer);
  1364.  
  1365.     delete df;
  1366. }
  1367.  
  1368. /* @bug 4110936
  1369.  * DecimalFormat.setMaximumIntegerDigits() works incorrectly.
  1370.  */
  1371. void NumberFormatRegressionTest::Test4110936()
  1372. {
  1373.     UErrorCode status = U_ZERO_ERROR;
  1374.     NumberFormat *nf = NumberFormat::createInstance(status);
  1375.     failure(status, "NumberFormat::createInstance");
  1376.     nf->setMaximumIntegerDigits(128);
  1377.     logln("setMaximumIntegerDigits(128)");
  1378.     if (nf->getMaximumIntegerDigits() != 128)
  1379.         errln("getMaximumIntegerDigits() returns " +
  1380.             nf->getMaximumIntegerDigits());
  1381.  
  1382.     delete nf;
  1383. }
  1384.  
  1385. /* @bug 4122840
  1386.  * Locale data should use generic currency symbol
  1387.  *
  1388.  * 1) Make sure that all currency formats use the generic currency symbol.
  1389.  * 2) Make sure we get the same results using the generic symbol or a
  1390.  *    hard-coded one.
  1391.  */
  1392. void NumberFormatRegressionTest::Test4122840()
  1393. {
  1394.     int32_t count = 0;
  1395.     const Locale *locales = Locale::getAvailableLocales(count);
  1396.     
  1397.     for (int i = 0; i < count; i++) {
  1398.         UErrorCode status = U_ZERO_ERROR;
  1399.         ResourceBundle *rb = new ResourceBundle(
  1400.             icu_getDefaultDataDirectory()/*"java.text.resources.LocaleElements"*/, 
  1401.             locales[i], status);
  1402.         failure(status, "new ResourceBundle");
  1403.         //
  1404.         // Get the currency pattern for this locale.  We have to fish it
  1405.         // out of the ResourceBundle directly, since DecimalFormat.toPattern
  1406.         // will return the localized symbol, not \00a4
  1407.         //
  1408.         int32_t resCount = 0;
  1409.         const UnicodeString *numPatterns = rb->getStringArray(UnicodeString("NumberPatterns"),
  1410.             resCount, status);
  1411.         failure(status, "rb->getStringArray");
  1412.         UnicodeString pattern = numPatterns[1];
  1413.         
  1414.         UChar fo[] = { 0x00A4 };
  1415.         UnicodeString foo(fo, 1, 1);
  1416.  
  1417.         //if (pattern.indexOf("\u00A4") == -1 ) {
  1418.         UnicodeString temp;
  1419.         if (pattern.indexOf(foo) == -1 ) {
  1420.             errln(UnicodeString("Currency format for ") + locales[i].getName(temp) +
  1421.                     " does not contain generic currency symbol:" +
  1422.                     pattern );
  1423.         }
  1424.         
  1425.         // Create a DecimalFormat using the pattern we got and format a number
  1426.         DecimalFormatSymbols *symbols = new DecimalFormatSymbols(locales[i], status);
  1427.         failure(status, "new DecimalFormatSymbols");
  1428.         DecimalFormat *fmt1 = new DecimalFormat(pattern, *symbols, status);
  1429.         failure(status, "new DecimalFormat");
  1430.         
  1431.         UnicodeString result1;
  1432.         FieldPosition pos(FieldPosition::DONT_CARE);
  1433.         result1 = fmt1->format(1.111, result1, pos);
  1434.         
  1435.         //
  1436.         // Now substitute in the locale's currency symbol and create another
  1437.         // pattern.  We have to skip locales where the currency symbol
  1438.         // contains decimal separators, because that confuses things
  1439.         //
  1440.         UChar ba[] = { 0x002E/*'.'*/ };
  1441.         UnicodeString bar(ba, 1, 1);
  1442.  
  1443.         if (symbols->getCurrencySymbol(temp).indexOf(bar) == -1) {
  1444.             // {sfb} Also, switch the decimal separator to the monetary decimal
  1445.             // separator to mimic the behavior of a currency format
  1446.             symbols->setDecimalSeparator(symbols->getMonetaryDecimalSeparator());
  1447.             
  1448.             UnicodeString buf(pattern);
  1449.             for (int j = 0; j < buf.size(); j++) {
  1450.                 if (buf[j] == 0x00a4 ) {
  1451.                     if(buf[j + 1] == 0x00a4) {
  1452.                         // {sfb} added to support double currency marker (intl currency sign)
  1453.                         buf.replace(j, /*j+*/2, symbols->getInternationalCurrencySymbol(temp)); 
  1454.                         j += symbols->getInternationalCurrencySymbol(temp).size() - 1 + 1;
  1455.                     }
  1456.                     else {
  1457.                         buf.replace(j, /*j+*/1, symbols->getCurrencySymbol(temp)); 
  1458.                         j += symbols->getCurrencySymbol(temp).size() - 1;
  1459.                     }                    
  1460.                 }
  1461.             }
  1462.  
  1463.             DecimalFormat *fmt2 = new DecimalFormat(buf, *symbols, status);
  1464.             failure(status, "new DecimalFormat");
  1465.             
  1466.             UnicodeString result2;
  1467.             result2 = fmt2->format(1.111, result2, pos);
  1468.             
  1469.             if (result1 != result2) {
  1470.                 errln("Results for " + locales[i].getName(temp) + " differ: " +
  1471.                         result1 + " vs " + result2);
  1472.             }
  1473.         
  1474.             delete fmt2;
  1475.         }
  1476.     
  1477.         delete rb;
  1478.         delete fmt1;
  1479.         delete symbols;
  1480.     }
  1481. }
  1482.  
  1483. /* @bug 4125885
  1484.  * DecimalFormat.format() delivers wrong string.
  1485.  */
  1486. void NumberFormatRegressionTest::Test4125885()
  1487. {
  1488.     UErrorCode status = U_ZERO_ERROR;
  1489.     double rate = 12.34;
  1490.     DecimalFormat *formatDec = new DecimalFormat ("000.00", status);
  1491.     failure(status, "new DecimalFormat");
  1492.     UnicodeString temp;
  1493.     logln("toPattern: " + formatDec->toPattern(temp));
  1494.     UnicodeString rateString;
  1495.     FieldPosition pos(FieldPosition::DONT_CARE);
  1496.     rateString = formatDec->format(rate, rateString, pos);
  1497.     if (rateString != UnicodeString("012.34"))
  1498.         errln("result : " + rateString + " expected : 012.34");
  1499.     rate = 0.1234;
  1500.     delete formatDec;// = null;
  1501.     formatDec = new DecimalFormat ("+000.00%;-000.00%", status);
  1502.     failure(status, "new DecimalFormat");
  1503.     logln("toPattern: " + formatDec->toPattern(temp));
  1504.     rateString.remove();
  1505.     rateString = formatDec->format(rate, rateString, pos);
  1506.     if (rateString != UnicodeString("+012.34%"))
  1507.         errln("result : " + rateString + " expected : +012.34%");
  1508.  
  1509.     delete formatDec;
  1510. }
  1511.  
  1512. /**
  1513.  * @bug 4134034
  1514.  * DecimalFormat produces extra zeros when formatting numbers.
  1515.  */
  1516. void NumberFormatRegressionTest::Test4134034() 
  1517. {
  1518.     UErrorCode status = U_ZERO_ERROR;
  1519.     DecimalFormat *nf = new DecimalFormat("##,###,###.00", status);
  1520.     failure(status, "new DecimalFormat");
  1521.     
  1522.     UnicodeString f;
  1523.     FieldPosition pos(FieldPosition::DONT_CARE);
  1524.     f = nf->format(9.02, f, pos);
  1525.     if (f == UnicodeString("9.02")) 
  1526.         logln(f + " ok"); 
  1527.     else 
  1528.         errln("9.02 -> " + f + "; want 9.02");
  1529.  
  1530.     f.remove();
  1531.     f = nf->format((int32_t)0, f, pos);
  1532.     if (f == UnicodeString(".00")) 
  1533.         logln(f + " ok"); 
  1534.     else 
  1535.         errln("0 -> " + f + "; want .00");
  1536.  
  1537.     delete nf;
  1538. }
  1539.  
  1540. /**
  1541.  * @bug 4134300
  1542.  * CANNOT REPRODUCE - This bug could not be reproduced.  It may be
  1543.  * a duplicate of 4134034.
  1544.  *
  1545.  * JDK 1.1.6 Bug, did NOT occur in 1.1.5
  1546.  * Possibly related to bug 4125885.
  1547.  * 
  1548.  * This class demonstrates a regression in version 1.1.6
  1549.  * of DecimalFormat class.
  1550.  * 
  1551.  * 1.1.6 Results
  1552.  * Value 1.2 Format #.00 Result '01.20' !!!wrong
  1553.  * Value 1.2 Format 0.00 Result '001.20' !!!wrong
  1554.  * Value 1.2 Format 00.00 Result '0001.20' !!!wrong
  1555.  * Value 1.2 Format #0.0# Result '1.2'
  1556.  * Value 1.2 Format #0.00 Result '001.20' !!!wrong
  1557.  * 
  1558.  * 1.1.5 Results
  1559.  * Value 1.2 Format #.00 Result '1.20'
  1560.  * Value 1.2 Format 0.00 Result '1.20'
  1561.  * Value 1.2 Format 00.00 Result '01.20'
  1562.  * Value 1.2 Format #0.0# Result '1.2'
  1563.  * Value 1.2 Format #0.00 Result '1.20'
  1564.  */
  1565. void NumberFormatRegressionTest::Test4134300() {
  1566.     UnicodeString DATA [] = {
  1567.      // Pattern      Expected string
  1568.         UnicodeString("#.00"),      UnicodeString("1.20"),
  1569.         UnicodeString("0.00"),      UnicodeString("1.20"),
  1570.         UnicodeString("00.00"),     UnicodeString("01.20"),
  1571.         UnicodeString("#0.0#"),     UnicodeString("1.2"),
  1572.         UnicodeString("#0.00"),     UnicodeString("1.20")
  1573.     };
  1574.  
  1575.     for (int i=0; i< 10; i+=2) {
  1576.         UnicodeString result;
  1577.         UErrorCode status = U_ZERO_ERROR;
  1578.         DecimalFormat *df = new DecimalFormat(DATA[i], status);
  1579.         failure(status, "new DecimalFormat");
  1580.         FieldPosition pos(FieldPosition::DONT_CARE);
  1581.         result = df->format(1.2, result, pos);
  1582.         if (result != DATA[i+1]) {
  1583.             errln("Fail: 1.2 x " + DATA[i] + " = " + result +
  1584.                   "; want " + DATA[i+1]);
  1585.         }
  1586.         else {
  1587.             logln("Ok: 1.2 x " + DATA[i] + " = " + result);
  1588.         }
  1589.     
  1590.         delete df;
  1591.     }
  1592. }
  1593.  
  1594. /**
  1595.  * @bug 4140009
  1596.  * Empty pattern produces double negative prefix.
  1597.  */
  1598. void NumberFormatRegressionTest::Test4140009() 
  1599. {
  1600.     UErrorCode status = U_ZERO_ERROR;
  1601.     DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::ENGLISH, status);
  1602.     failure(status, "new DecimalFormatSymbols");
  1603.     DecimalFormat *f = new DecimalFormat(UnicodeString(""), syms, status);
  1604.     failure(status, "new DecimalFormat");
  1605.     UnicodeString s;
  1606.     FieldPosition pos(FieldPosition::DONT_CARE);
  1607.     s = f->format(123.456, s, pos);
  1608.     if (s != UnicodeString("123.456"))
  1609.         errln("Fail: Format empty pattern x 123.456 => " + s);
  1610.     s.remove();
  1611.     s = f->format(-123.456, s, pos);
  1612.     if (s != UnicodeString("-123.456"))
  1613.         errln("Fail: Format empty pattern x -123.456 => " + s);
  1614. }
  1615.  
  1616. /**
  1617.  * @bug 4141750
  1618.  * BigDecimal numbers get their fractions truncated by NumberFormat.
  1619.  */
  1620. // {sfb} not pertinent in C++ ??
  1621. void NumberFormatRegressionTest::Test4141750() {
  1622.     /*try {
  1623.         UnicodeString str("12345.67");
  1624.         BigDecimal bd = new BigDecimal(str);
  1625.         String sd = NumberFormat.getInstance(Locale.US).format(bd);
  1626.         if (!sd.endsWith("67")) errln("Fail: " + str + " x format -> " + sd);
  1627.     }
  1628.     catch (Exception e) {
  1629.         errln(e.toString());
  1630.         e.printStackTrace();
  1631.     }*/
  1632. }
  1633.  
  1634. /**
  1635.  * @bug 4145457
  1636.  * DecimalFormat toPattern() doesn't quote special characters or handle
  1637.  * single quotes.
  1638.  */
  1639. void NumberFormatRegressionTest::Test4145457() {
  1640.     //try {
  1641.     UErrorCode status = U_ZERO_ERROR;
  1642.     NumberFormat *nff = NumberFormat::createInstance(status);
  1643.     failure(status, "NumberFormat::createInstance");
  1644.     if(nff->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  1645.         errln("DecimalFormat needed to continue");
  1646.         return;
  1647.     }
  1648.  
  1649.     DecimalFormat *nf = (DecimalFormat*)nff;
  1650.        DecimalFormatSymbols *sym = (DecimalFormatSymbols*) nf->getDecimalFormatSymbols();
  1651.         sym->setDecimalSeparator(/*'\''*/0x0027);
  1652.         nf->setDecimalFormatSymbols(*sym);
  1653.         double pi = 3.14159;
  1654.  
  1655.         UnicodeString PATS [] = { 
  1656.             UnicodeString("#.00 'num''ber'"), UnicodeString("''#.00''")
  1657.         };
  1658.  
  1659.         for (int32_t i=0; i<2; ++i) {
  1660.             nf->applyPattern(PATS[i], status);
  1661.             failure(status, "nf->applyPattern");
  1662.             UnicodeString out;
  1663.             FieldPosition pos(FieldPosition::DONT_CARE);
  1664.             out = nf->format(pi, out, pos);
  1665.             UnicodeString pat;
  1666.             pat = nf->toPattern(pat);
  1667.             Formattable num;
  1668.             ParsePosition pp(0);
  1669.             nf->parse(out, num, pp);
  1670.             double val = num.getDouble();
  1671.         
  1672.             nf->applyPattern(pat, status);
  1673.             failure(status, "nf->applyPattern");
  1674.             UnicodeString out2;
  1675.             out2 = nf->format(pi, out2, pos);
  1676.             UnicodeString pat2;
  1677.             pat2 = nf->toPattern(pat2);
  1678.             nf->parse(out2, num, pp);
  1679.             double val2 = num.getDouble();
  1680.         
  1681.             if (pat != pat2)
  1682.                 errln("Fail with \"" + PATS[i] + "\": Patterns should concur, \"" +
  1683.                       pat + "\" vs. \"" + pat2 + "\"");
  1684.             else
  1685.                 logln("Ok \"" + PATS[i] + "\" toPattern() -> \"" + pat + '"');
  1686.  
  1687.             if (val == val2 && out == out2) {
  1688.                 logln(UnicodeString("Ok ") + pi + " x \"" + PATS[i] + "\" -> \"" +
  1689.                       out + "\" -> " + val + " -> \"" +
  1690.                       out2 + "\" -> " + val2);
  1691.             }
  1692.             else {
  1693.                 errln(UnicodeString("Fail ") + pi + " x \"" + PATS[i] + "\" -> \"" +
  1694.                       out + "\" -> " + val + " -> \"" +
  1695.                       out2 + "\" -> " + val2);
  1696.             }
  1697.         }
  1698.     /*}
  1699.     catch (ParseException e) {
  1700.         errln("Fail: " + e);
  1701.         e.printStackTrace();
  1702.     }*/
  1703.  
  1704.     delete nff;
  1705. }
  1706.  
  1707. /**
  1708.  * @bug 4147295
  1709.  * DecimalFormat.applyPattern() sets minimum integer digits incorrectly.
  1710.  * CANNOT REPRODUCE
  1711.  * This bug is a duplicate of 4139344, which is a duplicate of 4134300
  1712.  */
  1713. void NumberFormatRegressionTest::Test4147295() 
  1714. {
  1715.     UErrorCode status = U_ZERO_ERROR;
  1716.     DecimalFormat *sdf = new DecimalFormat(status);
  1717.     UnicodeString pattern("#,###");
  1718.     logln("Applying pattern \"" + pattern + "\"");
  1719.     sdf->applyPattern(pattern, status);
  1720.     failure(status, "sdf->applyPattern");
  1721.     int minIntDig = sdf->getMinimumIntegerDigits();
  1722.     if (minIntDig != 0) {
  1723.         errln("Test failed");
  1724.         errln(" Minimum integer digits : " + minIntDig);
  1725.         UnicodeString temp;
  1726.         errln(" new pattern: " + sdf->toPattern(temp));
  1727.     } else {
  1728.         logln("Test passed");
  1729.         logln(" Minimum integer digits : " + minIntDig);
  1730.     }
  1731.     delete sdf;
  1732. }
  1733.  
  1734. /**
  1735.  * @bug 4147706
  1736.  * DecimalFormat formats -0.0 as +0.0
  1737.  * See also older related bug 4106658, 4106667
  1738.  */
  1739. void NumberFormatRegressionTest::Test4147706() 
  1740. {
  1741.     UErrorCode status = U_ZERO_ERROR;
  1742.     DecimalFormat *df = new DecimalFormat("#,##0.0##", status);
  1743.     failure(status, "new DecimalFormat");
  1744.     DecimalFormatSymbols *syms = new DecimalFormatSymbols(Locale::ENGLISH, status);
  1745.     failure(status, "new DecimalFormatSymbols");
  1746.     df->adoptDecimalFormatSymbols(syms);
  1747. #if defined(WIN32) || defined(HPUX)
  1748.     double d1 = 0.0 * -1;
  1749. #else
  1750.     double d1 = 0.0; d1 *= -1;
  1751. #endif
  1752.     double d2 = -0.0001;
  1753.     UnicodeString f1;
  1754.     FieldPosition pos(FieldPosition::DONT_CARE);
  1755.     f1 = df->format(d1, f1, pos);
  1756.     UnicodeString f2, temp;
  1757.     f2 = df->format(d2, f2, pos);
  1758.     if (f1 != UnicodeString("-0.0")) {
  1759.         errln(UnicodeString("") + d1 + UnicodeString(" x \"") + df->toPattern(temp) + "\" is formatted as \"" + f1 + '"');
  1760.     }
  1761.     if (f2 != UnicodeString("-0.0")) {
  1762.         errln(UnicodeString("") + d2 + UnicodeString(" x \"") + df->toPattern(temp) + "\" is formatted as \"" + f2 + '"');
  1763.     }
  1764.  
  1765.     delete df;
  1766. }
  1767.  
  1768.  
  1769. // Not applicable, since no serialization in C++
  1770. /*class myformat implements Serializable
  1771. {
  1772. DateFormat _dateFormat = DateFormat.getDateInstance();
  1773.  
  1774. public String Now()
  1775. {
  1776.     GregorianCalendar calendar = new GregorianCalendar();
  1777.     Date t = calendar.getTime();
  1778.     String nowStr = _dateFormat.format(t);
  1779.     return nowStr;
  1780. }
  1781. }*/
  1782.  
  1783. /**
  1784.  * @bug 4162198
  1785.  * NumberFormat cannot format Double.MAX_VALUE
  1786.  */
  1787. // TODO: make this test actually test something
  1788. void 
  1789. NumberFormatRegressionTest::Test4162198() 
  1790. {
  1791.     // for some reason, DBL_MAX will not round trip. (bug in sprintf/atof)
  1792.     double dbl = LONG_MAX * 1000.0;
  1793.     UErrorCode status = U_ZERO_ERROR;
  1794.     NumberFormat *f = NumberFormat::createInstance(status);
  1795.     if(U_FAILURE(status)) {
  1796.         errln("Couldn't create number format");
  1797.         return;
  1798.     }
  1799.     f->setMaximumFractionDigits(LONG_MAX);
  1800.     f->setMaximumIntegerDigits(LONG_MAX);
  1801.     UnicodeString s;
  1802.     f->format(dbl,s);
  1803.     logln(UnicodeString("The number ") + dbl + " formatted to " + s);
  1804.     Formattable n;
  1805.     //try {
  1806.     f->parse(s, n, status);
  1807.     if(U_FAILURE(status))
  1808.         errln("Couldn't parse!");
  1809.     //} catch (java.text.ParseException e) {
  1810.     //    errln("Caught a ParseException:");
  1811.     //    e.printStackTrace();
  1812.     //}
  1813.     
  1814.     //logln("The string " + s + " parsed as " + n);
  1815.     
  1816.     if(n.getDouble() != dbl) {
  1817.         errln("Round trip failure");
  1818.     }
  1819.     delete f;
  1820. }
  1821.  
  1822. /**
  1823.  * @bug 4162852
  1824.  * NumberFormat does not parse negative zero.
  1825.  */
  1826. void 
  1827. NumberFormatRegressionTest::Test4162852() 
  1828. {
  1829.     UErrorCode status = U_ZERO_ERROR;
  1830.     for(int32_t i=0; i < 2; ++i) {
  1831.         NumberFormat *f = (i == 0) ? NumberFormat::createInstance(status)
  1832.             : NumberFormat::createPercentInstance(status);
  1833.         if(U_FAILURE(status)) {
  1834.             errln("Couldn't create number format");
  1835.             return;
  1836.         }
  1837.         double d = 0.0;
  1838.         d *= -1.0;
  1839.         UnicodeString s;
  1840.         f->format(d, s);
  1841.         Formattable n;
  1842.         f->parse(s, n, status);
  1843.         if(U_FAILURE(status))
  1844.             errln("Couldn't parse!");
  1845.         double e = n.getDouble();
  1846.         logln(UnicodeString("") +
  1847.               d + " -> " +
  1848.               '"' + s + '"' + " -> " + e);
  1849.         if (e != 0.0 || 1.0/e > 0.0) {
  1850.             logln("Failed to parse negative zero");
  1851.         }
  1852.         delete f;
  1853.     }
  1854. }
  1855.  
  1856. static double _u_abs(double a) { return a<0?-a:a; }
  1857.  
  1858. /**
  1859.  * May 17 1999 sync up - liu
  1860.  * @bug 4167494
  1861.  * NumberFormat truncates data
  1862.  */
  1863. void NumberFormatRegressionTest::Test4167494() {
  1864.     UErrorCode status = U_ZERO_ERROR;
  1865.     NumberFormat *fmt = NumberFormat::createInstance(Locale::US, status);
  1866.     failure(status, "NumberFormat::createInstance");
  1867.  
  1868.     double a = DBL_MAX * 0.99; // DBL_MAX itself overflows to +Inf
  1869.     UnicodeString s;
  1870.     fmt->format(a, s);
  1871.     Formattable num;
  1872.     fmt->parse(s, num, status);
  1873.     failure(status, "Parse");
  1874.     if (num.getType() == Formattable::kDouble &&
  1875.         _u_abs(num.getDouble() - a) / a < 0.01) { // RT within 1%
  1876.         logln(UnicodeString("") + a + " -> \"" + s + "\" -> " +
  1877.               toString(num) + " ok");
  1878.     } else {
  1879.         errln(UnicodeString("") + a + " -> \"" + s + "\" -> " +
  1880.               toString(num) + " FAIL");
  1881.     }
  1882.  
  1883.     // We don't test Double.MIN_VALUE because the locale data for the US
  1884.     // currently doesn't specify enough digits to display Double.MIN_VALUE.
  1885.     // This is correct for now; however, we leave this here as a reminder
  1886.     // in case we want to address this later.
  1887.  
  1888.     delete fmt;
  1889. }
  1890.  
  1891. /**
  1892.  * May 17 1999 sync up - liu
  1893.  * @bug 4170798
  1894.  * DecimalFormat.parse() fails when ParseIntegerOnly set to true
  1895.  */
  1896. void NumberFormatRegressionTest::Test4170798() {
  1897.     UErrorCode status = U_ZERO_ERROR;
  1898.     NumberFormat *nf = NumberFormat::createInstance(Locale::US, status);
  1899.     failure(status, "NumberFormat::createInstance");
  1900.     if(nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  1901.         errln("DecimalFormat needed to continue");
  1902.         return;
  1903.     }
  1904.     DecimalFormat *df = (DecimalFormat*) nf;
  1905.     df->setParseIntegerOnly(TRUE);
  1906.     Formattable n;
  1907.     ParsePosition pos(0);
  1908.     df->parse("-0.0", n, pos);
  1909.     if (n.getType() != Formattable::kLong
  1910.         || n.getLong() != 0) {
  1911.         errln(UnicodeString("FAIL: parse(\"-0.0\") returns ") + toString(n));
  1912.     }
  1913. }
  1914.  
  1915. /**
  1916.  * May 17 1999 sync up - liu
  1917.  * toPattern only puts the first grouping separator in.
  1918.  */
  1919. void NumberFormatRegressionTest::Test4176114() {
  1920.     char* DATA[] = {
  1921.         "00", "#00",
  1922.         "000", "#000", // No grouping
  1923.         "#000", "#000", // No grouping
  1924.         "#,##0", "#,##0",
  1925.         "#,000", "#,000",
  1926.         "0,000", "#0,000",
  1927.         "00,000", "#00,000",
  1928.         "000,000", "#,000,000",
  1929.         "0,000,000,000,000.0000", "#0,000,000,000,000.0000", // Reported
  1930.     };
  1931.     int DATA_length = sizeof(DATA) / sizeof(DATA[0]);
  1932.     UErrorCode status = U_ZERO_ERROR;
  1933.     UnicodeString s;
  1934.     for (int i=0; i<DATA_length; i+=2) {
  1935.         DecimalFormat df(DATA[i], status);
  1936.         failure(status, "DecimalFormat constructor");
  1937.         df.toPattern(s);
  1938.         UnicodeString exp(DATA[i+1]);
  1939.         if (s != exp) {
  1940.             errln(UnicodeString("FAIL: ") + DATA[i] + " -> " +
  1941.                   s + ", want " + exp);
  1942.         }
  1943.     }
  1944. }
  1945.  
  1946. /**
  1947.  * May 17 1999 sync up - liu
  1948.  * @bug 4179818
  1949.  * DecimalFormat is incorrectly rounding numbers like 1.2501 to 1.2
  1950.  */
  1951. void NumberFormatRegressionTest::Test4179818() {
  1952.     char* DATA[] = {
  1953.         // Input  Pattern  Expected output
  1954.         "1.2511", "#.#",   "1.3",
  1955.         "1.2501", "#.#",   "1.3",
  1956.         "0.9999", "#",     "1",
  1957.     };
  1958.     int DATA_length = sizeof(DATA) / sizeof(DATA[0]); 
  1959.     double DOUBLE[] = {
  1960.         1.2511,
  1961.         1.2501,
  1962.         0.9999,
  1963.     };
  1964.     UErrorCode status = U_ZERO_ERROR;
  1965.     DecimalFormatSymbols sym(Locale::US, status);
  1966.     failure(status, "Construct DecimalFormatSymbols");
  1967.     DecimalFormat fmt("#", sym, status);
  1968.     failure(status, "Construct DecimalFormat");
  1969.     for (int i=0; i<DATA_length; i+=3) {
  1970.         double in = DOUBLE[i/3];
  1971.         UnicodeString pat(DATA[i+1]);
  1972.         UnicodeString exp(DATA[i+2]);
  1973.         fmt.applyPattern(pat, status);
  1974.         failure(status, "applyPattern");
  1975.         UnicodeString out;
  1976.         FieldPosition pos;
  1977.         fmt.format(in, out, pos);
  1978.         if (out == exp) {
  1979.             logln(UnicodeString("Ok: ") + in + " x " + pat + " = " + out);
  1980.         } else {
  1981.             errln(UnicodeString("FAIL: ") + in + " x  " + pat + " = " + out +
  1982.                   ", expected " + exp);
  1983.         }
  1984.     }
  1985. }
  1986.  
  1987. /**
  1988.  * May 17 1999 sync up - liu
  1989.  * Some DecimalFormatSymbols changes are not picked up by DecimalFormat.
  1990.  * This includes the minus sign, currency symbol, international currency
  1991.  * symbol, percent, and permille.  This is filed as bugs 4212072 and
  1992.  * 4212073.
  1993.  */
  1994. void NumberFormatRegressionTest::Test4212072() {
  1995.     UErrorCode status = U_ZERO_ERROR;
  1996.     DecimalFormatSymbols sym(Locale::US, status);
  1997.     failure(status, "DecimalFormatSymbols ct");
  1998.     DecimalFormat fmt(UnicodeString("#"), sym, status);
  1999.     failure(status, "DecimalFormat ct");
  2000.  
  2001.     UnicodeString s;
  2002.     FieldPosition pos;
  2003.  
  2004.     sym.setMinusSign('^');
  2005.     fmt.setDecimalFormatSymbols(sym);
  2006.     s.remove();
  2007.     if (fmt.format((int32_t)-1, s, pos) != UnicodeString("^1")) {
  2008.         errln(UnicodeString("FAIL: -1 x (minus=^) -> ") + s +
  2009.               ", exp ^1");
  2010.     }
  2011.     s.remove();
  2012.     if (fmt.getNegativePrefix(s) != UnicodeString("^")) {
  2013.         errln(UnicodeString("FAIL: (minus=^).getNegativePrefix -> ") +
  2014.               s + ", exp ^");
  2015.     }
  2016.     sym.setMinusSign('-');
  2017.  
  2018.     fmt.applyPattern(UnicodeString("#%"), status);
  2019.     failure(status, "applyPattern percent");
  2020.     sym.setPercent('^');
  2021.     fmt.setDecimalFormatSymbols(sym);
  2022.     s.remove();
  2023.     if (fmt.format(0.25, s, pos) != UnicodeString("25^")) {
  2024.         errln(UnicodeString("FAIL: 0.25 x (percent=^) -> ") + s +
  2025.               ", exp 25^");
  2026.     }
  2027.     s.remove();
  2028.     if (fmt.getPositiveSuffix(s) != UnicodeString("^")) {
  2029.         errln(UnicodeString("FAIL: (percent=^).getPositiveSuffix -> ") +
  2030.               s + ", exp ^");
  2031.     }
  2032.     sym.setPercent('%');
  2033.  
  2034.     fmt.applyPattern(str("#\\u2030"), status);
  2035.     failure(status, "applyPattern permill");
  2036.     sym.setPerMill('^');
  2037.     fmt.setDecimalFormatSymbols(sym);
  2038.     s.remove();
  2039.     if (fmt.format(0.25, s, pos) != UnicodeString("250^")) {
  2040.         errln(UnicodeString("FAIL: 0.25 x (permill=^) -> ") + s +
  2041.               ", exp 250^");
  2042.     }
  2043.     s.remove();
  2044.     if (fmt.getPositiveSuffix(s) != UnicodeString("^")) {
  2045.         errln(UnicodeString("FAIL: (permill=^).getPositiveSuffix -> ") +
  2046.               s + ", exp ^");
  2047.     }
  2048.     sym.setPerMill(0x2030);
  2049.  
  2050.     fmt.applyPattern(str("\\u00A4#.00"), status);
  2051.     failure(status, "applyPattern currency");
  2052.     sym.setCurrencySymbol("usd");
  2053.     fmt.setDecimalFormatSymbols(sym);
  2054.     s.remove();
  2055.     if (fmt.format(12.5, s, pos) != UnicodeString("usd12.50")) {
  2056.         errln(UnicodeString("FAIL: 12.5 x (currency=usd) -> ") + s +
  2057.               ", exp usd12.50");
  2058.     }
  2059.     s.remove();
  2060.     if (fmt.getPositivePrefix(s) != UnicodeString("usd")) {
  2061.         errln(UnicodeString("FAIL: (currency=usd).getPositivePrefix -> ") +
  2062.               s + ", exp usd");
  2063.     }
  2064.     sym.setCurrencySymbol("$");
  2065.  
  2066.     fmt.applyPattern(str("\\u00A4\\u00A4#.00"), status);
  2067.     failure(status, "applyPattern intl currency");
  2068.     sym.setInternationalCurrencySymbol("DOL");
  2069.     fmt.setDecimalFormatSymbols(sym);
  2070.     s.remove();
  2071.     if (fmt.format(12.5, s, pos) != UnicodeString("DOL12.50")) {
  2072.         errln(UnicodeString("FAIL: 12.5 x (intlcurrency=DOL) -> ") + s +
  2073.               ", exp DOL12.50");
  2074.     }
  2075.     s.remove();
  2076.     if (fmt.getPositivePrefix(s) != UnicodeString("DOL")) {
  2077.         errln(UnicodeString("FAIL: (intlcurrency=DOL).getPositivePrefix -> ") +
  2078.               s + ", exp DOL");
  2079.     }
  2080.     sym.setInternationalCurrencySymbol("USD");
  2081.  
  2082.     // Since the pattern logic has changed, make sure that patterns round
  2083.     // trip properly.  Test stream in/out integrity too.
  2084.     int32_t n;
  2085.     const Locale* avail = NumberFormat::getAvailableLocales(n);
  2086.     static char* type[] = {
  2087.         "",
  2088.         "$ ",
  2089.         "% ",
  2090.     };
  2091.     for (int i=0; i<n; ++i) {
  2092.         for (int j=0; j<3; ++j) {
  2093.             status = U_ZERO_ERROR;
  2094.             NumberFormat *nf;
  2095.             switch (j) {
  2096.             case 0:
  2097.                 nf = NumberFormat::createInstance(avail[i], status);
  2098.                 failure(status, "createInstance");
  2099.                 break;
  2100.             case 1:
  2101.                 nf = NumberFormat::createCurrencyInstance(avail[i], status);
  2102.                 failure(status, "createCurrencyInstance");
  2103.                 break;
  2104.             default:
  2105.                 nf = NumberFormat::createPercentInstance(avail[i], status);
  2106.                 failure(status, "createPercentInstance");
  2107.                 break;
  2108.             }
  2109.             if (U_FAILURE(status)) {
  2110.                 continue;
  2111.             }
  2112.             DecimalFormat *df = (DecimalFormat*) nf;
  2113.  
  2114.             // Test toPattern/applyPattern round trip
  2115.             UnicodeString pat;
  2116.             df->toPattern(pat);
  2117.             DecimalFormatSymbols symb(avail[i], status);
  2118.             failure(status, "Construct DecimalFormatSymbols");
  2119.             DecimalFormat f2(pat, symb, status);
  2120.             if (failure(status,
  2121.                         UnicodeString("Construct DecimalFormat(") + pat + ")")) {
  2122.                 continue;
  2123.             }
  2124.             if (*df != f2) {
  2125.                 UnicodeString l, p;
  2126.                 errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
  2127.                       " -> \"" + pat +
  2128.                       "\" -> \"" + f2.toPattern(p) + "\"");
  2129.             }
  2130.  
  2131.             // Test toLocalizedPattern/applyLocalizedPattern round trip
  2132.             df->toLocalizedPattern(pat);
  2133.             f2.applyLocalizedPattern(pat, status);
  2134.             failure(status,
  2135.                     UnicodeString("applyLocalizedPattern(") + pat + ")");
  2136.             if (U_FAILURE(status)) {
  2137.                 continue;
  2138.             }
  2139.             if (*df != f2) {
  2140.                 UnicodeString l, p;
  2141.                 errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayName(l) +
  2142.                       " -> localized \"" + pat +
  2143.                       "\" -> \"" + f2.toPattern(p) + "\"");
  2144.             }
  2145.  
  2146.             delete nf;
  2147.  
  2148.             // Test writeObject/readObject round trip
  2149.             // NOT ON ICU -- Java only
  2150.         }
  2151.     }
  2152. }
  2153.  
  2154. /**
  2155.  * May 17 1999 sync up - liu
  2156.  * DecimalFormat.parse() fails for mulipliers 2^n.
  2157.  */
  2158. void NumberFormatRegressionTest::Test4216742() {
  2159.     UErrorCode status = U_ZERO_ERROR;
  2160.     DecimalFormat *fmt = (DecimalFormat*) NumberFormat::createInstance(Locale::US, status);
  2161.     failure(status, "createInstance");
  2162.     int32_t DATA[] = { LONG_MIN, LONG_MAX, -100000000, 100000000 };
  2163.     int DATA_length = sizeof(DATA) / sizeof(DATA[0]);
  2164.     for (int i=0; i<DATA_length; ++i) {
  2165.         char buf[64];
  2166.         sprintf(buf, "%ld", DATA[i]);
  2167.         UnicodeString str(buf);
  2168.         for (int m = 1; m <= 100; m++) {
  2169.             fmt->setMultiplier(m);
  2170.             Formattable num;
  2171.             fmt->parse(str, num, status);
  2172.             failure(status, "parse");
  2173.             if (num.getType() != Formattable::kLong &&
  2174.                 num.getType() != Formattable::kDouble) {
  2175.                 errln(UnicodeString("FAIL: Wanted number, got ") +
  2176.                       toString(num));
  2177.             } else {
  2178.                 double d = num.getType() == Formattable::kDouble ?
  2179.                     num.getDouble() : (double) num.getLong();
  2180.                 if (d > 0 != DATA[i] > 0) {
  2181.                     errln(UnicodeString("\"") + str + "\" parse(x " +
  2182.                           fmt->getMultiplier() +
  2183.                           ") => " + toString(num));
  2184.                 }
  2185.             }
  2186.         }
  2187.     }
  2188.     delete fmt;
  2189. }
  2190.  
  2191. /**
  2192.  * May 17 1999 sync up - liu
  2193.  * DecimalFormat formats 1.001 to "1.00" instead of "1" with 2 fraction
  2194.  * digits.
  2195.  */
  2196. void NumberFormatRegressionTest::Test4217661() {
  2197.     double D[] = {  0.001, 1.001, 0.006,  1.006 };
  2198.     char*  S[] = { "0",   "1",   "0.01", "1.01" };
  2199.     int D_length = sizeof(D) / sizeof(D[0]);
  2200.     UErrorCode status = U_ZERO_ERROR;
  2201.     NumberFormat *fmt = NumberFormat::createInstance(Locale::US, status);
  2202.     failure(status, "createInstance");
  2203.     fmt->setMaximumFractionDigits(2);
  2204.     for (int i=0; i<D_length; i++) {
  2205.         UnicodeString s;
  2206.         fmt->format(D[i], s);
  2207.         if (s != UnicodeString(S[i])) {
  2208.             errln(UnicodeString("FAIL: Got ") + s + ", exp " + S[i]); 
  2209.         }
  2210.     }
  2211.     delete fmt;
  2212. }
  2213.  
  2214. /**
  2215.  * alphaWorks upgrade
  2216.  */
  2217. void NumberFormatRegressionTest::Test4161100() {
  2218.     UErrorCode status = U_ZERO_ERROR;
  2219.     NumberFormat *nf = NumberFormat::createInstance(Locale::US, status);
  2220.     failure(status, "createInstance");
  2221.     nf->setMinimumFractionDigits(1);
  2222.     nf->setMaximumFractionDigits(1);
  2223.     double a = -0.09;
  2224.     UnicodeString s;
  2225.     nf->format(a, s);
  2226.     UnicodeString pat;
  2227.     logln(UnicodeString() + a + " x " +
  2228.           ((DecimalFormat*) nf)->toPattern(pat) + " = " + s);
  2229.     if (s != UnicodeString("-0.1")) {
  2230.         errln("FAIL");
  2231.     }
  2232.     delete nf;
  2233. }
  2234.  
  2235. /**
  2236.  * June 16 1999 sync up - liu
  2237.  * Formatting .5 rounds to "1" instead of "0". (Regression in 1.2.2 RC1)
  2238.  */
  2239. void NumberFormatRegressionTest::Test4243011() {
  2240.     UErrorCode status = U_ZERO_ERROR;
  2241.     DecimalFormatSymbols sym(Locale::US, status);
  2242.     failure(status, "DecimalFormatSymbols ct");
  2243.     DecimalFormat fmt(UnicodeString("0."), sym, status);
  2244.     failure(status, "DecimalFormat ct");
  2245.  
  2246.     double NUM[] = {  -2.5,  -1.5,  -0.5,  0.5,  1.5,  2.5,  3.5,  4.5 };
  2247.     char*  STR[] = { "-2.", "-2.", "-0.", "0.", "2.", "2.", "4.", "4." };
  2248.     int32_t  N   = sizeof(NUM) / sizeof(NUM[0]);
  2249.  
  2250.     for (int32_t i=0; i<N; ++i) {
  2251.         UnicodeString str;
  2252.         UnicodeString exp(STR[i]);
  2253.         FieldPosition pos;
  2254.         fmt.format(NUM[i], str, pos);
  2255.         if (str == exp) {
  2256.             logln(UnicodeString("Ok   ") + NUM[i] + " x 0. = " + str);
  2257.         } else {
  2258.             errln(UnicodeString("FAIL ") + NUM[i] + " x 0. = " + str +
  2259.                   ", want " + exp);
  2260.         }
  2261.     }
  2262. }
  2263.  
  2264. /**
  2265.  * June 16 1999 sync up - liu
  2266.  * format(0.0) gives "0.1" if preceded by parse("99.99").
  2267.  * (Regression in 1.2.2 RC1)
  2268.  */
  2269. void NumberFormatRegressionTest::Test4243108() {
  2270.     UErrorCode status = U_ZERO_ERROR;
  2271.     DecimalFormatSymbols sym(Locale::US, status);
  2272.     failure(status, "DecimalFormatSymbols ct");
  2273.     DecimalFormat fmt(UnicodeString("#.#"), sym, status);
  2274.     failure(status, "DecimalFormat ct");
  2275.     
  2276.     UnicodeString str;
  2277.     FieldPosition pos;
  2278.  
  2279.     fmt.format(0.0, str, pos);
  2280.     UnicodeString exp("0");
  2281.     if (str == exp) {
  2282.         logln(UnicodeString("Ok   0.0 x #.# = ") + str);
  2283.     } else {
  2284.         errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
  2285.               ", want " + exp);
  2286.     }
  2287.  
  2288.     str = "99.99";
  2289.     Formattable val;
  2290.     fmt.parse(str, val, status);
  2291.     failure(status, "DecimalFormat.parse(99.99)");
  2292.     if (val.getType() == Formattable::kDouble &&
  2293.         val.getDouble() == 99.99) {
  2294.         logln(UnicodeString("Ok   99.99 / #.# = ") + toString(val));
  2295.     } else {
  2296.         errln(UnicodeString("FAIL 99.99 / #.# = ") + toString(val) +
  2297.               ", want " + 99.99);
  2298.     }
  2299.  
  2300.     str.remove();
  2301.     fmt.format(0.0, str, pos);
  2302.     if (str == exp) {
  2303.         logln(UnicodeString("Ok   0.0 x #.# = ") + str);
  2304.     } else {
  2305.         errln(UnicodeString("FAIL 0.0 x #.# = ") + str +
  2306.               ", want " + exp);
  2307.     }    
  2308. }
  2309.