Home | History | Annotate | Download | only in intltest
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 2007-2011, International Business Machines Corporation and    *
      4 * others. All Rights Reserved.                                                *
      5 *******************************************************************************
      6 */
      7 #include "unicode/utypes.h"
      8 
      9 #if !UCONFIG_NO_FORMATTING
     10 
     11 #include "tzfmttst.h"
     12 
     13 #include "simplethread.h"
     14 #include "unicode/timezone.h"
     15 #include "unicode/simpletz.h"
     16 #include "unicode/calendar.h"
     17 #include "unicode/strenum.h"
     18 #include "unicode/smpdtfmt.h"
     19 #include "unicode/uchar.h"
     20 #include "unicode/basictz.h"
     21 #include "cstring.h"
     22 
     23 static const char* PATTERNS[] = {"z", "zzzz", "Z", "ZZZZ", "v", "vvvv", "V", "VVVV"};
     24 static const int NUM_PATTERNS = sizeof(PATTERNS)/sizeof(const char*);
     25 
     26 void
     27 TimeZoneFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
     28 {
     29     if (exec) {
     30         logln("TestSuite TimeZoneFormatTest");
     31     }
     32     switch (index) {
     33         TESTCASE(0, TestTimeZoneRoundTrip);
     34         TESTCASE(1, TestTimeRoundTrip);
     35         default: name = ""; break;
     36     }
     37 }
     38 
     39 void
     40 TimeZoneFormatTest::TestTimeZoneRoundTrip(void) {
     41     UErrorCode status = U_ZERO_ERROR;
     42 
     43     SimpleTimeZone unknownZone(-31415, (UnicodeString)"Etc/Unknown");
     44     int32_t badDstOffset = -1234;
     45     int32_t badZoneOffset = -2345;
     46 
     47     int32_t testDateData[][3] = {
     48         {2007, 1, 15},
     49         {2007, 6, 15},
     50         {1990, 1, 15},
     51         {1990, 6, 15},
     52         {1960, 1, 15},
     53         {1960, 6, 15},
     54     };
     55 
     56     Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString)"UTC"), status);
     57     if (U_FAILURE(status)) {
     58         dataerrln("Calendar::createInstance failed: %s", u_errorName(status));
     59         return;
     60     }
     61 
     62     // Set up rule equivalency test range
     63     UDate low, high;
     64     cal->set(1900, UCAL_JANUARY, 1);
     65     low = cal->getTime(status);
     66     cal->set(2040, UCAL_JANUARY, 1);
     67     high = cal->getTime(status);
     68     if (U_FAILURE(status)) {
     69         errln("getTime failed");
     70         return;
     71     }
     72 
     73     // Set up test dates
     74     UDate DATES[(sizeof(testDateData)/sizeof(int32_t))/3];
     75     const int32_t nDates = (sizeof(testDateData)/sizeof(int32_t))/3;
     76     cal->clear();
     77     for (int32_t i = 0; i < nDates; i++) {
     78         cal->set(testDateData[i][0], testDateData[i][1], testDateData[i][2]);
     79         DATES[i] = cal->getTime(status);
     80         if (U_FAILURE(status)) {
     81             errln("getTime failed");
     82             return;
     83         }
     84     }
     85 
     86     // Set up test locales
     87     const Locale testLocales[] = {
     88         Locale("en"),
     89         Locale("en_CA"),
     90         Locale("fr"),
     91         Locale("zh_Hant")
     92     };
     93 
     94     const Locale *LOCALES;
     95     int32_t nLocales;
     96 
     97     if (quick) {
     98         LOCALES = testLocales;
     99         nLocales = sizeof(testLocales)/sizeof(Locale);
    100     } else {
    101         LOCALES = Locale::getAvailableLocales(nLocales);
    102     }
    103 
    104     StringEnumeration *tzids = TimeZone::createEnumeration();
    105     int32_t inRaw, inDst;
    106     int32_t outRaw, outDst;
    107 
    108     // Run the roundtrip test
    109     for (int32_t locidx = 0; locidx < nLocales; locidx++) {
    110         UnicodeString localGMTString;
    111         SimpleDateFormat gmtFmt(UnicodeString("ZZZZ"), LOCALES[locidx], status);
    112         gmtFmt.setTimeZone(*TimeZone::getGMT());
    113         gmtFmt.format(0.0, localGMTString);
    114 
    115         for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
    116 
    117             SimpleDateFormat *sdf = new SimpleDateFormat((UnicodeString)PATTERNS[patidx], LOCALES[locidx], status);
    118             if (U_FAILURE(status)) {
    119                 errcheckln(status, (UnicodeString)"new SimpleDateFormat failed for pattern " +
    120                     PATTERNS[patidx] + " for locale " + LOCALES[locidx].getName() + " - " + u_errorName(status));
    121                 status = U_ZERO_ERROR;
    122                 continue;
    123             }
    124 
    125             tzids->reset(status);
    126             const UnicodeString *tzid;
    127             while ((tzid = tzids->snext(status))) {
    128                 TimeZone *tz = TimeZone::createTimeZone(*tzid);
    129 
    130                 for (int32_t datidx = 0; datidx < nDates; datidx++) {
    131                     UnicodeString tzstr;
    132                     FieldPosition fpos(0);
    133                     // Format
    134                     sdf->setTimeZone(*tz);
    135                     sdf->format(DATES[datidx], tzstr, fpos);
    136 
    137                     // Before parse, set unknown zone to SimpleDateFormat instance
    138                     // just for making sure that it does not depends on the time zone
    139                     // originally set.
    140                     sdf->setTimeZone(unknownZone);
    141 
    142                     // Parse
    143                     ParsePosition pos(0);
    144                     Calendar *outcal = Calendar::createInstance(unknownZone, status);
    145                     if (U_FAILURE(status)) {
    146                         errln("Failed to create an instance of calendar for receiving parse result.");
    147                         status = U_ZERO_ERROR;
    148                         continue;
    149                     }
    150                     outcal->set(UCAL_DST_OFFSET, badDstOffset);
    151                     outcal->set(UCAL_ZONE_OFFSET, badZoneOffset);
    152 
    153                     sdf->parse(tzstr, *outcal, pos);
    154 
    155                     // Check the result
    156                     const TimeZone &outtz = outcal->getTimeZone();
    157                     UnicodeString outtzid;
    158                     outtz.getID(outtzid);
    159 
    160                     tz->getOffset(DATES[datidx], false, inRaw, inDst, status);
    161                     if (U_FAILURE(status)) {
    162                         errln((UnicodeString)"Failed to get offsets from time zone" + *tzid);
    163                         status = U_ZERO_ERROR;
    164                     }
    165                     outtz.getOffset(DATES[datidx], false, outRaw, outDst, status);
    166                     if (U_FAILURE(status)) {
    167                         errln((UnicodeString)"Failed to get offsets from time zone" + outtzid);
    168                         status = U_ZERO_ERROR;
    169                     }
    170 
    171                     if (uprv_strcmp(PATTERNS[patidx], "VVVV") == 0) {
    172                         // Location: time zone rule must be preserved except
    173                         // zones not actually associated with a specific location.
    174                         // Time zones in this category do not have "/" in its ID.
    175                         UnicodeString canonical;
    176                         TimeZone::getCanonicalID(*tzid, canonical, status);
    177                         if (U_FAILURE(status)) {
    178                             // Uknown ID - we should not get here
    179                             errln((UnicodeString)"Unknown ID " + *tzid);
    180                             status = U_ZERO_ERROR;
    181                         } else if (outtzid != canonical) {
    182                             // Canonical ID did not match - check the rules
    183                             if (!((BasicTimeZone*)&outtz)->hasEquivalentTransitions((BasicTimeZone&)*tz, low, high, TRUE, status)) {
    184                                 if (canonical.indexOf((UChar)0x27 /*'/'*/) == -1) {
    185                                     // Exceptional cases, such as CET, EET, MET and WET
    186                                     logln("Canonical round trip failed (as expected); tz=" + *tzid
    187                                             + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    188                                             + ", time=" + DATES[datidx] + ", str=" + tzstr
    189                                             + ", outtz=" + outtzid);
    190                                 } else {
    191                                     errln("Canonical round trip failed; tz=" + *tzid
    192                                         + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    193                                         + ", time=" + DATES[datidx] + ", str=" + tzstr
    194                                         + ", outtz=" + outtzid);
    195                                 }
    196                                 if (U_FAILURE(status)) {
    197                                     errln("hasEquivalentTransitions failed");
    198                                     status = U_ZERO_ERROR;
    199                                 }
    200                             }
    201                         }
    202 
    203                     } else {
    204                         // Check if localized GMT format or RFC format is used.
    205                         int32_t numDigits = 0;
    206                         for (int n = 0; n < tzstr.length(); n++) {
    207                             if (u_isdigit(tzstr.charAt(n))) {
    208                                 numDigits++;
    209                             }
    210                         }
    211                         if (tzstr == localGMTString || numDigits >= 3) {
    212                             // Localized GMT or RFC: total offset (raw + dst) must be preserved.
    213                             int32_t inOffset = inRaw + inDst;
    214                             int32_t outOffset = outRaw + outDst;
    215                             if (inOffset != outOffset) {
    216                                 errln((UnicodeString)"Offset round trip failed; tz=" + *tzid
    217                                     + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    218                                     + ", time=" + DATES[datidx] + ", str=" + tzstr
    219                                     + ", inOffset=" + inOffset + ", outOffset=" + outOffset);
    220                             }
    221                         } else {
    222                             // Specific or generic: raw offset must be preserved.
    223                             if (inRaw != outRaw) {
    224                                 errln((UnicodeString)"Raw offset round trip failed; tz=" + *tzid
    225                                     + ", locale=" + LOCALES[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    226                                     + ", time=" + DATES[datidx] + ", str=" + tzstr
    227                                     + ", inRawOffset=" + inRaw + ", outRawOffset=" + outRaw);
    228                             }
    229                         }
    230                     }
    231                     delete outcal;
    232                 }
    233                 delete tz;
    234             }
    235             delete sdf;
    236         }
    237     }
    238     delete cal;
    239     delete tzids;
    240 }
    241 
    242 struct LocaleData {
    243     int32_t index;
    244     int32_t testCounts;
    245     UDate *times;
    246     const Locale* locales; // Static
    247     int32_t nLocales; // Static
    248     UBool quick; // Static
    249     UDate START_TIME; // Static
    250     UDate END_TIME; // Static
    251     int32_t numDone;
    252 };
    253 
    254 class TestTimeRoundTripThread: public SimpleThread {
    255 public:
    256     TestTimeRoundTripThread(IntlTest& tlog, LocaleData &ld, int32_t i)
    257         : log(tlog), data(ld), index(i) {}
    258     virtual void run() {
    259         UErrorCode status = U_ZERO_ERROR;
    260         UBool REALLY_VERBOSE = FALSE;
    261 
    262         // Whether each pattern is ambiguous at DST->STD local time overlap
    263         UBool AMBIGUOUS_DST_DECESSION[] = { FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE };
    264         // Whether each pattern is ambiguous at STD->STD/DST->DST local time overlap
    265         UBool AMBIGUOUS_NEGATIVE_SHIFT[] = { TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE };
    266 
    267         // Workaround for #6338
    268         //UnicodeString BASEPATTERN("yyyy-MM-dd'T'HH:mm:ss.SSS");
    269         UnicodeString BASEPATTERN("yyyy.MM.dd HH:mm:ss.SSS");
    270 
    271         // timer for performance analysis
    272         UDate timer;
    273         UDate testTimes[4];
    274         UBool expectedRoundTrip[4];
    275         int32_t testLen = 0;
    276 
    277         StringEnumeration *tzids = TimeZone::createTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, status);
    278         if (U_FAILURE(status)) {
    279             if (status == U_MISSING_RESOURCE_ERROR) {
    280                 /* This error is generally caused by data not being present. However, an infinite loop will occur
    281                  * because the thread thinks that the test data is never done so we should treat the data as done.
    282                  */
    283                 log.dataerrln("TimeZone::createTimeZoneIDEnumeration failed - %s", u_errorName(status));
    284                 data.numDone = data.nLocales;
    285             } else {
    286                 log.errln("TimeZone::createTimeZoneIDEnumeration failed: %s", u_errorName(status));
    287             }
    288             return;
    289         }
    290 
    291         int32_t locidx = -1;
    292         UDate times[NUM_PATTERNS];
    293         for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    294             times[i] = 0;
    295         }
    296 
    297         int32_t testCounts = 0;
    298 
    299         while (true) {
    300             umtx_lock(NULL); // Lock to increment the index
    301             for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    302                 data.times[i] += times[i];
    303                 data.testCounts += testCounts;
    304             }
    305             if (data.index < data.nLocales) {
    306                 locidx = data.index;
    307                 data.index++;
    308             } else {
    309                 locidx = -1;
    310             }
    311             umtx_unlock(NULL); // Unlock for other threads to use
    312 
    313             if (locidx == -1) {
    314                 log.logln((UnicodeString) "Thread " + index + " is done.");
    315                 break;
    316             }
    317 
    318             log.logln((UnicodeString) "\nThread " + index + ": Locale: " + UnicodeString(data.locales[locidx].getName()));
    319 
    320             for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
    321                 log.logln((UnicodeString) "    Pattern: " + PATTERNS[patidx]);
    322                 times[patidx] = 0;
    323 
    324                 UnicodeString pattern(BASEPATTERN);
    325                 pattern.append(" ").append(PATTERNS[patidx]);
    326 
    327                 SimpleDateFormat *sdf = new SimpleDateFormat(pattern, data.locales[locidx], status);
    328                 if (U_FAILURE(status)) {
    329                     log.errcheckln(status, (UnicodeString) "new SimpleDateFormat failed for pattern " +
    330                         pattern + " for locale " + data.locales[locidx].getName() + " - " + u_errorName(status));
    331                     status = U_ZERO_ERROR;
    332                     continue;
    333                 }
    334 
    335                 tzids->reset(status);
    336                 const UnicodeString *tzid;
    337 
    338                 timer = Calendar::getNow();
    339 
    340                 while ((tzid = tzids->snext(status))) {
    341                     BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid);
    342                     sdf->setTimeZone(*tz);
    343 
    344                     UDate t = data.START_TIME;
    345                     TimeZoneTransition tzt;
    346                     UBool tztAvail = FALSE;
    347                     UBool middle = TRUE;
    348 
    349                     while (t < data.END_TIME) {
    350                         if (!tztAvail) {
    351                             testTimes[0] = t;
    352                             expectedRoundTrip[0] = TRUE;
    353                             testLen = 1;
    354                         } else {
    355                             int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings();
    356                             int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings();
    357                             int32_t delta = toOffset - fromOffset;
    358                             if (delta < 0) {
    359                                 UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0;
    360                                 testTimes[0] = t + delta - 1;
    361                                 expectedRoundTrip[0] = TRUE;
    362                                 testTimes[1] = t + delta;
    363                                 expectedRoundTrip[1] = isDstDecession ? !AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
    364                                 testTimes[2] = t - 1;
    365                                 expectedRoundTrip[2] = isDstDecession ? !AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
    366                                 testTimes[3] = t;
    367                                 expectedRoundTrip[3] = TRUE;
    368                                 testLen = 4;
    369                             } else {
    370                                 testTimes[0] = t - 1;
    371                                 expectedRoundTrip[0] = TRUE;
    372                                 testTimes[1] = t;
    373                                 expectedRoundTrip[1] = TRUE;
    374                                 testLen = 2;
    375                             }
    376                         }
    377                         for (int32_t testidx = 0; testidx < testLen; testidx++) {
    378                             if (data.quick) {
    379                                 // reduce regular test time
    380                                 if (!expectedRoundTrip[testidx]) {
    381                                     continue;
    382                                 }
    383                             }
    384 
    385                             testCounts++;
    386 
    387                             UnicodeString text;
    388                             FieldPosition fpos(0);
    389                             sdf->format(testTimes[testidx], text, fpos);
    390 
    391                             UDate parsedDate = sdf->parse(text, status);
    392                             if (U_FAILURE(status)) {
    393                                 log.errln((UnicodeString) "Parse failure for text=" + text + ", tzid=" + *tzid + ", locale=" + data.locales[locidx].getName()
    394                                         + ", pattern=" + PATTERNS[patidx] + ", time=" + testTimes[testidx]);
    395                                 status = U_ZERO_ERROR;
    396                                 continue;
    397                             }
    398                             if (parsedDate != testTimes[testidx]) {
    399                                 UnicodeString msg = (UnicodeString) "Time round trip failed for " + "tzid=" + *tzid + ", locale=" + data.locales[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    400                                         + ", text=" + text + ", time=" + testTimes[testidx] + ", restime=" + parsedDate + ", diff=" + (parsedDate - testTimes[testidx]);
    401                                 if (expectedRoundTrip[testidx]) {
    402                                     log.errln((UnicodeString) "FAIL: " + msg);
    403                                 } else if (REALLY_VERBOSE) {
    404                                     log.logln(msg);
    405                                 }
    406                             }
    407                         }
    408                         tztAvail = tz->getNextTransition(t, FALSE, tzt);
    409                         if (!tztAvail) {
    410                             break;
    411                         }
    412                         if (middle) {
    413                             // Test the date in the middle of two transitions.
    414                             t += (int64_t) ((tzt.getTime() - t) / 2);
    415                             middle = FALSE;
    416                             tztAvail = FALSE;
    417                         } else {
    418                             t = tzt.getTime();
    419                         }
    420                     }
    421                     delete tz;
    422                 }
    423                 times[patidx] += (Calendar::getNow() - timer);
    424                 delete sdf;
    425             }
    426             umtx_lock(NULL);
    427             data.numDone++;
    428             umtx_unlock(NULL);
    429         }
    430         delete tzids;
    431     }
    432 private:
    433     IntlTest& log;
    434     LocaleData& data;
    435     int32_t index;
    436 };
    437 
    438 void
    439 TimeZoneFormatTest::TestTimeRoundTrip(void) {
    440     int32_t nThreads = threadCount;
    441     const Locale *LOCALES;
    442     int32_t nLocales;
    443     int32_t testCounts = 0;
    444 
    445     UErrorCode status = U_ZERO_ERROR;
    446     Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString) "UTC"), status);
    447     if (U_FAILURE(status)) {
    448         dataerrln("Calendar::createInstance failed: %s", u_errorName(status));
    449         return;
    450     }
    451 
    452     const char* testAllProp = getProperty("TimeZoneRoundTripAll");
    453     UBool bTestAll = (testAllProp && uprv_strcmp(testAllProp, "true") == 0);
    454 
    455     UDate START_TIME, END_TIME;
    456     if (bTestAll || !quick) {
    457         cal->set(1900, UCAL_JANUARY, 1);
    458     } else {
    459         cal->set(1990, UCAL_JANUARY, 1);
    460     }
    461     START_TIME = cal->getTime(status);
    462 
    463     cal->set(2015, UCAL_JANUARY, 1);
    464     END_TIME = cal->getTime(status);
    465 
    466     if (U_FAILURE(status)) {
    467         errln("getTime failed");
    468         return;
    469     }
    470 
    471     UDate times[NUM_PATTERNS];
    472     for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    473         times[i] = 0;
    474     }
    475 
    476     // Set up test locales
    477     const Locale locales1[] = {Locale("en")};
    478     const Locale locales2[] = {
    479         Locale("ar_EG"), Locale("bg_BG"), Locale("ca_ES"), Locale("da_DK"), Locale("de"),
    480         Locale("de_DE"), Locale("el_GR"), Locale("en"), Locale("en_AU"), Locale("en_CA"),
    481         Locale("en_US"), Locale("es"), Locale("es_ES"), Locale("es_MX"), Locale("fi_FI"),
    482         Locale("fr"), Locale("fr_CA"), Locale("fr_FR"), Locale("he_IL"), Locale("hu_HU"),
    483         Locale("it"), Locale("it_IT"), Locale("ja"), Locale("ja_JP"), Locale("ko"),
    484         Locale("ko_KR"), Locale("nb_NO"), Locale("nl_NL"), Locale("nn_NO"), Locale("pl_PL"),
    485         Locale("pt"), Locale("pt_BR"), Locale("pt_PT"), Locale("ru_RU"), Locale("sv_SE"),
    486         Locale("th_TH"), Locale("tr_TR"), Locale("zh"), Locale("zh_Hans"), Locale("zh_Hans_CN"),
    487         Locale("zh_Hant"), Locale("zh_Hant_TW")
    488     };
    489 
    490     if (bTestAll) {
    491         LOCALES = Locale::getAvailableLocales(nLocales);
    492     } else if (quick) {
    493         LOCALES = locales1;
    494         nLocales = sizeof(locales1)/sizeof(Locale);
    495     } else {
    496         LOCALES = locales2;
    497         nLocales = sizeof(locales2)/sizeof(Locale);
    498     }
    499 
    500     LocaleData data;
    501     data.index = 0;
    502     data.testCounts = testCounts;
    503     data.times = times;
    504     data.locales = LOCALES;
    505     data.nLocales = nLocales;
    506     data.quick = quick;
    507     data.START_TIME = START_TIME;
    508     data.END_TIME = END_TIME;
    509     data.numDone = 0;
    510 
    511 #if (ICU_USE_THREADS==0)
    512     TestTimeRoundTripThread fakeThread(*this, data, 0);
    513     fakeThread.run();
    514 #else
    515     TestTimeRoundTripThread **threads = new TestTimeRoundTripThread*[threadCount];
    516     int32_t i;
    517     for (i = 0; i < nThreads; i++) {
    518         threads[i] = new TestTimeRoundTripThread(*this, data, i);
    519         if (threads[i]->start() != 0) {
    520             errln("Error starting thread %d", i);
    521         }
    522     }
    523 
    524     UBool done = false;
    525     while (true) {
    526         umtx_lock(NULL);
    527         if (data.numDone == nLocales) {
    528             done = true;
    529         }
    530         umtx_unlock(NULL);
    531         if (done)
    532             break;
    533         SimpleThread::sleep(1000);
    534     }
    535 
    536     for (i = 0; i < nThreads; i++) {
    537         delete threads[i];
    538     }
    539     delete [] threads;
    540 
    541 #endif
    542     UDate total = 0;
    543     logln("### Elapsed time by patterns ###");
    544     for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    545         logln(UnicodeString("") + data.times[i] + "ms (" + PATTERNS[i] + ")");
    546         total += data.times[i];
    547     }
    548     logln((UnicodeString) "Total: " + total + "ms");
    549     logln((UnicodeString) "Iteration: " + data.testCounts);
    550 
    551     delete cal;
    552 }
    553 
    554 #endif /* #if !UCONFIG_NO_FORMATTING */
    555