1 /********************************************************************
3 * Copyright (c) 1997-2008, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *********************************************************************************
15 /* C API TEST FOR MESSAGE FORMAT */
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
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"
33 static const char* const txt_testCasePatterns
[] = {
34 "Quotes '', '{', a {0,number,integer} '{'0}",
35 "Quotes '', '{', a {0,number,integer} '{'0}",
36 "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}",
37 "'{'2,time,full}, for {1, number }, {0,number,integer} is {2,time,full} and full date is {2,date,full}",
38 "'{'1,number,percent} for {0,number,integer} is {1,number,percent}",
41 static const char* const txt_testResultStrings
[] = {
42 "Quotes ', {, a 1 {0}",
43 "Quotes ', {, a 1 {0}",
44 "You deposited 1 times an amount of $3,456.00 on 1/12/70",
45 "{2,time,full}, for 3,456, 1 is 5:46:40 AM PT and full date is Monday, January 12, 1970",
46 "{1,number,percent} for 1 is 345,600%"
49 const int32_t cnt_testCases
= 5;
50 static UChar
* testCasePatterns
[5];
52 static UChar
* testResultStrings
[5];
54 static UBool strings_initialized
= FALSE
;
56 /* function used to create the test patterns for testing Message formatting */
57 static void InitStrings( void )
60 if (strings_initialized
)
63 for (i
=0; i
< cnt_testCases
; i
++ ) {
64 uint32_t strSize
= (uint32_t)strlen(txt_testCasePatterns
[i
]) + 1;
65 testCasePatterns
[i
]=(UChar
*)malloc(sizeof(UChar
) * strSize
);
66 u_uastrncpy(testCasePatterns
[i
], txt_testCasePatterns
[i
], strSize
);
68 for (i
=0; i
< cnt_testCases
; i
++ ) {
69 uint32_t strSize
= (uint32_t)strlen(txt_testResultStrings
[i
]) + 1;
70 testResultStrings
[i
] = (UChar
*)malloc(sizeof(UChar
) * strSize
);
71 u_uastrncpy(testResultStrings
[i
], txt_testResultStrings
[i
], strSize
);
74 strings_initialized
= TRUE
;
77 static void FreeStrings( void )
80 if (!strings_initialized
)
83 for (i
=0; i
< cnt_testCases
; i
++ ) {
84 free(testCasePatterns
[i
]);
86 for (i
=0; i
< cnt_testCases
; i
++ ) {
87 free(testResultStrings
[i
]);
89 strings_initialized
= FALSE
;
92 /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */
93 static UBool
returnsNullForType(int firstParam
, ...) {
96 va_start(marker
, firstParam
);
97 isNULL
= (UBool
)(va_arg(marker
, void*) == NULL
);
102 /* Test u_formatMessage() with various test patterns() */
103 static void MessageFormatTest( void )
107 int32_t resultLengthOut
,resultlength
,i
, patternlength
;
108 UErrorCode status
= U_ZERO_ERROR
;
109 UDate d1
=1000000000.0;
111 ctest_setTimeZone(NULL
, &status
);
113 str
=(UChar
*)malloc(sizeof(UChar
) * 7);
114 u_uastrncpy(str
, "MyDisk", 7);
116 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
117 log_verbose("Testing u_formatMessage()\n");
119 for (i
= 0; i
< cnt_testCases
; i
++) {
121 patternlength
=u_strlen(testCasePatterns
[i
]);
122 resultLengthOut
=u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
123 &status
, 1, 3456.00, d1
);
124 if(status
== U_BUFFER_OVERFLOW_ERROR
)
127 resultlength
=resultLengthOut
+1;
128 result
=(UChar
*)realloc(result
,sizeof(UChar
) * resultlength
);
129 u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
130 &status
, 1, 3456.00, d1
);
132 if(U_FAILURE(status
)){
133 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, myErrorName(status
) );
136 if(u_strcmp(result
, testResultStrings
[i
])==0){
137 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
140 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
141 austrdup(result
), austrdup(testResultStrings
[i
]) );
149 for (i
= 0; i
< cnt_testCases
; i
++) {
150 UParseError parseError
;
152 patternlength
=u_strlen(testCasePatterns
[i
]);
154 resultLengthOut
=u_formatMessageWithError( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
155 &parseError
,&status
, 1, 3456.00, d1
);
156 if(status
== U_BUFFER_OVERFLOW_ERROR
)
159 resultlength
=resultLengthOut
+1;
160 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
161 u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
162 &status
, 1, 3456.00, d1
);
164 if(U_FAILURE(status
)){
165 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, myErrorName(status
) );
168 if(u_strcmp(result
, testResultStrings
[i
])==0){
169 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
172 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
173 austrdup(result
), austrdup(testResultStrings
[i
]) );
180 UErrorCode ec
= U_ZERO_ERROR
;
181 int32_t patternLength
= u_strlen(testCasePatterns
[0]);
183 UMessageFormat formatter
= umsg_open(testCasePatterns
[0],patternLength
,"en_US",NULL
,&ec
);
186 log_err("umsg_open() failed for testCasePattens[%d].\n",i
);
189 for(i
= 0;i
<cnt_testCases
; i
++){
190 UParseError parseError
;
191 int32_t resultLength
=0,count
=0;
197 patternLength
= u_strlen(testCasePatterns
[i
]);
199 umsg_applyPattern(formatter
,testCasePatterns
[i
],patternLength
,&parseError
,&ec
);
201 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i
);
205 resultLength
= umsg_format(formatter
,result
,resultLength
,&ec
,1,3456.00,d1
);
206 if(ec
==U_BUFFER_OVERFLOW_ERROR
){
208 result
= (UChar
*) malloc(U_SIZEOF_UCHAR
*resultLength
+2);
209 resultLength
= umsg_format(formatter
,result
,resultLength
+2,&ec
,1,3456.00,d1
);
211 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, u_errorName(status
) );
216 if(u_strcmp(result
, testResultStrings
[i
])==0){
217 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
220 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
221 austrdup(result
), austrdup(testResultStrings
[i
]) );
224 if (returnsNullForType(1, (double)2.0)) {
225 /* HP/UX and possibly other platforms don't properly check for this case.
226 We pass in a UDate, but the function expects a UDate *. When va_arg is used,
227 most compilers will return NULL, but HP-UX won't do that and will return 2
228 in this case. This is a platform dependent test.
230 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
232 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,one
,two
,d2
);
233 if(ec
!=U_ILLEGAL_ARGUMENT_ERROR
){
234 log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec
));
240 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i
);
243 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,&one
,&two
,&d2
);
245 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec
));
249 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec
),i
);
252 umsg_close(formatter
);
256 ctest_resetTimeZone();
260 /*test u_formatMessage() with sample patterns */
261 static void TestSampleMessageFormat(void)
265 UChar pattern
[100], expected
[100];
266 int32_t resultLengthOut
, resultlength
;
267 UDate d
= 837039928046.0;
268 UErrorCode status
= U_ZERO_ERROR
;
270 ctest_setTimeZone(NULL
, &status
);
272 str
=(UChar
*)malloc(sizeof(UChar
) * 15);
273 u_uastrcpy(str
, "abc");
275 u_uastrcpy(pattern
, "There are {0} files on {1,date}");
276 u_uastrcpy(expected
, "There are abc files on Jul 10, 1996");
277 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
278 log_verbose("\nTesting a sample for Message format test#1\n");
280 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
281 if(status
==U_BUFFER_OVERFLOW_ERROR
)
284 resultlength
=resultLengthOut
+1;
285 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
286 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
288 if(U_FAILURE(status
)){
289 log_err("Error: failure in message format on test#1: %s\n", myErrorName(status
));
291 else if(u_strcmp(result
, expected
)==0)
292 log_verbose("PASS: MessagFormat successful on test#1\n");
294 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
295 austrdup(result
), austrdup(expected
) );
299 log_verbose("\nTesting message format with another pattern test#2\n");
300 u_uastrcpy(pattern
, "The disk \"{0}\" contains {1,number,integer} file(s)");
301 u_uastrcpy(expected
, "The disk \"MyDisk\" contains 23 file(s)");
302 u_uastrcpy(str
, "MyDisk");
304 resultLengthOut
=u_formatMessage( "en_US",
312 if(status
==U_BUFFER_OVERFLOW_ERROR
)
315 resultlength
=resultLengthOut
+1;
316 result
=(UChar
*)realloc(result
, sizeof(UChar
) * (resultlength
+1));
317 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 23);
319 if(U_FAILURE(status
)){
320 log_err("Error: failure in message format on test#2 : %s\n", myErrorName(status
));
322 else if(u_strcmp(result
, expected
)==0)
323 log_verbose("PASS: MessagFormat successful on test#2\n");
325 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
326 austrdup(result
), austrdup(expected
) );
331 log_verbose("\nTesting message format with another pattern test#3\n");
332 u_uastrcpy(pattern
, "You made a {0} of {1,number,currency}");
333 u_uastrcpy(expected
, "You made a deposit of $500.00");
334 u_uastrcpy(str
, "deposit");
336 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, str
, 500.00);
337 if(status
==U_BUFFER_OVERFLOW_ERROR
)
340 resultlength
=resultLengthOut
+1;
341 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
342 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 500.00);
344 if(U_FAILURE(status
)){
345 log_err("Error: failure in message format on test#3 : %s\n", myErrorName(status
));
347 else if(u_strcmp(result
, expected
)==0)
348 log_verbose("PASS: MessagFormat successful on test#3\n");
350 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result
),
351 austrdup(expected
) );
357 ctest_resetTimeZone();
360 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
361 static void TestNewFormatAndParseAPI(void)
364 UChar
*result
, tzID
[4], str
[25];
367 int32_t resultLengthOut
, resultlength
;
371 UErrorCode status
= U_ZERO_ERROR
;
374 UParseError parseError
;
375 UMessageFormat
* fmt
= NULL
;
378 ctest_setTimeZone(NULL
, &status
);
380 log_verbose("Testing format and parse with parse error\n");
382 u_uastrcpy(str
, "disturbance in force");
383 u_uastrcpy(tzID
, "PST");
384 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
385 if(U_FAILURE(status
)){
386 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
389 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
390 d1
=ucal_getMillis(cal
, &status
);
391 if(U_FAILURE(status
)){
392 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
396 log_verbose("\nTesting with pattern test#4");
397 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
398 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
400 fmt
= umsg_open(pattern
,u_strlen(pattern
),"en_US",&parseError
,&status
);
401 if(U_FAILURE(status
)){
402 log_err("error in umsg_open : %s\n", u_errorName(status
) );
405 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
407 resultLengthOut
=umsg_format(fmt
,result
, resultlength
,&status
, d1
, str
, 7);
408 if(status
==U_BUFFER_OVERFLOW_ERROR
)
411 resultlength
=resultLengthOut
+1;
412 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
413 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
416 if(U_FAILURE(status
)){
417 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
419 if(u_strcmp(result
, expected
)==0)
420 log_verbose("PASS: MessagFormat successful on test#4\n");
422 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
423 austrdup(expected
) );
427 /*try to parse this and check*/
428 log_verbose("\nTesting the parse Message test#5\n");
430 umsg_parse(fmt
, result
, u_strlen(result
),&count
,&status
, &d
, ret
, &value
);
431 if(U_FAILURE(status
)){
432 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
434 if(value
!=7 && u_strcmp(str
,ret
)!=0)
435 log_err("FAIL: Error in parseMessage on test#5 \n");
437 log_verbose("PASS: parseMessage successful on test#5\n");
439 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
440 if(U_FAILURE(status
))
442 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
445 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
446 log_verbose("PASS: parseMessage successful test#5\n");
448 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
449 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
458 ctest_resetTimeZone();
461 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
462 static void TestSampleFormatAndParseWithError(void)
465 UChar
*result
, *tzID
, *str
;
469 int32_t resultLengthOut
, resultlength
;
473 UErrorCode status
= U_ZERO_ERROR
;
476 UParseError parseError
;
478 ctest_setTimeZone(NULL
, &status
);
480 log_verbose("Testing format and parse with parse error\n");
482 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
483 u_uastrcpy(str
, "disturbance in force");
484 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
485 u_uastrcpy(tzID
, "PST");
486 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
487 if(U_FAILURE(status
)){
488 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
490 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
491 d1
=ucal_getMillis(cal
, &status
);
492 if(U_FAILURE(status
)){
493 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
496 log_verbose("\nTesting with pattern test#4");
497 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
498 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
500 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
501 resultLengthOut
=u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
502 if(status
==U_BUFFER_OVERFLOW_ERROR
)
505 resultlength
=resultLengthOut
+1;
506 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
507 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
510 if(U_FAILURE(status
)){
511 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
513 else if(u_strcmp(result
, expected
)==0)
514 log_verbose("PASS: MessagFormat successful on test#4\n");
516 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
517 austrdup(expected
) );
521 /*try to parse this and check*/
522 log_verbose("\nTesting the parse Message test#5\n");
524 u_parseMessageWithError("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &parseError
,&status
, &d
, ret
, &value
);
525 if(U_FAILURE(status
)){
526 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
528 else if(value
!=7 && u_strcmp(str
,ret
)!=0)
529 log_err("FAIL: Error in parseMessage on test#5 \n");
531 log_verbose("PASS: parseMessage successful on test#5\n");
533 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
534 if(U_FAILURE(status
))
536 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
539 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
540 log_verbose("PASS: parseMessage successful test#5\n");
542 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
543 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
553 ctest_resetTimeZone();
556 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
557 static void TestSampleFormatAndParse(void)
560 UChar
*result
, *tzID
, *str
;
563 int32_t resultLengthOut
, resultlength
;
567 UErrorCode status
= U_ZERO_ERROR
;
571 ctest_setTimeZone(NULL
, &status
);
573 log_verbose("Testing format and parse\n");
575 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
576 u_uastrcpy(str
, "disturbance in force");
577 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
578 u_uastrcpy(tzID
, "PST");
579 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
580 if(U_FAILURE(status
)){
581 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
583 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
584 d1
=ucal_getMillis(cal
, &status
);
585 if(U_FAILURE(status
)){
586 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
589 log_verbose("\nTesting with pattern test#4");
590 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
591 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
593 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
594 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
595 if(status
==U_BUFFER_OVERFLOW_ERROR
)
598 resultlength
=resultLengthOut
+1;
599 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
600 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
603 if(U_FAILURE(status
)){
604 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
606 else if(u_strcmp(result
, expected
)==0)
607 log_verbose("PASS: MessagFormat successful on test#4\n");
609 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
610 austrdup(expected
) );
614 /*try to parse this and check*/
615 log_verbose("\nTesting the parse Message test#5\n");
617 u_parseMessage("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &status
, &d
, ret
, &value
);
618 if(U_FAILURE(status
)){
619 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
621 else if(value
!=7 && u_strcmp(str
,ret
)!=0)
622 log_err("FAIL: Error in parseMessage on test#5 \n");
624 log_verbose("PASS: parseMessage successful on test#5\n");
626 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
627 if(U_FAILURE(status
))
629 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
632 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
633 log_verbose("PASS: parseMessage successful test#5\n");
635 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
636 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
646 ctest_resetTimeZone();
649 /* test message format with a choice option */
650 static void TestMsgFormatChoice(void)
653 UErrorCode status
= U_ZERO_ERROR
;
657 int32_t resultlength
,resultLengthOut
;
659 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
660 u_uastrcpy(str
, "MyDisk");
661 log_verbose("Testing message format with choice test #6\n:");
662 /*There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.*/
663 u_uastrcpy(pattern
, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
664 u_uastrcpy(expected
, "The disk MyDisk contains 100 files");
666 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 100., str
);
667 if(status
==U_BUFFER_OVERFLOW_ERROR
)
670 resultlength
=resultLengthOut
+1;
671 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
672 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 100., str
);
673 if(u_strcmp(result
, expected
)==0)
674 log_verbose("PASS: MessagFormat successful on test#6\n");
676 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result
),
677 austrdup(expected
) );
681 if(U_FAILURE(status
)){
682 log_err("ERROR: failure in message format on test#6 : %s\n", myErrorName(status
));
685 log_verbose("Testing message format with choice test #7\n:");
686 u_uastrcpy(expected
, "The disk MyDisk contains no files");
688 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 0., str
);
689 if(status
==U_BUFFER_OVERFLOW_ERROR
)
692 resultlength
=resultLengthOut
+1;
693 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
694 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 0., str
);
696 if(u_strcmp(result
, expected
)==0)
697 log_verbose("PASS: MessagFormat successful on test#7\n");
699 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result
),
700 austrdup(expected
) );
704 if(U_FAILURE(status
)){
705 log_err("ERROR: failure in message format on test#7 : %s\n", myErrorName(status
));
708 log_verbose("Testing message format with choice test #8\n:");
709 u_uastrcpy(expected
, "The disk MyDisk contains one file");
711 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 1., str
);
712 if(status
==U_BUFFER_OVERFLOW_ERROR
)
715 resultlength
=resultLengthOut
+1;
716 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
717 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 1., str
);
719 if(u_strcmp(result
, expected
)==0)
720 log_verbose("PASS: MessagFormat successful on test#8\n");
722 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result
),
723 austrdup(expected
) );
728 if(U_FAILURE(status
)){
729 log_err("ERROR: failure in message format on test#8 : %s\n", myErrorName(status
));
736 /*test u_parseMessage() with various test patterns */
737 static void TestParseMessage(void)
741 UErrorCode status
= U_ZERO_ERROR
;
746 log_verbose("\nTesting a sample for parse Message test#9\n");
748 u_uastrcpy(source
, "You deposited an amount of $500.00");
749 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
750 u_uastrcpy(res
,"deposited");
752 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
753 if(U_FAILURE(status
)){
754 log_err("ERROR: failure in parse Message on test#9: %s\n", myErrorName(status
));
756 else if(value
==500.00 && u_strcmp(str
,res
)==0)
757 log_verbose("PASS: parseMessage successful on test#9\n");
759 log_err("FAIL: Error in parseMessage on test#9 \n");
763 log_verbose("\nTesting a sample for parse Message test#10\n");
765 u_uastrcpy(source
, "There are 123 files on MyDisk created");
766 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
767 u_uastrcpy(res
,"MyDisk");
769 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
770 if(U_FAILURE(status
)){
771 log_err("ERROR: failure in parse Message on test#10: %s\n", myErrorName(status
));
773 else if(value
==123.00 && u_strcmp(str
,res
)==0)
774 log_verbose("PASS: parseMessage successful on test#10\n");
776 log_err("FAIL: Error in parseMessage on test#10 \n");
782 static int32_t CallFormatMessage(const char* locale
, UChar
* testCasePattern
, int32_t patternLength
,
783 UChar
* result
, int32_t resultLength
, UErrorCode
*status
, ...)
787 va_start(ap
, status
);
788 len
= u_vformatMessage(locale
, testCasePattern
, patternLength
, result
, resultLength
, ap
, status
);
793 /* Test u_vformatMessage() with various test patterns. */
794 static void TestMessageFormatWithValist( void )
799 int32_t resultLengthOut
,resultlength
,i
, patternlength
;
800 UErrorCode status
= U_ZERO_ERROR
;
801 UDate d1
=1000000000.0;
803 ctest_setTimeZone(NULL
, &status
);
805 str
=(UChar
*)malloc(sizeof(UChar
) * 7);
806 u_uastrcpy(str
, "MyDisk");
808 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
809 log_verbose("Testing u_formatMessage90\n");
811 for (i
= 0; i
< cnt_testCases
; i
++) {
813 patternlength
=u_strlen(testCasePatterns
[i
]);
814 resultLengthOut
=CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
815 &status
, 1, 3456.00, d1
);
816 if(status
== U_BUFFER_OVERFLOW_ERROR
)
819 resultlength
=resultLengthOut
+1;
820 result
=(UChar
*)realloc(result
,sizeof(UChar
) * resultlength
);
821 CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
822 &status
, 1, 3456.00, d1
);
824 if(U_FAILURE(status
)){
825 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, myErrorName(status
) );
827 else if(u_strcmp(result
, testResultStrings
[i
])==0){
828 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
831 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
832 austrdup(result
), austrdup(testResultStrings
[i
]) );
839 ctest_resetTimeZone();
842 static void CallParseMessage(const char* locale
, UChar
* pattern
, int32_t patternLength
,
843 UChar
* source
, int32_t sourceLength
, UErrorCode
*status
, ...)
846 va_start(ap
, status
);
847 u_vparseMessage(locale
, pattern
, patternLength
, source
, sourceLength
, ap
, status
);
851 /*test u_vparseMessage() with various test patterns */
852 static void TestParseMessageWithValist(void)
856 UErrorCode status
= U_ZERO_ERROR
;
861 log_verbose("\nTesting a sample for parse Message test#9\n");
863 u_uastrcpy(source
, "You deposited an amount of $500.00");
864 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
865 u_uastrcpy(res
,"deposited");
867 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
868 if(U_FAILURE(status
)){
869 log_err("ERROR: failure in parse Message on test#9: %s\n", myErrorName(status
));
871 if(value
==500.00 && u_strcmp(str
,res
)==0)
872 log_verbose("PASS: parseMessage successful on test#9\n");
874 log_err("FAIL: Error in parseMessage on test#9 \n");
877 log_verbose("\nTesting a sample for parse Message test#10\n");
879 u_uastrcpy(source
, "There are 123 files on MyDisk created");
880 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
881 u_uastrcpy(res
,"MyDisk");
883 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
884 if(U_FAILURE(status
)){
885 log_err("ERROR: failure in parse Message on test#10: %s\n", myErrorName(status
));
887 else if(value
==123.00 && u_strcmp(str
,res
)==0)
888 log_verbose("PASS: parseMessage successful on test#10\n");
890 log_err("FAIL: Error in parseMessage on test#10 \n");
894 * Regression test for ICU4C Jitterbug 904
896 static void TestJ904(void) {
902 UErrorCode status
= U_ZERO_ERROR
;
903 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
904 const char* EXP
= "Number 0,143, String foo, Date 12:34:56.789";
906 ctest_setTimeZone(NULL
, &status
);
908 u_uastrcpy(string
, "foo");
909 /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use
910 * 12:mm:ss.SSS. Why? So this test generates the same output --
911 * "12:34:56.789" -- regardless of time zone (as long as we aren't
912 * in one of the 30 minute offset zones!). */
913 u_uastrcpy(pattern
, PAT
);
914 length
= u_formatMessage("nl", pattern
, u_strlen(pattern
),
915 result
, 256, &status
,
917 789.0+1000*(56+60*(34+60*12)));
919 u_austrncpy(cresult
, result
, sizeof(cresult
));
921 /* This test passes if it DOESN'T CRASH. However, we test the
922 * output anyway. If the string doesn't match in the date part,
923 * check to see that the machine doesn't have an unusual time zone
924 * offset, that is, one with a non-zero minutes/seconds offset
925 * from GMT -- see above. */
926 if (strcmp(cresult
, EXP
) == 0) {
927 log_verbose("Ok: \"%s\"\n", cresult
);
929 log_err("FAIL: got \"%s\", expected \"%s\"\n", cresult
, EXP
);
932 ctest_resetTimeZone();
935 static void OpenMessageFormatTest(void)
937 UMessageFormat
*f1
, *f2
, *f3
;
941 UParseError parseError
;
942 const char* locale
= "hi_IN";
944 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
946 UErrorCode status
= U_ZERO_ERROR
;
948 u_uastrncpy(pattern
, PAT
, sizeof(pattern
)/sizeof(pattern
[0]));
951 f1
= umsg_open(pattern
,length
,NULL
,NULL
,&status
);
953 if(U_FAILURE(status
))
955 log_err("umsg_open failed with pattern %s. Error: \n", PAT
, u_errorName(status
));
959 /* Test umsg_open with parse error */
960 status
= U_ZERO_ERROR
;
961 f2
= umsg_open(pattern
,length
,NULL
,&parseError
,&status
);
963 if(U_FAILURE(status
))
965 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT
, u_errorName(status
));
969 /* Test umsg_clone */
970 status
= U_ZERO_ERROR
;
971 f3
= umsg_clone(f1
,&status
);
972 if(U_FAILURE(status
))
974 log_err("umsg_clone failed. Error %s \n", u_errorName(status
));
977 /* Test umsg_setLocale */
978 umsg_setLocale(f1
,locale
);
979 /* Test umsg_getLocale */
980 retLoc
= (char*)umsg_getLocale(f1
);
981 if(strcmp(retLoc
,locale
)!=0)
983 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale
, retLoc
);
986 /* Test umsg_applyPattern */
987 status
= U_ZERO_ERROR
;
988 umsg_applyPattern(f1
,pattern
,(int32_t)strlen(PAT
),NULL
,&status
);
989 if(U_FAILURE(status
))
991 log_err("umsg_applyPattern failed. Error %s \n",u_errorName(status
));
994 /* Test umsg_toPattern */
995 umsg_toPattern(f1
,result
,256,&status
);
996 if(U_FAILURE(status
) ){
997 log_err("umsg_toPattern method failed. Error: %s \n",u_errorName(status
));
999 if(u_strcmp(result
,pattern
)!=0){
1000 u_UCharsToChars(result
,cresult
,256);
1001 log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT
,cresult
);
1004 /* umsg_format umsg_parse */
1011 static void MessageLength(void)
1013 UErrorCode status
= U_ZERO_ERROR
;
1014 const char patChars
[] = {"123{0}456{0}"};
1015 const char expectedChars
[] = {"123abc"};
1016 UChar pattern
[sizeof(patChars
)];
1017 UChar arg
[] = {0x61,0x62,0x63,0};
1018 UChar result
[128] = {0};
1019 UChar expected
[sizeof(expectedChars
)];
1021 u_uastrncpy(pattern
, patChars
, sizeof(pattern
)/sizeof(pattern
[0]));
1022 u_uastrncpy(expected
, expectedChars
, sizeof(expected
)/sizeof(expected
[0]));
1024 u_formatMessage("en_US", pattern
, 6, result
, sizeof(result
)/sizeof(result
[0]), &status
, arg
);
1025 if (U_FAILURE(status
)) {
1026 log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status
));
1028 if (u_strcmp(result
, expected
) != 0) {
1029 log_err("u_formatMessage didn't return expected result\n");
1033 static void TestErrorChaining(void) {
1034 UErrorCode status
= U_USELESS_COLLATOR_ERROR
;
1036 umsg_open(NULL
, 0, NULL
, NULL
, &status
);
1037 umsg_applyPattern(NULL
, NULL
, 0, NULL
, &status
);
1038 umsg_toPattern(NULL
, NULL
, 0, &status
);
1039 umsg_clone(NULL
, &status
);
1040 umsg_format(NULL
, NULL
, 0, &status
);
1041 umsg_parse(NULL
, NULL
, 0, NULL
, &status
);
1044 /* All of this code should have done nothing. */
1045 if (status
!= U_USELESS_COLLATOR_ERROR
) {
1046 log_err("Status got changed to %s\n", u_errorName(status
));
1049 status
= U_ZERO_ERROR
;
1050 umsg_open(NULL
, 0, NULL
, NULL
, &status
);
1051 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1052 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1054 status
= U_ZERO_ERROR
;
1055 umsg_applyPattern(NULL
, NULL
, 0, NULL
, &status
);
1056 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1057 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1059 status
= U_ZERO_ERROR
;
1060 umsg_toPattern(NULL
, NULL
, 0, &status
);
1061 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1062 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1064 status
= U_ZERO_ERROR
;
1065 umsg_clone(NULL
, &status
);
1066 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1067 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1071 void addMsgForTest(TestNode
** root
);
1073 void addMsgForTest(TestNode
** root
)
1075 addTest(root
, &OpenMessageFormatTest
, "tsformat/cmsgtst/OpenMessageFormatTest");
1076 addTest(root
, &MessageFormatTest
, "tsformat/cmsgtst/MessageFormatTest");
1077 addTest(root
, &TestSampleMessageFormat
, "tsformat/cmsgtst/TestSampleMessageFormat");
1078 addTest(root
, &TestSampleFormatAndParse
, "tsformat/cmsgtst/TestSampleFormatAndParse");
1079 addTest(root
, &TestSampleFormatAndParseWithError
, "tsformat/cmsgtst/TestSampleFormatAndParseWithError");
1080 addTest(root
, &TestNewFormatAndParseAPI
, "tsformat/cmsgtst/TestNewFormatAndParseAPI");
1081 addTest(root
, &TestMsgFormatChoice
, "tsformat/cmsgtst/TestMsgFormatChoice");
1082 addTest(root
, &TestParseMessage
, "tsformat/cmsgtst/TestParseMessage");
1083 addTest(root
, &TestMessageFormatWithValist
, "tsformat/cmsgtst/TestMessageFormatWithValist");
1084 addTest(root
, &TestParseMessageWithValist
, "tsformat/cmsgtst/TestParseMessageWithValist");
1085 addTest(root
, &TestJ904
, "tsformat/cmsgtst/TestJ904");
1086 addTest(root
, &MessageLength
, "tsformat/cmsgtst/MessageLength");
1087 addTest(root
, &TestErrorChaining
, "tsformat/cmsgtst/TestErrorChaining");
1090 #endif /* #if !UCONFIG_NO_FORMATTING */