1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/***********************************************************************
* Copyright (c) 1997-2009, International Business Machines Corporation
* and others. All Rights Reserved.
***********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/datefmt.h"
#include "unicode/smpdtfmt.h"
#include "tsdate.h"
#include "putilimp.h"
#include "cstring.h"
#include <float.h>
#include <stdlib.h>
#include <math.h>
IntlTestDateFormat::~IntlTestDateFormat() {}
/**
* This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
* DateFormat.
*/
// par is ignored throughout this file
void IntlTestDateFormat::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
{
if (exec) logln("TestSuite DateFormat");
switch (index) {
case 0: name = "GenericTest";
if (exec) {
logln(name);
fFormat = DateFormat::createInstance();
fTestName = "createInstance";
fLimit = 3;
testFormat(/* par */);
}
break;
case 1: name = "DefaultLocale";
if (exec) {
logln(name);
testLocale(/*par, */Locale::getDefault(), "Default Locale");
}
break;
case 2: name = "TestAvailableLocales";
if (exec) {
logln(name);
testAvailableLocales(/* par */);
}
break;
case 3: name = "MonsterTest";
if (exec) {
logln(name);
monsterTest(/*par*/);
}
break;
default: name = ""; break;
}
}
void
IntlTestDateFormat::testLocale(/*char* par, */const Locale& locale, const UnicodeString& localeName)
{
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 = static_cast<DateFormat::EStyle>(0);
timeStyle < static_cast<DateFormat::EStyle>(4);
timeStyle = static_cast<DateFormat::EStyle>(timeStyle + 1))
{
fTestName = UnicodeString("Time test ") + static_cast<int32_t>(timeStyle) + " (" + localeName + ")";
fFormat = DateFormat::createTimeInstance(timeStyle, locale);
testFormat(/* par */);
}
fLimit = 2;
for (dateStyle = static_cast<DateFormat::EStyle>(0);
dateStyle < static_cast<DateFormat::EStyle>(4);
dateStyle = static_cast<DateFormat::EStyle>(dateStyle + 1))
{
fTestName = UnicodeString("Date test ") + static_cast<int32_t>(dateStyle) + " (" + localeName + ")";
fFormat = DateFormat::createDateInstance(dateStyle, locale);
testFormat(/* par */);
}
for (dateStyle = static_cast<DateFormat::EStyle>(0);
dateStyle < static_cast<DateFormat::EStyle>(4);
dateStyle = static_cast<DateFormat::EStyle>(dateStyle + 1))
{
for (timeStyle = static_cast<DateFormat::EStyle>(0);
timeStyle < static_cast<DateFormat::EStyle>(4);
timeStyle = static_cast<DateFormat::EStyle>(timeStyle + 1))
{
fTestName = UnicodeString("DateTime test ") + static_cast<int32_t>(dateStyle) + "/" + static_cast<int32_t>(timeStyle) + " (" + localeName + ")";
fFormat = DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale);
testFormat(/* par */);
}
}
}
void IntlTestDateFormat::testFormat(/* char* par */)
{
if (fFormat == nullptr)
{
dataerrln("FAIL: DateFormat creation failed");
return;
}
describeTest();
UDate now = Calendar::getNow();
tryDate(0);
tryDate(1278161801778.0);
tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
tryDate(9516987689250.0); // In the year 2271
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
for (int32_t i=0; i<3; ++i)
tryDate(uprv_floor(randDouble() * limit));
delete fFormat;
}
void
IntlTestDateFormat::describeTest()
{
// Assume it's a SimpleDateFormat and get some info
SimpleDateFormat *s = dynamic_cast<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;
UBool dump = false;
#if defined (U_CAL_DEBUG)
dump = true;
#endif
int32_t i;
date[0] = theDate;
fFormat->format(theDate, string[0]);
UErrorCode status = U_ZERO_ERROR;
const char* locID = "??";
Locale loc = fFormat->getCalendar()->getLocale(ULOC_VALID_LOCALE, status);
if (U_SUCCESS(status)) {
locID = loc.getName();
}
for (i=1; i<DEPTH; ++i)
{
status = U_ZERO_ERROR;
date[i] = fFormat->parse(string[i-1], status);
if (U_FAILURE(status))
{
describeTest();
errln("**** FAIL, locale " + UnicodeString(locID,-1,US_INV) +
": Parse of " + prettify(string[i-1], false) + " failed.");
dump = true;
break;
}
fFormat->format(date[i], string[i]);
if (dateMatch == 0 && date[i] == date[i-1])
dateMatch = i;
else if (dateMatch > 0 && date[i] != date[i-1])
{
describeTest();
errln("**** FAIL, locale " + UnicodeString(locID,-1,US_INV) +
": Date mismatch after match for " + string[i]);
dump = true;
break;
}
if (stringMatch == 0 && string[i] == string[i-1])
stringMatch = i;
else if (stringMatch > 0 && string[i] != string[i-1])
{
describeTest();
errln("**** FAIL, locale " + UnicodeString(locID,-1,US_INV) +
": String mismatch after match for " + string[i]);
dump = true;
break;
}
if (dateMatch > 0 && stringMatch > 0)
break;
}
if (i == DEPTH)
--i;
if (stringMatch > fLimit || dateMatch > fLimit)
{
describeTest();
errln(UnicodeString("**** FAIL: No string and/or date match within ") + fLimit
+ " iterations for the Date " + string[0] + "\t(" + theDate + ").");
dump = true;
}
if (dump)
{
for (int32_t k=0; k<=i; ++k)
{
logln(UnicodeString("") + k + ": " + date[k] + " F> " +
string[k] + " P> ");
}
}
}
// 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.
double d=0.0;
uint32_t i;
char* poke = reinterpret_cast<char*>(&d);
do {
do {
for (i=0; i < sizeof(double); ++i)
{
poke[i] = static_cast<char>(rand() & 0xFF);
}
} while (uprv_isNaN(d) || uprv_isInfinite(d));
if (d < 0.0)
d = -d;
if (d > 0.0)
{
double e = uprv_floor(log10(d));
if (e < -2.0)
d *= uprv_pow10(static_cast<int32_t>(-e - 2));
else if (e > -1.0)
d /= uprv_pow10(static_cast<int32_t>(e + 1));
}
// While this is not a real normalized number make another one.
} while (uprv_isNaN(d) || uprv_isInfinite(d)
|| !((-DBL_MAX < d && d < DBL_MAX) || (d < -DBL_MIN && DBL_MIN < d)));
return d;
}
void IntlTestDateFormat::testAvailableLocales(/* char* par */)
{
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 += ", ";
all += locales[i].getName();
}
logln(all);
}
else dataerrln(UnicodeString("**** FAIL: Zero available locales or null array pointer"));
}
void IntlTestDateFormat::monsterTest(/*char *par*/)
{
int32_t count;
const Locale* locales = DateFormat::getAvailableLocales(count);
if (locales && count)
{
if (quick && count > 3) {
logln("quick test: testing just 3 locales!");
count = 3;
}
for (int32_t i=0; i<count; ++i)
{
UnicodeString name = UnicodeString(locales[i].getName(), "");
logln(UnicodeString("Testing ") + name + "...");
testLocale(/*par, */locales[i], name);
}
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
|