/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2001, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** * * File CMSGTST.C * * Modification History: * Name Description * Madhu Katragadda Creation ********************************************************************************* */ /* C API TEST FOR MESSAGE FORMAT */ #include "unicode/uloc.h" #include "unicode/utypes.h" #include "unicode/umsg.h" #include "unicode/udat.h" #include "unicode/umsg.h" #include "unicode/ustring.h" #include "cintltst.h" #include "cmsgtst.h" #include "cformtst.h" #include "cstring.h" #include "cmemory.h" static const char* const txt_testCasePatterns[] = { "Quotes '', '{', a {0,number,integer} '{'0}", "Quotes '', '{', a {0,number,integer} '{'0}", "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}", "'{'2,time,full}, for {1, number }, {0,number,integer} is {2,time,full} and full date is {2,date,full}", "'{'1,number,percent} for {0,number,integer} is {1,number,percent}", }; static const char* const txt_testResultStrings[] = { "Quotes ', {, a 1 {0}", "Quotes ', {, a 1 {0}", "You deposited 1 times an amount of $3,456.00 on 1/12/70", "{2,time,full}, for 3,456, 1 is 5:46:40 AM PST and full date is Monday, January 12, 1970", "{1,number,percent} for 1 is 345,600%" }; const int32_t cnt_testCases = 5; static UChar* testCasePatterns[5]; static UChar* testResultStrings[5]; static UBool strings_initialized = FALSE; /* function used to create the test patterns for testing Message formatting */ static void InitStrings( void ) { int32_t i; if (strings_initialized) return; for (i=0; i < cnt_testCases; i++ ) { testCasePatterns[i]=(UChar*)malloc(sizeof(UChar) * (strlen(txt_testCasePatterns[i]) + 1)); u_uastrcpy(testCasePatterns[i], txt_testCasePatterns[i] ); } for (i=0; i < cnt_testCases; i++ ) { testResultStrings[i] = (UChar*)malloc(sizeof(UChar) * (strlen(txt_testResultStrings[i]) + 1)); u_uastrcpy(testResultStrings[i], txt_testResultStrings[i] ); } strings_initialized = TRUE; } static void FreeStrings( void ) { int32_t i; if (!strings_initialized) return; for (i=0; i < cnt_testCases; i++ ) { free(testCasePatterns[i]); } for (i=0; i < cnt_testCases; i++ ) { free(testResultStrings[i]); } strings_initialized = FALSE; } /* Test u_formatMessage() with various test patterns() */ static void MessageFormatTest( void ) { UChar *str; UChar* result; int32_t resultLengthOut,resultlength,i, patternlength; UErrorCode status = U_ZERO_ERROR; UDate d1=1000000000.0; str=(UChar*)malloc(sizeof(UChar) * 7); u_uastrcpy(str, "MyDisk"); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("Testing u_formatMessage90\n"); InitStrings(); for (i = 0; i < cnt_testCases; i++) { status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result,sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) ); } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } } free(result); free(str); { for (i = 0; i < cnt_testCases; i++) { UParseError parseError; status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultlength=0; resultLengthOut=u_formatMessageWithError( "en_US",testCasePatterns[i], patternlength, result, resultlength, &parseError,&status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)uprv_malloc(sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) ); continue; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } uprv_free(result); result=NULL; } } { UErrorCode ec = U_ZERO_ERROR; int32_t patternLength = u_strlen(testCasePatterns[0]); UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec); if(U_FAILURE(ec)){ log_err("umsg_open() failed for testCasePattens[%d].\n",i); return; } for(i = 0;i