ICU-3659 add test and fix for udat_format failing with extreme dates (circa 10^27)

X-SVN-Rev: 15463
This commit is contained in:
Alan Liu 2004-05-21 20:25:52 +00:00
parent 75afcba84d
commit 390b02ec5b
4 changed files with 145 additions and 9 deletions

View File

@ -15,6 +15,7 @@
#include "unicode/ucal.h"
#include "uresimp.h"
#include "cstring.h"
#include "uassert.h"
int32_t Math::floorDivide(int32_t numerator, int32_t denominator) {
return (numerator >= 0) ?
@ -29,6 +30,40 @@ int32_t Math::floorDivide(double numerator, int32_t denominator,
return (int32_t) quotient;
}
double Math::floorDivide(double dividend, double divisor,
double& remainder) {
// Only designed to work for positive divisors
U_ASSERT(divisor > 0);
double quotient = floorDivide(dividend, divisor);
remainder = dividend - (quotient * divisor);
// N.B. For certain large dividends, on certain platforms, there
// is a bug such that the quotient is off by one. If you doubt
// this to be true, set a breakpoint below and run cintltst.
if (remainder < 0 || remainder >= divisor) {
// E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my
// machine (too high by one). 4.1792057231752762e+024 /
// 86400000.0 is wrong the other way (too low).
double q = quotient;
quotient += (remainder < 0) ? -1 : +1;
if (q == quotient) {
// For quotients > ~2^53, we won't be able to add or
// subtract one, since the LSB of the mantissa will be >
// 2^0; that is, the exponent (base 2) will be larger than
// the length, in bits, of the mantissa. In that case, we
// can't give a correct answer, so we set the remainder to
// zero. This has the desired effect of making extreme
// values give back an approximate answer rather than
// crashing. For example, UDate values above a ~10^25
// might all have a time of midnight.
remainder = 0;
} else {
remainder = dividend - (quotient * divisor);
}
}
U_ASSERT(0 <= remainder && remainder < divisor);
return quotient;
}
const int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian
const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian

View File

@ -63,6 +63,19 @@ class U_I18N_API Math {
*/
static int32_t floorDivide(double numerator, int32_t denominator,
int32_t& remainder);
/**
* For a positive divisor, return the quotient and remainder
* such that dividend = quotient*divisor + remainder and
* 0 <= remainder < divisor.
*
* Works around edge-case bugs. Handles pathological input
* (divident >> divisor) reasonably.
*
* Calling with a divisor <= 0 is disallowed.
*/
static double floorDivide(double dividend, double divisor,
double& remainder);
};
// Useful millisecond constants

View File

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (c) 2003, International Business Machines
* Copyright (c) 2003-2004, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
@ -345,18 +345,17 @@ void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff,
// and finalZone == 0. For this case we add "&& finalZone != 0".
if (date >= finalMillis && finalZone != 0) {
int32_t year, month, dom, dow;
double days = uprv_floor(date / U_MILLIS_PER_DAY);
double millis;
double days = Math::floorDivide(date, U_MILLIS_PER_DAY, millis);
Grego::dayToFields(days, year, month, dom, dow);
int32_t millis = (int32_t) (date - days * U_MILLIS_PER_DAY);
rawoff = finalZone->getRawOffset();
if (!local) {
// Adjust from GMT to local
date += rawoff;
double days2 = uprv_floor(date / U_MILLIS_PER_DAY);
millis = (int32_t) (date - days2 * U_MILLIS_PER_DAY);
double days2 = Math::floorDivide(date, U_MILLIS_PER_DAY, millis);
if (days2 != days) {
Grego::dayToFields(days2, year, month, dom, dow);
}
@ -364,7 +363,7 @@ void OlsonTimeZone::getOffset(UDate date, UBool local, int32_t& rawoff,
dstoff = finalZone->getOffset(
GregorianCalendar::AD, year, month,
dom, (uint8_t) dow, millis, ec) - rawoff;
dom, (uint8_t) dow, (int32_t) millis, ec) - rawoff;
return;
}

View File

@ -29,14 +29,22 @@
#include "cformtst.h"
#include "cmemory.h"
#include <math.h>
static void TestExtremeDates(void);
#define LEN(a) (sizeof(a)/sizeof(a[0]))
void addDateForTest(TestNode** root);
#define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x)
void addDateForTest(TestNode** root)
{
addTest(root, &TestDateFormat, "tsformat/cdattst/TestDateFormat");
addTest(root, &TestSymbols, "tsformat/cdattst/TestSymbols");
addTest(root, &TestDateFormatCalendar, "tsformat/cdattst/TestDateFormatCalendar");
TESTCASE(TestDateFormat);
TESTCASE(TestSymbols);
TESTCASE(TestDateFormatCalendar);
TESTCASE(TestExtremeDates);
}
/* Testing the DateFormat API */
static void TestDateFormat()
@ -802,4 +810,85 @@ static UChar* myNumformat(const UNumberFormat* numfor, double d)
return result2;
}
/**
* The search depth for TestExtremeDates. The total number of
* dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1.
*/
#define EXTREME_DATES_DEPTH 8
/**
* Support for TestExtremeDates (below).
*
* Test a single date to see whether udat_format handles it properly.
*/
static UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date,
UChar* buf, int32_t buflen, char* cbuf,
UErrorCode* ec) {
int32_t len = udat_format(fmt, date, buf, buflen, 0, ec);
if (!assertSuccess("udat_format", ec)) return FALSE;
u_austrncpy(cbuf, buf, buflen);
if (len < 4) {
log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf);
} else {
log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf);
}
return TRUE;
}
/**
* Support for TestExtremeDates (below).
*
* Recursively test between 'small' and 'large', up to the depth
* limit specified by EXTREME_DATES_DEPTH.
*/
static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large,
UChar* buf, int32_t buflen, char* cbuf,
int32_t count,
UErrorCode* ec) {
/* Logarithmic midpoint; see below */
UDate mid = (UDate) exp((log(small) + log(large)) / 2);
if (count == EXTREME_DATES_DEPTH) {
return TRUE;
}
return
_aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) &&
_aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) &&
_aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec);
}
/**
* http://www.jtcsv.com/cgibin/icu-bugs?findid=3659
*
* For certain large dates, udat_format crashes on MacOS. This test
* attempts to reproduce this problem by doing a recursive logarithmic*
* binary search of a predefined interval (from 'small' to 'large').
*
* The limit of the search is given by EXTREME_DATES_DEPTH, above.
*
* *The search has to be logarithmic, not linear. A linear search of the
* range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and
* 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5
* and 10^22.5, etc.
*/
static void TestExtremeDates() {
UDateFormat *fmt;
UErrorCode ec;
UChar buf[256];
char cbuf[256];
const double small = 1000; /* 1 sec */
const double large = 1e+30; /* well beyond usable UDate range */
/* There is no need to test larger values from 1e+30 to 1e+300;
the failures occur around 1e+27, and never above 1e+30. */
ec = U_ZERO_ERROR;
fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US",
0, 0, 0, 0, &ec);
if (!assertSuccess("udat_open", &ec)) return;
_aux2ExtremeDates(fmt, small, large, buf, LEN(buf), cbuf, 0, &ec);
udat_close(fmt);
}
#endif /* #if !UCONFIG_NO_FORMATTING */