1 /***********************************************************************
3 * Copyright (c) 1997-2013, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/calendar.h"
14 #include "unicode/gregocal.h"
15 #include "unicode/datefmt.h"
16 #include "unicode/smpdtfmt.h"
21 void CalendarLimitTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
23 if (exec
) logln("TestSuite TestCalendarLimit");
25 // Re-enable this later
27 name
= "TestCalendarExtremeLimit";
29 logln("TestCalendarExtremeLimit---"); logln("");
30 TestCalendarExtremeLimit();
36 logln("TestLimits---"); logln("");
41 default: name
= ""; break;
46 // *****************************************************************************
47 // class CalendarLimitTest
48 // *****************************************************************************
50 // -------------------------------------
52 CalendarLimitTest::test(UDate millis
, icu::Calendar
* cal
, icu::DateFormat
* fmt
)
54 static const UDate kDrift
= 1e-10;
55 UErrorCode exception
= U_ZERO_ERROR
;
56 UnicodeString theDate
;
57 UErrorCode status
= U_ZERO_ERROR
;
58 cal
->setTime(millis
, exception
);
59 if (U_SUCCESS(exception
)) {
60 fmt
->format(millis
, theDate
);
61 UDate dt
= fmt
->parse(theDate
, status
);
62 // allow a small amount of error (drift)
63 if(! withinErr(dt
, millis
, kDrift
)) {
64 errln("FAIL:round trip for large milli, got: %.1lf wanted: %.1lf. (delta %.2lf greater than %.2lf)",
65 dt
, millis
, uprv_fabs(millis
-dt
), uprv_fabs(dt
*kDrift
));
66 logln(UnicodeString(" ") + theDate
+ " " + CalendarTest::calToStr(*cal
));
68 logln(UnicodeString("OK: got ") + dt
+ ", wanted " + millis
);
69 logln(UnicodeString(" ") + theDate
);
74 // -------------------------------------
76 // bug 986c: deprecate nextDouble/previousDouble
78 //|CalendarLimitTest::nextDouble(double a)
80 //| return uprv_nextDouble(a, TRUE);
84 //|CalendarLimitTest::previousDouble(double a)
86 //| return uprv_nextDouble(a, FALSE);
90 CalendarLimitTest::withinErr(double a
, double b
, double err
)
92 return ( uprv_fabs(a
- b
) < uprv_fabs(a
* err
) );
96 CalendarLimitTest::TestCalendarExtremeLimit()
98 UErrorCode status
= U_ZERO_ERROR
;
99 Calendar
*cal
= Calendar::createInstance(status
);
100 if (failure(status
, "Calendar::createInstance", TRUE
)) return;
101 cal
->adoptTimeZone(TimeZone::createTimeZone("GMT"));
102 DateFormat
*fmt
= DateFormat::createDateTimeInstance();
104 dataerrln("can't open cal and/or fmt");
107 fmt
->adoptCalendar(cal
);
108 ((SimpleDateFormat
*) fmt
)->applyPattern("HH:mm:ss.SSS Z, EEEE, MMMM d, yyyy G");
111 // This test used to test the algorithmic limits of the dates that
112 // GregorianCalendar could handle. However, the algorithm has
113 // been rewritten completely since then and the prior limits no
114 // longer apply. Instead, we now do basic round-trip testing of
115 // some extreme (but still manageable) dates.
117 logln("checking 1e16..1e17");
118 for ( m
= 1e16
; m
< 1e17
; m
*= 1.1) {
121 logln("checking -1e14..-1e15");
122 for ( m
= -1e14
; m
> -1e15
; m
*= 1.1) {
126 // This is 2^52 - 1, the largest allowable mantissa with a 0
127 // exponent in a 64-bit double
128 UDate VERY_EARLY_MILLIS
= - 4503599627370495.0;
129 UDate VERY_LATE_MILLIS
= 4503599627370495.0;
131 // I am removing the previousDouble and nextDouble calls below for
132 // two reasons: 1. As part of jitterbug 986, I am deprecating
133 // these methods and removing calls to them. 2. This test is a
134 // non-critical boundary behavior test.
135 test(VERY_EARLY_MILLIS
, cal
, fmt
);
136 //test(previousDouble(VERY_EARLY_MILLIS), cal, fmt);
137 test(VERY_LATE_MILLIS
, cal
, fmt
);
138 //test(nextDouble(VERY_LATE_MILLIS), cal, fmt);
143 CalendarLimitTest::TestLimits(void) {
144 static const UDate DEFAULT_START
= 944006400000.0; // 1999-12-01T00:00Z
145 static const int32_t DEFAULT_END
= -120; // Default for non-quick is run 2 minutes
147 static const struct {
150 UDate actualTestStart
;
151 int32_t actualTestEnd
;
153 {"gregorian", FALSE
, DEFAULT_START
, DEFAULT_END
},
154 {"japanese", FALSE
, 596937600000.0, DEFAULT_END
}, // 1988-12-01T00:00Z, Showa 63
155 {"buddhist", FALSE
, DEFAULT_START
, DEFAULT_END
},
156 {"roc", FALSE
, DEFAULT_START
, DEFAULT_END
},
157 {"persian", FALSE
, DEFAULT_START
, DEFAULT_END
},
158 {"islamic-civil", FALSE
, DEFAULT_START
, DEFAULT_END
},
159 {"islamic", FALSE
, DEFAULT_START
, 800000}, // Approx. 2250 years from now, after which some rounding errors occur in Islamic calendar
160 {"hebrew", TRUE
, DEFAULT_START
, DEFAULT_END
},
161 {"chinese", TRUE
, DEFAULT_START
, DEFAULT_END
},
162 {"dangi", TRUE
, DEFAULT_START
, DEFAULT_END
},
163 {"indian", FALSE
, DEFAULT_START
, DEFAULT_END
},
164 {"coptic", FALSE
, DEFAULT_START
, DEFAULT_END
},
165 {"ethiopic", FALSE
, DEFAULT_START
, DEFAULT_END
},
166 {"ethiopic-amete-alem", FALSE
, DEFAULT_START
, DEFAULT_END
},
173 for (i
= 0; TestCases
[i
].type
; i
++) {
174 UErrorCode status
= U_ZERO_ERROR
;
175 uprv_strcpy(buf
, "root@calendar=");
176 strcat(buf
, TestCases
[i
].type
);
177 Calendar
*cal
= Calendar::createInstance(buf
, status
);
178 if (failure(status
, "Calendar::createInstance", TRUE
)) {
181 if (uprv_strcmp(cal
->getType(), TestCases
[i
].type
) != 0) {
182 errln((UnicodeString
)"FAIL: Wrong calendar type: " + cal
->getType()
183 + " Requested: " + TestCases
[i
].type
);
188 doTheoreticalLimitsTest(*cal
, TestCases
[i
].hasLeapMonth
);
189 doLimitsTest(*cal
, TestCases
[i
].actualTestStart
,TestCases
[i
].actualTestEnd
);
195 CalendarLimitTest::doTheoreticalLimitsTest(Calendar
& cal
, UBool leapMonth
) {
196 const char* calType
= cal
.getType();
198 int32_t nDOW
= cal
.getMaximum(UCAL_DAY_OF_WEEK
);
199 int32_t maxDOY
= cal
.getMaximum(UCAL_DAY_OF_YEAR
);
200 int32_t lmaxDOW
= cal
.getLeastMaximum(UCAL_DAY_OF_YEAR
);
201 int32_t maxWOY
= cal
.getMaximum(UCAL_WEEK_OF_YEAR
);
202 int32_t lmaxWOY
= cal
.getLeastMaximum(UCAL_WEEK_OF_YEAR
);
203 int32_t maxM
= cal
.getMaximum(UCAL_MONTH
) + 1;
204 int32_t lmaxM
= cal
.getLeastMaximum(UCAL_MONTH
) + 1;
205 int32_t maxDOM
= cal
.getMaximum(UCAL_DAY_OF_MONTH
);
206 int32_t lmaxDOM
= cal
.getLeastMaximum(UCAL_DAY_OF_MONTH
);
207 int32_t maxDOWIM
= cal
.getMaximum(UCAL_DAY_OF_WEEK_IN_MONTH
);
208 int32_t lmaxDOWIM
= cal
.getLeastMaximum(UCAL_DAY_OF_WEEK_IN_MONTH
);
209 int32_t maxWOM
= cal
.getMaximum(UCAL_WEEK_OF_MONTH
);
210 int32_t lmaxWOM
= cal
.getLeastMaximum(UCAL_WEEK_OF_MONTH
);
211 int32_t minDaysInFirstWeek
= cal
.getMinimalDaysInFirstWeek();
216 expected
= maxM
*maxDOM
;
217 if (maxDOY
> expected
) {
218 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of DAY_OF_YEAR is too big: "
219 + maxDOY
+ "/expected: <=" + expected
);
221 expected
= lmaxM
*lmaxDOM
;
222 if (lmaxDOW
< expected
) {
223 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of DAY_OF_YEAR is too small: "
224 + lmaxDOW
+ "/expected: >=" + expected
);
229 expected
= maxDOY
/nDOW
+ 1;
230 if (maxWOY
> expected
) {
231 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of WEEK_OF_YEAR is too big: "
232 + maxWOY
+ "/expected: <=" + expected
);
234 expected
= lmaxDOW
/nDOW
;
235 if (lmaxWOY
< expected
) {
236 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of WEEK_OF_YEAR is too small: "
237 + lmaxWOY
+ "/expected >=" + expected
);
240 // Day of week in month
241 expected
= (maxDOM
+ nDOW
- 1)/nDOW
;
242 if (maxDOWIM
!= expected
) {
243 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of DAY_OF_WEEK_IN_MONTH is incorrect: "
244 + maxDOWIM
+ "/expected: " + expected
);
246 expected
= (lmaxDOM
+ nDOW
- 1)/nDOW
;
247 if (lmaxDOWIM
!= expected
) {
248 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of DAY_OF_WEEK_IN_MONTH is incorrect: "
249 + lmaxDOWIM
+ "/expected: " + expected
);
253 expected
= (maxDOM
+ (nDOW
- 1) + (nDOW
- minDaysInFirstWeek
)) / nDOW
;
254 if (maxWOM
!= expected
) {
255 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of WEEK_OF_MONTH is incorrect: "
256 + maxWOM
+ "/expected: " + expected
);
258 expected
= (lmaxDOM
+ (nDOW
- minDaysInFirstWeek
)) / nDOW
;
259 if (lmaxWOM
!= expected
) {
260 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of WEEK_OF_MONTH is incorrect: "
261 + lmaxWOM
+ "/expected: " + expected
);
266 CalendarLimitTest::doLimitsTest(Calendar
& cal
, UDate startDate
, int32_t endTime
) {
267 int32_t testTime
= quick
? ( endTime
/ 40 ) : endTime
;
268 doLimitsTest(cal
, NULL
/*default fields*/, startDate
, testTime
);
272 CalendarLimitTest::doLimitsTest(Calendar
& cal
,
273 const int32_t* fieldsToTest
,
275 int32_t testDuration
) {
276 static const int32_t FIELDS
[] = {
284 UCAL_DAY_OF_WEEK_IN_MONTH
,
290 static const char* FIELD_NAME
[] = {
291 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
292 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
293 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
294 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
295 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
296 "JULIAN_DAY", "MILLISECONDS_IN_DAY",
300 UErrorCode status
= U_ZERO_ERROR
;
304 GregorianCalendar
greg(status
);
305 if (failure(status
, "new GregorianCalendar")) {
308 greg
.setTime(startDate
, status
);
309 if (failure(status
, "GregorianCalendar::setTime")) {
312 logln((UnicodeString
)"Start: " + startDate
);
314 if (fieldsToTest
== NULL
) {
315 fieldsToTest
= FIELDS
;
319 // Keep a record of minima and maxima that we actually see.
320 // These are kept in an array of arrays of hashes.
321 int32_t limits
[UCAL_FIELD_COUNT
][4];
322 for (j
= 0; j
< UCAL_FIELD_COUNT
; j
++) {
323 limits
[j
][0] = INT32_MAX
;
324 limits
[j
][1] = INT32_MIN
;
325 limits
[j
][2] = INT32_MAX
;
326 limits
[j
][3] = INT32_MIN
;
329 // This test can run for a long time; show progress.
330 UDate millis
= ucal_getNow();
331 UDate mark
= millis
+ 5000; // 5 sec
332 millis
-= testDuration
* 1000; // stop time if testDuration<0
335 testDuration
> 0 ? i
< testDuration
336 : ucal_getNow() < millis
;
338 if (ucal_getNow() >= mark
) {
339 logln((UnicodeString
)"(" + i
+ " days)");
340 mark
+= 5000; // 5 sec
342 UDate testMillis
= greg
.getTime(status
);
343 cal
.setTime(testMillis
, status
);
344 cal
.setMinimalDaysInFirstWeek(1);
345 if (failure(status
, "Calendar set/getTime")) {
348 for (j
= 0; fieldsToTest
[j
] >= 0; ++j
) {
349 UCalendarDateFields f
= (UCalendarDateFields
)fieldsToTest
[j
];
350 int32_t v
= cal
.get(f
, status
);
351 int32_t minActual
= cal
.getActualMinimum(f
, status
);
352 int32_t maxActual
= cal
.getActualMaximum(f
, status
);
353 int32_t minLow
= cal
.getMinimum(f
);
354 int32_t minHigh
= cal
.getGreatestMinimum(f
);
355 int32_t maxLow
= cal
.getLeastMaximum(f
);
356 int32_t maxHigh
= cal
.getMaximum(f
);
358 if (limits
[j
][0] > minActual
) {
360 limits
[j
][0] = minActual
;
362 if (limits
[j
][1] < minActual
) {
363 // the greatest minimum
364 limits
[j
][1] = minActual
;
366 if (limits
[j
][2] > maxActual
) {
368 limits
[j
][2] = maxActual
;
370 if (limits
[j
][3] < maxActual
) {
372 limits
[j
][3] = maxActual
;
375 if (minActual
< minLow
|| minActual
> minHigh
) {
376 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
377 ymdToString(cal
, ymd
) +
378 " Range for min of " + FIELD_NAME
[f
] + "(" + f
+
379 ")=" + minLow
+ ".." + minHigh
+
380 ", actual_min=" + minActual
);
382 if (maxActual
< maxLow
|| maxActual
> maxHigh
) {
383 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
384 ymdToString(cal
, ymd
) +
385 " Range for max of " + FIELD_NAME
[f
] + "(" + f
+
386 ")=" + maxLow
+ ".." + maxHigh
+
387 ", actual_max=" + maxActual
);
389 if (v
< minActual
|| v
> maxActual
) {
390 // timebomb per #9967, fix with #9972
391 if ( isICUVersionBefore(52,0,2) && uprv_strcmp(cal
.getType(), "dangi") == 0 &&
392 testMillis
>= 1865635198000.0 ) { // Feb 2029 gregorian, end of dangi 4361
393 logln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
394 ymdToString(cal
, ymd
) +
395 " " + FIELD_NAME
[f
] + "(" + f
+ ")=" + v
+
396 ", actual=" + minActual
+ ".." + maxActual
+
397 ", allowed=(" + minLow
+ ".." + minHigh
+ ")..(" +
398 maxLow
+ ".." + maxHigh
+ ")");
400 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
401 ymdToString(cal
, ymd
) +
402 " " + FIELD_NAME
[f
] + "(" + f
+ ")=" + v
+
403 ", actual=" + minActual
+ ".." + maxActual
+
404 ", allowed=(" + minLow
+ ".." + minHigh
+ ")..(" +
405 maxLow
+ ".." + maxHigh
+ ")");
409 greg
.add(UCAL_DAY_OF_YEAR
, 1, status
);
410 if (failure(status
, "Calendar::add")) {
415 // Check actual maxima and minima seen against ranges returned
418 for (j
= 0; fieldsToTest
[j
] >= 0; ++j
) {
419 int32_t rangeLow
, rangeHigh
;
420 UBool fullRangeSeen
= TRUE
;
421 UCalendarDateFields f
= (UCalendarDateFields
)fieldsToTest
[j
];
424 buf
.append((UnicodeString
)"[" + cal
.getType() + "] " + FIELD_NAME
[f
]);
427 rangeLow
= cal
.getMinimum(f
);
428 rangeHigh
= cal
.getGreatestMinimum(f
);
429 if (limits
[j
][0] != rangeLow
|| limits
[j
][1] != rangeHigh
) {
430 fullRangeSeen
= FALSE
;
432 buf
.append((UnicodeString
)" minima range=" + rangeLow
+ ".." + rangeHigh
);
433 buf
.append((UnicodeString
)" minima actual=" + limits
[j
][0] + ".." + limits
[j
][1]);
436 rangeLow
= cal
.getLeastMaximum(f
);
437 rangeHigh
= cal
.getMaximum(f
);
438 if (limits
[j
][2] != rangeLow
|| limits
[j
][3] != rangeHigh
) {
439 fullRangeSeen
= FALSE
;
441 buf
.append((UnicodeString
)" maxima range=" + rangeLow
+ ".." + rangeHigh
);
442 buf
.append((UnicodeString
)" maxima actual=" + limits
[j
][2] + ".." + limits
[j
][3]);
445 logln((UnicodeString
)"OK: " + buf
);
447 // This may or may not be an error -- if the range of dates
448 // we scan over doesn't happen to contain a minimum or
449 // maximum, it doesn't mean some other range won't.
450 logln((UnicodeString
)"Warning: " + buf
);
454 logln((UnicodeString
)"End: " + greg
.getTime(status
));
458 CalendarLimitTest::ymdToString(const Calendar
& cal
, UnicodeString
& str
) {
459 UErrorCode status
= U_ZERO_ERROR
;
461 str
.append((UnicodeString
)"" + cal
.get(UCAL_EXTENDED_YEAR
, status
)
462 + "/" + (cal
.get(UCAL_MONTH
, status
) + 1)
463 + (cal
.get(UCAL_IS_LEAP_MONTH
, status
) == 1 ? "(leap)" : "")
464 + "/" + cal
.get(UCAL_DATE
, status
)
465 + " " + cal
.get(UCAL_HOUR_OF_DAY
, status
)
466 + ":" + cal
.get(UCAL_MINUTE
, status
)
467 + " zone(hrs) " + cal
.get(UCAL_ZONE_OFFSET
, status
)/(60.0*60.0*1000.0)
468 + " dst(hrs) " + cal
.get(UCAL_DST_OFFSET
, status
)/(60.0*60.0*1000.0)
469 + ", time(millis)=" + cal
.getTime(status
));
473 #endif /* #if !UCONFIG_NO_FORMATTING */