1 /***********************************************************************
3 * Copyright (c) 1997-2011, 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 {"indian", FALSE
, DEFAULT_START
, DEFAULT_END
},
163 {"coptic", FALSE
, DEFAULT_START
, DEFAULT_END
},
164 {"ethiopic", FALSE
, DEFAULT_START
, DEFAULT_END
},
165 {"ethiopic-amete-alem", FALSE
, DEFAULT_START
, DEFAULT_END
},
172 for (i
= 0; TestCases
[i
].type
; i
++) {
173 UErrorCode status
= U_ZERO_ERROR
;
174 uprv_strcpy(buf
, "root@calendar=");
175 strcat(buf
, TestCases
[i
].type
);
176 Calendar
*cal
= Calendar::createInstance(buf
, status
);
177 if (failure(status
, "Calendar::createInstance", TRUE
)) {
180 if (uprv_strcmp(cal
->getType(), TestCases
[i
].type
) != 0) {
181 errln((UnicodeString
)"FAIL: Wrong calendar type: " + cal
->getType()
182 + " Requested: " + TestCases
[i
].type
);
187 doTheoreticalLimitsTest(*cal
, TestCases
[i
].hasLeapMonth
);
188 doLimitsTest(*cal
, TestCases
[i
].actualTestStart
,TestCases
[i
].actualTestEnd
);
194 CalendarLimitTest::doTheoreticalLimitsTest(Calendar
& cal
, UBool leapMonth
) {
195 const char* calType
= cal
.getType();
197 int32_t nDOW
= cal
.getMaximum(UCAL_DAY_OF_WEEK
);
198 int32_t maxDOY
= cal
.getMaximum(UCAL_DAY_OF_YEAR
);
199 int32_t lmaxDOW
= cal
.getLeastMaximum(UCAL_DAY_OF_YEAR
);
200 int32_t maxWOY
= cal
.getMaximum(UCAL_WEEK_OF_YEAR
);
201 int32_t lmaxWOY
= cal
.getLeastMaximum(UCAL_WEEK_OF_YEAR
);
202 int32_t maxM
= cal
.getMaximum(UCAL_MONTH
) + 1;
203 int32_t lmaxM
= cal
.getLeastMaximum(UCAL_MONTH
) + 1;
204 int32_t maxDOM
= cal
.getMaximum(UCAL_DAY_OF_MONTH
);
205 int32_t lmaxDOM
= cal
.getLeastMaximum(UCAL_DAY_OF_MONTH
);
206 int32_t maxDOWIM
= cal
.getMaximum(UCAL_DAY_OF_WEEK_IN_MONTH
);
207 int32_t lmaxDOWIM
= cal
.getLeastMaximum(UCAL_DAY_OF_WEEK_IN_MONTH
);
208 int32_t maxWOM
= cal
.getMaximum(UCAL_WEEK_OF_MONTH
);
209 int32_t lmaxWOM
= cal
.getLeastMaximum(UCAL_WEEK_OF_MONTH
);
210 int32_t minDaysInFirstWeek
= cal
.getMinimalDaysInFirstWeek();
215 expected
= maxM
*maxDOM
;
216 if (maxDOY
> expected
) {
217 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of DAY_OF_YEAR is too big: "
218 + maxDOY
+ "/expected: <=" + expected
);
220 expected
= lmaxM
*lmaxDOM
;
221 if (lmaxDOW
< expected
) {
222 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of DAY_OF_YEAR is too small: "
223 + lmaxDOW
+ "/expected: >=" + expected
);
228 expected
= maxDOY
/nDOW
+ 1;
229 if (maxWOY
> expected
) {
230 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of WEEK_OF_YEAR is too big: "
231 + maxWOY
+ "/expected: <=" + expected
);
233 expected
= lmaxDOW
/nDOW
;
234 if (lmaxWOY
< expected
) {
235 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of WEEK_OF_YEAR is too small: "
236 + lmaxWOY
+ "/expected >=" + expected
);
239 // Day of week in month
240 expected
= (maxDOM
+ nDOW
- 1)/nDOW
;
241 if (maxDOWIM
!= expected
) {
242 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of DAY_OF_WEEK_IN_MONTH is incorrect: "
243 + maxDOWIM
+ "/expected: " + expected
);
245 expected
= (lmaxDOM
+ nDOW
- 1)/nDOW
;
246 if (lmaxDOWIM
!= expected
) {
247 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of DAY_OF_WEEK_IN_MONTH is incorrect: "
248 + lmaxDOWIM
+ "/expected: " + expected
);
252 expected
= (maxDOM
+ (nDOW
- 1) + (nDOW
- minDaysInFirstWeek
)) / nDOW
;
253 if (maxWOM
!= expected
) {
254 errln((UnicodeString
)"FAIL: [" + calType
+ "] Maximum value of WEEK_OF_MONTH is incorrect: "
255 + maxWOM
+ "/expected: " + expected
);
257 expected
= (lmaxDOM
+ (nDOW
- minDaysInFirstWeek
)) / nDOW
;
258 if (lmaxWOM
!= expected
) {
259 errln((UnicodeString
)"FAIL: [" + calType
+ "] Least maximum value of WEEK_OF_MONTH is incorrect: "
260 + lmaxWOM
+ "/expected: " + expected
);
265 CalendarLimitTest::doLimitsTest(Calendar
& cal
, UDate startDate
, int32_t endTime
) {
266 int32_t testTime
= quick
? ( endTime
/ 40 ) : endTime
;
267 doLimitsTest(cal
, NULL
/*default fields*/, startDate
, testTime
);
271 CalendarLimitTest::doLimitsTest(Calendar
& cal
,
272 const int32_t* fieldsToTest
,
274 int32_t testDuration
) {
275 static const int32_t FIELDS
[] = {
283 UCAL_DAY_OF_WEEK_IN_MONTH
,
289 static const char* FIELD_NAME
[] = {
290 "ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
291 "DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
292 "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
293 "MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
294 "DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
295 "JULIAN_DAY", "MILLISECONDS_IN_DAY",
299 UErrorCode status
= U_ZERO_ERROR
;
303 GregorianCalendar
greg(status
);
304 if (failure(status
, "new GregorianCalendar")) {
307 greg
.setTime(startDate
, status
);
308 if (failure(status
, "GregorianCalendar::setTime")) {
311 logln((UnicodeString
)"Start: " + startDate
);
313 if (fieldsToTest
== NULL
) {
314 fieldsToTest
= FIELDS
;
318 // Keep a record of minima and maxima that we actually see.
319 // These are kept in an array of arrays of hashes.
320 int32_t limits
[UCAL_FIELD_COUNT
][4];
321 for (j
= 0; j
< UCAL_FIELD_COUNT
; j
++) {
322 limits
[j
][0] = INT32_MAX
;
323 limits
[j
][1] = INT32_MIN
;
324 limits
[j
][2] = INT32_MAX
;
325 limits
[j
][3] = INT32_MIN
;
328 // This test can run for a long time; show progress.
329 UDate millis
= ucal_getNow();
330 UDate mark
= millis
+ 5000; // 5 sec
331 millis
-= testDuration
* 1000; // stop time if testDuration<0
334 testDuration
> 0 ? i
< testDuration
335 : ucal_getNow() < millis
;
337 if (ucal_getNow() >= mark
) {
338 logln((UnicodeString
)"(" + i
+ " days)");
339 mark
+= 5000; // 5 sec
341 cal
.setTime(greg
.getTime(status
), status
);
342 cal
.setMinimalDaysInFirstWeek(1);
343 if (failure(status
, "Calendar set/getTime")) {
346 for (j
= 0; fieldsToTest
[j
] >= 0; ++j
) {
347 UCalendarDateFields f
= (UCalendarDateFields
)fieldsToTest
[j
];
348 int32_t v
= cal
.get(f
, status
);
349 int32_t minActual
= cal
.getActualMinimum(f
, status
);
350 int32_t maxActual
= cal
.getActualMaximum(f
, status
);
351 int32_t minLow
= cal
.getMinimum(f
);
352 int32_t minHigh
= cal
.getGreatestMinimum(f
);
353 int32_t maxLow
= cal
.getLeastMaximum(f
);
354 int32_t maxHigh
= cal
.getMaximum(f
);
356 if (limits
[j
][0] > minActual
) {
358 limits
[j
][0] = minActual
;
360 if (limits
[j
][1] < minActual
) {
361 // the greatest minimum
362 limits
[j
][1] = minActual
;
364 if (limits
[j
][2] > maxActual
) {
366 limits
[j
][2] = maxActual
;
368 if (limits
[j
][3] < maxActual
) {
370 limits
[j
][3] = maxActual
;
373 if (minActual
< minLow
|| minActual
> minHigh
) {
374 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
375 ymdToString(cal
, ymd
) +
376 " Range for min of " + FIELD_NAME
[f
] + "(" + f
+
377 ")=" + minLow
+ ".." + minHigh
+
378 ", actual_min=" + minActual
);
380 if (maxActual
< maxLow
|| maxActual
> maxHigh
) {
381 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
382 ymdToString(cal
, ymd
) +
383 " Range for max of " + FIELD_NAME
[f
] + "(" + f
+
384 ")=" + maxLow
+ ".." + maxHigh
+
385 ", actual_max=" + maxActual
);
387 if (v
< minActual
|| v
> maxActual
) {
388 errln((UnicodeString
)"Fail: [" + cal
.getType() + "] " +
389 ymdToString(cal
, ymd
) +
390 " " + FIELD_NAME
[f
] + "(" + f
+ ")=" + v
+
391 ", actual range=" + minActual
+ ".." + maxActual
+
392 ", allowed=(" + minLow
+ ".." + minHigh
+ ")..(" +
393 maxLow
+ ".." + maxHigh
+ ")");
396 greg
.add(UCAL_DAY_OF_YEAR
, 1, status
);
397 if (failure(status
, "Calendar::add")) {
402 // Check actual maxima and minima seen against ranges returned
405 for (j
= 0; fieldsToTest
[j
] >= 0; ++j
) {
406 int32_t rangeLow
, rangeHigh
;
407 UBool fullRangeSeen
= TRUE
;
408 UCalendarDateFields f
= (UCalendarDateFields
)fieldsToTest
[j
];
411 buf
.append((UnicodeString
)"[" + cal
.getType() + "] " + FIELD_NAME
[f
]);
414 rangeLow
= cal
.getMinimum(f
);
415 rangeHigh
= cal
.getGreatestMinimum(f
);
416 if (limits
[j
][0] != rangeLow
|| limits
[j
][1] != rangeHigh
) {
417 fullRangeSeen
= FALSE
;
419 buf
.append((UnicodeString
)" minima range=" + rangeLow
+ ".." + rangeHigh
);
420 buf
.append((UnicodeString
)" minima actual=" + limits
[j
][0] + ".." + limits
[j
][1]);
423 rangeLow
= cal
.getLeastMaximum(f
);
424 rangeHigh
= cal
.getMaximum(f
);
425 if (limits
[j
][2] != rangeLow
|| limits
[j
][3] != rangeHigh
) {
426 fullRangeSeen
= FALSE
;
428 buf
.append((UnicodeString
)" maxima range=" + rangeLow
+ ".." + rangeHigh
);
429 buf
.append((UnicodeString
)" maxima actual=" + limits
[j
][2] + ".." + limits
[j
][3]);
432 logln((UnicodeString
)"OK: " + buf
);
434 // This may or may not be an error -- if the range of dates
435 // we scan over doesn't happen to contain a minimum or
436 // maximum, it doesn't mean some other range won't.
437 logln((UnicodeString
)"Warning: " + buf
);
441 logln((UnicodeString
)"End: " + greg
.getTime(status
));
445 CalendarLimitTest::ymdToString(const Calendar
& cal
, UnicodeString
& str
) {
446 UErrorCode status
= U_ZERO_ERROR
;
448 str
.append((UnicodeString
)"" + cal
.get(UCAL_EXTENDED_YEAR
, status
)
449 + "/" + (cal
.get(UCAL_MONTH
, status
) + 1)
450 + (cal
.get(UCAL_IS_LEAP_MONTH
, status
) == 1 ? "(leap)" : "")
451 + "/" + cal
.get(UCAL_DATE
, status
)
452 + ", time=" + cal
.getTime(status
));
456 #endif /* #if !UCONFIG_NO_FORMATTING */