CASE(52,TestPersianCalOverflow);
CASE(53,TestIslamicCalOverflow);
CASE(54,TestWeekOfYear13548);
+ CASE(55,Test13745);
default: name = ""; break;
}
}
return;
}
failure(status, "new GregorianCalendar");
- GregorianCalendar *cal2 = (GregorianCalendar*) cal1->clone() ;
+ GregorianCalendar *cal2 = cal1->clone() ;
printdate(cal1, "cal1: ") ;
printdate(cal2, "cal2 - cloned(): ") ;
void CalendarRegressionTest::test4125881()
{
UErrorCode status = U_ZERO_ERROR;
- GregorianCalendar *cal = (GregorianCalendar*) Calendar::createInstance(status);
+ LocalPointer<GregorianCalendar> cal((GregorianCalendar*) Calendar::createInstance(status), status);
if(U_FAILURE(status)) {
- dataerrln("Error creating calendar %s", u_errorName(status));
- delete cal;
- return;
+ dataerrln("Error creating calendar %s", u_errorName(status));
+ return;
}
- DateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"),status);
+ SimpleDateFormat fmt(UnicodeString("MMMM d, yyyy G"),status);
if(U_FAILURE(status)) {
- dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
- delete cal;
- return;
+ dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
+ return;
}
cal->clear();
for (int32_t y=-20; y<=10; ++y) {
cal->set(UCAL_ERA, y < 1 ? GregorianCalendar::BC : GregorianCalendar::AD);
cal->set(UCAL_YEAR, y < 1 ? 1 - y : y);
UnicodeString temp;
- logln(UnicodeString("") + y + UnicodeString(" = ") + fmt->format(cal->getTime(status), temp) + " " +
+ logln(UnicodeString("") + y + UnicodeString(" = ") + fmt.format(cal->getTime(status), temp) + " " +
cal->isLeapYear(y));
if (cal->isLeapYear(y) != ((y+40)%4 == 0))
errln("Leap years broken");
}
-
- delete cal;
- delete fmt;
}
/**
*/
void CalendarRegressionTest::test4125892() {
UErrorCode status = U_ZERO_ERROR;
- GregorianCalendar *cal = (GregorianCalendar*) Calendar::createInstance(status);
+ LocalPointer<GregorianCalendar> cal((GregorianCalendar*) Calendar::createInstance(status), status);
if(U_FAILURE(status)) {
- dataerrln("Error creating calendar %s", u_errorName(status));
- delete cal;
- return;
+ dataerrln("Error creating calendar %s", u_errorName(status));
+ return;
}
- DateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"),status);
+ SimpleDateFormat fmt(UnicodeString("MMMM d, yyyy G"),status);
if(U_FAILURE(status)) {
- dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
- delete cal;
- return;
+ dataerrln("Error creating SimpleDateFormat - %s", u_errorName(status));
+ return;
}
cal->clear();
cal->set(UCAL_ERA, GregorianCalendar::BC);
!cal->isLeapYear(-80)) // -80 == 81 BC
errln("Calendar not proleptic");
- delete cal;
- delete fmt;
}
/**
delete cal;
return;
}
- GregorianCalendar *cal2 = (GregorianCalendar*)cal->clone();
+ GregorianCalendar *cal2 = cal->clone();
UDate cut = cal->getGregorianChange();
UDate cut2 = cut + 100*24*60*60*1000.0; // 100 days later
if (*cal != *cal2) {
delete cal2;
}
+const UDate MILLIS_IN_DAY = 86400000.0;
+/**
+ * ICU-13745
+ * GregorianCalendar::setGregorianChange() overflow
+ */
+void CalendarRegressionTest::Test13745()
+{
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar *cal = new GregorianCalendar(status);
+ if(U_FAILURE(status)) {
+ dataerrln("Error creating calendar %s", u_errorName(status));
+ delete cal;
+ return;
+ }
+
+ // this line would overflow before fix 13745
+ cal->setGregorianChange(((double)INT32_MAX+1.0) * MILLIS_IN_DAY, status);
+ if(U_FAILURE(status)) {
+ errln("%s:%d Failure setting INT32_MAX+1 change on calendar: %s\n", __FILE__, __LINE__, u_errorName(status));
+ return;
+ }
+ assertEquals("getGregorianChange()", (double)INT32_MAX * MILLIS_IN_DAY, cal->getGregorianChange());
+
+ // test underflow
+ cal->setGregorianChange(((double)INT32_MIN-1.0) * MILLIS_IN_DAY, status);
+ if(U_FAILURE(status)) {
+ errln("%s:%d Failure setting INT32_MAX-1 change on calendar: %s\n", __FILE__, __LINE__, u_errorName(status));
+ return;
+ }
+ assertEquals("getGregorianChange()", (double)INT32_MIN * MILLIS_IN_DAY, cal->getGregorianChange());
+
+ delete cal;
+}
+
+
/**
* @bug 4142933
* Bug states that ArrayIndexOutOfBoundsException is thrown by GregorianCalendar::roll()
{
UErrorCode status = U_ZERO_ERROR;
UCalendarDateFields field = UCAL_YEAR;
- DateFormat *format = new SimpleDateFormat(UnicodeString("EEE MMM dd HH:mm:ss zzz yyyy G"),
- Locale::getUS(), status);
+ LocalPointer<DateFormat> format (new SimpleDateFormat(UnicodeString("EEE MMM dd HH:mm:ss zzz yyyy G"),
+ Locale::getUS(), status));
if(U_FAILURE(status)) {
dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status));
return;
}
- GregorianCalendar *calendars [] = {
- new GregorianCalendar(100, UCAL_NOVEMBER, 1, status),
- new GregorianCalendar(-99 /*100BC*/, UCAL_JANUARY, 1, status),
- new GregorianCalendar(1996, UCAL_FEBRUARY, 29, status),
+ GregorianCalendar calendars [] = {
+ {100, UCAL_NOVEMBER, 1, status},
+ {-99 /*100BC*/, UCAL_JANUARY, 1, status},
+ {1996, UCAL_FEBRUARY, 29, status}
};
if(U_FAILURE(status)) {
errln("Couldn't create GregorianCalendars");
return;
- // could leak
}
UnicodeString id [] = { "Hybrid", "Gregorian", "Julian" };
logln("--- " + id[k] + " ---");
for (int32_t j=0; j < 3; ++j) {
- GregorianCalendar *calendar = calendars[j];
+ GregorianCalendar *calendar = &calendars[j];
if (k == 1) {
calendar->setGregorianChange(EARLIEST_SUPPORTED_MILLIS, status);
}
if(U_FAILURE(status))
errln("setGregorianChange() failed");
- format->adoptCalendar((Calendar*)calendar->clone());
+ format->adoptCalendar(calendar->clone());
UDate dateBefore = calendar->getTime(status);
if(U_FAILURE(status))
}
}
}
-
- delete format;
- delete calendars[0];
- delete calendars[1];
- delete calendars[2];
}
/**
1999, UCAL_JUNE, 4, 1964, UCAL_SEPTEMBER, 7,
};
int32_t DATA_length = UPRV_LENGTHOF(DATA);
- Calendar* pcal = Calendar::createInstance(Locale::getUS(), ec);
+ LocalPointer<Calendar> pcal(Calendar::createInstance(Locale::getUS(), ec));
if(U_FAILURE(ec)) {
- dataerrln("Error creating calendar %s", u_errorName(ec));
- delete pcal;
- return;
+ dataerrln("Error creating calendar %s", u_errorName(ec));
+ return;
}
Calendar& cal = *pcal;
int32_t i;
SimpleDateFormat fmt(UnicodeString("MMM dd yyyy",""), ec);
+ if (U_FAILURE(ec)) {
+ dataerrln("Error creating calendar %s", u_errorName(ec));
+ return;
+ }
fmt.setCalendar(cal);
UnicodeString s, t, u;
if (U_SUCCESS(ec)) {
break;
{
- Calendar *cal2 = cal.clone();
+ LocalPointer<Calendar> cal2(cal.clone());
UErrorCode ec2 = U_ZERO_ERROR;
cal2->setTime(date1, ec2);
(dy2 != dy)){
errln("fieldDifference(UCAL_...) and fieldDifference(Calendar::...) give different results!\n");
}
- delete cal2;
}
} else {
dataerrln("Error creating SimpleDateFormat - %s", u_errorName(ec));
}
- delete pcal;
}
void CalendarRegressionTest::TestT5555()