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

  1.  
  2. /*
  3. ********************************************************************
  4. * COPYRIGHT: 
  5. * (C) Copyright Taligent, Inc., 1997
  6. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  7. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  8. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  9. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  10. *
  11. ********************************************************************
  12. * Modification History:
  13. *   Date        Name        Description
  14. *   07/15/99    helena      Ported to HPUX 10/11 CC.
  15. */
  16.  
  17. #include "numfmtst.h"
  18. #include "dcfmtsym.h"
  19. #include "decimfmt.h"
  20. #include "locid.h"
  21.  
  22. // *****************************************************************************
  23. // class NumberFormatTest
  24. // *****************************************************************************
  25.  
  26. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  27.  
  28. #define CHECK(status,str) if (U_FAILURE(status)) { errln(UnicodeString("FAIL: ") + str); return; }
  29.  
  30. void NumberFormatTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  31. {
  32.     // if (exec) logln((UnicodeString)"TestSuite DateFormatTest");
  33.     switch (index) {
  34.         CASE(0,TestCurrencySign)
  35.         CASE(1,TestCurrency)
  36.         CASE(2,TestParse)
  37.         CASE(3,TestRounding487)
  38.         CASE(4,TestQuotes)
  39.         CASE(5,TestExponential)
  40.         CASE(6,TestPatterns)
  41.  
  42.         // Upgrade to alphaWorks - liu 5/99
  43.         CASE(7,TestExponent)
  44.         CASE(8,TestScientific)
  45.         CASE(9,TestPad)
  46.         CASE(10,TestPatterns2)
  47.  
  48.         default: name = ""; break;
  49.     }
  50. }
  51.  
  52. // -------------------------------------
  53.  
  54. // Test various patterns
  55. void
  56. NumberFormatTest::TestPatterns()
  57. {
  58.     UErrorCode status = U_ZERO_ERROR;
  59.     DecimalFormatSymbols sym(Locale::US, status);
  60.     if (U_FAILURE(status)) { errln("FAIL: Could not construct DecimalFormatSymbols"); return; }
  61.  
  62.     const char* pat[]    = { "#.#", "#.", ".#", "#" };
  63.     int32_t pat_length = sizeof(pat) / sizeof(pat[0]);
  64.     const char* newpat[] = { "#0.#", "#0.", "#.0", "#" };
  65.     const char* num[]    = { "0",   "0.", ".0", "0" };
  66.     for (int32_t i=0; i<pat_length; ++i)
  67.     {
  68.         status = U_ZERO_ERROR;
  69.         DecimalFormat fmt(pat[i], sym, status);
  70.         if (U_FAILURE(status)) { errln((UnicodeString)"FAIL: DecimalFormat constructor failed for " + pat[i]); continue; }
  71.         UnicodeString newp; fmt.toPattern(newp);
  72.         if (!(newp == newpat[i]))
  73.             errln((UnicodeString)"FAIL: Pattern " + pat[i] + " should transmute to " + newpat[i] +
  74.                   "; " + newp + " seen instead");
  75.  
  76.         UnicodeString s; (*(NumberFormat*)&fmt).format(T_INT32(0), s);
  77.         if (!(s == num[i]))
  78.         {
  79.             errln((UnicodeString)"FAIL: Pattern " + pat[i] + " should format zero as " + num[i] +
  80.                   "; " + s + " seen instead");
  81.             logln((UnicodeString)"Min integer digits = " + fmt.getMinimumIntegerDigits());
  82.         }
  83.     }
  84. }
  85.  
  86. // -------------------------------------
  87.  
  88. // Test exponential pattern
  89. void
  90. NumberFormatTest::TestExponential()
  91. {
  92.     UErrorCode status = U_ZERO_ERROR;
  93.     DecimalFormatSymbols sym(Locale::US, status);
  94.     if (U_FAILURE(status)) { errln("FAIL: Bad status returned by DecimalFormatSymbols ct"); return; }
  95.     char* pat[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]"  };
  96.     int32_t pat_length = sizeof(pat) / sizeof(pat[0]);
  97.     double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
  98.     int32_t val_length = sizeof(val) / sizeof(val[0]);
  99.     int32_t lval[] = { 0, -1, 1, 123456789 };
  100.     int32_t lval_length = sizeof(lval) / sizeof(lval[0]);
  101.     char* valFormat[] =
  102.     {
  103.         // 0.####E0
  104.         "1.234E-2", "1.2346E8", "1.23E300", "-3.1416E-271",
  105.         // 00.000E00
  106.         "12.340E-03", "12.346E07", "12.300E299", "-31.416E-272",
  107.         // ##0.######E000
  108.         "12.34E-003", "123.4568E006", "1.23E300", "-314.1593E-273",
  109.         // 0.###E0;[0.###E0]
  110.         "1.234E-2", "1.235E8", "1.23E300", "[3.142E-271]"
  111.     };
  112.     char* lvalFormat[] =
  113.     {
  114.         // 0.####E0
  115.         "0E0", "-1E0", "1E0", "1.2346E8",
  116.         // 00.000E00
  117.         "00.000E00", "-10.000E-01", "10.000E-01", "12.346E07",
  118.         // ##0.######E000
  119.         "0E000", "-1E000", "1E000", "123.4568E006",
  120.         // 0.###E0;[0.###E0]
  121.         "0E0", "[1E0]", "1E0", "1.235E8"
  122.     };
  123.     double valParse[] =
  124.     {
  125.         0.01234, 123460000, 1.23E300, -3.1416E-271,
  126.         0.01234, 123460000, 1.23E300, -3.1416E-271,
  127.         0.01234, 123456800, 1.23E300, -3.141593E-271,
  128.         0.01234, 123500000, 1.23E300, -3.142E-271,
  129.     };
  130.     int32_t lvalParse[] =
  131.     {
  132.         0, -1, 1, 123460000,
  133.         0, -1, 1, 123460000,
  134.         0, -1, 1, 123456800,
  135.         0, -1, 1, 123500000,
  136.     };
  137.     int32_t ival = 0, ilval = 0;
  138.     for (int32_t p=0; p<pat_length; ++p)
  139.     {
  140.         DecimalFormat fmt(pat[p], sym, status);
  141.         if (U_FAILURE(status)) { errln("FAIL: Bad status returned by DecimalFormat ct"); continue; }
  142.         UnicodeString pattern;
  143.         logln((UnicodeString)"Pattern \"" + pat[p] + "\" -toPattern-> \"" +
  144.           fmt.toPattern(pattern) + "\"");
  145.         int32_t v;
  146.         for (v=0; v<val_length; ++v)
  147.         {
  148.             UnicodeString s; (*(NumberFormat*)&fmt).format(val[v], s);
  149.             logln((UnicodeString)" " + val[v] + " -format-> " + s);
  150.             if (s != valFormat[v+ival])
  151.                 errln((UnicodeString)"FAIL: Expected " + valFormat[v+ival]);
  152.  
  153.             ParsePosition pos(0);
  154.             Formattable af;
  155.             fmt.parse(s, af, pos);
  156.             double a;
  157.             if (af.getType() == Formattable::kLong) a = af.getLong();
  158.             else if (af.getType() == Formattable::kDouble) a = af.getDouble();
  159.             else errln((UnicodeString)"FAIL: Non-numeric Formattable returned");
  160.             if (pos.getIndex() == s.size())
  161.             {
  162.                 logln((UnicodeString)"  -parse-> " + a);
  163.                 if (a != valParse[v+ival])
  164.                 errln((UnicodeString)"FAIL: Expected " + valParse[v+ival]);
  165.             }
  166.             else
  167.                 errln((UnicodeString)"FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + a);
  168.         }
  169.         for (v=0; v<lval_length; ++v)
  170.         {
  171.             UnicodeString s; (*(NumberFormat*)&fmt).format(lval[v], s);
  172.             logln((UnicodeString)" " + lval[v] + "L -format-> " + s);
  173.             if (s != lvalFormat[v+ilval])
  174.                 errln((UnicodeString)"ERROR: Expected " + lvalFormat[v+ilval]);
  175.  
  176.             ParsePosition pos(0);
  177.             Formattable af;
  178.             fmt.parse(s, af, pos);
  179.             int32_t a;
  180.             if (af.getType() == Formattable::kLong) a = af.getLong();
  181.             else errln((UnicodeString)"FAIL: Non-long Formattable returned");
  182.             if (pos.getIndex() == s.size())
  183.             {
  184.                 logln((UnicodeString)"  -parse-> " + a);
  185.                 if (a != lvalParse[v+ilval])
  186.                 errln((UnicodeString)"FAIL: Expected " + lvalParse[v+ilval]);
  187.             }
  188.             else
  189.                 errln((UnicodeString)"FAIL: Partial parse (" + pos.getIndex() + " chars) -> " + a);
  190.         }
  191.         ival += val_length;
  192.         ilval += lval_length;
  193.     }
  194. }
  195.  
  196. // -------------------------------------
  197.  
  198. // Test the handling of quotes
  199. void
  200. NumberFormatTest::TestQuotes()
  201. {
  202.     UErrorCode status = U_ZERO_ERROR;
  203.     UnicodeString *pat;
  204.     DecimalFormatSymbols *sym = new DecimalFormatSymbols(Locale::US, status);
  205.     pat = new UnicodeString("a'fo''o'b#");
  206.     DecimalFormat *fmt = new DecimalFormat(*pat, *sym, status);
  207.     UnicodeString s; 
  208.     ((NumberFormat*)fmt)->format(T_INT32(123), s);
  209.     logln((UnicodeString)"Pattern \"" + *pat + "\"");
  210.     logln((UnicodeString)" Format 123 -> " + escape(s));
  211.     if (!(s=="afo'ob123")) 
  212.         errln((UnicodeString)"FAIL: Expected afo'ob123");
  213.     
  214.     s.truncate(0);
  215.     delete fmt;
  216.     delete pat;
  217.  
  218.     pat = new UnicodeString("a''b#");
  219.     fmt = new DecimalFormat(*pat, *sym, status);
  220.     ((NumberFormat*)fmt)->format(T_INT32(123), s);
  221.     logln((UnicodeString)"Pattern \"" + *pat + "\"");
  222.     logln((UnicodeString)" Format 123 -> " + escape(s));
  223.     if (!(s=="a'b123")) 
  224.         errln((UnicodeString)"FAIL: Expected a'b123");
  225.     delete fmt;
  226.     delete pat;
  227.     delete sym;
  228. }
  229.  
  230. /**
  231.  * Test the handling of the currency symbol in patterns.
  232.  */
  233. void
  234. NumberFormatTest::TestCurrencySign()
  235. {
  236.     UErrorCode status = U_ZERO_ERROR;
  237.     DecimalFormatSymbols* sym = new DecimalFormatSymbols(Locale::US, status);
  238.     DecimalFormat *fmt = new DecimalFormat("\xA4#,##0.00;-\xA4#,##0.00", *sym, status);
  239.     UnicodeString s; ((NumberFormat*)fmt)->format(1234.56, s);
  240.     UnicodeString pat;
  241.     logln((UnicodeString)"Pattern \"" + fmt->toPattern(pat) + "\"");
  242.     logln((UnicodeString)" Format " + 1234.56 + " -> " + escape(s));
  243.     if (s != "$1,234.56") errln((UnicodeString)"FAIL: Expected $1,234.56");
  244.     s.truncate(0);
  245.     ((NumberFormat*)fmt)->format(- 1234.56, s);
  246.     logln((UnicodeString)" Format " + (-1234.56) + " -> " + escape(s));
  247.     if (s != "-$1,234.56") errln((UnicodeString)"FAIL: Expected -$1,234.56");
  248.     delete fmt;
  249.     fmt = new DecimalFormat("\xA4\xA4 #,##0.00;\xA4\xA4 -#,##0.00", *sym, status);
  250.     s.truncate(0);
  251.     ((NumberFormat*)fmt)->format(1234.56, s);
  252.     logln((UnicodeString)"Pattern \"" + fmt->toPattern(pat) + "\"");
  253.     logln((UnicodeString)" Format " + 1234.56 + " -> " + escape(s));
  254.     if (s != "USD 1,234.56") errln((UnicodeString)"FAIL: Expected USD 1,234.56");
  255.     s.truncate(0);
  256.     ((NumberFormat*)fmt)->format(-1234.56, s);
  257.     logln((UnicodeString)" Format " + (-1234.56) + " -> " + escape(s));
  258.     if (s != "USD -1,234.56") errln((UnicodeString)"FAIL: Expected USD -1,234.56");
  259.     delete fmt;
  260.     delete sym;
  261.     if (U_FAILURE(status)) errln((UnicodeString)"FAIL: Status " + (int32_t)status);
  262. }
  263.  
  264. // -------------------------------------
  265.  
  266. static char toHexString(int32_t i) { return i + (i < 10 ? '0' : ('A' - 10)); }
  267.  
  268. UnicodeString&
  269. NumberFormatTest::escape(UnicodeString& s)
  270. {
  271.     UnicodeString buf;
  272.     for (int32_t i=0; i<s.size(); ++i)
  273.     {
  274.         UChar c = s[(UTextOffset)i];
  275.         if (c <= (UChar)0x7F) buf += c;
  276.         else {
  277.             buf += '\\'; buf += 'U';
  278.             buf += toHexString((c & 0xF000) >> 12);
  279.             buf += toHexString((c & 0x0F00) >> 8);
  280.             buf += toHexString((c & 0x00F0) >> 4);
  281.             buf += toHexString(c & 0x000F);
  282.         }
  283.     }
  284.     return (s = buf);
  285. }
  286.  
  287.  
  288. // -------------------------------------
  289.  
  290. /**
  291.  * Test localized currency patterns.
  292.  */
  293. void
  294. NumberFormatTest::TestCurrency()
  295. {
  296.     UErrorCode status = U_ZERO_ERROR;
  297.     NumberFormat* currencyFmt = NumberFormat::createCurrencyInstance(Locale::CANADA_FRENCH, status);
  298.     UnicodeString s; currencyFmt->format(1.50, s);
  299.     logln((UnicodeString)"Un pauvre ici a..........." + s);
  300.     if (!(s=="1,50 $")) errln((UnicodeString)"FAIL: Expected 1,50 $");
  301.     delete currencyFmt;
  302.     s.truncate(0);
  303.     currencyFmt = NumberFormat::createCurrencyInstance(Locale::GERMANY, status);
  304.     currencyFmt->format(1.50, s);
  305.     logln((UnicodeString)"Un pauvre en Allemagne a.." + s);
  306.     if (!(s=="1,50 DM")) errln((UnicodeString)"FAIL: Expected 1,50 DM");
  307.     delete currencyFmt;
  308.     s.truncate(0);
  309.     currencyFmt = NumberFormat::createCurrencyInstance(Locale::FRANCE, status);
  310.     currencyFmt->format(1.50, s);
  311.     logln((UnicodeString)"Un pauvre en France a....." + s);
  312.     if (!(s=="1,50 F")) errln((UnicodeString)"FAIL: Expected 1,50 F");
  313.     delete currencyFmt;
  314.     if (U_FAILURE(status)) errln((UnicodeString)"FAIL: Status " + (int32_t)status);
  315. }
  316.  
  317. // -------------------------------------
  318.  
  319. /**
  320.  * Do rudimentary testing of parsing.
  321.  */
  322. void
  323. NumberFormatTest::TestParse()
  324. {
  325.     UErrorCode status = U_ZERO_ERROR;
  326.     UnicodeString arg("0");
  327.     DecimalFormat* format = new DecimalFormat("00", status);
  328.     //try {
  329.         Formattable n; format->parse(arg, n, status);
  330.         logln((UnicodeString)"parse(" + arg + ") = " + n.getLong());
  331.         if (n.getType() != Formattable::kLong ||
  332.             n.getLong() != 0) errln((UnicodeString)"FAIL: Expected 0");
  333.     delete format;
  334.     if (U_FAILURE(status)) errln((UnicodeString)"FAIL: Status " + (int32_t)status);
  335.     //}
  336.     //catch(Exception e) {
  337.     //    errln((UnicodeString)"Exception caught: " + e);
  338.     //}
  339. }
  340.  
  341. // -------------------------------------
  342.  
  343. /**
  344.  * Test proper rounding by the format method.
  345.  */
  346. void
  347. NumberFormatTest::TestRounding487()
  348. {
  349.     UErrorCode status = U_ZERO_ERROR;
  350.     NumberFormat *nf = NumberFormat::createInstance(status);
  351.     roundingTest(*nf, 0.00159999, 4, "0.0016");
  352.     roundingTest(*nf, 0.00995, 4, "0.01");
  353.  
  354.     roundingTest(*nf, 12.3995, 3, "12.4");
  355.  
  356.     roundingTest(*nf, 12.4999, 0, "12");
  357.     roundingTest(*nf, - 19.5, 0, "-20");
  358.     delete nf;
  359.     if (U_FAILURE(status)) errln((UnicodeString)"FAIL: Status " + (int32_t)status);
  360. }
  361.  
  362. // -------------------------------------
  363.  
  364. void
  365. NumberFormatTest::roundingTest(NumberFormat& nf, double x, int32_t maxFractionDigits, const char* expected)
  366. {
  367.     nf.setMaximumFractionDigits(maxFractionDigits);
  368.     UnicodeString out; nf.format(x, out);
  369.     logln((UnicodeString)"" + x + " formats with " + maxFractionDigits + " fractional digits to " + out);
  370.     if (!(out==expected)) errln((UnicodeString)"FAIL: Expected " + expected);
  371. }
  372.  
  373. /**
  374.  * Upgrade to alphaWorks
  375.  */
  376. void NumberFormatTest::expect(NumberFormat& fmt, const UnicodeString& str, int32_t n) {
  377.     UErrorCode status = U_ZERO_ERROR;
  378.     Formattable num;
  379.     fmt.parse(str, num, status);
  380.     CHECK(status, "NumberFormat.parse");
  381.     UnicodeString pat;
  382.     ((DecimalFormat*) &fmt)->toPattern(pat);
  383.     if (num.getType() == Formattable::kLong &&
  384.         num.getLong() == n) {
  385.         logln(UnicodeString("Ok   \"") + str + "\" x " +
  386.               pat + " = " +
  387.               toString(num));
  388.     } else {
  389.         errln(UnicodeString("FAIL \"") + str + "\" x " +
  390.               pat + " = " +
  391.               toString(num) + ", expected " + n + "L");
  392.     }
  393. }
  394.  
  395. /**
  396.  * Upgrade to alphaWorks
  397.  */
  398. void NumberFormatTest::expect(NumberFormat& fmt, const Formattable& n,
  399.                               const UnicodeString& exp) {
  400.     UnicodeString saw;
  401.     FieldPosition pos;
  402.     UErrorCode status = U_ZERO_ERROR;
  403.     fmt.format(n, saw, pos, status);
  404.     CHECK(status, "format");
  405.     UnicodeString pat;
  406.     ((DecimalFormat*) &fmt)->toPattern(pat);
  407.     if (saw == exp) {
  408.         logln(UnicodeString("Ok   ") + toString(n) + " x " +
  409.               pat + " = \"" +
  410.               saw + "\"");
  411.     } else {
  412.         errln(UnicodeString("FAIL ") + toString(n) + " x " +
  413.               pat + " = \"" +
  414.               saw + "\", expected \"" + exp + "\"");
  415.     }
  416. }
  417.  
  418. void NumberFormatTest::expect(NumberFormat* fmt, const Formattable& n,
  419.                               const UnicodeString& exp,
  420.                               UErrorCode status) {
  421.     CHECK(status, "construct format");
  422.     expect(*fmt, n, exp);
  423.     delete fmt;
  424. }
  425.  
  426. /**
  427.  * Upgrade to alphaWorks
  428.  */
  429. void NumberFormatTest::TestExponent() {
  430.     UErrorCode status = U_ZERO_ERROR;
  431.     DecimalFormatSymbols US(Locale::US, status);
  432.     CHECK(status, "DecimalFormatSymbols constructor");
  433.     DecimalFormat fmt1(UnicodeString("0.###E0"), US, status);
  434.     CHECK(status, "DecimalFormat(0.###E0)");
  435.     DecimalFormat fmt2(UnicodeString("0.###E+0"), US, status);
  436.     CHECK(status, "DecimalFormat(0.###E+0)");
  437.     int32_t n = 1234;
  438.     expect(fmt1, n, "1.234E3");
  439.     expect(fmt2, n, "1.234E+3");
  440.     expect(fmt1, "1.234E3", n);
  441.     expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"
  442.     expect(fmt2, "1.234E+3", n);
  443. }
  444.  
  445. /**
  446.  * Upgrade to alphaWorks
  447.  */
  448. void NumberFormatTest::TestScientific() {
  449.     UErrorCode status = U_ZERO_ERROR;
  450.     DecimalFormatSymbols US(Locale::US, status);
  451.     CHECK(status, "DecimalFormatSymbols constructor");
  452.  
  453.     // Test pattern round-trip
  454.     char* PAT[] = { "#E0", "0.####E0", "00.000E00", "##0.####E000",
  455.                     "0.###E0;[0.###E0]" };
  456.     int32_t PAT_length = sizeof(PAT) / sizeof(PAT[0]);
  457.     int32_t DIGITS[] = {
  458.         // min int, max int, min frac, max frac
  459.         0, 1, 0, 0, // "#E0"
  460.         1, 1, 0, 4, // "0.####E0"
  461.         2, 2, 3, 3, // "00.000E00"
  462.         1, 3, 0, 4, // "##0.####E000"
  463.         1, 1, 0, 3, // "0.###E0;[0.###E0]"
  464.     };
  465.     for (int32_t i=0; i<PAT_length; ++i) {
  466.         UnicodeString pat(PAT[i]);
  467.         DecimalFormat df(pat, US, status);
  468.         CHECK(status, "DecimalFormat constructor");
  469.         UnicodeString pat2;
  470.         df.toPattern(pat2);
  471.         if (pat == pat2) {
  472.             logln(UnicodeString("Ok   Pattern rt \"") +
  473.                   pat + "\" -> \"" +
  474.                   pat2 + "\"");
  475.         } else {
  476.             errln(UnicodeString("FAIL Pattern rt \"") +
  477.                   pat + "\" -> \"" +
  478.                   pat2 + "\"");
  479.         }
  480.         // Make sure digit counts match what we expect
  481.         if (df.getMinimumIntegerDigits() != DIGITS[4*i] ||
  482.             df.getMaximumIntegerDigits() != DIGITS[4*i+1] ||
  483.             df.getMinimumFractionDigits() != DIGITS[4*i+2] ||
  484.             df.getMaximumFractionDigits() != DIGITS[4*i+3]) {
  485.             errln(UnicodeString("FAIL \"" + pat +
  486.                                 "\" min/max int; min/max frac = ") +
  487.                   df.getMinimumIntegerDigits() + "/" +
  488.                   df.getMaximumIntegerDigits() + ";" +
  489.                   df.getMinimumFractionDigits() + "/" +
  490.                   df.getMaximumFractionDigits() + ", expect " +
  491.                   DIGITS[4*i] + "/" +
  492.                   DIGITS[4*i+1] + ";" +
  493.                   DIGITS[4*i+2] + "/" +
  494.                   DIGITS[4*i+3]);
  495.         }
  496.     }
  497.  
  498.     expect(new DecimalFormat("#E0", US, status),
  499.            12345.0,
  500.            "1.2345E4", status);
  501.     expect(new DecimalFormat("0E0", US, status),
  502.            12345.0,
  503.            "1E4", status);
  504.     expect(NumberFormat::createScientificInstance(Locale::US, status),
  505.            12345.678901,
  506.            "1.2345678901E4", status);
  507.     expect(new DecimalFormat("##0.###E0", US, status),
  508.            12345.0,
  509.            "12.34E3", status);
  510.     expect(new DecimalFormat("##0.###E0", US, status),
  511.            12345.00001,
  512.            "12.35E3", status);
  513.     expect(new DecimalFormat("##0.####E0", US, status),
  514.            (int32_t) 12345,
  515.            "12.345E3", status);
  516.     expect(NumberFormat::createScientificInstance(Locale::FRANCE, status),
  517.            12345.678901,
  518.            "1,2345678901E4", status);
  519.     expect(new DecimalFormat("##0.####E0", US, status),
  520.            789.12345e-9,
  521.            "789.12E-9", status);
  522.     expect(new DecimalFormat("##0.####E0", US, status),
  523.            780.e-9,
  524.            "780E-9", status);
  525.     expect(new DecimalFormat(".###E0", US, status),
  526.            45678.0,
  527.            ".457E5", status);
  528.     expect(new DecimalFormat(".###E0", US, status),
  529.            (int32_t) 0,
  530.            ".0E0", status);
  531.     /*
  532.     expect(new DecimalFormat[] { new DecimalFormat("#E0", US),
  533.                                  new DecimalFormat("##E0", US),
  534.                                  new DecimalFormat("####E0", US),
  535.                                  new DecimalFormat("0E0", US),    
  536.                                  new DecimalFormat("00E0", US),   
  537.                                  new DecimalFormat("000E0", US), 
  538.                                },
  539.            new Long(45678000),
  540.            new String[] { "4.5678E7",
  541.                           "45.678E6",
  542.                           "4567.8E4",
  543.                           "5E7",
  544.                           "46E6",  
  545.                           "457E5",
  546.                         }
  547.            );
  548.     !
  549.     ! Unroll this test into individual tests below...
  550.     !
  551.     */
  552.     expect(new DecimalFormat("#E0", US, status),
  553.            (int32_t) 45678000, "4.5678E7", status);
  554.     expect(new DecimalFormat("##E0", US, status),
  555.            (int32_t) 45678000, "45.678E6", status);
  556.     expect(new DecimalFormat("####E0", US, status),
  557.            (int32_t) 45678000, "4567.8E4", status);
  558.     expect(new DecimalFormat("0E0", US, status),
  559.            (int32_t) 45678000, "5E7", status);
  560.     expect(new DecimalFormat("00E0", US, status),
  561.            (int32_t) 45678000, "46E6", status);
  562.     expect(new DecimalFormat("000E0", US, status),
  563.            (int32_t) 45678000, "457E5", status);
  564.     /*
  565.     expect(new DecimalFormat("###E0", US, status),
  566.            new Object[] { new Double(0.0000123), "12.3E-6",
  567.                           new Double(0.000123), "123E-6",
  568.                           new Double(0.00123), "1.23E-3",
  569.                           new Double(0.0123), "12.3E-3",
  570.                           new Double(0.123), "123E-3",
  571.                           new Double(1.23), "1.23E0",
  572.                           new Double(12.3), "12.3E0",
  573.                           new Double(123), "123E0",
  574.                           new Double(1230), "1.23E3",
  575.                          });
  576.     !
  577.     ! Unroll this test into individual tests below...
  578.     !
  579.     */
  580.     expect(new DecimalFormat("###E0", US, status),
  581.            0.0000123, "12.3E-6", status);
  582.     expect(new DecimalFormat("###E0", US, status),
  583.            0.000123, "123E-6", status);
  584.     expect(new DecimalFormat("###E0", US, status),
  585.            0.00123, "1.23E-3", status);
  586.     expect(new DecimalFormat("###E0", US, status),
  587.            0.0123, "12.3E-3", status);
  588.     expect(new DecimalFormat("###E0", US, status),
  589.            0.123, "123E-3", status);
  590.     expect(new DecimalFormat("###E0", US, status),
  591.            1.23, "1.23E0", status);
  592.     expect(new DecimalFormat("###E0", US, status),
  593.            12.3, "12.3E0", status);
  594.     expect(new DecimalFormat("###E0", US, status),
  595.            123.0, "123E0", status);
  596.     expect(new DecimalFormat("###E0", US, status),
  597.            1230.0, "1.23E3", status);
  598.     /*
  599.     expect(new DecimalFormat("0.#E+00", US, status),
  600.            new Object[] { new Double(0.00012), "1.2E-04",
  601.                           new Long(12000),     "1.2E+04",
  602.                          });
  603.     !
  604.     ! Unroll this test into individual tests below...
  605.     !
  606.     */
  607.     expect(new DecimalFormat("0.#E+00", US, status),
  608.            0.00012, "1.2E-04", status);
  609.     expect(new DecimalFormat("0.#E+00", US, status),
  610.            (int32_t) 12000, "1.2E+04", status);
  611. }
  612.  
  613. /**
  614.  * Upgrade to alphaWorks
  615.  */
  616. void NumberFormatTest::TestPad() {
  617.     UErrorCode status = U_ZERO_ERROR;
  618.     DecimalFormatSymbols US(Locale::US, status);
  619.     CHECK(status, "DecimalFormatSymbols constructor");
  620.  
  621.     expect(new DecimalFormat("*^##.##", US, status),
  622.            int32_t(0), "^^^^0", status);
  623.     expect(new DecimalFormat("*^##.##", US, status),
  624.            -1.3, "^-1.3", status);
  625.     expect(new DecimalFormat("##0.0####E0*_ g-m/s^2", US, status),
  626.            int32_t(0), "0.0E0______ g-m/s^2", status);
  627.     expect(new DecimalFormat("##0.0####E0*_ g-m/s^2", US, status),
  628.            1.0/3, "333.333E-3_ g-m/s^2", status);
  629.     expect(new DecimalFormat("##0.0####*_ g-m/s^2", US, status),
  630.            int32_t(0), "0.0______ g-m/s^2", status);
  631.     expect(new DecimalFormat("##0.0####*_ g-m/s^2", US, status),
  632.            1.0/3, "0.33333__ g-m/s^2", status);
  633. }
  634.  
  635. /**
  636.  * Upgrade to alphaWorks
  637.  */
  638. void NumberFormatTest::TestPatterns2() {
  639.     UErrorCode status = U_ZERO_ERROR;
  640.     DecimalFormatSymbols US(Locale::US, status);
  641.     CHECK(status, "DecimalFormatSymbols constructor");
  642.  
  643.     DecimalFormat fmt("#", US, status);
  644.     CHECK(status, "DecimalFormat constructor");
  645.  
  646.     expectPad(fmt, "*^#", DecimalFormat::kPadBeforePrefix, 1, '^');
  647.     expectPad(fmt, "$*^#", DecimalFormat::kPadAfterPrefix, 2, '^');
  648.     expectPad(fmt, "#*^", DecimalFormat::kPadBeforeSuffix, 1, '^');
  649.     expectPad(fmt, "#$*^", DecimalFormat::kPadAfterSuffix, 2, '^');
  650.     expectPad(fmt, "$*^$#", ILLEGAL);
  651.     expectPad(fmt, "#$*^$", ILLEGAL);
  652.     expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat::kPadBeforeSuffix,
  653.               12, 'x');
  654.     expectPad(fmt, "''#0*x", DecimalFormat::kPadBeforeSuffix,
  655.               3, 'x');
  656.     expectPad(fmt, "'I''ll'*a###.##", DecimalFormat::kPadAfterPrefix,
  657.               10, 'a');
  658.  
  659.     fmt.applyPattern("AA#,##0.00ZZ", status);
  660.     CHECK(status, "applyPattern");
  661.     fmt.setPadCharacter('^');
  662.  
  663.     fmt.setFormatWidth(10);
  664.  
  665.     fmt.setPadPosition(DecimalFormat::kPadBeforePrefix);
  666.     expectPat(fmt, "*^AA#,##0.00ZZ");
  667.  
  668.     fmt.setPadPosition(DecimalFormat::kPadBeforeSuffix);
  669.     expectPat(fmt, "AA#,##0.00*^ZZ");
  670.  
  671.     fmt.setPadPosition(DecimalFormat::kPadAfterSuffix);
  672.     expectPat(fmt, "AA#,##0.00ZZ*^");
  673.  
  674.     //            12  3456789012
  675.     UnicodeString exp("AA*^#,##0.00ZZ");
  676.     fmt.setFormatWidth(12);
  677.     fmt.setPadPosition(DecimalFormat::kPadAfterPrefix);
  678.     expectPat(fmt, exp);
  679.  
  680.     fmt.setFormatWidth(13);
  681.     //              12  34567890123
  682.     expectPat(fmt, "AA*^##,##0.00ZZ");
  683.  
  684.     fmt.setFormatWidth(14);
  685.     //              12  345678901234
  686.     expectPat(fmt, "AA*^###,##0.00ZZ");
  687.  
  688.     fmt.setFormatWidth(15);
  689.     //              12  3456789012345
  690.     expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case
  691.  
  692.     fmt.setFormatWidth(16);
  693.     //              12  34567890123456
  694.     expectPat(fmt, "AA*^#,###,##0.00ZZ");
  695. }
  696.  
  697. void NumberFormatTest::expectPad(DecimalFormat& fmt, const UnicodeString& pat,
  698.                                  int32_t pos) {
  699.     expectPad(fmt, pat, pos, 0, (UChar)0);
  700. }
  701.  
  702. void NumberFormatTest::expectPad(DecimalFormat& fmt, const UnicodeString& pat,
  703.                                  int32_t pos, int32_t width, UChar pad) {
  704.     int32_t apos = 0, awidth = 0;
  705.     UChar apad = 0;
  706.     UErrorCode status = U_ZERO_ERROR;
  707.     fmt.applyPattern(pat, status);
  708.     if (U_SUCCESS(status)) {
  709.         apos = fmt.getPadPosition();
  710.         awidth = fmt.getFormatWidth();
  711.         apad = fmt.getPadCharacter();
  712.     } else {
  713.         apos = -1;
  714.         awidth = width;
  715.         apad = pad;
  716.     }
  717.     if (apos == pos && awidth == width && apad == pad) {
  718.         logln(UnicodeString("Ok   \"") + pat + "\" pos=" + apos +
  719.               ((pos == ILLEGAL) ? UnicodeString() :
  720.                (UnicodeString(" width=") + awidth + " pad=" + UnicodeString(apad))));
  721.     } else {
  722.         errln(UnicodeString("FAIL \"") + pat + "\" pos=" + apos +
  723.               " width=" + awidth + " pad=" + UnicodeString(apad) +
  724.               ", expected " + pos + " " + width + " " + UnicodeString(pad));
  725.     }
  726. }
  727.  
  728. void NumberFormatTest::expectPat(DecimalFormat& fmt, const UnicodeString& exp) {
  729.     UnicodeString pat;
  730.     fmt.toPattern(pat);
  731.     if (pat == exp) {
  732.         logln(UnicodeString("Ok   \"") + pat + '"');
  733.     } else {
  734.         errln(UnicodeString("FAIL \"") + pat + "\", expected \"" + exp + '"');
  735.     }
  736. }
  737.