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