X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/57a6839dcb3bba09e8228b822b290604668416fe..HEAD:/icuSources/test/intltest/tzfmttst.cpp diff --git a/icuSources/test/intltest/tzfmttst.cpp b/icuSources/test/intltest/tzfmttst.cpp index fe0998bc..40362aac 100644 --- a/icuSources/test/intltest/tzfmttst.cpp +++ b/icuSources/test/intltest/tzfmttst.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* -* Copyright (C) 2007-2014, International Business Machines Corporation and * +* Copyright (C) 2007-2015, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -10,7 +12,6 @@ #include "tzfmttst.h" -#include "simplethread.h" #include "unicode/timezone.h" #include "unicode/simpletz.h" #include "unicode/calendar.h" @@ -20,7 +21,13 @@ #include "unicode/basictz.h" #include "unicode/tzfmt.h" #include "unicode/localpointer.h" +#include "unicode/utf16.h" + #include "cstring.h" +#include "cstr.h" +#include "mutex.h" +#include "simplethread.h" +#include "uassert.h" #include "zonemeta.h" static const char* PATTERNS[] = { @@ -47,7 +54,6 @@ static const char* PATTERNS[] = { "VVV", "VVVV" }; -static const int NUM_PATTERNS = sizeof(PATTERNS)/sizeof(const char*); static const UChar ETC_UNKNOWN[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0}; @@ -76,7 +82,11 @@ TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name TESTCASE(2, TestParse); TESTCASE(3, TestISOFormat); TESTCASE(4, TestFormat); - default: name = ""; break; + TESTCASE(5, TestFormatTZDBNames); + TESTCASE(6, TestFormatCustomZone); + TESTCASE(7, TestFormatTZDBNamesAllZoneCoverage); + TESTCASE(8, TestAdoptDefaultThreadSafe); + default: name = ""; break; } } @@ -115,8 +125,8 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { } // Set up test dates - UDate DATES[(sizeof(testDateData)/sizeof(int32_t))/3]; - const int32_t nDates = (sizeof(testDateData)/sizeof(int32_t))/3; + UDate DATES[UPRV_LENGTHOF(testDateData)]; + const int32_t nDates = UPRV_LENGTHOF(testDateData); cal->clear(); for (int32_t i = 0; i < nDates; i++) { cal->set(testDateData[i][0], testDateData[i][1], testDateData[i][2]); @@ -132,7 +142,9 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { Locale("en"), Locale("en_CA"), Locale("fr"), - Locale("zh_Hant") + Locale("zh_Hant"), + Locale("fa"), + Locale("ccp") }; const Locale *LOCALES; @@ -140,7 +152,7 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { if (quick) { LOCALES = testLocales; - nLocales = sizeof(testLocales)/sizeof(Locale); + nLocales = UPRV_LENGTHOF(testLocales); } else { LOCALES = Locale::getAvailableLocales(nLocales); } @@ -160,8 +172,7 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { gmtFmt.setTimeZone(*TimeZone::getGMT()); gmtFmt.format(0.0, localGMTString); - for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) { - + for (int32_t patidx = 0; patidx < UPRV_LENGTHOF(PATTERNS); patidx++) { SimpleDateFormat *sdf = new SimpleDateFormat((UnicodeString)PATTERNS[patidx], LOCALES[locidx], status); if (U_FAILURE(status)) { dataerrln((UnicodeString)"new SimpleDateFormat failed for pattern " + @@ -177,7 +188,7 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { for (int32_t datidx = 0; datidx < nDates; datidx++) { UnicodeString tzstr; - FieldPosition fpos(0); + FieldPosition fpos(FieldPosition::DONT_CARE); // Format sdf->setTimeZone(*tz); sdf->format(DATES[datidx], tzstr, fpos); @@ -292,10 +303,13 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) { if (!isOffsetFormat) { // Check if localized GMT format is used as a fallback of name styles int32_t numDigits = 0; - for (int n = 0; n < tzstr.length(); n++) { - if (u_isdigit(tzstr.charAt(n))) { + int32_t idx = 0; + while (idx < tzstr.length()) { + UChar32 cp = tzstr.char32At(idx); + if (u_isdigit(cp)) { numDigits++; } + idx += U16_LENGTH(cp); } isOffsetFormat = (numDigits > 0); } @@ -368,251 +382,71 @@ static UBool isSpecialTimeRoundTripCase(const char* loc, return isExcluded; } +// LocaleData. Somewhat misnamed. For TestTimeZoneRoundTrip, specifies the locales and patterns +// to be tested, and provides an iterator over these for the multi-threaded test +// functions to pick up the next combination to be tested. +// +// A single global instance of this struct is shared among all +// the test threads. +// +// "locales" is an array of locales to be tested. +// PATTERNS (a global) is an array of patterns to be tested for each locale. +// "localeIndex" and "patternIndex" keep track of the iteration through the above. +// Each of the parallel test threads calls LocaleData::nextTest() in a loop +// to find out what to test next. It must be thread safe. struct LocaleData { - int32_t index; + int32_t localeIndex; + int32_t patternIndex; int32_t testCounts; - UDate *times; - const Locale* locales; // Static - int32_t nLocales; // Static - UBool quick; // Static - UDate START_TIME; // Static - UDate END_TIME; // Static + UDate times[UPRV_LENGTHOF(PATTERNS)]; // Performance data, Elapsed time for each pattern. + const Locale* locales; + int32_t nLocales; + UDate START_TIME; + UDate END_TIME; int32_t numDone; -}; - -class TestTimeRoundTripThread: public SimpleThread { -public: - TestTimeRoundTripThread(IntlTest& tlog, LocaleData &ld, int32_t i) - : log(tlog), data(ld), index(i) {} - virtual void run() { - UErrorCode status = U_ZERO_ERROR; - UBool REALLY_VERBOSE = FALSE; - - // These patterns are ambiguous at DST->STD local time overlap - const char* AMBIGUOUS_DST_DECESSION[] = { "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 }; - - // These patterns are ambiguous at STD->STD/DST->DST local time overlap - const char* AMBIGUOUS_NEGATIVE_SHIFT[] = { "z", "zzzz", "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 }; - - // These patterns only support integer minutes offset - const char* MINUTES_OFFSET[] = { "X", "XX", "XXX", "x", "xx", "xxx", 0 }; - - // Workaround for #6338 - //UnicodeString BASEPATTERN("yyyy-MM-dd'T'HH:mm:ss.SSS"); - UnicodeString BASEPATTERN("yyyy.MM.dd HH:mm:ss.SSS"); - - // timer for performance analysis - UDate timer; - UDate testTimes[4]; - UBool expectedRoundTrip[4]; - int32_t testLen = 0; - - StringEnumeration *tzids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status); - if (U_FAILURE(status)) { - if (status == U_MISSING_RESOURCE_ERROR) { - /* This error is generally caused by data not being present. However, an infinite loop will occur - * because the thread thinks that the test data is never done so we should treat the data as done. - */ - log.dataerrln("TimeZone::createTimeZoneIDEnumeration failed - %s", u_errorName(status)); - data.numDone = data.nLocales; - } else { - log.errln("TimeZone::createTimeZoneIDEnumeration failed: %s", u_errorName(status)); - } - return; - } - int32_t locidx = -1; - UDate times[NUM_PATTERNS]; - for (int32_t i = 0; i < NUM_PATTERNS; i++) { + LocaleData() : localeIndex(0), patternIndex(0), testCounts(0), locales(NULL), + nLocales(0), START_TIME(0), END_TIME(0), numDone(0) { + for (int i=0; ireset(status); - const UnicodeString *tzid; - - timer = Calendar::getNow(); - - while ((tzid = tzids->snext(status))) { - if (uprv_strcmp(PATTERNS[patidx], "V") == 0) { - // Some zones do not have short ID assigned, such as Asia/Riyadh87. - // The time roundtrip will fail for such zones with pattern "V" (short zone ID). - // This is expected behavior. - const UChar* shortZoneID = ZoneMeta::getShortID(*tzid); - if (shortZoneID == NULL) { - continue; - } - } else if (uprv_strcmp(PATTERNS[patidx], "VVV") == 0) { - // Some zones are not associated with any region, such as Etc/GMT+8. - // The time roundtrip will fail for such zone with pattern "VVV" (exemplar location). - // This is expected behavior. - if (tzid->indexOf((UChar)0x2F) < 0 || tzid->indexOf(ETC_SLASH, -1, 0) >= 0 - || tzid->indexOf(SYSTEMV_SLASH, -1, 0) >= 0 || tzid->indexOf(RIYADH8, -1, 0) >= 0) { - continue; - } - } - - if (*tzid == "Pacific/Apia" && uprv_strcmp(PATTERNS[patidx], "vvvv") == 0 - && log.logKnownIssue("11052", "Ambiguous zone name - Samoa Time")) { - continue; - } - - BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid); - sdf->setTimeZone(*tz); - - UDate t = data.START_TIME; - TimeZoneTransition tzt; - UBool tztAvail = FALSE; - UBool middle = TRUE; - - while (t < data.END_TIME) { - if (!tztAvail) { - testTimes[0] = t; - expectedRoundTrip[0] = TRUE; - testLen = 1; - } else { - int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings(); - int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings(); - int32_t delta = toOffset - fromOffset; - if (delta < 0) { - UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0; - testTimes[0] = t + delta - 1; - expectedRoundTrip[0] = TRUE; - testTimes[1] = t + delta; - expectedRoundTrip[1] = isDstDecession ? - !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) : - !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]); - testTimes[2] = t - 1; - expectedRoundTrip[2] = isDstDecession ? - !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) : - !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]); - testTimes[3] = t; - expectedRoundTrip[3] = TRUE; - testLen = 4; - } else { - testTimes[0] = t - 1; - expectedRoundTrip[0] = TRUE; - testTimes[1] = t; - expectedRoundTrip[1] = TRUE; - testLen = 2; - } - } - for (int32_t testidx = 0; testidx < testLen; testidx++) { - if (data.quick) { - // reduce regular test time - if (!expectedRoundTrip[testidx]) { - continue; - } - } - - testCounts++; - - UnicodeString text; - FieldPosition fpos(0); - sdf->format(testTimes[testidx], text, fpos); - - UDate parsedDate = sdf->parse(text, status); - if (U_FAILURE(status)) { - log.errln((UnicodeString) "Parse failure for text=" + text + ", tzid=" + *tzid + ", locale=" + data.locales[locidx].getName() - + ", pattern=" + PATTERNS[patidx] + ", time=" + testTimes[testidx]); - status = U_ZERO_ERROR; - continue; - } + void resetTestIteration() { + localeIndex = -1; + patternIndex = UPRV_LENGTHOF(PATTERNS); + numDone = 0; + } - int32_t timeDiff = (int32_t)(parsedDate - testTimes[testidx]); - UBool bTimeMatch = minutesOffset ? - (timeDiff/60000)*60000 == 0 : timeDiff == 0; - if (!bTimeMatch) { - UnicodeString msg = (UnicodeString) "Time round trip failed for " + "tzid=" + *tzid + ", locale=" + data.locales[locidx].getName() + ", pattern=" + PATTERNS[patidx] - + ", text=" + text + ", time=" + testTimes[testidx] + ", restime=" + parsedDate + ", diff=" + (parsedDate - testTimes[testidx]); - // Timebomb for TZData update - if (expectedRoundTrip[testidx] - && !isSpecialTimeRoundTripCase(data.locales[locidx].getName(), *tzid, - PATTERNS[patidx], testTimes[testidx])) { - log.errln((UnicodeString) "FAIL: " + msg); - } else if (REALLY_VERBOSE) { - log.logln(msg); - } - } - } - tztAvail = tz->getNextTransition(t, FALSE, tzt); - if (!tztAvail) { - break; - } - if (middle) { - // Test the date in the middle of two transitions. - t += (int64_t) ((tzt.getTime() - t) / 2); - middle = FALSE; - tztAvail = FALSE; - } else { - t = tzt.getTime(); - } - } - delete tz; - } - times[patidx] += (Calendar::getNow() - timer); - delete sdf; + UBool nextTest(int32_t &rLocaleIndex, int32_t &rPatternIndex) { + Mutex lock; + if (patternIndex >= UPRV_LENGTHOF(PATTERNS) - 1) { + if (localeIndex >= nLocales - 1) { + return FALSE; } - umtx_lock(NULL); - data.numDone++; - umtx_unlock(NULL); + patternIndex = -1; + ++localeIndex; } - delete tzids; + ++patternIndex; + rLocaleIndex = localeIndex; + rPatternIndex = patternIndex; + ++numDone; + return TRUE; + } + + void addTime(UDate amount, int32_t patIdx) { + Mutex lock; + U_ASSERT(patIdx < UPRV_LENGTHOF(PATTERNS)); + times[patIdx] += amount; } -private: - IntlTest& log; - LocaleData& data; - int32_t index; }; +static LocaleData *gLocaleData = NULL; + void TimeZoneFormatTest::TestTimeRoundTrip(void) { - int32_t nThreads = threadCount; - const Locale *LOCALES; - int32_t nLocales; - int32_t testCounts = 0; - UErrorCode status = U_ZERO_ERROR; - Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString) "UTC"), status); + LocalPointer cal(Calendar::createInstance(TimeZone::createTimeZone((UnicodeString) "UTC"), status)); if (U_FAILURE(status)) { dataerrln("Calendar::createInstance failed: %s", u_errorName(status)); return; @@ -625,11 +459,11 @@ TimeZoneFormatTest::TestTimeRoundTrip(void) { if (bTestAll || !quick) { cal->set(1900, UCAL_JANUARY, 1); } else { - cal->set(1990, UCAL_JANUARY, 1); + cal->set(1999, UCAL_JANUARY, 1); } START_TIME = cal->getTime(status); - cal->set(2015, UCAL_JANUARY, 1); + cal->set(2022, UCAL_JANUARY, 1); END_TIME = cal->getTime(status); if (U_FAILURE(status)) { @@ -637,10 +471,8 @@ TimeZoneFormatTest::TestTimeRoundTrip(void) { return; } - UDate times[NUM_PATTERNS]; - for (int32_t i = 0; i < NUM_PATTERNS; i++) { - times[i] = 0; - } + LocaleData localeData; + gLocaleData = &localeData; // Set up test locales const Locale locales1[] = {Locale("en")}; @@ -653,80 +485,278 @@ TimeZoneFormatTest::TestTimeRoundTrip(void) { Locale("ko_KR"), Locale("nb_NO"), Locale("nl_NL"), Locale("nn_NO"), Locale("pl_PL"), Locale("pt"), Locale("pt_BR"), Locale("pt_PT"), Locale("ru_RU"), Locale("sv_SE"), Locale("th_TH"), Locale("tr_TR"), Locale("zh"), Locale("zh_Hans"), Locale("zh_Hans_CN"), - Locale("zh_Hant"), Locale("zh_Hant_TW") + Locale("zh_Hant"), Locale("zh_Hant_TW"), Locale("fa"), Locale("ccp") }; if (bTestAll) { - LOCALES = Locale::getAvailableLocales(nLocales); + gLocaleData->locales = Locale::getAvailableLocales(gLocaleData->nLocales); } else if (quick) { - LOCALES = locales1; - nLocales = sizeof(locales1)/sizeof(Locale); + gLocaleData->locales = locales1; + gLocaleData->nLocales = UPRV_LENGTHOF(locales1); } else { - LOCALES = locales2; - nLocales = sizeof(locales2)/sizeof(Locale); + gLocaleData->locales = locales2; + gLocaleData->nLocales = UPRV_LENGTHOF(locales2); } - LocaleData data; - data.index = 0; - data.testCounts = testCounts; - data.times = times; - data.locales = LOCALES; - data.nLocales = nLocales; - data.quick = quick; - data.START_TIME = START_TIME; - data.END_TIME = END_TIME; - data.numDone = 0; - -#if (ICU_USE_THREADS==0) - TestTimeRoundTripThread fakeThread(*this, data, 0); - fakeThread.run(); -#else - TestTimeRoundTripThread **threads = new TestTimeRoundTripThread*[threadCount]; - int32_t i; - for (i = 0; i < nThreads; i++) { - threads[i] = new TestTimeRoundTripThread(*this, data, i); - if (threads[i]->start() != 0) { - errln("Error starting thread %d", i); - } - } + gLocaleData->START_TIME = START_TIME; + gLocaleData->END_TIME = END_TIME; + gLocaleData->resetTestIteration(); - UBool done = false; - while (true) { - umtx_lock(NULL); - if (data.numDone == nLocales) { - done = true; - } - umtx_unlock(NULL); - if (done) - break; - SimpleThread::sleep(1000); - } + // start IntlTest.threadCount threads, each running the function RunTimeRoundTripTests(). - for (i = 0; i < nThreads; i++) { - delete threads[i]; - } - delete [] threads; + ThreadPool threads(this, threadCount, &TimeZoneFormatTest::RunTimeRoundTripTests); + threads.start(); // Start all threads. + threads.join(); // Wait for all threads to finish. -#endif UDate total = 0; logln("### Elapsed time by patterns ###"); - for (int32_t i = 0; i < NUM_PATTERNS; i++) { - logln(UnicodeString("") + data.times[i] + "ms (" + PATTERNS[i] + ")"); - total += data.times[i]; + for (int32_t i = 0; i < UPRV_LENGTHOF(PATTERNS); i++) { + logln(UnicodeString("") + gLocaleData->times[i] + "ms (" + PATTERNS[i] + ")"); + total += gLocaleData->times[i]; } logln((UnicodeString) "Total: " + total + "ms"); - logln((UnicodeString) "Iteration: " + data.testCounts); + logln((UnicodeString) "Iteration: " + gLocaleData->testCounts); +} - delete cal; + +// TimeZoneFormatTest::RunTimeRoundTripTests() +// This function loops, running time zone format round trip test cases until there are no more, then returns. +// Threading: multiple invocations of this function are started in parallel +// by TimeZoneFormatTest::TestTimeRoundTrip() +// +void TimeZoneFormatTest::RunTimeRoundTripTests(int32_t threadNumber) { + UErrorCode status = U_ZERO_ERROR; + UBool REALLY_VERBOSE = FALSE; + + // These patterns are ambiguous at DST->STD local time overlap + const char* AMBIGUOUS_DST_DECESSION[] = { "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 }; + + // These patterns are ambiguous at STD->STD/DST->DST local time overlap + const char* AMBIGUOUS_NEGATIVE_SHIFT[] = { "z", "zzzz", "v", "vvvv", "V", "VV", "VVV", "VVVV", 0 }; + + // These patterns only support integer minutes offset + const char* MINUTES_OFFSET[] = { "X", "XX", "XXX", "x", "xx", "xxx", 0 }; + + // Workaround for #6338 + //UnicodeString BASEPATTERN("yyyy-MM-dd'T'HH:mm:ss.SSS"); + UnicodeString BASEPATTERN("yyyy.MM.dd HH:mm:ss.SSS"); + + // timer for performance analysis + UDate timer; + UDate testTimes[4]; + UBool expectedRoundTrip[4]; + int32_t testLen = 0; + + StringEnumeration *tzids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status); + if (U_FAILURE(status)) { + if (status == U_MISSING_RESOURCE_ERROR) { + // This error is generally caused by data not being present. + dataerrln("TimeZone::createTimeZoneIDEnumeration failed - %s", u_errorName(status)); + } else { + errln("TimeZone::createTimeZoneIDEnumeration failed: %s", u_errorName(status)); + } + return; + } + + int32_t locidx = -1; + int32_t patidx = -1; + + while (gLocaleData->nextTest(locidx, patidx)) { + + UnicodeString pattern(BASEPATTERN); + pattern.append(" ").append(PATTERNS[patidx]); + logln(" Thread %d, Locale %s, Pattern %s", + threadNumber, gLocaleData->locales[locidx].getName(), CStr(pattern)()); + + SimpleDateFormat *sdf = new SimpleDateFormat(pattern, gLocaleData->locales[locidx], status); + if (U_FAILURE(status)) { + errcheckln(status, (UnicodeString) "new SimpleDateFormat failed for pattern " + + pattern + " for locale " + gLocaleData->locales[locidx].getName() + " - " + u_errorName(status)); + status = U_ZERO_ERROR; + continue; + } + + UBool minutesOffset = contains(MINUTES_OFFSET, PATTERNS[patidx]); + + tzids->reset(status); + const UnicodeString *tzid; + + timer = Calendar::getNow(); + + while ((tzid = tzids->snext(status))) { + if (uprv_strcmp(PATTERNS[patidx], "V") == 0) { + // Some zones do not have short ID assigned, such as Asia/Riyadh87. + // The time roundtrip will fail for such zones with pattern "V" (short zone ID). + // This is expected behavior. + const UChar* shortZoneID = ZoneMeta::getShortID(*tzid); + if (shortZoneID == NULL) { + continue; + } + } else if (uprv_strcmp(PATTERNS[patidx], "VVV") == 0) { + // Some zones are not associated with any region, such as Etc/GMT+8. + // The time roundtrip will fail for such zone with pattern "VVV" (exemplar location). + // This is expected behavior. + if (tzid->indexOf((UChar)0x2F) < 0 || tzid->indexOf(ETC_SLASH, -1, 0) >= 0 + || tzid->indexOf(SYSTEMV_SLASH, -1, 0) >= 0 || tzid->indexOf(RIYADH8, -1, 0) >= 0) { + continue; + } + } + + if ((*tzid == "Pacific/Apia" || *tzid == "Pacific/Midway" || *tzid == "Pacific/Pago_Pago") + && uprv_strcmp(PATTERNS[patidx], "vvvv") == 0 + && logKnownIssue("11052", "Ambiguous zone name - Samoa Time")) { + continue; + } + + BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid); + sdf->setTimeZone(*tz); + + UDate t = gLocaleData->START_TIME; + TimeZoneTransition tzt; + UBool tztAvail = FALSE; + UBool middle = TRUE; + + while (t < gLocaleData->END_TIME) { + if (!tztAvail) { + testTimes[0] = t; + expectedRoundTrip[0] = TRUE; + testLen = 1; + } else { + int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings(); + int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings(); + int32_t delta = toOffset - fromOffset; + if (delta < 0) { + UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0; + testTimes[0] = t + delta - 1; + expectedRoundTrip[0] = TRUE; + testTimes[1] = t + delta; + expectedRoundTrip[1] = isDstDecession ? + !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) : + !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]); + testTimes[2] = t - 1; + expectedRoundTrip[2] = isDstDecession ? + !contains(AMBIGUOUS_DST_DECESSION, PATTERNS[patidx]) : + !contains(AMBIGUOUS_NEGATIVE_SHIFT, PATTERNS[patidx]); + testTimes[3] = t; + expectedRoundTrip[3] = TRUE; + testLen = 4; + } else { + testTimes[0] = t - 1; + expectedRoundTrip[0] = TRUE; + testTimes[1] = t; + expectedRoundTrip[1] = TRUE; + testLen = 2; + } + } + for (int32_t testidx = 0; testidx < testLen; testidx++) { + if (quick) { + // reduce regular test time + if (!expectedRoundTrip[testidx]) { + continue; + } + } + + { + Mutex lock; + gLocaleData->testCounts++; + } + + UnicodeString text; + FieldPosition fpos(FieldPosition::DONT_CARE); + sdf->format(testTimes[testidx], text, fpos); + + UDate parsedDate = sdf->parse(text, status); + if (U_FAILURE(status)) { + errln((UnicodeString) "Parse failure for text=" + text + ", tzid=" + *tzid + ", locale=" + gLocaleData->locales[locidx].getName() + + ", pattern=" + PATTERNS[patidx] + ", time=" + testTimes[testidx]); + status = U_ZERO_ERROR; + continue; + } + + int32_t timeDiff = (int32_t)(parsedDate - testTimes[testidx]); + UBool bTimeMatch = minutesOffset ? + (timeDiff/60000)*60000 == 0 : timeDiff == 0; + if (!bTimeMatch) { + UnicodeString msg = (UnicodeString) "Time round trip failed for " + "tzid=" + *tzid + + ", locale=" + gLocaleData->locales[locidx].getName() + ", pattern=" + PATTERNS[patidx] + + ", text=" + text + ", time=" + testTimes[testidx] + ", restime=" + parsedDate + ", diff=" + (parsedDate - testTimes[testidx]); + // Timebomb for TZData update + if (expectedRoundTrip[testidx] + && !isSpecialTimeRoundTripCase(gLocaleData->locales[locidx].getName(), *tzid, + PATTERNS[patidx], testTimes[testidx])) { + errln((UnicodeString) "FAIL: " + msg); + } else if (REALLY_VERBOSE) { + logln(msg); + } + } + } + tztAvail = tz->getNextTransition(t, FALSE, tzt); + if (!tztAvail) { + break; + } + if (middle) { + // Test the date in the middle of two transitions. + t += (int64_t) ((tzt.getTime() - t) / 2); + middle = FALSE; + tztAvail = FALSE; + } else { + t = tzt.getTime(); + } + } + delete tz; + } + UDate elapsedTime = Calendar::getNow() - timer; + gLocaleData->addTime(elapsedTime, patidx); + delete sdf; + } + delete tzids; } +void +TimeZoneFormatTest::TestAdoptDefaultThreadSafe(void) { + ThreadPool threads(this, threadCount, &TimeZoneFormatTest::RunAdoptDefaultThreadSafeTests); + threads.start(); // Start all threads. + threads.join(); // Wait for all threads to finish. +} + +static const int32_t kAdoptDefaultIteration = 10; +static const int32_t kCreateDefaultIteration = 5000; +static const int64_t kStartTime = 1557288964845; + +void TimeZoneFormatTest::RunAdoptDefaultThreadSafeTests(int32_t threadNumber) { + UErrorCode status = U_ZERO_ERROR; + if (threadNumber % 2 == 0) { + for (int32_t i = 0; i < kAdoptDefaultIteration; i++) { + std::unique_ptr timezones( + icu::TimeZone::createEnumeration()); + // Fails with missing data. + if (!assertTrue(WHERE, (bool)timezones, false, true)) {return;} + while (const icu::UnicodeString* timezone = timezones->snext(status)) { + status = U_ZERO_ERROR; + icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(*timezone)); + } + } + } else { + int32_t rawOffset; + int32_t dstOffset; + int64_t date = kStartTime; + for (int32_t i = 0; i < kCreateDefaultIteration; i++) { + date += 6000 * i; + std::unique_ptr tz(icu::TimeZone::createDefault()); + status = U_ZERO_ERROR; + tz->getOffset(date, TRUE, rawOffset, dstOffset, status); + status = U_ZERO_ERROR; + tz->getOffset(date, FALSE, rawOffset, dstOffset, status); + } + } +} typedef struct { const char* text; int32_t inPos; const char* locale; UTimeZoneFormatStyle style; - UBool parseAll; + uint32_t parseOptions; const char* expected; int32_t outPos; UTimeZoneFormatTimeType timeType; @@ -735,26 +765,94 @@ typedef struct { void TimeZoneFormatTest::TestParse(void) { const ParseTestData DATA[] = { - // text inPos locale style parseAll expected outPos timeType - {"Z", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, - {"Z", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, false, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, - {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, true, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, - {"Zambia time", 0, "en_US", UTZFMT_STYLE_GENERIC_LOCATION, false, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN}, - {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, true, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN}, - {"+00:00", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "Etc/GMT", 6, UTZFMT_TIME_TYPE_UNKNOWN}, - {"-01:30:45", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, false, "GMT-01:30:45", 9, UTZFMT_TIME_TYPE_UNKNOWN}, - {"-7", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-07:00", 2, UTZFMT_TIME_TYPE_UNKNOWN}, - {"-2222", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-22:22", 5, UTZFMT_TIME_TYPE_UNKNOWN}, - {"-3333", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, false, "GMT-03:33", 4, UTZFMT_TIME_TYPE_UNKNOWN}, - {"XXX+01:30YYY", 3, "en_US", UTZFMT_STYLE_LOCALIZED_GMT, false, "GMT+01:30", 9, UTZFMT_TIME_TYPE_UNKNOWN}, - {"GMT0", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "Etc/GMT", 3, UTZFMT_TIME_TYPE_UNKNOWN}, - {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, - {"ESTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, - {"EDTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/New_York", 3, UTZFMT_TIME_TYPE_DAYLIGHT}, - {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, false, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}, - {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, true, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, - {"EST", 0, "en_CA", UTZFMT_STYLE_SPECIFIC_SHORT, false, "America/Toronto", 3, UTZFMT_TIME_TYPE_STANDARD}, - {NULL, 0, NULL, UTZFMT_STYLE_GENERIC_LOCATION, false, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN} + // text inPos locale style + // parseOptions expected outPos timeType + {"Z", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, + UTZFMT_PARSE_OPTION_NONE, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"Z", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_NONE, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, + UTZFMT_PARSE_OPTION_ALL_STYLES, "Etc/GMT", 1, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"Zambia time", 0, "en_US", UTZFMT_STYLE_GENERIC_LOCATION, + UTZFMT_PARSE_OPTION_NONE, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"Zambia time", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, + UTZFMT_PARSE_OPTION_ALL_STYLES, "Africa/Lusaka", 11, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"+00:00", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, + UTZFMT_PARSE_OPTION_NONE, "Etc/GMT", 6, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"-01:30:45", 0, "en_US", UTZFMT_STYLE_ISO_EXTENDED_FULL, + UTZFMT_PARSE_OPTION_NONE, "GMT-01:30:45", 9, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"-7", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, + UTZFMT_PARSE_OPTION_NONE, "GMT-07:00", 2, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"-2222", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, + UTZFMT_PARSE_OPTION_NONE, "GMT-22:22", 5, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"-3333", 0, "en_US", UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL, + UTZFMT_PARSE_OPTION_NONE, "GMT-03:33", 4, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"XXX+01:30YYY", 3, "en_US", UTZFMT_STYLE_LOCALIZED_GMT, + UTZFMT_PARSE_OPTION_NONE, "GMT+01:30", 9, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"GMT0", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "Etc/GMT", 3, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"ESTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"EDTx", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "America/New_York", 3, UTZFMT_TIME_TYPE_DAYLIGHT}, + + {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_NONE, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"EST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_ALL_STYLES, "America/New_York", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"EST", 0, "en_CA", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "America/Toronto", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"CST", 0, "en_US", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, "America/Chicago", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"CST", 0, "en_GB", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_NONE, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"CST", 0, "en_GB", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "America/Chicago", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"--CST--", 2, "en_GB", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "America/Chicago", 5, UTZFMT_TIME_TYPE_STANDARD}, + + {"CST", 0, "zh_CN", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "Asia/Shanghai", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"AEST", 0, "en_AU", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "Australia/Sydney", 4, UTZFMT_TIME_TYPE_STANDARD}, + + {"AST", 0, "ar_SA", UTZFMT_STYLE_SPECIFIC_SHORT, + UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "Asia/Riyadh", 3, UTZFMT_TIME_TYPE_STANDARD}, + + {"AQTST", 0, "en", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_NONE, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"AQTST", 0, "en", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_ALL_STYLES, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN}, + + {"AQTST", 0, "en", UTZFMT_STYLE_SPECIFIC_LONG, + UTZFMT_PARSE_OPTION_ALL_STYLES | UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS, "Asia/Aqtobe", 5, UTZFMT_TIME_TYPE_DAYLIGHT}, + + {NULL, 0, NULL, UTZFMT_STYLE_GENERIC_LOCATION, + UTZFMT_PARSE_OPTION_NONE, NULL, 0, UTZFMT_TIME_TYPE_UNKNOWN} }; for (int32_t i = 0; DATA[i].text; i++) { @@ -766,8 +864,7 @@ TimeZoneFormatTest::TestParse(void) { } UTimeZoneFormatTimeType ttype = UTZFMT_TIME_TYPE_UNKNOWN; ParsePosition pos(DATA[i].inPos); - int32_t parseOptions = DATA[i].parseAll ? UTZFMT_PARSE_OPTION_ALL_STYLES : UTZFMT_PARSE_OPTION_NONE; - TimeZone* tz = tzfmt->parse(DATA[i].style, DATA[i].text, pos, parseOptions, &ttype); + TimeZone* tz = tzfmt->parse(DATA[i].style, DATA[i].text, pos, DATA[i].parseOptions, &ttype); UnicodeString errMsg; if (tz) { @@ -783,7 +880,7 @@ TimeZoneFormatTest::TestParse(void) { delete tz; } else { if (DATA[i].expected) { - errln((UnicodeString)"Fail: Parse failure - expected: " + DATA[i].expected); + errMsg = (UnicodeString)"Parse failure - expected: " + DATA[i].expected; } } if (errMsg.length() > 0) { @@ -892,14 +989,14 @@ TimeZoneFormatTest::TestISOFormat(void) { // Formatting UErrorCode status = U_ZERO_ERROR; - LocalPointer sdf(new SimpleDateFormat(status)); + LocalPointer sdf(new SimpleDateFormat(status), status); if (U_FAILURE(status)) { dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status)); return; } UDate d = Calendar::getNow(); - for (uint32_t i = 0; i < sizeof(OFFSET)/sizeof(OFFSET[0]); i++) { + for (uint32_t i = 0; i < UPRV_LENGTHOF(OFFSET); i++) { SimpleTimeZone* tz = new SimpleTimeZone(OFFSET[i], UnicodeString("Zone Offset:") + OFFSET[i] + "ms"); sdf->adoptTimeZone(tz); for (int32_t j = 0; PATTERN[j] != 0; j++) { @@ -1066,4 +1163,181 @@ TimeZoneFormatTest::TestFormat(void) { } } +void +TimeZoneFormatTest::TestFormatTZDBNames(void) { + UDate dateJan = 1358208000000.0; // 2013-01-15T00:00:00Z + UDate dateJul = 1373846400000.0; // 2013-07-15T00:00:00Z + + const FormatTestData DATA[] = { + { + "en", + "America/Chicago", + dateJan, + UTZFMT_STYLE_SPECIFIC_SHORT, + "CST", + UTZFMT_TIME_TYPE_STANDARD + }, + { + "en", + "Asia/Shanghai", + dateJan, + UTZFMT_STYLE_SPECIFIC_SHORT, + "CST", + UTZFMT_TIME_TYPE_STANDARD + }, + { + "zh_Hans", + "Asia/Shanghai", + dateJan, + UTZFMT_STYLE_SPECIFIC_SHORT, + "CST", + UTZFMT_TIME_TYPE_STANDARD + }, + { + "en", + "America/Los_Angeles", + dateJul, + UTZFMT_STYLE_SPECIFIC_LONG, + "GMT-07:00", // No long display names + UTZFMT_TIME_TYPE_DAYLIGHT + }, + { + "ja", + "America/Los_Angeles", + dateJul, + UTZFMT_STYLE_SPECIFIC_SHORT, + "PDT", + UTZFMT_TIME_TYPE_DAYLIGHT + }, + { + "en", + "Australia/Sydney", + dateJan, + UTZFMT_STYLE_SPECIFIC_SHORT, + "AEDT", + UTZFMT_TIME_TYPE_DAYLIGHT + }, + { + "en", + "Australia/Sydney", + dateJul, + UTZFMT_STYLE_SPECIFIC_SHORT, + "AEST", + UTZFMT_TIME_TYPE_STANDARD + }, + + {0, 0, 0.0, UTZFMT_STYLE_GENERIC_LOCATION, 0, UTZFMT_TIME_TYPE_UNKNOWN} + }; + + for (int32_t i = 0; DATA[i].locale; i++) { + UErrorCode status = U_ZERO_ERROR; + Locale loc(DATA[i].locale); + LocalPointer tzfmt(TimeZoneFormat::createInstance(loc, status)); + if (U_FAILURE(status)) { + dataerrln("Fail TimeZoneFormat::createInstance: %s", u_errorName(status)); + continue; + } + TimeZoneNames *tzdbNames = TimeZoneNames::createTZDBInstance(loc, status); + if (U_FAILURE(status)) { + dataerrln("Fail TimeZoneNames::createTZDBInstance: %s", u_errorName(status)); + continue; + } + tzfmt->adoptTimeZoneNames(tzdbNames); + + LocalPointer tz(TimeZone::createTimeZone(DATA[i].tzid)); + UnicodeString out; + UTimeZoneFormatTimeType timeType; + + tzfmt->format(DATA[i].style, *(tz.getAlias()), DATA[i].date, out, &timeType); + UnicodeString expected(DATA[i].expected, -1, US_INV); + expected = expected.unescape(); + + assertEquals(UnicodeString("Format result for ") + DATA[i].tzid + " (Test Case " + i + ")", expected, out); + if (DATA[i].timeType != timeType) { + dataerrln(UnicodeString("Formatted time zone type (Test Case ") + i + "), returned=" + + timeType + ", expected=" + DATA[i].timeType); + } + } +} + +void +TimeZoneFormatTest::TestFormatCustomZone(void) { + struct { + const char* id; + int32_t offset; + const char* expected; + } TESTDATA[] = { + { "abc", 3600000, "GMT+01:00" }, // unknown ID + { "$abc", -3600000, "GMT-01:00" }, // unknown, with ASCII variant char '$' + { "\\u00c1\\u00df\\u00c7", 5400000, "GMT+01:30"}, // unknown, with non-ASCII chars + { 0, 0, 0 } + }; + + UDate now = Calendar::getNow(); + + for (int32_t i = 0; ; i++) { + const char *id = TESTDATA[i].id; + if (id == 0) { + break; + } + UnicodeString tzid = UnicodeString(id, -1, US_INV).unescape(); + SimpleTimeZone tz(TESTDATA[i].offset, tzid); + + UErrorCode status = U_ZERO_ERROR; + LocalPointer tzfmt(TimeZoneFormat::createInstance(Locale("en"), status)); + if (tzfmt.isNull()) { + dataerrln("FAIL: TimeZoneFormat::createInstance failed for en"); + return; + } + UnicodeString tzstr; + UnicodeString expected = UnicodeString(TESTDATA[i].expected, -1, US_INV).unescape(); + + tzfmt->format(UTZFMT_STYLE_SPECIFIC_LONG, tz, now, tzstr, NULL); + assertEquals(UnicodeString("Format result for ") + tzid, expected, tzstr); + } +} + +void +TimeZoneFormatTest::TestFormatTZDBNamesAllZoneCoverage(void) { + UErrorCode status = U_ZERO_ERROR; + LocalPointer tzids(TimeZone::createEnumeration()); + if (tzids.getAlias() == nullptr) { + dataerrln("%s %d tzids is null", __FILE__, __LINE__); + return; + } + const UnicodeString *tzid; + LocalPointer tzdbNames(TimeZoneNames::createTZDBInstance(Locale("en"), status)); + UDate now = Calendar::getNow(); + UnicodeString mzId; + UnicodeString name; + while ((tzid = tzids->snext(status))) { + logln("Zone: " + *tzid); + LocalPointer tz(TimeZone::createTimeZone(*tzid)); + tzdbNames->getMetaZoneID(*tzid, now, mzId); + if (mzId.isBogus()) { + logln((UnicodeString)"Meta zone: "); + } else { + logln((UnicodeString)"Meta zone: " + mzId); + } + + // mzID could be bogus here + tzdbNames->getMetaZoneDisplayName(mzId, UTZNM_SHORT_STANDARD, name); + // name could be bogus here + if (name.isBogus()) { + logln((UnicodeString)"Meta zone short standard name: "); + } + else { + logln((UnicodeString)"Meta zone short standard name: " + name); + } + + tzdbNames->getMetaZoneDisplayName(mzId, UTZNM_SHORT_DAYLIGHT, name); + // name could be bogus here + if (name.isBogus()) { + logln((UnicodeString)"Meta zone short daylight name: "); + } + else { + logln((UnicodeString)"Meta zone short daylight name: " + name); + } + } +} #endif /* #if !UCONFIG_NO_FORMATTING */