]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dtfmtrtts.cpp
ICU-511.27.tar.gz
[apple/icu.git] / icuSources / test / intltest / dtfmtrtts.cpp
1 /***********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2012, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "unicode/datefmt.h"
12 #include "unicode/smpdtfmt.h"
13 #include "unicode/gregocal.h"
14 #include "dtfmtrtts.h"
15 #include "caltest.h"
16
17 #include <stdio.h>
18 #include <string.h>
19
20 // *****************************************************************************
21 // class DateFormatRoundTripTest
22 // *****************************************************************************
23
24 // Useful for turning up subtle bugs: Change the following to TRUE, recompile,
25 // and run while at lunch.
26 // Warning -- makes test run infinite loop!!!
27 #ifndef INFINITE
28 #define INFINITE 0
29 #endif
30
31 static const UVersionInfo ICU_452 = {4,5,2,0};
32
33 // Define this to test just a single locale
34 //#define TEST_ONE_LOC "cs_CZ"
35
36 // If SPARSENESS is > 0, we don't run each exhaustive possibility.
37 // There are 24 total possible tests per each locale. A SPARSENESS
38 // of 12 means we run half of them. A SPARSENESS of 23 means we run
39 // 1 of them. SPARSENESS _must_ be in the range 0..23.
40 int32_t DateFormatRoundTripTest::SPARSENESS = 0;
41 int32_t DateFormatRoundTripTest::TRIALS = 4;
42 int32_t DateFormatRoundTripTest::DEPTH = 5;
43
44 DateFormatRoundTripTest::DateFormatRoundTripTest() : dateFormat(0) {
45 }
46
47 DateFormatRoundTripTest::~DateFormatRoundTripTest() {
48 delete dateFormat;
49 }
50
51 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
52
53 void
54 DateFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
55 {
56 optionv = (par && *par=='v');
57 switch (index) {
58 CASE(0,TestDateFormatRoundTrip)
59 CASE(1, TestCentury)
60 default: name = ""; break;
61 }
62 }
63
64 UBool
65 DateFormatRoundTripTest::failure(UErrorCode status, const char* msg)
66 {
67 if(U_FAILURE(status)) {
68 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
69 return TRUE;
70 }
71
72 return FALSE;
73 }
74
75 UBool
76 DateFormatRoundTripTest::failure(UErrorCode status, const char* msg, const UnicodeString& str)
77 {
78 if(U_FAILURE(status)) {
79 UnicodeString escaped;
80 escape(str,escaped);
81 errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status) + ", str=" + escaped);
82 return TRUE;
83 }
84
85 return FALSE;
86 }
87
88 void DateFormatRoundTripTest::TestCentury()
89 {
90 UErrorCode status = U_ZERO_ERROR;
91 Locale locale("es_PA");
92 UnicodeString pattern = "MM/dd/yy hh:mm:ss a z";
93 SimpleDateFormat fmt(pattern, locale, status);
94 if (U_FAILURE(status)) {
95 dataerrln("Fail: construct SimpleDateFormat: %s", u_errorName(status));
96 return;
97 }
98 UDate date[] = {-55018555891590.05, 0, 0};
99 UnicodeString result[2];
100
101 fmt.format(date[0], result[0]);
102 date[1] = fmt.parse(result[0], status);
103 fmt.format(date[1], result[1]);
104 date[2] = fmt.parse(result[1], status);
105
106 /* This test case worked OK by accident before. date[1] != date[0],
107 * because we use -80/+20 year window for 2-digit year parsing.
108 * (date[0] is in year 1926, date[1] is in year 2026.) result[1] set
109 * by the first format call returns "07/13/26 07:48:28 p.m. PST",
110 * which is correct, because DST was not used in year 1926 in zone
111 * America/Los_Angeles. When this is parsed, date[1] becomes a time
112 * in 2026, which is "07/13/26 08:48:28 p.m. PDT". There was a zone
113 * offset calculation bug that observed DST in 1926, which was resolved.
114 * Before the bug was resolved, result[0] == result[1] was true,
115 * but after the bug fix, the expected result is actually
116 * result[0] != result[1]. -Yoshito
117 */
118 /* TODO: We need to review this code and clarify what we really
119 * want to test here.
120 */
121 //if (date[1] != date[2] || result[0] != result[1]) {
122 if (date[1] != date[2]) {
123 errln("Round trip failure: \"%S\" (%f), \"%S\" (%f)", result[0].getBuffer(), date[1], result[1].getBuffer(), date[2]);
124 }
125 }
126
127 // ==
128
129 void DateFormatRoundTripTest::TestDateFormatRoundTrip()
130 {
131 UErrorCode status = U_ZERO_ERROR;
132
133 getFieldCal = Calendar::createInstance(status);
134 if (U_FAILURE(status)) {
135 dataerrln("Fail: Calendar::createInstance: %s", u_errorName(status));
136 return;
137 }
138
139
140 int32_t locCount = 0;
141 const Locale *avail = DateFormat::getAvailableLocales(locCount);
142 logln("DateFormat available locales: %d", locCount);
143 if(quick) {
144 SPARSENESS = 18;
145 logln("Quick mode: only testing SPARSENESS = 18");
146 }
147 TimeZone *tz = TimeZone::createDefault();
148 UnicodeString temp;
149 logln("Default TimeZone: " + tz->getID(temp));
150 delete tz;
151
152 #ifdef TEST_ONE_LOC // define this to just test ONE locale.
153 Locale loc(TEST_ONE_LOC);
154 test(loc);
155 #if INFINITE
156 for(;;) {
157 test(loc);
158 }
159 #endif
160
161 #else
162 # if INFINITE
163 // Special infinite loop test mode for finding hard to reproduce errors
164 Locale loc = Locale::getDefault();
165 logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName(temp));
166 for(;;)
167 test(loc);
168 # else
169 test(Locale::getDefault());
170
171 #if 1
172 // installed locales
173 for (int i=0; i < locCount; ++i) {
174 test(avail[i]);
175 }
176 #endif
177
178 #if 1
179 // special locales
180 int32_t jCount = CalendarTest::testLocaleCount();
181 for (int32_t j=0; j < jCount; ++j) {
182 test(Locale(CalendarTest::testLocaleID(j)));
183 }
184 #endif
185
186 # endif
187 #endif
188
189 delete getFieldCal;
190 }
191
192 static const char *styleName(DateFormat::EStyle s)
193 {
194 switch(s)
195 {
196 case DateFormat::SHORT: return "SHORT";
197 case DateFormat::MEDIUM: return "MEDIUM";
198 case DateFormat::LONG: return "LONG";
199 case DateFormat::FULL: return "FULL";
200 // case DateFormat::DEFAULT: return "DEFAULT";
201 case DateFormat::DATE_OFFSET: return "DATE_OFFSET";
202 case DateFormat::NONE: return "NONE";
203 case DateFormat::DATE_TIME: return "DATE_TIME";
204 default: return "Unknown";
205 }
206 }
207
208 void DateFormatRoundTripTest::test(const Locale& loc)
209 {
210 UnicodeString temp;
211 #if !INFINITE
212 logln("Locale: " + loc.getDisplayName(temp));
213 #endif
214
215 // Total possibilities = 24
216 // 4 date
217 // 4 time
218 // 16 date-time
219 UBool TEST_TABLE [24];//= new boolean[24];
220 int32_t i = 0;
221 for(i = 0; i < 24; ++i)
222 TEST_TABLE[i] = TRUE;
223
224 // If we have some sparseness, implement it here. Sparseness decreases
225 // test time by eliminating some tests, up to 23.
226 for(i = 0; i < SPARSENESS; ) {
227 int random = (int)(randFraction() * 24);
228 if (random >= 0 && random < 24 && TEST_TABLE[i]) {
229 TEST_TABLE[i] = FALSE;
230 ++i;
231 }
232 }
233
234 int32_t itable = 0;
235 int32_t style = 0;
236 for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
237 if(TEST_TABLE[itable++]) {
238 logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style)));
239 DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc);
240 if(df == NULL) {
241 errln(UnicodeString("Could not DF::createDateInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp));
242 } else {
243 test(df, loc);
244 delete df;
245 }
246 }
247 }
248
249 for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
250 if (TEST_TABLE[itable++]) {
251 logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style)));
252 DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc);
253 if(df == NULL) {
254 errln(UnicodeString("Could not DF::createTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp));
255 } else {
256 test(df, loc, TRUE);
257 delete df;
258 }
259 }
260 }
261
262 for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) {
263 for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) {
264 if(TEST_TABLE[itable++]) {
265 logln("Testing dstyle" + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) );
266 DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc);
267 if(df == NULL) {
268 dataerrln(UnicodeString("Could not DF::createDateTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) + "Locale: " + loc.getDisplayName(temp));
269 } else {
270 test(df, loc);
271 delete df;
272 }
273 }
274 }
275 }
276 }
277
278 void DateFormatRoundTripTest::test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly)
279 {
280 UnicodeString pat;
281 if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
282 errln("DateFormat wasn't a SimpleDateFormat");
283 return;
284 }
285
286 UBool isGregorian = FALSE;
287 UErrorCode minStatus = U_ZERO_ERROR;
288 UDate minDate = CalendarTest::minDateOfCalendar(*fmt->getCalendar(), isGregorian, minStatus);
289 if(U_FAILURE(minStatus)) {
290 errln((UnicodeString)"Failure getting min date for " + origLocale.getName());
291 return;
292 }
293 //logln(UnicodeString("Min date is ") + fullFormat(minDate) + " for " + origLocale.getName());
294
295 pat = ((SimpleDateFormat*)fmt)->toPattern(pat);
296
297 // NOTE TO MAINTAINER
298 // This indexOf check into the pattern needs to be refined to ignore
299 // quoted characters. Currently, this isn't a problem with the locale
300 // patterns we have, but it may be a problem later.
301
302 UBool hasEra = (pat.indexOf(UnicodeString("G")) != -1);
303 UBool hasZoneDisplayName = (pat.indexOf(UnicodeString("z")) != -1) || (pat.indexOf(UnicodeString("v")) != -1)
304 || (pat.indexOf(UnicodeString("V")) != -1);
305
306 // Because patterns contain incomplete data representing the Date,
307 // we must be careful of how we do the roundtrip. We start with
308 // a randomly generated Date because they're easier to generate.
309 // From this we get a string. The string is our real starting point,
310 // because this string should parse the same way all the time. Note
311 // that it will not necessarily parse back to the original date because
312 // of incompleteness in patterns. For example, a time-only pattern won't
313 // parse back to the same date.
314
315 //try {
316 for(int i = 0; i < TRIALS; ++i) {
317 UDate *d = new UDate [DEPTH];
318 UnicodeString *s = new UnicodeString[DEPTH];
319
320 if(isGregorian == TRUE) {
321 d[0] = generateDate();
322 } else {
323 d[0] = generateDate(minDate);
324 }
325
326 UErrorCode status = U_ZERO_ERROR;
327
328 // We go through this loop until we achieve a match or until
329 // the maximum loop count is reached. We record the points at
330 // which the date and the string starts to match. Once matching
331 // starts, it should continue.
332 int loop;
333 int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime()
334 int smatch = 0; // s[smatch].equals(s[smatch-1])
335 for(loop = 0; loop < DEPTH; ++loop) {
336 if (loop > 0) {
337 d[loop] = fmt->parse(s[loop-1], status);
338 failure(status, "fmt->parse", s[loop-1]+" in locale: " + origLocale.getName());
339 status = U_ZERO_ERROR; /* any error would have been reported */
340 }
341
342 s[loop] = fmt->format(d[loop], s[loop]);
343
344 // For displaying which date is being tested
345 //logln(s[loop] + " = " + fullFormat(d[loop]));
346
347 if(s[loop].length() == 0) {
348 errln("FAIL: fmt->format gave 0-length string in " + pat + " with number " + d[loop] + " in locale " + origLocale.getName());
349 }
350
351 if(loop > 0) {
352 if(smatch == 0) {
353 UBool match = s[loop] == s[loop-1];
354 if(smatch == 0) {
355 if(match)
356 smatch = loop;
357 }
358 else if( ! match)
359 errln("FAIL: String mismatch after match");
360 }
361
362 if(dmatch == 0) {
363 // {sfb} watch out here, this might not work
364 UBool match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/;
365 if(dmatch == 0) {
366 if(match)
367 dmatch = loop;
368 }
369 else if( ! match)
370 errln("FAIL: Date mismatch after match");
371 }
372
373 if(smatch != 0 && dmatch != 0)
374 break;
375 }
376 }
377 // At this point loop == DEPTH if we've failed, otherwise loop is the
378 // max(smatch, dmatch), that is, the index at which we have string and
379 // date matching.
380
381 // Date usually matches in 2. Exceptions handled below.
382 int maxDmatch = 2;
383 int maxSmatch = 1;
384 if (dmatch > maxDmatch) {
385 // Time-only pattern with zone information and a starting date in PST.
386 if(timeOnly && hasZoneDisplayName) {
387 int32_t startRaw, startDst;
388 fmt->getTimeZone().getOffset(d[0], FALSE, startRaw, startDst, status);
389 failure(status, "TimeZone::getOffset");
390 // if the start offset is greater than the offset on Jan 1, 1970
391 // in PST, then need one more round trip. There are two cases
392 // fall into this category. The start date is 1) DST or
393 // 2) LMT (GMT-07:52:58).
394 if (startRaw + startDst > -28800000) {
395 maxDmatch = 3;
396 maxSmatch = 2;
397 }
398 }
399 }
400
401 // String usually matches in 1. Exceptions are checked for here.
402 if(smatch > maxSmatch) { // Don't compute unless necessary
403 UBool in0;
404 // Starts in BC, with no era in pattern
405 if( ! hasEra && getField(d[0], UCAL_ERA) == GregorianCalendar::BC)
406 maxSmatch = 2;
407 // Starts in DST, no year in pattern
408 else if((in0=fmt->getTimeZone().inDaylightTime(d[0], status)) && ! failure(status, "gettingDaylightTime") &&
409 pat.indexOf(UnicodeString("yyyy")) == -1)
410 maxSmatch = 2;
411 // If we start not in DST, but transition into DST
412 else if (!in0 &&
413 fmt->getTimeZone().inDaylightTime(d[1], status) && !failure(status, "gettingDaylightTime"))
414 maxSmatch = 2;
415 // Two digit year with no time zone change,
416 // unless timezone isn't used or we aren't close to the DST changover
417 else if (pat.indexOf(UnicodeString("y")) != -1
418 && pat.indexOf(UnicodeString("yyyy")) == -1
419 && getField(d[0], UCAL_YEAR)
420 != getField(d[dmatch], UCAL_YEAR)
421 && !failure(status, "error status [smatch>maxSmatch]")
422 && ((hasZoneDisplayName
423 && (fmt->getTimeZone().inDaylightTime(d[0], status)
424 == fmt->getTimeZone().inDaylightTime(d[dmatch], status)
425 || getField(d[0], UCAL_MONTH) == UCAL_APRIL
426 || getField(d[0], UCAL_MONTH) == UCAL_OCTOBER))
427 || !hasZoneDisplayName)
428 )
429 {
430 maxSmatch = 2;
431 }
432 // If zone display name is used, fallback format might be used before 1970
433 else if (hasZoneDisplayName && d[0] < 0) {
434 maxSmatch = 2;
435 }
436 }
437
438 /*
439 * Special case for Japanese and Buddhist (could have large negative years)
440 * Also, Hebrew calendar need help handling leap month.
441 */
442 if(dmatch > maxDmatch || smatch > maxSmatch) {
443 const char *type = fmt->getCalendar()->getType();
444 if(!strcmp(type,"japanese") || (!strcmp(type,"buddhist"))) {
445 maxSmatch = 4;
446 maxDmatch = 4;
447 } else if(!strcmp(type,"hebrew")) {
448 maxSmatch = 3;
449 maxDmatch = 3;
450 }
451 }
452
453 // Use @v to see verbose results on successful cases
454 UBool fail = (dmatch > maxDmatch || smatch > maxSmatch);
455 if (optionv || fail) {
456 if (fail) {
457 errln(UnicodeString("\nFAIL: Pattern: ") + pat +
458 " in Locale: " + origLocale.getName());
459 } else {
460 errln(UnicodeString("\nOk: Pattern: ") + pat +
461 " in Locale: " + origLocale.getName());
462 }
463
464 logln("Date iters until match=%d (max allowed=%d), string iters until match=%d (max allowed=%d)",
465 dmatch,maxDmatch, smatch, maxSmatch);
466
467 for(int j = 0; j <= loop && j < DEPTH; ++j) {
468 UnicodeString temp;
469 FieldPosition pos(FieldPosition::DONT_CARE);
470 errln((j>0?" P> ":" ") + fullFormat(d[j]) + " F> " +
471 escape(s[j], temp) + UnicodeString(" d=") + d[j] +
472 (j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") +
473 (j > 0 && s[j] == s[j-1]?" s==":""));
474 }
475 }
476 delete[] d;
477 delete[] s;
478 }
479 /*}
480 catch (ParseException e) {
481 errln("Exception: " + e.getMessage());
482 logln(e.toString());
483 }*/
484 }
485
486 const UnicodeString& DateFormatRoundTripTest::fullFormat(UDate d) {
487 UErrorCode ec = U_ZERO_ERROR;
488 if (dateFormat == 0) {
489 dateFormat = new SimpleDateFormat((UnicodeString)"EEE MMM dd HH:mm:ss.SSS zzz yyyy G", ec);
490 if (U_FAILURE(ec) || dateFormat == 0) {
491 fgStr = "[FAIL: SimpleDateFormat constructor]";
492 delete dateFormat;
493 dateFormat = 0;
494 return fgStr;
495 }
496 }
497 fgStr.truncate(0);
498 dateFormat->format(d, fgStr);
499 return fgStr;
500 }
501
502 /**
503 * Return a field of the given date
504 */
505 int32_t DateFormatRoundTripTest::getField(UDate d, int32_t f) {
506 // Should be synchronized, but we're single threaded so it's ok
507 UErrorCode status = U_ZERO_ERROR;
508 getFieldCal->setTime(d, status);
509 failure(status, "getfieldCal->setTime");
510 int32_t ret = getFieldCal->get((UCalendarDateFields)f, status);
511 failure(status, "getfieldCal->get");
512 return ret;
513 }
514
515 UnicodeString& DateFormatRoundTripTest::escape(const UnicodeString& src, UnicodeString& dst )
516 {
517 dst.remove();
518 for (int32_t i = 0; i < src.length(); ++i) {
519 UChar c = src[i];
520 if(c < 0x0080)
521 dst += c;
522 else {
523 dst += UnicodeString("[");
524 char buf [8];
525 sprintf(buf, "%#x", c);
526 dst += UnicodeString(buf);
527 dst += UnicodeString("]");
528 }
529 }
530
531 return dst;
532 }
533
534 #define U_MILLIS_PER_YEAR (365.25 * 24 * 60 * 60 * 1000)
535
536 UDate DateFormatRoundTripTest::generateDate(UDate minDate)
537 {
538 // Bring range in conformance to generateDate() below.
539 if(minDate < (U_MILLIS_PER_YEAR * -(4000-1970))) {
540 minDate = (U_MILLIS_PER_YEAR * -(4000-1970));
541 }
542 for(int i=0;i<8;i++) {
543 double a = randFraction();
544
545 // Range from (min) to (8000-1970) AD
546 double dateRange = (0.0 - minDate) + (U_MILLIS_PER_YEAR + (8000-1970));
547
548 a *= dateRange;
549
550 // Now offset from minDate
551 a += minDate;
552
553 // Last sanity check
554 if(a>=minDate) {
555 return a;
556 }
557 }
558 return minDate;
559 }
560
561 UDate DateFormatRoundTripTest::generateDate()
562 {
563 double a = randFraction();
564
565 // Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years
566 a *= 8000;
567
568 // Range from (4000-1970) BC to (8000-1970) AD
569 a -= 4000;
570
571 // Now scale up to ms
572 a *= 365.25 * 24 * 60 * 60 * 1000;
573
574 //return new Date((long)a);
575 return a;
576 }
577
578 #endif /* #if !UCONFIG_NO_FORMATTING */
579
580 //eof