]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/dtfmrgts.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / dtfmrgts.cpp
index 886f764b8533d37dde5bb58e8d68ebb3db4b2f07..e2b90d47e71dbbdd493b5f1d71405bd5aed06a9f 100644 (file)
@@ -1,13 +1,14 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2010, International Business Machines Corporation and
+ * Copyright (c) 1997-2016, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
 #include "unicode/utypes.h"
 
 #if !UCONFIG_NO_FORMATTING
-
 #include "dtfmrgts.h"
 
 #include "unicode/timezone.h"
@@ -16,6 +17,7 @@
 #include "unicode/datefmt.h"
 #include "unicode/simpletz.h"
 #include "unicode/resbund.h"
+#include "cmemory.h"
 
 // *****************************************************************************
 // class DateFormatRegressionTest
@@ -55,6 +57,12 @@ DateFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char*
         CASE(24,Test714)
         CASE(25,Test1684)
         CASE(26,Test5554)
+        CASE(27,Test9237)
+        CASE(28,TestParsing)
+        CASE(29,TestT10334)
+        CASE(30,TestT10619)
+        CASE(31,TestT10855)
+        CASE(32,TestT10906)
         default: name = ""; break;
     }
 }
@@ -82,6 +90,7 @@ void DateFormatRegressionTest::Test4029195(void)
     pat = sdf->toPattern(pat);
     logln("pattern: " + pat);
     UnicodeString fmtd;
+
     FieldPosition pos(FieldPosition::DONT_CARE);
     fmtd = sdf->format(today, fmtd, pos);
     logln("today: " + fmtd);
@@ -132,8 +141,8 @@ void DateFormatRegressionTest::Test4052408(void)
     str = fmt->format(dt, str);
     logln(str);
     
-    if(str != "5/3/97 8:55 AM")
-        errln("Fail: Test broken; Want 5/3/97 8:55 AM Got " + str);   
+    if(str != "5/3/97, 8:55 AM")
+        errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str);   
     
     UnicodeString expected[] = {
         (UnicodeString) "", //"ERA_FIELD",
@@ -362,9 +371,8 @@ void DateFormatRegressionTest::Test4060212(void)
         errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
                             " Want 40");
 
-#if 0
     // this is an odd usage of "ddd" and it doesn't
-    // work now that date values are range checked.
+    // work now that date values are range checked per #3579.
     logln("Using yyyy-ddd.hh:mm:ss");
     delete formatter;
     formatter = NULL;
@@ -379,7 +387,6 @@ void DateFormatRegressionTest::Test4060212(void)
     if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
         errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
                             " Want 40");
-#endif
 
     delete formatter;
     delete fmt;
@@ -401,8 +408,8 @@ void DateFormatRegressionTest::Test4061287(void)
     }
     failure(status, "new SimpleDateFormat");
     //try {
-    logln(UnicodeString("") + df->parse("30/02/1971", status));  
-    failure(status, "df->parse(\"30/02/1971\")");
+    logln(UnicodeString("") + df->parse("35/01/1971", status));  
+    failure(status, "df->parse(\"35/01/1971\")");
     //logln(df.parse("35/01/1971").toString());
     //}
     /*catch (ParseException e) {
@@ -412,7 +419,7 @@ void DateFormatRegressionTest::Test4061287(void)
     df->setLenient(FALSE);
     UBool ok = FALSE;
     //try {
-    logln(UnicodeString("") + df->parse("30/02/1971", status));
+    logln(UnicodeString("") + df->parse("35/01/1971", status));
     if(U_FAILURE(status))
         ok = TRUE;
     //logln(df.parse("35/01/1971").toString());
@@ -720,8 +727,8 @@ void DateFormatRegressionTest::Test4101483(void)
     sdf->format(d, buf, fp);
     //logln(sdf.format(d, buf, fp).toString());
     logln(dateToString(d) + " => " + buf);
-    logln("beginIndex = " + fp.getBeginIndex());
-    logln("endIndex = " + fp.getEndIndex());
+    logln(UnicodeString("beginIndex = ") + fp.getBeginIndex());
+    logln(UnicodeString("endIndex = ") + fp.getEndIndex());
     if (fp.getBeginIndex() == fp.getEndIndex()) 
         errln("Fail: Empty field");
 
@@ -843,7 +850,7 @@ void DateFormatRegressionTest::Test4104136(void)
         logln(" index: %d", pos.getIndex()); 
         logln((UnicodeString) " result: " + d);
         if(pos.getIndex() != finish.getIndex())
-            errln("Fail: Expected pos " + finish.getIndex());
+            errln(UnicodeString("Fail: Expected pos ") + finish.getIndex());
         if (! ((d == 0 && exp == -1) || (d == exp)))
             errln((UnicodeString) "Fail: Expected result " + exp);
     }
@@ -1067,7 +1074,7 @@ void DateFormatRegressionTest::Test4151706(void)
         errln("Fail: " + e);
     }*/
     UnicodeString temp;
