]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/cintltst/cmsgtst.c
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cmsgtst.c
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 1997-2004, International Business Machines Corporation and
b75a7d8f
A
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>
374ca955 23#include <stdarg.h>
b75a7d8f
A
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
33static 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
41static 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 PST and full date is Monday, January 12, 1970",
46 "{1,number,percent} for 1 is 345,600%"
47};
48
49const int32_t cnt_testCases = 5;
50static UChar* testCasePatterns[5];
51
52static UChar* testResultStrings[5];
53
54static UBool strings_initialized = FALSE;
55
56/* function used to create the test patterns for testing Message formatting */
57static void InitStrings( void )
58{
59 int32_t i;
60 if (strings_initialized)
61 return;
62
63 for (i=0; i < cnt_testCases; i++ ) {
374ca955 64 uint32_t strSize = (uint32_t)strlen(txt_testCasePatterns[i]) + 1;
b75a7d8f
A
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++ ) {
374ca955 69 uint32_t strSize = (uint32_t)strlen(txt_testResultStrings[i]) + 1;
b75a7d8f
A
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
77static 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
374ca955
A
92/* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */
93static 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
b75a7d8f
A
102/* Test u_formatMessage() with various test patterns() */
103static 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;
374ca955
A
110
111 ctest_setTimeZone(NULL, &status);
112
b75a7d8f
A
113 str=(UChar*)malloc(sizeof(UChar) * 7);
114 u_uastrncpy(str, "MyDisk", 7);
115 resultlength=1;
116 result=(UChar*)malloc(sizeof(UChar) * 1);
374ca955 117 log_verbose("Testing u_formatMessage()\n");
b75a7d8f
A
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 }
135 if(u_strcmp(result, testResultStrings[i])==0){
136 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
137 }
138 else{
139 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
140 austrdup(result), austrdup(testResultStrings[i]) );
141 }
142 }
143 free(result);
144 result = NULL;
145 free(str);
146 {
147
148 for (i = 0; i < cnt_testCases; i++) {
149 UParseError parseError;
150 status=U_ZERO_ERROR;
151 patternlength=u_strlen(testCasePatterns[i]);
152 resultlength=0;
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)
156 {
157 status=U_ZERO_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);
162 }
163 if(U_FAILURE(status)){
164 log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) );
165 continue;
166 }
167 if(u_strcmp(result, testResultStrings[i])==0){
168 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
169 }
170 else{
171 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
172 austrdup(result), austrdup(testResultStrings[i]) );
173 }
174 free(result);
175 result=NULL;
176 }
177 }
178 {
179 UErrorCode ec = U_ZERO_ERROR;
180 int32_t patternLength = u_strlen(testCasePatterns[0]);
181
182 UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec);
183
184 if(U_FAILURE(ec)){
185 log_err("umsg_open() failed for testCasePattens[%d].\n",i);
186 return;
187 }
188 for(i = 0;i<cnt_testCases; i++){
189 UParseError parseError;
190 int32_t resultLength =0,count=0;
191 int32_t one=0;
192 int32_t two=0;
193 UDate d2=0;
194
195 result=NULL;
196 patternLength = u_strlen(testCasePatterns[i]);
197
198 umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec);
199 if(U_FAILURE(ec)){
200 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i);
201 return;
202 }
203 /* pre-flight */
204 resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1);
205 if(ec==U_BUFFER_OVERFLOW_ERROR){
206 ec=U_ZERO_ERROR;
207 result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2);
208 resultLength = umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1);
209 if(U_FAILURE(ec)){
210 log_err("ERROR: failure in message format on testcase %d: %s\n", i, u_errorName(status) );
211 free(result);
212 return;
213 }
214
215 if(u_strcmp(result, testResultStrings[i])==0){
216 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
217 }
218 else{
219 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
220 austrdup(result), austrdup(testResultStrings[i]) );
221 }
222
374ca955
A
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.
228
229 This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
230 */
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));
234 }else{
235 ec = U_ZERO_ERROR;
236 }
237 }
238 else {
239 log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i);
b75a7d8f
A
240 }
241
b75a7d8f
A
242 umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2);
243 if(U_FAILURE(ec)){
244 log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec));
245 }
246 free(result);
247 }else{
248 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i);
249 }
250 }
251 umsg_close(formatter);
252 }
253 FreeStrings();
374ca955
A
254
255 ctest_resetTimeZone();
b75a7d8f
A
256}
257
258
259/*test u_formatMessage() with sample patterns */
260static void TestSampleMessageFormat()
261{
262 UChar *str;
263 UChar *result;
264 UChar pattern[100], expected[100];
265 int32_t resultLengthOut, resultlength;
266 UDate d = 837039928046.0;
267 UErrorCode status = U_ZERO_ERROR;
374ca955
A
268
269 ctest_setTimeZone(NULL, &status);
270
b75a7d8f
A
271 str=(UChar*)malloc(sizeof(UChar) * 15);
272 u_uastrcpy(str, "abc");
273
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");
278 resultlength=1;
279 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
280 if(status==U_BUFFER_OVERFLOW_ERROR)
281 {
282 status=U_ZERO_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);
286 }
287 if(U_FAILURE(status)){
288 log_err("Error: failure in message format on test#1: %s\n", myErrorName(status));
289 }
290 if(u_strcmp(result, expected)==0)
291 log_verbose("PASS: MessagFormat successful on test#1\n");
292 else{
293 log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
294 austrdup(result), austrdup(expected) );
295 }
296
297
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");
302
303 resultLengthOut=u_formatMessage( "en_US",
304 pattern,
305 u_strlen(pattern),
306 result,
307 resultlength,
308 &status,
309 str,
310 235);
311 if(status==U_BUFFER_OVERFLOW_ERROR)
312 {
313 status=U_ZERO_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);
317 }
318 if(U_FAILURE(status)){
319 log_err("Error: failure in message format on test#2 : %s\n", myErrorName(status));
320 }
321 if(u_strcmp(result, expected)==0)
322 log_verbose("PASS: MessagFormat successful on test#2\n");
323 else{
324 log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
325 austrdup(result), austrdup(expected) );
326 }
327
328
329
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");
334 resultlength=0;
335 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00);
336 if(status==U_BUFFER_OVERFLOW_ERROR)
337 {
338 status=U_ZERO_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);
342 }
343 if(U_FAILURE(status)){
344 log_err("Error: failure in message format on test#3 : %s\n", myErrorName(status));
345 }
346 if(u_strcmp(result, expected)==0)
347 log_verbose("PASS: MessagFormat successful on test#3\n");
348 else{
349 log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result),
350 austrdup(expected) );
351 }
352
353 free(result);
354 free(str);
374ca955
A
355
356 ctest_resetTimeZone();
b75a7d8f
A
357}
358
359/* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
360static void TestNewFormatAndParseAPI(void)
361{
362
363 UChar *result, *tzID, *str;
364 UChar pattern[100];
365 UChar expected[100];
366 int32_t resultLengthOut, resultlength;
367 UCalendar *cal;
368 UDate d1,d;
369 UDateFormat *def1;
370 UErrorCode status = U_ZERO_ERROR;
371 double value = 0.0;
372 UChar ret[30];
373 UParseError parseError;
374 UMessageFormat* fmt = NULL;
375 int32_t count=0;
374ca955
A
376
377 ctest_setTimeZone(NULL, &status);
378
b75a7d8f
A
379 log_verbose("Testing format and parse with parse error\n");
380
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) );
388 return;
389 }
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) );
394 return;
395 }
396
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");
400 resultlength=1;
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) );
404 return;
405 }
406 result=(UChar*)malloc(sizeof(UChar) * resultlength);
407
408 resultLengthOut=umsg_format(fmt ,result, resultlength,&status, d1, str, 7);
409 if(status==U_BUFFER_OVERFLOW_ERROR)
410 {
411 status=U_ZERO_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);
415
416 }
417 if(U_FAILURE(status)){
418 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
419 }
420 if(u_strcmp(result, expected)==0)
421 log_verbose("PASS: MessagFormat successful on test#4\n");
422 else{
423 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
424 austrdup(expected) );
425 }
426
427
428 /*try to parse this and check*/
429 log_verbose("\nTesting the parse Message test#5\n");
430
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));
434 }
435 if(value!=7.00 && u_strcmp(str,ret)!=0)
436 log_err("FAIL: Error in parseMessage on test#5 \n");
437 else
438 log_verbose("PASS: parseMessage successful on test#5\n");
439
440 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
441 if(U_FAILURE(status))
442 {
443 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
444 }else{
445
446 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
447 log_verbose("PASS: parseMessage successful test#5\n");
448 else{
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)) );
451 }
452 }
453 umsg_close(fmt);
454 udat_close(def1);
455 ucal_close(cal);
456
457 free(result);
458 free(str);
459 free(tzID);
374ca955
A
460
461 ctest_resetTimeZone();
b75a7d8f
A
462}
463
464/* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
465static void TestSampleFormatAndParseWithError(void)
466{
467
468 UChar *result, *tzID, *str;
469 UChar pattern[100];
470
471 UChar expected[100];
472 int32_t resultLengthOut, resultlength;
473 UCalendar *cal;
474 UDate d1,d;
475 UDateFormat *def1;
476 UErrorCode status = U_ZERO_ERROR;
477 double value = 0.0;
478 UChar ret[30];
479 UParseError parseError;
480
374ca955
A
481 ctest_setTimeZone(NULL, &status);
482
b75a7d8f
A
483 log_verbose("Testing format and parse with parse error\n");
484
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) );
492 }
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) );
497 }
498
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");
502 resultlength=1;
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)
506 {
507 status=U_ZERO_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);
511
512 }
513 if(U_FAILURE(status)){
514 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
515 }
516 if(u_strcmp(result, expected)==0)
517 log_verbose("PASS: MessagFormat successful on test#4\n");
518 else{
519 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
520 austrdup(expected) );
521 }
522
523
524 /*try to parse this and check*/
525 log_verbose("\nTesting the parse Message test#5\n");
526
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));
530 }
531 if(value!=7.00 && u_strcmp(str,ret)!=0)
532 log_err("FAIL: Error in parseMessage on test#5 \n");
533 else
534 log_verbose("PASS: parseMessage successful on test#5\n");
535
536 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
537 if(U_FAILURE(status))
538 {
539 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
540 }else{
541
542 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
543 log_verbose("PASS: parseMessage successful test#5\n");
544 else{
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)) );
547 }
548 }
549 udat_close(def1);
550 ucal_close(cal);
551
552 free(result);
553 free(str);
554 free(tzID);
555
374ca955 556 ctest_resetTimeZone();
b75a7d8f
A
557}
558
559/* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
560static void TestSampleFormatAndParse()
561{
562
563 UChar *result, *tzID, *str;
564 UChar pattern[100];
565 UChar expected[100];
566 int32_t resultLengthOut, resultlength;
567 UCalendar *cal;
568 UDate d1,d;
569 UDateFormat *def1;
570 UErrorCode status = U_ZERO_ERROR;
571 double value = 0.0;
572 UChar ret[30];
374ca955
A
573
574 ctest_setTimeZone(NULL, &status);
575
b75a7d8f
A
576 log_verbose("Testing format and parse\n");
577
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) );
585 }
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) );
590 }
591
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");
595 resultlength=1;
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)
599 {
600 status=U_ZERO_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);
604
605 }
606 if(U_FAILURE(status)){
607 log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
608 }
609 if(u_strcmp(result, expected)==0)
610 log_verbose("PASS: MessagFormat successful on test#4\n");
611 else{
612 log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
613 austrdup(expected) );
614 }
615
616
617 /*try to parse this and check*/
618 log_verbose("\nTesting the parse Message test#5\n");
619
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));
623 }
624 if(value!=7.00 && u_strcmp(str,ret)!=0)
625 log_err("FAIL: Error in parseMessage on test#5 \n");
626 else
627 log_verbose("PASS: parseMessage successful on test#5\n");
628
629 def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
630 if(U_FAILURE(status))
631 {
632 log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
633 }else{
634
635 if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
636 log_verbose("PASS: parseMessage successful test#5\n");
637 else{
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)) );
640 }
641 }
642 udat_close(def1);
643 ucal_close(cal);
644
645 free(result);
646 free(str);
647 free(tzID);
648
374ca955 649 ctest_resetTimeZone();
b75a7d8f
A
650}
651
652/* test message format with a choice option */
653static void TestMsgFormatChoice()
654{
655 UChar* str;
656 UErrorCode status = U_ZERO_ERROR;
657 UChar *result;
658 UChar pattern[100];
659 UChar expected[100];
660 int32_t resultlength,resultLengthOut;
661
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");
668 resultlength=0;
669 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 100., str);
670 if(status==U_BUFFER_OVERFLOW_ERROR)
671 {
672 status=U_ZERO_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");
678 else{
679 log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result),
680 austrdup(expected) );
681 }
682 free(result);
683 }
684 if(U_FAILURE(status)){
685 log_err("ERROR: failure in message format on test#6 : %s\n", myErrorName(status));
686 }
687
688 log_verbose("Testing message format with choice test #7\n:");
689 u_uastrcpy(expected, "The disk MyDisk contains no files");
690 resultlength=0;
691 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 0., str);
692 if(status==U_BUFFER_OVERFLOW_ERROR)
693 {
694 status=U_ZERO_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);
698
699 if(u_strcmp(result, expected)==0)
700 log_verbose("PASS: MessagFormat successful on test#7\n");
701 else{
702 log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result),
703 austrdup(expected) );
704 }
705 free(result);
706 }
707 if(U_FAILURE(status)){
708 log_err("ERROR: failure in message format on test#7 : %s\n", myErrorName(status));
709 }
710
711 log_verbose("Testing message format with choice test #8\n:");
712 u_uastrcpy(expected, "The disk MyDisk contains one file");
713 resultlength=0;
714 resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 1., str);
715 if(status==U_BUFFER_OVERFLOW_ERROR)
716 {
717 status=U_ZERO_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);
721
722 if(u_strcmp(result, expected)==0)
723 log_verbose("PASS: MessagFormat successful on test#8\n");
724 else{
725 log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result),
726 austrdup(expected) );
727 }
728
729 free(result);
730 }
731 if(U_FAILURE(status)){
732 log_err("ERROR: failure in message format on test#8 : %s\n", myErrorName(status));
733 }
734
735 free(str);
736
737}
738
739/*test u_parseMessage() with various test patterns */
740static void TestParseMessage()
741{
742 UChar pattern[100];
743 UChar source[100];
744 UErrorCode status = U_ZERO_ERROR;
745 int32_t value;
746 UChar str[10];
747 UChar res[10];
748
749 log_verbose("\nTesting a sample for parse Message test#9\n");
750
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");
754
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));
758 }
759 if(value==500.00 && u_strcmp(str,res)==0)
760 log_verbose("PASS: parseMessage successful on test#9\n");
761 else
762 log_err("FAIL: Error in parseMessage on test#9 \n");
763
764
765
766 log_verbose("\nTesting a sample for parse Message test#10\n");
767
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");
771
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));
775 }
776 if(value==123.00 && u_strcmp(str,res)==0)
777 log_verbose("PASS: parseMessage successful on test#10\n");
778 else
779 log_err("FAIL: Error in parseMessage on test#10 \n");
780
781
782
783}
784
785static int32_t CallFormatMessage(const char* locale, UChar* testCasePattern, int32_t patternLength,
786 UChar* result, int32_t resultLength, UErrorCode *status, ...)
787{
788 int32_t len = 0;
789 va_list ap;
790 va_start(ap, status);
791 len = u_vformatMessage(locale, testCasePattern, patternLength, result, resultLength, ap, status);
792 va_end(ap);
793 return len;
794}
795
796/* Test u_vformatMessage() with various test patterns. */
797static void TestMessageFormatWithValist( void )
798{
799
800 UChar *str;
801 UChar* result;
802 int32_t resultLengthOut,resultlength,i, patternlength;
803 UErrorCode status = U_ZERO_ERROR;
804 UDate d1=1000000000.0;
374ca955
A
805
806 ctest_setTimeZone(NULL, &status);
807
b75a7d8f
A
808 str=(UChar*)malloc(sizeof(UChar) * 7);
809 u_uastrcpy(str, "MyDisk");
810 resultlength=1;
811 result=(UChar*)malloc(sizeof(UChar) * 1);
812 log_verbose("Testing u_formatMessage90\n");
813 InitStrings();
814 for (i = 0; i < cnt_testCases; i++) {
815 status=U_ZERO_ERROR;
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)
820 {
821 status=U_ZERO_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);
826 }
827 if(U_FAILURE(status)){
828 log_err("ERROR: failure in message format on testcase %d: %s\n", i, myErrorName(status) );
829 }
830 if(u_strcmp(result, testResultStrings[i])==0){
831 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
832 }
833 else{
834 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
835 austrdup(result), austrdup(testResultStrings[i]) );
836 }
837 }
838 free(result);
839 free(str);
840 FreeStrings();
374ca955
A
841
842 ctest_resetTimeZone();
b75a7d8f
A
843}
844
845static void CallParseMessage(const char* locale, UChar* pattern, int32_t patternLength,
846 UChar* source, int32_t sourceLength, UErrorCode *status, ...)
847{
848 va_list ap;
849 va_start(ap, status);
850 u_vparseMessage(locale, pattern, patternLength, source, sourceLength, ap, status);
851 va_end(ap);
852}
853
854/*test u_vparseMessage() with various test patterns */
855static void TestParseMessageWithValist(void)
856{
857 UChar pattern[100];
858 UChar source[100];
859 UErrorCode status = U_ZERO_ERROR;
860 int32_t value;
861 UChar str[10];
862 UChar res[10];
863
864 log_verbose("\nTesting a sample for parse Message test#9\n");
865
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");
869
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));
873 }
874 if(value==500.00 && u_strcmp(str,res)==0)
875 log_verbose("PASS: parseMessage successful on test#9\n");
876 else
877 log_err("FAIL: Error in parseMessage on test#9 \n");
878
879
880 log_verbose("\nTesting a sample for parse Message test#10\n");
881
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");
885
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));
889 }
890 if(value==123.00 && u_strcmp(str,res)==0)
891 log_verbose("PASS: parseMessage successful on test#10\n");
892 else
893 log_err("FAIL: Error in parseMessage on test#10 \n");
894}
895
896/**
897 * Regression test for ICU4C Jitterbug 904
898 */
899static void TestJ904(void) {
900 UChar pattern[256];
901 UChar result[256];
902 UChar string[16];
903 char cresult[256];
904 int32_t length;
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";
908
374ca955
A
909 ctest_setTimeZone(NULL, &status);
910
b75a7d8f
A
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,
919 string, 1/7.0,
920 789.0+1000*(56+60*(34+60*12)));
921
922 u_austrncpy(cresult, result, sizeof(cresult));
923
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);
931 } else {
932 log_err("FAIL: got \"%s\", expected \"%s\"\n", cresult, EXP);
933 }
374ca955
A
934
935 ctest_resetTimeZone();
b75a7d8f
A
936}
937
938static void OpenMessageFormatTest(void)
939{
940 UMessageFormat *f1, *f2, *f3;
941 UChar pattern[256];
942 UChar result[256];
943 char cresult[256];
944 UParseError parseError;
945 const char* locale = "hi_IN";
946 char* retLoc;
947 const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
948 int32_t length=0;
949 UErrorCode status = U_ZERO_ERROR;
950
951 u_uastrncpy(pattern, PAT, sizeof(pattern)/sizeof(pattern[0]));
952
953 /* Test umsg_open */
954 f1 = umsg_open(pattern,length,NULL,NULL,&status);
955
956 if(U_FAILURE(status))
957 {
958 log_err("umsg_open failed with pattern %s. Error: \n", PAT, u_errorName(status));
959 return;
960 }
961
962 /* Test umsg_open with parse error */
963 status = U_ZERO_ERROR;
964 f2 = umsg_open(pattern,length,NULL,&parseError,&status);
965
966 if(U_FAILURE(status))
967 {
968 log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT, u_errorName(status));
969 return;
970 }
971
972 /* Test umsg_clone */
973 status = U_ZERO_ERROR;
974 f3 = umsg_clone(f1,&status);
975 if(U_FAILURE(status))
976 {
977 log_err("umsg_clone failed. Error %s \n", u_errorName(status));
978 }
979
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)
985 {
986 log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale, retLoc);
987 }
988
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))
993 {
994 log_err("umsg_applyPattern failed. Error %s \n",u_errorName(status));
995 }
996
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));
1001 }
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);
1005 }
1006 /* umsg_format umsg_parse */
1007
1008 umsg_close(f1);
1009 umsg_close(f2);
1010 umsg_close(f3);
1011}
1012
374ca955
A
1013static void MessageLength(void)
1014{
1015 UErrorCode status = U_ZERO_ERROR;
1016 const char patChars[] = {"123{0}456{0}"};
1017 const char expectedChars[] = {"123abc"};
1018 UChar pattern[sizeof(patChars)];
1019 UChar arg[] = {0x61,0x62,0x63,0};
1020 UChar result[128] = {0};
1021 UChar expected[sizeof(expectedChars)];
1022
1023 u_uastrncpy(pattern, patChars, sizeof(pattern)/sizeof(pattern[0]));
1024 u_uastrncpy(expected, expectedChars, sizeof(expected)/sizeof(expected[0]));
1025
1026 u_formatMessage("en_US", pattern, 6, result, sizeof(result)/sizeof(result[0]), &status, arg);
1027 if (U_FAILURE(status)) {
1028 log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status));
1029 }
1030 if (u_strcmp(result, expected) != 0) {
1031 log_err("u_formatMessage didn't return expected result\n");
1032 }
1033}
1034
b75a7d8f
A
1035
1036void addMsgForTest(TestNode** root);
1037
1038void addMsgForTest(TestNode** root)
1039{
1040 addTest(root, &OpenMessageFormatTest, "tsformat/cmsgtst/OpenMessageFormatTest");
1041 addTest(root, &MessageFormatTest, "tsformat/cmsgtst/MessageFormatTest");
1042 addTest(root, &TestSampleMessageFormat, "tsformat/cmsgtst/TestSampleMessageFormat");
1043 addTest(root, &TestSampleFormatAndParse, "tsformat/cmsgtst/TestSampleFormatAndParse");
1044 addTest(root, &TestSampleFormatAndParseWithError, "tsformat/cmsgtst/TestSampleFormatAndParseWithError");
1045 addTest(root, &TestNewFormatAndParseAPI, "tsformat/cmsgtst/TestNewFormatAndParseAPI");
1046 addTest(root, &TestMsgFormatChoice, "tsformat/cmsgtst/TestMsgFormatChoice");
1047 addTest(root, &TestParseMessage, "tsformat/cmsgtst/TestParseMessage");
1048 addTest(root, &TestMessageFormatWithValist, "tsformat/cmsgtst/TestMessageFormatWithValist");
1049 addTest(root, &TestParseMessageWithValist, "tsformat/cmsgtst/TestParseMessageWithValist");
1050 addTest(root, &TestJ904, "tsformat/cmsgtst/TestJ904");
374ca955 1051 addTest(root, &MessageLength, "tsformat/cmsgtst/MessageLength");
b75a7d8f
A
1052}
1053
1054#endif /* #if !UCONFIG_NO_FORMATTING */