Home | History | Annotate | Download | only in intltest
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 2007-2010, 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     if (U_FAILURE(status)) {
    106         errln("tzids->count failed");
    107         return;
    108     }
    109 
    110     int32_t inRaw, inDst;
    111     int32_t outRaw, outDst;
    112 
    113     // Run the roundtrip test
    114     for (int32_t locidx = 0; locidx < nLocales; locidx++) {
    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 (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::createEnumeration();
    278         if (U_FAILURE(status)) {
    279             log.errln("tzids->count failed");
    280             return;
    281         }
    282 
    283         int32_t locidx = -1;
    284         UDate times[NUM_PATTERNS];
    285         for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    286             times[i] = 0;
    287         }
    288 
    289         int32_t testCounts = 0;
    290         UBool done = false;
    291 
    292         while (true) {
    293             umtx_lock(NULL); // Lock to increment the index
    294             for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    295                 data.times[i] += times[i];
    296                 data.testCounts += testCounts;
    297             }
    298             if (data.index < data.nLocales) {
    299                 locidx = data.index;
    300                 data.index++;
    301             } else {
    302                 locidx = -1;
    303             }
    304             umtx_unlock(NULL); // Unlock for other threads to use
    305 
    306             if (locidx == -1) {
    307                 log.logln((UnicodeString) "Thread " + index + " is done.");
    308                 break;
    309             }
    310 
    311             log.logln((UnicodeString) "\nThread " + index + ": Locale: " + UnicodeString(data.locales[locidx].getName()));
    312 
    313             for (int32_t patidx = 0; patidx < NUM_PATTERNS; patidx++) {
    314                 log.logln((UnicodeString) "    Pattern: " + PATTERNS[patidx]);
    315                 times[patidx] = 0;
    316 
    317                 UnicodeString pattern(BASEPATTERN);
    318                 pattern.append(" ").append(PATTERNS[patidx]);
    319 
    320                 SimpleDateFormat *sdf = new SimpleDateFormat(pattern, data.locales[locidx], status);
    321                 if (U_FAILURE(status)) {
    322                     log.errcheckln(status, (UnicodeString) "new SimpleDateFormat failed for pattern " +
    323                         pattern + " for locale " + data.locales[locidx].getName() + " - " + u_errorName(status));
    324                     status = U_ZERO_ERROR;
    325                     continue;
    326                 }
    327 
    328                 tzids->reset(status);
    329                 const UnicodeString *tzid;
    330 
    331                 timer = Calendar::getNow();
    332 
    333                 while ((tzid = tzids->snext(status))) {
    334                     UnicodeString canonical;
    335                     TimeZone::getCanonicalID(*tzid, canonical, status);
    336                     if (U_FAILURE(status)) {
    337                         // Unknown ID - we should not get here
    338                         status = U_ZERO_ERROR;
    339                         continue;
    340                     }
    341                     if (*tzid != canonical) {
    342                         // Skip aliases
    343                         continue;
    344                     }
    345                     BasicTimeZone *tz = (BasicTimeZone*) TimeZone::createTimeZone(*tzid);
    346                     sdf->setTimeZone(*tz);
    347 
    348                     UDate t = data.START_TIME;
    349                     TimeZoneTransition tzt;
    350                     UBool tztAvail = FALSE;
    351                     UBool middle = TRUE;
    352 
    353                     while (t < data.END_TIME) {
    354                         if (!tztAvail) {
    355                             testTimes[0] = t;
    356                             expectedRoundTrip[0] = TRUE;
    357                             testLen = 1;
    358                         } else {
    359                             int32_t fromOffset = tzt.getFrom()->getRawOffset() + tzt.getFrom()->getDSTSavings();
    360                             int32_t toOffset = tzt.getTo()->getRawOffset() + tzt.getTo()->getDSTSavings();
    361                             int32_t delta = toOffset - fromOffset;
    362                             if (delta < 0) {
    363                                 UBool isDstDecession = tzt.getFrom()->getDSTSavings() > 0 && tzt.getTo()->getDSTSavings() == 0;
    364                                 testTimes[0] = t + delta - 1;
    365                                 expectedRoundTrip[0] = TRUE;
    366                                 testTimes[1] = t + delta;
    367                                 expectedRoundTrip[1] = isDstDecession ? !AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
    368                                 testTimes[2] = t - 1;
    369                                 expectedRoundTrip[2] = isDstDecession ? !AMBIGUOUS_DST_DECESSION[patidx] : !AMBIGUOUS_NEGATIVE_SHIFT[patidx];
    370                                 testTimes[3] = t;
    371                                 expectedRoundTrip[3] = TRUE;
    372                                 testLen = 4;
    373                             } else {
    374                                 testTimes[0] = t - 1;
    375                                 expectedRoundTrip[0] = TRUE;
    376                                 testTimes[1] = t;
    377                                 expectedRoundTrip[1] = TRUE;
    378                                 testLen = 2;
    379                             }
    380                         }
    381                         for (int32_t testidx = 0; testidx < testLen; testidx++) {
    382                             if (data.quick) {
    383                                 // reduce regular test time
    384                                 if (!expectedRoundTrip[testidx]) {
    385                                     continue;
    386                                 }
    387                             }
    388 
    389                             testCounts++;
    390 
    391                             UnicodeString text;
    392                             FieldPosition fpos(0);
    393                             sdf->format(testTimes[testidx], text, fpos);
    394 
    395                             UDate parsedDate = sdf->parse(text, status);
    396                             if (U_FAILURE(status)) {
    397                                 log.errln((UnicodeString) "Parse failure for text=" + text + ", tzid=" + *tzid + ", locale=" + data.locales[locidx].getName()
    398                                         + ", pattern=" + PATTERNS[patidx] + ", time=" + testTimes[testidx]);
    399                                 status = U_ZERO_ERROR;
    400                                 continue;
    401                             }
    402                             if (parsedDate != testTimes[testidx]) {
    403                                 UnicodeString msg = (UnicodeString) "Time round trip failed for " + "tzid=" + *tzid + ", locale=" + data.locales[locidx].getName() + ", pattern=" + PATTERNS[patidx]
    404                                         + ", text=" + text + ", time=" + testTimes[testidx] + ", restime=" + parsedDate + ", diff=" + (parsedDate - testTimes[testidx]);
    405                                 if (expectedRoundTrip[testidx]) {
    406                                     log.errln((UnicodeString) "FAIL: " + msg);
    407                                 } else if (REALLY_VERBOSE) {
    408                                     log.logln(msg);
    409                                 }
    410                             }
    411                         }
    412                         tztAvail = tz->getNextTransition(t, FALSE, tzt);
    413                         if (!tztAvail) {
    414                             break;
    415                         }
    416                         if (middle) {
    417                             // Test the date in the middle of two transitions.
    418                             t += (int64_t) ((tzt.getTime() - t) / 2);
    419                             middle = FALSE;
    420                             tztAvail = FALSE;
    421                         } else {
    422                             t = tzt.getTime();
    423                         }
    424                     }
    425                     delete tz;
    426                 }
    427                 times[patidx] += (Calendar::getNow() - timer);
    428                 delete sdf;
    429             }
    430             umtx_lock(NULL);
    431             data.numDone++;
    432             umtx_unlock(NULL);
    433         }
    434         delete tzids;
    435     }
    436 private:
    437     IntlTest& log;
    438     LocaleData& data;
    439     int32_t index;
    440 };
    441 
    442 void
    443 TimeZoneFormatTest::TestTimeRoundTrip(void) {
    444     int32_t nThreads = threadCount;
    445     const Locale *LOCALES;
    446     int32_t nLocales;
    447     int32_t testCounts = 0;
    448 
    449     UErrorCode status = U_ZERO_ERROR;
    450     Calendar *cal = Calendar::createInstance(TimeZone::createTimeZone((UnicodeString) "UTC"), status);
    451     if (U_FAILURE(status)) {
    452         dataerrln("Calendar::createInstance failed: %s", u_errorName(status));
    453         return;
    454     }
    455 
    456     const char* testAllProp = getProperty("TimeZoneRoundTripAll");
    457     UBool bTestAll = (testAllProp && uprv_strcmp(testAllProp, "true") == 0);
    458 
    459     UDate START_TIME, END_TIME;
    460     if (bTestAll || !quick) {
    461         cal->set(1900, UCAL_JANUARY, 1);
    462     } else {
    463         cal->set(1990, UCAL_JANUARY, 1);
    464     }
    465     START_TIME = cal->getTime(status);
    466 
    467     cal->set(2015, UCAL_JANUARY, 1);
    468     END_TIME = cal->getTime(status);
    469 
    470     if (U_FAILURE(status)) {
    471         errln("getTime failed");
    472         return;
    473     }
    474 
    475     UDate times[NUM_PATTERNS];
    476     for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    477         times[i] = 0;
    478     }
    479 
    480     // Set up test locales
    481     const Locale locales1[] = {Locale("en")};
    482     const Locale locales2[] = {
    483         Locale("ar_EG"), Locale("bg_BG"), Locale("ca_ES"), Locale("da_DK"), Locale("de"),
    484         Locale("de_DE"), Locale("el_GR"), Locale("en"), Locale("en_AU"), Locale("en_CA"),
    485         Locale("en_US"), Locale("es"), Locale("es_ES"), Locale("es_MX"), Locale("fi_FI"),
    486         Locale("fr"), Locale("fr_CA"), Locale("fr_FR"), Locale("he_IL"), Locale("hu_HU"),
    487         Locale("it"), Locale("it_IT"), Locale("ja"), Locale("ja_JP"), Locale("ko"),
    488         Locale("ko_KR"), Locale("nb_NO"), Locale("nl_NL"), Locale("nn_NO"), Locale("pl_PL"),
    489         Locale("pt"), Locale("pt_BR"), Locale("pt_PT"), Locale("ru_RU"), Locale("sv_SE"),
    490         Locale("th_TH"), Locale("tr_TR"), Locale("zh"), Locale("zh_Hans"), Locale("zh_Hans_CN"),
    491         Locale("zh_Hant"), Locale("zh_Hant_TW")
    492     };
    493 
    494     if (bTestAll) {
    495         LOCALES = Locale::getAvailableLocales(nLocales);
    496     } else if (quick) {
    497         LOCALES = locales1;
    498         nLocales = sizeof(locales1)/sizeof(Locale);
    499     } else {
    500         LOCALES = locales2;
    501         nLocales = sizeof(locales2)/sizeof(Locale);
    502     }
    503 
    504     LocaleData data;
    505     data.index = 0;
    506     data.testCounts = testCounts;
    507     data.times = times;
    508     data.locales = LOCALES;
    509     data.nLocales = nLocales;
    510     data.quick = quick;
    511     data.START_TIME = START_TIME;
    512     data.END_TIME = END_TIME;
    513     data.numDone = 0;
    514 
    515 #if (ICU_USE_THREADS==0)
    516     TestTimeRoundTripThread fakeThread(*this, data, 0);
    517     fakeThread.run();
    518 #else
    519     TestTimeRoundTripThread **threads = new TestTimeRoundTripThread*[threadCount];
    520     int32_t i;
    521     for (i = 0; i < nThreads; i++) {
    522         threads[i] = new TestTimeRoundTripThread(*this, data, i);
    523         if (threads[i]->start() != 0) {
    524             errln("Error starting thread %d", i);
    525         }
    526     }
    527 
    528     UBool done = false;
    529     while (true) {
    530         umtx_lock(NULL);
    531         if (data.numDone == nLocales) {
    532             done = true;
    533         }
    534         umtx_unlock(NULL);
    535         if (done)
    536             break;
    537         SimpleThread::sleep(1000);
    538     }
    539 
    540     for (i = 0; i < nThreads; i++) {
    541         delete threads[i];
    542     }
    543     delete [] threads;
    544 
    545 #endif
    546     UDate total = 0;
    547     logln("### Elapsed time by patterns ###");
    548     for (int32_t i = 0; i < NUM_PATTERNS; i++) {
    549         logln(UnicodeString("") + data.times[i] + "ms (" + PATTERNS[i] + ")");
    550         total += data.times[i];
    551     }
    552     logln((UnicodeString) "Total: " + total + "ms");
    553     logln((UnicodeString) "Iteration: " + data.testCounts);
    554 
    555     delete cal;
    556 }
    557 
    558 #endif /* #if !UCONFIG_NO_FORMATTING */
    559 
    560 
    561 
    562