• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2010, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************
6  *
7  * File CMSGTST.C
8  *
9  * Modification History:
10  *        Name                     Description
11  *     Madhu Katragadda              Creation
12  ********************************************************************/
13 /* C API TEST FOR MESSAGE FORMAT */
14 
15 #include "unicode/utypes.h"
16 
17 #if !UCONFIG_NO_FORMATTING
18 
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdarg.h>
22 #include "unicode/uloc.h"
23 #include "unicode/umsg.h"
24 #include "unicode/udat.h"
25 #include "unicode/umsg.h"
26 #include "unicode/ustring.h"
27 #include "cintltst.h"
28 #include "cmsgtst.h"
29 #include "cformtst.h"
30 
31 static const char* const txt_testCasePatterns[] = {
32    "Quotes '', '{', a {0,number,integer} '{'0}",
33    "Quotes '', '{', a {0,number,integer} '{'0}",
34    "You deposited {0,number,integer} times an amount of {1,number,currency} on {2,date,short}",
35     "'{'2,time,full}, for {1, number }, {0,number,integer} is {2,time,full} and full date is {2,date,full}",
36    "'{'1,number,percent} for {0,number,integer} is {1,number,percent}",
37 };
38 
39 static const char* const txt_testResultStrings[] = {
40     "Quotes ', {, a 1 {0}",
41     "Quotes ', {, a 1 {0}",
42     "You deposited 1 times an amount of $3,456.00 on 1/12/70",
43     "{2,time,full}, for 3,456, 1 is 5:46:40 AM Pacific Standard Time and full date is Monday, January 12, 1970",
44     "{1,number,percent} for 1 is 345,600%"
45 };
46 
47 const int32_t cnt_testCases = 5;
48 static UChar* testCasePatterns[5];
49 
50 static UChar* testResultStrings[5];
51 
52 static UBool strings_initialized = FALSE;
53 
54 /* function used to create the test patterns for testing Message formatting */
InitStrings(void)55 static void InitStrings( void )
56 {
57     int32_t i;
58     if (strings_initialized)
59         return;
60 
61     for (i=0; i < cnt_testCases; i++ ) {
62         uint32_t strSize = (uint32_t)strlen(txt_testCasePatterns[i]) + 1;
63         testCasePatterns[i]=(UChar*)malloc(sizeof(UChar) * strSize);
64         u_uastrncpy(testCasePatterns[i], txt_testCasePatterns[i], strSize);
65     }
66     for (i=0; i < cnt_testCases; i++ ) {
67         uint32_t strSize = (uint32_t)strlen(txt_testResultStrings[i]) + 1;
68         testResultStrings[i] = (UChar*)malloc(sizeof(UChar) * strSize);
69         u_uastrncpy(testResultStrings[i], txt_testResultStrings[i], strSize);
70     }
71 
72     strings_initialized = TRUE;
73 }
74 
FreeStrings(void)75 static void FreeStrings( void )
76 {
77     int32_t i;
78     if (!strings_initialized)
79         return;
80 
81     for (i=0; i < cnt_testCases; i++ ) {
82         free(testCasePatterns[i]);
83     }
84     for (i=0; i < cnt_testCases; i++ ) {
85         free(testResultStrings[i]);
86     }
87     strings_initialized = FALSE;
88 }
89 
90 /* Platform dependent test to detect if this type will return NULL when interpreted as a pointer. */
returnsNullForType(int firstParam,...)91 static UBool returnsNullForType(int firstParam, ...) {
92     UBool isNULL;
93     va_list marker;
94     va_start(marker, firstParam);
95     isNULL = (UBool)(va_arg(marker, void*) == NULL);
96     va_end(marker);
97     return isNULL;
98 }
99 
100 /* Test u_formatMessage() with various test patterns() */
MessageFormatTest(void)101 static void MessageFormatTest( void )
102 {
103     UChar *str;
104     UChar* result;
105     int32_t resultLengthOut,resultlength,i, patternlength;
106     UErrorCode status = U_ZERO_ERROR;
107     UDate d1=1000000000.0;
108 
109     ctest_setTimeZone(NULL, &status);
110 
111     str=(UChar*)malloc(sizeof(UChar) * 7);
112     u_uastrncpy(str, "MyDisk", 7);
113     resultlength=1;
114     result=(UChar*)malloc(sizeof(UChar) * 1);
115     log_verbose("Testing u_formatMessage()\n");
116     InitStrings();
117     for (i = 0; i < cnt_testCases; i++) {
118         status=U_ZERO_ERROR;
119         patternlength=u_strlen(testCasePatterns[i]);
120         resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength,
121             &status, 1, 3456.00, d1);
122         if(status== U_BUFFER_OVERFLOW_ERROR)
123         {
124             status=U_ZERO_ERROR;
125             resultlength=resultLengthOut+1;
126             result=(UChar*)realloc(result,sizeof(UChar) * resultlength);
127             u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength,
128                 &status, 1, 3456.00, d1);
129         }
130         if(U_FAILURE(status)){
131             log_data_err("ERROR: failure in message format on testcase %d:  %s (Are you missing data?)\n", i, myErrorName(status) );
132             continue;
133         }
134         if(u_strcmp(result, testResultStrings[i])==0){
135             log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
136         }
137         else{
138             log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
139                 austrdup(result), austrdup(testResultStrings[i]) );
140         }
141     }
142     free(result);
143     result = NULL;
144     free(str);
145     {
146 
147          for (i = 0; i < cnt_testCases; i++) {
148             UParseError parseError;
149             status=U_ZERO_ERROR;
150             patternlength=u_strlen(testCasePatterns[i]);
151             resultlength=0;
152             resultLengthOut=u_formatMessageWithError( "en_US",testCasePatterns[i], patternlength, result, resultlength,
153                 &parseError,&status, 1, 3456.00, d1);
154             if(status== U_BUFFER_OVERFLOW_ERROR)
155             {
156                 status=U_ZERO_ERROR;
157                 resultlength=resultLengthOut+1;
158                 result=(UChar*)malloc(sizeof(UChar) * resultlength);
159                 u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength,
160                     &status, 1, 3456.00, d1);
161             }
162             if(U_FAILURE(status)){
163                 log_data_err("ERROR: failure in message format on testcase %d:  %s (Are you missing data?)\n", i, myErrorName(status) );
164                 continue;
165             }
166             if(u_strcmp(result, testResultStrings[i])==0){
167                 log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
168             }
169             else{
170                 log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
171                     austrdup(result), austrdup(testResultStrings[i]) );
172             }
173             free(result);
174             result=NULL;
175         }
176     }
177     {
178         UErrorCode ec = U_ZERO_ERROR;
179         int32_t patternLength = u_strlen(testCasePatterns[0]);
180 
181         UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec);
182 
183         if(U_FAILURE(ec)){
184             log_data_err("umsg_open() failed for testCasePattens[%d]. -> %s (Are you missing data?)\n",i, u_errorName(ec));
185             return;
186         }
187         for(i = 0;i<cnt_testCases; i++){
188             UParseError parseError;
189             int32_t resultLength =0,count=0;
190             int32_t one=0;
191             int32_t two=0;
192             UDate d2=0;
193 
194             result=NULL;
195             patternLength = u_strlen(testCasePatterns[i]);
196 
197             umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec);
198             if(U_FAILURE(ec)){
199                 log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i);
200                 return;
201             }
202             /* pre-flight */
203             resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1);
204             if(ec==U_BUFFER_OVERFLOW_ERROR){
205                 ec=U_ZERO_ERROR;
206                 result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2);
207                 resultLength =  umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1);
208                 if(U_FAILURE(ec)){
209                       log_err("ERROR: failure in message format on testcase %d:  %s\n", i, u_errorName(status) );
210                       free(result);
211                       return;
212                 }
213 
214                 if(u_strcmp(result, testResultStrings[i])==0){
215                     log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
216                 }
217                 else{
218                     log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
219                         austrdup(result), austrdup(testResultStrings[i]) );
220                 }
221 
222 #if defined (U_DARWIN)  /* add platforms here .. */
223                 log_verbose("Skipping potentially crashing test for mismatched varargs.\n");
224 #else
225                 log_verbose("Note: the next is a platform dependent test. If it crashes, add an exclusion for your platform near %s:%d\n", __FILE__, __LINE__);
226 
227                 if (returnsNullForType(1, (double)2.0)) {
228                     /* HP/UX and possibly other platforms don't properly check for this case.
229                     We pass in a UDate, but the function expects a UDate *.  When va_arg is used,
230                     most compilers will return NULL, but HP-UX won't do that and will return 2
231                     in this case.  This is a platform dependent test.  It crashes on some systems.
232 
233                     If you get a crash here, see the definition of returnsNullForType.
234 
235                     This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2
236                     */
237                     umsg_parse(formatter,result,resultLength,&count,&ec,one,two,d2);
238                     if(ec!=U_ILLEGAL_ARGUMENT_ERROR){
239                         log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec));
240                     }else{
241                         ec = U_ZERO_ERROR;
242                     }
243                 }
244                 else {
245                     log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i);
246                 }
247 #endif
248 
249                 umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2);
250                 if(U_FAILURE(ec)){
251                     log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec));
252                 }
253                 free(result);
254             }else{
255                 log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i);
256             }
257         }
258         umsg_close(formatter);
259     }
260     FreeStrings();
261 
262     ctest_resetTimeZone();
263 }
264 
265 
266 /*test u_formatMessage() with sample patterns */
TestSampleMessageFormat(void)267 static void TestSampleMessageFormat(void)
268 {
269     UChar *str;
270     UChar *result;
271     UChar pattern[100], expected[100];
272     int32_t resultLengthOut, resultlength;
273     UDate d = 837039928046.0;
274     UErrorCode status = U_ZERO_ERROR;
275 
276     ctest_setTimeZone(NULL, &status);
277 
278     str=(UChar*)malloc(sizeof(UChar) * 15);
279     u_uastrcpy(str, "abc");
280 
281     u_uastrcpy(pattern, "There are {0} files on {1,date}");
282     u_uastrcpy(expected, "There are abc files on Jul 10, 1996");
283     result=(UChar*)malloc(sizeof(UChar) * 1);
284     log_verbose("\nTesting a sample for Message format test#1\n");
285     resultlength=1;
286     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
287     if(status==U_BUFFER_OVERFLOW_ERROR)
288     {
289         status=U_ZERO_ERROR;
290         resultlength=resultLengthOut+1;
291         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
292         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d);
293     }
294     if(U_FAILURE(status)){
295         log_data_err("Error: failure in message format on test#1: %s (Are you missing data?)\n", myErrorName(status));
296     }
297     else if(u_strcmp(result, expected)==0)
298         log_verbose("PASS: MessagFormat successful on test#1\n");
299     else{
300         log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n",
301             austrdup(result), austrdup(expected) );
302     }
303 
304 
305     log_verbose("\nTesting message format with another pattern test#2\n");
306     u_uastrcpy(pattern, "The disk \"{0}\" contains {1,number,integer} file(s)");
307     u_uastrcpy(expected, "The disk \"MyDisk\" contains 23 file(s)");
308     u_uastrcpy(str, "MyDisk");
309 
310     resultLengthOut=u_formatMessage( "en_US",
311         pattern,
312         u_strlen(pattern),
313         result,
314         resultlength,
315         &status,
316         str,
317         235);
318     if(status==U_BUFFER_OVERFLOW_ERROR)
319     {
320         status=U_ZERO_ERROR;
321         resultlength=resultLengthOut+1;
322         result=(UChar*)realloc(result, sizeof(UChar) * (resultlength+1));
323         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 23);
324     }
325     if(U_FAILURE(status)){
326         log_data_err("Error: failure in message format on test#2 : %s (Are you missing data?)\n", myErrorName(status));
327     }
328     else if(u_strcmp(result, expected)==0)
329         log_verbose("PASS: MessagFormat successful on test#2\n");
330     else{
331         log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n",
332             austrdup(result), austrdup(expected) );
333     }
334 
335 
336 
337     log_verbose("\nTesting message format with another pattern test#3\n");
338     u_uastrcpy(pattern, "You made a {0} of {1,number,currency}");
339     u_uastrcpy(expected, "You made a deposit of $500.00");
340     u_uastrcpy(str, "deposit");
341     resultlength=0;
342     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00);
343     if(status==U_BUFFER_OVERFLOW_ERROR)
344     {
345         status=U_ZERO_ERROR;
346         resultlength=resultLengthOut+1;
347         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
348         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 500.00);
349     }
350     if(U_FAILURE(status)){
351         log_data_err("Error: failure in message format on test#3 : %s (Are you missing data?)\n", myErrorName(status));
352     }
353     else if(u_strcmp(result, expected)==0)
354         log_verbose("PASS: MessagFormat successful on test#3\n");
355     else{
356         log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result),
357             austrdup(expected) );
358     }
359 
360     free(result);
361     free(str);
362 
363     ctest_resetTimeZone();
364 }
365 
366 /* Test umsg_format() and umsg_parse() , format and parse sequence and round trip */
TestNewFormatAndParseAPI(void)367 static void TestNewFormatAndParseAPI(void)
368 {
369 
370     UChar *result, tzID[4], str[25];
371     UChar pattern[100];
372     UChar expected[100];
373     int32_t resultLengthOut, resultlength;
374     UCalendar *cal;
375     UDate d1,d;
376     UDateFormat *def1;
377     UErrorCode status = U_ZERO_ERROR;
378     int32_t value = 0;
379     UChar ret[30];
380     UParseError parseError;
381     UMessageFormat* fmt = NULL;
382     int32_t count=0;
383 
384     ctest_setTimeZone(NULL, &status);
385 
386     log_verbose("Testing format and parse with parse error\n");
387 
388     u_uastrcpy(str, "disturbance in force");
389     u_uastrcpy(tzID, "PST");
390     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
391     if(U_FAILURE(status)){
392         log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) );
393         return;
394     }
395     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
396     d1=ucal_getMillis(cal, &status);
397     if(U_FAILURE(status)){
398             log_err("Error: failure in get millis: %s\n", myErrorName(status) );
399             return;
400     }
401 
402     log_verbose("\nTesting with pattern test#4");
403     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
404     u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7");
405     resultlength=1;
406     fmt = umsg_open(pattern,u_strlen(pattern),"en_US",&parseError,&status);
407     if(U_FAILURE(status)){
408         log_data_err("error in umsg_open  : %s (Are you missing data?)\n", u_errorName(status) );
409         return;
410     }
411     result=(UChar*)malloc(sizeof(UChar) * resultlength);
412 
413     resultLengthOut=umsg_format(fmt ,result, resultlength,&status, d1, str, 7);
414     if(status==U_BUFFER_OVERFLOW_ERROR)
415     {
416         status=U_ZERO_ERROR;
417         resultlength=resultLengthOut+1;
418         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
419         u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7);
420 
421     }
422     if(U_FAILURE(status)){
423         log_err("ERROR: failure in message format test#4: %s\n", myErrorName(status));
424     }
425     if(u_strcmp(result, expected)==0)
426         log_verbose("PASS: MessagFormat successful on test#4\n");
427     else{
428         log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
429             austrdup(expected) );
430     }
431 
432 
433     /*try to parse this and check*/
434     log_verbose("\nTesting the parse Message test#5\n");
435 
436     umsg_parse(fmt, result, u_strlen(result),&count,&status, &d, ret, &value);
437     if(U_FAILURE(status)){
438         log_err("ERROR: error in parsing: test#5: %s\n", myErrorName(status));
439     }
440     if(value!=7 && u_strcmp(str,ret)!=0)
441         log_err("FAIL: Error in parseMessage on test#5 \n");
442     else
443         log_verbose("PASS: parseMessage successful on test#5\n");
444 
445     def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
446     if(U_FAILURE(status))
447     {
448         log_err("error in creating the dateformat using short date and time style:\n %s\n", myErrorName(status));
449     }else{
450 
451         if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
452             log_verbose("PASS: parseMessage successful test#5\n");
453         else{
454             log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
455                 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
456         }
457     }
458     umsg_close(fmt);
459     udat_close(def1);
460     ucal_close(cal);
461 
462     free(result);
463 
464     ctest_resetTimeZone();
465 }
466 
467 /* Test u_formatMessageWithError() and u_parseMessageWithError() , format and parse sequence and round trip */
TestSampleFormatAndParseWithError(void)468 static void TestSampleFormatAndParseWithError(void)
469 {
470 
471     UChar *result, *tzID, *str;
472     UChar pattern[100];
473 
474     UChar expected[100];
475     int32_t resultLengthOut, resultlength;
476     UCalendar *cal;
477     UDate d1,d;
478     UDateFormat *def1;
479     UErrorCode status = U_ZERO_ERROR;
480     int32_t value = 0;
481     UChar ret[30];
482     UParseError parseError;
483 
484     ctest_setTimeZone(NULL, &status);
485 
486     log_verbose("Testing format and parse with parse error\n");
487 
488     str=(UChar*)malloc(sizeof(UChar) * 25);
489     u_uastrcpy(str, "disturbance in force");
490     tzID=(UChar*)malloc(sizeof(UChar) * 4);
491     u_uastrcpy(tzID, "PST");
492     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
493     if(U_FAILURE(status)){
494         log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) );
495     }
496     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
497     d1=ucal_getMillis(cal, &status);
498     if(U_FAILURE(status)){
499             log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) );
500     }
501 
502     log_verbose("\nTesting with pattern test#4");
503     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
504     u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7");
505     resultlength=1;
506     result=(UChar*)malloc(sizeof(UChar) * resultlength);
507     resultLengthOut=u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7);
508     if(status==U_BUFFER_OVERFLOW_ERROR)
509     {
510         status=U_ZERO_ERROR;
511         resultlength=resultLengthOut+1;
512         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
513         u_formatMessageWithError( "en_US", pattern, u_strlen(pattern), result, resultlength,&parseError, &status, d1, str, 7);
514 
515     }
516     if(U_FAILURE(status)){
517         log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status));
518     }
519     else if(u_strcmp(result, expected)==0)
520         log_verbose("PASS: MessagFormat successful on test#4\n");
521     else{
522         log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
523             austrdup(expected) );
524     }
525 
526 
527     /*try to parse this and check*/
528     log_verbose("\nTesting the parse Message test#5\n");
529 
530     u_parseMessageWithError("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &parseError,&status, &d, ret, &value);
531     if(U_FAILURE(status)){
532         log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status));
533     }
534     else if(value!=7 && u_strcmp(str,ret)!=0)
535         log_err("FAIL: Error in parseMessage on test#5 \n");
536     else
537         log_verbose("PASS: parseMessage successful on test#5\n");
538 
539     def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
540     if(U_FAILURE(status))
541     {
542         log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status));
543     }else{
544 
545         if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
546             log_verbose("PASS: parseMessage successful test#5\n");
547         else{
548             log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
549                 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
550         }
551     }
552     udat_close(def1);
553     ucal_close(cal);
554 
555     free(result);
556     free(str);
557     free(tzID);
558 
559     ctest_resetTimeZone();
560 }
561 
562 /* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */
TestSampleFormatAndParse(void)563 static void TestSampleFormatAndParse(void)
564 {
565 
566     UChar *result, *tzID, *str;
567     UChar pattern[100];
568     UChar expected[100];
569     int32_t resultLengthOut, resultlength;
570     UCalendar *cal;
571     UDate d1,d;
572     UDateFormat *def1;
573     UErrorCode status = U_ZERO_ERROR;
574     int32_t value = 0;
575     UChar ret[30];
576 
577     ctest_setTimeZone(NULL, &status);
578 
579     log_verbose("Testing format and parse\n");
580 
581     str=(UChar*)malloc(sizeof(UChar) * 25);
582     u_uastrcpy(str, "disturbance in force");
583     tzID=(UChar*)malloc(sizeof(UChar) * 4);
584     u_uastrcpy(tzID, "PST");
585     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
586     if(U_FAILURE(status)){
587         log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) );
588     }
589     ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status);
590     d1=ucal_getMillis(cal, &status);
591     if(U_FAILURE(status)){
592             log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) );
593     }
594 
595     log_verbose("\nTesting with pattern test#4");
596     u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}");
597     u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7");
598     resultlength=1;
599     result=(UChar*)malloc(sizeof(UChar) * resultlength);
600     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
601     if(status==U_BUFFER_OVERFLOW_ERROR)
602     {
603         status=U_ZERO_ERROR;
604         resultlength=resultLengthOut+1;
605         result=(UChar*)realloc(result, sizeof(UChar) * resultlength);
606         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7);
607 
608     }
609     if(U_FAILURE(status)){
610         log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status));
611     }
612     else if(u_strcmp(result, expected)==0)
613         log_verbose("PASS: MessagFormat successful on test#4\n");
614     else{
615         log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result),
616             austrdup(expected) );
617     }
618 
619 
620     /*try to parse this and check*/
621     log_verbose("\nTesting the parse Message test#5\n");
622 
623     u_parseMessage("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &status, &d, ret, &value);
624     if(U_FAILURE(status)){
625         log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status));
626     }
627     else if(value!=7 && u_strcmp(str,ret)!=0)
628         log_err("FAIL: Error in parseMessage on test#5 \n");
629     else
630         log_verbose("PASS: parseMessage successful on test#5\n");
631 
632     def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status);
633     if(U_FAILURE(status))
634     {
635         log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status));
636     }else{
637 
638         if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0)
639             log_verbose("PASS: parseMessage successful test#5\n");
640         else{
641             log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n",
642                 austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) );
643         }
644     }
645     udat_close(def1);
646     ucal_close(cal);
647 
648     free(result);
649     free(str);
650     free(tzID);
651 
652     ctest_resetTimeZone();
653 }
654 
655 /* Test message format with a Select option */
TestMsgFormatSelect(void)656 static void TestMsgFormatSelect(void)
657 {
658     UChar* str;
659     UChar* str1;
660     UErrorCode status = U_ZERO_ERROR;
661     UChar *result;
662     UChar pattern[100];
663     UChar expected[100];
664     int32_t resultlength,resultLengthOut;
665 
666     str=(UChar*)malloc(sizeof(UChar) * 25);
667     u_uastrcpy(str, "Kirti");
668     str1=(UChar*)malloc(sizeof(UChar) * 25);
669     u_uastrcpy(str1, "female");
670     log_verbose("Testing message format with Select test #1\n:");
671     u_uastrcpy(pattern, "{0} est {1, select, female {all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
672     u_uastrcpy(expected, "Kirti est all\\u00E9e \\u00E0 Paris.");
673     resultlength=0;
674     resultLengthOut=u_formatMessage( "fr", pattern, u_strlen(pattern), NULL, resultlength, &status, str , str1);
675     if(status==U_BUFFER_OVERFLOW_ERROR)
676     {
677         status=U_ZERO_ERROR;
678         resultlength=resultLengthOut+1;
679         result=(UChar*)malloc(sizeof(UChar) * resultlength);
680         u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1);
681         if(u_strcmp(result, expected)==0)
682             log_verbose("PASS: MessagFormat successful on Select test#1\n");
683         else{
684             log_err("FAIL: Error in MessageFormat on Select test#1\n GOT %s EXPECTED %s\n", austrdup(result),
685                 austrdup(expected) );
686         }
687         free(result);
688     }
689     if(U_FAILURE(status)){
690         log_data_err("ERROR: failure in message format on Select test#1 : %s \n", myErrorName(status));
691     }
692     free(str);
693     free(str1);
694 
695     /*Test a nested pattern*/
696     str=(UChar*)malloc(sizeof(UChar) * 25);
697     u_uastrcpy(str, "Noname");
698     str1=(UChar*)malloc(sizeof(UChar) * 25);
699     u_uastrcpy(str1, "other");
700     log_verbose("Testing message format with Select test #2\n:");
701     u_uastrcpy(pattern, "{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
702     u_uastrcpy(expected, "Noname est all\\u00E9 \\u00E0 Paris.");
703     resultlength=0;
704     resultLengthOut=u_formatMessage( "fr", pattern, u_strlen(pattern), NULL, resultlength, &status, str , str1,6);
705     if(status==U_BUFFER_OVERFLOW_ERROR)
706     {
707         status=U_ZERO_ERROR;
708         resultlength=resultLengthOut+1;
709         result=(UChar*)malloc(sizeof(UChar) * resultlength);
710         u_formatMessage( "fr", pattern, u_strlen(pattern), result, resultlength, &status, str , str1);
711         if(u_strcmp(result, expected)==0)
712             log_verbose("PASS: MessagFormat successful on Select test#2\n");
713         else{
714             log_err("FAIL: Error in MessageFormat on Select test#2\n GOT %s EXPECTED %s\n", austrdup(result),
715                 austrdup(expected) );
716         }
717         free(result);
718     }
719     if(U_FAILURE(status)){
720         log_data_err("ERROR: failure in message format on Select test#2 : %s \n", myErrorName(status));
721     }
722     free(str);
723     free(str1);
724 }
725 
726 /* test message format with a choice option */
TestMsgFormatChoice(void)727 static void TestMsgFormatChoice(void)
728 {
729     UChar* str;
730     UErrorCode status = U_ZERO_ERROR;
731     UChar *result;
732     UChar pattern[100];
733     UChar expected[100];
734     int32_t resultlength,resultLengthOut;
735 
736     str=(UChar*)malloc(sizeof(UChar) * 25);
737     u_uastrcpy(str, "MyDisk");
738     log_verbose("Testing message format with choice test #6\n:");
739     /*There {0,choice,0#are no files|1#is one file|1<are {0,number,integer} files}.*/
740     u_uastrcpy(pattern, "The disk {1} contains {0,choice,0#no files|1#one file|1<{0,number,integer} files}");
741     u_uastrcpy(expected, "The disk MyDisk contains 100 files");
742     resultlength=0;
743     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 100., str);
744     if(status==U_BUFFER_OVERFLOW_ERROR)
745     {
746         status=U_ZERO_ERROR;
747         resultlength=resultLengthOut+1;
748         result=(UChar*)malloc(sizeof(UChar) * resultlength);
749         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 100., str);
750         if(u_strcmp(result, expected)==0)
751             log_verbose("PASS: MessagFormat successful on test#6\n");
752         else{
753             log_err("FAIL: Error in MessageFormat on test#6\n GOT %s EXPECTED %s\n", austrdup(result),
754                 austrdup(expected) );
755         }
756         free(result);
757     }
758     if(U_FAILURE(status)){
759         log_data_err("ERROR: failure in message format on test#6 : %s (Are you missing data?)\n", myErrorName(status));
760     }
761 
762     log_verbose("Testing message format with choice test #7\n:");
763     u_uastrcpy(expected, "The disk MyDisk contains no files");
764     resultlength=0;
765     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 0., str);
766     if(status==U_BUFFER_OVERFLOW_ERROR)
767     {
768         status=U_ZERO_ERROR;
769         resultlength=resultLengthOut+1;
770         result=(UChar*)malloc(sizeof(UChar) * resultlength);
771         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 0., str);
772 
773         if(u_strcmp(result, expected)==0)
774             log_verbose("PASS: MessagFormat successful on test#7\n");
775         else{
776             log_err("FAIL: Error in MessageFormat on test#7\n GOT: %s EXPECTED %s\n", austrdup(result),
777                 austrdup(expected) );
778         }
779         free(result);
780     }
781     if(U_FAILURE(status)){
782         log_data_err("ERROR: failure in message format on test#7 : %s (Are you missing data?)\n", myErrorName(status));
783     }
784 
785     log_verbose("Testing message format with choice test #8\n:");
786     u_uastrcpy(expected, "The disk MyDisk contains one file");
787     resultlength=0;
788     resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, 1., str);
789     if(status==U_BUFFER_OVERFLOW_ERROR)
790     {
791         status=U_ZERO_ERROR;
792         resultlength=resultLengthOut+1;
793         result=(UChar*)malloc(sizeof(UChar) * resultlength);
794         u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, 1., str);
795 
796         if(u_strcmp(result, expected)==0)
797             log_verbose("PASS: MessagFormat successful on test#8\n");
798         else{
799             log_err("FAIL: Error in MessageFormat on test#8\n GOT %s EXPECTED: %s\n", austrdup(result),
800                 austrdup(expected) );
801         }
802 
803         free(result);
804     }
805     if(U_FAILURE(status)){
806         log_data_err("ERROR: failure in message format on test#8 : %s (Are you missing data?)\n", myErrorName(status));
807     }
808 
809     free(str);
810 
811 }
812 
813 /*test u_parseMessage() with various test patterns */
TestParseMessage(void)814 static void TestParseMessage(void)
815 {
816     UChar pattern[100];
817     UChar source[100];
818     UErrorCode status = U_ZERO_ERROR;
819     int32_t value;
820     UChar str[10];
821     UChar res[10];
822 
823     log_verbose("\nTesting a sample for parse Message test#9\n");
824 
825     u_uastrcpy(source, "You deposited an amount of $500.00");
826     u_uastrcpy(pattern, "You {0} an amount of {1,number,currency}");
827     u_uastrcpy(res,"deposited");
828 
829     u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, str, &value);
830     if(U_FAILURE(status)){
831         log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status));
832     }
833     else if(value==500.00  && u_strcmp(str,res)==0)
834         log_verbose("PASS: parseMessage successful on test#9\n");
835     else
836         log_err("FAIL: Error in parseMessage on test#9 \n");
837 
838 
839 
840     log_verbose("\nTesting a sample for parse Message test#10\n");
841 
842     u_uastrcpy(source, "There are 123 files on MyDisk created");
843     u_uastrcpy(pattern, "There are {0,number,integer} files on {1} created");
844     u_uastrcpy(res,"MyDisk");
845 
846     u_parseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, &value, str);
847     if(U_FAILURE(status)){
848         log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status));
849     }
850     else if(value==123.00 && u_strcmp(str,res)==0)
851         log_verbose("PASS: parseMessage successful on test#10\n");
852     else
853         log_err("FAIL: Error in parseMessage on test#10 \n");
854 }
855 
CallFormatMessage(const char * locale,UChar * testCasePattern,int32_t patternLength,UChar * result,int32_t resultLength,UErrorCode * status,...)856 static int32_t CallFormatMessage(const char* locale, UChar* testCasePattern, int32_t patternLength,
857                        UChar* result, int32_t resultLength, UErrorCode *status, ...)
858 {
859     int32_t len = 0;
860     va_list ap;
861     va_start(ap, status);
862     len = u_vformatMessage(locale, testCasePattern, patternLength, result, resultLength, ap, status);
863     va_end(ap);
864     return len;
865 }
866 
867 /* Test u_vformatMessage() with various test patterns. */
TestMessageFormatWithValist(void)868 static void TestMessageFormatWithValist( void )
869 {
870 
871     UChar *str;
872     UChar* result;
873     int32_t resultLengthOut,resultlength,i, patternlength;
874     UErrorCode status = U_ZERO_ERROR;
875     UDate d1=1000000000.0;
876 
877     ctest_setTimeZone(NULL, &status);
878 
879     str=(UChar*)malloc(sizeof(UChar) * 7);
880     u_uastrcpy(str, "MyDisk");
881     resultlength=1;
882     result=(UChar*)malloc(sizeof(UChar) * 1);
883     log_verbose("Testing u_formatMessage90\n");
884     InitStrings();
885     for (i = 0; i < cnt_testCases; i++) {
886         status=U_ZERO_ERROR;
887         patternlength=u_strlen(testCasePatterns[i]);
888         resultLengthOut=CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength,
889             &status, 1, 3456.00, d1);
890         if(status== U_BUFFER_OVERFLOW_ERROR)
891         {
892             status=U_ZERO_ERROR;
893             resultlength=resultLengthOut+1;
894             result=(UChar*)realloc(result,sizeof(UChar) * resultlength);
895             CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength,
896                 &status, 1, 3456.00, d1);
897         }
898         if(U_FAILURE(status)){
899             log_data_err("ERROR: failure in message format on testcase %d:  %s (Are you missing data?)\n", i, myErrorName(status) );
900         }
901         else if(u_strcmp(result, testResultStrings[i])==0){
902             log_verbose("PASS: MessagFormat successful on testcase : %d\n", i);
903         }
904         else{
905             log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i,
906                 austrdup(result), austrdup(testResultStrings[i]) );
907         }
908     }
909     free(result);
910     free(str);
911     FreeStrings();
912 
913     ctest_resetTimeZone();
914 }
915 
CallParseMessage(const char * locale,UChar * pattern,int32_t patternLength,UChar * source,int32_t sourceLength,UErrorCode * status,...)916 static void CallParseMessage(const char* locale, UChar* pattern, int32_t patternLength,
917                        UChar* source, int32_t sourceLength, UErrorCode *status, ...)
918 {
919     va_list ap;
920     va_start(ap, status);
921     u_vparseMessage(locale, pattern, patternLength, source, sourceLength, ap, status);
922     va_end(ap);
923 }
924 
925 /*test u_vparseMessage() with various test patterns */
TestParseMessageWithValist(void)926 static void TestParseMessageWithValist(void)
927 {
928     UChar pattern[100];
929     UChar source[100];
930     UErrorCode status = U_ZERO_ERROR;
931     int32_t value;
932     UChar str[10];
933     UChar res[10];
934 
935     log_verbose("\nTesting a sample for parse Message test#9\n");
936 
937     u_uastrcpy(source, "You deposited an amount of $500.00");
938     u_uastrcpy(pattern, "You {0} an amount of {1,number,currency}");
939     u_uastrcpy(res,"deposited");
940 
941     CallParseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, str, &value);
942     if(U_FAILURE(status)){
943         log_data_err("ERROR: failure in parse Message on test#9: %s (Are you missing data?)\n", myErrorName(status));
944     }
945     else if(value==500.00  && u_strcmp(str,res)==0)
946         log_verbose("PASS: parseMessage successful on test#9\n");
947     else
948         log_err("FAIL: Error in parseMessage on test#9\n");
949 
950 
951     log_verbose("\nTesting a sample for parse Message test#10\n");
952 
953     u_uastrcpy(source, "There are 123 files on MyDisk created");
954     u_uastrcpy(pattern, "There are {0,number,integer} files on {1} created");
955     u_uastrcpy(res,"MyDisk");
956 
957     CallParseMessage( "en_US", pattern, u_strlen(pattern), source, u_strlen(source), &status, &value, str);
958     if(U_FAILURE(status)){
959         log_data_err("ERROR: failure in parse Message on test#10: %s (Are you missing data?)\n", myErrorName(status));
960     }
961     else if(value==123.00 && u_strcmp(str,res)==0)
962         log_verbose("PASS: parseMessage successful on test#10\n");
963     else
964         log_err("FAIL: Error in parseMessage on test#10 \n");
965 }
966 
967 /**
968  * Regression test for ICU4C Jitterbug 904
969  */
TestJ904(void)970 static void TestJ904(void) {
971     UChar pattern[256];
972     UChar result[256];
973     UChar string[16];
974     char cresult[256];
975     int32_t length;
976     UErrorCode status = U_ZERO_ERROR;
977     const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
978     const char* EXP = "Number 0,143, String foo, Date 12:34:56.789";
979 
980     ctest_setTimeZone(NULL, &status);
981 
982     u_uastrcpy(string, "foo");
983     /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use
984      * 12:mm:ss.SSS.  Why?  So this test generates the same output --
985      * "12:34:56.789" -- regardless of time zone (as long as we aren't
986      * in one of the 30 minute offset zones!). */
987     u_uastrcpy(pattern, PAT);
988     length = u_formatMessage("nl", pattern, u_strlen(pattern),
989                              result, 256, &status,
990                              string, 1/7.0,
991                              789.0+1000*(56+60*(34+60*12)));
992 
993     u_austrncpy(cresult, result, sizeof(cresult));
994 
995     /* This test passes if it DOESN'T CRASH.  However, we test the
996      * output anyway.  If the string doesn't match in the date part,
997      * check to see that the machine doesn't have an unusual time zone
998      * offset, that is, one with a non-zero minutes/seconds offset
999      * from GMT -- see above. */
1000     if (strcmp(cresult, EXP) == 0) {
1001         log_verbose("Ok: \"%s\"\n", cresult);
1002     } else {
1003         log_data_err("FAIL: got \"%s\", expected \"%s\" -> %s (Are you missing data?)\n", cresult, EXP, u_errorName(status));
1004     }
1005 
1006     ctest_resetTimeZone();
1007 }
1008 
OpenMessageFormatTest(void)1009 static void OpenMessageFormatTest(void)
1010 {
1011     UMessageFormat *f1, *f2, *f3;
1012     UChar pattern[256];
1013     UChar result[256];
1014     char cresult[256];
1015     UParseError parseError;
1016     const char* locale = "hi_IN";
1017     char* retLoc;
1018     const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}";
1019     int32_t length=0;
1020     UErrorCode status = U_ZERO_ERROR;
1021 
1022     u_uastrncpy(pattern, PAT, sizeof(pattern)/sizeof(pattern[0]));
1023 
1024     /* Test umsg_open                   */
1025     f1 = umsg_open(pattern,length,NULL,NULL,&status);
1026 
1027     if(U_FAILURE(status))
1028     {
1029         log_err("umsg_open failed with pattern %s. Error: \n", PAT, u_errorName(status));
1030         return;
1031     }
1032 
1033     /* Test umsg_open with parse error  */
1034     status = U_ZERO_ERROR;
1035     f2 = umsg_open(pattern,length,NULL,&parseError,&status);
1036 
1037     if(U_FAILURE(status))
1038     {
1039         log_err("umsg_open with parseError failed with pattern %s. Error: %s\n", PAT, u_errorName(status));
1040         return;
1041     }
1042 
1043     /* Test umsg_clone                  */
1044     status = U_ZERO_ERROR;
1045     f3 = umsg_clone(f1,&status);
1046     if(U_FAILURE(status))
1047     {
1048         log_err("umsg_clone failed. Error %s \n", u_errorName(status));
1049     }
1050 
1051     /* Test umsg_setLocale              */
1052     umsg_setLocale(f1,locale);
1053     /* Test umsg_getLocale              */
1054     retLoc = (char*)umsg_getLocale(f1);
1055     if(strcmp(retLoc,locale)!=0)
1056     {
1057         log_err("umsg_setLocale and umsg_getLocale methods failed. Expected:%s Got: %s \n", locale, retLoc);
1058     }
1059 
1060     /* Test umsg_applyPattern           */
1061     status = U_ZERO_ERROR;
1062     umsg_applyPattern(f1,pattern,(int32_t)strlen(PAT),NULL,&status);
1063     if(U_FAILURE(status))
1064     {
1065         log_data_err("umsg_applyPattern failed. Error %s (Are you missing data?)\n",u_errorName(status));
1066     }
1067 
1068     /* Test umsg_toPattern              */
1069     umsg_toPattern(f1,result,256,&status);
1070     if(U_FAILURE(status) ){
1071         log_data_err("umsg_toPattern method failed. Error: %s (Are you missing data?)\n",u_errorName(status));
1072     } else {
1073         if(u_strcmp(result,pattern)!=0){
1074             u_UCharsToChars(result,cresult,256);
1075             log_err("umsg_toPattern method failed. Expected: %s Got: %s \n",PAT,cresult);
1076         }
1077     }
1078     /* umsg_format umsg_parse */
1079 
1080     umsg_close(f1);
1081     umsg_close(f2);
1082     umsg_close(f3);
1083 }
1084 
MessageLength(void)1085 static void MessageLength(void)
1086 {
1087     UErrorCode status = U_ZERO_ERROR;
1088     const char patChars[] = {"123{0}456{0}"};
1089     const char expectedChars[] = {"123abc"};
1090     UChar pattern[sizeof(patChars)];
1091     UChar arg[] = {0x61,0x62,0x63,0};
1092     UChar result[128] = {0};
1093     UChar expected[sizeof(expectedChars)];
1094 
1095     u_uastrncpy(pattern, patChars, sizeof(pattern)/sizeof(pattern[0]));
1096     u_uastrncpy(expected, expectedChars, sizeof(expected)/sizeof(expected[0]));
1097 
1098     u_formatMessage("en_US", pattern, 6, result, sizeof(result)/sizeof(result[0]), &status, arg);
1099     if (U_FAILURE(status)) {
1100         log_err("u_formatMessage method failed. Error: %s \n",u_errorName(status));
1101     }
1102     if (u_strcmp(result, expected) != 0) {
1103         log_err("u_formatMessage didn't return expected result\n");
1104     }
1105 }
1106 
TestErrorChaining(void)1107 static void TestErrorChaining(void) {
1108     UErrorCode status = U_USELESS_COLLATOR_ERROR;
1109 
1110     umsg_open(NULL, 0, NULL, NULL, &status);
1111     umsg_applyPattern(NULL, NULL, 0, NULL, &status);
1112     umsg_toPattern(NULL, NULL, 0, &status);
1113     umsg_clone(NULL, &status);
1114     umsg_format(NULL, NULL, 0, &status);
1115     umsg_parse(NULL, NULL, 0, NULL, &status);
1116     umsg_close(NULL);
1117 
1118     /* All of this code should have done nothing. */
1119     if (status != U_USELESS_COLLATOR_ERROR) {
1120         log_err("Status got changed to %s\n", u_errorName(status));
1121     }
1122 
1123     status = U_ZERO_ERROR;
1124     umsg_open(NULL, 0, NULL, NULL, &status);
1125     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
1126         log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
1127     }
1128     status = U_ZERO_ERROR;
1129     umsg_applyPattern(NULL, NULL, 0, NULL, &status);
1130     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
1131         log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
1132     }
1133     status = U_ZERO_ERROR;
1134     umsg_toPattern(NULL, NULL, 0, &status);
1135     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
1136         log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
1137     }
1138     status = U_ZERO_ERROR;
1139     umsg_clone(NULL, &status);
1140     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
1141         log_err("Status should be U_ILLEGAL_ARGUMENT_ERROR instead of %s\n", u_errorName(status));
1142     }
1143 }
1144 
1145 void addMsgForTest(TestNode** root);
1146 
addMsgForTest(TestNode ** root)1147 void addMsgForTest(TestNode** root)
1148 {
1149     addTest(root, &OpenMessageFormatTest, "tsformat/cmsgtst/OpenMessageFormatTest");
1150     addTest(root, &MessageFormatTest, "tsformat/cmsgtst/MessageFormatTest");
1151     addTest(root, &TestSampleMessageFormat, "tsformat/cmsgtst/TestSampleMessageFormat");
1152     addTest(root, &TestSampleFormatAndParse, "tsformat/cmsgtst/TestSampleFormatAndParse");
1153     addTest(root, &TestSampleFormatAndParseWithError, "tsformat/cmsgtst/TestSampleFormatAndParseWithError");
1154     addTest(root, &TestNewFormatAndParseAPI, "tsformat/cmsgtst/TestNewFormatAndParseAPI");
1155     addTest(root, &TestMsgFormatChoice, "tsformat/cmsgtst/TestMsgFormatChoice");
1156     addTest(root, &TestParseMessage, "tsformat/cmsgtst/TestParseMessage");
1157     addTest(root, &TestMessageFormatWithValist, "tsformat/cmsgtst/TestMessageFormatWithValist");
1158     addTest(root, &TestParseMessageWithValist, "tsformat/cmsgtst/TestParseMessageWithValist");
1159     addTest(root, &TestJ904, "tsformat/cmsgtst/TestJ904");
1160     addTest(root, &MessageLength, "tsformat/cmsgtst/MessageLength");
1161     addTest(root, &TestErrorChaining, "tsformat/cmsgtst/TestErrorChaining");
1162     addTest(root, &TestMsgFormatSelect, "tsformat/cmsgtst/TestMsgFormatSelect");
1163 }
1164 
1165 #endif /* #if !UCONFIG_NO_FORMATTING */
1166