-    FieldPosition pos(0);
+    FieldPosition pos(FieldPosition::DONT_CARE);
     logln(dateString + " -> " + fmt.format(d, temp, pos));
 }
 
@@ -1095,7 +1102,7 @@ DateFormatRegressionTest::Test4162071(void)
         else
             errln("Parse format \"" + format + "\" failed.");
         UnicodeString temp;
-        FieldPosition pos(0);
+        FieldPosition pos(FieldPosition::DONT_CARE);
         logln(dateString + " -> " + df.format(x, temp, pos));
     //} catch (Exception e) {
     //    errln("Parse format \"" + format + "\" failed.");
@@ -1129,7 +1136,7 @@ void DateFormatRegressionTest::Test4182066(void) {
         "09/12/+1",
         "09/12/001",
     };
-    int32_t STRINGS_COUNT = (int32_t)(sizeof(STRINGS) / sizeof(STRINGS[0]));
+    int32_t STRINGS_COUNT = UPRV_LENGTHOF(STRINGS);
     UDate FAIL_DATE = (UDate) 0;
     UDate DATES[] = {
         date(2000-1900, UCAL_FEBRUARY, 29),
@@ -1323,7 +1330,7 @@ void DateFormatRegressionTest::Test1684(void)
     new Test1684Data(2001,12,30, /*2002, 1,  6,*/  2002,1,1,UCAL_SUNDAY,    "2002 01 01 Sun", "2001 12 06 Sun")
   };
 
-#define kTest1684Count  ((int32_t)(sizeof(tests)/sizeof(tests[0])))
+#define kTest1684Count  UPRV_LENGTHOF(tests)
 
   int32_t pass = 0, error = 0, warning = 0;
   int32_t i;
@@ -1463,6 +1470,260 @@ void DateFormatRegressionTest::Test5554(void)
   delete sdf;
 }
 
