]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cdtrgtst.c
1 /********************************************************************
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Madhu Katragadda Ported for C API
11 * Modification History:
12 * Date Name Description
13 * 07/15/99 helena Ported to HPUX 10/11 CC.
14 *********************************************************************************
16 /* REGRESSION 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"
31 void addDateForRgrTest(TestNode
** root
);
33 void addDateForRgrTest(TestNode
** root
)
35 addTest(root
, &Test4029195
, "tsformat/cdtrgtst/Test4029195");
36 addTest(root
, &Test4056591
, "tsformat/cdtrgtst/Test4056591");
37 addTest(root
, &Test4059917
, "tsformat/cdtrgtst/Test4059917");
38 addTest(root
, &Test4060212
, "tsformat/cdtrgtst/Test4060212");
39 addTest(root
, &Test4061287
, "tsformat/cdtrgtst/Test4061287");
40 addTest(root
, &Test4073003
, "tsformat/cdtrgtst/Test4073003");
41 addTest(root
, &Test4162071
, "tsformat/cdtrgtst/Test4162071");
42 addTest(root
, &Test714
, "tsformat/cdtrgtst/Test714");
50 int32_t resultlength
, resultlengthneeded
;
51 UChar
*fmdt
, *todayS
, *rt
;
57 UErrorCode status
= U_ZERO_ERROR
;
59 log_verbose("Testing date format and parse function in regression test\n");
60 today
= ucal_getNow();
62 df
= udat_open(UDAT_DEFAULT
,UDAT_DEFAULT
,"en_US", NULL
, 0, NULL
, 0, &status
);
65 log_err("FAIL: error in creating the dateformat using default date and time style : %s\n", myErrorName(status
));
69 resultlengthneeded
=udat_toPattern(df
, TRUE
, NULL
, resultlength
, &status
);
70 if(status
==U_BUFFER_OVERFLOW_ERROR
)
73 resultlength
=resultlengthneeded
+ 1;
74 pat
=(UChar
*)malloc(sizeof(UChar
) * resultlength
);
75 udat_toPattern(df
, TRUE
, pat
, resultlength
, &status
);
78 log_verbose("pattern: %s\n", austrdup(pat
));
81 fmdt
= myFormatit(df
, today
);
83 log_verbose("today: %s\n", austrdup(fmdt
));
85 log_data_err("ERROR: couldn't format, exitting test");
89 temp
=(UChar
*)malloc(sizeof(UChar
) * 10);
90 u_uastrcpy(temp
, "M yyyy dd");
91 udat_applyPattern(df
, TRUE
, temp
, u_strlen(temp
));
93 todayS
=myFormatit(df
, today
);
94 log_verbose("After teh pattern is applied\n today: %s\n", austrdup(todayS
) );
96 d1
=udat_parse(df
, todayS
, u_strlen(todayS
), &parsepos
, &status
);
99 log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status
));
102 rt
=myFormatit(df
, d1
);
103 log_verbose("today: %s\n", austrdup(rt
) );
105 log_verbose("round trip: %s\n", austrdup(rt
) );
107 if(u_strcmp(rt
, todayS
)!=0) {
108 log_err("Fail: Want %s Got %s\n", austrdup(todayS
), austrdup(rt
) );
111 log_verbose("Pass: parse and format working fine\n");
122 * Verify the function of the [s|g]et2DigitYearStart() API.
131 UChar
*gotdate
, *expdate
;
134 UErrorCode status
= U_ZERO_ERROR
;
135 const char* strings
[] = {
142 log_verbose("Testing s[get] 2 digit year start regressively\n");
143 cal
=ucal_open(NULL
, 0, "en_US", UCAL_GREGORIAN
, &status
);
144 if(U_FAILURE(status
)){
145 log_err("error in ucal_open caldef : %s\n", myErrorName(status
));
147 ucal_setDateTime(cal
, 1809, UCAL_DECEMBER
, 25, 17, 40, 30, &status
);
148 d
[0]=ucal_getMillis(cal
, &status
);
149 if(U_FAILURE(status
)){
150 log_err("Error: failure in get millis: %s\n", myErrorName(status
));
152 ucal_setDateTime(cal
, 1909, UCAL_DECEMBER
, 24, 17, 40, 30, &status
);
153 d
[1]=ucal_getMillis(cal
, &status
);
154 ucal_setDateTime(cal
, 1861, UCAL_DECEMBER
, 26, 17, 40, 30, &status
);
155 d
[2]=ucal_getMillis(cal
, &status
);
156 ucal_setDateTime(cal
, 1999, UCAL_DECEMBER
, 27, 17, 40, 30, &status
);
157 d
[3]=ucal_getMillis(cal
, &status
);
160 u_uastrcpy(pat
, "yyMMdd");
161 def
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,NULL
, NULL
, 0, pat
, u_strlen(pat
), &status
);
162 if(U_FAILURE(status
))
164 log_err("FAIL: error in creating the dateformat using u_openPattern(): %s\n", myErrorName(status
));
168 udat_set2DigitYearStart(def
, start
, &status
);
169 if(U_FAILURE(status
))
170 log_err("ERROR: in setTwoDigitStartDate: %s\n", myErrorName(status
));
171 if( (udat_get2DigitYearStart(def
, &status
) != start
))
172 log_err("ERROR: get2DigitYearStart broken\n");
175 for(i
= 0; i
< 4; ++i
) {
176 u_uastrcpy(s
, strings
[i
]);
178 got
= udat_parse(def
, s
, u_strlen(s
), 0, &status
);
179 gotdate
=myFormatit(def
, got
);
180 expdate
=myFormatit(def
, exp
);
182 if (gotdate
== NULL
|| expdate
== NULL
) {
183 log_err("myFormatit failed!\n");
185 else if(u_strcmp(gotdate
, expdate
) !=0){
186 log_err("set2DigitYearStart broken for %s \n got: %s, expected: %s\n", austrdup(s
),
187 austrdup(gotdate
), austrdup(expdate
) );
197 * SimpleDateFormat does not properly parse date strings without delimiters
204 UErrorCode status
= U_ZERO_ERROR
;
208 log_verbose("Testing apply pattern and to pattern regressively\n");
209 u_uastrcpy(tzID
, "PST");
210 pattern
=(UChar
*)malloc(sizeof(UChar
) * 11);
211 u_uastrcpy(pattern
, "yyyy/MM/dd");
212 log_verbose("%s\n", austrdup(pattern
) );
213 def
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,NULL
,tzID
,-1,pattern
, u_strlen(pattern
),&status
);
214 if(U_FAILURE(status
))
216 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status
));
219 myDate
=(UChar
*)malloc(sizeof(UChar
) * 11);
220 u_uastrcpy(myDate
, "1970/01/12");
222 aux917( def
, myDate
);
225 u_uastrcpy(pattern
, "yyyyMMdd");
226 def
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,NULL
,tzID
,-1,pattern
, u_strlen(pattern
),&status
);
227 if(U_FAILURE(status
))
229 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status
));
232 u_uastrcpy(myDate
, "19700112");
233 aux917( def
, myDate
);
240 void aux917( UDateFormat
*fmt
, UChar
* str
)
242 int32_t resultlength
, resultlengthneeded
;
243 UErrorCode status
= U_ZERO_ERROR
;
244 UChar
* formatted
=NULL
;
246 UDate d1
=1000000000.0;
249 resultlengthneeded
=udat_toPattern(fmt
, TRUE
, NULL
, resultlength
, &status
);
250 if(status
==U_BUFFER_OVERFLOW_ERROR
)
253 resultlength
=resultlengthneeded
+ 1;
254 pat
=(UChar
*)malloc(sizeof(UChar
) * (resultlength
));
255 udat_toPattern(fmt
, TRUE
, pat
, resultlength
, &status
);
257 if(U_FAILURE(status
)){
258 log_err("failure in retrieving the pattern: %s\n", myErrorName(status
));
260 log_verbose("pattern: %s\n", austrdup(pat
) );
262 status
= U_ZERO_ERROR
;
263 formatted
= myFormatit(fmt
, d1
);
264 if( u_strcmp(formatted
,str
)!=0) {
265 log_err("Fail: Want %s Got: %s\n", austrdup(str
), austrdup(formatted
) );
277 UDateFormat
*formatter
, *fmt
;
278 UErrorCode status
= U_ZERO_ERROR
;
281 UChar dateString
[30], pattern
[20], tzID
[4];
282 u_uastrcpy(dateString
, "1995-040.05:01:29 -8");
283 u_uastrcpy(pattern
, "yyyy-DDD.hh:mm:ss z");
285 log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString
) );
286 status
= U_ZERO_ERROR
;
287 u_uastrcpy(tzID
, "PST");
289 formatter
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,"en_US",tzID
,-1,pattern
, u_strlen(pattern
), &status
);
291 myDate
= udat_parse(formatter
, dateString
, u_strlen(dateString
), &pos
, &status
);
294 fmt
= udat_open(UDAT_FULL
,UDAT_LONG
,NULL
, tzID
, -1, NULL
, 0, &status
);
295 if(U_FAILURE(status
))
297 log_err("FAIL: error in creating the dateformat using default date and time style: %s\n",
298 myErrorName(status
) );
301 myString
= myFormatit(fmt
, myDate
);
302 cal
=ucal_open(tzID
, u_strlen(tzID
), "en_US", UCAL_GREGORIAN
, &status
);
303 if(U_FAILURE(status
)){
304 log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status
));
306 ucal_setMillis(cal
, myDate
, &status
);
307 if ((ucal_get(cal
, UCAL_DAY_OF_YEAR
, &status
) != 40)){
308 log_err("Fail: Got %d Expected 40\n", ucal_get(cal
, UCAL_DAY_OF_YEAR
, &status
));
311 udat_close(formatter
);
325 UErrorCode status
= U_ZERO_ERROR
;
327 UChar pattern
[21], dateString
[11];
329 u_uastrcpy(dateString
, "35/13/1971");
330 u_uastrcpy(pattern
, "dd/mm/yyyy");
331 status
= U_ZERO_ERROR
;
332 log_verbose("Testing parsing by changing the attribute lenient\n");
333 df
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,NULL
,NULL
,0,pattern
, u_strlen(pattern
),&status
);
334 if(U_FAILURE(status
)){
335 log_err("ERROR: failure in open pattern of test4061287: %s\n", myErrorName(status
));
341 udat_setLenient(df
, FALSE
);
342 ok
=udat_isLenient(df
);
344 log_err("setLenient nor working\n");
346 myDate
= udat_parse(df
, dateString
, u_strlen(dateString
), &pos
, &status
);
347 if(U_FAILURE(status
))
350 log_err("Fail: Lenient not working: does lenient parsing in spite of setting Leninent as FALSE ");
358 /* The java.text.DateFormat.parse(String) method expects for the
359 US locale a string formatted according to mm/dd/yy and parses it
362 When given a string mm/dd/yyyy it only parses up to the first
363 two y's, typically resulting in a date in the year 1919.
365 Please extend the parsing method(s) to handle strings with
366 four-digit year values (probably also applicable to various
377 UErrorCode status
= U_ZERO_ERROR
;
379 UChar
*result
, *result2
;
380 const char* tests
[] = {
387 fmt
= udat_open(UDAT_SHORT
,UDAT_SHORT
,NULL
, NULL
, 0, NULL
, 0, &status
);
388 if(U_FAILURE(status
))
390 log_err("FAIL: error in creating the dateformat using short date and time style: %s\n",
391 myErrorName(status
));
394 u_uastrcpy(temp
, "m/D/yy");
395 udat_applyPattern(fmt
, FALSE
, temp
, u_strlen(temp
));
397 for(i
= 0; i
< 4; i
+=2) {
399 datestr
=(UChar
*)malloc(sizeof(UChar
) * (strlen(tests
[i
])+1));
400 u_uastrcpy(datestr
, tests
[i
]);
403 d
= udat_parse(fmt
, datestr
, u_strlen(datestr
), &pos
, &status
);
404 if(U_FAILURE(status
)){
405 log_err("ERROR : in test 4073003: %s\n", myErrorName(status
));
409 datestr
=(UChar
*)malloc(sizeof(UChar
) * (strlen(tests
[i
+1])+1));
410 u_uastrcpy(datestr
, tests
[i
+1]);
414 dd
= udat_parse(fmt
, datestr
, u_strlen(datestr
), &pos
, &status
);
415 if(U_FAILURE(status
)){
416 log_err("ERROR : in test 4073003: %s\n", myErrorName(status
));
420 result
=myFormatit(fmt
, d
);
421 result2
=myFormatit(fmt
, dd
);
422 if(!result
|| !result2
) {
423 log_data_err("Fail: could not format - exitting test\n");
426 if (u_strcmp(result
, result2
)!=0){
427 log_err("Fail: %s != %s\n", austrdup(result
), austrdup(result2
) );
430 log_verbose("Ok: %s == %s\n", austrdup(result
), austrdup(result2
) );
444 UErrorCode status
= U_ZERO_ERROR
;
448 u_uastrcpy(datestr
, "Thu, 30-Jul-1999 11:51:14 GMT");
449 u_uastrcpy(format
, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /* RFC 822/1123 */
450 status
= U_ZERO_ERROR
;
451 /* Can't hardcode the result to assume the default locale is "en_US". */
452 df
= udat_open(UDAT_IGNORE
,UDAT_IGNORE
,"en_US",NULL
,0,format
, u_strlen(format
),&status
);
453 if(U_FAILURE(status
)){
454 log_data_err("ERROR: couldn't create date format: %s\n", myErrorName(status
));
458 x
= udat_parse(df
, datestr
, u_strlen(datestr
), &pos
, &status
);
459 if(U_FAILURE(status
)){
460 log_data_err("ERROR : parse format %s fails : %s\n", austrdup(format
), myErrorName(status
));
463 log_verbose("Parse format \"%s \" ok.\n", austrdup(format
) );
465 /*log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );*/
471 UDate d
=978103543000.0;
473 UErrorCode status
= U_ZERO_ERROR
;
476 const char* expect
= "7:25:43 AM";
478 ctest_setTimeZone(NULL
, &status
);
480 fmt
= udat_open(UDAT_MEDIUM
,UDAT_NONE
,"en_US_CA", NULL
, -1, NULL
, 0, &status
);
481 if(U_FAILURE(status
))
483 log_err("FAIL: error in creating the dateformat using medium time style and NO date style: %s\n",
484 myErrorName(status
));
487 result
=myFormatit(fmt
, d
);
489 log_data_err("Fail: could not format - exitting test\n");
492 u_uastrcpy(temp
, expect
);
493 if (u_strcmp(result
, temp
)!=0){
494 log_err("Fail: %s != %s\n", austrdup(result
), expect
);
497 log_verbose("Ok: %s == %s\n", austrdup(result
), expect
);
502 ctest_resetTimeZone();
505 /*INTERNAL FUNCTION USED */
507 UChar
* myFormatit(UDateFormat
* datdef
, UDate d1
)
510 int32_t resultlength
, resultlengthneeded
;
511 UErrorCode status
= U_ZERO_ERROR
;
514 resultlengthneeded
=udat_format(datdef
, d1
, NULL
, resultlength
, NULL
, &status
);
515 if(status
==U_BUFFER_OVERFLOW_ERROR
)
518 resultlength
=resultlengthneeded
+1;
519 /*result1=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /*this leaks*/
520 result1
=(UChar
*)ctst_malloc(sizeof(UChar
) * resultlength
); /*this won't*/
521 udat_format(datdef
, d1
, result1
, resultlength
, NULL
, &status
);
523 if(U_FAILURE(status
))
525 log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status
));
534 #endif /* #if !UCONFIG_NO_FORMATTING */