X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..e4f10fab0c078f399c9deef476d9c9b73b47dff8:/icuSources/test/intltest/dtfmrgts.cpp diff --git a/icuSources/test/intltest/dtfmrgts.cpp b/icuSources/test/intltest/dtfmrgts.cpp index 40e19395..220c2739 100644 --- a/icuSources/test/intltest/dtfmrgts.cpp +++ b/icuSources/test/intltest/dtfmrgts.cpp @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2003, International Business Machines Corporation and + * Copyright (c) 1997-2012, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -54,6 +54,9 @@ DateFormatRegressionTest::runIndexedTest( int32_t index, UBool exec, const char* CASE(23,Test4210209) CASE(24,Test714) CASE(25,Test1684) + CASE(26,Test5554) + CASE(27,Test9237) + CASE(28,TestParsing) default: name = ""; break; } } @@ -69,8 +72,15 @@ void DateFormatRegressionTest::Test4029195(void) logln((UnicodeString) "today: " + today); SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance(); - failure(status, "SimpleDateFormat::createDateInstance"); + if (failure(status, "SimpleDateFormat::createDateInstance")) { + return; + } UnicodeString pat; + if(sdf == NULL){ + dataerrln("Error calling DateFormat::createDateTimeInstance"); + return; + } + pat = sdf->toPattern(pat); logln("pattern: " + pat); UnicodeString fmtd; @@ -114,13 +124,18 @@ void DateFormatRegressionTest::Test4052408(void) DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::SHORT, DateFormat::SHORT, Locale::getUS()); + if (fmt == NULL) { + dataerrln("Error calling DateFormat::createDateTimeInstance"); + return; + } + UDate dt = date(97, UCAL_MAY, 3, 8, 55); UnicodeString str; 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", @@ -211,7 +226,10 @@ void DateFormatRegressionTest::Test4056591(void) //try { SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("yyMMdd"), Locale::getUS(), status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) { + delete fmt; + return; + } UDate start = date(1809-1900, UCAL_DECEMBER, 25); fmt->set2DigitYearStart(start, status); failure(status, "fmt->setTwoDigitStartDate"); @@ -267,7 +285,7 @@ void DateFormatRegressionTest::Test4059917(void) UnicodeString myDate; fmt = new SimpleDateFormat( UnicodeString("yyyy/MM/dd"), status ); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; myDate = "1997/01/01"; aux917( fmt, myDate ); @@ -275,7 +293,7 @@ void DateFormatRegressionTest::Test4059917(void) fmt = NULL; fmt = new SimpleDateFormat( UnicodeString("yyyyMMdd"), status ); - failure(status, "new SimpleDateFormat"); + if(failure(status, "new SimpleDateFormat")) return; myDate = "19970101"; aux917( fmt, myDate ); @@ -323,12 +341,18 @@ void DateFormatRegressionTest::Test4060212(void) logln("Using yyyy-DDD.hh:mm:ss"); UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; ParsePosition pos(0); UDate myDate = formatter->parse( dateString, pos ); UnicodeString myString; DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL, DateFormat::LONG); + if (fmt == NULL) { + dataerrln("Error calling DateFormat::createDateTimeInstance"); + delete formatter; + return; + } + myString = fmt->format( myDate, myString); logln( myString ); @@ -340,11 +364,13 @@ void DateFormatRegressionTest::Test4060212(void) errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) + " Want 40"); + // this is an odd usage of "ddd" and it doesn't + // work now that date values are range checked per #3579. logln("Using yyyy-ddd.hh:mm:ss"); delete formatter; formatter = NULL; formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status); - failure(status, "new SimpleDateFormat"); + if(failure(status, "new SimpleDateFormat")) return; pos.setIndex(0); myDate = formatter->parse( dateString, pos ); myString = fmt->format( myDate, myString ); @@ -368,15 +394,15 @@ void DateFormatRegressionTest::Test4061287(void) UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("dd/MM/yyyy"), status); - if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error: %s", u_errorName(status)); - delete df; - return; + if (U_FAILURE(status)) { + dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status)); + delete df; + return; } failure(status, "new SimpleDateFormat"); //try { logln(UnicodeString("") + df->parse("35/01/1971", status)); - failure(status, "df->parse"); + failure(status, "df->parse(\"35/01/1971\")"); //logln(df.parse("35/01/1971").toString()); //} /*catch (ParseException e) { @@ -417,7 +443,16 @@ void DateFormatRegressionTest::Test4065240(void) TimeZone::adoptDefault(TimeZone::createTimeZone("EST")); curDate = date(98, 0, 1); shortdate = DateFormat::createDateInstance(DateFormat::SHORT); + if (shortdate == NULL){ + dataerrln("Error calling DateFormat::createDateInstance"); + return; + } + fulldate = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG); + if (fulldate == NULL){ + dataerrln("Error calling DateFormat::createDateTimeInstance"); + return; + } strShortDate = "The current date (short form) is "; UnicodeString temp; temp = shortdate->format(curDate, temp); @@ -434,7 +469,7 @@ void DateFormatRegressionTest::Test4065240(void) // Check to see if the resource is present; if not, we can't test ResourceBundle *bundle = new ResourceBundle( - u_getDataDirectory(), *curLocale, status); + NULL, *curLocale, status); failure(status, "new ResourceBundle"); //(UnicodeString) "java.text.resources.DateFormatZoneData", curLocale); @@ -454,7 +489,6 @@ void DateFormatRegressionTest::Test4065240(void) failure(status, "Locale::setDefault"); TimeZone::setDefault(*saveZone); //} - delete shortdate; delete fulldate; delete saveZone; @@ -478,7 +512,14 @@ void DateFormatRegressionTest::Test4071441(void) { DateFormat *fmtA = DateFormat::createInstance(); DateFormat *fmtB = DateFormat::createInstance(); - + + if (fmtA == NULL || fmtB == NULL){ + dataerrln("Error calling DateFormat::createInstance"); + delete fmtA; + delete fmtB; + return; + } + // {sfb} Is it OK to cast away const here? Calendar *calA = (Calendar*) fmtA->getCalendar(); Calendar *calB = (Calendar*) fmtB->getCalendar(); @@ -517,7 +558,7 @@ void DateFormatRegressionTest::Test4071441(void) US locale a string formatted according to mm/dd/yy and parses it correctly. - When given a string mm/dd/yyyy it only parses up to the first + When given a string mm/dd/yyyy [sic] it only parses up to the first two y's, typically resulting in a date in the year 1919. Please extend the parsing method(s) to handle strings with @@ -530,9 +571,9 @@ void DateFormatRegressionTest::Test4073003(void) { //try { UErrorCode ec = U_ZERO_ERROR; - SimpleDateFormat fmt("dd/MM/yy", Locale::getUK(), ec); + SimpleDateFormat fmt("MM/dd/yy", Locale::getUK(), ec); if (U_FAILURE(ec)) { - errln("FAIL: SimpleDateFormat constructor"); + dataerrln("FAIL: SimpleDateFormat constructor - %s", u_errorName(ec)); return; } UnicodeString tests [] = { @@ -571,7 +612,7 @@ void DateFormatRegressionTest::Test4089106(void) UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *f = new SimpleDateFormat(status); if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); + dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete f; delete def; delete z; @@ -670,8 +711,8 @@ void DateFormatRegressionTest::Test4101483(void) { UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status); - failure(status, "new SimpleDateFormat"); - FieldPosition fp(DateFormat::TIMEZONE_FIELD); + if (failure(status, "new SimpleDateFormat", TRUE)) return; + FieldPosition fp(UDAT_TIMEZONE_FIELD); //Date d = date(9234567890L); UDate d = 9234567890.0; //StringBuffer buf = new StringBuffer(""); @@ -679,8 +720,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"); @@ -703,7 +744,7 @@ void DateFormatRegressionTest::Test4103340(void) // and some arbitrary time UDate d = date(97, 3, 1, 1, 1, 1); SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("MMMM"), Locale::getUS(), status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; UnicodeString s; s = dateToString(d, s); @@ -733,7 +774,7 @@ void DateFormatRegressionTest::Test4103341(void) UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *simple = new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status); if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); + dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete simple; return; } @@ -758,11 +799,11 @@ void DateFormatRegressionTest::Test4104136(void) UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *sdf = new SimpleDateFormat(status); if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); + dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete sdf; return; } - failure(status, "new SimpleDateFormat"); + if(failure(status, "new SimpleDateFormat")) return; UnicodeString pattern = "'time' hh:mm"; sdf->applyPattern(pattern); logln("pattern: \"" + pattern + "\""); @@ -802,7 +843,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); } @@ -823,7 +864,7 @@ void DateFormatRegressionTest::Test4104522(void) SimpleDateFormat *sdf = new SimpleDateFormat(status); if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); + dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete sdf; return; } @@ -866,7 +907,7 @@ void DateFormatRegressionTest::Test4106807(void) new SimpleDateFormat(UnicodeString("yyyyMMddHHmmss %"), status) }; if(U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); + dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status)); delete sdfs[0]; delete sdfs[1]; delete sdfs[2]; @@ -969,7 +1010,7 @@ void DateFormatRegressionTest::Test4134203(void) UErrorCode status = U_ZERO_ERROR; UnicodeString dateFormat = "MM/dd/yy HH:mm:ss zzz"; SimpleDateFormat *fmt = new SimpleDateFormat(dateFormat, status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; ParsePosition p0(0); UDate d = fmt->parse("01/22/92 04:52:00 GMT", p0); logln(dateToString(d)); @@ -991,7 +1032,7 @@ void DateFormatRegressionTest::Test4151631(void) logln("pattern=" + pattern); UErrorCode status = U_ZERO_ERROR; SimpleDateFormat *format = new SimpleDateFormat(pattern, Locale::getUS(), status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; UnicodeString result; FieldPosition pos(FieldPosition::DONT_CARE); result = format->format(date(1998-1900, UCAL_JUNE, 30, 13, 30, 0), result, pos); @@ -1015,7 +1056,7 @@ void DateFormatRegressionTest::Test4151706(void) UnicodeString dateString("Thursday, 31-Dec-98 23:00:00 GMT"); UErrorCode status = U_ZERO_ERROR; SimpleDateFormat fmt(UnicodeString("EEEE, dd-MMM-yy HH:mm:ss z"), Locale::getUS(), status); - failure(status, "new SimpleDateFormat"); + if (failure(status, "new SimpleDateFormat", TRUE)) return; //try { UDate d = fmt.parse(dateString, status); failure(status, "fmt->parse"); @@ -1042,8 +1083,10 @@ DateFormatRegressionTest::Test4162071(void) UnicodeString format("EEE', 'dd-MMM-yyyy HH:mm:ss z"); // RFC 822/1123 UErrorCode status = U_ZERO_ERROR; SimpleDateFormat df(format, Locale::getUS(), status); - if(U_FAILURE(status)) - errln("Couldn't create SimpleDateFormat"); + if(U_FAILURE(status)) { + dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); + return; + } //try { UDate x = df.parse(dateString, status); @@ -1067,7 +1110,7 @@ void DateFormatRegressionTest::Test4182066(void) { SimpleDateFormat fmt("MM/dd/yy", Locale::getUS(), status); SimpleDateFormat dispFmt("MMM dd yyyy GG", Locale::getUS(), status); if (U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat"); + dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); return; } @@ -1152,7 +1195,7 @@ DateFormatRegressionTest::Test4210209(void) { DateFormat& fmt = *(DateFormat*)&sfmt; // Yuck: See j25 DateFormat& disp = *(DateFormat*)&sdisp; // Yuck: See j25 if (U_FAILURE(status)) { - errln("Couldn't create SimpleDateFormat"); + dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); return; } Calendar* calx = (Calendar*)fmt.getCalendar(); // cast away const! @@ -1197,6 +1240,11 @@ void DateFormatRegressionTest::Test714(void) DateFormat *fmt = DateFormat::createDateTimeInstance(DateFormat::NONE, DateFormat::MEDIUM, Locale::getUS()); + if (fmt == NULL) { + dataerrln("Error calling DateFormat::createDateTimeInstance"); + return; + } + UnicodeString s; UnicodeString tests = (UnicodeString) "7:25:43 AM" ; @@ -1284,6 +1332,15 @@ void DateFormatRegressionTest::Test1684(void) UnicodeString pattern("yyyy MM WW EEE",""); Calendar *cal = new GregorianCalendar(status); SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status); + if (U_FAILURE(status)) { + dataerrln("Error constructing SimpleDateFormat"); + for(i=0;isetFirstDayOfWeek(UCAL_SUNDAY); cal->setMinimalDaysInFirstWeek(1); @@ -1368,7 +1425,7 @@ void DateFormatRegressionTest::Test1684(void) } else { logln(info); } - + for(i=0;iset(2007, 1, 14); + UDate date = cal->getTime(status); + if (U_FAILURE(status)) { + errln("Error getting time to format"); + return; + }; + sdf->adoptCalendar(cal); + UnicodeString result; + UnicodeString correct("-0330", ""); + sdf->format(date, result); + if (result != correct) { + errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result + "', expected '" + correct + "'"); + } + 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; +} + #endif /* #if !UCONFIG_NO_FORMATTING */ //eof