1 /********************************************************************
3 * Copyright (c) 1997-2016, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************
9 * Modification History:
11 * Madhu Katragadda Creation
12 ********************************************************************/
13 /* C API TEST FOR MESSAGE FORMAT */
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_FORMATTING
22 #include "unicode/uloc.h"
23 #include "unicode/umsg.h"
24 #include "unicode/udat.h"
25 #include "unicode/umsg.h"
26 #include "unicode/ustring.h"
32 static const char* const txt_testCasePatterns
[] = {
33 "Quotes '', '{', a {0,number,integer} '{'0}",
34 "Quotes '', '{', a {0,number,integer} '{'0}",
35 "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}",
36 "'{'2,time,full}, for {1, number }, {0,number,integer} is {2,time,full} and full date is {2,date,full}",
37 "'{'1,number,percent} for {0,number,integer} is {1,number,percent}",
40 static const char* const txt_testResultStrings
[] = {
41 "Quotes ', {, a 1 {0}",
42 "Quotes ', {, a 1 {0}",
43 "You deposited 1 times an amount of $3,456.00 on 1/12/70",
44 "{2,time,full}, for 3,456, 1 is 5:46:40 AM Pacific Standard Time and full date is Monday, January 12, 1970",
45 "{1,number,percent} for 1 is 345,600%"
48 const int32_t cnt_testCases
= 5;
49 static UChar
* testCasePatterns
[5];
51 static UChar
* testResultStrings
[5];
53 static UBool strings_initialized
= FALSE
;
55 /* function used to create the test patterns for testing Message formatting */
56 static void InitStrings( void )
59 if (strings_initialized
)
62 for (i
=0; i
< cnt_testCases
; i
++ ) {
63 uint32_t strSize
= (uint32_t)strlen(txt_testCasePatterns
[i
]) + 1;
64 testCasePatterns
[i
]=(UChar
*)malloc(sizeof(UChar
) * strSize
);
65 u_uastrncpy(testCasePatterns
[i
], txt_testCasePatterns
[i
], strSize
);
67 for (i
=0; i
< cnt_testCases
; i
++ ) {
68 uint32_t strSize
= (uint32_t)strlen(txt_testResultStrings
[i
]) + 1;
69 testResultStrings
[i
] = (UChar
*)malloc(sizeof(UChar
) * strSize
);
70 u_uastrncpy(testResultStrings
[i
], txt_testResultStrings
[i
], strSize
);
73 strings_initialized
= TRUE
;
76 static void FreeStrings( void )
79 if (!strings_initialized
)
82 for (i
=0; i
< cnt_testCases
; i
++ ) {
83 free(testCasePatterns
[i
]);
85 for (i
=0; i
< cnt_testCases
; i
++ ) {
86 free(testResultStrings
[i
]);
88 strings_initialized
= FALSE
;
91 #if (U_PLATFORM == U_PF_LINUX) /* add platforms here .. */
92 /* Keep the #if above in sync with the one below that has the same "add platforms here .." comment. */
94 /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */
95 static UBool
returnsNullForType(int firstParam
, ...) {
98 va_start(marker
, firstParam
);
99 isNULL
= (UBool
)(va_arg(marker
, void*) == NULL
);
105 /* Test u_formatMessage() with various test patterns() */
106 static void MessageFormatTest( void )
110 int32_t resultLengthOut
,resultlength
,i
, patternlength
;
111 UErrorCode status
= U_ZERO_ERROR
;
112 UDate d1
=1000000000.0;
114 ctest_setTimeZone(NULL
, &status
);
116 str
=(UChar
*)malloc(sizeof(UChar
) * 7);
117 u_uastrncpy(str
, "MyDisk", 7);
119 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
120 log_verbose("Testing u_formatMessage()\n");
122 for (i
= 0; i
< cnt_testCases
; i
++) {
124 patternlength
=u_strlen(testCasePatterns
[i
]);
125 resultLengthOut
=u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
126 &status
, 1, 3456.00, d1
);
127 if(status
== U_BUFFER_OVERFLOW_ERROR
)
130 resultlength
=resultLengthOut
+1;
131 result
=(UChar
*)realloc(result
,sizeof(UChar
) * resultlength
);
132 u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
133 &status
, 1, 3456.00, d1
);
135 if(U_FAILURE(status
)){
136 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i
, myErrorName(status
) );
139 if(u_strcmp(result
, testResultStrings
[i
])==0){
140 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
143 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
144 austrdup(result
), austrdup(testResultStrings
[i
]) );
152 for (i
= 0; i
< cnt_testCases
; i
++) {
153 UParseError parseError
;
155 patternlength
=u_strlen(testCasePatterns
[i
]);
157 resultLengthOut
=u_formatMessageWithError( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
158 &parseError
,&status
, 1, 3456.00, d1
);
159 if(status
== U_BUFFER_OVERFLOW_ERROR
)
162 resultlength
=resultLengthOut
+1;
163 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
164 u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
165 &status
, 1, 3456.00, d1
);
167 if(U_FAILURE(status
)){
168 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i
, myErrorName(status
) );
171 if(u_strcmp(result
, testResultStrings
[i
])==0){
172 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
175 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
176 austrdup(result
), austrdup(testResultStrings
[i
]) );
183 UErrorCode ec
= U_ZERO_ERROR
;
184 int32_t patternLength
= u_strlen(testCasePatterns
[0]);
186 UMessageFormat formatter
= umsg_open(testCasePatterns
[0],patternLength
,"en_US",NULL
,&ec
);
189 log_data_err("umsg_open() failed for testCasePattens[%d]. -> %s (Are you missing data?)\n",i
, u_errorName(ec
));
192 for(i
= 0;i
<cnt_testCases
; i
++){
193 UParseError parseError
;
194 int32_t resultLength
=0,count
=0;
200 patternLength
= u_strlen(testCasePatterns
[i
]);
202 umsg_applyPattern(formatter
,testCasePatterns
[i
],patternLength
,&parseError
,&ec
);
204 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i
);
208 resultLength
= umsg_format(formatter
,result
,resultLength
,&ec
,1,3456.00,d1
);
209 if(ec
==U_BUFFER_OVERFLOW_ERROR
){
211 result
= (UChar
*) malloc(U_SIZEOF_UCHAR
*resultLength
+2);
212 resultLength
= umsg_format(formatter
,result
,resultLength
+2,&ec
,1,3456.00,d1
);
214 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, u_errorName(status
) );
219 if(u_strcmp(result
, testResultStrings
[i
])==0){
220 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
223 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
224 austrdup(result
), austrdup(testResultStrings
[i
]) );
227 #if (U_PLATFORM == U_PF_LINUX) /* add platforms here .. */
228 log_verbose("Skipping potentially crashing test for mismatched varargs.\n");
230 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__
);
232 if (returnsNullForType(1, (double)2.0)) {
233 /* HP/UX and possibly other platforms don't properly check for this case.
234 We pass in a UDate, but the function expects a UDate *. When va_arg is used,
235 most compilers will return NULL, but HP-UX won't do that and will return 2
236 in this case. This is a platform dependent test. It crashes on some systems.
238 If you get a crash here, see the definition of returnsNullForType.
240 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
242 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,one
,two
,d2
);
243 if(ec
!=U_ILLEGAL_ARGUMENT_ERROR
){
244 log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec
));
250 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i
);
254 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,&one
,&two
,&d2
);
256 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec
));
260 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec
),i
);
263 umsg_close(formatter
);
267 ctest_resetTimeZone();
271 /*test u_formatMessage() with sample patterns */
272 static void TestSampleMessageFormat(void)
276 UChar pattern
[100], expected
[100];
277 int32_t resultLengthOut
, resultlength
;
278 UDate d
= 837039928046.0;
279 UErrorCode status
= U_ZERO_ERROR
;
281 ctest_setTimeZone(NULL
, &status
);
283 str
=(UChar
*)malloc(sizeof(UChar
) * 15);
284 u_uastrcpy(str
, "abc");
286 u_uastrcpy(pattern
, "There are {0} files on {1,date}");
287 u_uastrcpy(expected
, "There are abc files on Jul 10, 1996");
288 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
289 log_verbose("\nTesting a sample for Message format test#1\n");
291 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
292 if(status
==U_BUFFER_OVERFLOW_ERROR
)
295 resultlength
=resultLengthOut
+1;
296 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
297 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
299 if(U_FAILURE(status
)){
300 log_data_err("Error: failure in message format on test#1: %s (Are you missing data?)\n", myErrorName(status
));
302 else if(u_strcmp(result
, expected
)==0)
303 log_verbose("PASS: MessagFormat successful on test#1\n");
305 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
306 austrdup(result
), austrdup(expected
) );
310 log_verbose("\nTesting message format with another pattern test#2\n");
311 u_uastrcpy(pattern
, "The disk \"{0}\" contains {1,number,integer} file(s)");
312 u_uastrcpy(expected
, "The disk \"MyDisk\" contains 23 file(s)");
313 u_uastrcpy(str
, "MyDisk");
315 resultLengthOut
=u_formatMessage( "en_US",
323 if(status
==U_BUFFER_OVERFLOW_ERROR
)
326 resultlength
=resultLengthOut
+1;
327 result
=(UChar
*)realloc(result
, sizeof(UChar
) * (resultlength
+1));
328 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 23);
330 if(U_FAILURE(status
)){
331 log_data_err("Error: failure in message format on test#2 : %s (Are you missing data?)\n", myErrorName(status
));
333 else if(u_strcmp(result
, expected
)==0)
334 log_verbose("PASS: MessagFormat successful on test#2\n");
336 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
337 austrdup(result
), austrdup(expected
) );
342 log_verbose("\nTesting message format with another pattern test#3\n");
343 u_uastrcpy(pattern
, "You made a {0} of {1,number,currency}");
344 u_uastrcpy(expected
, "You made a deposit of $500.00");
345 u_uastrcpy(str
, "deposit");
347 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, str
, 500.00);
348 if(status
==U_BUFFER_OVERFLOW_ERROR
)
351 resultlength
=resultLengthOut
+1;
352 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
353 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 500.00);
355 if(U_FAILURE(status
)){
356 log_data_err("Error: failure in message format on test#3 : %s (Are you missing data?)\n", myErrorName(status
));
358 else if(u_strcmp(result
, expected
)==0)
359 log_verbose("PASS: MessagFormat successful on test#3\n");
361 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result
),
362 austrdup(expected
) );
368 ctest_resetTimeZone();
371 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
372 static void TestNewFormatAndParseAPI(void)
375 UChar
*result
, tzID
[4], str
[25];
378 int32_t resultLengthOut
, resultlength
;
382 UErrorCode status
= U_ZERO_ERROR
;
385 UParseError parseError
;
386 UMessageFormat
* fmt
= NULL
;
389 ctest_setTimeZone(NULL
, &status
);
391 log_verbose("Testing format and parse with parse error\n");
393 u_uastrcpy(str
, "disturbance in force");
394 u_uastrcpy(tzID
, "PST");
395 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
396 if(U_FAILURE(status
)){
397 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status
) );
400 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
401 d1
=ucal_getMillis(cal
, &status
);
402 if(U_FAILURE(status
)){
403 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
407 log_verbose("\nTesting with pattern test#4");
408 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
409 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
411 fmt
= umsg_open(pattern
,u_strlen(pattern
),"en_US",&parseError
,&status
);
412 if(U_FAILURE(status
)){
413 log_data_err("error in umsg_open : %s (Are you missing data?)\n", u_errorName(status
) );
416 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
418 resultLengthOut
=umsg_format(fmt
,result
, resultlength
,&status
, d1
, str
, 7);
419 if(status
==U_BUFFER_OVERFLOW_ERROR
)
422 resultlength
=resultLengthOut
+1;
423 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
424 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
427 if(U_FAILURE(status
)){
428 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
430 if(u_strcmp(result
, expected
)==0)
431 log_verbose("PASS: MessagFormat successful on test#4\n");
433 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
434 austrdup(expected
) );
438 /*try to parse this and check*/
439 log_verbose("\nTesting the parse Message test#5\n");
441 umsg_parse(fmt
, result
, u_strlen(result
),&count
,&status
, &d
, ret
, &value
);
442 if(U_FAILURE(status
)){
443 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
445 if(value
!=7 && u_strcmp(str
,ret
)!=0)
446 log_err("FAIL: Error in parseMessage on test#5 \n");
448 log_verbose("PASS: parseMessage successful on test#5\n");
450 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
451 if(U_FAILURE(status
))
453 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
456 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
457 log_verbose("PASS: parseMessage successful test#5\n");
459 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
460 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
469 ctest_resetTimeZone();
472 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
473 static void TestSampleFormatAndParseWithError(void)
476 UChar
*result
, *tzID
, *str
;
480 int32_t resultLengthOut
, resultlength
;
484 UErrorCode status
= U_ZERO_ERROR
;
487 UParseError parseError
;
489 ctest_setTimeZone(NULL
, &status
);
491 log_verbose("Testing format and parse with parse error\n");
493 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
494 u_uastrcpy(str
, "disturbance in force");
495 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
496 u_uastrcpy(tzID
, "PST");
497 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
498 if(U_FAILURE(status
)){
499 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status
) );
501 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
502 d1
=ucal_getMillis(cal
, &status
);
503 if(U_FAILURE(status
)){
504 log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status
) );
507 log_verbose("\nTesting with pattern test#4");
508 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
509 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
511 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
512 resultLengthOut
=u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
513 if(status
==U_BUFFER_OVERFLOW_ERROR
)
516 resultlength
=resultLengthOut
+1;
517 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
518 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
521 if(U_FAILURE(status
)){
522 log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status
));
524 else if(u_strcmp(result
, expected
)==0)
525 log_verbose("PASS: MessagFormat successful on test#4\n");
527 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
528 austrdup(expected
) );
532 /*try to parse this and check*/
533 log_verbose("\nTesting the parse Message test#5\n");
535 u_parseMessageWithError("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &parseError
,&status
, &d
, ret
, &value
);
536 if(U_FAILURE(status
)){
537 log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status
));
539 else if(value
!=7 && u_strcmp(str
,ret
)!=0)
540 log_err("FAIL: Error in parseMessage on test#5 \n");
542 log_verbose("PASS: parseMessage successful on test#5\n");
544 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
545 if(U_FAILURE(status
))
547 log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status
));
550 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
551 log_verbose("PASS: parseMessage successful test#5\n");
553 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
554 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
564 ctest_resetTimeZone();
567 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
568 static void TestSampleFormatAndParse(void)
571 UChar
*result
, *tzID
, *str
;
574 int32_t resultLengthOut
, resultlength
;
578 UErrorCode status
= U_ZERO_ERROR
;
582 ctest_setTimeZone(NULL
, &status
);
584 log_verbose("Testing format and parse\n");
586 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
587 u_uastrcpy(str
, "disturbance in force");
588 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
589 u_uastrcpy(tzID
, "PST");
590 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
591 if(U_FAILURE(status
)){
592 log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status
) );
594 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
595 d1
=ucal_getMillis(cal
, &status
);
596 if(U_FAILURE(status
)){
597 log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status
) );
600 log_verbose("\nTesting with pattern test#4");
601 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
602 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
604 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
605 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
606 if(status
==U_BUFFER_OVERFLOW_ERROR
)
609 resultlength
=resultLengthOut
+1;
610 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
611 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
614 if(U_FAILURE(status
)){
615 log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status
));
617 else if(u_strcmp(result
, expected
)==0)
618 log_verbose("PASS: MessagFormat successful on test#4\n");
620 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
621 austrdup(expected
) );
625 /*try to parse this and check*/
626 log_verbose("\nTesting the parse Message test#5\n");
628 u_parseMessage("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &status
, &d
, ret
, &value
);
629 if(U_FAILURE(status
)){
630 log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status
));
632 else if(value
!=7 && u_strcmp(str
,ret
)!=0)
633 log_err("FAIL: Error in parseMessage on test#5 \n");
635 log_verbose("PASS: parseMessage successful on test#5\n");
637 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
638 if(U_FAILURE(status
))
640 log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status
));
643 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
644 log_verbose("PASS: parseMessage successful test#5\n");
646 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
647 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
657 ctest_resetTimeZone();
660 /* Test message format with a Select option */
661 static void TestMsgFormatSelect(void)
665 UErrorCode status
= U_ZERO_ERROR
;
669 int32_t resultlength
,resultLengthOut
;
671 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
672 u_uastrcpy(str
, "Kirti");
673 str1
=(UChar
*)malloc(sizeof(UChar
) * 25);
674 u_uastrcpy(str1
, "female");
675 log_verbose("Testing message format with Select test #1\n:");
676 u_uastrcpy(pattern
, "{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
677 u_uastrcpy(expected
, "Kirti est all\\u00E9e \\u00E0 Paris.");
679 resultLengthOut
=u_formatMessage( "fr", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, str
, str1
);
680 if(status
==U_BUFFER_OVERFLOW_ERROR
)
683 resultlength
=resultLengthOut
+1;
684 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
685 u_formatMessage( "fr", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, str1
);
686 if(u_strcmp(result
, expected
)==0)
687 log_verbose("PASS: MessagFormat successful on Select test#1\n");
689 log_err("FAIL: Error in MessageFormat on Select test#1\n GOT %s EXPECTED %s\n", austrdup(result
),
690 austrdup(expected
) );
694 if(U_FAILURE(status
)){
695 log_data_err("ERROR: failure in message format on Select test#1 : %s \n", myErrorName(status
));
700 /*Test a nested pattern*/
701 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
702 u_uastrcpy(str
, "Noname");
703 str1
=(UChar
*)malloc(sizeof(UChar
) * 25);
704 u_uastrcpy(str1
, "other");
705 log_verbose("Testing message format with Select test #2\n:");
706 u_uastrcpy(pattern
, "{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
707 u_uastrcpy(expected
, "Noname est all\\u00E9 \\u00E0 Paris.");
709 resultLengthOut
=u_formatMessage( "fr", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, str
, str1
,6);
710 if(status
==U_BUFFER_OVERFLOW_ERROR
)
713 resultlength
=resultLengthOut
+1;
714 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
715 u_formatMessage( "fr", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, str1
);
716 if(u_strcmp(result
, expected
)==0)
717 log_verbose("PASS: MessagFormat successful on Select test#2\n");
719 log_err("FAIL: Error in MessageFormat on Select test#2\n GOT %s EXPECTED %s\n", austrdup(result
),
720 austrdup(expected
) );
724 if(U_FAILURE(status
)){
725 log_data_err("ERROR: failure in message format on Select test#2 : %s \n", myErrorName(status
));
731 /* test message format with a choice option */
732 static void TestMsgFormatChoice(void)
735 UErrorCode status
= U_ZERO_ERROR
;
739 int32_t resultlength
,resultLengthOut
;
741 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
742 u_uastrcpy(str
, "MyDisk");
743 log_verbose("Testing message format with choice test #6\n:");
745 * Before ICU 4.8, umsg_xxx() did not detect conflicting argument types,
746 * and this pattern had {0,number,integer} as the inner argument.
747 * The choice argument has kDouble type while {0,number,integer} has kLong (int32_t).
748 * ICU 4.8 and above detects this as an error.
749 * We changed this pattern to work as intended.
751 u_uastrcpy(pattern
, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number} files}");
752 u_uastrcpy(expected
, "The disk MyDisk contains 100 files");
754 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 100., str
);
755 if(status
==U_BUFFER_OVERFLOW_ERROR
)
758 resultlength
=resultLengthOut
+1;
759 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
760 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 100., str
);
761 if(u_strcmp(result
, expected
)==0)
762 log_verbose("PASS: MessagFormat successful on test#6\n");
764 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result
),
765 austrdup(expected
) );
769 if(U_FAILURE(status
)){
770 log_data_err("ERROR: failure in message format on test#6 : %s (Are you missing data?)\n", myErrorName(status
));
773 log_verbose("Testing message format with choice test #7\n:");
774 u_uastrcpy(expected
, "The disk MyDisk contains no files");
776 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 0., str
);
777 if(status
==U_BUFFER_OVERFLOW_ERROR
)
780 resultlength
=resultLengthOut
+1;
781 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
782 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 0., str
);
784 if(u_strcmp(result
, expected
)==0)
785 log_verbose("PASS: MessagFormat successful on test#7\n");
787 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result
),
788 austrdup(expected
) );
792 if(U_FAILURE(status
)){
793 log_data_err("ERROR: failure in message format on test#7 : %s (Are you missing data?)\n", myErrorName(status
));
796 log_verbose("Testing message format with choice test #8\n:");
797 u_uastrcpy(expected
, "The disk MyDisk contains one file");
799 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 1., str
);
800 if(status
==U_BUFFER_OVERFLOW_ERROR
)
803 resultlength
=resultLengthOut
+1;
804 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
805 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 1., str
);
807 if(u_strcmp(result
, expected
)==0)
808 log_verbose("PASS: MessagFormat successful on test#8\n");
810 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result
),
811 austrdup(expected
) );
816 if(U_FAILURE(status
)){
817 log_data_err("ERROR: failure in message format on test#8 : %s (Are you missing data?)\n", myErrorName(status
));
824 /*test u_parseMessage() with various test patterns */
825 static void TestParseMessage(void)
829 UErrorCode status
= U_ZERO_ERROR
;
834 log_verbose("\nTesting a sample for parse Message test#9\n");
836 u_uastrcpy(source
, "You deposited an amount of $500.00");
837 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
838 u_uastrcpy(res
,"deposited");
840 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
841 if(U_FAILURE(status
)){
842 log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status
));
844 else if(value
==500.00 && u_strcmp(str
,res
)==0)
845 log_verbose("PASS: parseMessage successful on test#9\n");
847 log_err("FAIL: Error in parseMessage on test#9 \n");
851 log_verbose("\nTesting a sample for parse Message test#10\n");
853 u_uastrcpy(source
, "There are 123 files on MyDisk created");
854 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
855 u_uastrcpy(res
,"MyDisk");
857 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
858 if(U_FAILURE(status
)){
859 log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status
));
861 else if(value
==123.00 && u_strcmp(str
,res
)==0)
862 log_verbose("PASS: parseMessage successful on test#10\n");
864 log_err("FAIL: Error in parseMessage on test#10 \n");
867 static int32_t CallFormatMessage(const char* locale
, UChar
* testCasePattern
, int32_t patternLength
,
868 UChar
* result
, int32_t resultLength
, UErrorCode
*status
, ...)
872 va_start(ap
, status
);
873 len
= u_vformatMessage(locale
, testCasePattern
, patternLength
, result
, resultLength
, ap
, status
);
878 /* Test u_vformatMessage() with various test patterns. */
879 static void TestMessageFormatWithValist( void )
884 int32_t resultLengthOut
,resultlength
,i
, patternlength
;
885 UErrorCode status
= U_ZERO_ERROR
;
886 UDate d1
=1000000000.0;
888 ctest_setTimeZone(NULL
, &status
);
890 str
=(UChar
*)malloc(sizeof(UChar
) * 7);
891 u_uastrcpy(str
, "MyDisk");
893 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
894 log_verbose("Testing u_formatMessage90\n");
896 for (i
= 0; i
< cnt_testCases
; i
++) {
898 patternlength
=u_strlen(testCasePatterns
[i
]);
899 resultLengthOut
=CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
900 &status
, 1, 3456.00, d1
);
901 if(status
== U_BUFFER_OVERFLOW_ERROR
)
904 resultlength
=resultLengthOut
+1;
905 result
=(UChar
*)realloc(result
,sizeof(UChar
) * resultlength
);
906 CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
907 &status
, 1, 3456.00, d1
);
909 if(U_FAILURE(status
)){
910 log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i
, myErrorName(status
) );
912 else if(u_strcmp(result
, testResultStrings
[i
])==0){
913 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
916 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
917 austrdup(result
), austrdup(testResultStrings
[i
]) );
924 ctest_resetTimeZone();
927 static void CallParseMessage(const char* locale
, UChar
* pattern
, int32_t patternLength
,
928 UChar
* source
, int32_t sourceLength
, UErrorCode
*status
, ...)
931 va_start(ap
, status
);
932 u_vparseMessage(locale
, pattern
, patternLength
, source
, sourceLength
, ap
, status
);
936 /*test u_vparseMessage() with various test patterns */
937 static void TestParseMessageWithValist(void)
941 UErrorCode status
= U_ZERO_ERROR
;
946 log_verbose("\nTesting a sample for parse Message test#9\n");
948 u_uastrcpy(source
, "You deposited an amount of $500.00");
949 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
950 u_uastrcpy(res
,"deposited");
952 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
953 if(U_FAILURE(status
)){
954 log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status
));
956 else if(value
==500.00 && u_strcmp(str
,res
)==0)
957 log_verbose("PASS: parseMessage successful on test#9\n");
959 log_err("FAIL: Error in parseMessage on test#9\n");
962 log_verbose("\nTesting a sample for parse Message test#10\n");
964 u_uastrcpy(source
, "There are 123 files on MyDisk created");
965 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
966 u_uastrcpy(res
,"MyDisk");
968 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
969 if(U_FAILURE(status
)){
970 log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status
));
972 else if(value
==123.00 && u_strcmp(str
,res
)==0)
973 log_verbose("PASS: parseMessage successful on test#10\n");
975 log_err("FAIL: Error in parseMessage on test#10 \n");
979 * Regression test for ICU4C Jitterbug 904
981 static void TestJ904(void) {
987 UErrorCode status
= U_ZERO_ERROR
;
988 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
989 const char* EXP
= "Number 0,143, String foo, Date 12:34:56.789";
991 ctest_setTimeZone(NULL
, &status
);
993 u_uastrcpy(string
, "foo");
994 /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use
995 * 12:mm:ss.SSS. Why? So this test generates the same output --
996 * "12:34:56.789" -- regardless of time zone (as long as we aren't
997 * in one of the 30 minute offset zones!). */
998 u_uastrcpy(pattern
, PAT
);
999 length
= u_formatMessage("nl", pattern
, u_strlen(pattern
),
1000 result
, 256, &status
,
1002 789.0+1000*(56+60*(34+60*12)));
1003 (void)length
; /* Suppress set but not used warning. */
1005 u_austrncpy(cresult
, result
, sizeof(cresult
));
1007 /* This test passes if it DOESN'T CRASH. However, we test the
1008 * output anyway. If the string doesn't match in the date part,
1009 * check to see that the machine doesn't have an unusual time zone
1010 * offset, that is, one with a non-zero minutes/seconds offset
1011 * from GMT -- see above. */
1012 if (strcmp(cresult
, EXP
) == 0) {
1013 log_verbose("Ok: \"%s\"\n", cresult
);
1015 log_data_err("FAIL: got \"%s\", expected \"%s\" -> %s (Are you missing data?)\n", cresult
, EXP
, u_errorName(status
));
1018 ctest_resetTimeZone();
1021 static void OpenMessageFormatTest(void)
1023 UMessageFormat
*f1
, *f2
, *f3
;
1027 UParseError parseError
;
1028 const char* locale
= "hi_IN";
1030 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
1032 UErrorCode status
= U_ZERO_ERROR
;
1034 u_uastrncpy(pattern
, PAT
, UPRV_LENGTHOF(pattern
));
1036 /* Test umsg_open */
1037 f1
= umsg_open(pattern
,length
,NULL
,NULL
,&status
);
1039 if(U_FAILURE(status
))
1041 log_err("umsg_open failed with pattern %s. Error: \n", PAT
, u_errorName(status
));
1045 /* Test umsg_open with parse error */
1046 status
= U_ZERO_ERROR
;
1047 f2
= umsg_open(pattern
,length
,NULL
,&parseError
,&status
);
1049 if(U_FAILURE(status
))
1051 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT
, u_errorName(status
));
1055 /* Test umsg_clone */
1056 status
= U_ZERO_ERROR
;
1057 f3
= umsg_clone(f1
,&status
);
1058 if(U_FAILURE(status
))
1060 log_err("umsg_clone failed. Error %s \n", u_errorName(status
));
1063 /* Test umsg_setLocale */
1064 umsg_setLocale(f1
,locale
);
1065 /* Test umsg_getLocale */
1066 retLoc
= (char*)umsg_getLocale(f1
);
1067 if(strcmp(retLoc
,locale
)!=0)
1069 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale
, retLoc
);
1072 /* Test umsg_applyPattern */
1073 status
= U_ZERO_ERROR
;
1074 umsg_applyPattern(f1
,pattern
,(int32_t)strlen(PAT
),NULL
,&status
);
1075 if(U_FAILURE(status
))
1077 log_data_err("umsg_applyPattern failed. Error %s (Are you missing data?)\n",u_errorName(status
));
1080 /* Test umsg_toPattern */
1081 umsg_toPattern(f1
,result
,256,&status
);
1082 if(U_FAILURE(status
) ){
1083 log_data_err("umsg_toPattern method failed. Error: %s (Are you missing data?)\n",u_errorName(status
));
1085 if(u_strcmp(result
,pattern
)!=0){
1086 u_UCharsToChars(result
,cresult
,256);
1087 log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT
,cresult
);
1090 /* umsg_format umsg_parse */
1097 static void MessageLength(void)
1099 UErrorCode status
= U_ZERO_ERROR
;
1100 const char patChars
[] = {"123{0}456{0}"};
1101 const char expectedChars
[] = {"123abc"};
1102 UChar pattern
[sizeof(patChars
)];
1103 UChar arg
[] = {0x61,0x62,0x63,0};
1104 UChar result
[128] = {0};
1105 UChar expected
[sizeof(expectedChars
)];
1107 u_uastrncpy(pattern
, patChars
, UPRV_LENGTHOF(pattern
));
1108 u_uastrncpy(expected
, expectedChars
, UPRV_LENGTHOF(expected
));
1110 u_formatMessage("en_US", pattern
, 6, result
, UPRV_LENGTHOF(result
), &status
, arg
);
1111 if (U_FAILURE(status
)) {
1112 log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status
));
1114 if (u_strcmp(result
, expected
) != 0) {
1115 log_err("u_formatMessage didn't return expected result\n");
1119 static void TestMessageWithUnusedArgNumber() {
1120 UErrorCode errorCode
= U_ZERO_ERROR
;
1121 U_STRING_DECL(pattern
, "abc {1} def", 11);
1122 UChar x
[2] = { 0x78, 0 }; // "x"
1123 UChar y
[2] = { 0x79, 0 }; // "y"
1124 U_STRING_DECL(expected
, "abc y def", 9);
1128 U_STRING_INIT(pattern
, "abc {1} def", 11);
1129 U_STRING_INIT(expected
, "abc y def", 9);
1130 length
= u_formatMessage("en", pattern
, -1, result
, UPRV_LENGTHOF(result
), &errorCode
, x
, y
);
1131 if (U_FAILURE(errorCode
) || length
!= u_strlen(expected
) || u_strcmp(result
, expected
) != 0) {
1132 log_err("u_formatMessage(pattern with only {1}, 2 args) failed: result length %d, UErrorCode %s \n",
1133 (int)length
, u_errorName(errorCode
));
1137 static void TestErrorChaining(void) {
1138 UErrorCode status
= U_USELESS_COLLATOR_ERROR
;
1140 umsg_open(NULL
, 0, NULL
, NULL
, &status
);
1141 umsg_applyPattern(NULL
, NULL
, 0, NULL
, &status
);
1142 umsg_toPattern(NULL
, NULL
, 0, &status
);
1143 umsg_clone(NULL
, &status
);
1144 umsg_format(NULL
, NULL
, 0, &status
);
1145 umsg_parse(NULL
, NULL
, 0, NULL
, &status
);
1148 /* All of this code should have done nothing. */
1149 if (status
!= U_USELESS_COLLATOR_ERROR
) {
1150 log_err("Status got changed to %s\n", u_errorName(status
));
1153 status
= U_ZERO_ERROR
;
1154 umsg_open(NULL
, 0, NULL
, NULL
, &status
);
1155 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1156 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1158 status
= U_ZERO_ERROR
;
1159 umsg_applyPattern(NULL
, NULL
, 0, NULL
, &status
);
1160 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1161 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1163 status
= U_ZERO_ERROR
;
1164 umsg_toPattern(NULL
, NULL
, 0, &status
);
1165 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1166 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1168 status
= U_ZERO_ERROR
;
1169 umsg_clone(NULL
, &status
);
1170 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
) {
1171 log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status
));
1175 void addMsgForTest(TestNode
** root
);
1177 void addMsgForTest(TestNode
** root
)
1179 addTest(root
, &OpenMessageFormatTest
, "tsformat/cmsgtst/OpenMessageFormatTest");
1180 addTest(root
, &MessageFormatTest
, "tsformat/cmsgtst/MessageFormatTest");
1181 addTest(root
, &TestSampleMessageFormat
, "tsformat/cmsgtst/TestSampleMessageFormat");
1182 addTest(root
, &TestSampleFormatAndParse
, "tsformat/cmsgtst/TestSampleFormatAndParse");
1183 addTest(root
, &TestSampleFormatAndParseWithError
, "tsformat/cmsgtst/TestSampleFormatAndParseWithError");
1184 addTest(root
, &TestNewFormatAndParseAPI
, "tsformat/cmsgtst/TestNewFormatAndParseAPI");
1185 addTest(root
, &TestMsgFormatChoice
, "tsformat/cmsgtst/TestMsgFormatChoice");
1186 addTest(root
, &TestParseMessage
, "tsformat/cmsgtst/TestParseMessage");
1187 addTest(root
, &TestMessageFormatWithValist
, "tsformat/cmsgtst/TestMessageFormatWithValist");
1188 addTest(root
, &TestParseMessageWithValist
, "tsformat/cmsgtst/TestParseMessageWithValist");
1189 addTest(root
, &TestJ904
, "tsformat/cmsgtst/TestJ904");
1190 addTest(root
, &MessageLength
, "tsformat/cmsgtst/MessageLength");
1191 addTest(root
, &TestMessageWithUnusedArgNumber
, "tsformat/cmsgtst/TestMessageWithUnusedArgNumber");
1192 addTest(root
, &TestErrorChaining
, "tsformat/cmsgtst/TestErrorChaining");
1193 addTest(root
, &TestMsgFormatSelect
, "tsformat/cmsgtst/TestMsgFormatSelect");
1196 #endif /* #if !UCONFIG_NO_FORMATTING */