]>
Commit | Line | Data |
---|---|---|
1 | /******************************************************************** | |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2004, International Business Machines Corporation and | |
4 | * others. All Rights Reserved. | |
5 | ********************************************************************/ | |
6 | /******************************************************************************** | |
7 | * | |
8 | * File CDATTST.C | |
9 | * | |
10 | * Modification History: | |
11 | * Name Description | |
12 | * Madhu Katragadda Creation | |
13 | ********************************************************************************* | |
14 | */ | |
15 | ||
16 | /* C API TEST FOR DATE FORMAT */ | |
17 | ||
18 | #include "unicode/utypes.h" | |
19 | ||
20 | #if !UCONFIG_NO_FORMATTING | |
21 | ||
22 | #include "unicode/uloc.h" | |
23 | #include "unicode/udat.h" | |
24 | #include "unicode/ucal.h" | |
25 | #include "unicode/unum.h" | |
26 | #include "unicode/ustring.h" | |
27 | #include "cintltst.h" | |
28 | #include "cdattst.h" | |
29 | #include "cformtst.h" | |
30 | #include "cmemory.h" | |
31 | ||
32 | #include <math.h> | |
33 | ||
34 | static void TestExtremeDates(void); | |
35 | ||
36 | #define LEN(a) (sizeof(a)/sizeof(a[0])) | |
37 | ||
38 | void addDateForTest(TestNode** root); | |
39 | ||
40 | #define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x) | |
41 | ||
42 | void addDateForTest(TestNode** root) | |
43 | { | |
44 | TESTCASE(TestDateFormat); | |
45 | TESTCASE(TestSymbols); | |
46 | TESTCASE(TestDateFormatCalendar); | |
47 | TESTCASE(TestExtremeDates); | |
48 | } | |
49 | /* Testing the DateFormat API */ | |
50 | static void TestDateFormat() | |
51 | { | |
52 | UDateFormat *def, *fr, *it, *de, *def1, *fr_pat; | |
53 | UDateFormat *any; | |
54 | UDateFormat *copy; | |
55 | UErrorCode status = U_ZERO_ERROR; | |
56 | UChar* result = NULL; | |
57 | const UCalendar *cal; | |
58 | const UNumberFormat *numformat1, *numformat2; | |
59 | UChar temp[30]; | |
60 | int32_t numlocales; | |
61 | UDate d1; | |
62 | int i; | |
63 | int32_t resultlength; | |
64 | int32_t resultlengthneeded; | |
65 | int32_t parsepos; | |
66 | UDate d = 837039928046.0; | |
67 | double num = -10456.37; | |
68 | /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z"; | |
69 | const char t[]="2/3/76 2:50 AM";*/ | |
70 | /*Testing udat_open() to open a dateformat */ | |
71 | ||
72 | ctest_setTimeZone(NULL, &status); | |
73 | ||
74 | log_verbose("\nTesting udat_open() with various parameters\n"); | |
75 | fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status); | |
76 | if(U_FAILURE(status)) | |
77 | { | |
78 | log_err("FAIL: error in creating the dateformat using full time style with french locale\n %s\n", | |
79 | myErrorName(status) ); | |
80 | return; | |
81 | } | |
82 | /* this is supposed to open default date format, but later on it treats it like it is "en_US" | |
83 | - very bad if you try to run the tests on machine where default locale is NOT "en_US" */ | |
84 | /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */ | |
85 | def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status); | |
86 | if(U_FAILURE(status)) | |
87 | { | |
88 | log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", | |
89 | myErrorName(status) ); | |
90 | return; | |
91 | } | |
92 | it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status); | |
93 | if(U_FAILURE(status)) | |
94 | { | |
95 | log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n", | |
96 | myErrorName(status) ); | |
97 | return; | |
98 | } | |
99 | de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status); | |
100 | if(U_FAILURE(status)) | |
101 | { | |
102 | log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n", | |
103 | myErrorName(status)); | |
104 | return; | |
105 | } | |
106 | /*creating a default dateformat */ | |
107 | def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status); | |
108 | if(U_FAILURE(status)) | |
109 | { | |
110 | log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", | |
111 | myErrorName(status) ); | |
112 | return; | |
113 | } | |
114 | ||
115 | ||
116 | /*Testing udat_getAvailable() and udat_countAvailable()*/ | |
117 | log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); | |
118 | numlocales=udat_countAvailable(); | |
119 | /* use something sensible w/o hardcoding the count */ | |
120 | if(numlocales < 0) | |
121 | log_data_err("FAIL: error in countAvailable\n"); | |
122 | log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales); | |
123 | ||
124 | for(i=0;i<numlocales;i++) { | |
125 | UErrorCode subStatus = U_ZERO_ERROR; | |
126 | log_verbose("Testing open of %s\n", udat_getAvailable(i)); | |
127 | any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &status); | |
128 | if(U_FAILURE(subStatus)) { | |
129 | log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus)); | |
130 | } | |
131 | udat_close(any); | |
132 | } | |
133 | ||
134 | /*Testing udat_clone()*/ | |
135 | log_verbose("\nTesting the udat_clone() function of date format\n"); | |
136 | copy=udat_clone(def, &status); | |
137 | if(U_FAILURE(status)){ | |
138 | log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) ); | |
139 | } | |
140 | /*if(def != copy) | |
141 | log_err("Error in udat_clone");*/ /*how should i check for equality???? */ | |
142 | ||
143 | /*Testing udat_format()*/ | |
144 | log_verbose("\nTesting the udat_format() function of date format\n"); | |
145 | u_uastrcpy(temp, "7/10/96 4:05 PM"); | |
146 | /*format using def */ | |
147 | resultlength=0; | |
148 | resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status); | |
149 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
150 | { | |
151 | status=U_ZERO_ERROR; | |
152 | resultlength=resultlengthneeded+1; | |
153 | if(result != NULL) { | |
154 | free(result); | |
155 | result = NULL; | |
156 | } | |
157 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
158 | udat_format(def, d, result, resultlength, NULL, &status); | |
159 | } | |
160 | if(U_FAILURE(status) || !result) | |
161 | { | |
162 | log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) ); | |
163 | return; | |
164 | } | |
165 | else | |
166 | log_verbose("PASS: formatting successful\n"); | |
167 | if(u_strcmp(result, temp)==0) | |
168 | log_verbose("PASS: Date Format for US locale successful uisng udat_format()\n"); | |
169 | else | |
170 | log_err("FAIL: Date Format for US locale failed using udat_format()\n"); | |
171 | /*format using fr */ | |
172 | ||
173 | u_unescape("10 juil. 96 16 h 05 HAP (\\u00c9UA)", temp, 30); | |
174 | if(result != NULL) { | |
175 | free(result); | |
176 | result = NULL; | |
177 | } | |
178 | result=myDateFormat(fr, d); | |
179 | if(u_strcmp(result, temp)==0) | |
180 | log_verbose("PASS: Date Format for french locale successful using udat_format()\n"); | |
181 | else | |
182 | log_data_err("FAIL: Date Format for french locale failed using udat_format(). Expected:\n"); | |
183 | /*format using it */ | |
184 | u_uastrcpy(temp, "10/lug/96 16:05:28"); | |
185 | ||
186 | if(u_strcmp(myDateFormat(it,d), temp)==0) | |
187 | log_verbose("PASS: Date Format for italian locale successful uisng udat_format()\n"); | |
188 | else | |
189 | log_data_err("FAIL: Date Format for italian locale failed using udat_format()\n"); | |
190 | ||
191 | ||
192 | /*Testing parsing using udat_parse()*/ | |
193 | log_verbose("\nTesting parsing using udat_parse()\n"); | |
194 | u_uastrcpy(temp,"2/3/76 2:50 AM"); | |
195 | parsepos=0; | |
196 | status=U_ZERO_ERROR; | |
197 | ||
198 | d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status); | |
199 | if(U_FAILURE(status)) | |
200 | { | |
201 | log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) ); | |
202 | } | |
203 | else | |
204 | log_verbose("PASS: parsing succesful\n"); | |
205 | /*format it back and check for equality */ | |
206 | ||
207 | ||
208 | if(u_strcmp(myDateFormat(def, d1),temp)!=0) | |
209 | log_err("FAIL: error in parsing\n"); | |
210 | ||
211 | /*Testing parsing using udat_parse()*/ | |
212 | log_verbose("\nTesting parsing using udat_parse()\n"); | |
213 | u_uastrcpy(temp,"2/Don't parse this part"); | |
214 | status=U_ZERO_ERROR; | |
215 | ||
216 | d1=udat_parse(def, temp, u_strlen(temp), NULL, &status); | |
217 | if(status != U_PARSE_ERROR) | |
218 | { | |
219 | log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n"); | |
220 | } | |
221 | else | |
222 | log_verbose("PASS: parsing succesful\n"); | |
223 | ||
224 | ||
225 | ||
226 | /*Testing udat_openPattern() */ | |
227 | status=U_ZERO_ERROR; | |
228 | log_verbose("\nTesting the udat_openPattern with a specified pattern\n"); | |
229 | /*for french locale */ | |
230 | fr_pat=udat_open(UDAT_IGNORE, UDAT_IGNORE,"fr_FR",NULL,0,temp, u_strlen(temp), &status); | |
231 | if(U_FAILURE(status)) | |
232 | { | |
233 | log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n", | |
234 | myErrorName(status) ); | |
235 | } | |
236 | else | |
237 | log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n"); | |
238 | ||
239 | ||
240 | /*Testing applyPattern and toPattern */ | |
241 | log_verbose("\nTesting applyPattern and toPattern()\n"); | |
242 | udat_applyPattern(def1, FALSE, temp, u_strlen(temp)); | |
243 | log_verbose("Extracting the pattern\n"); | |
244 | ||
245 | resultlength=0; | |
246 | resultlengthneeded=udat_toPattern(def1, FALSE, NULL, resultlength, &status); | |
247 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
248 | { | |
249 | status=U_ZERO_ERROR; | |
250 | resultlength=resultlengthneeded + 1; | |
251 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
252 | udat_toPattern(def1, FALSE, result, resultlength, &status); | |
253 | } | |
254 | if(U_FAILURE(status)) | |
255 | { | |
256 | log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", | |
257 | myErrorName(status) ); | |
258 | } | |
259 | if(u_strcmp(result, temp)!=0) | |
260 | log_err("FAIL: Error in extracting the pattern\n"); | |
261 | else | |
262 | log_verbose("PASS: applyPattern and toPattern work fine\n"); | |
263 | ||
264 | if(result != NULL) { | |
265 | free(result); | |
266 | result = NULL; | |
267 | } | |
268 | ||
269 | ||
270 | /*Testing getter and setter functions*/ | |
271 | /*isLenient and setLenient()*/ | |
272 | log_verbose("\nTesting the isLenient and setLenient properties\n"); | |
273 | udat_setLenient(fr, udat_isLenient(it)); | |
274 | if(udat_isLenient(fr) != udat_isLenient(it)) | |
275 | log_err("ERROR: setLenient() failed\n"); | |
276 | else | |
277 | log_verbose("PASS: setLenient() successful\n"); | |
278 | ||
279 | ||
280 | /*Test get2DigitYearStart set2DigitYearStart */ | |
281 | log_verbose("\nTesting the get and set 2DigitYearStart properties\n"); | |
282 | d1= udat_get2DigitYearStart(fr_pat,&status); | |
283 | if(U_FAILURE(status)) { | |
284 | log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) ); | |
285 | } | |
286 | status = U_ZERO_ERROR; | |
287 | udat_set2DigitYearStart(def1 ,d1, &status); | |
288 | if(U_FAILURE(status)) { | |
289 | log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); | |
290 | } | |
291 | if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status)) | |
292 | log_err("FAIL: error in set2DigitYearStart\n"); | |
293 | else | |
294 | log_verbose("PASS: set2DigitYearStart successful\n"); | |
295 | /*try setting it to another value */ | |
296 | udat_set2DigitYearStart(de, 2000.0, &status); | |
297 | if(U_FAILURE(status)){ | |
298 | log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); | |
299 | } | |
300 | if(udat_get2DigitYearStart(de, &status) != 2000) | |
301 | log_err("FAIL: error in set2DigitYearStart\n"); | |
302 | else | |
303 | log_verbose("PASS: set2DigitYearStart successful\n"); | |
304 | ||
305 | ||
306 | ||
307 | /*Test getNumberFormat() and setNumberFormat() */ | |
308 | log_verbose("\nTesting the get and set NumberFormat properties of date format\n"); | |
309 | numformat1=udat_getNumberFormat(fr_pat); | |
310 | udat_setNumberFormat(def1, numformat1); | |
311 | numformat2=udat_getNumberFormat(def1); | |
312 | if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) | |
313 | log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); | |
314 | else | |
315 | log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n"); | |
316 | ||
317 | /*try setting the number format to another format */ | |
318 | numformat1=udat_getNumberFormat(def); | |
319 | udat_setNumberFormat(def1, numformat1); | |
320 | numformat2=udat_getNumberFormat(def1); | |
321 | if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) | |
322 | log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); | |
323 | else | |
324 | log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n"); | |
325 | ||
326 | ||
327 | ||
328 | /*Test getCalendar and setCalendar*/ | |
329 | log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n"); | |
330 | cal=udat_getCalendar(fr_pat); | |
331 | ||
332 | ||
333 | udat_setCalendar(def1, cal); | |
334 | if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1))) | |
335 | log_err("FAIL: Error in setting and getting the calendar\n"); | |
336 | else | |
337 | log_verbose("PASS: getting and setting calendar successful\n"); | |
338 | ||
339 | if(result!=NULL) { | |
340 | free(result); | |
341 | } | |
342 | ||
343 | /*Closing the UDateForamt */ | |
344 | udat_close(def); | |
345 | udat_close(fr); | |
346 | udat_close(it); | |
347 | udat_close(de); | |
348 | udat_close(def1); | |
349 | udat_close(fr_pat); | |
350 | udat_close(copy); | |
351 | ||
352 | ctest_resetTimeZone(); | |
353 | } | |
354 | ||
355 | /*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/ | |
356 | static void TestSymbols() | |
357 | { | |
358 | UDateFormat *def, *fr; | |
359 | UErrorCode status = U_ZERO_ERROR; | |
360 | UChar *value=NULL; | |
361 | UChar *result = NULL; | |
362 | int32_t resultlength; | |
363 | int32_t resultlengthout; | |
364 | UChar *pattern; | |
365 | ||
366 | ||
367 | /*creating a dateformat with french locale */ | |
368 | log_verbose("\ncreating a date format with french locale\n"); | |
369 | fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL, 0, NULL, 0, &status); | |
370 | if(U_FAILURE(status)) | |
371 | { | |
372 | log_err("error in creating the dateformat using full time style with french locale\n %s\n", | |
373 | myErrorName(status) ); | |
374 | return; | |
375 | } | |
376 | /*creating a default dateformat */ | |
377 | log_verbose("\ncreating a date format with default locale\n"); | |
378 | /* this is supposed to open default date format, but later on it treats it like it is "en_US" | |
379 | - very bad if you try to run the tests on machine where default locale is NOT "en_US" */ | |
380 | /* def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */ | |
381 | def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status); | |
382 | if(U_FAILURE(status)) | |
383 | { | |
384 | log_err("error in creating the dateformat using short date and time style\n %s\n", | |
385 | myErrorName(status) ); | |
386 | return; | |
387 | } | |
388 | ||
389 | ||
390 | /*Testing countSymbols, getSymbols and setSymbols*/ | |
391 | log_verbose("\nTesting countSymbols\n"); | |
392 | /*since the month names has the last string empty and week names are 1 based 1.e first string in the weeknames array is empty */ | |
393 | if(udat_countSymbols(def, UDAT_ERAS)!=2 || udat_countSymbols(def, UDAT_MONTHS)!=12 || | |
394 | udat_countSymbols(def, UDAT_SHORT_MONTHS)!=12 || udat_countSymbols(def, UDAT_WEEKDAYS)!=8 || | |
395 | udat_countSymbols(def, UDAT_SHORT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_AM_PMS)!=2 || | |
396 | udat_countSymbols(def, UDAT_LOCALIZED_CHARS)!=1) | |
397 | { | |
398 | log_err("FAIL: error in udat_countSymbols\n"); | |
399 | } | |
400 | else | |
401 | log_verbose("PASS: udat_countSymbols() successful\n"); | |
402 | ||
403 | /*testing getSymbols*/ | |
404 | log_verbose("\nTesting getSymbols\n"); | |
405 | pattern=(UChar*)malloc(sizeof(UChar) * 10); | |
406 | u_uastrcpy(pattern, "jeudi"); | |
407 | resultlength=0; | |
408 | resultlengthout=udat_getSymbols(fr, UDAT_WEEKDAYS, 5 , NULL, resultlength, &status); | |
409 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
410 | { | |
411 | status=U_ZERO_ERROR; | |
412 | resultlength=resultlengthout+1; | |
413 | if(result != NULL) { | |
414 | free(result); | |
415 | result = NULL; | |
416 | } | |
417 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
418 | udat_getSymbols(fr, UDAT_WEEKDAYS, 5, result, resultlength, &status); | |
419 | ||
420 | } | |
421 | if(U_FAILURE(status)) | |
422 | { | |
423 | log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status) ); | |
424 | } | |
425 | else | |
426 | log_verbose("PASS: getSymbols succesful\n"); | |
427 | ||
428 | if(u_strcmp(result, pattern)==0) | |
429 | log_verbose("PASS: getSymbols retrieved the right value\n"); | |
430 | else | |
431 | log_data_err("FAIL: getSymbols retrieved the wrong value\n"); | |
432 | ||
433 | /*run series of tests to test getsymbols regressively*/ | |
434 | log_verbose("\nTesting getSymbols() regressively\n"); | |
435 | VerifygetSymbols(fr, UDAT_WEEKDAYS, 1, "dimanche"); | |
436 | VerifygetSymbols(def, UDAT_WEEKDAYS, 1, "Sunday"); | |
437 | VerifygetSymbols(fr, UDAT_SHORT_WEEKDAYS, 7, "sam."); | |
438 | VerifygetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Sat"); | |
439 | VerifygetSymbols(def, UDAT_MONTHS, 11, "December"); | |
440 | VerifygetSymbols(def, UDAT_MONTHS, 0, "January"); | |
441 | VerifygetSymbols(fr, UDAT_ERAS, 0, "av. J.-C."); | |
442 | VerifygetSymbols(def, UDAT_AM_PMS, 0, "AM"); | |
443 | VerifygetSymbols(def, UDAT_AM_PMS, 1, "PM"); | |
444 | VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv."); | |
445 | VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec"); | |
446 | VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZ"); | |
447 | ||
448 | ||
449 | if(result != NULL) { | |
450 | free(result); | |
451 | result = NULL; | |
452 | } | |
453 | free(pattern); | |
454 | ||
455 | log_verbose("\nTesting setSymbols\n"); | |
456 | /*applying the pattern so that setSymbolss works */ | |
457 | resultlength=0; | |
458 | resultlengthout=udat_toPattern(fr, FALSE, NULL, resultlength, &status); | |
459 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
460 | { | |
461 | status=U_ZERO_ERROR; | |
462 | resultlength=resultlengthout + 1; | |
463 | pattern=(UChar*)malloc(sizeof(UChar) * resultlength); | |
464 | udat_toPattern(fr, FALSE, pattern, resultlength, &status); | |
465 | } | |
466 | if(U_FAILURE(status)) | |
467 | { | |
468 | log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", | |
469 | myErrorName(status) ); | |
470 | } | |
471 | ||
472 | udat_applyPattern(def, FALSE, pattern, u_strlen(pattern)); | |
473 | resultlength=0; | |
474 | resultlengthout=udat_toPattern(def, FALSE, NULL, resultlength,&status); | |
475 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
476 | { | |
477 | status=U_ZERO_ERROR; | |
478 | resultlength=resultlengthout + 1; | |
479 | if(result != NULL) { | |
480 | free(result); | |
481 | result = NULL; | |
482 | } | |
483 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
484 | udat_toPattern(fr, FALSE,result, resultlength, &status); | |
485 | } | |
486 | if(U_FAILURE(status)) | |
487 | { | |
488 | log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", | |
489 | myErrorName(status) ); | |
490 | } | |
491 | if(u_strcmp(result, pattern)==0) | |
492 | log_verbose("Pattern applied properly\n"); | |
493 | else | |
494 | log_err("pattern could not be applied properly\n"); | |
495 | ||
496 | free(pattern); | |
497 | /*testing set symbols */ | |
498 | resultlength=0; | |
499 | resultlengthout=udat_getSymbols(fr, UDAT_MONTHS, 11 , NULL, resultlength, &status); | |
500 | if(status==U_BUFFER_OVERFLOW_ERROR){ | |
501 | status=U_ZERO_ERROR; | |
502 | resultlength=resultlengthout+1; | |
503 | if(result != NULL) { | |
504 | free(result); | |
505 | result = NULL; | |
506 | } | |
507 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
508 | udat_getSymbols(fr, UDAT_MONTHS, 11, result, resultlength, &status); | |
509 | ||
510 | } | |
511 | if(U_FAILURE(status)) | |
512 | log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) ); | |
513 | resultlength=resultlengthout+1; | |
514 | ||
515 | udat_setSymbols(def, UDAT_MONTHS, 11, result, resultlength, &status); | |
516 | if(U_FAILURE(status)) | |
517 | { | |
518 | log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) ); | |
519 | } | |
520 | else | |
521 | log_verbose("PASS: SetSymbols successful\n"); | |
522 | ||
523 | resultlength=0; | |
524 | resultlengthout=udat_getSymbols(def, UDAT_MONTHS, 11, NULL, resultlength, &status); | |
525 | if(status==U_BUFFER_OVERFLOW_ERROR){ | |
526 | status=U_ZERO_ERROR; | |
527 | resultlength=resultlengthout+1; | |
528 | value=(UChar*)malloc(sizeof(UChar) * resultlength); | |
529 | udat_getSymbols(def, UDAT_MONTHS, 11, value, resultlength, &status); | |
530 | } | |
531 | if(U_FAILURE(status)) | |
532 | log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n"); | |
533 | ||
534 | if(u_strcmp(result, value)!=0) | |
535 | log_data_err("FAIL: Error in settting and getting symbols\n"); | |
536 | else | |
537 | log_verbose("PASS: setSymbols successful\n"); | |
538 | ||
539 | ||
540 | /*run series of tests to test setSymbols regressively*/ | |
541 | log_verbose("\nTesting setSymbols regressively\n"); | |
542 | VerifysetSymbols(def, UDAT_WEEKDAYS, 1, "Sundayweek"); | |
543 | VerifysetSymbols(def, UDAT_ERAS, 0, "BeforeChrist"); | |
544 | VerifysetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Satweek"); | |
545 | VerifysetSymbols(fr, UDAT_MONTHS, 11, "december"); | |
546 | VerifysetSymbols(fr, UDAT_SHORT_MONTHS, 0, "Jan"); | |
547 | ||
548 | ||
549 | /*run series of tests to test get and setSymbols regressively*/ | |
550 | log_verbose("\nTesting get and set symbols regressively\n"); | |
551 | VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 1); | |
552 | VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 7); | |
553 | VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 1); | |
554 | VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 7); | |
555 | VerifygetsetSymbols(fr, def, UDAT_MONTHS, 0); | |
556 | VerifygetsetSymbols(fr, def, UDAT_SHORT_MONTHS, 0); | |
557 | VerifygetsetSymbols(fr, def, UDAT_ERAS,1); | |
558 | VerifygetsetSymbols(fr, def, UDAT_LOCALIZED_CHARS, 0); | |
559 | VerifygetsetSymbols(fr, def, UDAT_AM_PMS, 1); | |
560 | ||
561 | ||
562 | /*closing*/ | |
563 | ||
564 | udat_close(fr); | |
565 | udat_close(def); | |
566 | if(result != NULL) { | |
567 | free(result); | |
568 | result = NULL; | |
569 | } | |
570 | free(value); | |
571 | ||
572 | } | |
573 | ||
574 | /** | |
575 | * Test DateFormat(Calendar) API | |
576 | */ | |
577 | static void TestDateFormatCalendar() { | |
578 | UDateFormat *date=0, *time=0, *full=0; | |
579 | UCalendar *cal=0; | |
580 | UChar buf[256]; | |
581 | char cbuf[256]; | |
582 | int32_t pos; | |
583 | UDate when; | |
584 | UErrorCode ec = U_ZERO_ERROR; | |
585 | ||
586 | ctest_setTimeZone(NULL, &ec); | |
587 | ||
588 | /* Create a formatter for date fields. */ | |
589 | date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec); | |
590 | if (U_FAILURE(ec)) { | |
591 | log_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s\n", | |
592 | u_errorName(ec)); | |
593 | goto FAIL; | |
594 | } | |
595 | ||
596 | /* Create a formatter for time fields. */ | |
597 | time = udat_open(UDAT_SHORT, UDAT_NONE, "en_US", NULL, 0, NULL, 0, &ec); | |
598 | if (U_FAILURE(ec)) { | |
599 | log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n", | |
600 | u_errorName(ec)); | |
601 | goto FAIL; | |
602 | } | |
603 | ||
604 | /* Create a full format for output */ | |
605 | full = udat_open(UDAT_FULL, UDAT_FULL, "en_US", NULL, 0, NULL, 0, &ec); | |
606 | if (U_FAILURE(ec)) { | |
607 | log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n", | |
608 | u_errorName(ec)); | |
609 | goto FAIL; | |
610 | } | |
611 | ||
612 | /* Create a calendar */ | |
613 | cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &ec); | |
614 | if (U_FAILURE(ec)) { | |
615 | log_err("FAIL: ucal_open(en_US) failed with %s\n", | |
616 | u_errorName(ec)); | |
617 | goto FAIL; | |
618 | } | |
619 | ||
620 | /* Parse the date */ | |
621 | ucal_clear(cal); | |
622 | u_uastrcpy(buf, "4/5/2001"); | |
623 | pos = 0; | |
624 | udat_parseCalendar(date, cal, buf, -1, &pos, &ec); | |
625 | if (U_FAILURE(ec)) { | |
626 | log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n", | |
627 | pos, u_errorName(ec)); | |
628 | goto FAIL; | |
629 | } | |
630 | ||
631 | /* Parse the time */ | |
632 | u_uastrcpy(buf, "5:45 PM"); | |
633 | pos = 0; | |
634 | udat_parseCalendar(time, cal, buf, -1, &pos, &ec); | |
635 | if (U_FAILURE(ec)) { | |
636 | log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n", | |
637 | pos, u_errorName(ec)); | |
638 | goto FAIL; | |
639 | } | |
640 | ||
641 | /* Check result */ | |
642 | when = ucal_getMillis(cal, &ec); | |
643 | if (U_FAILURE(ec)) { | |
644 | log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec)); | |
645 | goto FAIL; | |
646 | } | |
647 | udat_format(full, when, buf, sizeof(buf), NULL, &ec); | |
648 | if (U_FAILURE(ec)) { | |
649 | log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec)); | |
650 | goto FAIL; | |
651 | } | |
652 | u_austrcpy(cbuf, buf); | |
653 | /* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */ | |
654 | if (when == 986517900000.0) { | |
655 | log_verbose("Ok: Parsed result: %s\n", cbuf); | |
656 | } else { | |
657 | log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf); | |
658 | } | |
659 | ||
660 | FAIL: | |
661 | udat_close(date); | |
662 | udat_close(time); | |
663 | udat_close(full); | |
664 | ucal_close(cal); | |
665 | ||
666 | ctest_resetTimeZone(); | |
667 | } | |
668 | ||
669 | /*INTERNAL FUNCTIONS USED*/ | |
670 | static void VerifygetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected) | |
671 | { | |
672 | UChar *pattern=NULL; | |
673 | UErrorCode status = U_ZERO_ERROR; | |
674 | UChar *result=NULL; | |
675 | int32_t resultlength, resultlengthout; | |
676 | ||
677 | ||
678 | pattern=(UChar*)malloc(sizeof(UChar) * (strlen(expected)+1)); | |
679 | u_uastrcpy(pattern, expected); | |
680 | resultlength=0; | |
681 | resultlengthout=udat_getSymbols(datfor, type, index , NULL, resultlength, &status); | |
682 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
683 | { | |
684 | status=U_ZERO_ERROR; | |
685 | resultlength=resultlengthout+1; | |
686 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
687 | udat_getSymbols(datfor, type, index, result, resultlength, &status); | |
688 | ||
689 | } | |
690 | if(U_FAILURE(status)) | |
691 | { | |
692 | log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status) ); | |
693 | return; | |
694 | } | |
695 | if(u_strcmp(result, pattern)==0) | |
696 | log_verbose("PASS: getSymbols retrieved the right value\n"); | |
697 | else{ | |
698 | log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", austrdup(pattern), | |
699 | austrdup(result) ); | |
700 | } | |
701 | free(result); | |
702 | free(pattern); | |
703 | } | |
704 | ||
705 | static void VerifysetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t index, const char* expected) | |
706 | { | |
707 | UChar *result=NULL; | |
708 | UChar *value=NULL; | |
709 | int32_t resultlength, resultlengthout; | |
710 | UErrorCode status = U_ZERO_ERROR; | |
711 | ||
712 | value=(UChar*)malloc(sizeof(UChar) * (strlen(expected) + 1)); | |
713 | u_uastrcpy(value, expected); | |
714 | udat_setSymbols(datfor, type, index, value, u_strlen(value), &status); | |
715 | if(U_FAILURE(status)) | |
716 | { | |
717 | log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status) ); | |
718 | return; | |
719 | } | |
720 | ||
721 | resultlength=0; | |
722 | resultlengthout=udat_getSymbols(datfor, type, index, NULL, resultlength, &status); | |
723 | if(status==U_BUFFER_OVERFLOW_ERROR){ | |
724 | status=U_ZERO_ERROR; | |
725 | resultlength=resultlengthout+1; | |
726 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
727 | udat_getSymbols(datfor, type, index, result, resultlength, &status); | |
728 | } | |
729 | if(U_FAILURE(status)){ | |
730 | log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n", | |
731 | myErrorName(status) ); | |
732 | return; | |
733 | } | |
734 | ||
735 | if(u_strcmp(result, value)!=0){ | |
736 | log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(value), | |
737 | austrdup(result) ); | |
738 | } | |
739 | else | |
740 | log_verbose("PASS: setSymbols successful\n"); | |
741 | ||
742 | free(value); | |
743 | free(result); | |
744 | } | |
745 | ||
746 | ||
747 | static void VerifygetsetSymbols(UDateFormat* from, UDateFormat* to, UDateFormatSymbolType type, int32_t index) | |
748 | { | |
749 | UChar *result=NULL; | |
750 | UChar *value=NULL; | |
751 | int32_t resultlength, resultlengthout; | |
752 | UErrorCode status = U_ZERO_ERROR; | |
753 | ||
754 | resultlength=0; | |
755 | resultlengthout=udat_getSymbols(from, type, index , NULL, resultlength, &status); | |
756 | if(status==U_BUFFER_OVERFLOW_ERROR){ | |
757 | status=U_ZERO_ERROR; | |
758 | resultlength=resultlengthout+1; | |
759 | result=(UChar*)malloc(sizeof(UChar) * resultlength); | |
760 | udat_getSymbols(from, type, index, result, resultlength, &status); | |
761 | } | |
762 | if(U_FAILURE(status)){ | |
763 | log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) ); | |
764 | return; | |
765 | } | |
766 | ||
767 | resultlength=resultlengthout+1; | |
768 | udat_setSymbols(to, type, index, result, resultlength, &status); | |
769 | if(U_FAILURE(status)) | |
770 | { | |
771 | log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) ); | |
772 | return; | |
773 | } | |
774 | ||
775 | resultlength=0; | |
776 | resultlengthout=udat_getSymbols(to, type, index, NULL, resultlength, &status); | |
777 | if(status==U_BUFFER_OVERFLOW_ERROR){ | |
778 | status=U_ZERO_ERROR; | |
779 | resultlength=resultlengthout+1; | |
780 | value=(UChar*)malloc(sizeof(UChar) * resultlength); | |
781 | udat_getSymbols(to, type, index, value, resultlength, &status); | |
782 | } | |
783 | if(U_FAILURE(status)){ | |
784 | log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n", | |
785 | myErrorName(status) ); | |
786 | return; | |
787 | } | |
788 | ||
789 | if(u_strcmp(result, value)!=0){ | |
790 | log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result), | |
791 | austrdup(value) ); | |
792 | } | |
793 | else | |
794 | log_verbose("PASS: setSymbols successful\n"); | |
795 | ||
796 | free(value); | |
797 | free(result); | |
798 | } | |
799 | ||
800 | ||
801 | static UChar* myNumformat(const UNumberFormat* numfor, double d) | |
802 | { | |
803 | UChar *result2=NULL; | |
804 | int32_t resultlength, resultlengthneeded; | |
805 | UErrorCode status = U_ZERO_ERROR; | |
806 | ||
807 | resultlength=0; | |
808 | resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status); | |
809 | if(status==U_BUFFER_OVERFLOW_ERROR) | |
810 | { | |
811 | status=U_ZERO_ERROR; | |
812 | resultlength=resultlengthneeded+1; | |
813 | /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */ | |
814 | result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/ | |
815 | unum_formatDouble(numfor, d, result2, resultlength, NULL, &status); | |
816 | } | |
817 | if(U_FAILURE(status)) | |
818 | { | |
819 | log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) ); | |
820 | return 0; | |
821 | } | |
822 | ||
823 | return result2; | |
824 | } | |
825 | ||
826 | /** | |
827 | * The search depth for TestExtremeDates. The total number of | |
828 | * dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1. | |
829 | */ | |
830 | #define EXTREME_DATES_DEPTH 8 | |
831 | ||
832 | /** | |
833 | * Support for TestExtremeDates (below). | |
834 | * | |
835 | * Test a single date to see whether udat_format handles it properly. | |
836 | */ | |
837 | static UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date, | |
838 | UChar* buf, int32_t buflen, char* cbuf, | |
839 | UErrorCode* ec) { | |
840 | int32_t len = udat_format(fmt, date, buf, buflen, 0, ec); | |
841 | if (!assertSuccess("udat_format", ec)) return FALSE; | |
842 | u_austrncpy(cbuf, buf, buflen); | |
843 | if (len < 4) { | |
844 | log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf); | |
845 | } else { | |
846 | log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf); | |
847 | } | |
848 | return TRUE; | |
849 | } | |
850 | ||
851 | /** | |
852 | * Support for TestExtremeDates (below). | |
853 | * | |
854 | * Recursively test between 'small' and 'large', up to the depth | |
855 | * limit specified by EXTREME_DATES_DEPTH. | |
856 | */ | |
857 | static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large, | |
858 | UChar* buf, int32_t buflen, char* cbuf, | |
859 | int32_t count, | |
860 | UErrorCode* ec) { | |
861 | /* Logarithmic midpoint; see below */ | |
862 | UDate mid = (UDate) exp((log(small) + log(large)) / 2); | |
863 | if (count == EXTREME_DATES_DEPTH) { | |
864 | return TRUE; | |
865 | } | |
866 | return | |
867 | _aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) && | |
868 | _aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) && | |
869 | _aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec); | |
870 | } | |
871 | ||
872 | /** | |
873 | * http://www.jtcsv.com/cgibin/icu-bugs?findid=3659 | |
874 | * | |
875 | * For certain large dates, udat_format crashes on MacOS. This test | |
876 | * attempts to reproduce this problem by doing a recursive logarithmic* | |
877 | * binary search of a predefined interval (from 'small' to 'large'). | |
878 | * | |
879 | * The limit of the search is given by EXTREME_DATES_DEPTH, above. | |
880 | * | |
881 | * *The search has to be logarithmic, not linear. A linear search of the | |
882 | * range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and | |
883 | * 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5 | |
884 | * and 10^22.5, etc. | |
885 | */ | |
886 | static void TestExtremeDates() { | |
887 | UDateFormat *fmt; | |
888 | UErrorCode ec; | |
889 | UChar buf[256]; | |
890 | char cbuf[256]; | |
891 | const double small = 1000; /* 1 sec */ | |
892 | const double large = 1e+30; /* well beyond usable UDate range */ | |
893 | ||
894 | /* There is no need to test larger values from 1e+30 to 1e+300; | |
895 | the failures occur around 1e+27, and never above 1e+30. */ | |
896 | ||
897 | ec = U_ZERO_ERROR; | |
898 | fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US", | |
899 | 0, 0, 0, 0, &ec); | |
900 | if (!assertSuccess("udat_open", &ec)) return; | |
901 | ||
902 | _aux2ExtremeDates(fmt, small, large, buf, LEN(buf), cbuf, 0, &ec); | |
903 | ||
904 | udat_close(fmt); | |
905 | } | |
906 | ||
907 | #endif /* #if !UCONFIG_NO_FORMATTING */ |