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