+void CalendarRegressionTest::TestT8057(void) {
+ // Set the calendar to the last day in a leap year
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar *cal = (GregorianCalendar*)Calendar::createInstance(status);
+ if(U_FAILURE(status)) {
+ errln("Error creating Calendar: %s", u_errorName(status));
+ delete cal;
+ return;
+ }
+ cal->setLenient(FALSE);
+ cal->clear();
+ cal->set(2008, UCAL_DECEMBER, 31);
+
+ // Force calculating then fields once.
+ UDate t = cal->getTime(status);
+ if(U_FAILURE(status)) {
+ errln("Error while calculating the date");
+ delete cal;
+ return;
+ }
+
+ UDate expected = 1262246400000.0; // 2009-12-31 00:00 PST
+
+ cal->add(UCAL_YEAR, 1, status);
+ t = cal->getTime(status);
+ if (U_SUCCESS(status)) {
+ if (t != expected) {
+ dataerrln((UnicodeString)"FAIL: wrong date after add: expected=" + expected + " returned=" + t);
+ }
+ } else {
+ errln("FAIL: error while adding one year");
+ }
+
+ delete cal;
+}
+
+// Test case for ticket#8596.
+// Setting an year followed by getActualMaximum(Calendar.WEEK_OF_YEAR)
+// may result wrong maximum week.
+void CalendarRegressionTest::TestT8596(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar *gc = new GregorianCalendar(*TimeZone::getGMT(), status);
+
+ if (U_FAILURE(status)) {
+ dataerrln("Error creating Calendar: %s", u_errorName(status));
+ delete gc;
+ return;
+ }
+
+ gc->setFirstDayOfWeek(UCAL_MONDAY);
+ gc->setMinimalDaysInFirstWeek(4);
+
+ // Force the calender to resolve the fields once.
+ // The maximum week number in 2011 is 52.
+ gc->set(UCAL_YEAR, 2011);
+ gc->get(UCAL_YEAR, status);
+
+ // Set a date in year 2009, but not calling get to resolve
+ // the calendar's internal field yet.
+ gc->set(2009, UCAL_JULY, 1);
+
+ // Then call getActuamMaximum for week of year.
+ // #8596 was caused by conflict between year set
+ // above and internal work calendar field resolution.
+ int32_t maxWeeks = gc->getActualMaximum(UCAL_WEEK_OF_YEAR, status);
+
+ if (U_FAILURE(status)) {
+ errln("Error calendar calculation: %s", u_errorName(status));
+ delete gc;
+ return;
+ }
+
+ if (maxWeeks != 53) {
+ errln((UnicodeString)"FAIL: Max week in 2009 in ISO calendar is 53, but got " + maxWeeks);
+ }
+
+ delete gc;
+}
+
+// Test case for ticket 9452
+// Calendar addition fall onto the missing date - 2011-12-30 in Samoa
+void CalendarRegressionTest::TestT9452(void) {
+ UErrorCode status = U_ZERO_ERROR;
+ GregorianCalendar cal(TimeZone::createTimeZone("Pacific/Apia"), status);
+ failure(status, "initializing GregorianCalendar");
+
+ SimpleDateFormat sdf(UnicodeString("y-MM-dd'T'HH:mm:ssZZZZZ"), status);
+ failure(status, "initializing SimpleDateFormat");
+ sdf.setCalendar(cal);
+
+ UnicodeString dstr;
+
+ // Set date to 2011-12-29 00:00
+ cal.clear();
+ cal.set(2011, UCAL_DECEMBER, 29, 0, 0, 0);
+
+ UDate d = cal.getTime(status);
+ if (!failure(status, "getTime for initial date")) {
+ sdf.format(d, dstr);
+ logln(UnicodeString("Initial date: ") + dstr);
+
+ // Add 1 day
+ cal.add(UCAL_DATE, 1, status);
+ failure(status, "add 1 day");
+ d = cal.getTime(status);
+ failure(status, "getTime after +1 day");
+ dstr.remove();
+ sdf.format(d, dstr);
+ logln(UnicodeString("+1 day: ") + dstr);
+ assertEquals("Add 1 day", UnicodeString("2011-12-31T00:00:00+14:00"), dstr);
+
+ // Subtract 1 day
+ cal.add(UCAL_DATE, -1, status);
+ failure(status, "subtract 1 day");
+ d = cal.getTime(status);
+ failure(status, "getTime after -1 day");
+ dstr.remove();
+ sdf.format(d, dstr);
+ logln(UnicodeString("-1 day: ") + dstr);
+ assertEquals("Subtract 1 day", UnicodeString("2011-12-29T00:00:00-10:00"), dstr);
+ }
+}
+
+/**
+ * @bug ticket 13454
+ */
+void CalendarRegressionTest::TestPersianCalOverflow(void) {
+ const char* localeID = "bs_Cyrl@calendar=persian";
+ UErrorCode status = U_ZERO_ERROR;
+ Calendar* cal = Calendar::createInstance(Locale(localeID), status);
+ if(U_FAILURE(status)) {
+ dataerrln("FAIL: Calendar::createInstance for localeID %s: %s", localeID, u_errorName(status));
+ } else {
+ int32_t maxMonth = cal->getMaximum(UCAL_MONTH);
+ int32_t maxDayOfMonth = cal->getMaximum(UCAL_DATE);
+ int32_t jd, month, dayOfMonth;
+ for (jd = 67023580; jd <= 67023584; jd++) { // year 178171, int32_t overflow if jd >= 67023582
+ status = U_ZERO_ERROR;
+ cal->clear();
+ cal->set(UCAL_JULIAN_DAY, jd);
+ month = cal->get(UCAL_MONTH, status);
+ dayOfMonth = cal->get(UCAL_DATE, status);
+ if ( U_FAILURE(status) ) {
+ errln("FAIL: Calendar->get MONTH/DATE for localeID %s, julianDay %d, status %s", localeID, jd, u_errorName(status));
+ } else if (month > maxMonth || dayOfMonth > maxDayOfMonth) {
+ errln("FAIL: localeID %s, julianDay %d; maxMonth %d, got month %d; maxDayOfMonth %d, got dayOfMonth %d",
+ localeID, jd, maxMonth, month, maxDayOfMonth, dayOfMonth);
+ }
+ }
+ delete cal;
+ }
+}
+
+/**
+ * @bug tickets 12661, 13538
+ */
+void CalendarRegressionTest::TestIslamicCalOverflow(void) {
+ const char* localeID = "ar@calendar=islamic-civil";
+ UErrorCode status = U_ZERO_ERROR;
+ Calendar* cal = Calendar::createInstance(Locale(localeID), status);
+ if(U_FAILURE(status)) {
+ dataerrln("FAIL: Calendar::createInstance for localeID %s: %s", localeID, u_errorName(status));
+ } else {
+ int32_t maxMonth = cal->getMaximum(UCAL_MONTH);
+ int32_t maxDayOfMonth = cal->getMaximum(UCAL_DATE);
+ int32_t jd, year, month, dayOfMonth;
+ for (jd = 73530872; jd <= 73530876; jd++) { // year 202002, int32_t overflow if jd >= 73530874
+ status = U_ZERO_ERROR;
+ cal->clear();
+ cal->set(UCAL_JULIAN_DAY, jd);
+ year = cal->get(UCAL_YEAR, status);
+ month = cal->get(UCAL_MONTH, status);
+ dayOfMonth = cal->get(UCAL_DATE, status);
+ if ( U_FAILURE(status) ) {
+ errln("FAIL: Calendar->get YEAR/MONTH/DATE for localeID %s, julianDay %d, status %s", localeID, jd, u_errorName(status));
+ } else if (month > maxMonth || dayOfMonth > maxDayOfMonth) {
+ errln("FAIL: localeID %s, julianDay %d; got year %d; maxMonth %d, got month %d; maxDayOfMonth %d, got dayOfMonth %d",
+ localeID, jd, year, maxMonth, month, maxDayOfMonth, dayOfMonth);
+ }
+ }
+ delete cal;
+ }
+}