+void DateFormatRegressionTest::Test9237(void)
+{
+    UErrorCode status = U_ZERO_ERROR;
+    UnicodeString pattern("VVVV");
+
+    SimpleDateFormat fmt(pattern, status);  // default locale
+    SimpleDateFormat fmtDE(pattern, Locale("de_DE"), status);
+    if (U_FAILURE(status)) {
+        dataerrln("Error constructing SimpleDateFormat");
+        return;
+    }
+
+    // copy constructor
+    SimpleDateFormat fmtCopyDE(fmtDE);
+    UnicodeString resDE, resCopyDE;
+
+    fmtDE.format(0.0, resDE);
+    fmtCopyDE.format(0.0, resCopyDE);
+
+    if (resDE != resCopyDE) {
+        errln(UnicodeString("Error: different result by the copied instance - org:") + resDE + " copy:" + resCopyDE);
+    }
+
+    // test for assignment operator
+    fmt = fmtDE;
+
+    UnicodeString resAssigned;
+    fmt.format(0.0, resAssigned);
+
+    if (resDE != resAssigned) {
+        errln(UnicodeString("Error: different results by the assigned instance - org:") + resDE + " assigned:" + resAssigned);
+    }
+}
+
+void DateFormatRegressionTest::TestParsing(void) {
+    UErrorCode status = U_ZERO_ERROR;
+    UnicodeString pattern("EEE-WW-MMMM-yyyy");
+    UnicodeString text("mon-02-march-2011");
+    int32_t expectedDay = 7;
+
+    SimpleDateFormat format(pattern, status);
+    if (U_FAILURE(status)) {
+        dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status));
+        return;
+    }
+
+    Calendar *cal = new GregorianCalendar(status);
+    if (cal == NULL || U_FAILURE(status)) {
+        errln("Unable to create calendar - %s", u_errorName(status));
+        return;
+    }
+
+    ParsePosition pos(0);
+    format.parse(text, *cal, pos);
+
+    if (cal->get(UCAL_DAY_OF_MONTH, status) != expectedDay) {
+        errln("Parsing failed: day of month should be '7' with pattern: \"" + pattern + "\" for text: \"" + text + "\"");
+    }
+
+    delete cal;
+}
+
+void DateFormatRegressionTest::TestT10334(void) {
+    UErrorCode status = U_ZERO_ERROR;
+    UnicodeString pattern("'--: 'EEE-WW-MMMM-yyyy");
+    UnicodeString text("--mon-02-march-2011");
+    SimpleDateFormat format(pattern, status);
+
+    logln("pattern["+pattern+"] text["+text+"]");
+
+    if (U_FAILURE(status)) {
+        dataerrln("Fail creating SimpleDateFormat object - %s", u_errorName(status));
+        return;
+    }
+
+    format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, FALSE, status);
+    format.parse(text, status);
+    if (!U_FAILURE(status)) {
+        errln("parse partial match did NOT fail in strict mode - %s", u_errorName(status));
+    }
+
+    status = U_ZERO_ERROR;
+    format.setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, TRUE, status);
+    format.parse(text, status);
+    if (U_FAILURE(status)) {
+        errln("parse partial match failure in lenient mode - %s", u_errorName(status));
+    }
+
+    status = U_ZERO_ERROR;
+    pattern = UnicodeString("YYYY MM dd");
+    text =    UnicodeString("2013 12 10");
+    format.applyPattern(pattern);
+    UDate referenceDate = format.parse(text, status);
+
+    FieldPosition fp(FieldPosition::DONT_CARE);
+    UnicodeString formattedString("");
+    pattern = UnicodeString("YYYY LL dd ee cc qq QQ");
+    format.applyPattern(pattern);
+    format.format(referenceDate, formattedString, fp, status);
+    logln("ref date: " + formattedString);
+
+
+    char patternArray[] = "YYYY LLL dd eee ccc qqq QQQ";
+    pattern = UnicodeString(patternArray);
+    text = UnicodeString("2013 12 10 03 3 04 04");
+    status = U_ZERO_ERROR;
+    format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, TRUE, status);
+    format.applyPattern(pattern);
+    ParsePosition pp(0);
+    format.parse(text, pp);
+    if (pp.getErrorIndex() != -1) {
+        errln("numeric parse error");
+    }
+
+    status = U_ZERO_ERROR;
+    format.setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, FALSE, status);
+    format.parse(text, status);
+    if (!U_FAILURE(status)) {
+        errln("numeric parse did NOT fail in strict mode", u_errorName(status));
+    }
+
+}
+
+
+typedef struct {
+    const char * locale;
+    UBool leniency;
+    UnicodeString parseString;
+    UnicodeString pattern;
+    UnicodeString expectedResult;       // null indicates expected error
+} TestDateFormatLeniencyItem;
+
+
+void DateFormatRegressionTest::TestT10619(void) {
+    const UDate july022008 = 1215000001979.0;
+    const TestDateFormatLeniencyItem items[] = {
+        //locale    leniency    parse String                    pattern                             expected result
+        { "en",     true,       UnicodeString("2008-07 02"),    UnicodeString("yyyy-LLLL dd"),      UnicodeString("2008-July 02") },
+        { "en",     false,      UnicodeString("2008-07 03"),    UnicodeString("yyyy-LLLL dd"),      UnicodeString("") },
+        { "en",     true,       UnicodeString("2008-Jan. 04"),  UnicodeString("yyyy-LLL dd"),       UnicodeString("2008-Jan 04") },
+        { "en",     false,      UnicodeString("2008-Jan. 05"),  UnicodeString("yyyy-LLL dd"),       UnicodeString("") },
+        { "en",     true,       UnicodeString("2008-Jan--06"),  UnicodeString("yyyy-MMM -- dd"),    UnicodeString("2008-Jan 06") },
+        { "en",     false,      UnicodeString("2008-Jan--07"),  UnicodeString("yyyy-MMM -- dd"),    UnicodeString("") },
+        { "en",     true,       UnicodeString("6 Jan 08 2008"), UnicodeString("eee MMM dd yyyy"),   UnicodeString("Sat Jan 08 2008") },
+        { "en",     false,      UnicodeString("6 Jan 09 2008"), UnicodeString("eee MMM dd yyyy"),   UnicodeString("") },
+        // terminator
+        { NULL,     true,       UnicodeString(""),              UnicodeString(""),                  UnicodeString("") }                
+    };
+    UErrorCode status = U_ZERO_ERROR;
+    Calendar* cal = Calendar::createInstance(status);
+    if (U_FAILURE(status)) {
+        dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
+    } else {
+        cal->setTime(july022008, status);
+        const TestDateFormatLeniencyItem * itemPtr;
+        for (itemPtr = items; itemPtr->locale != NULL; itemPtr++ ) {
+                                            
+            Locale locale = Locale::createFromName(itemPtr->locale);
+            status = U_ZERO_ERROR;
+            ParsePosition pos(0);
+            SimpleDateFormat * sdmft = new SimpleDateFormat(itemPtr->pattern, locale, status);
+            if (U_FAILURE(status)) {
+                dataerrln("Unable to create SimpleDateFormat - %s", u_errorName(status));
+                continue;
+            }
+            logln("parsing " + itemPtr->parseString);
+            sdmft->setLenient(itemPtr->leniency);
+            sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, itemPtr->leniency, status);
+            sdmft->setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, itemPtr->leniency, status);
+            sdmft->setBooleanAttribute(UDAT_PARSE_PARTIAL_LITERAL_MATCH, itemPtr->leniency, status);
+            sdmft->parse(itemPtr->parseString, pos);
+
+            delete sdmft;
+            if(pos.getErrorIndex() > -1) {
+                if(itemPtr->expectedResult.length() != 0) {
+                   errln("error: unexpected error - " + itemPtr->parseString + " - error index " + pos.getErrorIndex() +
+                           " - leniency " + itemPtr->leniency);
+                   continue;
+                } else {
+                   continue;
+                }
+            }
+        }
+    }
+    delete cal;
+
+}
+
+
+typedef struct {
+    UnicodeString text;
+    UnicodeString pattern;
+    int initialParsePos;
+} T10855Data;
+    
+void DateFormatRegressionTest::TestT10855(void) {
+    // NOTE: these should NOT parse
+    const T10855Data items[] = {
+        //parse String                          pattern                         initial parse pos
+//      { UnicodeString("September 30, 1998"),  UnicodeString("MM-dd-yyyy"),    0}, // for Apple, this can succeed
+        { UnicodeString("123-73-1950"),         UnicodeString("MM-dd-yyyy"),    -1},
+        { UnicodeString("12-23-1950"),          UnicodeString("MM-dd-yyyy"),    -1},
+        // terminator
+        { UnicodeString(""),                    UnicodeString(""),              0}                
+    };
+    UErrorCode status = U_ZERO_ERROR;
+
+    int x = 0;
+    while(items[x].pattern.length() > 0)
+    {
+        status = U_ZERO_ERROR;
+        logln("Date to parse: \""+items[x].text+"\"");
+        logln("Starting Index: %d", items[x].initialParsePos);
+
+        SimpleDateFormat dateFmt(items[x].pattern, status);
+        if(U_FAILURE(status)) { 
+            errcheckln(status, "Failed dateFmt: %s", u_errorName(status));
+            ++x;
+            continue;
+        } 
+        status = U_ZERO_ERROR;  
+
+        dateFmt.setLenient(false);
+        dateFmt.setTimeZone(*TimeZone::getGMT());
+
+        ParsePosition position(items[x].initialParsePos);
+        logln("set position is now: %d", position.getIndex());
+        UDate d = dateFmt.parse(items[x].text, position);
+        if (position.getErrorIndex() != -1 || position.getIndex() == items[x].initialParsePos) {
+            logln("Parse Failed. ErrorIndex is %d - Index is %d", position.getErrorIndex(), position.getIndex());
+        } else {
+            errln("Parse Succeeded...should have failed. Index is %d - ErrorIndex is %d", position.getIndex(), position.getErrorIndex());
+        }
+        logln("Parsed date returns %d\n", d);
+
+        ++x;
+    }
+}
+
+void DateFormatRegressionTest::TestT10906(void) {
+
+      UErrorCode status = U_ZERO_ERROR;
+      UnicodeString pattern = "MM-dd-yyyy";
+      UnicodeString text = "06-10-2014";
+      SimpleDateFormat format(pattern, status);
+      int32_t errorIdx = 0;
+      ParsePosition pp(-1);
+      format.parse(text, pp);
+      errorIdx = pp.getErrorIndex();
+      if (errorIdx == -1) {          
+          errln("failed to report invalid (negative) starting parse position");
+      }
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
 
 //eof