1 /********************************************************************
3 * Copyright (c) 1997-2006, 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
) );
135 if(u_strcmp(result
, testResultStrings
[i
])==0){
136 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
139 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
140 austrdup(result
), austrdup(testResultStrings
[i
]) );
148 for (i
= 0; i
< cnt_testCases
; i
++) {
149 UParseError parseError
;
151 patternlength
=u_strlen(testCasePatterns
[i
]);
153 resultLengthOut
=u_formatMessageWithError( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
154 &parseError
,&status
, 1, 3456.00, d1
);
155 if(status
== U_BUFFER_OVERFLOW_ERROR
)
158 resultlength
=resultLengthOut
+1;
159 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
160 u_formatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
161 &status
, 1, 3456.00, d1
);
163 if(U_FAILURE(status
)){
164 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, myErrorName(status
) );
167 if(u_strcmp(result
, testResultStrings
[i
])==0){
168 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
171 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
172 austrdup(result
), austrdup(testResultStrings
[i
]) );
179 UErrorCode ec
= U_ZERO_ERROR
;
180 int32_t patternLength
= u_strlen(testCasePatterns
[0]);
182 UMessageFormat formatter
= umsg_open(testCasePatterns
[0],patternLength
,"en_US",NULL
,&ec
);
185 log_err("umsg_open() failed for testCasePattens[%d].\n",i
);
188 for(i
= 0;i
<cnt_testCases
; i
++){
189 UParseError parseError
;
190 int32_t resultLength
=0,count
=0;
196 patternLength
= u_strlen(testCasePatterns
[i
]);
198 umsg_applyPattern(formatter
,testCasePatterns
[i
],patternLength
,&parseError
,&ec
);
200 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i
);
204 resultLength
= umsg_format(formatter
,result
,resultLength
,&ec
,1,3456.00,d1
);
205 if(ec
==U_BUFFER_OVERFLOW_ERROR
){
207 result
= (UChar
*) malloc(U_SIZEOF_UCHAR
*resultLength
+2);
208 resultLength
= umsg_format(formatter
,result
,resultLength
+2,&ec
,1,3456.00,d1
);
210 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, u_errorName(status
) );
215 if(u_strcmp(result
, testResultStrings
[i
])==0){
216 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
219 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
220 austrdup(result
), austrdup(testResultStrings
[i
]) );
223 if (returnsNullForType(1, (double)2.0)) {
224 /* HP/UX and possibly other platforms don't properly check for this case.
225 We pass in a UDate, but the function expects a UDate *. When va_arg is used,
226 most compilers will return NULL, but HP-UX won't do that and will return 2
227 in this case. This is a platform dependent test.
229 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
231 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,one
,two
,d2
);
232 if(ec
!=U_ILLEGAL_ARGUMENT_ERROR
){
233 log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec
));
239 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i
);
242 umsg_parse(formatter
,result
,resultLength
,&count
,&ec
,&one
,&two
,&d2
);
244 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec
));
248 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec
),i
);
251 umsg_close(formatter
);
255 ctest_resetTimeZone();
259 /*test u_formatMessage() with sample patterns */
260 static void TestSampleMessageFormat(void)
264 UChar pattern
[100], expected
[100];
265 int32_t resultLengthOut
, resultlength
;
266 UDate d
= 837039928046.0;
267 UErrorCode status
= U_ZERO_ERROR
;
269 ctest_setTimeZone(NULL
, &status
);
271 str
=(UChar
*)malloc(sizeof(UChar
) * 15);
272 u_uastrcpy(str
, "abc");
274 u_uastrcpy(pattern
, "There are {0} files on {1,date}");
275 u_uastrcpy(expected
, "There are abc files on Jul 10, 1996");
276 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
277 log_verbose("\nTesting a sample for Message format test#1\n");
279 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
280 if(status
==U_BUFFER_OVERFLOW_ERROR
)
283 resultlength
=resultLengthOut
+1;
284 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
285 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, d
);
287 if(U_FAILURE(status
)){
288 log_err("Error: failure in message format on test#1: %s\n", myErrorName(status
));
290 if(u_strcmp(result
, expected
)==0)
291 log_verbose("PASS: MessagFormat successful on test#1\n");
293 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
294 austrdup(result
), austrdup(expected
) );
298 log_verbose("\nTesting message format with another pattern test#2\n");
299 u_uastrcpy(pattern
, "The disk \"{0}\" contains {1,number,integer} file(s)");
300 u_uastrcpy(expected
, "The disk \"MyDisk\" contains 23 file(s)");
301 u_uastrcpy(str
, "MyDisk");
303 resultLengthOut
=u_formatMessage( "en_US",
311 if(status
==U_BUFFER_OVERFLOW_ERROR
)
314 resultlength
=resultLengthOut
+1;
315 result
=(UChar
*)realloc(result
, sizeof(UChar
) * (resultlength
+1));
316 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 23);
318 if(U_FAILURE(status
)){
319 log_err("Error: failure in message format on test#2 : %s\n", myErrorName(status
));
321 if(u_strcmp(result
, expected
)==0)
322 log_verbose("PASS: MessagFormat successful on test#2\n");
324 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
325 austrdup(result
), austrdup(expected
) );
330 log_verbose("\nTesting message format with another pattern test#3\n");
331 u_uastrcpy(pattern
, "You made a {0} of {1,number,currency}");
332 u_uastrcpy(expected
, "You made a deposit of $500.00");
333 u_uastrcpy(str
, "deposit");
335 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, str
, 500.00);
336 if(status
==U_BUFFER_OVERFLOW_ERROR
)
339 resultlength
=resultLengthOut
+1;
340 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
341 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, str
, 500.00);
343 if(U_FAILURE(status
)){
344 log_err("Error: failure in message format on test#3 : %s\n", myErrorName(status
));
346 if(u_strcmp(result
, expected
)==0)
347 log_verbose("PASS: MessagFormat successful on test#3\n");
349 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result
),
350 austrdup(expected
) );
356 ctest_resetTimeZone();
359 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
360 static void TestNewFormatAndParseAPI(void)
363 UChar
*result
, *tzID
, *str
;
366 int32_t resultLengthOut
, resultlength
;
370 UErrorCode status
= U_ZERO_ERROR
;
373 UParseError parseError
;
374 UMessageFormat
* fmt
= NULL
;
377 ctest_setTimeZone(NULL
, &status
);
379 log_verbose("Testing format and parse with parse error\n");
381 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
382 u_uastrcpy(str
, "disturbance in force");
383 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
384 u_uastrcpy(tzID
, "PST");
385 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
386 if(U_FAILURE(status
)){
387 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
390 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
391 d1
=ucal_getMillis(cal
, &status
);
392 if(U_FAILURE(status
)){
393 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
397 log_verbose("\nTesting with pattern test#4");
398 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
399 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
401 fmt
= umsg_open(pattern
,u_strlen(pattern
),"en_US",&parseError
,&status
);
402 if(U_FAILURE(status
)){
403 log_err("error in umsg_open : %s\n", u_errorName(status
) );
406 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
408 resultLengthOut
=umsg_format(fmt
,result
, resultlength
,&status
, d1
, str
, 7);
409 if(status
==U_BUFFER_OVERFLOW_ERROR
)
412 resultlength
=resultLengthOut
+1;
413 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
414 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
417 if(U_FAILURE(status
)){
418 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
420 if(u_strcmp(result
, expected
)==0)
421 log_verbose("PASS: MessagFormat successful on test#4\n");
423 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
424 austrdup(expected
) );
428 /*try to parse this and check*/
429 log_verbose("\nTesting the parse Message test#5\n");
431 umsg_parse(fmt
, result
, u_strlen(result
),&count
,&status
, &d
, ret
, &value
);
432 if(U_FAILURE(status
)){
433 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
435 if(value
!=7 && u_strcmp(str
,ret
)!=0)
436 log_err("FAIL: Error in parseMessage on test#5 \n");
438 log_verbose("PASS: parseMessage successful on test#5\n");
440 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
441 if(U_FAILURE(status
))
443 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
446 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
447 log_verbose("PASS: parseMessage successful test#5\n");
449 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
450 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
461 ctest_resetTimeZone();
464 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
465 static void TestSampleFormatAndParseWithError(void)
468 UChar
*result
, *tzID
, *str
;
472 int32_t resultLengthOut
, resultlength
;
476 UErrorCode status
= U_ZERO_ERROR
;
479 UParseError parseError
;
481 ctest_setTimeZone(NULL
, &status
);
483 log_verbose("Testing format and parse with parse error\n");
485 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
486 u_uastrcpy(str
, "disturbance in force");
487 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
488 u_uastrcpy(tzID
, "PST");
489 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
490 if(U_FAILURE(status
)){
491 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
493 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
494 d1
=ucal_getMillis(cal
, &status
);
495 if(U_FAILURE(status
)){
496 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
499 log_verbose("\nTesting with pattern test#4");
500 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
501 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
503 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
504 resultLengthOut
=u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
505 if(status
==U_BUFFER_OVERFLOW_ERROR
)
508 resultlength
=resultLengthOut
+1;
509 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
510 u_formatMessageWithError( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
,&parseError
, &status
, d1
, str
, 7);
513 if(U_FAILURE(status
)){
514 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
516 if(u_strcmp(result
, expected
)==0)
517 log_verbose("PASS: MessagFormat successful on test#4\n");
519 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
520 austrdup(expected
) );
524 /*try to parse this and check*/
525 log_verbose("\nTesting the parse Message test#5\n");
527 u_parseMessageWithError("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &parseError
,&status
, &d
, ret
, &value
);
528 if(U_FAILURE(status
)){
529 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
531 if(value
!=7 && u_strcmp(str
,ret
)!=0)
532 log_err("FAIL: Error in parseMessage on test#5 \n");
534 log_verbose("PASS: parseMessage successful on test#5\n");
536 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
537 if(U_FAILURE(status
))
539 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
542 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
543 log_verbose("PASS: parseMessage successful test#5\n");
545 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
546 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
556 ctest_resetTimeZone();
559 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
560 static void TestSampleFormatAndParse(void)
563 UChar
*result
, *tzID
, *str
;
566 int32_t resultLengthOut
, resultlength
;
570 UErrorCode status
= U_ZERO_ERROR
;
574 ctest_setTimeZone(NULL
, &status
);
576 log_verbose("Testing format and parse\n");
578 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
579 u_uastrcpy(str
, "disturbance in force");
580 tzID
=(UChar
*)malloc(sizeof(UChar
) * 4);
581 u_uastrcpy(tzID
, "PST");
582 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_TRADITIONAL
, &status
);
583 if(U_FAILURE(status
)){
584 log_err("error in ucal_open caldef : %s\n", myErrorName(status
) );
586 ucal_setDateTime(cal
, 1999, UCAL_MARCH
, 18, 0, 0, 0, &status
);
587 d1
=ucal_getMillis(cal
, &status
);
588 if(U_FAILURE(status
)){
589 log_err("Error: failure in get millis: %s\n", myErrorName(status
) );
592 log_verbose("\nTesting with pattern test#4");
593 u_uastrcpy(pattern
, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
594 u_uastrcpy(expected
, "On March 18, 1999, there was a disturbance in force on planet 7");
596 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
597 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
598 if(status
==U_BUFFER_OVERFLOW_ERROR
)
601 resultlength
=resultLengthOut
+1;
602 result
=(UChar
*)realloc(result
, sizeof(UChar
) * resultlength
);
603 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, d1
, str
, 7);
606 if(U_FAILURE(status
)){
607 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status
));
609 if(u_strcmp(result
, expected
)==0)
610 log_verbose("PASS: MessagFormat successful on test#4\n");
612 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result
),
613 austrdup(expected
) );
617 /*try to parse this and check*/
618 log_verbose("\nTesting the parse Message test#5\n");
620 u_parseMessage("en_US", pattern
, u_strlen(pattern
), result
, u_strlen(result
), &status
, &d
, ret
, &value
);
621 if(U_FAILURE(status
)){
622 log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status
));
624 if(value
!=7 && u_strcmp(str
,ret
)!=0)
625 log_err("FAIL: Error in parseMessage on test#5 \n");
627 log_verbose("PASS: parseMessage successful on test#5\n");
629 def1
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,NULL
, NULL
, 0, NULL
,0,&status
);
630 if(U_FAILURE(status
))
632 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status
));
635 if(u_strcmp(myDateFormat(def1
, d
), myDateFormat(def1
, d1
))==0)
636 log_verbose("PASS: parseMessage successful test#5\n");
638 log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
639 austrdup(myDateFormat(def1
,d
)), austrdup(myDateFormat(def1
,d1
)) );
649 ctest_resetTimeZone();
652 /* test message format with a choice option */
653 static void TestMsgFormatChoice(void)
656 UErrorCode status
= U_ZERO_ERROR
;
660 int32_t resultlength
,resultLengthOut
;
662 str
=(UChar
*)malloc(sizeof(UChar
) * 25);
663 u_uastrcpy(str
, "MyDisk");
664 log_verbose("Testing message format with choice test #6\n:");
665 /*There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.*/
666 u_uastrcpy(pattern
, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
667 u_uastrcpy(expected
, "The disk MyDisk contains 100 files");
669 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 100., str
);
670 if(status
==U_BUFFER_OVERFLOW_ERROR
)
673 resultlength
=resultLengthOut
+1;
674 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
675 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 100., str
);
676 if(u_strcmp(result
, expected
)==0)
677 log_verbose("PASS: MessagFormat successful on test#6\n");
679 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result
),
680 austrdup(expected
) );
684 if(U_FAILURE(status
)){
685 log_err("ERROR: failure in message format on test#6 : %s\n", myErrorName(status
));
688 log_verbose("Testing message format with choice test #7\n:");
689 u_uastrcpy(expected
, "The disk MyDisk contains no files");
691 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 0., str
);
692 if(status
==U_BUFFER_OVERFLOW_ERROR
)
695 resultlength
=resultLengthOut
+1;
696 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
697 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 0., str
);
699 if(u_strcmp(result
, expected
)==0)
700 log_verbose("PASS: MessagFormat successful on test#7\n");
702 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result
),
703 austrdup(expected
) );
707 if(U_FAILURE(status
)){
708 log_err("ERROR: failure in message format on test#7 : %s\n", myErrorName(status
));
711 log_verbose("Testing message format with choice test #8\n:");
712 u_uastrcpy(expected
, "The disk MyDisk contains one file");
714 resultLengthOut
=u_formatMessage( "en_US", pattern
, u_strlen(pattern
), NULL
, resultlength
, &status
, 1., str
);
715 if(status
==U_BUFFER_OVERFLOW_ERROR
)
718 resultlength
=resultLengthOut
+1;
719 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
720 u_formatMessage( "en_US", pattern
, u_strlen(pattern
), result
, resultlength
, &status
, 1., str
);
722 if(u_strcmp(result
, expected
)==0)
723 log_verbose("PASS: MessagFormat successful on test#8\n");
725 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result
),
726 austrdup(expected
) );
731 if(U_FAILURE(status
)){
732 log_err("ERROR: failure in message format on test#8 : %s\n", myErrorName(status
));
739 /*test u_parseMessage() with various test patterns */
740 static void TestParseMessage(void)
744 UErrorCode status
= U_ZERO_ERROR
;
749 log_verbose("\nTesting a sample for parse Message test#9\n");
751 u_uastrcpy(source
, "You deposited an amount of $500.00");
752 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
753 u_uastrcpy(res
,"deposited");
755 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
756 if(U_FAILURE(status
)){
757 log_err("ERROR: failure in parse Message on test#9: %s\n", myErrorName(status
));
759 if(value
==500.00 && u_strcmp(str
,res
)==0)
760 log_verbose("PASS: parseMessage successful on test#9\n");
762 log_err("FAIL: Error in parseMessage on test#9 \n");
766 log_verbose("\nTesting a sample for parse Message test#10\n");
768 u_uastrcpy(source
, "There are 123 files on MyDisk created");
769 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
770 u_uastrcpy(res
,"MyDisk");
772 u_parseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
773 if(U_FAILURE(status
)){
774 log_err("ERROR: failure in parse Message on test#10: %s\n", myErrorName(status
));
776 if(value
==123.00 && u_strcmp(str
,res
)==0)
777 log_verbose("PASS: parseMessage successful on test#10\n");
779 log_err("FAIL: Error in parseMessage on test#10 \n");
785 static int32_t CallFormatMessage(const char* locale
, UChar
* testCasePattern
, int32_t patternLength
,
786 UChar
* result
, int32_t resultLength
, UErrorCode
*status
, ...)
790 va_start(ap
, status
);
791 len
= u_vformatMessage(locale
, testCasePattern
, patternLength
, result
, resultLength
, ap
, status
);
796 /* Test u_vformatMessage() with various test patterns. */
797 static void TestMessageFormatWithValist( void )
802 int32_t resultLengthOut
,resultlength
,i
, patternlength
;
803 UErrorCode status
= U_ZERO_ERROR
;
804 UDate d1
=1000000000.0;
806 ctest_setTimeZone(NULL
, &status
);
808 str
=(UChar
*)malloc(sizeof(UChar
) * 7);
809 u_uastrcpy(str
, "MyDisk");
811 result
=(UChar
*)malloc(sizeof(UChar
) * 1);
812 log_verbose("Testing u_formatMessage90\n");
814 for (i
= 0; i
< cnt_testCases
; i
++) {
816 patternlength
=u_strlen(testCasePatterns
[i
]);
817 resultLengthOut
=CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
818 &status
, 1, 3456.00, d1
);
819 if(status
== U_BUFFER_OVERFLOW_ERROR
)
822 resultlength
=resultLengthOut
+1;
823 result
=(UChar
*)realloc(result
,sizeof(UChar
) * resultlength
);
824 CallFormatMessage( "en_US",testCasePatterns
[i
], patternlength
, result
, resultlength
,
825 &status
, 1, 3456.00, d1
);
827 if(U_FAILURE(status
)){
828 log_err("ERROR: failure in message format on testcase %d: %s\n", i
, myErrorName(status
) );
830 if(u_strcmp(result
, testResultStrings
[i
])==0){
831 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i
);
834 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i
,
835 austrdup(result
), austrdup(testResultStrings
[i
]) );
842 ctest_resetTimeZone();
845 static void CallParseMessage(const char* locale
, UChar
* pattern
, int32_t patternLength
,
846 UChar
* source
, int32_t sourceLength
, UErrorCode
*status
, ...)
849 va_start(ap
, status
);
850 u_vparseMessage(locale
, pattern
, patternLength
, source
, sourceLength
, ap
, status
);
854 /*test u_vparseMessage() with various test patterns */
855 static void TestParseMessageWithValist(void)
859 UErrorCode status
= U_ZERO_ERROR
;
864 log_verbose("\nTesting a sample for parse Message test#9\n");
866 u_uastrcpy(source
, "You deposited an amount of $500.00");
867 u_uastrcpy(pattern
, "You {0} an amount of {1,number,currency}");
868 u_uastrcpy(res
,"deposited");
870 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, str
, &value
);
871 if(U_FAILURE(status
)){
872 log_err("ERROR: failure in parse Message on test#9: %s\n", myErrorName(status
));
874 if(value
==500.00 && u_strcmp(str
,res
)==0)
875 log_verbose("PASS: parseMessage successful on test#9\n");
877 log_err("FAIL: Error in parseMessage on test#9 \n");
880 log_verbose("\nTesting a sample for parse Message test#10\n");
882 u_uastrcpy(source
, "There are 123 files on MyDisk created");
883 u_uastrcpy(pattern
, "There are {0,number,integer} files on {1} created");
884 u_uastrcpy(res
,"MyDisk");
886 CallParseMessage( "en_US", pattern
, u_strlen(pattern
), source
, u_strlen(source
), &status
, &value
, str
);
887 if(U_FAILURE(status
)){
888 log_err("ERROR: failure in parse Message on test#10: %s\n", myErrorName(status
));
890 if(value
==123.00 && u_strcmp(str
,res
)==0)
891 log_verbose("PASS: parseMessage successful on test#10\n");
893 log_err("FAIL: Error in parseMessage on test#10 \n");
897 * Regression test for ICU4C Jitterbug 904
899 static void TestJ904(void) {
905 UErrorCode status
= U_ZERO_ERROR
;
906 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
907 const char* EXP
= "Number 0,143, String foo, Date 12:34:56.789";
909 ctest_setTimeZone(NULL
, &status
);
911 u_uastrcpy(string
, "foo");
912 /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use
913 * 12:mm:ss.SSS. Why? So this test generates the same output --
914 * "12:34:56.789" -- regardless of time zone (as long as we aren't
915 * in one of the 30 minute offset zones!). */
916 u_uastrcpy(pattern
, PAT
);
917 length
= u_formatMessage("nl", pattern
, u_strlen(pattern
),
918 result
, 256, &status
,
920 789.0+1000*(56+60*(34+60*12)));
922 u_austrncpy(cresult
, result
, sizeof(cresult
));
924 /* This test passes if it DOESN'T CRASH. However, we test the
925 * output anyway. If the string doesn't match in the date part,
926 * check to see that the machine doesn't have an unusual time zone
927 * offset, that is, one with a non-zero minutes/seconds offset
928 * from GMT -- see above. */
929 if (strcmp(cresult
, EXP
) == 0) {
930 log_verbose("Ok: \"%s\"\n", cresult
);
932 log_err("FAIL: got \"%s\", expected \"%s\"\n", cresult
, EXP
);
935 ctest_resetTimeZone();
938 static void OpenMessageFormatTest(void)
940 UMessageFormat
*f1
, *f2
, *f3
;
944 UParseError parseError
;
945 const char* locale
= "hi_IN";
947 const char* PAT
= "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
949 UErrorCode status
= U_ZERO_ERROR
;
951 u_uastrncpy(pattern
, PAT
, sizeof(pattern
)/sizeof(pattern
[0]));
954 f1
= umsg_open(pattern
,length
,NULL
,NULL
,&status
);
956 if(U_FAILURE(status
))
958 log_err("umsg_open failed with pattern %s. Error: \n", PAT
, u_errorName(status
));
962 /* Test umsg_open with parse error */
963 status
= U_ZERO_ERROR
;
964 f2
= umsg_open(pattern
,length
,NULL
,&parseError
,&status
);
966 if(U_FAILURE(status
))
968 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT
, u_errorName(status
));
972 /* Test umsg_clone */
973 status
= U_ZERO_ERROR
;
974 f3
= umsg_clone(f1
,&status
);
975 if(U_FAILURE(status
))
977 log_err("umsg_clone failed. Error %s \n", u_errorName(status
));
980 /* Test umsg_setLocale */
981 umsg_setLocale(f1
,locale
);
982 /* Test umsg_getLocale */
983 retLoc
= (char*)umsg_getLocale(f1
);
984 if(strcmp(retLoc
,locale
)!=0)
986 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale
, retLoc
);
989 /* Test umsg_applyPattern */
990 status
= U_ZERO_ERROR
;
991 umsg_applyPattern(f1
,pattern
,(int32_t)strlen(PAT
),NULL
,&status
);
992 if(U_FAILURE(status
))
994 log_err("umsg_applyPattern failed. Error %s \n",u_errorName(status
));
997 /* Test umsg_toPattern */
998 umsg_toPattern(f1
,result
,256,&status
);
999 if(U_FAILURE(status
) ){
1000 log_err("umsg_toPattern method failed. Error: %s \n",u_errorName(status
));
1002 if(u_strcmp(result
,pattern
)!=0){
1003 u_UCharsToChars(result
,cresult
,256);
1004 log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT
,cresult
);
1007 /* umsg_format umsg_parse */
1014 static void MessageLength(void)
1016 UErrorCode status
= U_ZERO_ERROR
;
1017 const char patChars
[] = {"123{0}456{0}"};
1018 const char expectedChars
[] = {"123abc"};
1019 UChar pattern
[sizeof(patChars
)];
1020 UChar arg
[] = {0x61,0x62,0x63,0};
1021 UChar result
[128] = {0};
1022 UChar expected
[sizeof(expectedChars
)];
1024 u_uastrncpy(pattern
, patChars
, sizeof(pattern
)/sizeof(pattern
[0]));
1025 u_uastrncpy(expected
, expectedChars
, sizeof(expected
)/sizeof(expected
[0]));
1027 u_formatMessage("en_US", pattern
, 6, result
, sizeof(result
)/sizeof(result
[0]), &status
, arg
);
1028 if (U_FAILURE(status
)) {
1029 log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status
));
1031 if (u_strcmp(result
, expected
) != 0) {
1032 log_err("u_formatMessage didn't return expected result\n");
1036 static void TestErrorChaining(void) {
1037 UErrorCode status
= U_USELESS_COLLATOR_ERROR
;
1039 umsg_open(NULL
, 0, NULL
, NULL
, &status
);
1040 umsg_applyPattern(NULL
, NULL
, 0, NULL
, &status
);
1041 umsg_clone(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
));
1050 void addMsgForTest(TestNode
** root
);
1052 void addMsgForTest(TestNode
** root
)
1054 addTest(root
, &OpenMessageFormatTest
, "tsformat/cmsgtst/OpenMessageFormatTest");
1055 addTest(root
, &MessageFormatTest
, "tsformat/cmsgtst/MessageFormatTest");
1056 addTest(root
, &TestSampleMessageFormat
, "tsformat/cmsgtst/TestSampleMessageFormat");
1057 addTest(root
, &TestSampleFormatAndParse
, "tsformat/cmsgtst/TestSampleFormatAndParse");
1058 addTest(root
, &TestSampleFormatAndParseWithError
, "tsformat/cmsgtst/TestSampleFormatAndParseWithError");
1059 addTest(root
, &TestNewFormatAndParseAPI
, "tsformat/cmsgtst/TestNewFormatAndParseAPI");
1060 addTest(root
, &TestMsgFormatChoice
, "tsformat/cmsgtst/TestMsgFormatChoice");
1061 addTest(root
, &TestParseMessage
, "tsformat/cmsgtst/TestParseMessage");
1062 addTest(root
, &TestMessageFormatWithValist
, "tsformat/cmsgtst/TestMessageFormatWithValist");
1063 addTest(root
, &TestParseMessageWithValist
, "tsformat/cmsgtst/TestParseMessageWithValist");
1064 addTest(root
, &TestJ904
, "tsformat/cmsgtst/TestJ904");
1065 addTest(root
, &MessageLength
, "tsformat/cmsgtst/MessageLength");
1066 addTest(root
, &TestErrorChaining
, "tsformat/cmsgtst/TestErrorChaining");
1069 #endif /* #if !UCONFIG_NO_FORMATTING */