1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /******************************************************************** 4 * COPYRIGHT: 5 * Copyright (c) 1997-2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************** 8 * 9 * File CMSGTST.C 10 * 11 * Modification History: 12 * Name Description 13 * Madhu Katragadda Creation 14 ********************************************************************/ 15 /* C API TEST FOR MESSAGE FORMAT */ 16 17 #include "unicode/utypes.h" 18 19 #if !UCONFIG_NO_FORMATTING 20 21 #include <stdlib.h> 22 #include <string.h> 23 #include <stdarg.h> 24 #include "unicode/uloc.h" 25 #include "unicode/umsg.h" 26 #include "unicode/udat.h" 27 #include "unicode/umsg.h" 28 #include "unicode/ustring.h" 29 #include "cintltst.h" 30 #include "cmsgtst.h" 31 #include "cformtst.h" 32 #include "cmemory.h" 33 34 static const char* const txt_testCasePatterns[] = { 35 "Quotes '', '{', a {0,number,integer} '{'0}", 36 "Quotes '', '{', a {0,number,integer} '{'0}", 37 "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}", 38 "'{'2,time,full}, for {1, number }, {0,number,integer} is {2,time,full} and full date is {2,date,full}", 39 "'{'1,number,percent} for {0,number,integer} is {1,number,percent}", 40 }; 41 42 static const char* const txt_testResultStrings[] = { 43 "Quotes ', {, a 1 {0}", 44 "Quotes ', {, a 1 {0}", 45 "You deposited 1 times an amount of $3,456.00 on 1/12/70", 46 "{2,time,full}, for 3,456, 1 is 5:46:40 AM Pacific Standard Time and full date is Monday, January 12, 1970", 47 "{1,number,percent} for 1 is 345,600%" 48 }; 49 50 const int32_t cnt_testCases = 5; 51 static UChar* testCasePatterns[5]; 52 53 static UChar* testResultStrings[5]; 54 55 static UBool strings_initialized = FALSE; 56 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) 62 return; 63 64 for (i=0; i < cnt_testCases; i++ ) { 65 uint32_t strSize = (uint32_t)strlen(txt_testCasePatterns[i]) + 1; 66 testCasePatterns[i]=(UChar*)malloc(sizeof(UChar) * strSize); 67 u_uastrncpy(testCasePatterns[i], txt_testCasePatterns[i], strSize); 68 } 69 for (i=0; i < cnt_testCases; i++ ) { 70 uint32_t strSize = (uint32_t)strlen(txt_testResultStrings[i]) + 1; 71 testResultStrings[i] = (UChar*)malloc(sizeof(UChar) * strSize); 72 u_uastrncpy(testResultStrings[i], txt_testResultStrings[i], strSize); 73 } 74 75 strings_initialized = TRUE; 76 } 77 78 static void FreeStrings( void ) 79 { 80 int32_t i; 81 if (!strings_initialized) 82 return; 83 84 for (i=0; i < cnt_testCases; i++ ) { 85 free(testCasePatterns[i]); 86 } 87 for (i=0; i < cnt_testCases; i++ ) { 88 free(testResultStrings[i]); 89 } 90 strings_initialized = FALSE; 91 } 92 93 #if (U_PLATFORM == U_PF_LINUX) /* add platforms here .. */ 94 /* Keep the #if above in sync with the one below that has the same "add platforms here .." comment. */ 95 #else 96 /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */ 97 static UBool returnsNullForType(int firstParam, ...) { 98 UBool isNULL; 99 va_list marker; 100 va_start(marker, firstParam); 101 isNULL = (UBool)(va_arg(marker, void*) == NULL); 102 va_end(marker); 103 return isNULL; 104 } 105 #endif 106 107 /* Test u_formatMessage() with various test patterns() */ 108 static void MessageFormatTest( void ) 109 { 110 UChar *str; 111 UChar* result; 112 int32_t resultLengthOut,resultlength,i, patternlength; 113 UErrorCode status = U_ZERO_ERROR; 114 UDate d1=1000000000.0; 115 116 ctest_setTimeZone(NULL, &status); 117 118 str=(UChar*)malloc(sizeof(UChar) * 7); 119 u_uastrncpy(str, "MyDisk", 7); 120 resultlength=1; 121 result=(UChar*)malloc(sizeof(UChar) * 1); 122 log_verbose("Testing u_formatMessage()\n"); 123 InitStrings(); 124 for (i = 0; i < cnt_testCases; i++) { 125 status=U_ZERO_ERROR; 126 patternlength=u_strlen(testCasePatterns[i]); 127 resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 128 &status, 1, 3456.00, d1); 129 if(status== U_BUFFER_OVERFLOW_ERROR) 130 { 131 status=U_ZERO_ERROR; 132 resultlength=resultLengthOut+1; 133 result=(UChar*)realloc(result,sizeof(UChar) * resultlength); 134 u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 135 &status, 1, 3456.00, d1); 136 } 137 if(U_FAILURE(status)){ 138 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); 139 continue; 140 } 141 if(u_strcmp(result, testResultStrings[i])==0){ 142 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); 143 } 144 else{ 145 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, 146 austrdup(result), austrdup(testResultStrings[i]) ); 147 } 148 } 149 free(result); 150 result = NULL; 151 free(str); 152 { 153 154 for (i = 0; i < cnt_testCases; i++) { 155 UParseError parseError; 156 status=U_ZERO_ERROR; 157 patternlength=u_strlen(testCasePatterns[i]); 158 resultlength=0; 159 resultLengthOut=u_formatMessageWithError( "en_US",testCasePatterns[i], patternlength, result, resultlength, 160 &parseError,&status, 1, 3456.00, d1); 161 if(status== U_BUFFER_OVERFLOW_ERROR) 162 { 163 status=U_ZERO_ERROR; 164 resultlength=resultLengthOut+1; 165 result=(UChar*)malloc(sizeof(UChar) * resultlength); 166 u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 167 &status, 1, 3456.00, d1); 168 } 169 if(U_FAILURE(status)){ 170 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); 171 continue; 172 } 173 if(u_strcmp(result, testResultStrings[i])==0){ 174 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); 175 } 176 else{ 177 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, 178 austrdup(result), austrdup(testResultStrings[i]) ); 179 } 180 free(result); 181 result=NULL; 182 } 183 } 184 { 185 UErrorCode ec = U_ZERO_ERROR; 186 int32_t patternLength = u_strlen(testCasePatterns[0]); 187 188 UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec); 189 190 if(U_FAILURE(ec)){ 191 log_data_err("umsg_open() failed for testCasePattens[0]. -> %s (Are you missing data?)\n", u_errorName(ec)); 192 return; 193 } 194 for(i = 0;i<cnt_testCases; i++){ 195 UParseError parseError; 196 int32_t resultLength =0,count=0; 197 int32_t one=0; 198 int32_t two=0; 199 UDate d2=0; 200 201 result=NULL; 202 // Alternate between specifying the length and using NUL-termination. 203 patternLength = ((i & 1) == 0) ? u_strlen(testCasePatterns[i]) : -1; 204 205 umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec); 206 if(U_FAILURE(ec)){ 207 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i); 208 return; 209 } 210 /* pre-flight */ 211 resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1); 212 if(ec==U_BUFFER_OVERFLOW_ERROR){ 213 ec=U_ZERO_ERROR; 214 result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2); 215 resultLength = umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1); 216 if(U_FAILURE(ec)){ 217 log_err("ERROR: failure in message format on testcase %d: %s\n", i, u_errorName(status) ); 218 free(result); 219 return; 220 } 221 222 if(u_strcmp(result, testResultStrings[i])==0){ 223 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); 224 } 225 else{ 226 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, 227 austrdup(result), austrdup(testResultStrings[i]) ); 228 } 229 230 #if (U_PLATFORM == U_PF_LINUX) /* add platforms here .. */ 231 log_verbose("Skipping potentially crashing test for mismatched varargs.\n"); 232 #else 233 log_verbose("Note: the next is a platform dependent test. If it crashes, add an exclusion for your platform near %s:%d\n", __FILE__, __LINE__); 234 235 if (returnsNullForType(1, (double)2.0)) { 236 /* HP/UX and possibly other platforms don't properly check for this case. 237 We pass in a UDate, but the function expects a UDate *. When va_arg is used, 238 most compilers will return NULL, but HP-UX won't do that and will return 2 239 in this case. This is a platform dependent test. It crashes on some systems. 240 241 If you get a crash here, see the definition of returnsNullForType. 242 243 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2 244 */ 245 umsg_parse(formatter,result,resultLength,&count,&ec,one,two,d2); 246 if(ec!=U_ILLEGAL_ARGUMENT_ERROR){ 247 log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec)); 248 }else{ 249 ec = U_ZERO_ERROR; 250 } 251 } 252 else { 253 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i); 254 } 255 #endif 256 257 umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2); 258 if(U_FAILURE(ec)){ 259 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec)); 260 } 261 free(result); 262 }else{ 263 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i); 264 } 265 } 266 umsg_close(formatter); 267 } 268 FreeStrings(); 269 270 ctest_resetTimeZone(); 271 } 272 273 274 /*test u_formatMessage() with sample patterns */ 275 static void TestSampleMessageFormat(void) 276 { 277 UChar *str; 278 UChar *result; 279 UChar pattern[100], expected[100]; 280 int32_t resultLengthOut, resultlength; 281 UDate d = 837039928046.0; 282 UErrorCode status = U_ZERO_ERROR; 283 284 ctest_setTimeZone(NULL, &status); 285 286 str=(UChar*)malloc(sizeof(UChar) * 15); 287 u_uastrcpy(str, "abc"); 288 289 u_uastrcpy(pattern, "There are {0} files on {1,date}"); 290 u_uastrcpy(expected, "There are abc files on Jul 10, 1996"); 291 result=(UChar*)malloc(sizeof(UChar) * 1); 292 log_verbose("\nTesting a sample for Message format test#1\n"); 293 resultlength=1; 294 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d); 295 if(status==U_BUFFER_OVERFLOW_ERROR) 296 { 297 status=U_ZERO_ERROR; 298 resultlength=resultLengthOut+1; 299 result=(UChar*)realloc(result, sizeof(UChar) * resultlength); 300 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d); 301 } 302 if(U_FAILURE(status)){ 303 log_data_err("Error: failure in message format on test#1: %s (Are you missing data?)\n", myErrorName(status)); 304 } 305 else if(u_strcmp(result, expected)==0) 306 log_verbose("PASS: MessagFormat successful on test#1\n"); 307 else{ 308 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n", 309 austrdup(result), austrdup(expected) ); 310 } 311 312 313 log_verbose("\nTesting message format with another pattern test#2\n"); 314 u_uastrcpy(pattern, "The disk \"{0}\" contains {1,number,integer} file(s)"); 315 u_uastrcpy(expected, "The disk \"MyDisk\" contains 23 file(s)"); 316 u_uastrcpy(str, "MyDisk"); 317 318 resultLengthOut=u_formatMessage( "en_US", 319 pattern, 320 u_strlen(pattern), 321 result, 322 resultlength, 323 &status, 324 str, 325 235); 326 if(status==U_BUFFER_OVERFLOW_ERROR) 327 { 328 status=U_ZERO_ERROR; 329 resultlength=resultLengthOut+1; 330 result=(UChar*)realloc(result, sizeof(UChar) * (resultlength+1)); 331 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 23); 332 } 333 if(U_FAILURE(status)){ 334 log_data_err("Error: failure in message format on test#2 : %s (Are you missing data?)\n", myErrorName(status)); 335 } 336 else if(u_strcmp(result, expected)==0) 337 log_verbose("PASS: MessagFormat successful on test#2\n"); 338 else{ 339 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n", 340 austrdup(result), austrdup(expected) ); 341 } 342 343 344 345 log_verbose("\nTesting message format with another pattern test#3\n"); 346 u_uastrcpy(pattern, "You made a {0} of {1,number,currency}"); 347 u_uastrcpy(expected, "You made a deposit of $500.00"); 348 u_uastrcpy(str, "deposit"); 349 resultlength=0; 350 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00); 351 if(status==U_BUFFER_OVERFLOW_ERROR) 352 { 353 status=U_ZERO_ERROR; 354 resultlength=resultLengthOut+1; 355 result=(UChar*)realloc(result, sizeof(UChar) * resultlength); 356 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 500.00); 357 } 358 if(U_FAILURE(status)){ 359 log_data_err("Error: failure in message format on test#3 : %s (Are you missing data?)\n", myErrorName(status)); 360 } 361 else if(u_strcmp(result, expected)==0) 362 log_verbose("PASS: MessagFormat successful on test#3\n"); 363 else{ 364 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result), 365 austrdup(expected) ); 366 } 367 368 free(result); 369 free(str); 370 371 ctest_resetTimeZone(); 372 } 373 374 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */ 375 static void TestNewFormatAndParseAPI(void) 376 { 377 378 UChar *result, tzID[4], str[25]; 379 UChar pattern[100]; 380 UChar expected[100]; 381 int32_t resultLengthOut, resultlength; 382 UCalendar *cal; 383 UDate d1,d; 384 UDateFormat *def1; 385 UErrorCode status = U_ZERO_ERROR; 386 int32_t value = 0; 387 UChar ret[30]; 388 UParseError parseError; 389 UMessageFormat* fmt = NULL; 390 int32_t count=0; 391 392 ctest_setTimeZone(NULL, &status); 393 394 log_verbose("Testing format and parse with parse error\n"); 395 396 u_uastrcpy(str, "disturbance in force"); 397 u_uastrcpy(tzID, "PST"); 398 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); 399 if(U_FAILURE(status)){ 400 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); 401 return; 402 } 403 ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); 404 d1=ucal_getMillis(cal, &status); 405 if(U_FAILURE(status)){ 406 log_err("Error: failure in get millis: %s\n", myErrorName(status) ); 407 return; 408 } 409 410 log_verbose("\nTesting with pattern test#4"); 411 u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); 412 u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); 413 resultlength=1; 414 fmt = umsg_open(pattern,u_strlen(pattern),"en_US",&parseError,&status); 415 if(U_FAILURE(status)){ 416 log_data_err("error in umsg_open : %s (Are you missing data?)\n", u_errorName(status) ); 417 return; 418 } 419 result=(UChar*)malloc(sizeof(UChar) * resultlength); 420 421 resultLengthOut=umsg_format(fmt ,result, resultlength,&status, d1, str, 7); 422 if(status==U_BUFFER_OVERFLOW_ERROR) 423 { 424 status=U_ZERO_ERROR; 425 resultlength=resultLengthOut+1; 426 result=(UChar*)realloc(result, sizeof(UChar) * resultlength); 427 u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7); 428 429 } 430 if(U_FAILURE(status)){ 431 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status)); 432 } 433 if(u_strcmp(result, expected)==0) 434 log_verbose("PASS: MessagFormat successful on test#4\n"); 435 else{ 436 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), 437 austrdup(expected) ); 438 } 439 440 441 /*try to parse this and check*/ 442 log_verbose("\nTesting the parse Message test#5\n"); 443 444 umsg_parse(fmt, result, u_strlen(result),&count,&status, &d, ret, &value); 445 if(U_FAILURE(status)){ 446 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status)); 447 } 448 if(value!=7 && u_strcmp(str,ret)!=0) 449 log_err("FAIL: Error in parseMessage on test#5 \n"); 450 else 451 log_verbose("PASS: parseMessage successful on test#5\n"); 452 453 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); 454 if(U_FAILURE(status)) 455 { 456 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status)); 457 }else{ 458 459 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) 460 log_verbose("PASS: parseMessage successful test#5\n"); 461 else{ 462 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", 463 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); 464 } 465 } 466 umsg_close(fmt); 467 udat_close(def1); 468 ucal_close(cal); 469 470 free(result); 471 472 ctest_resetTimeZone(); 473 } 474 475 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */ 476 static void TestSampleFormatAndParseWithError(void) 477 { 478 479 UChar *result, *tzID, *str; 480 UChar pattern[100]; 481 482 UChar expected[100]; 483 int32_t resultLengthOut, resultlength; 484 UCalendar *cal; 485 UDate d1,d; 486 UDateFormat *def1; 487 UErrorCode status = U_ZERO_ERROR; 488 int32_t value = 0; 489 UChar ret[30]; 490 UParseError parseError; 491 492 ctest_setTimeZone(NULL, &status); 493 494 log_verbose("Testing format and parse with parse error\n"); 495 496 str=(UChar*)malloc(sizeof(UChar) * 25); 497 u_uastrcpy(str, "disturbance in force"); 498 tzID=(UChar*)malloc(sizeof(UChar) * 4); 499 u_uastrcpy(tzID, "PST"); 500 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); 501 if(U_FAILURE(status)){ 502 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); 503 } 504 ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); 505 d1=ucal_getMillis(cal, &status); 506 if(U_FAILURE(status)){ 507 log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) ); 508 } 509 510 log_verbose("\nTesting with pattern test#4"); 511 u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); 512 u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); 513 resultlength=1; 514 result=(UChar*)malloc(sizeof(UChar) * resultlength); 515 resultLengthOut=u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7); 516 if(status==U_BUFFER_OVERFLOW_ERROR) 517 { 518 status=U_ZERO_ERROR; 519 resultlength=resultLengthOut+1; 520 result=(UChar*)realloc(result, sizeof(UChar) * resultlength); 521 u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7); 522 523 } 524 if(U_FAILURE(status)){ 525 log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status)); 526 } 527 else if(u_strcmp(result, expected)==0) 528 log_verbose("PASS: MessagFormat successful on test#4\n"); 529 else{ 530 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), 531 austrdup(expected) ); 532 } 533 534 535 /*try to parse this and check*/ 536 log_verbose("\nTesting the parse Message test#5\n"); 537 538 u_parseMessageWithError("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &parseError,&status, &d, ret, &value); 539 if(U_FAILURE(status)){ 540 log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status)); 541 } 542 else if(value!=7 && u_strcmp(str,ret)!=0) 543 log_err("FAIL: Error in parseMessage on test#5 \n"); 544 else 545 log_verbose("PASS: parseMessage successful on test#5\n"); 546 547 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); 548 if(U_FAILURE(status)) 549 { 550 log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status)); 551 }else{ 552 553 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) 554 log_verbose("PASS: parseMessage successful test#5\n"); 555 else{ 556 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", 557 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); 558 } 559 } 560 udat_close(def1); 561 ucal_close(cal); 562 563 free(result); 564 free(str); 565 free(tzID); 566 567 ctest_resetTimeZone(); 568 } 569 570 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */ 571 static void TestSampleFormatAndParse(void) 572 { 573 574 UChar *result, *tzID, *str; 575 UChar pattern[100]; 576 UChar expected[100]; 577 int32_t resultLengthOut, resultlength; 578 UCalendar *cal; 579 UDate d1,d; 580 UDateFormat *def1; 581 UErrorCode status = U_ZERO_ERROR; 582 int32_t value = 0; 583 UChar ret[30]; 584 585 ctest_setTimeZone(NULL, &status); 586 587 log_verbose("Testing format and parse\n"); 588 589 str=(UChar*)malloc(sizeof(UChar) * 25); 590 u_uastrcpy(str, "disturbance in force"); 591 tzID=(UChar*)malloc(sizeof(UChar) * 4); 592 u_uastrcpy(tzID, "PST"); 593 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); 594 if(U_FAILURE(status)){ 595 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); 596 } 597 ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); 598 d1=ucal_getMillis(cal, &status); 599 if(U_FAILURE(status)){ 600 log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) ); 601 } 602 603 log_verbose("\nTesting with pattern test#4"); 604 u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); 605 u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); 606 resultlength=1; 607 result=(UChar*)malloc(sizeof(UChar) * resultlength); 608 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); 609 if(status==U_BUFFER_OVERFLOW_ERROR) 610 { 611 status=U_ZERO_ERROR; 612 resultlength=resultLengthOut+1; 613 result=(UChar*)realloc(result, sizeof(UChar) * resultlength); 614 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); 615 616 } 617 if(U_FAILURE(status)){ 618 log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status)); 619 } 620 else if(u_strcmp(result, expected)==0) 621 log_verbose("PASS: MessagFormat successful on test#4\n"); 622 else{ 623 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), 624 austrdup(expected) ); 625 } 626 627 628 /*try to parse this and check*/ 629 log_verbose("\nTesting the parse Message test#5\n"); 630 631 u_parseMessage("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &status, &d, ret, &value); 632 if(U_FAILURE(status)){ 633 log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status)); 634 } 635 else if(value!=7 && u_strcmp(str,ret)!=0) 636 log_err("FAIL: Error in parseMessage on test#5 \n"); 637 else 638 log_verbose("PASS: parseMessage successful on test#5\n"); 639 640 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); 641 if(U_FAILURE(status)) 642 { 643 log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status)); 644 }else{ 645 646 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) 647 log_verbose("PASS: parseMessage successful test#5\n"); 648 else{ 649 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", 650 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); 651 } 652 } 653 udat_close(def1); 654 ucal_close(cal); 655 656 free(result); 657 free(str); 658 free(tzID); 659 660 ctest_resetTimeZone(); 661 } 662 663 /* Test message format with a Select option */ 664 static void TestMsgFormatSelect(void) 665 { 666 UChar* str; 667 UChar* str1; 668 UErrorCode status = U_ZERO_ERROR; 669 UChar *result; 670 UChar pattern[100]; 671 UChar expected[100]; 672 int32_t resultlength,resultLengthOut; 673 674 str=(UChar*)malloc(sizeof(UChar) * 25); 675 u_uastrcpy(str, "Kirti"); 676 str1=(UChar*)malloc(sizeof(UChar) * 25); 677 u_uastrcpy(str1, "female"); 678 log_verbose("Testing message format with Select test #1\n:"); 679 u_uastrcpy(pattern, "{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris."); 680 u_uastrcpy(expected, "Kirti est all\\u00E9e \\u00E0 Paris."); 681 resultlength=0; 682 resultLengthOut=u_formatMessage( "fr", pattern, u_strlen(pattern), NULL, resultlength, &status, str , str1); 683 if(status==U_BUFFER_OVERFLOW_ERROR) 684 { 685 status=U_ZERO_ERROR; 686 resultlength=resultLengthOut+1; 687 result=(UChar*)malloc(sizeof(UChar) * resultlength); 688 u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1); 689 if(u_strcmp(result, expected)==0) 690 log_verbose("PASS: MessagFormat successful on Select test#1\n"); 691 else{ 692 log_err("FAIL: Error in MessageFormat on Select test#1\n GOT %s EXPECTED %s\n", austrdup(result), 693 austrdup(expected) ); 694 } 695 free(result); 696 } 697 if(U_FAILURE(status)){ 698 log_data_err("ERROR: failure in message format on Select test#1 : %s \n", myErrorName(status)); 699 } 700 free(str); 701 free(str1); 702 703 /*Test a nested pattern*/ 704 str=(UChar*)malloc(sizeof(UChar) * 25); 705 u_uastrcpy(str, "Noname"); 706 str1=(UChar*)malloc(sizeof(UChar) * 25); 707 u_uastrcpy(str1, "other"); 708 log_verbose("Testing message format with Select test #2\n:"); 709 u_uastrcpy(pattern, "{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris."); 710 u_uastrcpy(expected, "Noname est all\\u00E9 \\u00E0 Paris."); 711 resultlength=0; 712 resultLengthOut=u_formatMessage( "fr", pattern, u_strlen(pattern), NULL, resultlength, &status, str , str1,6); 713 if(status==U_BUFFER_OVERFLOW_ERROR) 714 { 715 status=U_ZERO_ERROR; 716 resultlength=resultLengthOut+1; 717 result=(UChar*)malloc(sizeof(UChar) * resultlength); 718 u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1, 6); 719 if(u_strcmp(result, expected)==0) 720 log_verbose("PASS: MessagFormat successful on Select test#2\n"); 721 else{ 722 log_err("FAIL: Error in MessageFormat on Select test#2\n GOT %s EXPECTED %s\n", austrdup(result), 723 austrdup(expected) ); 724 } 725 free(result); 726 } 727 if(U_FAILURE(status)){ 728 log_data_err("ERROR: failure in message format on Select test#2 : %s \n", myErrorName(status)); 729 } 730 free(str); 731 free(str1); 732 } 733 734 /* test message format with a choice option */ 735 static void TestMsgFormatChoice(void) 736 { 737 UChar* str; 738 UErrorCode status = U_ZERO_ERROR; 739 UChar *result; 740 UChar pattern[100]; 741 UChar expected[100]; 742 int32_t resultlength,resultLengthOut; 743 744 str=(UChar*)malloc(sizeof(UChar) * 25); 745 u_uastrcpy(str, "MyDisk"); 746 log_verbose("Testing message format with choice test #6\n:"); 747 /* 748 * Before ICU 4.8, umsg_xxx() did not detect conflicting argument types, 749 * and this pattern had {0,number,integer} as the inner argument. 750 * The choice argument has kDouble type while {0,number,integer} has kLong (int32_t). 751 * ICU 4.8 and above detects this as an error. 752 * We changed this pattern to work as intended. 753 */ 754 u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number} files}"); 755 u_uastrcpy(expected, "The disk MyDisk contains 100 files"); 756 resultlength=0; 757 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 100., str); 758 if(status==U_BUFFER_OVERFLOW_ERROR) 759 { 760 status=U_ZERO_ERROR; 761 resultlength=resultLengthOut+1; 762 result=(UChar*)malloc(sizeof(UChar) * resultlength); 763 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 100., str); 764 if(u_strcmp(result, expected)==0) 765 log_verbose("PASS: MessagFormat successful on test#6\n"); 766 else{ 767 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result), 768 austrdup(expected) ); 769 } 770 free(result); 771 } 772 if(U_FAILURE(status)){ 773 log_data_err("ERROR: failure in message format on test#6 : %s (Are you missing data?)\n", myErrorName(status)); 774 } 775 776 log_verbose("Testing message format with choice test #7\n:"); 777 u_uastrcpy(expected, "The disk MyDisk contains no files"); 778 resultlength=0; 779 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 0., str); 780 if(status==U_BUFFER_OVERFLOW_ERROR) 781 { 782 status=U_ZERO_ERROR; 783 resultlength=resultLengthOut+1; 784 result=(UChar*)malloc(sizeof(UChar) * resultlength); 785 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 0., str); 786 787 if(u_strcmp(result, expected)==0) 788 log_verbose("PASS: MessagFormat successful on test#7\n"); 789 else{ 790 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result), 791 austrdup(expected) ); 792 } 793 free(result); 794 } 795 if(U_FAILURE(status)){ 796 log_data_err("ERROR: failure in message format on test#7 : %s (Are you missing data?)\n", myErrorName(status)); 797 } 798 799 log_verbose("Testing message format with choice test #8\n:"); 800 u_uastrcpy(expected, "The disk MyDisk contains one file"); 801 resultlength=0; 802 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 1., str); 803 if(status==U_BUFFER_OVERFLOW_ERROR) 804 { 805 status=U_ZERO_ERROR; 806 resultlength=resultLengthOut+1; 807 result=(UChar*)malloc(sizeof(UChar) * resultlength); 808 u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 1., str); 809 810 if(u_strcmp(result, expected)==0) 811 log_verbose("PASS: MessagFormat successful on test#8\n"); 812 else{ 813 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result), 814 austrdup(expected) ); 815 } 816 817 free(result); 818 } 819 if(U_FAILURE(status)){ 820 log_data_err("ERROR: failure in message format on test#8 : %s (Are you missing data?)\n", myErrorName(status)); 821 } 822 823 free(str); 824 825 } 826 827 /*test u_parseMessage() with various test patterns */ 828 static void TestParseMessage(void) 829 { 830 UChar pattern[100]; 831 UChar source[100]; 832 UErrorCode status = U_ZERO_ERROR; 833 int32_t value; 834 UChar str[10]; 835 UChar res[10]; 836 837 log_verbose("\nTesting a sample for parse Message test#9\n"); 838 839 u_uastrcpy(source, "You deposited an amount of $500.00"); 840 u_uastrcpy(pattern, "You {0} an amount of {1,number,currency}"); 841 u_uastrcpy(res,"deposited"); 842 843 u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, str, &value); 844 if(U_FAILURE(status)){ 845 log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status)); 846 } 847 else if(value==500.00 && u_strcmp(str,res)==0) 848 log_verbose("PASS: parseMessage successful on test#9\n"); 849 else 850 log_err("FAIL: Error in parseMessage on test#9 \n"); 851 852 853 854 log_verbose("\nTesting a sample for parse Message test#10\n"); 855 856 u_uastrcpy(source, "There are 123 files on MyDisk created"); 857 u_uastrcpy(pattern, "There are {0,number,integer} files on {1} created"); 858 u_uastrcpy(res,"MyDisk"); 859 860 u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, &value, str); 861 if(U_FAILURE(status)){ 862 log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status)); 863 } 864 else if(value==123.00 && u_strcmp(str,res)==0) 865 log_verbose("PASS: parseMessage successful on test#10\n"); 866 else 867 log_err("FAIL: Error in parseMessage on test#10 \n"); 868 } 869 870 static int32_t CallFormatMessage(const char* locale, UChar* testCasePattern, int32_t patternLength, 871 UChar* result, int32_t resultLength, UErrorCode *status, ...) 872 { 873 int32_t len = 0; 874 va_list ap; 875 va_start(ap, status); 876 len = u_vformatMessage(locale, testCasePattern, patternLength, result, resultLength, ap, status); 877 va_end(ap); 878 return len; 879 } 880 881 /* Test u_vformatMessage() with various test patterns. */ 882 static void TestMessageFormatWithValist( void ) 883 { 884 885 UChar *str; 886 UChar* result; 887 int32_t resultLengthOut,resultlength,i, patternlength; 888 UErrorCode status = U_ZERO_ERROR; 889 UDate d1=1000000000.0; 890 891 ctest_setTimeZone(NULL, &status); 892 893 str=(UChar*)malloc(sizeof(UChar) * 7); 894 u_uastrcpy(str, "MyDisk"); 895 resultlength=1; 896 result=(UChar*)malloc(sizeof(UChar) * 1); 897 log_verbose("Testing u_formatMessage90\n"); 898 InitStrings(); 899 for (i = 0; i < cnt_testCases; i++) { 900 status=U_ZERO_ERROR; 901 patternlength=u_strlen(testCasePatterns[i]); 902 resultLengthOut=CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 903 &status, 1, 3456.00, d1); 904 if(status== U_BUFFER_OVERFLOW_ERROR) 905 { 906 status=U_ZERO_ERROR; 907 resultlength=resultLengthOut+1; 908 result=(UChar*)realloc(result,sizeof(UChar) * resultlength); 909 CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, 910 &status, 1, 3456.00, d1); 911 } 912 if(U_FAILURE(status)){ 913 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); 914 } 915 else if(u_strcmp(result, testResultStrings[i])==0){ 916 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); 917 } 918 else{ 919 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, 920 austrdup(result), austrdup(testResultStrings[i]) ); 921 } 922 } 923 free(result); 924 free(str); 925 FreeStrings(); 926 927 ctest_resetTimeZone(); 928 } 929 930 static void CallParseMessage(const char* locale, UChar* pattern, int32_t patternLength, 931 UChar* source, int32_t sourceLength, UErrorCode *status, ...) 932 { 933 va_list ap; 934 va_start(ap, status); 935 u_vparseMessage(locale, pattern, patternLength, source, sourceLength, ap, status); 936 va_end(ap); 937 } 938 939 /*test u_vparseMessage() with various test patterns */ 940 static void TestParseMessageWithValist(void) 941 { 942 UChar pattern[100]; 943 UChar source[100]; 944 UErrorCode status = U_ZERO_ERROR; 945 int32_t value; 946 UChar str[10]; 947 UChar res[10]; 948 949 log_verbose("\nTesting a sample for parse Message test#9\n"); 950 951 u_uastrcpy(source, "You deposited an amount of $500.00"); 952 u_uastrcpy(pattern, "You {0} an amount of {1,number,currency}"); 953 u_uastrcpy(res,"deposited"); 954 955 CallParseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, str, &value); 956 if(U_FAILURE(status)){ 957 log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status)); 958 } 959 else if(value==500.00 && u_strcmp(str,res)==0) 960 log_verbose("PASS: parseMessage successful on test#9\n"); 961 else 962 log_err("FAIL: Error in parseMessage on test#9\n"); 963 964 965 log_verbose("\nTesting a sample for parse Message test#10\n"); 966 967 u_uastrcpy(source, "There are 123 files on MyDisk created"); 968 u_uastrcpy(pattern, "There are {0,number,integer} files on {1} created"); 969 u_uastrcpy(res,"MyDisk"); 970 971 CallParseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, &value, str); 972 if(U_FAILURE(status)){ 973 log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status)); 974 } 975 else if(value==123.00 && u_strcmp(str,res)==0) 976 log_verbose("PASS: parseMessage successful on test#10\n"); 977 else 978 log_err("FAIL: Error in parseMessage on test#10 \n"); 979 } 980 981 /** 982 * Regression test for ICU4C Jitterbug 904 983 */ 984 static void TestJ904(void) { 985 UChar pattern[256]; 986 UChar result[256]; 987 UChar string[16]; 988 char cresult[256]; 989 int32_t length; 990 UErrorCode status = U_ZERO_ERROR; 991 const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}"; 992 const char* EXP = "Number 0,143, String foo, Date 12:34:56.789"; 993 994 ctest_setTimeZone(NULL, &status); 995 996 u_uastrcpy(string, "foo"); 997 /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use 998 * 12:mm:ss.SSS. Why? So this test generates the same output -- 999 * "12:34:56.789" -- regardless of time zone (as long as we aren't 1000 * in one of the 30 minute offset zones!). */ 1001 u_uastrcpy(pattern, PAT); 1002 length = u_formatMessage("nl", pattern, u_strlen(pattern), 1003 result, 256, &status, 1004 string, 1/7.0, 1005 789.0+1000*(56+60*(34+60*12))); 1006 (void)length; /* Suppress set but not used warning. */ 1007 1008 u_austrncpy(cresult, result, sizeof(cresult)); 1009 1010 /* This test passes if it DOESN'T CRASH. However, we test the 1011 * output anyway. If the string doesn't match in the date part, 1012 * check to see that the machine doesn't have an unusual time zone 1013 * offset, that is, one with a non-zero minutes/seconds offset 1014 * from GMT -- see above. */ 1015 if (strcmp(cresult, EXP) == 0) { 1016 log_verbose("Ok: \"%s\"\n", cresult); 1017 } else { 1018 log_data_err("FAIL: got \"%s\", expected \"%s\" -> %s (Are you missing data?)\n", cresult, EXP, u_errorName(status)); 1019 } 1020 1021 ctest_resetTimeZone(); 1022 } 1023 1024 static void OpenMessageFormatTest(void) 1025 { 1026 UMessageFormat *f1, *f2, *f3; 1027 UChar pattern[256]; 1028 UChar result[256]; 1029 char cresult[256]; 1030 UParseError parseError; 1031 const char* locale = "hi_IN"; 1032 char* retLoc; 1033 const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}"; 1034 int32_t length=0; 1035 UErrorCode status = U_ZERO_ERROR; 1036 1037 u_uastrncpy(pattern, PAT, UPRV_LENGTHOF(pattern)); 1038 1039 /* Test umsg_open */ 1040 f1 = umsg_open(pattern,length,NULL,NULL,&status); 1041 1042 if(U_FAILURE(status)) 1043 { 1044 log_err("umsg_open failed with pattern %s. Error: \n", PAT, u_errorName(status)); 1045 return; 1046 } 1047 1048 /* Test umsg_open with parse error */ 1049 status = U_ZERO_ERROR; 1050 f2 = umsg_open(pattern,length,NULL,&parseError,&status); 1051 1052 if(U_FAILURE(status)) 1053 { 1054 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT, u_errorName(status)); 1055 return; 1056 } 1057 1058 /* Test umsg_clone */ 1059 status = U_ZERO_ERROR; 1060 f3 = umsg_clone(f1,&status); 1061 if(U_FAILURE(status)) 1062 { 1063 log_err("umsg_clone failed. Error %s \n", u_errorName(status)); 1064 } 1065 1066 /* Test umsg_setLocale */ 1067 umsg_setLocale(f1,locale); 1068 /* Test umsg_getLocale */ 1069 retLoc = (char*)umsg_getLocale(f1); 1070 if(strcmp(retLoc,locale)!=0) 1071 { 1072 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale, retLoc); 1073 } 1074 1075 /* Test umsg_applyPattern */ 1076 status = U_ZERO_ERROR; 1077 umsg_applyPattern(f1,pattern,(int32_t)strlen(PAT),NULL,&status); 1078 if(U_FAILURE(status)) 1079 { 1080 log_data_err("umsg_applyPattern failed. Error %s (Are you missing data?)\n",u_errorName(status)); 1081 } 1082 1083 /* Test umsg_toPattern */ 1084 umsg_toPattern(f1,result,256,&status); 1085 if(U_FAILURE(status) ){ 1086 log_data_err("umsg_toPattern method failed. Error: %s (Are you missing data?)\n",u_errorName(status)); 1087 } else { 1088 if(u_strcmp(result,pattern)!=0){ 1089 u_UCharsToChars(result,cresult,256); 1090 log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT,cresult); 1091 } 1092 } 1093 /* umsg_format umsg_parse */ 1094 1095 umsg_close(f1); 1096 umsg_close(f2); 1097 umsg_close(f3); 1098 } 1099 1100 static void MessageLength(void) 1101 { 1102 UErrorCode status = U_ZERO_ERROR; 1103 const char patChars[] = {"123{0}456{0}"}; 1104 const char expectedChars[] = {"123abc"}; 1105 UChar pattern[sizeof(patChars)]; 1106 UChar arg[] = {0x61,0x62,0x63,0}; 1107 UChar result[128] = {0}; 1108 UChar expected[sizeof(expectedChars)]; 1109 1110 u_uastrncpy(pattern, patChars, UPRV_LENGTHOF(pattern)); 1111 u_uastrncpy(expected, expectedChars, UPRV_LENGTHOF(expected)); 1112 1113 u_formatMessage("en_US", pattern, 6, result, UPRV_LENGTHOF(result), &status, arg); 1114 if (U_FAILURE(status)) { 1115 log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status)); 1116 } 1117 if (u_strcmp(result, expected) != 0) { 1118 log_err("u_formatMessage didn't return expected result\n"); 1119 } 1120 } 1121 1122 static void TestMessageWithUnusedArgNumber() { 1123 UErrorCode errorCode = U_ZERO_ERROR; 1124 U_STRING_DECL(pattern, "abc {1} def", 11); 1125 UChar x[2] = { 0x78, 0 }; // "x" 1126 UChar y[2] = { 0x79, 0 }; // "y" 1127 U_STRING_DECL(expected, "abc y def", 9); 1128 UChar result[20]; 1129 int32_t length; 1130 1131 U_STRING_INIT(pattern, "abc {1} def", 11); 1132 U_STRING_INIT(expected, "abc y def", 9); 1133 length = u_formatMessage("en", pattern, -1, result, UPRV_LENGTHOF(result), &errorCode, x, y); 1134 if (U_FAILURE(errorCode) || length != u_strlen(expected) || u_strcmp(result, expected) != 0) { 1135 log_err("u_formatMessage(pattern with only {1}, 2 args) failed: result length %d, UErrorCode %s \n", 1136 (int)length, u_errorName(errorCode)); 1137 } 1138 } 1139 1140 static void TestErrorChaining(void) { 1141 UErrorCode status = U_USELESS_COLLATOR_ERROR; 1142 1143 umsg_open(NULL, 0, NULL, NULL, &status); 1144 umsg_applyPattern(NULL, NULL, 0, NULL, &status); 1145 umsg_toPattern(NULL, NULL, 0, &status); 1146 umsg_clone(NULL, &status); 1147 umsg_format(NULL, NULL, 0, &status); 1148 umsg_parse(NULL, NULL, 0, NULL, &status); 1149 umsg_close(NULL); 1150 1151 /* All of this code should have done nothing. */ 1152 if (status != U_USELESS_COLLATOR_ERROR) { 1153 log_err("Status got changed to %s\n", u_errorName(status)); 1154 } 1155 1156 status = U_ZERO_ERROR; 1157 umsg_open(NULL, 0, NULL, NULL, &status); 1158 if (status != U_ILLEGAL_ARGUMENT_ERROR) { 1159 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); 1160 } 1161 status = U_ZERO_ERROR; 1162 umsg_applyPattern(NULL, NULL, 0, NULL, &status); 1163 if (status != U_ILLEGAL_ARGUMENT_ERROR) { 1164 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); 1165 } 1166 status = U_ZERO_ERROR; 1167 umsg_toPattern(NULL, NULL, 0, &status); 1168 if (status != U_ILLEGAL_ARGUMENT_ERROR) { 1169 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); 1170 } 1171 status = U_ZERO_ERROR; 1172 umsg_clone(NULL, &status); 1173 if (status != U_ILLEGAL_ARGUMENT_ERROR) { 1174 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status)); 1175 } 1176 } 1177 1178 void addMsgForTest(TestNode** root); 1179 1180 void addMsgForTest(TestNode** root) 1181 { 1182 addTest(root, &OpenMessageFormatTest, "tsformat/cmsgtst/OpenMessageFormatTest"); 1183 addTest(root, &MessageFormatTest, "tsformat/cmsgtst/MessageFormatTest"); 1184 addTest(root, &TestSampleMessageFormat, "tsformat/cmsgtst/TestSampleMessageFormat"); 1185 addTest(root, &TestSampleFormatAndParse, "tsformat/cmsgtst/TestSampleFormatAndParse"); 1186 addTest(root, &TestSampleFormatAndParseWithError, "tsformat/cmsgtst/TestSampleFormatAndParseWithError"); 1187 addTest(root, &TestNewFormatAndParseAPI, "tsformat/cmsgtst/TestNewFormatAndParseAPI"); 1188 addTest(root, &TestMsgFormatChoice, "tsformat/cmsgtst/TestMsgFormatChoice"); 1189 addTest(root, &TestParseMessage, "tsformat/cmsgtst/TestParseMessage"); 1190 addTest(root, &TestMessageFormatWithValist, "tsformat/cmsgtst/TestMessageFormatWithValist"); 1191 addTest(root, &TestParseMessageWithValist, "tsformat/cmsgtst/TestParseMessageWithValist"); 1192 addTest(root, &TestJ904, "tsformat/cmsgtst/TestJ904"); 1193 addTest(root, &MessageLength, "tsformat/cmsgtst/MessageLength"); 1194 addTest(root, &TestMessageWithUnusedArgNumber, "tsformat/cmsgtst/TestMessageWithUnusedArgNumber"); 1195 addTest(root, &TestErrorChaining, "tsformat/cmsgtst/TestErrorChaining"); 1196 addTest(root, &TestMsgFormatSelect, "tsformat/cmsgtst/TestMsgFormatSelect"); 1197 } 1198 1199 #endif /* #if !UCONFIG_NO_FORMATTING */ 1200