]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
374ca955 | 3 | /*********************************************************************** |
b75a7d8f | 4 | * COPYRIGHT: |
2ca993e8 | 5 | * Copyright (c) 1997-2015, International Business Machines Corporation |
374ca955 A |
6 | * and others. All Rights Reserved. |
7 | ***********************************************************************/ | |
b75a7d8f A |
8 | |
9 | #include "unicode/utypes.h" | |
10 | ||
11 | #if !UCONFIG_NO_FORMATTING | |
12 | ||
13 | #include "unicode/datefmt.h" | |
14 | #include "unicode/smpdtfmt.h" | |
15 | #include "unicode/gregocal.h" | |
16 | #include "dtfmtrtts.h" | |
374ca955 | 17 | #include "caltest.h" |
b331163b | 18 | #include "cstring.h" |
b75a7d8f A |
19 | |
20 | #include <stdio.h> | |
21 | #include <string.h> | |
22 | ||
23 | // ***************************************************************************** | |
24 | // class DateFormatRoundTripTest | |
25 | // ***************************************************************************** | |
26 | ||
27 | // Useful for turning up subtle bugs: Change the following to TRUE, recompile, | |
28 | // and run while at lunch. | |
29 | // Warning -- makes test run infinite loop!!! | |
30 | #ifndef INFINITE | |
31 | #define INFINITE 0 | |
32 | #endif | |
33 | ||
34 | // Define this to test just a single locale | |
73c04bcf | 35 | //#define TEST_ONE_LOC "cs_CZ" |
b75a7d8f A |
36 | |
37 | // If SPARSENESS is > 0, we don't run each exhaustive possibility. | |
38 | // There are 24 total possible tests per each locale. A SPARSENESS | |
39 | // of 12 means we run half of them. A SPARSENESS of 23 means we run | |
40 | // 1 of them. SPARSENESS _must_ be in the range 0..23. | |
41 | int32_t DateFormatRoundTripTest::SPARSENESS = 0; | |
42 | int32_t DateFormatRoundTripTest::TRIALS = 4; | |
43 | int32_t DateFormatRoundTripTest::DEPTH = 5; | |
44 | ||
374ca955 A |
45 | DateFormatRoundTripTest::DateFormatRoundTripTest() : dateFormat(0) { |
46 | } | |
47 | ||
48 | DateFormatRoundTripTest::~DateFormatRoundTripTest() { | |
49 | delete dateFormat; | |
50 | } | |
b75a7d8f A |
51 | |
52 | #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; | |
53 | ||
54 | void | |
374ca955 | 55 | DateFormatRoundTripTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par ) |
b75a7d8f | 56 | { |
374ca955 | 57 | optionv = (par && *par=='v'); |
b75a7d8f A |
58 | switch (index) { |
59 | CASE(0,TestDateFormatRoundTrip) | |
73c04bcf | 60 | CASE(1, TestCentury) |
b75a7d8f A |
61 | default: name = ""; break; |
62 | } | |
63 | } | |
64 | ||
65 | UBool | |
66 | DateFormatRoundTripTest::failure(UErrorCode status, const char* msg) | |
67 | { | |
68 | if(U_FAILURE(status)) { | |
69 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); | |
70 | return TRUE; | |
71 | } | |
72 | ||
73 | return FALSE; | |
74 | } | |
75 | ||
76 | UBool | |
77 | DateFormatRoundTripTest::failure(UErrorCode status, const char* msg, const UnicodeString& str) | |
78 | { | |
79 | if(U_FAILURE(status)) { | |
80 | UnicodeString escaped; | |
81 | escape(str,escaped); | |
82 | errln(UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status) + ", str=" + escaped); | |
83 | return TRUE; | |
84 | } | |
85 | ||
86 | return FALSE; | |
87 | } | |
88 | ||
73c04bcf A |
89 | void DateFormatRoundTripTest::TestCentury() |
90 | { | |
91 | UErrorCode status = U_ZERO_ERROR; | |
92 | Locale locale("es_PA"); | |
93 | UnicodeString pattern = "MM/dd/yy hh:mm:ss a z"; | |
94 | SimpleDateFormat fmt(pattern, locale, status); | |
729e4ab9 A |
95 | if (U_FAILURE(status)) { |
96 | dataerrln("Fail: construct SimpleDateFormat: %s", u_errorName(status)); | |
97 | return; | |
98 | } | |
73c04bcf A |
99 | UDate date[] = {-55018555891590.05, 0, 0}; |
100 | UnicodeString result[2]; | |
101 | ||
102 | fmt.format(date[0], result[0]); | |
103 | date[1] = fmt.parse(result[0], status); | |
104 | fmt.format(date[1], result[1]); | |
105 | date[2] = fmt.parse(result[1], status); | |
106 | ||
46f4442e | 107 | /* This test case worked OK by accident before. date[1] != date[0], |
4162bf98 | 108 | * because we use -80/+20 year window for 2-digit year parsing. |
46f4442e | 109 | * (date[0] is in year 1926, date[1] is in year 2026.) result[1] set |
4162bf98 A |
110 | * by the first format call returns "07/13/26 07:48:28 p.m. PST", |
111 | * which is correct, because DST was not used in year 1926 in zone | |
46f4442e A |
112 | * America/Los_Angeles. When this is parsed, date[1] becomes a time |
113 | * in 2026, which is "07/13/26 08:48:28 p.m. PDT". There was a zone | |
4162bf98 A |
114 | * offset calculation bug that observed DST in 1926, which was resolved. |
115 | * Before the bug was resolved, result[0] == result[1] was true, | |
116 | * but after the bug fix, the expected result is actually | |
46f4442e | 117 | * result[0] != result[1]. -Yoshito |
4162bf98 A |
118 | */ |
119 | /* TODO: We need to review this code and clarify what we really | |
120 | * want to test here. | |
121 | */ | |
122 | //if (date[1] != date[2] || result[0] != result[1]) { | |
123 | if (date[1] != date[2]) { | |
73c04bcf A |
124 | errln("Round trip failure: \"%S\" (%f), \"%S\" (%f)", result[0].getBuffer(), date[1], result[1].getBuffer(), date[2]); |
125 | } | |
126 | } | |
127 | ||
b75a7d8f | 128 | // == |
4388f060 | 129 | |
b75a7d8f A |
130 | void DateFormatRoundTripTest::TestDateFormatRoundTrip() |
131 | { | |
132 | UErrorCode status = U_ZERO_ERROR; | |
b75a7d8f A |
133 | |
134 | getFieldCal = Calendar::createInstance(status); | |
729e4ab9 A |
135 | if (U_FAILURE(status)) { |
136 | dataerrln("Fail: Calendar::createInstance: %s", u_errorName(status)); | |
137 | return; | |
138 | } | |
b75a7d8f A |
139 | |
140 | ||
141 | int32_t locCount = 0; | |
142 | const Locale *avail = DateFormat::getAvailableLocales(locCount); | |
143 | logln("DateFormat available locales: %d", locCount); | |
144 | if(quick) { | |
145 | SPARSENESS = 18; | |
146 | logln("Quick mode: only testing SPARSENESS = 18"); | |
147 | } | |
148 | TimeZone *tz = TimeZone::createDefault(); | |
149 | UnicodeString temp; | |
150 | logln("Default TimeZone: " + tz->getID(temp)); | |
151 | delete tz; | |
152 | ||
153 | #ifdef TEST_ONE_LOC // define this to just test ONE locale. | |
154 | Locale loc(TEST_ONE_LOC); | |
155 | test(loc); | |
156 | #if INFINITE | |
157 | for(;;) { | |
158 | test(loc); | |
159 | } | |
160 | #endif | |
161 | ||
162 | #else | |
163 | # if INFINITE | |
164 | // Special infinite loop test mode for finding hard to reproduce errors | |
165 | Locale loc = Locale::getDefault(); | |
166 | logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName(temp)); | |
167 | for(;;) | |
168 | test(loc); | |
169 | # else | |
170 | test(Locale::getDefault()); | |
171 | ||
374ca955 A |
172 | #if 1 |
173 | // installed locales | |
b75a7d8f | 174 | for (int i=0; i < locCount; ++i) { |
b331163b | 175 | test(avail[i]); |
b75a7d8f | 176 | } |
374ca955 A |
177 | #endif |
178 | ||
179 | #if 1 | |
180 | // special locales | |
181 | int32_t jCount = CalendarTest::testLocaleCount(); | |
182 | for (int32_t j=0; j < jCount; ++j) { | |
183 | test(Locale(CalendarTest::testLocaleID(j))); | |
184 | } | |
185 | #endif | |
186 | ||
b75a7d8f A |
187 | # endif |
188 | #endif | |
189 | ||
b75a7d8f A |
190 | delete getFieldCal; |
191 | } | |
192 | ||
193 | static const char *styleName(DateFormat::EStyle s) | |
194 | { | |
195 | switch(s) | |
196 | { | |
197 | case DateFormat::SHORT: return "SHORT"; | |
198 | case DateFormat::MEDIUM: return "MEDIUM"; | |
199 | case DateFormat::LONG: return "LONG"; | |
200 | case DateFormat::FULL: return "FULL"; | |
201 | // case DateFormat::DEFAULT: return "DEFAULT"; | |
202 | case DateFormat::DATE_OFFSET: return "DATE_OFFSET"; | |
203 | case DateFormat::NONE: return "NONE"; | |
204 | case DateFormat::DATE_TIME: return "DATE_TIME"; | |
205 | default: return "Unknown"; | |
206 | } | |
207 | } | |
208 | ||
209 | void DateFormatRoundTripTest::test(const Locale& loc) | |
210 | { | |
211 | UnicodeString temp; | |
212 | #if !INFINITE | |
213 | logln("Locale: " + loc.getDisplayName(temp)); | |
214 | #endif | |
215 | ||
216 | // Total possibilities = 24 | |
217 | // 4 date | |
218 | // 4 time | |
219 | // 16 date-time | |
220 | UBool TEST_TABLE [24];//= new boolean[24]; | |
221 | int32_t i = 0; | |
222 | for(i = 0; i < 24; ++i) | |
223 | TEST_TABLE[i] = TRUE; | |
224 | ||
225 | // If we have some sparseness, implement it here. Sparseness decreases | |
226 | // test time by eliminating some tests, up to 23. | |
227 | for(i = 0; i < SPARSENESS; ) { | |
228 | int random = (int)(randFraction() * 24); | |
229 | if (random >= 0 && random < 24 && TEST_TABLE[i]) { | |
230 | TEST_TABLE[i] = FALSE; | |
231 | ++i; | |
232 | } | |
233 | } | |
234 | ||
235 | int32_t itable = 0; | |
236 | int32_t style = 0; | |
237 | for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { | |
238 | if(TEST_TABLE[itable++]) { | |
239 | logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); | |
240 | DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc); | |
241 | if(df == NULL) { | |
242 | errln(UnicodeString("Could not DF::createDateInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); | |
243 | } else { | |
244 | test(df, loc); | |
245 | delete df; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { | |
251 | if (TEST_TABLE[itable++]) { | |
0f5d89e8 | 252 | logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); |
b75a7d8f A |
253 | DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc); |
254 | if(df == NULL) { | |
255 | errln(UnicodeString("Could not DF::createTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); | |
256 | } else { | |
257 | test(df, loc, TRUE); | |
258 | delete df; | |
259 | } | |
260 | } | |
261 | } | |
262 | ||
263 | for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) { | |
264 | for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) { | |
265 | if(TEST_TABLE[itable++]) { | |
374ca955 | 266 | logln("Testing dstyle" + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) ); |
b75a7d8f A |
267 | DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc); |
268 | if(df == NULL) { | |
729e4ab9 | 269 | dataerrln(UnicodeString("Could not DF::createDateTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) + "Locale: " + loc.getDisplayName(temp)); |
b75a7d8f | 270 | } else { |
374ca955 A |
271 | test(df, loc); |
272 | delete df; | |
b75a7d8f A |
273 | } |
274 | } | |
275 | } | |
276 | } | |
277 | } | |
278 | ||
279 | void DateFormatRoundTripTest::test(DateFormat *fmt, const Locale &origLocale, UBool timeOnly) | |
280 | { | |
281 | UnicodeString pat; | |
282 | if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) { | |
283 | errln("DateFormat wasn't a SimpleDateFormat"); | |
284 | return; | |
285 | } | |
374ca955 A |
286 | |
287 | UBool isGregorian = FALSE; | |
288 | UErrorCode minStatus = U_ZERO_ERROR; | |
b331163b A |
289 | if(fmt->getCalendar() == NULL) { |
290 | errln((UnicodeString)"DateFormatRoundTripTest::test, DateFormat getCalendar() returns null for " + origLocale.getName()); | |
291 | return; | |
292 | } | |
374ca955 A |
293 | UDate minDate = CalendarTest::minDateOfCalendar(*fmt->getCalendar(), isGregorian, minStatus); |
294 | if(U_FAILURE(minStatus)) { | |
295 | errln((UnicodeString)"Failure getting min date for " + origLocale.getName()); | |
296 | return; | |
297 | } | |
298 | //logln(UnicodeString("Min date is ") + fullFormat(minDate) + " for " + origLocale.getName()); | |
b75a7d8f A |
299 | |
300 | pat = ((SimpleDateFormat*)fmt)->toPattern(pat); | |
301 | ||
302 | // NOTE TO MAINTAINER | |
303 | // This indexOf check into the pattern needs to be refined to ignore | |
304 | // quoted characters. Currently, this isn't a problem with the locale | |
305 | // patterns we have, but it may be a problem later. | |
306 | ||
307 | UBool hasEra = (pat.indexOf(UnicodeString("G")) != -1); | |
46f4442e A |
308 | UBool hasZoneDisplayName = (pat.indexOf(UnicodeString("z")) != -1) || (pat.indexOf(UnicodeString("v")) != -1) |
309 | || (pat.indexOf(UnicodeString("V")) != -1); | |
b75a7d8f A |
310 | |
311 | // Because patterns contain incomplete data representing the Date, | |
312 | // we must be careful of how we do the roundtrip. We start with | |
313 | // a randomly generated Date because they're easier to generate. | |
314 | // From this we get a string. The string is our real starting point, | |
315 | // because this string should parse the same way all the time. Note | |
316 | // that it will not necessarily parse back to the original date because | |
317 | // of incompleteness in patterns. For example, a time-only pattern won't | |
318 | // parse back to the same date. | |
319 | ||
320 | //try { | |
321 | for(int i = 0; i < TRIALS; ++i) { | |
322 | UDate *d = new UDate [DEPTH]; | |
323 | UnicodeString *s = new UnicodeString[DEPTH]; | |
324 | ||
374ca955 A |
325 | if(isGregorian == TRUE) { |
326 | d[0] = generateDate(); | |
327 | } else { | |
328 | d[0] = generateDate(minDate); | |
329 | } | |
b75a7d8f A |
330 | |
331 | UErrorCode status = U_ZERO_ERROR; | |
332 | ||
333 | // We go through this loop until we achieve a match or until | |
334 | // the maximum loop count is reached. We record the points at | |
335 | // which the date and the string starts to match. Once matching | |
336 | // starts, it should continue. | |
337 | int loop; | |
338 | int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime() | |
339 | int smatch = 0; // s[smatch].equals(s[smatch-1]) | |
340 | for(loop = 0; loop < DEPTH; ++loop) { | |
341 | if (loop > 0) { | |
342 | d[loop] = fmt->parse(s[loop-1], status); | |
57a6839d | 343 | failure(status, "fmt->parse", s[loop-1]+" in locale: " + origLocale.getName() + " with pattern: " + pat); |
b75a7d8f A |
344 | status = U_ZERO_ERROR; /* any error would have been reported */ |
345 | } | |
346 | ||
347 | s[loop] = fmt->format(d[loop], s[loop]); | |
348 | ||
374ca955 A |
349 | // For displaying which date is being tested |
350 | //logln(s[loop] + " = " + fullFormat(d[loop])); | |
351 | ||
b75a7d8f A |
352 | if(s[loop].length() == 0) { |
353 | errln("FAIL: fmt->format gave 0-length string in " + pat + " with number " + d[loop] + " in locale " + origLocale.getName()); | |
354 | } | |
355 | ||
356 | if(loop > 0) { | |
357 | if(smatch == 0) { | |
358 | UBool match = s[loop] == s[loop-1]; | |
359 | if(smatch == 0) { | |
360 | if(match) | |
361 | smatch = loop; | |
362 | } | |
363 | else if( ! match) | |
364 | errln("FAIL: String mismatch after match"); | |
365 | } | |
366 | ||
367 | if(dmatch == 0) { | |
368 | // {sfb} watch out here, this might not work | |
369 | UBool match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/; | |
370 | if(dmatch == 0) { | |
371 | if(match) | |
372 | dmatch = loop; | |
373 | } | |
374 | else if( ! match) | |
375 | errln("FAIL: Date mismatch after match"); | |
376 | } | |
377 | ||
378 | if(smatch != 0 && dmatch != 0) | |
379 | break; | |
380 | } | |
381 | } | |
382 | // At this point loop == DEPTH if we've failed, otherwise loop is the | |
383 | // max(smatch, dmatch), that is, the index at which we have string and | |
384 | // date matching. | |
385 | ||
386 | // Date usually matches in 2. Exceptions handled below. | |
387 | int maxDmatch = 2; | |
388 | int maxSmatch = 1; | |
389 | if (dmatch > maxDmatch) { | |
390 | // Time-only pattern with zone information and a starting date in PST. | |
729e4ab9 A |
391 | if(timeOnly && hasZoneDisplayName) { |
392 | int32_t startRaw, startDst; | |
393 | fmt->getTimeZone().getOffset(d[0], FALSE, startRaw, startDst, status); | |
394 | failure(status, "TimeZone::getOffset"); | |
395 | // if the start offset is greater than the offset on Jan 1, 1970 | |
396 | // in PST, then need one more round trip. There are two cases | |
397 | // fall into this category. The start date is 1) DST or | |
398 | // 2) LMT (GMT-07:52:58). | |
399 | if (startRaw + startDst > -28800000) { | |
400 | maxDmatch = 3; | |
401 | maxSmatch = 2; | |
402 | } | |
b75a7d8f A |
403 | } |
404 | } | |
405 | ||
406 | // String usually matches in 1. Exceptions are checked for here. | |
407 | if(smatch > maxSmatch) { // Don't compute unless necessary | |
374ca955 | 408 | UBool in0; |
b75a7d8f A |
409 | // Starts in BC, with no era in pattern |
410 | if( ! hasEra && getField(d[0], UCAL_ERA) == GregorianCalendar::BC) | |
411 | maxSmatch = 2; | |
412 | // Starts in DST, no year in pattern | |
374ca955 | 413 | else if((in0=fmt->getTimeZone().inDaylightTime(d[0], status)) && ! failure(status, "gettingDaylightTime") && |
b75a7d8f A |
414 | pat.indexOf(UnicodeString("yyyy")) == -1) |
415 | maxSmatch = 2; | |
374ca955 A |
416 | // If we start not in DST, but transition into DST |
417 | else if (!in0 && | |
418 | fmt->getTimeZone().inDaylightTime(d[1], status) && !failure(status, "gettingDaylightTime")) | |
419 | maxSmatch = 2; | |
b75a7d8f A |
420 | // Two digit year with no time zone change, |
421 | // unless timezone isn't used or we aren't close to the DST changover | |
422 | else if (pat.indexOf(UnicodeString("y")) != -1 | |
423 | && pat.indexOf(UnicodeString("yyyy")) == -1 | |
424 | && getField(d[0], UCAL_YEAR) | |
425 | != getField(d[dmatch], UCAL_YEAR) | |
426 | && !failure(status, "error status [smatch>maxSmatch]") | |
46f4442e | 427 | && ((hasZoneDisplayName |
b75a7d8f A |
428 | && (fmt->getTimeZone().inDaylightTime(d[0], status) |
429 | == fmt->getTimeZone().inDaylightTime(d[dmatch], status) | |
430 | || getField(d[0], UCAL_MONTH) == UCAL_APRIL | |
431 | || getField(d[0], UCAL_MONTH) == UCAL_OCTOBER)) | |
46f4442e | 432 | || !hasZoneDisplayName) |
b75a7d8f A |
433 | ) |
434 | { | |
435 | maxSmatch = 2; | |
46f4442e A |
436 | } |
437 | // If zone display name is used, fallback format might be used before 1970 | |
438 | else if (hasZoneDisplayName && d[0] < 0) { | |
439 | maxSmatch = 2; | |
440 | } | |
2ca993e8 A |
441 | else if (timeOnly && !isGregorian && hasZoneDisplayName && maxSmatch == 1) { |
442 | int32_t startRaw, startDst; | |
443 | fmt->getTimeZone().getOffset(d[1], FALSE, startRaw, startDst, status); | |
444 | failure(status, "TimeZone::getOffset"); | |
445 | // If the calendar type is not Gregorian and the pattern is time only, | |
446 | // the calendar implementation may use a date before 1970 as day 0. | |
447 | // In this case, time zone offset of the default year might be | |
448 | // different from the one at 1970-01-01 in PST and string match requires | |
449 | // one more iteration. | |
450 | if (startRaw + startDst != -28800000) { | |
451 | maxSmatch = 2; | |
452 | } | |
453 | } | |
b75a7d8f A |
454 | } |
455 | ||
51004dcb A |
456 | /* |
457 | * Special case for Japanese and Buddhist (could have large negative years) | |
458 | * Also, Hebrew calendar need help handling leap month. | |
459 | */ | |
460 | if(dmatch > maxDmatch || smatch > maxSmatch) { | |
374ca955 | 461 | const char *type = fmt->getCalendar()->getType(); |
729e4ab9 | 462 | if(!strcmp(type,"japanese") || (!strcmp(type,"buddhist"))) { |
374ca955 A |
463 | maxSmatch = 4; |
464 | maxDmatch = 4; | |
51004dcb A |
465 | } else if(!strcmp(type,"hebrew")) { |
466 | maxSmatch = 3; | |
467 | maxDmatch = 3; | |
468 | } | |
b75a7d8f | 469 | } |
b75a7d8f | 470 | |
374ca955 A |
471 | // Use @v to see verbose results on successful cases |
472 | UBool fail = (dmatch > maxDmatch || smatch > maxSmatch); | |
473 | if (optionv || fail) { | |
474 | if (fail) { | |
475 | errln(UnicodeString("\nFAIL: Pattern: ") + pat + | |
476 | " in Locale: " + origLocale.getName()); | |
477 | } else { | |
478 | errln(UnicodeString("\nOk: Pattern: ") + pat + | |
479 | " in Locale: " + origLocale.getName()); | |
480 | } | |
b75a7d8f | 481 | |
374ca955 A |
482 | logln("Date iters until match=%d (max allowed=%d), string iters until match=%d (max allowed=%d)", |
483 | dmatch,maxDmatch, smatch, maxSmatch); | |
484 | ||
b75a7d8f A |
485 | for(int j = 0; j <= loop && j < DEPTH; ++j) { |
486 | UnicodeString temp; | |
487 | FieldPosition pos(FieldPosition::DONT_CARE); | |
374ca955 | 488 | errln((j>0?" P> ":" ") + fullFormat(d[j]) + " F> " + |
b75a7d8f A |
489 | escape(s[j], temp) + UnicodeString(" d=") + d[j] + |
490 | (j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") + | |
491 | (j > 0 && s[j] == s[j-1]?" s==":"")); | |
492 | } | |
493 | } | |
494 | delete[] d; | |
495 | delete[] s; | |
496 | } | |
497 | /*} | |
498 | catch (ParseException e) { | |
499 | errln("Exception: " + e.getMessage()); | |
500 | logln(e.toString()); | |
501 | }*/ | |
502 | } | |
503 | ||
374ca955 A |
504 | const UnicodeString& DateFormatRoundTripTest::fullFormat(UDate d) { |
505 | UErrorCode ec = U_ZERO_ERROR; | |
506 | if (dateFormat == 0) { | |
507 | dateFormat = new SimpleDateFormat((UnicodeString)"EEE MMM dd HH:mm:ss.SSS zzz yyyy G", ec); | |
508 | if (U_FAILURE(ec) || dateFormat == 0) { | |
509 | fgStr = "[FAIL: SimpleDateFormat constructor]"; | |
510 | delete dateFormat; | |
511 | dateFormat = 0; | |
512 | return fgStr; | |
513 | } | |
514 | } | |
515 | fgStr.truncate(0); | |
516 | dateFormat->format(d, fgStr); | |
517 | return fgStr; | |
518 | } | |
519 | ||
b75a7d8f A |
520 | /** |
521 | * Return a field of the given date | |
522 | */ | |
523 | int32_t DateFormatRoundTripTest::getField(UDate d, int32_t f) { | |
524 | // Should be synchronized, but we're single threaded so it's ok | |
525 | UErrorCode status = U_ZERO_ERROR; | |
526 | getFieldCal->setTime(d, status); | |
527 | failure(status, "getfieldCal->setTime"); | |
528 | int32_t ret = getFieldCal->get((UCalendarDateFields)f, status); | |
529 | failure(status, "getfieldCal->get"); | |
530 | return ret; | |
531 | } | |
532 | ||
533 | UnicodeString& DateFormatRoundTripTest::escape(const UnicodeString& src, UnicodeString& dst ) | |
534 | { | |
535 | dst.remove(); | |
536 | for (int32_t i = 0; i < src.length(); ++i) { | |
0f5d89e8 A |
537 | UChar32 c = src.char32At(i); |
538 | if (c >= 0x10000) { | |
539 | ++i; | |
540 | } | |
541 | if (c < 0x0080) { | |
b75a7d8f | 542 | dst += c; |
0f5d89e8 | 543 | } else { |
b75a7d8f | 544 | dst += UnicodeString("["); |
0f5d89e8 A |
545 | char buf [12]; |
546 | sprintf(buf, "%#04x", c); | |
b75a7d8f A |
547 | dst += UnicodeString(buf); |
548 | dst += UnicodeString("]"); | |
549 | } | |
550 | } | |
551 | ||
552 | return dst; | |
553 | } | |
554 | ||
374ca955 A |
555 | #define U_MILLIS_PER_YEAR (365.25 * 24 * 60 * 60 * 1000) |
556 | ||
557 | UDate DateFormatRoundTripTest::generateDate(UDate minDate) | |
558 | { | |
559 | // Bring range in conformance to generateDate() below. | |
560 | if(minDate < (U_MILLIS_PER_YEAR * -(4000-1970))) { | |
561 | minDate = (U_MILLIS_PER_YEAR * -(4000-1970)); | |
562 | } | |
563 | for(int i=0;i<8;i++) { | |
564 | double a = randFraction(); | |
565 | ||
566 | // Range from (min) to (8000-1970) AD | |
567 | double dateRange = (0.0 - minDate) + (U_MILLIS_PER_YEAR + (8000-1970)); | |
568 | ||
569 | a *= dateRange; | |
570 | ||
571 | // Now offset from minDate | |
572 | a += minDate; | |
573 | ||
574 | // Last sanity check | |
575 | if(a>=minDate) { | |
576 | return a; | |
577 | } | |
578 | } | |
579 | return minDate; | |
580 | } | |
581 | ||
b75a7d8f A |
582 | UDate DateFormatRoundTripTest::generateDate() |
583 | { | |
584 | double a = randFraction(); | |
585 | ||
586 | // Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years | |
587 | a *= 8000; | |
588 | ||
589 | // Range from (4000-1970) BC to (8000-1970) AD | |
590 | a -= 4000; | |
591 | ||
592 | // Now scale up to ms | |
593 | a *= 365.25 * 24 * 60 * 60 * 1000; | |
594 | ||
595 | //return new Date((long)a); | |
596 | return a; | |
597 | } | |
598 | ||
599 | #endif /* #if !UCONFIG_NO_FORMATTING */ | |
600 | ||
601 | //eof |