home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / cmsgtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  16.3 KB  |  435 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 CMSGTST.C
  15. *
  16. * Modification History:
  17. *        Name                     Description            
  18. *     Madhu Katragadda              Creation
  19. *********************************************************************************
  20. */
  21. /* C API TEST FOR MESSAGE FORMAT */
  22.  
  23. #include "uloc.h"
  24. #include "utypes.h"
  25. #include "umsg.h"
  26. #include "udat.h"
  27. #include "umsg.h"
  28. #include "ustring.h"
  29. #include "cintltst.h"
  30. #include "cmsgtst.h"
  31. #include<stdio.h>
  32. #include <string.h>
  33. #include "cformtst.h"
  34.  
  35. static char* txt_testCasePatterns[] = {
  36.    "Quotes '', '{', a {0,number,integer} '{'0}",
  37.    "Quotes '', '{', a {0,number,integer} '{'0}",
  38.    "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}",
  39.     "'{'2,time,full}, for {1, number, integer}, {0,number,integer} is {2,time,full} and full date is {2,date,full}",
  40.    "'{'1,number,percent} for {0,number,integer} is {1,number,percent}",
  41. };
  42.  
  43. static char* txt_testResultStrings[] = {
  44.     "Quotes ', {, a 1 {0}",
  45.     "Quotes ', {, a 1 {0}",
  46.     "You deposited 1 times an amount of $3,456.00 on 1/12/70",
  47.     "{2,time,full}, for 3,456, 1 is 5:46:40 AM PST and full date is Monday, January 12, 1970",
  48.     "{1,number,percent} for 1 is 345,600%"
  49. };
  50.  
  51. const int32_t cnt_testCases = 5;
  52. static UChar* testCasePatterns[5];
  53.  
  54. static UChar* testResultStrings[5];
  55.  
  56. static bool_t strings_initialized = FALSE;
  57. /* function used to create the test patterns for testing Message formatting */
  58. static void InitStrings( void )
  59. {
  60.     int32_t i;
  61.     if (strings_initialized) return;
  62.     strings_initialized = TRUE;
  63.  
  64.     for (i=0; i < cnt_testCases; i++ ) {
  65.         testCasePatterns[i]=(UChar*)malloc(sizeof(UChar) * (strlen(txt_testCasePatterns[i]) + 1));
  66.         u_uastrcpy(testCasePatterns[i], txt_testCasePatterns[i] );
  67.     }
  68.     for (i=0; i < cnt_testCases; i++ ) {
  69.         testResultStrings[i] = (UChar*)malloc(sizeof(UChar) * (strlen(txt_testResultStrings[i]) + 1));
  70.         u_uastrcpy(testResultStrings[i], txt_testResultStrings[i] );
  71.     }
  72. }
  73. /* Test u_formatMessage() with various test patterns() */
  74. void MessageFormatTest( void ) 
  75. {
  76.  
  77.     UChar *str;
  78.     UChar* result;
  79.     int32_t resultLengthOut,resultlength,i, patternlength;
  80.     UErrorCode status = U_ZERO_ERROR;
  81.     UDate d1=1000000000.0;
  82.     str=(UChar*)malloc(sizeof(UChar) * 7);
  83.     u_uastrcpy(str, "MyDisk");
  84.     resultlength=1;
  85.     result=(UChar*)malloc(sizeof(UChar) * 1);
  86.     log_verbose("Testing u_formatMessage90\n");
  87.     InitStrings();
  88.     for (i = 0; i < cnt_testCases; i++) {
  89.         status=U_ZERO_ERROR;
  90.         patternlength=u_strlen(testCasePatterns[i]);
  91.         resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 
  92.             &status, 1, 3456.00, d1);
  93.         if(status== U_BUFFER_OVERFLOW_ERROR)
  94.         {
  95.         status=U_ZERO_ERROR;
  96.         resultlength=resultLengthOut+1;
  97.         result=(UChar*)realloc(result,sizeof(UChar) * resultlength);
  98.         u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 
  99.             &status, 1, 3456.00, d1);
  100.     }
  101.     if(U_FAILURE(status)){
  102.         log_err("ERROR: failure in message format on testcase %d:  %s\n", i, myErrorName(status) );
  103.     }
  104.     if(u_strcmp(result, testResultStrings[i])==0){
  105.         log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
  106.     }
  107.     else{
  108.         log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, 
  109.             austrdup(result), austrdup(testResultStrings[i]) );
  110.     }
  111.     }
  112.     free(result);
  113.     free(str);
  114. }
  115.  
  116.  
  117. /*test u_formatMessage() with sample patterns */
  118. void TestSampleMessageFormat()
  119. {
  120.   UChar *str;
  121.   UChar *result;
  122.   UChar pattern[100], expected[100];
  123.   int32_t resultLengthOut, resultlength;
  124.   UDate d = 837039928046.0;
  125.   UErrorCode status = U_ZERO_ERROR;
  126.   str=(UChar*)malloc(sizeof(UChar) * 15);
  127.   u_uastrcpy(str, "abc");    
  128.     
  129.   u_uastrcpy(pattern, "There are {0} files on {1,date}");
  130.   u_uastrcpy(expected, "There are abc files on Jul 10, 1996");
  131.   result=(UChar*)malloc(sizeof(UChar) * 1);
  132.   log_verbose("\nTesting a sample for Message format test#1\n");
  133.   resultlength=1;
  134.   resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
  135.   if(status==U_BUFFER_OVERFLOW_ERROR)
  136.     {
  137.       status=U_ZERO_ERROR;
  138.       resultlength=resultLengthOut+1;
  139.       result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
  140.       u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
  141.     }
  142.   if(U_FAILURE(status)){
  143.     log_err("Error: failure in message format on test#1: %s\n", myErrorName(status));
  144.   }
  145.   if(u_strcmp(result, expected)==0)
  146.     log_verbose("PASS: MessagFormat successful on test#1\n");
  147.   else{
  148.     log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n", 
  149.         austrdup(result), austrdup(expected) );
  150.   }
  151.  
  152.        
  153.   log_verbose("\nTesting message format with another pattern test#2\n");
  154.   u_uastrcpy(pattern, "The disk \"{0}\" contains {1,number,integer} file(s)");
  155.   u_uastrcpy(expected, "The disk \"MyDisk\" contains 23 file(s)");
  156.   u_uastrcpy(str, "MyDisk");
  157.     
  158.   resultLengthOut=u_formatMessage( "en_US", 
  159.                    pattern, 
  160.                    u_strlen(pattern),
  161.                    result,
  162.                    resultlength,
  163.                    &status, 
  164.                    str,
  165.                    235);
  166.   if(status==U_BUFFER_OVERFLOW_ERROR)
  167.     {
  168.       status=U_ZERO_ERROR;
  169.       resultlength=resultLengthOut+1;
  170.       result=(UChar*)realloc(result, sizeof(UChar) * (resultlength+1));
  171.       u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 23);
  172.     }
  173.   if(U_FAILURE(status)){
  174.     log_err("Error: failure in message format on test#2 : %s\n", myErrorName(status));
  175.   }
  176.   if(u_strcmp(result, expected)==0)
  177.     log_verbose("PASS: MessagFormat successful on test#2\n");
  178.   else{
  179.     log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n", 
  180.         austrdup(result), austrdup(expected) );
  181.   }
  182.  
  183.    
  184.  
  185.   log_verbose("\nTesting message format with another pattern test#3\n");
  186.   u_uastrcpy(pattern, "You made a {0} of {1,number,currency}");
  187.   u_uastrcpy(expected, "You made a deposit of $500.00");
  188.   u_uastrcpy(str, "deposit");
  189.   resultlength=0;
  190.   resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00);
  191.   if(status==U_BUFFER_OVERFLOW_ERROR)
  192.     {
  193.       status=U_ZERO_ERROR;
  194.       resultlength=resultLengthOut+1;
  195.       result=(UChar*)malloc(sizeof(UChar) * resultlength);
  196.       u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 500.00);
  197.     }
  198.   if(U_FAILURE(status)){
  199.     log_err("Error: failure in message format on test#3 : %s\n", myErrorName(status));
  200.   }
  201.   if(u_strcmp(result, expected)==0)
  202.     log_verbose("PASS: MessagFormat successful on test#3\n");
  203.   else{
  204.     log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result), 
  205.         austrdup(expected) );
  206.   }
  207.  
  208.   free(result);
  209.     
  210.  
  211.     
  212. }
  213. /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
  214. void TestSampleFormatAndParse()
  215. {
  216.  
  217.     UChar *result, *tzID, *str;
  218.     UChar pattern[100];
  219.     UChar expected[100];
  220.     int32_t resultLengthOut, resultlength;
  221.     UCalendar *cal;
  222.     UDate d1,d;
  223.     UDateFormat *def1;
  224.     UErrorCode status = U_ZERO_ERROR;
  225.     double value;
  226.     UChar ret[30];
  227.     log_verbose("Testing format and parse\n");
  228.  
  229.     str=(UChar*)malloc(sizeof(UChar) * 25);
  230.     u_uastrcpy(str, "disturbance in force");
  231.     tzID=(UChar*)malloc(sizeof(UChar) * 4);
  232.     u_uastrcpy(tzID, "PST");
  233.     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
  234.     if(U_FAILURE(status)){
  235.         log_err("error in ucal_open caldef : %s\n", myErrorName(status) );
  236.     }
  237.     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
  238.     d1=ucal_getMillis(cal, &status);
  239.     if(U_FAILURE(status)){
  240.             log_err("Error: failure in get millis: %s\n", myErrorName(status) );
  241.     }
  242.     
  243.     log_verbose("\nTesting with pattern test#4");
  244.     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
  245.     u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); 
  246.     resultlength=1;
  247.     result=(UChar*)malloc(sizeof(UChar) * resultlength);
  248.     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
  249.     if(status==U_BUFFER_OVERFLOW_ERROR)
  250.     {
  251.         status=U_ZERO_ERROR;
  252.         resultlength=resultLengthOut+1;
  253.         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
  254.         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
  255.         
  256.     }
  257.     if(U_FAILURE(status)){
  258.         log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
  259.     }
  260.     if(u_strcmp(result, expected)==0)
  261.         log_verbose("PASS: MessagFormat successful on test#4\n");
  262.     else{
  263.         log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
  264.             austrdup(expected) );
  265.     }
  266.     
  267.     
  268.     /*try to parse this and check*/
  269.     log_verbose("\nTesting the parse Message test#5\n");
  270.     def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status);
  271.     if(U_FAILURE(status))
  272.     {
  273.         log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
  274.     }
  275.     u_parseMessage("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &status, &d, ret, &value);
  276.     if(U_FAILURE(status)){
  277.         log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status));
  278.     }
  279.     if(value!=7.00 && u_strcmp(str,ret)!=0)
  280.         log_err("FAIL: Error in parseMessage on test#5 \n");
  281.     else
  282.         log_verbose("PASS: parseMessage successful on test#5\n");
  283.         
  284.  
  285.  
  286.     if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
  287.         log_verbose("PASS: parseMessage successful test#5\n");
  288.     else{
  289.         log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", 
  290.             austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
  291.         }
  292.  
  293.     free(result);
  294.     free(str);
  295.     
  296. }
  297. /* test message format with a choice option */
  298. void TestMsgFormatChoice()
  299. {
  300.     UChar* str;
  301.     UErrorCode status = U_ZERO_ERROR;
  302.     UChar *result;
  303.     UChar pattern[100];
  304.     UChar expected[100];
  305.     int32_t resultlength,resultLengthOut;
  306.     
  307.     str=(UChar*)malloc(sizeof(UChar) * 25);
  308.     u_uastrcpy(str, "MyDisk");
  309.     log_verbose("Testing message format with choice test #6\n:");
  310.     /*There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.*/
  311.     u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
  312.     u_uastrcpy(expected, "The disk MyDisk contains 100 files");
  313.     resultlength=0;
  314.     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 100., str);
  315.     if(status==U_BUFFER_OVERFLOW_ERROR)
  316.     {
  317.         status=U_ZERO_ERROR;
  318.         resultlength=resultLengthOut+1;
  319.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  320.         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 100., str);
  321.     }
  322.     if(U_FAILURE(status)){
  323.         log_err("ERROR: failure in message format on test#6 : %s\n", myErrorName(status));
  324.     }
  325.     if(u_strcmp(result, expected)==0)
  326.         log_verbose("PASS: MessagFormat successful on test#6\n");
  327.     else{
  328.         log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result),
  329.             austrdup(expected) );
  330.     }
  331.     free(result);
  332.  
  333.     log_verbose("Testing message format with choice test #7\n:");
  334.     u_uastrcpy(expected, "The disk MyDisk contains no files");
  335.     resultlength=0;
  336.     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 0., str);
  337.     if(status==U_BUFFER_OVERFLOW_ERROR)
  338.     {
  339.         status=U_ZERO_ERROR;
  340.         resultlength=resultLengthOut+1;
  341.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  342.         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 0., str);
  343.     }
  344.     if(U_FAILURE(status)){
  345.         log_err("ERROR: failure in message format on test#7 : %s\n", myErrorName(status));
  346.     }
  347.     if(u_strcmp(result, expected)==0)
  348.         log_verbose("PASS: MessagFormat successful on test#7\n");
  349.     else{
  350.         log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result), 
  351.             austrdup(expected) );
  352.     }
  353.  
  354.     free(result);
  355.     log_verbose("Testing message format with choice test #8\n:");
  356.     u_uastrcpy(expected, "The disk MyDisk contains one file");
  357.     resultlength=0;
  358.     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 1., str);
  359.     if(status==U_BUFFER_OVERFLOW_ERROR)
  360.     {
  361.         status=U_ZERO_ERROR;
  362.         resultlength=resultLengthOut+1;
  363.         result=(UChar*)malloc(sizeof(UChar) * resultlength);
  364.         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 1., str);
  365.     }
  366.     if(U_FAILURE(status)){
  367.         log_err("ERROR: failure in message format on test#8 : %s\n", myErrorName(status));
  368.     }
  369.     if(u_strcmp(result, expected)==0)
  370.         log_verbose("PASS: MessagFormat successful on test#8\n");
  371.     else{
  372.         log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result), 
  373.             austrdup(expected) );
  374.     }
  375.  
  376.     free(result);
  377.     free(str);
  378.  
  379. }
  380. /*test u_parseMessage() with various test patterns */
  381. void TestParseMessage()
  382. {
  383.     UChar pattern[100];
  384.     UChar source[100];
  385.     UErrorCode status = U_ZERO_ERROR;
  386.     double value;
  387.     UChar str[10];
  388.     UChar res[10];
  389.         
  390.     log_verbose("\nTesting a sample for parse Message test#9\n");
  391.     
  392.     u_uastrcpy(source, "You deposited an amount of $500.00");
  393.     u_uastrcpy(pattern, "You {0} an amount of {1,number,currency}");
  394.     u_uastrcpy(res,"deposited");
  395.         
  396.     u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, str, &value);
  397.     if(U_FAILURE(status)){
  398.         log_err("ERROR: failure in parse Message on test#9: %s\n", myErrorName(status));
  399.     }
  400.     if(value==500.00  && u_strcmp(str,res)==0)
  401.         log_verbose("PASS: parseMessage successful on test#9\n");
  402.     else
  403.         log_err("FAIL: Error in parseMessage on test#9 \n");
  404.  
  405.     
  406.     
  407.     log_verbose("\nTesting a sample for parse Message test#10\n");
  408.     
  409.     u_uastrcpy(source, "There are 123 files on MyDisk created");
  410.     u_uastrcpy(pattern, "There are {0,number,integer} files on {1} created");
  411.     u_uastrcpy(res,"MyDisk");
  412.         
  413.     u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, &value, str);
  414.     if(U_FAILURE(status)){
  415.         log_err("ERROR: failure in parse Message on test#10: %s\n", myErrorName(status));
  416.     }
  417.     if(value==123.00 && u_strcmp(str,res)==0)
  418.         log_verbose("PASS: parseMessage successful on test#10\n");
  419.     else
  420.         log_err("FAIL: Error in parseMessage on test#10 \n");
  421.  
  422.  
  423.     
  424. }
  425.  
  426. void addMsgForTest(TestNode** root)
  427. {
  428.     addTest(root, &MessageFormatTest, "tsformat/cmsgtst/MessageFormatTest");
  429.     addTest(root, &TestSampleMessageFormat, "tsformat/cmsgtst/TestSampleMessageFormat");
  430.     addTest(root, &TestSampleFormatAndParse, "tsformat/cmsgtst/TestSampleFormatAndParse");
  431.     addTest(root, &TestMsgFormatChoice, "tsformat/cmsgtst/TestMsgFormatChoice");
  432.     addTest(root, &TestParseMessage, "tsformat/cmsgtst/TestParseMessage");
  433.  
  434. }
  435.