]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cdtrgtst.c
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdtrgtst.c
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File CDTRGTST.C
9 *
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 *********************************************************************************
15 */
16 /* REGRESSION 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 "cdtrgtst.h"
29 #include "cmemory.h"
30
31 void addDateForRgrTest(TestNode** root);
32
33 void addDateForRgrTest(TestNode** root)
34 {
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");
43 }
44
45 /**
46 * @bug 4029195
47 */
48 void Test4029195()
49 {
50 int32_t resultlength, resultlengthneeded;
51 UChar *fmdt, *todayS, *rt;
52 UChar *pat=NULL;
53 UChar *temp;
54 UDate today, d1;
55 UDateFormat *df;
56 int32_t parsepos;
57 UErrorCode status = U_ZERO_ERROR;
58
59 log_verbose("Testing date format and parse function in regression test\n");
60 today = ucal_getNow();
61
62 df = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status);
63 if(U_FAILURE(status))
64 {
65 log_err("FAIL: error in creating the dateformat using default date and time style : %s\n", myErrorName(status));
66 return;
67 }
68 resultlength=0;
69 resultlengthneeded=udat_toPattern(df, TRUE, NULL, resultlength, &status);
70 if(status==U_BUFFER_OVERFLOW_ERROR)
71 {
72 status=U_ZERO_ERROR;
73 resultlength=resultlengthneeded + 1;
74 pat=(UChar*)malloc(sizeof(UChar) * resultlength);
75 udat_toPattern(df, TRUE, pat, resultlength, &status);
76 }
77
78 log_verbose("pattern: %s\n", austrdup(pat));
79
80
81 fmdt = myFormatit(df, today);
82 if(fmdt) {
83 log_verbose("today: %s\n", austrdup(fmdt));
84 } else {
85 log_data_err("ERROR: couldn't format, exitting test");
86 return;
87 }
88
89 temp=(UChar*)malloc(sizeof(UChar) * 10);
90 u_uastrcpy(temp, "M yyyy dd");
91 udat_applyPattern(df, TRUE, temp, u_strlen(temp));
92
93 todayS =myFormatit(df, today);
94 log_verbose("After teh pattern is applied\n today: %s\n", austrdup(todayS) );
95 parsepos=0;
96 d1=udat_parse(df, todayS, u_strlen(todayS), &parsepos, &status);
97 if(U_FAILURE(status))
98 {
99 log_err("FAIL: Error in parsing using udat_parse(.....): %s\n", myErrorName(status));
100 }
101
102 rt =myFormatit(df, d1);
103 log_verbose("today: %s\n", austrdup(rt) );
104
105 log_verbose("round trip: %s\n", austrdup(rt) );
106
107 if(u_strcmp(rt, todayS)!=0) {
108 log_err("Fail: Want %s Got %s\n", austrdup(todayS), austrdup(rt) );
109 }
110 else
111 log_verbose("Pass: parse and format working fine\n");
112 udat_close(df);
113 free(temp);
114 if(pat != NULL) {
115 free(pat);
116 }
117 }
118
119
120 /**
121 * @bug 4056591
122 * Verify the function of the [s|g]et2DigitYearStart() API.
123 */
124 void Test4056591()
125 {
126 int i;
127 UCalendar *cal;
128 UDateFormat *def;
129 UDate start,exp,got;
130 UChar s[10];
131 UChar *gotdate, *expdate;
132 UChar pat[10];
133 UDate d[4];
134 UErrorCode status = U_ZERO_ERROR;
135 const char* strings[] = {
136 "091225",
137 "091224",
138 "611226",
139 "991227"
140 };
141
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));
146 }
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));
151 }
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);
158
159
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))
163 {
164 log_err("FAIL: error in creating the dateformat using u_openPattern(): %s\n", myErrorName(status));
165 return;
166 }
167 start = 1800;
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");
173
174
175 for(i = 0; i < 4; ++i) {
176 u_uastrcpy(s, strings[i]);
177 exp = d[i];
178 got = udat_parse(def, s, u_strlen(s), 0, &status);
179 gotdate=myFormatit(def, got);
180 expdate=myFormatit(def, exp);
181
182 if(u_strcmp(gotdate, expdate) !=0){
183 log_err("set2DigitYearStart broken for %s \n got: %s, expected: %s\n", austrdup(s),
184 austrdup(gotdate), austrdup(expdate) );
185 }
186 }
187
188 udat_close(def);
189 ucal_close(cal);
190 }
191
192
193 /**
194 * @bug 4059917
195 */
196 void Test4059917()
197 {
198 UDateFormat* def;
199 UChar *myDate;
200 UErrorCode status = U_ZERO_ERROR;
201 UChar *pattern;
202 UChar tzID[4];
203
204 log_verbose("Testing apply pattern and to pattern regressively\n");
205 u_uastrcpy(tzID, "PST");
206 pattern=(UChar*)malloc(sizeof(UChar) * 11);
207 u_uastrcpy(pattern, "yyyy/MM/dd");
208 log_verbose("%s\n", austrdup(pattern) );
209 def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,tzID,-1,pattern, u_strlen(pattern),&status);
210 if(U_FAILURE(status))
211 {
212 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
213 return;
214 }
215 myDate=(UChar*)malloc(sizeof(UChar) * 11);
216 u_uastrcpy(myDate, "1970/01/12");
217
218 aux917( def, myDate );
219 udat_close(def);
220
221 u_uastrcpy(pattern, "yyyyMMdd");
222 def = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,NULL,0,pattern, u_strlen(pattern),&status);
223 if(U_FAILURE(status))
224 {
225 log_err("FAIL: error in creating the dateformat using openPattern: %s\n", myErrorName(status));
226 return;
227 }
228 u_uastrcpy(myDate, "19700112");
229 aux917( def, myDate );
230 udat_close(def);
231 free(pattern);
232 free(myDate);
233
234 }
235
236 void aux917( UDateFormat *fmt, UChar* str)
237 {
238 int32_t resultlength, resultlengthneeded;
239 UErrorCode status = U_ZERO_ERROR;
240 UChar* formatted=NULL;
241 UChar *pat=NULL;
242 UDate d1=1000000000.0;
243
244 resultlength=0;
245 resultlengthneeded=udat_toPattern(fmt, TRUE, NULL, resultlength, &status);
246 if(status==U_BUFFER_OVERFLOW_ERROR)
247 {
248 status=U_ZERO_ERROR;
249 resultlength=resultlengthneeded + 1;
250 pat=(UChar*)malloc(sizeof(UChar) * (resultlength));
251 udat_toPattern(fmt, TRUE, pat, resultlength, &status);
252 }
253 if(U_FAILURE(status)){
254 log_err("failure in retrieving the pattern: %s\n", myErrorName(status));
255 }
256 log_verbose("pattern: %s\n", austrdup(pat) );
257
258 status = U_ZERO_ERROR;
259 formatted = myFormatit(fmt, d1);
260 if( u_strcmp(formatted,str)!=0) {
261 log_err("Fail: Want %s Got: %s\n", austrdup(str), austrdup(formatted) );
262 }
263 free(pat);
264 }
265
266 /**
267 * @bug 4060212
268 */
269 void Test4060212()
270 {
271 int32_t pos;
272 UCalendar *cal;
273 UDateFormat *formatter, *fmt;
274 UErrorCode status = U_ZERO_ERROR;
275 UDate myDate;
276 UChar *myString;
277 UChar dateString[30], pattern[20], tzID[4];
278 u_uastrcpy(dateString, "1995-040.05:01:29 -8");
279 u_uastrcpy(pattern, "yyyy-DDD.hh:mm:ss z");
280
281 log_verbose( "dateString= %s Using yyyy-DDD.hh:mm:ss\n", austrdup(dateString) );
282 status = U_ZERO_ERROR;
283 u_uastrcpy(tzID, "PST");
284
285 formatter = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",tzID,-1,pattern, u_strlen(pattern), &status);
286 pos=0;
287 myDate = udat_parse(formatter, dateString, u_strlen(dateString), &pos, &status);
288
289
290 fmt = udat_open(UDAT_FULL,UDAT_LONG ,NULL, tzID, -1, NULL, 0, &status);
291 if(U_FAILURE(status))
292 {
293 log_err("FAIL: error in creating the dateformat using default date and time style: %s\n",
294 myErrorName(status) );
295 return;
296 }
297 myString = myFormatit(fmt, myDate);
298 cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
299 if(U_FAILURE(status)){
300 log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status));
301 }
302 ucal_setMillis(cal, myDate, &status);
303 if ((ucal_get(cal, UCAL_DAY_OF_YEAR, &status) != 40)){
304 log_err("Fail: Got %d Expected 40\n", ucal_get(cal, UCAL_DAY_OF_YEAR, &status));
305 }
306
307 udat_close(formatter);
308 ucal_close(cal);
309 udat_close(fmt);
310
311 }
312
313 /**
314 * @bug 4061287
315 */
316 void Test4061287()
317 {
318 UBool ok;
319 int32_t pos;
320 UDateFormat *df;
321 UErrorCode status = U_ZERO_ERROR;
322 UDate myDate;
323 UChar pattern[21], dateString[11];
324
325 u_uastrcpy(dateString, "35/13/1971");
326 u_uastrcpy(pattern, "dd/mm/yyyy");
327 status = U_ZERO_ERROR;
328 log_verbose("Testing parsing by changing the attribute lenient\n");
329 df = udat_open(UDAT_IGNORE,UDAT_IGNORE,NULL,NULL,0,pattern, u_strlen(pattern),&status);
330 if(U_FAILURE(status)){
331 log_err("ERROR: failure in open pattern of test4061287: %s\n", myErrorName(status));
332 return;
333 }
334
335 pos=0;
336
337 udat_setLenient(df, FALSE);
338 ok=udat_isLenient(df);
339 if(ok==TRUE)
340 log_err("setLenient nor working\n");
341 ok = FALSE;
342 myDate = udat_parse(df, dateString, u_strlen(dateString), &pos, &status);
343 if(U_FAILURE(status))
344 ok = TRUE;
345 if(ok!=TRUE)
346 log_err("Fail: Lenient not working: does lenient parsing in spite of setting Leninent as FALSE ");
347
348 udat_close(df);
349
350 }
351
352
353
354 /* The java.text.DateFormat.parse(String) method expects for the
355 US locale a string formatted according to mm/dd/yy and parses it
356 correctly.
357
358 When given a string mm/dd/yyyy it only parses up to the first
359 two y's, typically resulting in a date in the year 1919.
360
361 Please extend the parsing method(s) to handle strings with
362 four-digit year values (probably also applicable to various
363 other locales. */
364 /**
365 * @bug 4073003
366 */
367 void Test4073003()
368 {
369 int32_t pos,i;
370 UDate d,dd;
371 UChar *datestr;
372 UChar temp[15];
373 UErrorCode status = U_ZERO_ERROR;
374 UDateFormat *fmt;
375 UChar *result, *result2;
376 const char* tests [] = {
377 "12/25/61",
378 "12/25/1961",
379 "4/3/1999",
380 "4/3/99"
381 };
382
383 fmt= udat_open(UDAT_SHORT,UDAT_SHORT ,NULL, NULL, 0, NULL, 0, &status);
384 if(U_FAILURE(status))
385 {
386 log_err("FAIL: error in creating the dateformat using short date and time style: %s\n",
387 myErrorName(status));
388 return;
389 }
390 u_uastrcpy(temp, "m/D/yy");
391 udat_applyPattern(fmt, TRUE, temp, u_strlen(temp));
392
393 for(i= 0; i < 4; i+=2) {
394 status=U_ZERO_ERROR;
395 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i])+1));
396 u_uastrcpy(datestr, tests[i]);
397
398 pos=0;
399 d = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
400 if(U_FAILURE(status)){
401 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
402 }
403
404 free(datestr);
405 datestr=(UChar*)malloc(sizeof(UChar) * (strlen(tests[i+1])+1));
406 u_uastrcpy(datestr, tests[i+1]);
407
408 pos=0;
409 status=U_ZERO_ERROR;
410 dd = udat_parse(fmt, datestr, u_strlen(datestr), &pos, &status);
411 if(U_FAILURE(status)){
412 log_err("ERROR : in test 4073003: %s\n", myErrorName(status));
413 }
414 free(datestr);
415
416 result =myFormatit(fmt, d);
417 result2 =myFormatit(fmt, dd);
418 if(!result || !result2) {
419 log_data_err("Fail: could not format - exitting test");
420 return;
421 }
422 if (u_strcmp(result, result2)!=0){
423 log_err("Fail: %s != %s\n", austrdup(result), austrdup(result2) );
424 }
425 else{
426 log_verbose("Ok: %s == %s\n", austrdup(result), austrdup(result2) );
427 }
428
429
430
431 }
432 udat_close(fmt);
433 }
434
435 /**
436 * @bug 4162071
437 **/
438 void Test4162071()
439 {
440 int32_t pos;
441 UDate x;
442 UErrorCode status = U_ZERO_ERROR;
443 UDateFormat *df;
444 UChar datestr[30];
445 UChar format[50];
446 u_uastrcpy(datestr, "Thu, 30-Jul-1999 11:51:14 GMT");
447 u_uastrcpy(format, "EEE', 'dd-MMM-yyyy HH:mm:ss z"); /* RFC 822/1123 */
448 status = U_ZERO_ERROR;
449 /* Can't hardcode the result to assume the default locale is "en_US". */
450 df = udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US",NULL,0,format, u_strlen(format),&status);
451 if(U_FAILURE(status)){
452 log_data_err("ERROR: couldn't create date format: %s\n", myErrorName(status));
453 return;
454 }
455 pos=0;
456 x = udat_parse(df, datestr, u_strlen(datestr), &pos, &status);
457 if(U_FAILURE(status)){
458 log_data_err("ERROR : parse format %s fails : %s\n", austrdup(format), myErrorName(status));
459 }
460 else{
461 log_verbose("Parse format \"%s \" ok.\n", austrdup(format) );
462 }
463 log_verbose("date= %s\n", austrdup(myFormatit(df, x)) );
464 udat_close(df);
465 }
466
467 void Test714(void)
468 {
469 UDate d=978103543000.0;
470 UChar temp[20];
471 UErrorCode status = U_ZERO_ERROR;
472 UDateFormat *fmt;
473 UChar *result;
474 const char* expect = "7:25:43 AM";
475
476 ctest_setTimeZone(NULL, &status);
477
478 fmt= udat_open(UDAT_MEDIUM,UDAT_NONE ,"en_US_CA", NULL, -1, NULL, 0, &status);
479 if(U_FAILURE(status))
480 {
481 log_err("FAIL: error in creating the dateformat using medium time style and NO date style: %s\n",
482 myErrorName(status));
483 return;
484 }
485 result =myFormatit(fmt, d);
486 if(!result) {
487 log_data_err("Fail: could not format - exitting test\n");
488 return;
489 }
490 u_uastrcpy(temp, expect);
491 if (u_strcmp(result, temp)!=0){
492 log_err("Fail: %s != %s\n", austrdup(result), expect);
493 }
494 else{
495 log_verbose("Ok: %s == %s\n", austrdup(result), expect );
496 }
497
498 udat_close(fmt);
499
500 ctest_resetTimeZone();
501 }
502
503 /*INTERNAL FUNCTION USED */
504
505 UChar* myFormatit(UDateFormat* datdef, UDate d1)
506 {
507 UChar *result1=NULL;
508 int32_t resultlength, resultlengthneeded;
509 UErrorCode status = U_ZERO_ERROR;
510
511 resultlength=0;
512 resultlengthneeded=udat_format(datdef, d1, NULL, resultlength, NULL, &status);
513 if(status==U_BUFFER_OVERFLOW_ERROR)
514 {
515 status=U_ZERO_ERROR;
516 resultlength=resultlengthneeded+1;
517 /*result1=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /*this leaks*/
518 result1=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/
519 udat_format(datdef, d1, result1, resultlength, NULL, &status);
520 }
521 if(U_FAILURE(status))
522 {
523 log_err("FAIL: Error in formatting using udat_format(.....): %s\n", myErrorName(status));
524 return 0;
525 }
526
527
528 return result1;
529
530 }
531
532 #endif /* #if !UCONFIG_NO_FORMATTING */
533
534 /*eof*/