2004-11-01 18:56:24 +00:00
|
|
|
/***********************************************************************
|
2009-06-12 19:34:21 +00:00
|
|
|
* Copyright (c) 1997-2009, 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
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
2002-09-21 00:43:14 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
2001-10-08 23:26:58 +00:00
|
|
|
#include "tsdate.h"
|
2004-10-18 02:43:33 +00:00
|
|
|
#include "putilimp.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-12-04 20:24:37 +00:00
|
|
|
#include <float.h>
|
2000-08-24 01:12:12 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-21 22:16:30 +00:00
|
|
|
#include <math.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const double IntlTestDateFormat::ONEYEAR = 365.25 * ONEDAY; // Approximate
|
|
|
|
|
2003-08-29 15:57:38 +00:00
|
|
|
IntlTestDateFormat::~IntlTestDateFormat() {}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
|
|
|
|
* DateFormat.
|
|
|
|
*/
|
|
|
|
// par is ignored throughout this file
|
2000-08-23 19:11:16 +00:00
|
|
|
void IntlTestDateFormat::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (exec) logln("TestSuite DateFormat");
|
|
|
|
switch (index) {
|
2002-04-09 03:20:50 +00:00
|
|
|
case 0: name = "GenericTest";
|
1999-08-16 21:50:52 +00:00
|
|
|
if (exec) {
|
|
|
|
logln(name);
|
|
|
|
fFormat = DateFormat::createInstance();
|
|
|
|
fTestName = "createInstance";
|
|
|
|
fLimit = 3;
|
2000-08-23 19:11:16 +00:00
|
|
|
testFormat(/* par */);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-04-09 03:20:50 +00:00
|
|
|
case 1: name = "DefaultLocale";
|
1999-08-16 21:50:52 +00:00
|
|
|
if (exec) {
|
|
|
|
logln(name);
|
2000-08-23 19:11:16 +00:00
|
|
|
testLocale(/*par, */Locale::getDefault(), "Default Locale");
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-04-09 03:20:50 +00:00
|
|
|
case 2: name = "TestAvailableLocales";
|
1999-08-16 21:50:52 +00:00
|
|
|
if (exec) {
|
|
|
|
logln(name);
|
2000-08-23 19:11:16 +00:00
|
|
|
testAvailableLocales(/* par */);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-04-09 03:20:50 +00:00
|
|
|
case 3: name = "MonsterTest";
|
1999-08-16 21:50:52 +00:00
|
|
|
if (exec) {
|
|
|
|
logln(name);
|
2000-08-23 19:11:16 +00:00
|
|
|
monsterTest(/*par*/);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-23 19:11:16 +00:00
|
|
|
IntlTestDateFormat::testLocale(/*char* par, */const Locale& locale, const UnicodeString& localeName)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
DateFormat::EStyle timeStyle, dateStyle;
|
|
|
|
|
|
|
|
// For patterns including only time information and a timezone, it may take
|
|
|
|
// up to three iterations, since the timezone may shift as the year number
|
|
|
|
// is determined. For other patterns, 2 iterations should suffice.
|
|
|
|
fLimit = 3;
|
|
|
|
|
|
|
|
for(timeStyle = (DateFormat::EStyle)0;
|
|
|
|
timeStyle < (DateFormat::EStyle)4;
|
|
|
|
timeStyle = (DateFormat::EStyle) (timeStyle+1))
|
|
|
|
{
|
|
|
|
fTestName = (UnicodeString) "Time test " + (int32_t) timeStyle + " (" + localeName + ")";
|
|
|
|
fFormat = DateFormat::createTimeInstance(timeStyle, locale);
|
2000-08-23 19:11:16 +00:00
|
|
|
testFormat(/* par */);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fLimit = 2;
|
|
|
|
|
|
|
|
for(dateStyle = (DateFormat::EStyle)0;
|
|
|
|
dateStyle < (DateFormat::EStyle)4;
|
|
|
|
dateStyle = (DateFormat::EStyle) (dateStyle+1))
|
|
|
|
{
|
|
|
|
fTestName = (UnicodeString) "Date test " + (int32_t) dateStyle + " (" + localeName + ")";
|
|
|
|
fFormat = DateFormat::createDateInstance(dateStyle, locale);
|
2000-08-23 19:11:16 +00:00
|
|
|
testFormat(/* par */);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(dateStyle = (DateFormat::EStyle)0;
|
|
|
|
dateStyle < (DateFormat::EStyle)4;
|
|
|
|
dateStyle = (DateFormat::EStyle) (dateStyle+1))
|
|
|
|
{
|
|
|
|
for(timeStyle = (DateFormat::EStyle)0;
|
|
|
|
timeStyle < (DateFormat::EStyle)4;
|
|
|
|
timeStyle = (DateFormat::EStyle) (timeStyle+1))
|
|
|
|
{
|
|
|
|
fTestName = (UnicodeString) "DateTime test " + (int32_t) dateStyle + "/" + (int32_t) timeStyle + " (" + localeName + ")";
|
|
|
|
fFormat = DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale);
|
2000-08-23 19:11:16 +00:00
|
|
|
testFormat(/* par */);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void IntlTestDateFormat::testFormat(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (fFormat == 0)
|
|
|
|
{
|
2009-06-12 19:34:21 +00:00
|
|
|
dataerrln("FAIL: DateFormat creation failed");
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-12-19 01:07:02 +00:00
|
|
|
describeTest();
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UDate now = Calendar::getNow();
|
|
|
|
tryDate(0);
|
|
|
|
tryDate(1278161801778.0);
|
2000-12-19 01:07:02 +00:00
|
|
|
tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
|
2001-12-04 20:24:37 +00:00
|
|
|
tryDate(9516987689250.0); // In the year 2271
|
1999-08-16 21:50:52 +00:00
|
|
|
tryDate(now);
|
|
|
|
// Shift 6 months into the future, AT THE SAME TIME OF DAY.
|
|
|
|
// This will test the DST handling.
|
|
|
|
tryDate(now + 6.0*30*ONEDAY);
|
|
|
|
|
|
|
|
UDate limit = now * 10; // Arbitrary limit
|
2000-12-18 19:02:30 +00:00
|
|
|
for (int32_t i=0; i<3; ++i)
|
|
|
|
tryDate(uprv_floor(randDouble() * limit));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
delete fFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IntlTestDateFormat::describeTest()
|
|
|
|
{
|
|
|
|
// Assume it's a SimpleDateFormat and get some info
|
|
|
|
SimpleDateFormat *s = (SimpleDateFormat*)fFormat;
|
|
|
|
UnicodeString str;
|
|
|
|
logln(fTestName + " Pattern " + s->toPattern(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IntlTestDateFormat::tryDate(UDate theDate)
|
|
|
|
{
|
|
|
|
const int32_t DEPTH = 10;
|
|
|
|
UDate date[DEPTH];
|
|
|
|
UnicodeString string[DEPTH];
|
|
|
|
|
|
|
|
int32_t dateMatch = 0;
|
|
|
|
int32_t stringMatch = 0;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool dump = FALSE;
|
2003-10-26 10:20:40 +00:00
|
|
|
#if defined (U_CAL_DEBUG)
|
|
|
|
dump = TRUE;
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t i;
|
2000-12-18 19:02:30 +00:00
|
|
|
|
|
|
|
date[0] = theDate;
|
|
|
|
fFormat->format(theDate, string[0]);
|
|
|
|
|
|
|
|
for (i=1; i<DEPTH; ++i)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2000-12-18 19:02:30 +00:00
|
|
|
date[i] = fFormat->parse(string[i-1], status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if (U_FAILURE(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
describeTest();
|
2002-04-09 03:20:50 +00:00
|
|
|
errln("**** FAIL: Parse of " + prettify(string[i-1], FALSE) + " failed.");
|
1999-08-16 21:50:52 +00:00
|
|
|
dump = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fFormat->format(date[i], string[i]);
|
2000-12-18 19:02:30 +00:00
|
|
|
if (dateMatch == 0 && date[i] == date[i-1])
|
|
|
|
dateMatch = i;
|
|
|
|
else if (dateMatch > 0 && date[i] != date[i-1])
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-12-18 19:02:30 +00:00
|
|
|
describeTest();
|
|
|
|
errln("**** FAIL: Date mismatch after match for " + string[i]);
|
|
|
|
dump = TRUE;
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-12-18 19:02:30 +00:00
|
|
|
if (stringMatch == 0 && string[i] == string[i-1])
|
|
|
|
stringMatch = i;
|
|
|
|
else if (stringMatch > 0 && string[i] != string[i-1])
|
|
|
|
{
|
|
|
|
describeTest();
|
|
|
|
errln("**** FAIL: String mismatch after match for " + string[i]);
|
|
|
|
dump = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dateMatch > 0 && stringMatch > 0)
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-12-18 19:02:30 +00:00
|
|
|
if (i == DEPTH)
|
|
|
|
--i;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (stringMatch > fLimit || dateMatch > fLimit)
|
|
|
|
{
|
|
|
|
describeTest();
|
2000-12-18 19:02:30 +00:00
|
|
|
errln((UnicodeString)"**** FAIL: No string and/or date match within " + fLimit
|
2000-12-19 01:07:02 +00:00
|
|
|
+ " iterations for the Date " + string[0] + "\t(" + theDate + ").");
|
1999-08-16 21:50:52 +00:00
|
|
|
dump = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dump)
|
|
|
|
{
|
|
|
|
for (int32_t k=0; k<=i; ++k)
|
|
|
|
{
|
|
|
|
logln((UnicodeString)"" + k + ": " + date[k] + " F> " +
|
|
|
|
string[k] + " P> ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-21 22:16:30 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Return a random double from 0.01 to 1, inclusive
|
|
|
|
double IntlTestDateFormat::randDouble()
|
|
|
|
{
|
|
|
|
// Assume 8-bit (or larger) rand values. Also assume
|
|
|
|
// that the system rand() function is very poor, which it always is.
|
2000-08-14 17:31:04 +00:00
|
|
|
double d=0.0;
|
2000-08-24 01:12:12 +00:00
|
|
|
uint32_t i;
|
2000-08-14 21:42:36 +00:00
|
|
|
char* poke = (char*)&d;
|
1999-08-16 21:50:52 +00:00
|
|
|
do {
|
2001-12-04 20:24:37 +00:00
|
|
|
do {
|
|
|
|
for (i=0; i < sizeof(double); ++i)
|
|
|
|
{
|
|
|
|
poke[i] = (char)(rand() & 0xFF);
|
|
|
|
}
|
|
|
|
} while (uprv_isNaN(d) || uprv_isInfinite(d));
|
|
|
|
|
|
|
|
if (d < 0.0)
|
|
|
|
d = -d;
|
|
|
|
if (d > 0.0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2006-07-24 18:02:38 +00:00
|
|
|
double e = uprv_floor(log10(d));
|
2001-12-04 20:24:37 +00:00
|
|
|
if (e < -2.0)
|
|
|
|
d *= uprv_pow10((int32_t)(-e-2));
|
|
|
|
else if (e > -1.0)
|
|
|
|
d /= uprv_pow10((int32_t)(e+1));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-12-04 20:24:37 +00:00
|
|
|
// While this is not a real normalized number make another one.
|
|
|
|
} while (uprv_isNaN(d) || uprv_isInfinite(d)
|
2001-12-05 17:37:41 +00:00
|
|
|
|| !((-DBL_MAX < d && d < DBL_MAX) || (d < -DBL_MIN && DBL_MIN < d)));
|
1999-08-16 21:50:52 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void IntlTestDateFormat::testAvailableLocales(/* char* par */)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
int32_t count = 0;
|
|
|
|
const Locale* locales = DateFormat::getAvailableLocales(count);
|
|
|
|
logln((UnicodeString)"" + count + " available locales");
|
|
|
|
if (locales && count)
|
|
|
|
{
|
|
|
|
UnicodeString name;
|
|
|
|
UnicodeString all;
|
|
|
|
for (int32_t i=0; i<count; ++i)
|
|
|
|
{
|
|
|
|
if (i!=0) all += ", ";
|
2000-04-15 21:28:17 +00:00
|
|
|
all += locales[i].getName();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
logln(all);
|
|
|
|
}
|
2009-06-12 19:34:21 +00:00
|
|
|
else dataerrln((UnicodeString)"**** FAIL: Zero available locales or null array pointer");
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-08-23 19:11:16 +00:00
|
|
|
void IntlTestDateFormat::monsterTest(/*char *par*/)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
int32_t count;
|
|
|
|
const Locale* locales = DateFormat::getAvailableLocales(count);
|
|
|
|
if (locales && count)
|
|
|
|
{
|
2000-12-19 01:07:02 +00:00
|
|
|
if (quick && count > 3) {
|
|
|
|
logln("quick test: testing just 3 locales!");
|
|
|
|
count = 3;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
for (int32_t i=0; i<count; ++i)
|
|
|
|
{
|
2000-04-15 21:28:17 +00:00
|
|
|
UnicodeString name = UnicodeString(locales[i].getName(), "");
|
1999-08-16 21:50:52 +00:00
|
|
|
logln((UnicodeString)"Testing " + name + "...");
|
2000-08-23 19:11:16 +00:00
|
|
|
testLocale(/*par, */locales[i], name);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-21 00:43:14 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|