home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cnumtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  17.5 KB  |  474 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. *
  14. * File CNUMTST.C
  15. *
  16. *     Madhu Katragadda              Creation
  17. *
  18. * Modification History:
  19. *
  20. *   Date        Name        Description
  21. *   06/24/99    helena      Integrated Alan's NF enhancements and Java2 bug fixes
  22. *   07/15/99    helena      Ported to HPUX 10/11 CC.
  23. *********************************************************************************
  24. */
  25.  
  26. /* C API TEST FOR NUMBER FORMAT */
  27.  
  28. #include "uloc.h"
  29. #include "utypes.h"
  30. #include "unum.h"
  31. #include "ustring.h"
  32. #include "cintltst.h"
  33. #include "cnumtst.h"
  34. #include<stdio.h>
  35.  
  36. void addNumForTest(TestNode** root)
  37. {
  38.     addTest(root, &TestNumberFormat, "tsformat/cnumtst/TestNumberFormat");
  39. }
  40. /* test Number Format API */
  41. void TestNumberFormat()
  42. {
  43.     UChar *result;
  44.     UChar *temp1;
  45.  
  46.     UChar temp[5];
  47.     
  48.     UChar prefix[5];
  49.     UChar suffix[5];
  50.     int32_t resultlength;
  51.     int32_t resultlengthneeded;
  52.     int32_t parsepos;
  53.     double d1;
  54.     int32_t l1;
  55.     double d = -10456.37;
  56.     int32_t l = 100000000;
  57.     UFieldPosition pos1;
  58.     UFieldPosition pos2;
  59.     int32_t numlocales;
  60.  
  61.     UNumberFormatAttribute attr;
  62.     UNumberFormatSymbols symbols1, symbols2;
  63.     int32_t newvalue;  
  64.     UErrorCode status=U_ZERO_ERROR;
  65.     UNumberFormatStyle style= UNUM_DEFAULT;
  66.     UNumberFormat *pattern;
  67.     UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, *spellout_def, *cur_frpattern;
  68.     /* Testing unum_open() with various Numberformat styles and locales*/
  69.     status = U_ZERO_ERROR;
  70.     log_verbose("Testing  unum_open() with default style and locale\n");
  71.     def=unum_open(style, NULL, &status);
  72.     if(U_FAILURE(status))
  73.         log_err("Error in creating NumberFormat default using unum_open(): %s\n", myErrorName(status));
  74.  
  75.     log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n");
  76.     fr=unum_open(style, "fr_FR", &status);
  77.     if(U_FAILURE(status))
  78.         log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status));
  79.  
  80.     log_verbose("\nTesting unum_open(currency,NULL,status)\n");
  81.     style=UNUM_CURRENCY;
  82.     /* Can't hardcode the result to assume the default locale is "en_US". */
  83.     cur_def=unum_open(style, "en_US", &status);
  84.     if(U_FAILURE(status))
  85.         log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n",
  86.                         myErrorName(status) );
  87.  
  88.     log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n");
  89.     cur_fr=unum_open(style, "fr_FR", &status);
  90.     if(U_FAILURE(status))
  91.         log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n", 
  92.                 myErrorName(status));
  93.  
  94.     log_verbose("\nTesting unum_open(percent, NULL, status)\n");
  95.     style=UNUM_PERCENT;
  96.     per_def=unum_open(style, NULL, &status);
  97.     if(U_FAILURE(status))
  98.         log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status));
  99.  
  100.     log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n");
  101.     per_fr=unum_open(style, "fr_FR", &status);
  102.     if(U_FAILURE(status))
  103.         log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status));
  104.  
  105.     /*
  106.     log_verbose("\nTesting unum_open(spellout, NULL, status)");
  107.     style=UNUM_SPELLOUT;
  108.     spellout_def=unum_open(style, NULL, &status);
  109.     if(U_FAILURE(status))
  110.         log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status));
  111.     */
  112.     
  113.     
  114.     /*Testing unum_getAvailable() and unum_countAvailable()*/ 
  115.     log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
  116.     numlocales=unum_countAvailable();
  117.     /* use something sensible w/o hardcoding the count */
  118.     if(numlocales < 0)
  119.         log_err("error in countAvailable");
  120.     else{
  121.         log_verbose("unum_countAvialable() successful\n");
  122.         log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales);
  123.     }
  124.     /*for(i=0;i<numlocales;i++)
  125.         log_verbose("%s\n", uloc_getName(unum_getAvailable(i))); */
  126.  
  127.     
  128.     /*Testing unum_format() and unum_formatdouble()*/
  129.     temp1=(UChar*)malloc(sizeof(UChar) * 25);
  130.     u_uastrcpy(temp1, "$100,000,000.00");
  131.     
  132.     log_verbose("\nTesting unum_format() \n");
  133.     resultlength=0;
  134.     resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
  135.     if(status==U_BUFFER_OVERFLOW_ERROR)
  136.     {
  137.         status=U_ZERO_ERROR;
  138.         resultlength=resultlengthneeded+1;
  139.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  140.         unum_format(cur_def, l, result, resultlength, &pos1, &status);
  141.     }
  142.  
  143.     if(U_FAILURE(status))
  144.     {
  145.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) );
  146.     }
  147.     if(u_strcmp(result, temp1)==0)
  148.         log_verbose("Pass: Number formatting using unum_foramt() successful\n");
  149.     else
  150.         log_err("Fail: Error in number Formatting using unum_foramt()\n");
  151.     
  152. free(result);    
  153.     result = 0;
  154.  
  155.     log_verbose("\nTesting unum_formatDouble()\n");
  156.     u_uastrcpy(temp1, "($10,456.37)");
  157.     resultlength=0;
  158.     resultlengthneeded=unum_formatDouble(cur_def, d, NULL, resultlength, &pos2, &status);
  159.     if(status==U_BUFFER_OVERFLOW_ERROR)
  160.     {
  161.         status=U_ZERO_ERROR;
  162.         resultlength=resultlengthneeded+1;
  163.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  164.         unum_formatDouble(cur_def, d, result, resultlength, &pos2, &status);
  165.     }
  166.     if(U_FAILURE(status))
  167.     {
  168.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
  169.     }
  170.     if(u_strcmp(result, temp1)==0)
  171.         log_verbose("Pass: Number Formatting using unum_formatDouble() Successful\n");
  172.     else
  173.         log_err("FAIL: Error in munber formatting using unum_formatDouble()\n");
  174.  
  175.  
  176.     /* Testing unum_parse() and unum_parseDouble() */
  177.     parsepos=0;
  178.     log_verbose("\nTesting unum_parseDouble()\n");
  179.     d1=unum_parseDouble(cur_def, result, u_strlen(result), &parsepos, &status);
  180.     if(U_FAILURE(status))
  181.     {
  182.         log_err("parse failed. The error is  : %s\n", myErrorName(status));
  183.     }
  184.  
  185.     if(d1!=d)
  186.         log_err("Fail: Error in parsing\n");
  187.     else
  188.         log_verbose("Pass: parsing successful\n");
  189.  
  190. free(result);
  191.  
  192.     log_verbose("\nTesting unum_parse()\n");
  193.     resultlength=0;
  194.     parsepos=0;
  195.     resultlengthneeded=unum_format(per_fr, l, NULL, resultlength, &pos1, &status);
  196.     if(status==U_BUFFER_OVERFLOW_ERROR)
  197.     {
  198.         status=U_ZERO_ERROR;
  199.         resultlength=resultlengthneeded+1;
  200.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  201.         unum_format(per_fr, l, result, resultlength, &pos1, &status);
  202.     }
  203.     if(U_FAILURE(status))
  204.     {
  205.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
  206.     }
  207.     
  208.     
  209.     l1=unum_parse(per_fr, result, u_strlen(result), &parsepos, &status);
  210.     if(U_FAILURE(status))
  211.     {
  212.         log_err("parse failed. The error is  : %s\n", myErrorName(status));
  213.     }
  214.     
  215.     if(l1!=l)
  216.         log_err("Fail: Error in parsing\n");
  217.     else
  218.         log_verbose("Pass: parsing successful\n");
  219.     
  220. free(result);    
  221.  
  222.     /* create a number format using unum_openPattern(....)*/
  223.     log_verbose("\nTesting unum_openPattern()\n");
  224.     u_uastrcpy(temp1, "#,##0.0#;(#,##0.0#)");
  225.     pattern=unum_openPattern(temp1, u_strlen(temp1), NULL, &status);
  226.     if(U_FAILURE(status))
  227.     {
  228.     log_err("error in unum_openPattern(): %s\n", myErrorName(status) );;
  229.     }
  230.     else
  231.         log_verbose("Pass: unum_openPattern() works fine\n");
  232.     
  233.     /*test for unum_toPattern()*/
  234.     log_verbose("\nTesting unum_toPattern()\n");
  235.     resultlength=0;
  236.     resultlengthneeded=unum_toPattern(pattern, FALSE, NULL, resultlength, &status);
  237.     if(status==U_BUFFER_OVERFLOW_ERROR)
  238.     {
  239.         status=U_ZERO_ERROR;
  240.         resultlength=resultlengthneeded+1;
  241.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  242.         unum_toPattern(pattern, FALSE, result, resultlength, &status);
  243.     }
  244.     if(U_FAILURE(status))
  245.     {
  246.         log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
  247.     }
  248.     if(u_strcmp(result, temp1)!=0)
  249.         log_err("FAIL: Error in extracting the pattern using unum_toPattern()\n");
  250.     else
  251.         log_verbose("Pass: extracted the pattern correctly using unum_toPattern()\n");
  252.  
  253.     
  254.  
  255. free(result);
  256. free(temp1);
  257.     
  258.     /*Testing unum_getSymbols() and unum_setSymbols()*/
  259.     log_verbose("\nTesting unum_getSymbols and unum_setSymbols()\n");
  260.     /*when we try to change the symbols of french to default we need to apply the pattern as well to fetch correct results */
  261.     resultlength=0;
  262.     resultlengthneeded=unum_toPattern(cur_def, FALSE, NULL, resultlength, &status);
  263.     if(status==U_BUFFER_OVERFLOW_ERROR)
  264.     {
  265.         status=U_ZERO_ERROR;
  266.         resultlength=resultlengthneeded+1;
  267.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  268.         unum_toPattern(cur_def, FALSE, result, resultlength, &status);
  269.     }
  270.     if(U_FAILURE(status))
  271.     {
  272.         log_err("error in extracting the pattern from UNumberFormat: %s\n", myErrorName(status));
  273.     }
  274.  
  275.     cur_frpattern=unum_openPattern(result, u_strlen(result), "fr_FR", &status);
  276.     if(U_FAILURE(status))
  277.     {
  278.         log_err("error in unum_openPattern(): %s\n", myErrorName(status));
  279.     }
  280.     
  281. free(result);
  282.  
  283.     /*getting the symbols of cur_def */
  284.     unum_getSymbols(cur_def, &symbols1);
  285.     
  286.     /*format to check the result */
  287.     resultlength=0;
  288.     resultlengthneeded=unum_format(cur_def, l, NULL, resultlength, &pos1, &status);
  289.     if(status==U_BUFFER_OVERFLOW_ERROR)
  290.     {
  291.         status=U_ZERO_ERROR;
  292.         resultlength=resultlengthneeded+1;
  293.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  294.         unum_format(cur_def, l, result, resultlength, &pos1, &status);
  295.     }
  296.     if(U_FAILURE(status))
  297.     {
  298.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
  299.     }
  300.     
  301.  
  302.     /*set the symbols of cur_frpattern to cur_def */
  303.     unum_setSymbols(cur_frpattern, &symbols1, &status);
  304.     if(U_FAILURE(status)){
  305.         log_err("Fail: error in unum_setSymbols: %s\n", myErrorName(status));
  306.     }
  307.  
  308.     unum_getSymbols(cur_frpattern, &symbols2);
  309.     if((symbols1.decimalSeparator != symbols2.decimalSeparator) ||
  310.        (symbols1.groupingSeparator != symbols2.groupingSeparator) ||
  311.        (symbols1.patternSeparator != symbols2.patternSeparator) ||
  312.        (symbols1.percent != symbols2.percent) ||
  313.        (symbols1.zeroDigit != symbols2.zeroDigit) ||
  314.        (symbols1.digit != symbols2.digit) ||
  315.        (symbols1.minusSign != symbols2.minusSign) ||
  316.        (symbols1.plusSign != symbols2.plusSign) ||
  317.        (u_strcmp(symbols1.currency,symbols2.currency)!=0) ||
  318.        (u_strcmp(symbols1.intlCurrency,symbols2.intlCurrency)!=0) ||
  319.        (symbols1.monetarySeparator != symbols2.monetarySeparator) ||
  320.        (symbols1.exponential != symbols2.exponential) ||
  321.        (symbols1.perMill != symbols2.perMill) ||
  322.        (symbols1.padEscape != symbols2.padEscape) ||
  323.        (u_strcmp(symbols1.infinity,symbols2.infinity)!=0) ||
  324.        (u_strcmp(symbols1.naN,symbols2.naN)!=0))
  325.         log_err("Fail: error in setting and getting symbols\n");
  326.     else
  327.         log_verbose("Pass: get and set symbols successful\n");
  328.     
  329.     /*format and check with the previous result */
  330.  
  331.     resultlength=0;
  332.     resultlengthneeded=unum_format(cur_frpattern, l, NULL, resultlength, &pos1, &status);
  333.     if(status==U_BUFFER_OVERFLOW_ERROR)
  334.     {
  335.         status=U_ZERO_ERROR;
  336.         resultlength=resultlengthneeded+1;
  337.         temp1=(UChar*)malloc(sizeof(UChar) * resultlength);
  338.         unum_format(cur_frpattern, l, temp1, resultlength, &pos1, &status);
  339.     }
  340.     if(U_FAILURE(status))
  341.     {
  342.         log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status));
  343.     }
  344.     
  345.     /*----------- */
  346.     
  347. free(result);
  348. free(temp1);    
  349.     
  350.     /* Testing unum_getTextAttribute() and unum_setTextAttribute()*/
  351.     log_verbose("\nTesting getting and setting text attributes\n");
  352.     resultlength=5;
  353.     unum_getTextAttribute(cur_fr, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
  354.     if(U_FAILURE(status))
  355.     {
  356.         log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
  357.     }
  358.     unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
  359.     if(U_FAILURE(status))
  360.     {
  361.         log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
  362.     }
  363.     unum_getTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, suffix, resultlength, &status);
  364.     if(U_FAILURE(status))
  365.     {
  366.         log_err("Failure in gettting the Text attributes of number format: %s\n", myErrorName(status));
  367.     }
  368.     if(u_strcmp(suffix,temp)!=0)
  369.         log_err("Fail:Error in setTextAttribute or getTextAttribute in setting and getting suffix\n");
  370.     else
  371.         log_verbose("Pass: setting and getting suffix works fine\n");
  372.     /*set it back to normal */
  373.     u_uastrcpy(temp,"$");
  374.     unum_setTextAttribute(cur_def, UNUM_NEGATIVE_SUFFIX, temp, u_strlen(temp), &status);
  375.     
  376.     /*checking some more text setter conditions */
  377.     u_uastrcpy(prefix, "+");
  378.     unum_setTextAttribute(def, UNUM_POSITIVE_PREFIX, prefix, u_strlen(prefix) , &status);
  379.     if(U_FAILURE(status))
  380.     {
  381.         log_err("error in setting the text attributes : %s\n", myErrorName(status));
  382.     }
  383.     unum_getTextAttribute(def, UNUM_POSITIVE_PREFIX, temp, resultlength, &status);
  384.     if(U_FAILURE(status))
  385.     {
  386.         log_err("error in getting the text attributes : %s\n", myErrorName(status));
  387.     }
  388.     
  389.     if(u_strcmp(prefix, temp)!=0) 
  390.         log_err("ERROR: get and setTextAttributes with positive prefix failed\n");
  391.     else
  392.         log_verbose("Pass: get and setTextAttributes with positive prefix works fine\n");
  393.     
  394.     u_uastrcpy(prefix, "+");
  395.     unum_setTextAttribute(def, UNUM_NEGATIVE_PREFIX, prefix, u_strlen(prefix), &status);
  396.     if(U_FAILURE(status))
  397.     {
  398.         log_err("error in setting the text attributes : %s\n", myErrorName(status));
  399.     }
  400.     unum_getTextAttribute(def, UNUM_NEGATIVE_PREFIX, temp, resultlength, &status);
  401.     if(U_FAILURE(status))
  402.     {
  403.         log_err("error in getting the text attributes : %s\n", myErrorName(status));
  404.     }
  405.     if(u_strcmp(prefix, temp)!=0) 
  406.         log_err("ERROR: get and setTextAttributes with negative prefix failed\n");
  407.     else
  408.         log_verbose("Pass: get and setTextAttributes with negative prefix works fine\n");
  409.  
  410.     u_uastrcpy(suffix, "+");
  411.     unum_setTextAttribute(def, UNUM_NEGATIVE_SUFFIX, suffix, u_strlen(suffix) , &status);
  412.     if(U_FAILURE(status))
  413.     {
  414.         log_err("error in setting the text attributes: %s\n", myErrorName(status));
  415.     }
  416.     
  417.     unum_getTextAttribute(def, UNUM_NEGATIVE_SUFFIX, temp, resultlength, &status);
  418.     if(U_FAILURE(status))
  419.     {
  420.         log_err("error in getting the text attributes : %s\n", myErrorName(status));
  421.     }
  422.     if(u_strcmp(suffix, temp)!=0) 
  423.         log_err("ERROR: get and setTextAttributes with negative suffix failed\n");
  424.     else
  425.         log_verbose("Pass: get and settextAttributes with negative suffix works fine\n");
  426.     
  427.     
  428.     
  429.  
  430.     /*Testing unum_getAttribute and  unum_setAttribute() */
  431.     log_verbose("\nTesting get and set Attributes\n");
  432.     attr=UNUM_GROUPING_SIZE;
  433.     newvalue=unum_getAttribute(def, attr);
  434.     newvalue=2;
  435.     unum_setAttribute(def, attr, newvalue);
  436.     if(unum_getAttribute(def,attr)!=2)
  437.         log_err("Fail: error in setting and getting attributes for UNUM_GROUPING_SIZE\n");
  438.     else
  439.         log_verbose("Pass: setting and getting attributes for UNUM_GROUPING_SIZE works fine\n");
  440.  
  441.     attr=UNUM_MULTIPLIER;
  442.     newvalue=unum_getAttribute(def, attr);
  443.     newvalue=8;
  444.     unum_setAttribute(def, attr, newvalue);
  445.     if(unum_getAttribute(def,attr) != 8)
  446.         log_err("error in setting and getting attributes for UNUM_MULTIPLIER\n");
  447.     else
  448.         log_verbose("Pass:setting and getting attributes for UNUM_MULTIPLIER works fine\n");
  449.     
  450.     /*testing set and get Attributes extensively */
  451.     log_verbose("\nTesting get and set attributes extensively\n");
  452.     for(attr=UNUM_PARSE_INT_ONLY; attr<= UNUM_GROUPING_SIZE; attr=(UNumberFormatAttribute)((int32_t)attr + 1) ){
  453.     newvalue=unum_getAttribute(fr, attr);
  454.     unum_setAttribute(def, attr, newvalue);
  455.     if(unum_getAttribute(def,attr)!=unum_getAttribute(fr, attr))
  456.         log_err("error in setting and getting attributes\n");
  457.     else
  458.         log_verbose("Pass: attributes set and retrieved successfully\n");
  459.     }
  460.  
  461.     
  462.     /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/
  463.     unum_close(def);
  464.     unum_close(fr);
  465.     unum_close(cur_def);
  466.     unum_close(cur_fr);
  467.     unum_close(per_def);
  468.     unum_close(per_fr);
  469.     /*unum_close(spellout_def);*/
  470.     unum_close(pattern);
  471.     unum_close(cur_frpattern);
  472.     
  473. }
  474.