/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2006, 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/utypes.h" #if !UCONFIG_NO_FORMATTING #include #include #include #include "unicode/uloc.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" 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 PT 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++ ) { uint32_t strSize = (uint32_t)strlen(txt_testCasePatterns[i]) + 1; testCasePatterns[i]=(UChar*)malloc(sizeof(UChar) * strSize); u_uastrncpy(testCasePatterns[i], txt_testCasePatterns[i], strSize); } for (i=0; i < cnt_testCases; i++ ) { uint32_t strSize = (uint32_t)strlen(txt_testResultStrings[i]) + 1; testResultStrings[i] = (UChar*)malloc(sizeof(UChar) * strSize); u_uastrncpy(testResultStrings[i], txt_testResultStrings[i], strSize); } 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; } /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */ static UBool returnsNullForType(int firstParam, ...) { UBool isNULL; va_list marker; va_start(marker, firstParam); isNULL = (UBool)(va_arg(marker, void*) == NULL); va_end(marker); return isNULL; } /* 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; ctest_setTimeZone(NULL, &status); str=(UChar*)malloc(sizeof(UChar) * 7); u_uastrncpy(str, "MyDisk", 7); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("Testing u_formatMessage()\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); result = NULL; 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*)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]) ); } 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