]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/tzfmttst.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / intltest / tzfmttst.cpp
index 0c14eec57a7db57d5920163dac43669ec0d567a9..40362aacd0a7d1a42fe4f532ac759421be5295fa 100644 (file)
@@ -21,6 +21,7 @@
 #include "unicode/basictz.h"
 #include "unicode/tzfmt.h"
 #include "unicode/localpointer.h"
+#include "unicode/utf16.h"
 
 #include "cstring.h"
 #include "cstr.h"
@@ -83,7 +84,9 @@ TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name
         TESTCASE(4, TestFormat);
         TESTCASE(5, TestFormatTZDBNames);
         TESTCASE(6, TestFormatCustomZone);
-        default: name = ""; break;
+        TESTCASE(7, TestFormatTZDBNamesAllZoneCoverage);
+        TESTCASE(8, TestAdoptDefaultThreadSafe);
+    default: name = ""; break;
     }
 }
 
@@ -139,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;
@@ -168,7 +173,6 @@ TimeZoneFormatTest::TestTimeZoneRoundTrip(void) {
         gmtFmt.format(0.0, localGMTString);
 
         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 " +
@@ -299,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);
                         }
@@ -403,7 +410,7 @@ struct LocaleData {
         for (int i=0; i<UPRV_LENGTHOF(times); i++) {
             times[i] = 0;
         }
-    };
+    }
 
     void resetTestIteration() {
         localeIndex = -1;
@@ -478,7 +485,7 @@ 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) {
@@ -595,16 +602,11 @@ void TimeZoneFormatTest::RunTimeRoundTripTests(int32_t threadNumber) {
                 }
             }
 
-            if (*tzid == "Pacific/Apia" && uprv_strcmp(PATTERNS[patidx], "vvvv") == 0
+            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;
             }
-            // skip some zone/format combinations until we have a metazone for them and/or
-            // better fallback zone formatting...
-            if ((*tzid == "America/Punta_Arenas" || *tzid == "Antarctica/Palmer") &&
-                    uprv_strcmp(PATTERNS[patidx], "zzzz") == 0) {
-                continue;
-            }
 
             BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid);
             sdf->setTimeZone(*tz);
@@ -710,6 +712,44 @@ void TimeZoneFormatTest::RunTimeRoundTripTests(int32_t threadNumber) {
     delete tzids;
 }
 
+void
+TimeZoneFormatTest::TestAdoptDefaultThreadSafe(void) {
+    ThreadPool<TimeZoneFormatTest> 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<icu::StringEnumeration> 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<icu::TimeZone> 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;
@@ -1257,5 +1297,47 @@ TimeZoneFormatTest::TestFormatCustomZone(void) {
     }
 }
 
+void
+TimeZoneFormatTest::TestFormatTZDBNamesAllZoneCoverage(void) {
+    UErrorCode status = U_ZERO_ERROR;
+    LocalPointer<StringEnumeration> tzids(TimeZone::createEnumeration());
+    if (tzids.getAlias() == nullptr) {
+        dataerrln("%s %d tzids is null", __FILE__, __LINE__);
+        return;
+    }
+    const UnicodeString *tzid;
+    LocalPointer<TimeZoneNames> tzdbNames(TimeZoneNames::createTZDBInstance(Locale("en"), status));
+    UDate now = Calendar::getNow();
+    UnicodeString mzId;
+    UnicodeString name;
+    while ((tzid = tzids->snext(status))) {
+        logln("Zone: " + *tzid);
+        LocalPointer<TimeZone> tz(TimeZone::createTimeZone(*tzid));
+        tzdbNames->getMetaZoneID(*tzid, now, mzId);
+        if (mzId.isBogus()) {
+            logln((UnicodeString)"Meta zone: <not available>");
+        } 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: <not available>");
+        }
+        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: <not available>");
+        }
+        else {
+            logln((UnicodeString)"Meta zone short daylight name: " + name);
+        }
+    }
+}
 #endif /* #if !UCONFIG_NO_FORMATTING */