1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /***********************************************************************
4 * Copyright (c) 1997-2009, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ***********************************************************************/
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/datefmt.h"
13 #include "unicode/smpdtfmt.h"
21 const double IntlTestDateFormat::ONEYEAR = 365.25 * ONEDAY; // Approximate
23 IntlTestDateFormat::~IntlTestDateFormat() {}
26 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
29 // par is ignored throughout this file
30 void IntlTestDateFormat::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
32 if (exec) logln("TestSuite DateFormat");
34 case 0: name = "GenericTest";
37 fFormat = DateFormat::createInstance();
38 fTestName = "createInstance";
40 testFormat(/* par */);
43 case 1: name = "DefaultLocale";
46 testLocale(/*par, */Locale::getDefault(), "Default Locale");
50 case 2: name = "TestAvailableLocales";
53 testAvailableLocales(/* par */);
57 case 3: name = "MonsterTest";
64 default: name = ""; break;
69 IntlTestDateFormat::testLocale(/*char* par, */const Locale& locale, const UnicodeString& localeName)
71 DateFormat::EStyle timeStyle, dateStyle;
73 // For patterns including only time information and a timezone, it may take
74 // up to three iterations, since the timezone may shift as the year number
75 // is determined. For other patterns, 2 iterations should suffice.
78 for(timeStyle = (DateFormat::EStyle)0;
79 timeStyle < (DateFormat::EStyle)4;
80 timeStyle = (DateFormat::EStyle) (timeStyle+1))
82 fTestName = (UnicodeString) "Time test " + (int32_t) timeStyle + " (" + localeName + ")";
83 fFormat = DateFormat::createTimeInstance(timeStyle, locale);
84 testFormat(/* par */);
89 for(dateStyle = (DateFormat::EStyle)0;
90 dateStyle < (DateFormat::EStyle)4;
91 dateStyle = (DateFormat::EStyle) (dateStyle+1))
93 fTestName = (UnicodeString) "Date test " + (int32_t) dateStyle + " (" + localeName + ")";
94 fFormat = DateFormat::createDateInstance(dateStyle, locale);
95 testFormat(/* par */);
98 for(dateStyle = (DateFormat::EStyle)0;
99 dateStyle < (DateFormat::EStyle)4;
100 dateStyle = (DateFormat::EStyle) (dateStyle+1))
102 for(timeStyle = (DateFormat::EStyle)0;
103 timeStyle < (DateFormat::EStyle)4;
104 timeStyle = (DateFormat::EStyle) (timeStyle+1))
106 fTestName = (UnicodeString) "DateTime test " + (int32_t) dateStyle + "/" + (int32_t) timeStyle + " (" + localeName + ")";
107 fFormat = DateFormat::createDateTimeInstance(dateStyle, timeStyle, locale);
108 testFormat(/* par */);
113 void IntlTestDateFormat::testFormat(/* char* par */)
117 dataerrln("FAIL: DateFormat creation failed");
123 UDate now = Calendar::getNow();
125 tryDate(1278161801778.0);
126 tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
127 tryDate(9516987689250.0); // In the year 2271
129 // Shift 6 months into the future, AT THE SAME TIME OF DAY.
130 // This will test the DST handling.
131 tryDate(now + 6.0*30*ONEDAY);
133 UDate limit = now * 10; // Arbitrary limit
134 for (int32_t i=0; i<3; ++i)
135 tryDate(uprv_floor(randDouble() * limit));
141 IntlTestDateFormat::describeTest()
143 // Assume it's a SimpleDateFormat and get some info
144 SimpleDateFormat *s = (SimpleDateFormat*)fFormat;
146 logln(fTestName + " Pattern " + s->toPattern(str));
149 void IntlTestDateFormat::tryDate(UDate theDate)
151 const int32_t DEPTH = 10;
153 UnicodeString string[DEPTH];
155 int32_t dateMatch = 0;
156 int32_t stringMatch = 0;
158 #if defined (U_CAL_DEBUG)
164 fFormat->format(theDate, string[0]);
166 for (i=1; i<DEPTH; ++i)
168 UErrorCode status = U_ZERO_ERROR;
169 date[i] = fFormat->parse(string[i-1], status);
170 if (U_FAILURE(status))
173 errln("**** FAIL: Parse of " + prettify(string[i-1], FALSE) + " failed.");
177 fFormat->format(date[i], string[i]);
178 if (dateMatch == 0 && date[i] == date[i-1])
180 else if (dateMatch > 0 && date[i] != date[i-1])
183 errln("**** FAIL: Date mismatch after match for " + string[i]);
187 if (stringMatch == 0 && string[i] == string[i-1])
189 else if (stringMatch > 0 && string[i] != string[i-1])
192 errln("**** FAIL: String mismatch after match for " + string[i]);
196 if (dateMatch > 0 && stringMatch > 0)
202 if (stringMatch > fLimit || dateMatch > fLimit)
205 errln((UnicodeString)"**** FAIL: No string and/or date match within " + fLimit
206 + " iterations for the Date " + string[0] + "\t(" + theDate + ").");
212 for (int32_t k=0; k<=i; ++k)
214 logln((UnicodeString)"" + k + ": " + date[k] + " F> " +
220 // Return a random double from 0.01 to 1, inclusive
221 double IntlTestDateFormat::randDouble()
223 // Assume 8-bit (or larger) rand values. Also assume
224 // that the system rand() function is very poor, which it always is.
227 char* poke = (char*)&d;
230 for (i=0; i < sizeof(double); ++i)
232 poke[i] = (char)(rand() & 0xFF);
234 } while (uprv_isNaN(d) || uprv_isInfinite(d));
240 double e = uprv_floor(log10(d));
242 d *= uprv_pow10((int32_t)(-e-2));
244 d /= uprv_pow10((int32_t)(e+1));
246 // While this is not a real normalized number make another one.
247 } while (uprv_isNaN(d) || uprv_isInfinite(d)
248 || !((-DBL_MAX < d && d < DBL_MAX) || (d < -DBL_MIN && DBL_MIN < d)));
252 void IntlTestDateFormat::testAvailableLocales(/* char* par */)
255 const Locale* locales = DateFormat::getAvailableLocales(count);
256 logln((UnicodeString)"" + count + " available locales");
257 if (locales && count)
261 for (int32_t i=0; i<count; ++i)
263 if (i!=0) all += ", ";
264 all += locales[i].getName();
268 else dataerrln((UnicodeString)"**** FAIL: Zero available locales or null array pointer");
271 void IntlTestDateFormat::monsterTest(/*char *par*/)
274 const Locale* locales = DateFormat::getAvailableLocales(count);
275 if (locales && count)
277 if (quick && count > 3) {
278 logln("quick test: testing just 3 locales!");
281 for (int32_t i=0; i<count; ++i)
283 UnicodeString name = UnicodeString(locales[i].getName(), "");
284 logln((UnicodeString)"Testing " + name + "...");
285 testLocale(/*par, */locales[i], name);
290 #endif /* #if !UCONFIG_NO_FORMATTING */