2004-11-01 18:56:24 +00:00
|
|
|
/***********************************************************************
|
2000-01-15 02:00:06 +00:00
|
|
|
* COPYRIGHT:
|
2006-07-21 22:01:55 +00:00
|
|
|
* Copyright (c) 1997-2006, International Business Machines Corporation
|
2004-11-01 18:56:24 +00:00
|
|
|
* and others. All Rights Reserved.
|
|
|
|
***********************************************************************/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "callimts.h"
|
2003-10-26 10:20:40 +00:00
|
|
|
#include "caltest.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/calendar.h"
|
|
|
|
#include "unicode/gregocal.h"
|
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
2004-10-18 02:43:33 +00:00
|
|
|
#include "putilimp.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_USE
|
2000-08-15 18:25:20 +00:00
|
|
|
void CalendarLimitTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (exec) logln("TestSuite TestCalendarLimit");
|
|
|
|
switch (index) {
|
|
|
|
// Re-enable this later
|
|
|
|
case 0:
|
|
|
|
name = "TestCalendarLimit";
|
|
|
|
if (exec) {
|
|
|
|
logln("TestCalendarLimit---"); logln("");
|
|
|
|
TestCalendarLimit();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class CalendarLimitTest
|
|
|
|
// *****************************************************************************
|
2000-08-10 00:28:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
void
|
2001-10-08 23:26:58 +00:00
|
|
|
CalendarLimitTest::test(UDate millis, U_NAMESPACE_QUALIFIER Calendar* cal, U_NAMESPACE_QUALIFIER DateFormat* fmt)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-10-26 10:20:40 +00:00
|
|
|
static const UDate kDrift = 1e-10;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode exception = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString theDate;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
cal->setTime(millis, exception);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_SUCCESS(exception)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
fmt->format(millis, theDate);
|
|
|
|
UDate dt = fmt->parse(theDate, status);
|
|
|
|
// allow a small amount of error (drift)
|
2003-10-26 10:20:40 +00:00
|
|
|
if(! withinErr(dt, millis, kDrift)) {
|
|
|
|
errln("FAIL:round trip for large milli, got: %.1lf wanted: %.1lf. (delta %.2lf greater than %.2lf)",
|
|
|
|
dt, millis, uprv_fabs(millis-dt), uprv_fabs(dt*kDrift));
|
|
|
|
logln(UnicodeString(" ") + theDate + " " + CalendarTest::calToStr(*cal));
|
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
logln(UnicodeString("OK: got ") + dt + ", wanted " + millis);
|
|
|
|
logln(UnicodeString(" ") + theDate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2001-06-11 17:32:50 +00:00
|
|
|
// bug 986c: deprecate nextDouble/previousDouble
|
|
|
|
//|double
|
|
|
|
//|CalendarLimitTest::nextDouble(double a)
|
|
|
|
//|{
|
|
|
|
//| return uprv_nextDouble(a, TRUE);
|
|
|
|
//|}
|
|
|
|
//|
|
|
|
|
//|double
|
|
|
|
//|CalendarLimitTest::previousDouble(double a)
|
|
|
|
//|{
|
|
|
|
//| return uprv_nextDouble(a, FALSE);
|
|
|
|
//|}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
CalendarLimitTest::withinErr(double a, double b, double err)
|
|
|
|
{
|
1999-12-28 23:57:50 +00:00
|
|
|
return ( uprv_fabs(a - b) < uprv_fabs(a * err) );
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CalendarLimitTest::TestCalendarLimit()
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
Calendar *cal = Calendar::createInstance(status);
|
|
|
|
if (failure(status, "Calendar::createInstance")) return;
|
2001-03-14 21:06:53 +00:00
|
|
|
cal->adoptTimeZone(TimeZone::createTimeZone("GMT"));
|
1999-08-16 21:50:52 +00:00
|
|
|
DateFormat *fmt = DateFormat::createDateTimeInstance();
|
2006-07-21 22:01:55 +00:00
|
|
|
if(!fmt || !cal) {
|
|
|
|
dataerrln("can't open cal and/or fmt");
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
fmt->adoptCalendar(cal);
|
|
|
|
((SimpleDateFormat*) fmt)->applyPattern("HH:mm:ss.SSS zzz, EEEE, MMMM d, yyyy G");
|
|
|
|
|
2003-10-26 10:20:40 +00:00
|
|
|
|
2001-03-14 21:06:53 +00:00
|
|
|
// This test used to test the algorithmic limits of the dates that
|
|
|
|
// GregorianCalendar could handle. However, the algorithm has
|
|
|
|
// been rewritten completely since then and the prior limits no
|
|
|
|
// longer apply. Instead, we now do basic round-trip testing of
|
|
|
|
// some extreme (but still manageable) dates.
|
1999-08-16 21:50:52 +00:00
|
|
|
UDate m;
|
2003-10-26 10:20:40 +00:00
|
|
|
logln("checking 1e16..1e17");
|
|
|
|
for ( m = 1e16; m < 1e17; m *= 1.1) {
|
1999-08-16 21:50:52 +00:00
|
|
|
test(m, cal, fmt);
|
|
|
|
}
|
2003-10-26 10:20:40 +00:00
|
|
|
logln("checking -1e14..-1e15");
|
1999-08-16 21:50:52 +00:00
|
|
|
for ( m = -1e14; m > -1e15; m *= 1.1) {
|
|
|
|
test(m, cal, fmt);
|
|
|
|
}
|
|
|
|
|
2001-03-14 21:06:53 +00:00
|
|
|
// This is 2^52 - 1, the largest allowable mantissa with a 0
|
|
|
|
// exponent in a 64-bit double
|
|
|
|
UDate VERY_EARLY_MILLIS = - 4503599627370495.0;
|
|
|
|
UDate VERY_LATE_MILLIS = 4503599627370495.0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-06-11 17:32:50 +00:00
|
|
|
// I am removing the previousDouble and nextDouble calls below for
|
|
|
|
// two reasons: 1. As part of jitterbug 986, I am deprecating
|
|
|
|
// these methods and removing calls to them. 2. This test is a
|
|
|
|
// non-critical boundary behavior test.
|
2001-03-14 21:06:53 +00:00
|
|
|
test(VERY_EARLY_MILLIS, cal, fmt);
|
2001-06-11 17:32:50 +00:00
|
|
|
//test(previousDouble(VERY_EARLY_MILLIS), cal, fmt);
|
2001-03-14 21:06:53 +00:00
|
|
|
test(VERY_LATE_MILLIS, cal, fmt);
|
2001-06-11 17:32:50 +00:00
|
|
|
//test(nextDouble(VERY_LATE_MILLIS), cal, fmt);
|
2001-03-14 21:06:53 +00:00
|
|
|
delete fmt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// eof
|