1 /********************************************************************
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *********************************************************************************
16 /* C API TEST FOR DATE FORMAT */
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_FORMATTING
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"
34 static void TestExtremeDates(void);
35 static void TestAllLocales(void);
37 #define LEN(a) (sizeof(a)/sizeof(a[0]))
39 void addDateForTest(TestNode
** root
);
41 #define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x)
43 void addDateForTest(TestNode
** root
)
45 TESTCASE(TestDateFormat
);
46 TESTCASE(TestSymbols
);
47 TESTCASE(TestDateFormatCalendar
);
48 TESTCASE(TestExtremeDates
);
49 TESTCASE(TestAllLocales
);
51 /* Testing the DateFormat API */
52 static void TestDateFormat()
54 UDateFormat
*def
, *fr
, *it
, *de
, *def1
, *fr_pat
;
57 UErrorCode status
= U_ZERO_ERROR
;
60 const UNumberFormat
*numformat1
, *numformat2
;
66 int32_t resultlengthneeded
;
68 UDate d
= 837039928046.0;
69 double num
= -10456.37;
70 /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z";
71 const char t[]="2/3/76 2:50 AM";*/
72 /*Testing udat_open() to open a dateformat */
74 ctest_setTimeZone(NULL
, &status
);
76 log_verbose("\nTesting udat_open() with various parameters\n");
77 fr
= udat_open(UDAT_FULL
, UDAT_DEFAULT
, "fr_FR", NULL
,0, NULL
, 0,&status
);
80 log_err("FAIL: error in creating the dateformat using full time style with french locale\n %s\n",
81 myErrorName(status
) );
84 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
85 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
86 /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */
87 def
= udat_open(UDAT_SHORT
, UDAT_SHORT
, "en_US", NULL
, 0,NULL
, 0, &status
);
90 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
91 myErrorName(status
) );
94 it
= udat_open(UDAT_DEFAULT
, UDAT_MEDIUM
, "it_IT", NULL
, 0, NULL
, 0,&status
);
97 log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n",
98 myErrorName(status
) );
101 de
= udat_open(UDAT_LONG
, UDAT_LONG
, "de_DE", NULL
, 0, NULL
, 0,&status
);
102 if(U_FAILURE(status
))
104 log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n",
105 myErrorName(status
));
108 /*creating a default dateformat */
109 def1
= udat_open(UDAT_SHORT
, UDAT_SHORT
, NULL
, NULL
, 0,NULL
, 0, &status
);
110 if(U_FAILURE(status
))
112 log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n",
113 myErrorName(status
) );
118 /*Testing udat_getAvailable() and udat_countAvailable()*/
119 log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
120 numlocales
=udat_countAvailable();
121 /* use something sensible w/o hardcoding the count */
123 log_data_err("FAIL: error in countAvailable\n");
124 log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales
);
126 for(i
=0;i
<numlocales
;i
++) {
127 UErrorCode subStatus
= U_ZERO_ERROR
;
128 log_verbose("Testing open of %s\n", udat_getAvailable(i
));
129 any
= udat_open(UDAT_SHORT
, UDAT_SHORT
, udat_getAvailable(i
), NULL
,0, NULL
, 0, &subStatus
);
130 if(U_FAILURE(subStatus
)) {
131 log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i
), i
, u_errorName(subStatus
));
136 /*Testing udat_clone()*/
137 log_verbose("\nTesting the udat_clone() function of date format\n");
138 copy
=udat_clone(def
, &status
);
139 if(U_FAILURE(status
)){
140 log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status
) );
143 log_err("Error in udat_clone");*/ /*how should i check for equality???? */
145 /*Testing udat_format()*/
146 log_verbose("\nTesting the udat_format() function of date format\n");
147 u_uastrcpy(temp
, "7/10/96 4:05 PM");
148 /*format using def */
150 resultlengthneeded
=udat_format(def
, d
, NULL
, resultlength
, NULL
, &status
);
151 if(status
==U_BUFFER_OVERFLOW_ERROR
)
154 resultlength
=resultlengthneeded
+1;
159 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
160 udat_format(def
, d
, result
, resultlength
, NULL
, &status
);
162 if(U_FAILURE(status
) || !result
)
164 log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status
) );
168 log_verbose("PASS: formatting successful\n");
169 if(u_strcmp(result
, temp
)==0)
170 log_verbose("PASS: Date Format for US locale successful uisng udat_format()\n");
172 log_err("FAIL: Date Format for US locale failed using udat_format()\n");
175 u_unescape("10 juil. 96 16 h 05 HAP (\\u00c9UA)", temp
, 30);
180 result
=myDateFormat(fr
, d
);
181 if(u_strcmp(result
, temp
)==0)
182 log_verbose("PASS: Date Format for french locale successful using udat_format()\n");
184 log_data_err("FAIL: Date Format for french locale failed using udat_format(). Expected:\n");
186 u_uastrcpy(temp
, "10/lug/96 16:05:28");
188 if(u_strcmp(myDateFormat(it
,d
), temp
)==0)
189 log_verbose("PASS: Date Format for italian locale successful uisng udat_format()\n");
191 log_data_err("FAIL: Date Format for italian locale failed using udat_format()\n");
194 /*Testing parsing using udat_parse()*/
195 log_verbose("\nTesting parsing using udat_parse()\n");
196 u_uastrcpy(temp
,"2/3/76 2:50 AM");
200 d1
=udat_parse(def
, temp
, u_strlen(temp
), &parsepos
, &status
);
201 if(U_FAILURE(status
))
203 log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status
) );
206 log_verbose("PASS: parsing succesful\n");
207 /*format it back and check for equality */
210 if(u_strcmp(myDateFormat(def
, d1
),temp
)!=0)
211 log_err("FAIL: error in parsing\n");
213 /*Testing parsing using udat_parse()*/
214 log_verbose("\nTesting parsing using udat_parse()\n");
215 u_uastrcpy(temp
,"2/Don't parse this part");
218 d1
=udat_parse(def
, temp
, u_strlen(temp
), NULL
, &status
);
219 if(status
!= U_PARSE_ERROR
)
221 log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n");
224 log_verbose("PASS: parsing succesful\n");
228 /*Testing udat_openPattern() */
230 log_verbose("\nTesting the udat_openPattern with a specified pattern\n");
231 /*for french locale */
232 fr_pat
=udat_open(UDAT_IGNORE
, UDAT_IGNORE
,"fr_FR",NULL
,0,temp
, u_strlen(temp
), &status
);
233 if(U_FAILURE(status
))
235 log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n",
236 myErrorName(status
) );
239 log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n");
242 /*Testing applyPattern and toPattern */
243 log_verbose("\nTesting applyPattern and toPattern()\n");
244 udat_applyPattern(def1
, FALSE
, temp
, u_strlen(temp
));
245 log_verbose("Extracting the pattern\n");
248 resultlengthneeded
=udat_toPattern(def1
, FALSE
, NULL
, resultlength
, &status
);
249 if(status
==U_BUFFER_OVERFLOW_ERROR
)
252 resultlength
=resultlengthneeded
+ 1;
253 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
254 udat_toPattern(def1
, FALSE
, result
, resultlength
, &status
);
256 if(U_FAILURE(status
))
258 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
259 myErrorName(status
) );
261 if(u_strcmp(result
, temp
)!=0)
262 log_err("FAIL: Error in extracting the pattern\n");
264 log_verbose("PASS: applyPattern and toPattern work fine\n");
272 /*Testing getter and setter functions*/
273 /*isLenient and setLenient()*/
274 log_verbose("\nTesting the isLenient and setLenient properties\n");
275 udat_setLenient(fr
, udat_isLenient(it
));
276 if(udat_isLenient(fr
) != udat_isLenient(it
))
277 log_err("ERROR: setLenient() failed\n");
279 log_verbose("PASS: setLenient() successful\n");
282 /*Test get2DigitYearStart set2DigitYearStart */
283 log_verbose("\nTesting the get and set 2DigitYearStart properties\n");
284 d1
= udat_get2DigitYearStart(fr_pat
,&status
);
285 if(U_FAILURE(status
)) {
286 log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status
) );
288 status
= U_ZERO_ERROR
;
289 udat_set2DigitYearStart(def1
,d1
, &status
);
290 if(U_FAILURE(status
)) {
291 log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status
) );
293 if(udat_get2DigitYearStart(fr_pat
, &status
) != udat_get2DigitYearStart(def1
, &status
))
294 log_err("FAIL: error in set2DigitYearStart\n");
296 log_verbose("PASS: set2DigitYearStart successful\n");
297 /*try setting it to another value */
298 udat_set2DigitYearStart(de
, 2000.0, &status
);
299 if(U_FAILURE(status
)){
300 log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status
) );
302 if(udat_get2DigitYearStart(de
, &status
) != 2000)
303 log_err("FAIL: error in set2DigitYearStart\n");
305 log_verbose("PASS: set2DigitYearStart successful\n");
309 /*Test getNumberFormat() and setNumberFormat() */
310 log_verbose("\nTesting the get and set NumberFormat properties of date format\n");
311 numformat1
=udat_getNumberFormat(fr_pat
);
312 udat_setNumberFormat(def1
, numformat1
);
313 numformat2
=udat_getNumberFormat(def1
);
314 if(u_strcmp(myNumformat(numformat1
, num
), myNumformat(numformat2
, num
)) !=0)
315 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
317 log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n");
319 /*try setting the number format to another format */
320 numformat1
=udat_getNumberFormat(def
);
321 udat_setNumberFormat(def1
, numformat1
);
322 numformat2
=udat_getNumberFormat(def1
);
323 if(u_strcmp(myNumformat(numformat1
, num
), myNumformat(numformat2
, num
)) !=0)
324 log_err("FAIL: error in setNumberFormat or getNumberFormat()\n");
326 log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n");
330 /*Test getCalendar and setCalendar*/
331 log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n");
332 cal
=udat_getCalendar(fr_pat
);
335 udat_setCalendar(def1
, cal
);
336 if(!ucal_equivalentTo(udat_getCalendar(fr_pat
), udat_getCalendar(def1
)))
337 log_err("FAIL: Error in setting and getting the calendar\n");
339 log_verbose("PASS: getting and setting calendar successful\n");
345 /*Closing the UDateForamt */
354 ctest_resetTimeZone();
357 /*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/
358 static void TestSymbols()
360 UDateFormat
*def
, *fr
;
361 UErrorCode status
= U_ZERO_ERROR
;
363 UChar
*result
= NULL
;
364 int32_t resultlength
;
365 int32_t resultlengthout
;
369 /*creating a dateformat with french locale */
370 log_verbose("\ncreating a date format with french locale\n");
371 fr
= udat_open(UDAT_FULL
, UDAT_DEFAULT
, "fr_FR", NULL
, 0, NULL
, 0, &status
);
372 if(U_FAILURE(status
))
374 log_err("error in creating the dateformat using full time style with french locale\n %s\n",
375 myErrorName(status
) );
378 /*creating a default dateformat */
379 log_verbose("\ncreating a date format with default locale\n");
380 /* this is supposed to open default date format, but later on it treats it like it is "en_US"
381 - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
382 /* def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */
383 def
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,"en_US", NULL
, 0, NULL
, 0, &status
);
384 if(U_FAILURE(status
))
386 log_err("error in creating the dateformat using short date and time style\n %s\n",
387 myErrorName(status
) );
392 /*Testing countSymbols, getSymbols and setSymbols*/
393 log_verbose("\nTesting countSymbols\n");
394 /*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 */
395 if(udat_countSymbols(def
, UDAT_ERAS
)!=2 || udat_countSymbols(def
, UDAT_MONTHS
)!=12 ||
396 udat_countSymbols(def
, UDAT_SHORT_MONTHS
)!=12 || udat_countSymbols(def
, UDAT_WEEKDAYS
)!=8 ||
397 udat_countSymbols(def
, UDAT_SHORT_WEEKDAYS
)!=8 || udat_countSymbols(def
, UDAT_AM_PMS
)!=2 ||
398 udat_countSymbols(def
, UDAT_QUARTERS
) != 4 || udat_countSymbols(def
, UDAT_SHORT_QUARTERS
) != 4 ||
399 udat_countSymbols(def
, UDAT_LOCALIZED_CHARS
)!=1)
401 log_err("FAIL: error in udat_countSymbols\n");
404 log_verbose("PASS: udat_countSymbols() successful\n");
406 /*testing getSymbols*/
407 log_verbose("\nTesting getSymbols\n");
408 pattern
=(UChar
*)malloc(sizeof(UChar
) * 10);
409 u_uastrcpy(pattern
, "jeudi");
411 resultlengthout
=udat_getSymbols(fr
, UDAT_WEEKDAYS
, 5 , NULL
, resultlength
, &status
);
412 if(status
==U_BUFFER_OVERFLOW_ERROR
)
415 resultlength
=resultlengthout
+1;
420 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
421 udat_getSymbols(fr
, UDAT_WEEKDAYS
, 5, result
, resultlength
, &status
);
424 if(U_FAILURE(status
))
426 log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status
) );
429 log_verbose("PASS: getSymbols succesful\n");
431 if(u_strcmp(result
, pattern
)==0)
432 log_verbose("PASS: getSymbols retrieved the right value\n");
434 log_data_err("FAIL: getSymbols retrieved the wrong value\n");
436 /*run series of tests to test getsymbols regressively*/
437 log_verbose("\nTesting getSymbols() regressively\n");
438 VerifygetSymbols(fr
, UDAT_WEEKDAYS
, 1, "dimanche");
439 VerifygetSymbols(def
, UDAT_WEEKDAYS
, 1, "Sunday");
440 VerifygetSymbols(fr
, UDAT_SHORT_WEEKDAYS
, 7, "sam.");
441 VerifygetSymbols(def
, UDAT_SHORT_WEEKDAYS
, 7, "Sat");
442 VerifygetSymbols(def
, UDAT_MONTHS
, 11, "December");
443 VerifygetSymbols(def
, UDAT_MONTHS
, 0, "January");
444 VerifygetSymbols(fr
, UDAT_ERAS
, 0, "av. J.-C.");
445 VerifygetSymbols(def
, UDAT_AM_PMS
, 0, "AM");
446 VerifygetSymbols(def
, UDAT_AM_PMS
, 1, "PM");
447 VerifygetSymbols(fr
, UDAT_SHORT_MONTHS
, 0, "janv.");
448 VerifygetSymbols(def
, UDAT_SHORT_MONTHS
, 11, "Dec");
449 VerifygetSymbols(fr
, UDAT_QUARTERS
, 0, "1er trimestre");
450 VerifygetSymbols(def
, UDAT_QUARTERS
, 3, "4th quarter");
451 VerifygetSymbols(fr
, UDAT_SHORT_QUARTERS
, 1, "T2");
452 VerifygetSymbols(def
, UDAT_SHORT_QUARTERS
, 2, "Q3");
453 VerifygetSymbols(def
,UDAT_LOCALIZED_CHARS
, 0, "GyMdkHmsSEDFwWahKzYeugAZvcL");
462 log_verbose("\nTesting setSymbols\n");
463 /*applying the pattern so that setSymbolss works */
465 resultlengthout
=udat_toPattern(fr
, FALSE
, NULL
, resultlength
, &status
);
466 if(status
==U_BUFFER_OVERFLOW_ERROR
)
469 resultlength
=resultlengthout
+ 1;
470 pattern
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
471 udat_toPattern(fr
, FALSE
, pattern
, resultlength
, &status
);
473 if(U_FAILURE(status
))
475 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
476 myErrorName(status
) );
479 udat_applyPattern(def
, FALSE
, pattern
, u_strlen(pattern
));
481 resultlengthout
=udat_toPattern(def
, FALSE
, NULL
, resultlength
,&status
);
482 if(status
==U_BUFFER_OVERFLOW_ERROR
)
485 resultlength
=resultlengthout
+ 1;
490 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
491 udat_toPattern(fr
, FALSE
,result
, resultlength
, &status
);
493 if(U_FAILURE(status
))
495 log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n",
496 myErrorName(status
) );
498 if(u_strcmp(result
, pattern
)==0)
499 log_verbose("Pattern applied properly\n");
501 log_err("pattern could not be applied properly\n");
504 /*testing set symbols */
506 resultlengthout
=udat_getSymbols(fr
, UDAT_MONTHS
, 11 , NULL
, resultlength
, &status
);
507 if(status
==U_BUFFER_OVERFLOW_ERROR
){
509 resultlength
=resultlengthout
+1;
514 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
515 udat_getSymbols(fr
, UDAT_MONTHS
, 11, result
, resultlength
, &status
);
518 if(U_FAILURE(status
))
519 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status
) );
520 resultlength
=resultlengthout
+1;
522 udat_setSymbols(def
, UDAT_MONTHS
, 11, result
, resultlength
, &status
);
523 if(U_FAILURE(status
))
525 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status
) );
528 log_verbose("PASS: SetSymbols successful\n");
531 resultlengthout
=udat_getSymbols(def
, UDAT_MONTHS
, 11, NULL
, resultlength
, &status
);
532 if(status
==U_BUFFER_OVERFLOW_ERROR
){
534 resultlength
=resultlengthout
+1;
535 value
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
536 udat_getSymbols(def
, UDAT_MONTHS
, 11, value
, resultlength
, &status
);
538 if(U_FAILURE(status
))
539 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n");
541 if(u_strcmp(result
, value
)!=0)
542 log_data_err("FAIL: Error in settting and getting symbols\n");
544 log_verbose("PASS: setSymbols successful\n");
547 /*run series of tests to test setSymbols regressively*/
548 log_verbose("\nTesting setSymbols regressively\n");
549 VerifysetSymbols(def
, UDAT_ERAS
, 0, "BeforeChrist");
550 VerifysetSymbols(def
, UDAT_ERA_NAMES
, 1, "AnnoDomini");
551 VerifysetSymbols(def
, UDAT_WEEKDAYS
, 1, "Sundayweek");
552 VerifysetSymbols(def
, UDAT_SHORT_WEEKDAYS
, 7, "Satweek");
553 VerifysetSymbols(def
, UDAT_NARROW_WEEKDAYS
, 4, "M");
554 VerifysetSymbols(def
, UDAT_STANDALONE_WEEKDAYS
, 1, "Sonntagweek");
555 VerifysetSymbols(def
, UDAT_STANDALONE_SHORT_WEEKDAYS
, 7, "Sams");
556 VerifysetSymbols(def
, UDAT_STANDALONE_NARROW_WEEKDAYS
, 4, "V");
557 VerifysetSymbols(fr
, UDAT_MONTHS
, 11, "december");
558 VerifysetSymbols(fr
, UDAT_SHORT_MONTHS
, 0, "Jan");
559 VerifysetSymbols(fr
, UDAT_NARROW_MONTHS
, 1, "R");
560 VerifysetSymbols(fr
, UDAT_STANDALONE_MONTHS
, 11, "dezember");
561 VerifysetSymbols(fr
, UDAT_STANDALONE_SHORT_MONTHS
, 7, "Aug");
562 VerifysetSymbols(fr
, UDAT_STANDALONE_NARROW_MONTHS
, 2, "M");
563 VerifysetSymbols(fr
, UDAT_QUARTERS
, 0, "1. Quart");
564 VerifysetSymbols(fr
, UDAT_SHORT_QUARTERS
, 1, "QQ2");
565 VerifysetSymbols(fr
, UDAT_STANDALONE_QUARTERS
, 2, "3rd Quar.");
566 VerifysetSymbols(fr
, UDAT_STANDALONE_SHORT_QUARTERS
, 3, "4QQ");
569 /*run series of tests to test get and setSymbols regressively*/
570 log_verbose("\nTesting get and set symbols regressively\n");
571 VerifygetsetSymbols(fr
, def
, UDAT_WEEKDAYS
, 1);
572 VerifygetsetSymbols(fr
, def
, UDAT_WEEKDAYS
, 7);
573 VerifygetsetSymbols(fr
, def
, UDAT_SHORT_WEEKDAYS
, 1);
574 VerifygetsetSymbols(fr
, def
, UDAT_SHORT_WEEKDAYS
, 7);
575 VerifygetsetSymbols(fr
, def
, UDAT_MONTHS
, 0);
576 VerifygetsetSymbols(fr
, def
, UDAT_SHORT_MONTHS
, 0);
577 VerifygetsetSymbols(fr
, def
, UDAT_ERAS
,1);
578 VerifygetsetSymbols(fr
, def
, UDAT_LOCALIZED_CHARS
, 0);
579 VerifygetsetSymbols(fr
, def
, UDAT_AM_PMS
, 1);
595 * Test DateFormat(Calendar) API
597 static void TestDateFormatCalendar() {
598 UDateFormat
*date
=0, *time
=0, *full
=0;
604 UErrorCode ec
= U_ZERO_ERROR
;
606 ctest_setTimeZone(NULL
, &ec
);
608 /* Create a formatter for date fields. */
609 date
= udat_open(UDAT_NONE
, UDAT_SHORT
, "en_US", NULL
, 0, NULL
, 0, &ec
);
611 log_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s\n",
616 /* Create a formatter for time fields. */
617 time
= udat_open(UDAT_SHORT
, UDAT_NONE
, "en_US", NULL
, 0, NULL
, 0, &ec
);
619 log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n",
624 /* Create a full format for output */
625 full
= udat_open(UDAT_FULL
, UDAT_FULL
, "en_US", NULL
, 0, NULL
, 0, &ec
);
627 log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n",
632 /* Create a calendar */
633 cal
= ucal_open(NULL
, 0, "en_US", UCAL_GREGORIAN
, &ec
);
635 log_err("FAIL: ucal_open(en_US) failed with %s\n",
642 u_uastrcpy(buf
, "4/5/2001");
644 udat_parseCalendar(date
, cal
, buf
, -1, &pos
, &ec
);
646 log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n",
647 pos
, u_errorName(ec
));
652 u_uastrcpy(buf
, "5:45 PM");
654 udat_parseCalendar(time
, cal
, buf
, -1, &pos
, &ec
);
656 log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n",
657 pos
, u_errorName(ec
));
662 when
= ucal_getMillis(cal
, &ec
);
664 log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec
));
667 udat_format(full
, when
, buf
, sizeof(buf
), NULL
, &ec
);
669 log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec
));
672 u_austrcpy(cbuf
, buf
);
673 /* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */
674 if (when
== 986517900000.0) {
675 log_verbose("Ok: Parsed result: %s\n", cbuf
);
677 log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf
);
686 ctest_resetTimeZone();
689 /*INTERNAL FUNCTIONS USED*/
690 static void VerifygetSymbols(UDateFormat
* datfor
, UDateFormatSymbolType type
, int32_t index
, const char* expected
)
693 UErrorCode status
= U_ZERO_ERROR
;
695 int32_t resultlength
, resultlengthout
;
698 pattern
=(UChar
*)malloc(sizeof(UChar
) * (strlen(expected
)+1));
699 u_uastrcpy(pattern
, expected
);
701 resultlengthout
=udat_getSymbols(datfor
, type
, index
, NULL
, resultlength
, &status
);
702 if(status
==U_BUFFER_OVERFLOW_ERROR
)
705 resultlength
=resultlengthout
+1;
706 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
707 udat_getSymbols(datfor
, type
, index
, result
, resultlength
, &status
);
710 if(U_FAILURE(status
))
712 log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status
) );
715 if(u_strcmp(result
, pattern
)==0)
716 log_verbose("PASS: getSymbols retrieved the right value\n");
718 log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", austrdup(pattern
),
725 static void VerifysetSymbols(UDateFormat
* datfor
, UDateFormatSymbolType type
, int32_t index
, const char* expected
)
729 int32_t resultlength
, resultlengthout
;
730 UErrorCode status
= U_ZERO_ERROR
;
732 value
=(UChar
*)malloc(sizeof(UChar
) * (strlen(expected
) + 1));
733 u_uastrcpy(value
, expected
);
734 udat_setSymbols(datfor
, type
, index
, value
, u_strlen(value
), &status
);
735 if(U_FAILURE(status
))
737 log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status
) );
742 resultlengthout
=udat_getSymbols(datfor
, type
, index
, NULL
, resultlength
, &status
);
743 if(status
==U_BUFFER_OVERFLOW_ERROR
){
745 resultlength
=resultlengthout
+1;
746 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
747 udat_getSymbols(datfor
, type
, index
, result
, resultlength
, &status
);
749 if(U_FAILURE(status
)){
750 log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n",
751 myErrorName(status
) );
755 if(u_strcmp(result
, value
)!=0){
756 log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(value
),
760 log_verbose("PASS: setSymbols successful\n");
767 static void VerifygetsetSymbols(UDateFormat
* from
, UDateFormat
* to
, UDateFormatSymbolType type
, int32_t index
)
771 int32_t resultlength
, resultlengthout
;
772 UErrorCode status
= U_ZERO_ERROR
;
775 resultlengthout
=udat_getSymbols(from
, type
, index
, NULL
, resultlength
, &status
);
776 if(status
==U_BUFFER_OVERFLOW_ERROR
){
778 resultlength
=resultlengthout
+1;
779 result
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
780 udat_getSymbols(from
, type
, index
, result
, resultlength
, &status
);
782 if(U_FAILURE(status
)){
783 log_err("FAIL: error in getSymbols() %s\n", myErrorName(status
) );
787 resultlength
=resultlengthout
+1;
788 udat_setSymbols(to
, type
, index
, result
, resultlength
, &status
);
789 if(U_FAILURE(status
))
791 log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status
) );
796 resultlengthout
=udat_getSymbols(to
, type
, index
, NULL
, resultlength
, &status
);
797 if(status
==U_BUFFER_OVERFLOW_ERROR
){
799 resultlength
=resultlengthout
+1;
800 value
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
801 udat_getSymbols(to
, type
, index
, value
, resultlength
, &status
);
803 if(U_FAILURE(status
)){
804 log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n",
805 myErrorName(status
) );
809 if(u_strcmp(result
, value
)!=0){
810 log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result
),
814 log_verbose("PASS: setSymbols successful\n");
821 static UChar
* myNumformat(const UNumberFormat
* numfor
, double d
)
824 int32_t resultlength
, resultlengthneeded
;
825 UErrorCode status
= U_ZERO_ERROR
;
828 resultlengthneeded
=unum_formatDouble(numfor
, d
, NULL
, resultlength
, NULL
, &status
);
829 if(status
==U_BUFFER_OVERFLOW_ERROR
)
832 resultlength
=resultlengthneeded
+1;
833 /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */
834 result2
=(UChar
*)ctst_malloc(sizeof(UChar
) * resultlength
); /*this won't*/
835 unum_formatDouble(numfor
, d
, result2
, resultlength
, NULL
, &status
);
837 if(U_FAILURE(status
))
839 log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status
) );
847 * The search depth for TestExtremeDates. The total number of
848 * dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1.
850 #define EXTREME_DATES_DEPTH 8
853 * Support for TestExtremeDates (below).
855 * Test a single date to see whether udat_format handles it properly.
857 static UBool
_aux1ExtremeDates(UDateFormat
* fmt
, UDate date
,
858 UChar
* buf
, int32_t buflen
, char* cbuf
,
860 int32_t len
= udat_format(fmt
, date
, buf
, buflen
, 0, ec
);
861 if (!assertSuccess("udat_format", ec
)) return FALSE
;
862 u_austrncpy(cbuf
, buf
, buflen
);
864 log_err("FAIL: udat_format(%g) => \"%s\"\n", date
, cbuf
);
866 log_verbose("udat_format(%g) => \"%s\"\n", date
, cbuf
);
872 * Support for TestExtremeDates (below).
874 * Recursively test between 'small' and 'large', up to the depth
875 * limit specified by EXTREME_DATES_DEPTH.
877 static UBool
_aux2ExtremeDates(UDateFormat
* fmt
, UDate small
, UDate large
,
878 UChar
* buf
, int32_t buflen
, char* cbuf
,
881 /* Logarithmic midpoint; see below */
882 UDate mid
= (UDate
) exp((log(small
) + log(large
)) / 2);
883 if (count
== EXTREME_DATES_DEPTH
) {
887 _aux1ExtremeDates(fmt
, mid
, buf
, buflen
, cbuf
, ec
) &&
888 _aux2ExtremeDates(fmt
, small
, mid
, buf
, buflen
, cbuf
, count
+1, ec
) &&
889 _aux2ExtremeDates(fmt
, mid
, large
, buf
, buflen
, cbuf
, count
+1, ec
);
893 * http://www.jtcsv.com/cgibin/icu-bugs?findid=3659
895 * For certain large dates, udat_format crashes on MacOS. This test
896 * attempts to reproduce this problem by doing a recursive logarithmic*
897 * binary search of a predefined interval (from 'small' to 'large').
899 * The limit of the search is given by EXTREME_DATES_DEPTH, above.
901 * *The search has to be logarithmic, not linear. A linear search of the
902 * range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and
903 * 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5
906 static void TestExtremeDates() {
911 const double small
= 1000; /* 1 sec */
912 const double large
= 1e+30; /* well beyond usable UDate range */
914 /* There is no need to test larger values from 1e+30 to 1e+300;
915 the failures occur around 1e+27, and never above 1e+30. */
918 fmt
= udat_open(UDAT_LONG
, UDAT_LONG
, "en_US",
920 if (!assertSuccess("udat_open", &ec
)) return;
922 _aux2ExtremeDates(fmt
, small
, large
, buf
, LEN(buf
), cbuf
, 0, &ec
);
927 static void TestAllLocales(void) {
928 int32_t idx
, dateIdx
, timeIdx
, localeCount
;
929 static const UDateFormatStyle style
[] = {
930 UDAT_FULL
, UDAT_LONG
, UDAT_MEDIUM
, UDAT_SHORT
932 localeCount
= uloc_countAvailable();
933 for (idx
= 0; idx
< localeCount
; idx
++) {
934 for (dateIdx
= 0; dateIdx
< (int32_t)(sizeof(style
)/sizeof(style
[0])); dateIdx
++) {
935 for (timeIdx
= 0; timeIdx
< (int32_t)(sizeof(style
)/sizeof(style
[0])); timeIdx
++) {
936 UErrorCode status
= U_ZERO_ERROR
;
937 udat_close(udat_open(style
[dateIdx
], style
[timeIdx
],
938 uloc_getAvailable(idx
), NULL
, 0, NULL
, 0, &status
));
939 if (U_FAILURE(status
)) {
940 log_err("FAIL: udat_open(%s) failed with (%s) dateIdx=%d, timeIdx=%d\n",
941 uloc_getAvailable(idx
), u_errorName(status
), dateIdx
, timeIdx
);
948 #endif /* #if !UCONFIG_NO_FORMATTING */