home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cnmdptst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  14.6 KB  |  455 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright Taligent, Inc.,  1996                                       *
  6. *   (C) Copyright International Business Machines Corporation,  1999          *
  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. *
  13. * File CNMDPTST.C
  14. *
  15. *  Madhu Katragadda                       Creation
  16. * Modification History:
  17. *
  18. *   Date        Name        Description
  19. *   06/24/99    helena      Integrated Alan's NF enhancements and Java2 bug fixes
  20. *******************************************************************************
  21. */
  22.  
  23. /* C DEPTH TEST FOR NUMBER FORMAT */
  24.  
  25. #include "uloc.h"
  26. #include "utypes.h"
  27. #include "unum.h"
  28. #include "ustring.h"
  29. #include "cintltst.h"
  30. #include "cnmdptst.h"
  31. #include <stdio.h>
  32. #include <string.h>
  33.  
  34. void addNumFrDepTest(TestNode** root)
  35. {
  36.   addTest(root, &TestPatterns, "tsformat/cnmdptst/TestPatterns");
  37.   addTest(root, &TestQuotes, "tsformat/cnmdptst/TestQuotes");
  38.   addTest(root, &TestExponential, "tsformat/cnmdptst/TestExponential");
  39.   addTest(root, &TestCurrencySign, "tsformat/cnmdptst/TestCurrencySign");
  40.   addTest(root, &TestCurrency,  "tsformat/cnmdptst/TestCurrency");
  41.   addTest(root, &TestRounding487, "tsformat/cnmdptst/TestRounding487");
  42.  
  43.  
  44. }
  45. /*Test Various format patterns*/
  46. void TestPatterns()
  47. {
  48.   int32_t pat_length, i, lneed;
  49.   UNumberFormat *fmt;
  50.   UFieldPosition pos;
  51.   UChar upat[5];
  52.   UChar unewpat[5];
  53.   UChar unum[5];
  54.   UChar *unewp;
  55.   UChar *str;
  56.   UErrorCode status = U_ZERO_ERROR;
  57.   const char* pat[]    = { "#.#", "#.", ".#", "#" };
  58.   const char* newpat[] = { "#0.#", "#0.", "#.0", "#" };
  59.   const char* num[]    = { "0",   "0.", ".0", "0" };
  60.  
  61.   log_verbose("\nTesting different format patterns\n");
  62.   pat_length = sizeof(pat) / sizeof(pat[0]);
  63.   for (i=0; i < pat_length; ++i)
  64.     {
  65.       status = U_ZERO_ERROR;
  66.       u_uastrcpy(upat, pat[i]);
  67.       fmt= unum_openPattern(upat, u_strlen(upat), "en_US", &status);
  68.       if (U_FAILURE(status)) {
  69.     log_err("FAIL: Number format constructor failed for pattern %s\n", pat[i]);
  70.     continue; 
  71.       }
  72.       lneed=0;
  73.       lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status);
  74.       if(status==U_BUFFER_OVERFLOW_ERROR){
  75.     status= U_ZERO_ERROR;
  76.     unewp=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  77.     unum_toPattern(fmt, FALSE, unewp, lneed+1, &status);
  78.       }
  79.       if(U_FAILURE(status)){
  80.     log_err("FAIL: Number format extracting the pattern failed for %s\n", pat[i]);
  81.       }
  82.       u_uastrcpy(unewpat, newpat[i]);
  83.       if(u_strcmp(unewp, unewpat) != 0)
  84.     log_err("FAIL: Pattern  %s should be transmute to %s; %s seen instead\n", pat[i], newpat[i],  austrdup(unewp) );
  85.         
  86.       lneed=0;
  87.       lneed=unum_format(fmt, 0, NULL, lneed, &pos, &status);
  88.       if(status==U_BUFFER_OVERFLOW_ERROR){
  89.     status=U_ZERO_ERROR;
  90.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  91.     unum_format(fmt, 0, str, lneed+1,  &pos, &status);
  92.       }
  93.       if(U_FAILURE(status))    {
  94.     log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  95.       }
  96.       u_uastrcpy(unum, num[i]);
  97.       if (u_strcmp(str, unum) != 0)
  98.         {
  99.       log_err("FAIL: Pattern %s should format zero as %s; %s Seen instead\n", pat[i], num[i], austrdup(str) );
  100.             
  101.         }
  102.     }
  103. }
  104. /* Test the handling of quotes*/
  105. void TestQuotes()
  106. {
  107.   int32_t lneed;
  108.   UErrorCode status;
  109.   UFieldPosition pos;
  110.   UChar pat[15];
  111.   UChar res[15];
  112.   UChar *str;
  113.   UNumberFormat *fmt;
  114.   status=U_ZERO_ERROR;
  115.   log_verbose("\nTestting the handling of quotes in number format\n");
  116.   u_uastrcpy(pat, "a'fo''o'b#");
  117.   fmt =unum_openPattern(pat, u_strlen(pat), "en_US", &status);
  118.   if(U_FAILURE(status)){
  119.     log_err("Error in number format costruction using pattern \"a'fo''o'b#\"\n");
  120.   }
  121.   lneed=0;
  122.   lneed=unum_format(fmt, 123, NULL, lneed, &pos, &status);
  123.   if(status==U_BUFFER_OVERFLOW_ERROR){
  124.     status=U_ZERO_ERROR;
  125.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  126.     unum_format(fmt, 123, str, lneed+1,  &pos, &status);
  127.   }
  128.   if(U_FAILURE(status))    {
  129.     log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  130.   }
  131.   log_verbose("Pattern \"%s\" \n", austrdup(pat) );
  132.   log_verbose("Format 123 -> %s\n", austrdup(str) );
  133.   u_uastrcpy(res, "afo'ob123");
  134.   if(u_strcmp(str, res) != 0)
  135.     log_err("FAIL: Expected afo'ob123");
  136.     
  137.   free(str);
  138.   unum_close(fmt);
  139.     
  140.     
  141.   u_uastrcpy(pat, "");
  142.   u_uastrcpy(pat, "a''b#");
  143.     
  144.     
  145.   fmt =unum_openPattern(pat, u_strlen(pat), "en_US", &status);
  146.   if(U_FAILURE(status)){
  147.     log_err("Error in number format costruction using pattern \"a''b#\"\n");
  148.   }
  149.   lneed=0;
  150.   lneed=unum_format(fmt, 123, NULL, lneed, &pos, &status);
  151.   if(status==U_BUFFER_OVERFLOW_ERROR){
  152.     status=U_ZERO_ERROR;
  153.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  154.     unum_format(fmt, 123, str, lneed+1,  &pos, &status);
  155.   }
  156.   if(U_FAILURE(status))    {
  157.     log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  158.   }
  159.   log_verbose("Pattern \"%s\" \n", austrdup(pat) );
  160.   log_verbose("Format 123 -> %s\n", austrdup(str) );
  161.   u_uastrcpy(res, "");
  162.   u_uastrcpy(res, "a'b123");
  163.   if(u_strcmp(str, res) != 0)
  164.     log_err("FAIL: Expected a'b123\n");
  165.     
  166.   free(str);
  167.   unum_close(fmt);
  168. }
  169.  
  170. /* Test exponential pattern*/
  171. void TestExponential()
  172. {
  173.   int32_t pat_length, val_length, lval_length;
  174.   int32_t ival, ilval, p, v, lneed;
  175.   UNumberFormat *fmt;
  176.   UFieldPosition pos;
  177.   int32_t ppos;
  178.   UChar *upat;
  179.   UChar pattern[20];
  180.   UChar *str;
  181.   UChar uvalfor[20], ulvalfor[20];
  182.   double a;
  183.   UErrorCode status = U_ZERO_ERROR;
  184.   double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
  185.   char* pat[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]"  };
  186.   int32_t lval[] = { 0, -1, 1, 123456789 };
  187.     
  188.   char* valFormat[] =
  189.   {
  190.     "1.234E-2", "1.2346E8", "1.23E300", "-3.1416E-271",
  191.     "12.340E-03", "12.346E07", "12.300E299", "-31.416E-272",
  192.     "12.34E-003", "123.4568E006", "1.23E300", "-314.1593E-273",
  193.     "1.234E-2", "1.235E8", "1.23E300", "[3.142E-271]"
  194.   };
  195.   char* lvalFormat[] =
  196.   {
  197.     "0E0", "-1E0", "1E0", "1.2346E8",
  198.     "00.000E00", "-10.000E-01", "10.000E-01", "12.346E07",
  199.     "0E000", "-1E000", "1E000", "123.4568E006",
  200.     "0E0", "[1E0]", "1E0", "1.235E8"
  201.   };
  202.   double valParse[] =
  203.   {
  204.     0.01234, 123460000, 1.23E300, -3.1416E-271,
  205.     0.01234, 123460000, 1.23E300, -3.1416E-271,
  206.     0.01234, 123456800, 1.23E300, -3.141593E-271,
  207.     0.01234, 123500000, 1.23E300, -3.142E-271,
  208.   };
  209.   int32_t lvalParse[] =
  210.   {
  211.     0, -1, 1, 123460000,
  212.     0, -1, 1, 123460000,
  213.     0, -1, 1, 123456800,
  214.     0, -1, 1, 123500000,
  215.   };
  216.  
  217.  
  218.   pat_length = sizeof(pat) / sizeof(pat[0]);
  219.   val_length = sizeof(val) / sizeof(val[0]);
  220.   lval_length = sizeof(lval) / sizeof(lval[0]);
  221.   ival = 0;
  222.   ilval = 0;
  223.   for (p=0; p < pat_length; ++p)
  224.     {
  225.       upat=(UChar*)malloc(sizeof(UChar) * (strlen(pat[p])+1) );
  226.       u_uastrcpy(upat, pat[p]);
  227.       fmt=unum_openPattern(upat, u_strlen(upat), "en_US", &status);
  228.       if (U_FAILURE(status)) { 
  229.     log_err("FAIL: Bad status returned by Number format construction with pattern %s\n, pat[i]"); 
  230.     continue; 
  231.       }
  232.       lneed= u_strlen(upat) + 1;
  233.       unum_toPattern(fmt, FALSE, pattern, lneed, &status);
  234.       log_verbose("Pattern \" %s \" -toPattern-> \" %s \" \n", upat, austrdup(pattern) );
  235.       for (v=0; v<val_length; ++v)
  236.         {
  237.       /*format*/
  238.       lneed=0; 
  239.       lneed=unum_formatDouble(fmt, val[v], NULL, lneed, &pos, &status);
  240.       if(status==U_BUFFER_OVERFLOW_ERROR){
  241.         status=U_ZERO_ERROR;
  242.         str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  243.         unum_formatDouble(fmt, val[v], str, lneed+1,  &pos, &status);
  244.       }
  245.       if(U_FAILURE(status))    {
  246.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  247.       }
  248.       
  249.             
  250.             
  251.       u_uastrcpy(uvalfor, valFormat[v+ival]);
  252.       if(u_strcmp(str, uvalfor) != 0)
  253.         log_verbose("FAIL: Expected %s ( %s )\n", valFormat[v+ival], austrdup(uvalfor) );
  254.  
  255.       /*parsing*/
  256.       ppos=0;
  257.       a=unum_parseDouble(fmt, str, u_strlen(str), &ppos, &status);
  258.       if (ppos== u_strlen(str)) {
  259.          if (a != valParse[v+ival])
  260.           printf("FAIL: Expected : %e\n", valParse[v+ival]);
  261.       }
  262.       else
  263.         printf(" FAIL: Partial parse (  %d  chars ) ->  %e\n",  ppos, a);
  264.         
  265.       free(str);
  266.     }
  267.       for (v=0; v<lval_length; ++v)
  268.         {
  269.       /*format*/
  270.       lneed=0; 
  271.       lneed=unum_formatDouble(fmt, lval[v], NULL, lneed, &pos, &status);
  272.       if(status==U_BUFFER_OVERFLOW_ERROR){
  273.         status=U_ZERO_ERROR;
  274.         str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  275.         unum_formatDouble(fmt, lval[v], str, lneed+1,  &pos, &status);
  276.       }
  277.       if(U_FAILURE(status))    {
  278.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  279.       }
  280.       /*printf(" Format %e -> %s\n",  lval[v], austrdup(str) );*/
  281.       u_uastrcpy(ulvalfor, lvalFormat[v+ilval]);
  282.       if(u_strcmp(str, ulvalfor) != 0)
  283.         log_err("FAIL: Expected %s ( %s )\n", valFormat[v+ilval], austrdup(ulvalfor) );
  284.  
  285.       /*parsing*/
  286.       ppos=0;
  287.       a=unum_parseDouble(fmt, str, u_strlen(str), &ppos, &status);
  288.       if (ppos== u_strlen(str)) {
  289.         /*printf(" Parse -> %e\n",  a);*/
  290.         if (a != lvalParse[v+ilval])
  291.           printf("FAIL: Expected : %e\n", valParse[v+ival]);
  292.       }
  293.       else
  294.         printf(" FAIL: Partial parse (  %d  chars ) ->  %e\n",  ppos, a);
  295.         
  296.       free(str);
  297.  
  298.         }
  299.       ival += val_length;
  300.       ilval += lval_length;
  301.       unum_close(fmt);
  302.       free(upat);
  303.     }
  304. }
  305.  
  306. /**
  307.  * Test the handling of the currency symbol in patterns.
  308.  */
  309. void TestCurrencySign()
  310. {
  311.   int32_t lneed;
  312.   UNumberFormat *fmt;
  313.   UChar *pattern;
  314.   UChar *str;
  315.   UChar *pat;
  316.   UChar *res;
  317.   UFieldPosition pos;
  318.   UErrorCode status = U_ZERO_ERROR;
  319.   pattern=(UChar*)malloc(sizeof(UChar) * (strlen("\xA4#,##0.00;-\xA4#,##0.00") + 1) );
  320.   u_uastrcpy(pattern, "\xA4#,##0.00;-\xA4#,##0.00");
  321.   fmt = unum_openPattern(pattern, u_strlen(pattern), "en_US", &status);
  322.   if(U_FAILURE(status)){
  323.     log_err("Error in number format construction with pattern  \"\\xA4#,##0.00;-\\xA4#,##0.00\\\" \n");
  324.   }
  325.   lneed=0; 
  326.   lneed=unum_formatDouble(fmt, 1234.56, NULL, lneed, &pos, &status);
  327.   if(status==U_BUFFER_OVERFLOW_ERROR){
  328.     status=U_ZERO_ERROR;
  329.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  330.     unum_formatDouble(fmt, 1234.56, str, lneed+1,  &pos, &status);
  331.   }
  332.   if(U_FAILURE(status))    {
  333.     log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  334.   }
  335.   lneed=0;
  336.   lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status);
  337.   if(status==U_BUFFER_OVERFLOW_ERROR){
  338.     status=U_ZERO_ERROR;
  339.     pat=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  340.     unum_formatDouble(fmt, FALSE, pat, lneed+1,  &pos, &status);
  341.   }
  342.   log_verbose("Pattern \" %s \" \n", austrdup(pat));
  343.   log_verbose("Format 1234.56 -> %s\n", austrdup(str) );
  344.     
  345.   res=(UChar*)malloc(sizeof(UChar) * (strlen("$1,234.56")+1) );
  346.   u_uastrcpy(res, "$1,234.56");
  347.   if (u_strcmp(str, res) !=0) log_err("FAIL: Expected $1,234.56\n");
  348.   free(str);
  349.   free(res);
  350.   free(pat);
  351.  
  352.   lneed=0; 
  353.   lneed=unum_formatDouble(fmt, -1234.56, NULL, lneed, &pos, &status);
  354.   if(status==U_BUFFER_OVERFLOW_ERROR){
  355.     status=U_ZERO_ERROR;
  356.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  357.     unum_formatDouble(fmt, -1234.56, str, lneed+1,  &pos, &status);
  358.   }
  359.   if(U_FAILURE(status))    {
  360.     log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  361.   }
  362.   res=(UChar*)malloc(sizeof(UChar) * (strlen("-$1,234.56")+1) );
  363.   u_uastrcpy(res, "-$1,234.56");
  364.   if (u_strcmp(str, res) != 0) log_err("FAIL: Expected -$1,234.56\n");
  365.   free(str);
  366.   free(res);
  367.  
  368.   unum_close(fmt);  
  369.   free(pattern);
  370. }
  371. /**
  372.  * Test localized currency patterns.
  373.  */
  374. void TestCurrency()
  375. {
  376.   UNumberFormat *currencyFmt;
  377.   UChar *str, *res;
  378.   int32_t lneed, i;
  379.   UFieldPosition pos;
  380.   UErrorCode status = U_ZERO_ERROR;
  381.   const char* locale[]={"fr_CA", "de_DE", "fr_FR"};
  382.   const char* result[]={"1,50 $", "1,50 DM", "1,50 F"};
  383.   log_verbose("\nTesting the number format with different currency patterns\n");
  384.   for(i=0; i < 3; i++)
  385.     {
  386.       currencyFmt = unum_open(UNUM_CURRENCY, locale[i], &status);
  387.       if(U_FAILURE(status)){log_err("Error in the construction of number format with style currency:\n%s\n",
  388.                   myErrorName(status));
  389.       }
  390.       lneed=0;
  391.       lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, &pos, &status);
  392.       if(status==U_BUFFER_OVERFLOW_ERROR){
  393.     status=U_ZERO_ERROR;
  394.     str=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  395.     unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status);
  396.       }
  397.       if(U_FAILURE(status))    {
  398.     log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
  399.       }
  400.       res=(UChar*)malloc(sizeof(UChar) * (strlen(result[i])+1) );
  401.       u_uastrcpy(res, result[i]);
  402.       if (u_strcmp(str, res) != 0) log_err("FAIL: Expected %s\n", result[i]);
  403.       unum_close(currencyFmt);
  404.       free(str);
  405.     }
  406. }
  407. /**
  408.  * Test proper rounding by the format method.
  409.  */
  410. void TestRounding487()
  411. {
  412.   UNumberFormat *nnf;
  413.   UErrorCode status = U_ZERO_ERROR;
  414.   nnf = unum_open(UNUM_DEFAULT, NULL, &status);
  415.   if(U_FAILURE(status)){
  416.     log_err("FAIL: failure in the construction of number format: %s\n", myErrorName(status));
  417.   }
  418.   roundingTest(nnf, 0.00159999, 4, "0.0016");
  419.   roundingTest(nnf, 0.00995, 4, "0.01");
  420.  
  421.   roundingTest(nnf, 12.3995, 3, "12.4");
  422.  
  423.   roundingTest(nnf, 12.4999, 0, "12");
  424.   roundingTest(nnf, - 19.5, 0, "-20");
  425.   unum_close(nnf);
  426. }
  427.  
  428. /*-------------------------------------*/
  429.  
  430. void roundingTest(UNumberFormat* nf, double x, int32_t maxFractionDigits, const char* expected)
  431. {
  432.   UChar *out;
  433.   UChar *res;
  434.   UFieldPosition pos;
  435.   UErrorCode status;
  436.   int32_t lneed;
  437.   status=U_ZERO_ERROR;
  438.   unum_setAttribute(nf, UNUM_MAX_FRACTION_DIGITS, maxFractionDigits);
  439.   lneed=0;
  440.   lneed=unum_formatDouble(nf, x, NULL, lneed, &pos, &status);
  441.   if(status==U_BUFFER_OVERFLOW_ERROR){
  442.     status=U_ZERO_ERROR;
  443.     out=(UChar*)malloc(sizeof(UChar) * (lneed+1) );
  444.     unum_formatDouble(nf, x, out, lneed+1, &pos, &status);
  445.   }
  446.   if(U_FAILURE(status))    {
  447.     log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) );
  448.   }
  449.   /*Need to use log_verbose here. Problem with the float*/
  450.   /*printf("%f format with %d fraction digits to %s\n", x, maxFractionDigits, austrdup(out) );*/
  451.   res=(UChar*)malloc(sizeof(UChar) * (strlen(expected)+1) );
  452.   u_uastrcpy(res, expected);
  453.   if (u_strcmp(out, res) != 0) log_err("FAIL: Expected: %s or %s\n", expected, austrdup(res) );
  454. }
  455.