]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cmsgtst.c
ICU-400.39.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cmsgtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2008, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CMSGTST.C
9 *
10 * Modification History:
11 * Name Description
12 * Madhu Katragadda Creation
13 *********************************************************************************
14 */
15 /* C API TEST FOR MESSAGE FORMAT */
16
17 #include "unicode/utypes.h"
18
19 #if !UCONFIG_NO_FORMATTING
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include "unicode/uloc.h"
25 #include "unicode/umsg.h"
26 #include "unicode/udat.h"
27 #include "unicode/umsg.h"
28 #include "unicode/ustring.h"
29 #include "cintltst.h"
30 #include "cmsgtst.h"
31 #include "cformtst.h"
32
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}",
39 };
40
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%"
47 };
48
49 const int32_t cnt_testCases = 5;
50 static UChar* testCasePatterns[5];
51
52 static UChar* testResultStrings[5];
53
54 static UBool strings_initialized = FALSE;
55
56 /* function used to create the test patterns for testing Message formatting */
57 static void InitStrings( void )
58 {
59 int32_t i;
60 if (strings_initialized)
61 return;
62
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);
67 }
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);
72 }
73
74 strings_initialized = TRUE;
75 }
76
77 static void FreeStrings( void )
78 {
79 int32_t i;
80 if (!strings_initialized)
81 return;
82
83 for (i=0; i < cnt_testCases; i++ ) {
84 free(testCasePatterns[i]);
85 }
86 for (i=0; i < cnt_testCases; i++ ) {
87 free(testResultStrings[i]);
88 }
89 strings_initialized = FALSE;
90 }
91
92 /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */
93 static UBool returnsNullForType(int firstParam, ...) {
94 UBool isNULL;
95 va_list marker;
96 va_start(marker, firstParam);
97 isNULL = (UBool)(va_arg(marker, void*) == NULL);
98 va_end(marker);
99 return isNULL;
100 }
101
102 /* Test u_formatMessage() with various test patterns() */
103 static void MessageFormatTest( void )
104 {
105 UChar *str;
106 UChar* result;
107 int32_t resultLengthOut,resultlength,i, patternlength;
108 UErrorCode status = U_ZERO_ERROR;
109 UDate d1=1000000000.0;
110
111 ctest_setTimeZone(NULL, &status);
112
113 str=(UChar*)malloc(sizeof(UChar) * 7);
114 u_uastrncpy(str, "MyDisk", 7);
115 resultlength=1;
116 result=(UChar*)malloc(sizeof(UChar) * 1);
117 log_verbose("Testing u_formatMessage()\n");
118 InitStrings();
119 for (i = 0; i < cnt_testCases; i++) {
120 status=U_ZERO_ERROR;
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)
125 {
126 status=U_ZERO_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);
131 }
132 if(U_FAILURE(status)){
133 log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) );
134 continue;
135 }
136 if(u_strcmp(result, testResultStrings[i])==0){
137 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
138 }
139 else{
140 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
141 austrdup(result), austrdup(testResultStrings[i]) );
142 }
143 }
144 free(result);
145 result = NULL;
146 free(str);
147 {
148
149 for (i = 0; i < cnt_testCases; i++) {
150 UParseError parseError;
151 status=U_ZERO_ERROR;
152 patternlength=u_strlen(testCasePatterns[i]);
153 resultlength=0;
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)
157 {
158 status=U_ZERO_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);
163 }
164 if(U_FAILURE(status)){
165 log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) );
166 continue;
167 }
168 if(u_strcmp(result, testResultStrings[i])==0){
169 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
170 }
171 else{
172 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
173 austrdup(result), austrdup(testResultStrings[i]) );
174 }
175 free(result);
176 result=NULL;
177 }
178 }
179 {
180 UErrorCode ec = U_ZERO_ERROR;
181 int32_t patternLength = u_strlen(testCasePatterns[0]);
182
183 UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec);
184
185 if(U_FAILURE(ec)){
186 log_err("umsg_open() failed for testCasePattens[%d].\n",i);
187 return;
188 }
189 for(i = 0;i<cnt_testCases; i++){
190 UParseError parseError;
191 int32_t resultLength =0,count=0;
192 int32_t one=0;
193 int32_t two=0;
194 UDate d2=0;
195
196 result=NULL;
197 patternLength = u_strlen(testCasePatterns[i]);
198
199 umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec);
200 if(U_FAILURE(ec)){
201 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i);
202 return;
203 }
204 /* pre-flight */
205 resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1);
206 if(ec==U_BUFFER_OVERFLOW_ERROR){
207 ec=U_ZERO_ERROR;
208 result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2);
209 resultLength = umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1);
210 if(U_FAILURE(ec)){
211 log_err("ERROR: failure in message format on testcase %d: %s\n", i, u_errorName(status) );
212 free(result);
213 return;
214 }
215
216 if(u_strcmp(result, testResultStrings[i])==0){
217 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
218 }
219 else{
220 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
221 austrdup(result), austrdup(testResultStrings[i]) );
222 }
223
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.
229
230 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
231 */
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));
235 }else{
236 ec = U_ZERO_ERROR;
237 }
238 }
239 else {
240 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i);
241 }
242
243 umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2);
244 if(U_FAILURE(ec)){
245 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec));
246 }
247 free(result);
248 }else{
249 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i);
250 }
251 }
252 umsg_close(formatter);
253 }
254 FreeStrings();
255
256 ctest_resetTimeZone();
257 }
258
259
260 /*test u_formatMessage() with sample patterns */
261 static void TestSampleMessageFormat(void)
262 {
263 UChar *str;
264 UChar *result;
265 UChar pattern[100], expected[100];
266 int32_t resultLengthOut, resultlength;
267 UDate d = 837039928046.0;
268 UErrorCode status = U_ZERO_ERROR;
269
270 ctest_setTimeZone(NULL, &status);
271
272 str=(UChar*)malloc(sizeof(UChar) * 15);
273 u_uastrcpy(str, "abc");
274
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");
279 resultlength=1;
280 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
281 if(status==U_BUFFER_OVERFLOW_ERROR)
282 {
283 status=U_ZERO_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);
287 }
288 if(U_FAILURE(status)){
289 log_err("Error: failure in message format on test#1: %s\n", myErrorName(status));
290 }
291 else if(u_strcmp(result, expected)==0)
292 log_verbose("PASS: MessagFormat successful on test#1\n");
293 else{
294 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
295 austrdup(result), austrdup(expected) );
296 }
297
298
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");
303
304 resultLengthOut=u_formatMessage( "en_US",
305 pattern,
306 u_strlen(pattern),
307 result,
308 resultlength,
309 &status,
310 str,
311 235);
312 if(status==U_BUFFER_OVERFLOW_ERROR)
313 {
314 status=U_ZERO_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);
318 }
319 if(U_FAILURE(status)){
320 log_err("Error: failure in message format on test#2 : %s\n", myErrorName(status));
321 }
322 else if(u_strcmp(result, expected)==0)
323 log_verbose("PASS: MessagFormat successful on test#2\n");
324 else{
325 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
326 austrdup(result), austrdup(expected) );
327 }
328
329
330
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");
335 resultlength=0;
336 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00);
337 if(status==U_BUFFER_OVERFLOW_ERROR)
338 {
339 status=U_ZERO_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);
343 }
344 if(U_FAILURE(status)){
345 log_err("Error: failure in message format on test#3 : %s\n", myErrorName(status));
346 }
347 else if(u_strcmp(result, expected)==0)
348 log_verbose("PASS: MessagFormat successful on test#3\n");
349 else{
350 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result),
351 austrdup(expected) );
352 }
353
354 free(result);
355 free(str);
356
357 ctest_resetTimeZone();
358 }
359
360 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
361 static void TestNewFormatAndParseAPI(void)
362 {
363
364 UChar *result, tzID[4], str[25];
365 UChar pattern[100];
366 UChar expected[100];
367 int32_t resultLengthOut, resultlength;
368 UCalendar *cal;
369 UDate d1,d;
370 UDateFormat *def1;
371 UErrorCode status = U_ZERO_ERROR;
372 int32_t value = 0;
373 UChar ret[30];
374 UParseError parseError;
375 UMessageFormat* fmt = NULL;
376 int32_t count=0;
377
378 ctest_setTimeZone(NULL, &status);
379
380 log_verbose("Testing format and parse with parse error\n");
381
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) );
387 return;
388 }
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) );
393 return;
394 }
395
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");
399 resultlength=1;
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) );
403 return;
404 }
405 result=(UChar*)malloc(sizeof(UChar) * resultlength);
406
407 resultLengthOut=umsg_format(fmt ,result, resultlength,&status, d1, str, 7);
408 if(status==U_BUFFER_OVERFLOW_ERROR)
409 {
410 status=U_ZERO_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);
414
415 }
416 if(U_FAILURE(status)){
417 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
418 }
419 if(u_strcmp(result, expected)==0)
420 log_verbose("PASS: MessagFormat successful on test#4\n");
421 else{
422 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
423 austrdup(expected) );
424 }
425
426
427 /*try to parse this and check*/
428 log_verbose("\nTesting the parse Message test#5\n");
429
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));
433 }
434 if(value!=7 && u_strcmp(str,ret)!=0)
435 log_err("FAIL: Error in parseMessage on test#5 \n");
436 else
437 log_verbose("PASS: parseMessage successful on test#5\n");
438
439 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
440 if(U_FAILURE(status))
441 {
442 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
443 }else{
444
445 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
446 log_verbose("PASS: parseMessage successful test#5\n");
447 else{
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)) );
450 }
451 }
452 umsg_close(fmt);
453 udat_close(def1);
454 ucal_close(cal);
455
456 free(result);
457
458 ctest_resetTimeZone();
459 }
460
461 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
462 static void TestSampleFormatAndParseWithError(void)
463 {
464
465 UChar *result, *tzID, *str;
466 UChar pattern[100];
467
468 UChar expected[100];
469 int32_t resultLengthOut, resultlength;
470 UCalendar *cal;
471 UDate d1,d;
472 UDateFormat *def1;
473 UErrorCode status = U_ZERO_ERROR;
474 int32_t value = 0;
475 UChar ret[30];
476 UParseError parseError;
477
478 ctest_setTimeZone(NULL, &status);
479
480 log_verbose("Testing format and parse with parse error\n");
481
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) );
489 }
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) );
494 }
495
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");
499 resultlength=1;
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)
503 {
504 status=U_ZERO_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);
508
509 }
510 if(U_FAILURE(status)){
511 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
512 }
513 else if(u_strcmp(result, expected)==0)
514 log_verbose("PASS: MessagFormat successful on test#4\n");
515 else{
516 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
517 austrdup(expected) );
518 }
519
520
521 /*try to parse this and check*/
522 log_verbose("\nTesting the parse Message test#5\n");
523
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));
527 }
528 else if(value!=7 && u_strcmp(str,ret)!=0)
529 log_err("FAIL: Error in parseMessage on test#5 \n");
530 else
531 log_verbose("PASS: parseMessage successful on test#5\n");
532
533 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
534 if(U_FAILURE(status))
535 {
536 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
537 }else{
538
539 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
540 log_verbose("PASS: parseMessage successful test#5\n");
541 else{
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)) );
544 }
545 }
546 udat_close(def1);
547 ucal_close(cal);
548
549 free(result);
550 free(str);
551 free(tzID);
552
553 ctest_resetTimeZone();
554 }
555
556 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
557 static void TestSampleFormatAndParse(void)
558 {
559
560 UChar *result, *tzID, *str;
561 UChar pattern[100];
562 UChar expected[100];
563 int32_t resultLengthOut, resultlength;
564 UCalendar *cal;
565 UDate d1,d;
566 UDateFormat *def1;
567 UErrorCode status = U_ZERO_ERROR;
568 int32_t value = 0;
569 UChar ret[30];
570
571 ctest_setTimeZone(NULL, &status);
572
573 log_verbose("Testing format and parse\n");
574
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) );
582 }
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) );
587 }
588
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");
592 resultlength=1;
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)
596 {
597 status=U_ZERO_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);
601
602 }
603 if(U_FAILURE(status)){
604 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
605 }
606 else if(u_strcmp(result, expected)==0)
607 log_verbose("PASS: MessagFormat successful on test#4\n");
608 else{
609 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
610 austrdup(expected) );
611 }
612
613
614 /*try to parse this and check*/
615 log_verbose("\nTesting the parse Message test#5\n");
616
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));
620 }
621 else if(value!=7 && u_strcmp(str,ret)!=0)
622 log_err("FAIL: Error in parseMessage on test#5 \n");
623 else
624 log_verbose("PASS: parseMessage successful on test#5\n");
625
626 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
627 if(U_FAILURE(status))
628 {
629 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
630 }else{
631
632 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
633 log_verbose("PASS: parseMessage successful test#5\n");
634 else{
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)) );
637 }
638 }
639 udat_close(def1);
640 ucal_close(cal);
641
642 free(result);
643 free(str);
644 free(tzID);
645
646 ctest_resetTimeZone();
647 }
648
649 /* test message format with a choice option */
650 static void TestMsgFormatChoice(void)
651 {
652 UChar* str;
653 UErrorCode status = U_ZERO_ERROR;
654 UChar *result;
655 UChar pattern[100];
656 UChar expected[100];
657 int32_t resultlength,resultLengthOut;
658
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");
665 resultlength=0;
666 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 100., str);
667 if(status==U_BUFFER_OVERFLOW_ERROR)
668 {
669 status=U_ZERO_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");
675 else{
676 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result),
677 austrdup(expected) );
678 }
679 free(result);
680 }
681 if(U_FAILURE(status)){
682 log_err("ERROR: failure in message format on test#6 : %s\n", myErrorName(status));
683 }
684
685 log_verbose("Testing message format with choice test #7\n:");
686 u_uastrcpy(expected, "The disk MyDisk contains no files");
687 resultlength=0;
688 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 0., str);
689 if(status==U_BUFFER_OVERFLOW_ERROR)
690 {
691 status=U_ZERO_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);
695
696 if(u_strcmp(result, expected)==0)
697 log_verbose("PASS: MessagFormat successful on test#7\n");
698 else{
699 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result),
700 austrdup(expected) );
701 }
702 free(result);
703 }
704 if(U_FAILURE(status)){
705 log_err("ERROR: failure in message format on test#7 : %s\n", myErrorName(status));
706 }
707
708 log_verbose("Testing message format with choice test #8\n:");
709 u_uastrcpy(expected, "The disk MyDisk contains one file");
710 resultlength=0;
711 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 1., str);
712 if(status==U_BUFFER_OVERFLOW_ERROR)
713 {
714 status=U_ZERO_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);
718
719 if(u_strcmp(result, expected)==0)
720 log_verbose("PASS: MessagFormat successful on test#8\n");
721 else{
722 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result),
723 austrdup(expected) );
724 }
725
726 free(result);
727 }
728 if(U_FAILURE(status)){
729 log_err("ERROR: failure in message format on test#8 : %s\n", myErrorName(status));
730 }
731
732 free(str);
733
734 }
735
736 /*test u_parseMessage() with various test patterns */
737 static void TestParseMessage(void)
738 {
739 UChar pattern[100];
740 UChar source[100];
741 UErrorCode status = U_ZERO_ERROR;
742 int32_t value;
743 UChar str[10];
744 UChar res[10];
745
746 log_verbose("\nTesting a sample for parse Message test#9\n");
747
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");
751
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));
755 }
756 else if(value==500.00 && u_strcmp(str,res)==0)
757 log_verbose("PASS: parseMessage successful on test#9\n");
758 else
759 log_err("FAIL: Error in parseMessage on test#9 \n");
760
761
762
763 log_verbose("\nTesting a sample for parse Message test#10\n");
764
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");
768
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));
772 }
773 else if(value==123.00 && u_strcmp(str,res)==0)
774 log_verbose("PASS: parseMessage successful on test#10\n");
775 else
776 log_err("FAIL: Error in parseMessage on test#10 \n");
777
778
779
780 }
781
782 static int32_t CallFormatMessage(const char* locale, UChar* testCasePattern, int32_t patternLength,
783 UChar* result, int32_t resultLength, UErrorCode *status, ...)
784 {
785 int32_t len = 0;
786 va_list ap;
787 va_start(ap, status);
788 len = u_vformatMessage(locale, testCasePattern, patternLength, result, resultLength, ap, status);
789 va_end(ap);
790 return len;
791 }
792
793 /* Test u_vformatMessage() with various test patterns. */
794 static void TestMessageFormatWithValist( void )
795 {
796
797 UChar *str;
798 UChar* result;
799 int32_t resultLengthOut,resultlength,i, patternlength;
800 UErrorCode status = U_ZERO_ERROR;
801 UDate d1=1000000000.0;
802
803 ctest_setTimeZone(NULL, &status);
804
805 str=(UChar*)malloc(sizeof(UChar) * 7);
806 u_uastrcpy(str, "MyDisk");
807 resultlength=1;
808 result=(UChar*)malloc(sizeof(UChar) * 1);
809 log_verbose("Testing u_formatMessage90\n");
810 InitStrings();
811 for (i = 0; i < cnt_testCases; i++) {
812 status=U_ZERO_ERROR;
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)
817 {
818 status=U_ZERO_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);
823 }
824 if(U_FAILURE(status)){
825 log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) );
826 }
827 else if(u_strcmp(result, testResultStrings[i])==0){
828 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
829 }
830 else{
831 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
832 austrdup(result), austrdup(testResultStrings[i]) );
833 }
834 }
835 free(result);
836 free(str);
837 FreeStrings();
838
839 ctest_resetTimeZone();
840 }
841
842 static void CallParseMessage(const char* locale, UChar* pattern, int32_t patternLength,
843 UChar* source, int32_t sourceLength, UErrorCode *status, ...)
844 {
845 va_list ap;
846 va_start(ap, status);
847 u_vparseMessage(locale, pattern, patternLength, source, sourceLength, ap, status);
848 va_end(ap);
849 }
850
851 /*test u_vparseMessage() with various test patterns */
852 static void TestParseMessageWithValist(void)
853 {
854 UChar pattern[100];
855 UChar source[100];
856 UErrorCode status = U_ZERO_ERROR;
857 int32_t value;
858 UChar str[10];
859 UChar res[10];
860
861 log_verbose("\nTesting a sample for parse Message test#9\n");
862
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");
866
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));
870 }
871 if(value==500.00 && u_strcmp(str,res)==0)
872 log_verbose("PASS: parseMessage successful on test#9\n");
873 else
874 log_err("FAIL: Error in parseMessage on test#9 \n");
875
876
877 log_verbose("\nTesting a sample for parse Message test#10\n");
878
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");
882
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));
886 }
887 else if(value==123.00 && u_strcmp(str,res)==0)
888 log_verbose("PASS: parseMessage successful on test#10\n");
889 else
890 log_err("FAIL: Error in parseMessage on test#10 \n");
891 }
892
893 /**
894 * Regression test for ICU4C Jitterbug 904
895 */
896 static void TestJ904(void) {
897 UChar pattern[256];
898 UChar result[256];
899 UChar string[16];
900 char cresult[256];
901 int32_t length;
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";
905
906 ctest_setTimeZone(NULL, &status);
907
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,
916 string, 1/7.0,
917 789.0+1000*(56+60*(34+60*12)));
918
919 u_austrncpy(cresult, result, sizeof(cresult));
920
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);
928 } else {
929 log_err("FAIL: got \"%s\", expected \"%s\"\n", cresult, EXP);
930 }
931
932 ctest_resetTimeZone();
933 }
934
935 static void OpenMessageFormatTest(void)
936 {
937 UMessageFormat *f1, *f2, *f3;
938 UChar pattern[256];
939 UChar result[256];
940 char cresult[256];
941 UParseError parseError;
942 const char* locale = "hi_IN";
943 char* retLoc;
944 const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
945 int32_t length=0;
946 UErrorCode status = U_ZERO_ERROR;
947
948 u_uastrncpy(pattern, PAT, sizeof(pattern)/sizeof(pattern[0]));
949
950 /* Test umsg_open */
951 f1 = umsg_open(pattern,length,NULL,NULL,&status);
952
953 if(U_FAILURE(status))
954 {
955 log_err("umsg_open failed with pattern %s. Error: \n", PAT, u_errorName(status));
956 return;
957 }
958
959 /* Test umsg_open with parse error */
960 status = U_ZERO_ERROR;
961 f2 = umsg_open(pattern,length,NULL,&parseError,&status);
962
963 if(U_FAILURE(status))
964 {
965 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT, u_errorName(status));
966 return;
967 }
968
969 /* Test umsg_clone */
970 status = U_ZERO_ERROR;
971 f3 = umsg_clone(f1,&status);
972 if(U_FAILURE(status))
973 {
974 log_err("umsg_clone failed. Error %s \n", u_errorName(status));
975 }
976
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)
982 {
983 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale, retLoc);
984 }
985
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))
990 {
991 log_err("umsg_applyPattern failed. Error %s \n",u_errorName(status));
992 }
993
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));
998 } else {
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);
1002 }
1003 }
1004 /* umsg_format umsg_parse */
1005
1006 umsg_close(f1);
1007 umsg_close(f2);
1008 umsg_close(f3);
1009 }
1010
1011 static void MessageLength(void)
1012 {
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)];
1020
1021 u_uastrncpy(pattern, patChars, sizeof(pattern)/sizeof(pattern[0]));
1022 u_uastrncpy(expected, expectedChars, sizeof(expected)/sizeof(expected[0]));
1023
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));
1027 }
1028 if (u_strcmp(result, expected) != 0) {
1029 log_err("u_formatMessage didn't return expected result\n");
1030 }
1031 }
1032
1033 static void TestErrorChaining(void) {
1034 UErrorCode status = U_USELESS_COLLATOR_ERROR;
1035
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);
1042 umsg_close(NULL);
1043
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));
1047 }
1048
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));
1053 }
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));
1058 }
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));
1063 }
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));
1068 }
1069 }
1070
1071 void addMsgForTest(TestNode** root);
1072
1073 void addMsgForTest(TestNode** root)
1074 {
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");
1088 }
1089
1090 #endif /* #if !UCONFIG_NO_FORMATTING */