Home | History | Annotate | Download | only in intltest
      1 
      2 /********************************************************************
      3  * COPYRIGHT:
      4  * Copyright (c) 1997-2009, International Business Machines Corporation and
      5  * others. All Rights Reserved.
      6  ********************************************************************/
      7 
      8 #include "unicode/utypes.h"
      9 
     10 #if !UCONFIG_NO_FORMATTING
     11 
     12 #include "sdtfmtts.h"
     13 
     14 #include "unicode/smpdtfmt.h"
     15 #include "unicode/dtfmtsym.h"
     16 
     17 // This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
     18 // try to test the full functionality.  It just calls each function in the class and
     19 // verifies that it works on a basic level.
     20 
     21 void IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
     22 {
     23     if (exec) logln("TestSuite SimpleDateFormatAPI");
     24     switch (index) {
     25         case 0: name = "SimpleDateFormat API test";
     26                 if (exec) {
     27                     logln("SimpleDateFormat API test---"); logln("");
     28                     UErrorCode status = U_ZERO_ERROR;
     29                     Locale saveLocale;
     30                     Locale::setDefault(Locale::getEnglish(), status);
     31                     if(U_FAILURE(status)) {
     32                         errln("ERROR: Could not set default locale, test may not give correct results");
     33                     }
     34                     testAPI(/*par*/);
     35                     Locale::setDefault(saveLocale, status);
     36                 }
     37                 break;
     38 
     39         default: name = ""; break;
     40     }
     41 }
     42 
     43 /**
     44  * Test various generic API methods of SimpleDateFormat for API coverage.
     45  */
     46 void IntlTestSimpleDateFormatAPI::testAPI(/*char *par*/)
     47 {
     48     UErrorCode status = U_ZERO_ERROR;
     49 
     50 // ======= Test constructors
     51 
     52     logln("Testing SimpleDateFormat constructors");
     53 
     54     SimpleDateFormat def(status);
     55     if(U_FAILURE(status)) {
     56         dataerrln("ERROR: Could not create SimpleDateFormat (default) - exitting");
     57         return;
     58     }
     59 
     60     status = U_ZERO_ERROR;
     61     const UnicodeString pattern("yyyy.MM.dd G 'at' hh:mm:ss z");
     62     const UnicodeString override("y=hebr;d=thai;s=arab");
     63     const UnicodeString override_bogus("y=hebr;d=thai;s=bogus");
     64 
     65     SimpleDateFormat pat(pattern, status);
     66     if(U_FAILURE(status)) {
     67         errln("ERROR: Could not create SimpleDateFormat (pattern)");
     68     }
     69 
     70     status = U_ZERO_ERROR;
     71     SimpleDateFormat pat_fr(pattern, Locale::getFrench(), status);
     72     if(U_FAILURE(status)) {
     73         errln("ERROR: Could not create SimpleDateFormat (pattern French)");
     74     }
     75 
     76     status = U_ZERO_ERROR;
     77     DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getFrench(), status);
     78     if(U_FAILURE(status)) {
     79         errln("ERROR: Could not create DateFormatSymbols (French)");
     80     }
     81 
     82     status = U_ZERO_ERROR;
     83     SimpleDateFormat cust1(pattern, symbols, status);
     84     if(U_FAILURE(status)) {
     85         dataerrln("ERROR: Could not create SimpleDateFormat (pattern, symbols*) - exitting");
     86         return;
     87     }
     88 
     89     status = U_ZERO_ERROR;
     90     SimpleDateFormat cust2(pattern, *symbols, status);
     91     if(U_FAILURE(status)) {
     92         errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
     93     }
     94 
     95     status = U_ZERO_ERROR;
     96     SimpleDateFormat ovr1(pattern, override, status);
     97     if(U_FAILURE(status)) {
     98         errln("ERROR: Could not create SimpleDateFormat (pattern, override)");
     99     }
    100 
    101     status = U_ZERO_ERROR;
    102     SimpleDateFormat ovr2(pattern, override, Locale::getGerman(), status);
    103     if(U_FAILURE(status)) {
    104         errln("ERROR: Could not create SimpleDateFormat (pattern, override, locale)");
    105     }
    106 
    107     status = U_ZERO_ERROR;
    108     SimpleDateFormat ovr3(pattern, override_bogus, Locale::getGerman(), status);
    109     if(U_SUCCESS(status)) {
    110         errln("ERROR: Should not have been able to create SimpleDateFormat (pattern, override, locale) with a bogus override");
    111     }
    112 
    113 
    114     SimpleDateFormat copy(pat);
    115 
    116 // ======= Test clone(), assignment, and equality
    117 
    118     logln("Testing clone(), assignment and equality operators");
    119 
    120     if( ! (copy == pat) || copy != pat) {
    121         errln("ERROR: Copy constructor (or ==) failed");
    122     }
    123 
    124     copy = cust1;
    125     if(copy != cust1) {
    126         errln("ERROR: Assignment (or !=) failed");
    127     }
    128 
    129     Format *clone = def.clone();
    130     if( ! (*clone == def) ) {
    131         errln("ERROR: Clone() (or ==) failed");
    132     }
    133     delete clone;
    134 
    135 // ======= Test various format() methods
    136 
    137     logln("Testing various format() methods");
    138 
    139     UDate d = 837039928046.0;
    140     Formattable fD(d, Formattable::kIsDate);
    141 
    142     UnicodeString res1, res2;
    143     FieldPosition pos1(0), pos2(0);
    144 
    145     res1 = def.format(d, res1, pos1);
    146     logln( (UnicodeString) "" + d + " formatted to " + res1);
    147 
    148     status = U_ZERO_ERROR;
    149     res2 = cust1.format(fD, res2, pos2, status);
    150     if(U_FAILURE(status)) {
    151         errln("ERROR: format(Formattable [Date]) failed");
    152     }
    153     logln((UnicodeString) "" + fD.getDate() + " formatted to " + res2);
    154 
    155 // ======= Test parse()
    156 
    157     logln("Testing parse()");
    158 
    159     UnicodeString text("02/03/76 2:50 AM, CST");
    160     UDate result1, result2;
    161     ParsePosition pos(0);
    162     result1 = def.parse(text, pos);
    163     logln(text + " parsed into " + result1);
    164 
    165     status = U_ZERO_ERROR;
    166     result2 = def.parse(text, status);
    167     if(U_FAILURE(status)) {
    168         errln("ERROR: parse() failed");
    169     }
    170     logln(text + " parsed into " + result2);
    171 
    172 // ======= Test getters and setters
    173 
    174     logln("Testing getters and setters");
    175 
    176     const DateFormatSymbols *syms = pat.getDateFormatSymbols();
    177     if(!syms) {
    178       errln("Couldn't obtain DateFormatSymbols. Quitting test!");
    179       return;
    180     }
    181     if(syms->getDynamicClassID() != DateFormatSymbols::getStaticClassID()) {
    182         errln("ERROR: format->getDateFormatSymbols()->getDynamicClassID() != DateFormatSymbols::getStaticClassID()");
    183     }
    184     DateFormatSymbols *newSyms = new DateFormatSymbols(*syms);
    185     def.adoptDateFormatSymbols(newSyms);
    186     pat_fr.setDateFormatSymbols(*newSyms);
    187     if( *(pat.getDateFormatSymbols()) != *(def.getDateFormatSymbols())) {
    188         errln("ERROR: adopt or set DateFormatSymbols() failed");
    189     }
    190 
    191     status = U_ZERO_ERROR;
    192     UDate startDate = pat.get2DigitYearStart(status);
    193     if(U_FAILURE(status)) {
    194         errln("ERROR: getTwoDigitStartDate() failed");
    195     }
    196 
    197     status = U_ZERO_ERROR;
    198     pat_fr.set2DigitYearStart(startDate, status);
    199     if(U_FAILURE(status)) {
    200         errln("ERROR: setTwoDigitStartDate() failed");
    201     }
    202 
    203 // ======= Test DateFormatSymbols constructor
    204     newSyms  =new DateFormatSymbols("gregorian", status);
    205     if(U_FAILURE(status)) {
    206         errln("ERROR: new DateFormatSymbols() failed");
    207     }
    208     def.adoptDateFormatSymbols(newSyms);
    209 
    210 // ======= Test applyPattern()
    211 
    212     logln("Testing applyPattern()");
    213 
    214     UnicodeString p1("yyyy.MM.dd G 'at' hh:mm:ss z");
    215     logln("Applying pattern " + p1);
    216     status = U_ZERO_ERROR;
    217     pat.applyPattern(p1);
    218 
    219     UnicodeString s2;
    220     s2 = pat.toPattern(s2);
    221     logln("Extracted pattern is " + s2);
    222     if(s2 != p1) {
    223         errln("ERROR: toPattern() result did not match pattern applied");
    224     }
    225 
    226     logln("Applying pattern " + p1);
    227     status = U_ZERO_ERROR;
    228     pat.applyLocalizedPattern(p1, status);
    229     if(U_FAILURE(status)) {
    230         errln("ERROR: applyPattern() failed with " + (int32_t) status);
    231     }
    232     UnicodeString s3;
    233     status = U_ZERO_ERROR;
    234     s3 = pat.toLocalizedPattern(s3, status);
    235     if(U_FAILURE(status)) {
    236         errln("ERROR: toLocalizedPattern() failed");
    237     }
    238     logln("Extracted pattern is " + s3);
    239     if(s3 != p1) {
    240         errln("ERROR: toLocalizedPattern() result did not match pattern applied");
    241     }
    242 
    243 // ======= Test getStaticClassID()
    244 
    245     logln("Testing getStaticClassID()");
    246 
    247     status = U_ZERO_ERROR;
    248     DateFormat *test = new SimpleDateFormat(status);
    249     if(U_FAILURE(status)) {
    250         errln("ERROR: Couldn't create a SimpleDateFormat");
    251     }
    252 
    253     if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
    254         errln("ERROR: getDynamicClassID() didn't return the expected value");
    255     }
    256 
    257     delete test;
    258 
    259 // ======= Test Ticket 5684 (Parsing with 'e' and 'Y')
    260     SimpleDateFormat object(UNICODE_STRING_SIMPLE("YYYY'W'wwe"), status);
    261     if(U_FAILURE(status)) {
    262         errln("ERROR: Couldn't create a SimpleDateFormat");
    263     }
    264     object.setLenient(false);
    265     ParsePosition pp(0);
    266     UDate udDate = object.parse("2007W014", pp);
    267     if ((double)udDate == 0.0) {
    268         errln("ERROR: Parsing failed using 'Y' and 'e'");
    269     }
    270 }
    271 
    272 #endif /* #if !UCONFIG_NO_FORMATTING */
    273