]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/tsdate.cpp
1 /********************************************************************
3 * Copyright (c) 1997-2001, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/datefmt.h"
12 #include "unicode/smpdtfmt.h"
18 const double IntlTestDateFormat::ONEYEAR
= 365.25 * ONEDAY
; // Approximate
21 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
24 // par is ignored throughout this file
25 void IntlTestDateFormat::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
27 if (exec
) logln("TestSuite DateFormat");
29 case 0: name
= "GenericTest";
32 fFormat
= DateFormat::createInstance();
33 fTestName
= "createInstance";
35 testFormat(/* par */);
38 case 1: name
= "DefaultLocale";
41 testLocale(/*par, */Locale::getDefault(), "Default Locale");
45 case 2: name
= "TestAvailableLocales";
48 testAvailableLocales(/* par */);
52 case 3: name
= "MonsterTest";
59 default: name
= ""; break;
64 IntlTestDateFormat::testLocale(/*char* par, */const Locale
& locale
, const UnicodeString
& localeName
)
66 DateFormat::EStyle timeStyle
, dateStyle
;
68 // For patterns including only time information and a timezone, it may take
69 // up to three iterations, since the timezone may shift as the year number
70 // is determined. For other patterns, 2 iterations should suffice.
73 for(timeStyle
= (DateFormat::EStyle
)0;
74 timeStyle
< (DateFormat::EStyle
)4;
75 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
77 fTestName
= (UnicodeString
) "Time test " + (int32_t) timeStyle
+ " (" + localeName
+ ")";
78 fFormat
= DateFormat::createTimeInstance(timeStyle
, locale
);
79 testFormat(/* par */);
84 for(dateStyle
= (DateFormat::EStyle
)0;
85 dateStyle
< (DateFormat::EStyle
)4;
86 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
88 fTestName
= (UnicodeString
) "Date test " + (int32_t) dateStyle
+ " (" + localeName
+ ")";
89 fFormat
= DateFormat::createDateInstance(dateStyle
, locale
);
90 testFormat(/* par */);
93 for(dateStyle
= (DateFormat::EStyle
)0;
94 dateStyle
< (DateFormat::EStyle
)4;
95 dateStyle
= (DateFormat::EStyle
) (dateStyle
+1))
97 for(timeStyle
= (DateFormat::EStyle
)0;
98 timeStyle
< (DateFormat::EStyle
)4;
99 timeStyle
= (DateFormat::EStyle
) (timeStyle
+1))
101 fTestName
= (UnicodeString
) "DateTime test " + (int32_t) dateStyle
+ "/" + (int32_t) timeStyle
+ " (" + localeName
+ ")";
102 fFormat
= DateFormat::createDateTimeInstance(dateStyle
, timeStyle
, locale
);
103 testFormat(/* par */);
108 void IntlTestDateFormat::testFormat(/* char* par */)
112 errln("FAIL: DateFormat creation failed");
118 UDate now
= Calendar::getNow();
120 tryDate(1278161801778.0);
121 tryDate(5264498352317.0); // Sunday, October 28, 2136 8:39:12 AM PST
122 tryDate(9516987689250.0); // In the year 2271
124 // Shift 6 months into the future, AT THE SAME TIME OF DAY.
125 // This will test the DST handling.
126 tryDate(now
+ 6.0*30*ONEDAY
);
128 UDate limit
= now
* 10; // Arbitrary limit
129 for (int32_t i
=0; i
<3; ++i
)
130 tryDate(uprv_floor(randDouble() * limit
));
136 IntlTestDateFormat::describeTest()
138 // Assume it's a SimpleDateFormat and get some info
139 SimpleDateFormat
*s
= (SimpleDateFormat
*)fFormat
;
141 logln(fTestName
+ " Pattern " + s
->toPattern(str
));
144 void IntlTestDateFormat::tryDate(UDate theDate
)
146 const int32_t DEPTH
= 10;
148 UnicodeString string
[DEPTH
];
150 int32_t dateMatch
= 0;
151 int32_t stringMatch
= 0;
156 fFormat
->format(theDate
, string
[0]);
158 for (i
=1; i
<DEPTH
; ++i
)
160 UErrorCode status
= U_ZERO_ERROR
;
161 date
[i
] = fFormat
->parse(string
[i
-1], status
);
162 if (U_FAILURE(status
))
165 errln("**** FAIL: Parse of " + prettify(string
[i
-1], FALSE
) + " failed.");
169 fFormat
->format(date
[i
], string
[i
]);
170 if (dateMatch
== 0 && date
[i
] == date
[i
-1])
172 else if (dateMatch
> 0 && date
[i
] != date
[i
-1])
175 errln("**** FAIL: Date mismatch after match for " + string
[i
]);
179 if (stringMatch
== 0 && string
[i
] == string
[i
-1])
181 else if (stringMatch
> 0 && string
[i
] != string
[i
-1])
184 errln("**** FAIL: String mismatch after match for " + string
[i
]);
188 if (dateMatch
> 0 && stringMatch
> 0)
194 if (stringMatch
> fLimit
|| dateMatch
> fLimit
)
197 errln((UnicodeString
)"**** FAIL: No string and/or date match within " + fLimit
198 + " iterations for the Date " + string
[0] + "\t(" + theDate
+ ").");
204 for (int32_t k
=0; k
<=i
; ++k
)
206 logln((UnicodeString
)"" + k
+ ": " + date
[k
] + " F> " +
212 // Return a random double from 0.01 to 1, inclusive
213 double IntlTestDateFormat::randDouble()
215 // Assume 8-bit (or larger) rand values. Also assume
216 // that the system rand() function is very poor, which it always is.
219 char* poke
= (char*)&d
;
222 for (i
=0; i
< sizeof(double); ++i
)
224 poke
[i
] = (char)(rand() & 0xFF);
226 } while (uprv_isNaN(d
) || uprv_isInfinite(d
));
232 double e
= uprv_floor(uprv_log10(d
));
234 d
*= uprv_pow10((int32_t)(-e
-2));
236 d
/= uprv_pow10((int32_t)(e
+1));
238 // While this is not a real normalized number make another one.
239 } while (uprv_isNaN(d
) || uprv_isInfinite(d
)
240 || !((-DBL_MAX
< d
&& d
< DBL_MAX
) || (d
< -DBL_MIN
&& DBL_MIN
< d
)));
244 void IntlTestDateFormat::testAvailableLocales(/* char* par */)
247 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
248 logln((UnicodeString
)"" + count
+ " available locales");
249 if (locales
&& count
)
253 for (int32_t i
=0; i
<count
; ++i
)
255 if (i
!=0) all
+= ", ";
256 all
+= locales
[i
].getName();
260 else errln((UnicodeString
)"**** FAIL: Zero available locales or null array pointer");
263 void IntlTestDateFormat::monsterTest(/*char *par*/)
266 const Locale
* locales
= DateFormat::getAvailableLocales(count
);
267 if (locales
&& count
)
269 if (quick
&& count
> 3) {
270 logln("quick test: testing just 3 locales!");
273 for (int32_t i
=0; i
<count
; ++i
)
275 UnicodeString name
= UnicodeString(locales
[i
].getName(), "");
276 logln((UnicodeString
)"Testing " + name
+ "...");
277 testLocale(/*par, */locales
[i
], name
);
282 #endif /* #if !UCONFIG_NO_FORMATTING */