2004-11-01 18:56:24 +00:00
|
|
|
/***********************************************************************
|
2000-01-15 02:00:06 +00:00
|
|
|
* COPYRIGHT:
|
2004-11-01 18:56:24 +00:00
|
|
|
* Copyright (c) 1997-2004, International Business Machines Corporation
|
|
|
|
* 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"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/datefmt.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
|
|
|
#include "unicode/gregocal.h"
|
2001-10-08 23:26:58 +00:00
|
|
|
#include "dtfmtrtts.h"
|
2003-12-04 23:16:57 +00:00
|
|
|
#include "caltest.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-08-21 19:09:33 +00:00
|
|
|
#include <stdio.h>
|
2003-05-30 00:52:43 +00:00
|
|
|
#include <string.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class DateFormatRoundTripTest
|
|
|
|
// *****************************************************************************
|
|
|
|
|
|
|
|
// Useful for turning up subtle bugs: Change the following to TRUE, recompile,
|
|
|
|
// and run while at lunch.
|
2001-03-07 20:56:15 +00:00
|
|
|
// Warning -- makes test run infinite loop!!!
|
2003-05-30 00:52:43 +00:00
|
|
|
#ifndef INFINITE
|
2001-03-07 20:56:15 +00:00
|
|
|
#define INFINITE 0
|
2003-05-30 00:52:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Define this to test just a single locale
|
|
|
|
//#define TEST_ONE_LOC "ja_JP_TRADITIONAL"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// If SPARSENESS is > 0, we don't run each exhaustive possibility.
|
|
|
|
// There are 24 total possible tests per each locale. A SPARSENESS
|
|
|
|
// of 12 means we run half of them. A SPARSENESS of 23 means we run
|
|
|
|
// 1 of them. SPARSENESS _must_ be in the range 0..23.
|
2002-04-11 02:44:53 +00:00
|
|
|
int32_t DateFormatRoundTripTest::SPARSENESS = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t DateFormatRoundTripTest::TRIALS = 4;
|
|
|
|
int32_t DateFormatRoundTripTest::DEPTH = 5;
|
|
|
|
|
2003-08-12 19:22:32 +00:00
|
|
|
DateFormatRoundTripTest::DateFormatRoundTripTest() : dateFormat(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
DateFormatRoundTripTest::~DateFormatRoundTripTest() {
|
|
|
|
delete dateFormat;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
|
|
|
|
|
|
|
|
void
|
2003-08-12 19:22:32 +00:00
|
|
|
DateFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-12 19:22:32 +00:00
|
|
|
optionv = (par && *par=='v');
|
1999-08-16 21:50:52 +00:00
|
|
|
switch (index) {
|
|
|
|
CASE(0,TestDateFormatRoundTrip)
|
|
|
|
default: name = ""; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
DateFormatRoundTripTest::failure(UErrorCode status, const char* msg)
|
|
|
|
{
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-12-08 23:31:17 +00:00
|
|
|
errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
|
1999-08-16 21:50:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-05-30 00:52:43 +00:00
|
|
|
UBool
|
|
|
|
DateFormatRoundTripTest::failure(UErrorCode status, const char* msg, const UnicodeString& str)
|
|
|
|
{
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
UnicodeString escaped;
|
|
|
|
escape(str,escaped);
|
|
|
|
errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status) + ", str=" + escaped);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// ==
|
|
|
|
|
|
|
|
void DateFormatRoundTripTest::TestDateFormatRoundTrip()
|
|
|
|
{
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
getFieldCal = Calendar::createInstance(status);
|
|
|
|
failure(status, "Calendar::createInstance");
|
|
|
|
|
|
|
|
|
|
|
|
int32_t locCount = 0;
|
|
|
|
const Locale *avail = DateFormat::getAvailableLocales(locCount);
|
2001-05-26 01:23:28 +00:00
|
|
|
logln("DateFormat available locales: %d", locCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
if(quick) {
|
2002-04-11 02:44:53 +00:00
|
|
|
SPARSENESS = 18;
|
|
|
|
logln("Quick mode: only testing SPARSENESS = 18");
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
TimeZone *tz = TimeZone::createDefault();
|
|
|
|
UnicodeString temp;
|
|
|
|
logln("Default TimeZone: " + tz->getID(temp));
|
|
|
|
delete tz;
|
|
|
|
|
2003-05-30 00:52:43 +00:00
|
|
|
#ifdef TEST_ONE_LOC // define this to just test ONE locale.
|
|
|
|
Locale loc(TEST_ONE_LOC);
|
|
|
|
test(loc);
|
2001-03-07 20:56:15 +00:00
|
|
|
#if INFINITE
|
2003-05-30 00:52:43 +00:00
|
|
|
for(;;) {
|
|
|
|
test(loc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
# if INFINITE
|
2001-03-07 20:56:15 +00:00
|
|
|
// Special infinite loop test mode for finding hard to reproduce errors
|
|
|
|
Locale loc = Locale::getDefault();
|
|
|
|
logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName(temp));
|
|
|
|
for(;;)
|
|
|
|
test(loc);
|
2003-05-30 00:52:43 +00:00
|
|
|
# else
|
2001-03-07 20:56:15 +00:00
|
|
|
test(Locale::getDefault());
|
|
|
|
|
2003-12-09 20:52:52 +00:00
|
|
|
#if 1
|
|
|
|
// installed locales
|
2001-03-07 20:56:15 +00:00
|
|
|
for (int i=0; i < locCount; ++i) {
|
|
|
|
test(avail[i]);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-12-09 20:52:52 +00:00
|
|
|
#endif
|
2003-12-04 23:16:57 +00:00
|
|
|
|
2003-12-09 20:52:52 +00:00
|
|
|
#if 1
|
|
|
|
// special locales
|
2003-12-04 23:16:57 +00:00
|
|
|
int32_t jCount = CalendarTest::testLocaleCount();
|
|
|
|
for (int32_t j=0; j < jCount; ++j) {
|
|
|
|
test(Locale(CalendarTest::testLocaleID(j)));
|
|
|
|
}
|
2003-12-09 20:52:52 +00:00
|
|
|
#endif
|
|
|
|
|
2003-05-30 00:52:43 +00:00
|
|
|
# endif
|
2001-03-07 20:56:15 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
delete getFieldCal;
|
|
|
|
}
|
|
|
|
|
2000-10-12 19:35:01 +00:00
|
|
|
static const char *styleName(DateFormat::EStyle s)
|
|
|
|
{
|
2000-11-23 00:14:39 +00:00
|
|
|
switch(s)
|
2000-10-12 19:35:01 +00:00
|
|
|
{
|
|
|
|
case DateFormat::SHORT: return "SHORT";
|
|
|
|
case DateFormat::MEDIUM: return "MEDIUM";
|
|
|
|
case DateFormat::LONG: return "LONG";
|
|
|
|
case DateFormat::FULL: return "FULL";
|
|
|
|
// case DateFormat::DEFAULT: return "DEFAULT";
|
|
|
|
case DateFormat::DATE_OFFSET: return "DATE_OFFSET";
|
|
|
|
case DateFormat::NONE: return "NONE";
|
|
|
|
case DateFormat::DATE_TIME: return "DATE_TIME";
|
2000-11-23 00:14:39 +00:00
|
|
|
default: return "Unknown";
|
2000-10-12 19:35:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void DateFormatRoundTripTest::test(const Locale& loc)
|
|
|
|
{
|
|
|
|
UnicodeString temp;
|
2001-03-07 20:56:15 +00:00
|
|
|
#if !INFINITE
|
|
|
|
logln("Locale: " + loc.getDisplayName(temp));
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Total possibilities = 24
|
|
|
|
// 4 date
|
|
|
|
// 4 time
|
|
|
|
// 16 date-time
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool TEST_TABLE [24];//= new boolean[24];
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t i = 0;
|
|
|
|
for(i = 0; i < 24; ++i)
|
|
|
|
TEST_TABLE[i] = TRUE;
|
|
|
|
|
|
|
|
// If we have some sparseness, implement it here. Sparseness decreases
|
|
|
|
// test time by eliminating some tests, up to 23.
|
|
|
|
for(i = 0; i < SPARSENESS; ) {
|
|
|
|
int random = (int)(randFraction() * 24);
|
|
|
|
if (random >= 0 && random < 24 && TEST_TABLE[i]) {
|
|
|
|
TEST_TABLE[i] = FALSE;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t itable = 0;
|
|
|
|
int32_t style = 0;
|
|
|
|
for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
|
|
|
|
if(TEST_TABLE[itable++]) {
|
2002-04-11 02:44:53 +00:00
|
|
|
logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style)));
|
1999-08-16 21:50:52 +00:00
|
|
|
DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc);
|
2003-05-16 21:58:37 +00:00
|
|
|
if(df == NULL) {
|
|
|
|
errln(UnicodeString("Could not DF::createDateInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp));
|
|
|
|
} else {
|
|
|
|
test(df, loc);
|
|
|
|
delete df;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
|
|
|
|
if (TEST_TABLE[itable++]) {
|
2000-10-12 19:35:01 +00:00
|
|
|
logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style)));
|
1999-08-16 21:50:52 +00:00
|
|
|
DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc);
|
2003-05-16 21:58:37 +00:00
|
|
|
if(df == NULL) {
|
|
|
|
errln(UnicodeString("Could not DF::createTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp));
|
|
|
|
} else {
|
|
|
|
test(df, loc, TRUE);
|
|
|
|
delete df;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) {
|
|
|
|
for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) {
|
|
|
|
if(TEST_TABLE[itable++]) {
|
2004-07-18 02:02:38 +00:00
|
|
|
logln("Testing dstyle" + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) );
|
1999-08-16 21:50:52 +00:00
|
|
|
DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc);
|
2003-05-16 21:58:37 +00:00
|
|
|
if(df == NULL) {
|
2004-07-18 02:02:38 +00:00
|
|
|
errln(UnicodeString("Could not DF::createDateTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) + "Locale: " + loc.getDisplayName(temp));
|
2003-05-16 21:58:37 +00:00
|
|
|
} else {
|
2004-07-18 02:02:38 +00:00
|
|
|
test(df, loc);
|
|
|
|
delete df;
|
2003-05-16 21:58:37 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-11 02:44:53 +00:00
|
|
|
void DateFormatRoundTripTest::test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
UnicodeString pat;
|
|
|
|
if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
|
|
|
|
errln("DateFormat wasn't a SimpleDateFormat");
|
|
|
|
return;
|
|
|
|
}
|
2003-12-04 23:16:57 +00:00
|
|
|
|
|
|
|
UBool isGregorian = FALSE;
|
|
|
|
UErrorCode minStatus = U_ZERO_ERROR;
|
|
|
|
UDate minDate = CalendarTest::minDateOfCalendar(*fmt->getCalendar(), isGregorian, minStatus);
|
|
|
|
if(U_FAILURE(minStatus)) {
|
|
|
|
errln((UnicodeString)"Failure getting min date for " + origLocale.getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//logln(UnicodeString("Min date is ") + fullFormat(minDate) + " for " + origLocale.getName());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
pat = ((SimpleDateFormat*)fmt)->toPattern(pat);
|
|
|
|
|
|
|
|
// NOTE TO MAINTAINER
|
|
|
|
// This indexOf check into the pattern needs to be refined to ignore
|
|
|
|
// quoted characters. Currently, this isn't a problem with the locale
|
|
|
|
// patterns we have, but it may be a problem later.
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool hasEra = (pat.indexOf(UnicodeString("G")) != -1);
|
|
|
|
UBool hasZone = (pat.indexOf(UnicodeString("z")) != -1);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Because patterns contain incomplete data representing the Date,
|
|
|
|
// we must be careful of how we do the roundtrip. We start with
|
|
|
|
// a randomly generated Date because they're easier to generate.
|
|
|
|
// From this we get a string. The string is our real starting point,
|
|
|
|
// because this string should parse the same way all the time. Note
|
|
|
|
// that it will not necessarily parse back to the original date because
|
|
|
|
// of incompleteness in patterns. For example, a time-only pattern won't
|
|
|
|
// parse back to the same date.
|
|
|
|
|
|
|
|
//try {
|
|
|
|
for(int i = 0; i < TRIALS; ++i) {
|
|
|
|
UDate *d = new UDate [DEPTH];
|
|
|
|
UnicodeString *s = new UnicodeString[DEPTH];
|
|
|
|
|
2003-12-04 23:16:57 +00:00
|
|
|
if(isGregorian == TRUE) {
|
|
|
|
d[0] = generateDate();
|
|
|
|
} else {
|
|
|
|
d[0] = generateDate(minDate);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// We go through this loop until we achieve a match or until
|
|
|
|
// the maximum loop count is reached. We record the points at
|
|
|
|
// which the date and the string starts to match. Once matching
|
|
|
|
// starts, it should continue.
|
|
|
|
int loop;
|
|
|
|
int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime()
|
|
|
|
int smatch = 0; // s[smatch].equals(s[smatch-1])
|
|
|
|
for(loop = 0; loop < DEPTH; ++loop) {
|
|
|
|
if (loop > 0) {
|
|
|
|
d[loop] = fmt->parse(s[loop-1], status);
|
2003-05-30 00:52:43 +00:00
|
|
|
failure(status, "fmt->parse", s[loop-1]);
|
|
|
|
status = U_ZERO_ERROR; /* any error would have been reported */
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s[loop] = fmt->format(d[loop], s[loop]);
|
2003-05-30 00:52:43 +00:00
|
|
|
|
2003-12-04 23:16:57 +00:00
|
|
|
// For displaying which date is being tested
|
|
|
|
//logln(s[loop] + " = " + fullFormat(d[loop]));
|
|
|
|
|
2003-05-30 00:52:43 +00:00
|
|
|
if(s[loop].length() == 0) {
|
|
|
|
errln("FAIL: fmt->format gave 0-length string in " + pat + " with number " + d[loop] + " in locale " + origLocale.getName());
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if(loop > 0) {
|
|
|
|
if(smatch == 0) {
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool match = s[loop] == s[loop-1];
|
1999-08-16 21:50:52 +00:00
|
|
|
if(smatch == 0) {
|
|
|
|
if(match)
|
|
|
|
smatch = loop;
|
|
|
|
}
|
|
|
|
else if( ! match)
|
|
|
|
errln("FAIL: String mismatch after match");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dmatch == 0) {
|
|
|
|
// {sfb} watch out here, this might not work
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/;
|
1999-08-16 21:50:52 +00:00
|
|
|
if(dmatch == 0) {
|
|
|
|
if(match)
|
|
|
|
dmatch = loop;
|
|
|
|
}
|
|
|
|
else if( ! match)
|
|
|
|
errln("FAIL: Date mismatch after match");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(smatch != 0 && dmatch != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At this point loop == DEPTH if we've failed, otherwise loop is the
|
|
|
|
// max(smatch, dmatch), that is, the index at which we have string and
|
|
|
|
// date matching.
|
|
|
|
|
|
|
|
// Date usually matches in 2. Exceptions handled below.
|
|
|
|
int maxDmatch = 2;
|
|
|
|
int maxSmatch = 1;
|
|
|
|
if (dmatch > maxDmatch) {
|
|
|
|
// Time-only pattern with zone information and a starting date in PST.
|
|
|
|
if(timeOnly && hasZone && fmt->getTimeZone().inDaylightTime(d[0], status) && ! failure(status, "TimeZone::inDST()")) {
|
|
|
|
maxDmatch = 3;
|
|
|
|
maxSmatch = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String usually matches in 1. Exceptions are checked for here.
|
|
|
|
if(smatch > maxSmatch) { // Don't compute unless necessary
|
2003-08-15 18:24:31 +00:00
|
|
|
UBool in0;
|
1999-08-16 21:50:52 +00:00
|
|
|
// Starts in BC, with no era in pattern
|
2003-02-21 08:32:22 +00:00
|
|
|
if( ! hasEra && getField(d[0], UCAL_ERA) == GregorianCalendar::BC)
|
1999-08-16 21:50:52 +00:00
|
|
|
maxSmatch = 2;
|
|
|
|
// Starts in DST, no year in pattern
|
2003-11-27 01:37:21 +00:00
|
|
|
else if((in0=fmt->getTimeZone().inDaylightTime(d[0], status)) && ! failure(status, "gettingDaylightTime") &&
|
1999-08-16 21:50:52 +00:00
|
|
|
pat.indexOf(UnicodeString("yyyy")) == -1)
|
|
|
|
maxSmatch = 2;
|
2003-08-15 18:24:31 +00:00
|
|
|
// If we start not in DST, but transition into DST
|
|
|
|
else if (!in0 &&
|
|
|
|
fmt->getTimeZone().inDaylightTime(d[1], status) && !failure(status, "gettingDaylightTime"))
|
|
|
|
maxSmatch = 2;
|
2002-04-12 19:05:31 +00:00
|
|
|
// Two digit year with no time zone change,
|
|
|
|
// unless timezone isn't used or we aren't close to the DST changover
|
|
|
|
else if (pat.indexOf(UnicodeString("y")) != -1
|
|
|
|
&& pat.indexOf(UnicodeString("yyyy")) == -1
|
2003-02-21 08:32:22 +00:00
|
|
|
&& getField(d[0], UCAL_YEAR)
|
|
|
|
!= getField(d[dmatch], UCAL_YEAR)
|
2003-05-30 00:52:43 +00:00
|
|
|
&& !failure(status, "error status [smatch>maxSmatch]")
|
2002-04-12 19:05:31 +00:00
|
|
|
&& ((hasZone
|
|
|
|
&& (fmt->getTimeZone().inDaylightTime(d[0], status)
|
|
|
|
== fmt->getTimeZone().inDaylightTime(d[dmatch], status)
|
2003-02-21 08:32:22 +00:00
|
|
|
|| getField(d[0], UCAL_MONTH) == UCAL_APRIL
|
|
|
|
|| getField(d[0], UCAL_MONTH) == UCAL_OCTOBER))
|
2002-04-12 19:05:31 +00:00
|
|
|
|| !hasZone)
|
|
|
|
)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
maxSmatch = 2;
|
2003-05-30 00:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-04 23:16:57 +00:00
|
|
|
if(dmatch > maxDmatch || smatch > maxSmatch) { // Special case for Japanese and Islamic (could have large negative years)
|
|
|
|
const char *type = fmt->getCalendar()->getType();
|
|
|
|
if(!strcmp(type,"japanese")) {
|
|
|
|
maxSmatch = 4;
|
|
|
|
maxDmatch = 4;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-12-04 23:16:57 +00:00
|
|
|
|
2003-08-12 19:22:32 +00:00
|
|
|
// Use @v to see verbose results on successful cases
|
|
|
|
UBool fail = (dmatch > maxDmatch || smatch > maxSmatch);
|
|
|
|
if (optionv || fail) {
|
|
|
|
if (fail) {
|
|
|
|
errln(UnicodeString("\nFAIL: Pattern: ") + pat +
|
|
|
|
" in Locale: " + origLocale.getName());
|
|
|
|
} else {
|
|
|
|
errln(UnicodeString("\nOk: Pattern: ") + pat +
|
|
|
|
" in Locale: " + origLocale.getName());
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-12 19:22:32 +00:00
|
|
|
logln("Date iters until match=%d (max allowed=%d), string iters until match=%d (max allowed=%d)",
|
|
|
|
dmatch,maxDmatch, smatch, maxSmatch);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
for(int j = 0; j <= loop && j < DEPTH; ++j) {
|
|
|
|
UnicodeString temp;
|
|
|
|
FieldPosition pos(FieldPosition::DONT_CARE);
|
2003-08-12 19:22:32 +00:00
|
|
|
errln((j>0?" P> ":" ") + fullFormat(d[j]) + " F> " +
|
2002-04-11 02:44:53 +00:00
|
|
|
escape(s[j], temp) + UnicodeString(" d=") + d[j] +
|
1999-08-16 21:50:52 +00:00
|
|
|
(j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") +
|
|
|
|
(j > 0 && s[j] == s[j-1]?" s==":""));
|
|
|
|
}
|
|
|
|
}
|
2002-12-04 00:28:06 +00:00
|
|
|
delete[] d;
|
2000-06-29 19:42:17 +00:00
|
|
|
delete[] s;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
/*}
|
|
|
|
catch (ParseException e) {
|
|
|
|
errln("Exception: " + e.getMessage());
|
|
|
|
logln(e.toString());
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2003-08-12 19:22:32 +00:00
|
|
|
const UnicodeString& DateFormatRoundTripTest::fullFormat(UDate d) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
if (dateFormat == 0) {
|
|
|
|
dateFormat = new SimpleDateFormat((UnicodeString)"EEE MMM dd HH:mm:ss.SSS zzz yyyy G", ec);
|
|
|
|
if (U_FAILURE(ec) || dateFormat == 0) {
|
|
|
|
fgStr = "[FAIL: SimpleDateFormat constructor]";
|
|
|
|
delete dateFormat;
|
|
|
|
dateFormat = 0;
|
|
|
|
return fgStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fgStr.truncate(0);
|
|
|
|
dateFormat->format(d, fgStr);
|
|
|
|
return fgStr;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Return a field of the given date
|
|
|
|
*/
|
|
|
|
int32_t DateFormatRoundTripTest::getField(UDate d, int32_t f) {
|
|
|
|
// Should be synchronized, but we're single threaded so it's ok
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
getFieldCal->setTime(d, status);
|
|
|
|
failure(status, "getfieldCal->setTime");
|
2003-02-21 08:32:22 +00:00
|
|
|
int32_t ret = getFieldCal->get((UCalendarDateFields)f, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
failure(status, "getfieldCal->get");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString& DateFormatRoundTripTest::escape(const UnicodeString& src, UnicodeString& dst )
|
|
|
|
{
|
|
|
|
dst.remove();
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int32_t i = 0; i < src.length(); ++i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar c = src[i];
|
|
|
|
if(c < 0x0080)
|
|
|
|
dst += c;
|
|
|
|
else {
|
|
|
|
dst += UnicodeString("[");
|
|
|
|
char buf [8];
|
|
|
|
sprintf(buf, "%#x", c);
|
|
|
|
dst += UnicodeString(buf);
|
|
|
|
dst += UnicodeString("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2003-12-04 23:16:57 +00:00
|
|
|
#define U_MILLIS_PER_YEAR (365.25 * 24 * 60 * 60 * 1000)
|
|
|
|
|
|
|
|
UDate DateFormatRoundTripTest::generateDate(UDate minDate)
|
|
|
|
{
|
|
|
|
// Bring range in conformance to generateDate() below.
|
|
|
|
if(minDate < (U_MILLIS_PER_YEAR * -(4000-1970))) {
|
|
|
|
minDate = (U_MILLIS_PER_YEAR * -(4000-1970));
|
|
|
|
}
|
|
|
|
for(int i=0;i<8;i++) {
|
|
|
|
double a = randFraction();
|
|
|
|
|
|
|
|
// Range from (min) to (8000-1970) AD
|
|
|
|
double dateRange = (0.0 - minDate) + (U_MILLIS_PER_YEAR + (8000-1970));
|
|
|
|
|
|
|
|
a *= dateRange;
|
|
|
|
|
|
|
|
// Now offset from minDate
|
|
|
|
a += minDate;
|
|
|
|
|
|
|
|
// Last sanity check
|
|
|
|
if(a>=minDate) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return minDate;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UDate DateFormatRoundTripTest::generateDate()
|
|
|
|
{
|
|
|
|
double a = randFraction();
|
|
|
|
|
|
|
|
// Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years
|
|
|
|
a *= 8000;
|
|
|
|
|
|
|
|
// Range from (4000-1970) BC to (8000-1970) AD
|
|
|
|
a -= 4000;
|
|
|
|
|
|
|
|
// Now scale up to ms
|
|
|
|
a *= 365.25 * 24 * 60 * 60 * 1000;
|
2002-04-12 19:05:31 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//return new Date((long)a);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|