Home | History | Annotate | Download | only in intltest
      1 /***********************************************************************
      2  * Copyright (c) 1997-2011, International Business Machines Corporation
      3  * and others. All Rights Reserved.
      4  ***********************************************************************/
      5 
      6 #include "unicode/utypes.h"
      7 
      8 #if !UCONFIG_NO_FORMATTING
      9 
     10 #include "miscdtfm.h"
     11 
     12 #include "unicode/format.h"
     13 #include "unicode/decimfmt.h"
     14 #include "unicode/datefmt.h"
     15 #include "unicode/smpdtfmt.h"
     16 #include "unicode/dtfmtsym.h"
     17 #include "unicode/locid.h"
     18 #include "unicode/msgfmt.h"
     19 #include "unicode/numfmt.h"
     20 #include "unicode/choicfmt.h"
     21 #include "unicode/gregocal.h"
     22 
     23 // *****************************************************************************
     24 // class DateFormatMiscTests
     25 // *****************************************************************************
     26 
     27 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
     28 
     29 void
     30 DateFormatMiscTests::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
     31 {
     32     // if (exec) logln((UnicodeString)"TestSuite DateFormatMiscTests");
     33     switch (index) {
     34         CASE(0, test4097450)
     35         CASE(1, test4099975)
     36         CASE(2, test4117335)
     37 
     38         default: name = ""; break;
     39     }
     40 }
     41 
     42 UBool
     43 DateFormatMiscTests::failure(UErrorCode status, const char* msg)
     44 {
     45     if(U_FAILURE(status)) {
     46         errcheckln(status, UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
     47         return TRUE;
     48     }
     49 
     50     return FALSE;
     51 }
     52 
     53 /*
     54  * @bug 4097450
     55  */
     56 void
     57 DateFormatMiscTests::test4097450()
     58 {
     59     //
     60     // Date parse requiring 4 digit year.
     61     //
     62     UnicodeString  dstring [] = {
     63         UnicodeString("97"),
     64         UnicodeString("1997"),
     65         UnicodeString("97"),
     66         UnicodeString("1997"),
     67         UnicodeString("01"),
     68         UnicodeString("2001"),
     69         UnicodeString("01"),
     70         UnicodeString("2001"),
     71         UnicodeString("1"),
     72         UnicodeString("1"),
     73         UnicodeString("11"),
     74         UnicodeString("11"),
     75         UnicodeString("111"),
     76         UnicodeString("111")
     77     };
     78 
     79     UnicodeString dformat [] = {
     80         UnicodeString("yy"),
     81         UnicodeString("yy"),
     82         UnicodeString("yyyy"),
     83         UnicodeString("yyyy"),
     84         UnicodeString("yy"),
     85         UnicodeString("yy"),
     86         UnicodeString("yyyy"),
     87         UnicodeString("yyyy"),
     88         UnicodeString("yy"),
     89         UnicodeString("yyyy"),
     90         UnicodeString("yy"),
     91         UnicodeString("yyyy"),
     92         UnicodeString("yy"),
     93         UnicodeString("yyyy")
     94     };
     95 
     96 /*    UBool dresult [] = {
     97         TRUE,
     98         FALSE,
     99         FALSE,
    100         TRUE,
    101         TRUE,
    102         FALSE,
    103         FALSE,
    104         TRUE,
    105         FALSE,
    106         FALSE,
    107         TRUE,
    108         FALSE,
    109         FALSE,
    110         FALSE
    111     };*/
    112 
    113     UErrorCode status = U_ZERO_ERROR;
    114     SimpleDateFormat *formatter;
    115     SimpleDateFormat *resultFormatter = new SimpleDateFormat((UnicodeString)"yyyy", status);
    116     if (U_FAILURE(status)) {
    117         dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
    118         return;
    119     }
    120 
    121     logln("Format\tSource\tResult");
    122     logln("-------\t-------\t-------");
    123     for (int i = 0; i < 14/*dstring.length*/; i++)
    124     {
    125         log(dformat[i] + "\t" + dstring[i] + "\t");
    126         formatter = new SimpleDateFormat(dformat[i], status);
    127         if(failure(status, "new SimpleDateFormat")) return;
    128         //try {
    129         UnicodeString str;
    130         FieldPosition pos(FieldPosition::DONT_CARE);
    131         logln(resultFormatter->format(formatter->parse(dstring[i], status), str, pos));
    132         failure(status, "resultFormatter->format");
    133             //if ( !dresult[i] ) System.out.print("   <-- error!");
    134         /*}
    135         catch (ParseException exception) {
    136             //if ( dresult[i] ) System.out.print("   <-- error!");
    137             System.out.print("exception --> " + exception);
    138         }*/
    139         delete formatter;
    140         logln();
    141     }
    142 
    143     delete resultFormatter;
    144 }
    145 
    146 /*
    147  * @bug 4099975
    148  */
    149 void
    150 DateFormatMiscTests::test4099975()
    151 {
    152     /**
    153      * Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern,
    154      *                                    const DateFormatSymbols & formatData, UErrorCode & status )
    155      * The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols
    156      * should not change the SimpleDateFormat's behavior.
    157      */
    158     UDate d = Calendar::getNow();
    159     {
    160         UErrorCode status = U_ZERO_ERROR;
    161         DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
    162         if (U_FAILURE(status)) {
    163             dataerrln("Unable to create DateFormatSymbols - %s", u_errorName(status));
    164             return;
    165         }
    166         SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols, status);
    167         if(failure(status, "new SimpleDateFormat")) return;
    168         UnicodeString format0;
    169         format0 = df->format(d, format0);
    170         UnicodeString localizedPattern0;
    171         localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
    172         failure(status, "df->toLocalizedPattern");
    173         symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
    174         UnicodeString format1;
    175         format1 = df->format(d, format1);
    176         if (format0 != format1) {
    177             errln(UnicodeString("Formats are different. format0: ") + format0
    178                 + UnicodeString("; format1: ") + format1);
    179         }
    180         UnicodeString localizedPattern1;
    181         localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
    182         failure(status, "df->toLocalizedPattern");
    183         if (localizedPattern0 != localizedPattern1) {
    184             errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
    185                 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
    186         }
    187         delete symbols;
    188         delete df;
    189     }
    190     /*
    191      * Test void SimpleDateFormat::setDateFormatSymbols (  const DateFormatSymbols & newFormatSymbols )
    192      * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
    193      */
    194     {
    195         UErrorCode status = U_ZERO_ERROR;
    196         DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
    197         if(failure(status, "new DateFormatSymbols")) return;
    198         SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status);
    199         if(failure(status, "new SimpleDateFormat")) return;
    200         df->setDateFormatSymbols(*symbols);
    201         UnicodeString format0;
    202         format0 = df->format(d, format0);
    203         UnicodeString localizedPattern0;
    204         localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
    205         failure(status, "df->toLocalizedPattern");
    206         symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
    207         UnicodeString format1;
    208         format1 = df->format(d, format1);
    209         if (format0 != format1) {
    210             errln(UnicodeString("Formats are different. format0: ") + format0
    211                 + UnicodeString("; format1: ") + format1);
    212         }
    213         UnicodeString localizedPattern1;
    214         localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
    215         failure(status, "df->toLocalizedPattern");
    216         if (localizedPattern0 != localizedPattern1) {
    217             errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
    218                 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
    219         }
    220         delete symbols;
    221         delete df;
    222 
    223     }
    224     //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
    225     {
    226         UErrorCode status = U_ZERO_ERROR;
    227         DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
    228         if(failure(status, "new DateFormatSymbols")) return;
    229         SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), symbols, status);
    230         if(failure(status, "new SimpleDateFormat")) return;
    231         UnicodeString format0;
    232         format0 = df->format(d, format0);
    233         UnicodeString localizedPattern0;
    234         localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
    235         failure(status, "df->toLocalizedPattern");
    236         symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
    237         UnicodeString format1;
    238         format1 = df->format(d, format1);
    239         if (format0 != format1) {
    240             errln(UnicodeString("Formats are different. format0: ") + format0
    241                 + UnicodeString("; format1: ") + format1);
    242         }
    243         UnicodeString localizedPattern1;
    244         localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
    245         failure(status, "df->toLocalizedPattern");
    246         if (localizedPattern0 == localizedPattern1) {
    247             errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
    248                 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
    249         }
    250         //delete symbols; the caller is no longer responsible for deleting the symbols
    251         delete df;
    252     }
    253     //
    254     {
    255         UErrorCode status = U_ZERO_ERROR;
    256         DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
    257         failure(status, "new DateFormatSymbols");
    258         SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status);
    259         if(failure(status, "new SimpleDateFormat")) return;
    260         df-> adoptDateFormatSymbols(symbols);
    261         UnicodeString format0;
    262         format0 = df->format(d, format0);
    263         UnicodeString localizedPattern0;
    264         localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
    265         failure(status, "df->toLocalizedPattern");
    266         symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
    267         UnicodeString format1;
    268         format1 = df->format(d, format1);
    269         if (format0 != format1) {
    270             errln(UnicodeString("Formats are different. format0: ") + format0
    271                 + UnicodeString("; format1: ") + format1);
    272         }
    273         UnicodeString localizedPattern1;
    274         localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
    275         failure(status, "df->toLocalizedPattern");
    276         if (localizedPattern0 == localizedPattern1) {
    277             errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
    278                 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
    279         }
    280         //delete symbols; the caller is no longer responsible for deleting the symbols
    281         delete df;
    282     }
    283 }
    284 
    285 /*
    286  * @test @(#)bug4117335.java    1.1 3/5/98
    287  *
    288  * @bug 4117335
    289  */
    290 void
    291 DateFormatMiscTests::test4117335()
    292 {
    293     //UnicodeString bc = "\u7d00\u5143\u524d";
    294     UChar bcC [] = {
    295         0x7D00,
    296         0x5143,
    297         0x524D
    298     };
    299     UnicodeString bc(bcC, 3, 3);
    300 
    301     //UnicodeString ad = "\u897f\u66a6";
    302     UChar adC [] = {
    303         0x897F,
    304         0x66A6
    305     };
    306     UnicodeString ad(adC, 2, 2);
    307 
    308     //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
    309     UChar jstLongC [] = {
    310         0x65e5,
    311         0x672c,
    312         0x6a19,
    313         0x6e96,
    314         0x6642
    315     };
    316     UChar jdtLongC [] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
    317 
    318     UnicodeString jstLong(jstLongC, 5, 5);
    319 
    320 //    UnicodeString jstShort = "JST";
    321 
    322     UnicodeString tzID = "Asia/Tokyo";
    323 
    324     UnicodeString jdtLong(jdtLongC, 5, 5);
    325 
    326 //    UnicodeString jdtShort = "JDT";
    327     UErrorCode status = U_ZERO_ERROR;
    328     DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getJapan(), status);
    329     if(U_FAILURE(status)) {
    330       dataerrln("Failure creating DateFormatSymbols, %s", u_errorName(status));
    331       delete symbols;
    332       return;
    333     }
    334     failure(status, "new DateFormatSymbols");
    335     int32_t eraCount = 0;
    336     const UnicodeString *eras = symbols->getEraNames(eraCount);
    337 
    338     logln(UnicodeString("BC = ") + eras[0]);
    339     if (eras[0] != bc) {
    340         errln("*** Should have been " + bc);
    341         //throw new Exception("Error in BC");
    342     }
    343 
    344     logln(UnicodeString("AD = ") + eras[1]);
    345     if (eras[1] != ad) {
    346         errln("*** Should have been " + ad);
    347         //throw new Exception("Error in AD");
    348     }
    349 
    350     int32_t rowCount, colCount;
    351     const UnicodeString **zones = symbols->getZoneStrings(rowCount, colCount);
    352     //don't hard code the index .. compute it.
    353     int32_t index = -1;
    354     for (int32_t i = 0; i < rowCount; ++i) {
    355         if (tzID == (zones[i][0])) {
    356             index = i;
    357             break;
    358         }
    359     }
    360     logln(UnicodeString("Long zone name = ") + zones[index][1]);
    361     if (zones[index][1] != jstLong) {
    362         errln("*** Should have been " + prettify(jstLong)+ " but it is: " + prettify(zones[index][1]));
    363         //throw new Exception("Error in long TZ name");
    364     }
    365 //    logln(UnicodeString("Short zone name = ") + zones[index][2]);
    366 //    if (zones[index][2] != jstShort) {
    367 //        errln("*** Should have been " + prettify(jstShort) + " but it is: " + prettify(zones[index][2]));
    368 //        //throw new Exception("Error in short TZ name");
    369 //    }
    370     logln(UnicodeString("Long zone name = ") + zones[index][3]);
    371     if (zones[index][3] != jdtLong) {
    372         errln("*** Should have been " + prettify(jstLong) + " but it is: " + prettify(zones[index][3]));
    373         //throw new Exception("Error in long TZ name");
    374     }
    375 //    logln(UnicodeString("SHORT zone name = ") + zones[index][4]);
    376 //    if (zones[index][4] != jdtShort) {
    377 //        errln("*** Should have been " + prettify(jstShort)+ " but it is: " + prettify(zones[index][4]));
    378 //        //throw new Exception("Error in short TZ name");
    379 //    }
    380     delete symbols;
    381 
    382 }
    383 
    384 #endif /* #if !UCONFIG_NO_FORMATTING */
    385