]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsdate.cpp
1 /***********************************************************************
2 * Copyright (c) 1997-2009, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ***********************************************************************/
6 #include "unicode/utypes.h"
8 #if !UCONFIG_NO_FORMATTING
10 #include "unicode/datefmt.h"
11 #include "unicode/smpdtfmt.h"
19 const double IntlTestDateFormat::ONEYEAR
= 365.25 * ONEDAY
; // Approximate
21 IntlTestDateFormat::~IntlTestDateFormat() {}
24 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
27 // par is ignored throughout this file
28 void IntlTestDateFormat::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
30 if (exec
) logln("TestSuite DateFormat");
32 case 0: name
= "GenericTest";
35 fFormat
= DateFormat::createInstance();
36 fTestName
= "createInstance";
38 testFormat(/* par */);
41 case 1: name
= "DefaultLocale";
44 testLocale(/*par, */Locale::getDefault(), "Default Locale");
48 case 2: name
= "TestAvailableLocales";
51 testAvailableLocales(/* par */);
55 case 3: name
= "MonsterTest";
62 default: name
= ""; break;
67 IntlTestDateFormat::testLocale(/*char* par, */const Locale
& locale
, const UnicodeString
& localeName
)
69 DateFormat::EStyle timeStyle
, dateStyle
;
71 // For patterns including only time information and a timezone, it may take
72 // up to three iterations, since the timezone may shift as the year number
73 // is determined. For other patterns, 2 iterations should suffice.
76 for(timeStyle
= (DateFormat::EStyle
)0;
77 timeStyle
< (DateFormat::EStyle
)4;
78 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
80 fTestName
= (UnicodeString
) "Time test " + (int32_t) timeStyle
+ " (" + localeName
+ ")";
81 fFormat
= DateFormat::createTimeInstance(timeStyle
, locale
);
82 testFormat(/* par */);
87 for(dateStyle
= (DateFormat::EStyle
)0;
88 dateStyle
< (DateFormat::EStyle
)4;
89 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
91 fTestName
= (UnicodeString
) "Date test " + (int32_t) dateStyle
+ " (" + localeName
+ ")";
92 fFormat
= DateFormat::createDateInstance(dateStyle
, locale
);
93 testFormat(/* par */);
96 for(dateStyle
= (DateFormat::EStyle
)0;
97 dateStyle
< (DateFormat::EStyle
)4;
98 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
100 for(timeStyle
= (DateFormat::EStyle
)0;
101 timeStyle
< (DateFormat::EStyle
)4;
102 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
104 fTestName
= (UnicodeString
) "DateTime test " + (int32_t) dateStyle
+ "/" + (int32_t) timeStyle
+ " (" + localeName
+ ")";
105 fFormat
= DateFormat::createDateTimeInstance(dateStyle
, timeStyle
, locale
);
106 testFormat(/* par */);
111 void IntlTestDateFormat::testFormat(/* char* par */)
115 dataerrln("FAIL: DateFormat creation failed");
121 UDate now
= Calendar::getNow();
123 tryDate(1278161801778.0);
124 tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
125 tryDate(9516987689250.0); // In the year 2271
127 // Shift 6 months into the future, AT THE SAME TIME OF DAY.
128 // This will test the DST handling.
129 tryDate(now
+ 6.0*30*ONEDAY
);
131 UDate limit
= now
* 10; // Arbitrary limit
132 for (int32_t i
=0; i
<3; ++i
)
133 tryDate(uprv_floor(randDouble() * limit
));
139 IntlTestDateFormat::describeTest()
141 // Assume it's a SimpleDateFormat and get some info
142 SimpleDateFormat
*s
= (SimpleDateFormat
*)fFormat
;
144 logln(fTestName
+ " Pattern " + s
->toPattern(str
));
147 void IntlTestDateFormat::tryDate(UDate theDate
)
149 const int32_t DEPTH
= 10;
151 UnicodeString string
[DEPTH
];
153 int32_t dateMatch
= 0;
154 int32_t stringMatch
= 0;
156 #if defined (U_CAL_DEBUG)
162 fFormat
->format(theDate
, string
[0]);
164 for (i
=1; i
<DEPTH
; ++i
)
166 UErrorCode status
= U_ZERO_ERROR
;
167 date
[i
] = fFormat
->parse(string
[i
-1], status
);
168 if (U_FAILURE(status
))
171 errln("**** FAIL: Parse of " + prettify(string
[i
-1], FALSE
) + " failed.");
175 fFormat
->format(date
[i
], string
[i
]);
176 if (dateMatch
== 0 && date
[i
] == date
[i
-1])
178 else if (dateMatch
> 0 && date
[i
] != date
[i
-1])
181 errln("**** FAIL: Date mismatch after match for " + string
[i
]);
185 if (stringMatch
== 0 && string
[i
] == string
[i
-1])
187 else if (stringMatch
> 0 && string
[i
] != string
[i
-1])
190 errln("**** FAIL: String mismatch after match for " + string
[i
]);
194 if (dateMatch
> 0 && stringMatch
> 0)
200 if (stringMatch
> fLimit
|| dateMatch
> fLimit
)
203 errln((UnicodeString
)"**** FAIL: No string and/or date match within " + fLimit
204 + " iterations for the Date " + string
[0] + "\t(" + theDate
+ ").");
210 for (int32_t k
=0; k
<=i
; ++k
)
212 logln((UnicodeString
)"" + k
+ ": " + date
[k
] + " F> " +
218 // Return a random double from 0.01 to 1, inclusive
219 double IntlTestDateFormat::randDouble()
221 // Assume 8-bit (or larger) rand values. Also assume
222 // that the system rand() function is very poor, which it always is.
225 char* poke
= (char*)&d
;
228 for (i
=0; i
< sizeof(double); ++i
)
230 poke
[i
] = (char)(rand() & 0xFF);
232 } while (uprv_isNaN(d
) || uprv_isInfinite(d
));
238 double e
= uprv_floor(log10(d
));
240 d
*= uprv_pow10((int32_t)(-e
-2));
242 d
/= uprv_pow10((int32_t)(e
+1));
244 // While this is not a real normalized number make another one.
245 } while (uprv_isNaN(d
) || uprv_isInfinite(d
)
246 || !((-DBL_MAX
< d
&& d
< DBL_MAX
) || (d
< -DBL_MIN
&& DBL_MIN
< d
)));
250 void IntlTestDateFormat::testAvailableLocales(/* char* par */)
253 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
254 logln((UnicodeString
)"" + count
+ " available locales");
255 if (locales
&& count
)
259 for (int32_t i
=0; i
<count
; ++i
)
261 if (i
!=0) all
+= ", ";
262 all
+= locales
[i
].getName();
266 else dataerrln((UnicodeString
)"**** FAIL: Zero available locales or null array pointer");
269 void IntlTestDateFormat::monsterTest(/*char *par*/)
272 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
273 if (locales
&& count
)
275 if (quick
&& count
> 3) {
276 logln("quick test: testing just 3 locales!");
279 for (int32_t i
=0; i
<count
; ++i
)
281 UnicodeString name
= UnicodeString(locales
[i
].getName(), "");
282 logln((UnicodeString
)"Testing " + name
+ "...");
283 testLocale(/*par, */locales
[i
], name
);
288 #endif /* #if !UCONFIG_NO_FORMATTING */