]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsdate.cpp
1 // © 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"
22 const double IntlTestDateFormat::ONEYEAR
= 365.25 * ONEDAY
; // Approximate
24 IntlTestDateFormat::~IntlTestDateFormat() {}
27 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
30 // par is ignored throughout this file
31 void IntlTestDateFormat::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
33 if (exec
) logln("TestSuite DateFormat");
35 case 0: name
= "GenericTest";
38 fFormat
= DateFormat::createInstance();
39 fTestName
= "createInstance";
41 testFormat(/* par */);
44 case 1: name
= "DefaultLocale";
47 testLocale(/*par, */Locale::getDefault(), "Default Locale");
51 case 2: name
= "TestAvailableLocales";
54 testAvailableLocales(/* par */);
58 case 3: name
= "MonsterTest";
65 default: name
= ""; break;
70 IntlTestDateFormat::testLocale(/*char* par, */const Locale
& locale
, const UnicodeString
& localeName
)
72 DateFormat::EStyle timeStyle
, dateStyle
;
74 // For patterns including only time information and a timezone, it may take
75 // up to three iterations, since the timezone may shift as the year number
76 // is determined. For other patterns, 2 iterations should suffice.
79 for(timeStyle
= (DateFormat::EStyle
)0;
80 timeStyle
< (DateFormat::EStyle
)4;
81 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
83 fTestName
= (UnicodeString
) "Time test " + (int32_t) timeStyle
+ " (" + localeName
+ ")";
84 fFormat
= DateFormat::createTimeInstance(timeStyle
, locale
);
85 testFormat(/* par */);
90 for(dateStyle
= (DateFormat::EStyle
)0;
91 dateStyle
< (DateFormat::EStyle
)4;
92 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
94 fTestName
= (UnicodeString
) "Date test " + (int32_t) dateStyle
+ " (" + localeName
+ ")";
95 fFormat
= DateFormat::createDateInstance(dateStyle
, locale
);
96 testFormat(/* par */);
99 for(dateStyle
= (DateFormat::EStyle
)0;
100 dateStyle
< (DateFormat::EStyle
)4;
101 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
103 for(timeStyle
= (DateFormat::EStyle
)0;
104 timeStyle
< (DateFormat::EStyle
)4;
105 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
107 fTestName
= (UnicodeString
) "DateTime test " + (int32_t) dateStyle
+ "/" + (int32_t) timeStyle
+ " (" + localeName
+ ")";
108 fFormat
= DateFormat::createDateTimeInstance(dateStyle
, timeStyle
, locale
);
109 testFormat(/* par */);
114 void IntlTestDateFormat::testFormat(/* char* par */)
118 dataerrln("FAIL: DateFormat creation failed");
124 UDate now
= Calendar::getNow();
126 tryDate(1278161801778.0);
127 tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
128 tryDate(9516987689250.0); // In the year 2271
130 // Shift 6 months into the future, AT THE SAME TIME OF DAY.
131 // This will test the DST handling.
132 tryDate(now
+ 6.0*30*ONEDAY
);
134 UDate limit
= now
* 10; // Arbitrary limit
135 for (int32_t i
=0; i
<3; ++i
)
136 tryDate(uprv_floor(randDouble() * limit
));
142 IntlTestDateFormat::describeTest()
144 // Assume it's a SimpleDateFormat and get some info
145 SimpleDateFormat
*s
= (SimpleDateFormat
*)fFormat
;
147 logln(fTestName
+ " Pattern " + s
->toPattern(str
));
150 void IntlTestDateFormat::tryDate(UDate theDate
)
152 const int32_t DEPTH
= 10;
154 UnicodeString string
[DEPTH
];
156 int32_t dateMatch
= 0;
157 int32_t stringMatch
= 0;
159 #if defined (U_CAL_DEBUG)
165 fFormat
->format(theDate
, string
[0]);
167 for (i
=1; i
<DEPTH
; ++i
)
169 UErrorCode status
= U_ZERO_ERROR
;
170 date
[i
] = fFormat
->parse(string
[i
-1], status
);
171 if (U_FAILURE(status
))
174 errln("**** FAIL: Parse of " + prettify(string
[i
-1], FALSE
) + " failed.");
178 fFormat
->format(date
[i
], string
[i
]);
179 if (dateMatch
== 0 && date
[i
] == date
[i
-1])
181 else if (dateMatch
> 0 && date
[i
] != date
[i
-1])
184 errln("**** FAIL: Date mismatch after match for " + string
[i
]);
188 if (stringMatch
== 0 && string
[i
] == string
[i
-1])
190 else if (stringMatch
> 0 && string
[i
] != string
[i
-1])
193 errln("**** FAIL: String mismatch after match for " + string
[i
]);
197 if (dateMatch
> 0 && stringMatch
> 0)
203 if (stringMatch
> fLimit
|| dateMatch
> fLimit
)
206 errln((UnicodeString
)"**** FAIL: No string and/or date match within " + fLimit
207 + " iterations for the Date " + string
[0] + "\t(" + theDate
+ ").");
213 for (int32_t k
=0; k
<=i
; ++k
)
215 logln((UnicodeString
)"" + k
+ ": " + date
[k
] + " F> " +
221 // Return a random double from 0.01 to 1, inclusive
222 double IntlTestDateFormat::randDouble()
224 // Assume 8-bit (or larger) rand values. Also assume
225 // that the system rand() function is very poor, which it always is.
228 char* poke
= (char*)&d
;
231 for (i
=0; i
< sizeof(double); ++i
)
233 poke
[i
] = (char)(rand() & 0xFF);
235 } while (uprv_isNaN(d
) || uprv_isInfinite(d
));
241 double e
= uprv_floor(log10(d
));
243 d
*= uprv_pow10((int32_t)(-e
-2));
245 d
/= uprv_pow10((int32_t)(e
+1));
247 // While this is not a real normalized number make another one.
248 } while (uprv_isNaN(d
) || uprv_isInfinite(d
)
249 || !((-DBL_MAX
< d
&& d
< DBL_MAX
) || (d
< -DBL_MIN
&& DBL_MIN
< d
)));
253 void IntlTestDateFormat::testAvailableLocales(/* char* par */)
256 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
257 logln((UnicodeString
)"" + count
+ " available locales");
258 if (locales
&& count
)
262 for (int32_t i
=0; i
<count
; ++i
)
264 if (i
!=0) all
+= ", ";
265 all
+= locales
[i
].getName();
269 else dataerrln((UnicodeString
)"**** FAIL: Zero available locales or null array pointer");
272 void IntlTestDateFormat::monsterTest(/*char *par*/)
275 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
276 if (locales
&& count
)
278 if (quick
&& count
> 3) {
279 logln("quick test: testing just 3 locales!");
282 for (int32_t i
=0; i
<count
; ++i
)
284 UnicodeString name
= UnicodeString(locales
[i
].getName(), "");
285 logln((UnicodeString
)"Testing " + name
+ "...");
286 testLocale(/*par, */locales
[i
], name
);
291 #endif /* #if !UCONFIG_NO_FORMATTING */