]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dtfmapts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2011, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/datefmt.h"
14 #include "unicode/smpdtfmt.h"
15 #include "unicode/decimfmt.h"
16 #include "unicode/choicfmt.h"
17 #include "unicode/msgfmt.h"
20 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
21 // try to test the full functionality. It just calls each function in the class and
22 // verifies that it works on a basic level.
24 void IntlTestDateFormatAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
26 if (exec
) logln("TestSuite DateFormatAPI");
28 case 0: name
= "DateFormat API test";
30 logln("DateFormat API test---"); logln("");
31 UErrorCode status
= U_ZERO_ERROR
;
33 Locale::setDefault(Locale::getEnglish(), status
);
34 if(U_FAILURE(status
)) {
35 errln("ERROR: Could not set default locale, test may not give correct results");
38 Locale::setDefault(saveLocale
, status
);
42 case 1: name
= "TestEquals";
44 logln("TestEquals---"); logln("");
49 case 2: name
= "TestNameHiding";
51 logln("TestNameHiding---"); logln("");
56 case 3: name
= "TestCoverage";
58 logln("TestCoverage---"); logln("");
63 default: name
= ""; break;
68 * Add better code coverage.
70 void IntlTestDateFormatAPI::TestCoverage(void)
72 const char *LOCALES
[] = {
73 "zh_CN@calendar=chinese",
74 "cop_EG@calendar=coptic",
75 "hi_IN@calendar=indian",
76 "am_ET@calendar=ethiopic"
78 int32_t numOfLocales
= 4;
80 for (int32_t i
= 0; i
< numOfLocales
; i
++) {
81 DateFormat
*df
= DateFormat::createDateTimeInstance(DateFormat::kMedium
, DateFormat::kMedium
, Locale(LOCALES
[i
]));
83 dataerrln("Error creating DateFormat instances.");
90 * Test that the equals method works correctly.
92 void IntlTestDateFormatAPI::TestEquals(void)
94 UErrorCode status
= U_ZERO_ERROR
;
95 // Create two objects at different system times
96 DateFormat
*a
= DateFormat::createInstance();
97 UDate start
= Calendar::getNow();
98 while (Calendar::getNow() == start
) ; // Wait for time to change
99 DateFormat
*b
= DateFormat::createInstance();
101 if (a
== NULL
|| b
== NULL
){
102 dataerrln("Error calling DateFormat::createInstance()");
109 errln("FAIL: DateFormat objects created at different times are unequal.");
111 SimpleDateFormat
*sdtfmt
= dynamic_cast<SimpleDateFormat
*>(b
);
114 double ONE_YEAR
= 365*24*60*60*1000.0;
115 sdtfmt
->set2DigitYearStart(start
+ 50*ONE_YEAR
, status
);
116 if (U_FAILURE(status
))
117 errln("FAIL: setTwoDigitStartDate failed.");
119 errln("FAIL: DateFormat objects with different two digit start dates are equal.");
126 * This test checks various generic API methods in DateFormat to achieve 100%
129 void IntlTestDateFormatAPI::testAPI(/* char* par */)
131 UErrorCode status
= U_ZERO_ERROR
;
133 // ======= Test constructors
135 logln("Testing DateFormat constructors");
137 DateFormat
*def
= DateFormat::createInstance();
138 DateFormat
*fr
= DateFormat::createTimeInstance(DateFormat::FULL
, Locale::getFrench());
139 DateFormat
*it
= DateFormat::createDateInstance(DateFormat::MEDIUM
, Locale::getItalian());
140 DateFormat
*de
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
, Locale::getGerman());
142 if (def
== NULL
|| fr
== NULL
|| it
== NULL
|| de
== NULL
){
143 dataerrln("Error creating DateFormat instances.");
146 // ======= Test equality
147 if (fr
!= NULL
&& def
!= NULL
)
149 logln("Testing equality operator");
152 errln("ERROR: == failed");
156 // ======= Test various format() methods
157 if (fr
!= NULL
&& it
!= NULL
&& de
!= NULL
)
159 logln("Testing various format() methods");
161 UDate d
= 837039928046.0;
162 Formattable
fD(d
, Formattable::kIsDate
);
164 UnicodeString res1
, res2
, res3
;
165 FieldPosition
pos1(0), pos2(0);
167 status
= U_ZERO_ERROR
;
168 res1
= fr
->format(d
, res1
, pos1
, status
);
169 if(U_FAILURE(status
)) {
170 errln("ERROR: format() failed (French)");
172 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
174 res2
= it
->format(d
, res2
, pos2
);
175 logln( (UnicodeString
) "" + d
+ " formatted to " + res2
);
177 res3
= de
->format(d
, res3
);
178 logln( (UnicodeString
) "" + d
+ " formatted to " + res3
);
181 // ======= Test parse()
184 logln("Testing parse()");
186 UnicodeString
text("02/03/76 2:50 AM, CST");
188 UDate result2
, result3
;
189 ParsePosition
pos(0), pos01(0);
190 def
->parseObject(text
, result1
, pos
);
191 if(result1
.getType() != Formattable::kDate
) {
192 errln("ERROR: parseObject() failed for " + text
);
194 logln(text
+ " parsed into " + result1
.getDate());
196 status
= U_ZERO_ERROR
;
197 result2
= def
->parse(text
, status
);
198 if(U_FAILURE(status
)) {
199 errln("ERROR: parse() failed, stopping testing");
202 logln(text
+ " parsed into " + result2
);
204 result3
= def
->parse(text
, pos01
);
205 logln(text
+ " parsed into " + result3
);
208 // ======= Test getters and setters
209 if (fr
!= NULL
&& it
!= NULL
&& de
!= NULL
)
211 logln("Testing getters and setters");
214 const Locale
*locales
= DateFormat::getAvailableLocales(count
);
215 logln((UnicodeString
) "Got " + count
+ " locales" );
216 for(int32_t i
= 0; i
< count
; i
++) {
218 name
= locales
[i
].getName();
222 fr
->setLenient(it
->isLenient());
223 if(fr
->isLenient() != it
->isLenient()) {
224 errln("ERROR: setLenient() failed");
227 const Calendar
*cal
= def
->getCalendar();
228 Calendar
*newCal
= cal
->clone();
229 de
->adoptCalendar(newCal
);
230 it
->setCalendar(*newCal
);
231 if( *(de
->getCalendar()) != *(it
->getCalendar())) {
232 errln("ERROR: adopt or set Calendar() failed");
235 const NumberFormat
*nf
= def
->getNumberFormat();
236 NumberFormat
*newNf
= (NumberFormat
*) nf
->clone();
237 de
->adoptNumberFormat(newNf
);
238 it
->setNumberFormat(*newNf
);
239 if( *(de
->getNumberFormat()) != *(it
->getNumberFormat())) {
240 errln("ERROR: adopt or set NumberFormat() failed");
243 const TimeZone
& tz
= def
->getTimeZone();
244 TimeZone
*newTz
= tz
.clone();
245 de
->adoptTimeZone(newTz
);
246 it
->setTimeZone(*newTz
);
247 if( de
->getTimeZone() != it
->getTimeZone()) {
248 errln("ERROR: adopt or set TimeZone() failed");
251 // ======= Test getStaticClassID()
253 logln("Testing getStaticClassID()");
255 status
= U_ZERO_ERROR
;
256 DateFormat
*test
= new SimpleDateFormat(status
);
257 if(U_FAILURE(status
)) {
258 dataerrln("ERROR: Couldn't create a DateFormat - %s", u_errorName(status
));
261 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
262 errln("ERROR: getDynamicClassID() didn't return the expected value");
273 * Test hiding of parse() and format() APIs in the Format hierarchy.
274 * We test the entire hierarchy, even though this test is located in
275 * the DateFormat API test.
278 IntlTestDateFormatAPI::TestNameHiding(void) {
280 // N.B.: This test passes if it COMPILES, since it's a test of
281 // compile-time name hiding.
283 UErrorCode status
= U_ZERO_ERROR
;
284 Formattable
dateObj(0, Formattable::kIsDate
);
285 Formattable
numObj(3.1415926535897932384626433832795);
291 // DateFormat calling Format API
294 DateFormat
*dateFmt
= DateFormat::createInstance();
296 dateFmt
->format(dateObj
, str
, status
);
297 dateFmt
->format(dateObj
, str
, fpos
, status
);
300 dataerrln("FAIL: Can't create DateFormat");
304 // SimpleDateFormat calling Format & DateFormat API
306 logln("SimpleDateFormat");
307 status
= U_ZERO_ERROR
;
308 SimpleDateFormat
sdf(status
);
309 if (U_SUCCESS(status
)) {
311 sdf
.format(dateObj
, str
, status
);
312 sdf
.format(dateObj
, str
, fpos
, status
);
314 sdf
.format((UDate
)0, str
, fpos
);
315 sdf
.format((UDate
)0, str
);
316 sdf
.parse(str
, status
);
317 sdf
.parse(str
, ppos
);
318 sdf
.getNumberFormat();
320 dataerrln("FAIL: Can't create SimpleDateFormat() - %s", u_errorName(status
));
324 // NumberFormat calling Format API
326 logln("NumberFormat");
327 status
= U_ZERO_ERROR
;
328 NumberFormat
*fmt
= NumberFormat::createInstance(status
);
330 fmt
->format(numObj
, str
, status
);
331 fmt
->format(numObj
, str
, fpos
, status
);
334 dataerrln("FAIL: Can't create NumberFormat()");
338 // DecimalFormat calling Format & NumberFormat API
340 logln("DecimalFormat");
341 status
= U_ZERO_ERROR
;
342 DecimalFormat
fmt(status
);
343 if(U_SUCCESS(status
)) {
345 fmt
.format(numObj
, str
, status
);
346 fmt
.format(numObj
, str
, fpos
, status
);
348 fmt
.format(2.71828, str
);
349 fmt
.format((int32_t)1234567, str
);
350 fmt
.format(1.41421, str
, fpos
);
351 fmt
.format((int32_t)9876543, str
, fpos
);
352 fmt
.parse(str
, obj
, ppos
);
353 fmt
.parse(str
, obj
, status
);
355 errcheckln(status
, "FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status
));
359 // ChoiceFormat calling Format & NumberFormat API
361 logln("ChoiceFormat");
362 status
= U_ZERO_ERROR
;
363 ChoiceFormat
fmt("0#foo|1#foos|2#foos", status
);
365 fmt
.format(numObj
, str
, status
);
366 fmt
.format(numObj
, str
, fpos
, status
);
368 fmt
.format(2.71828, str
);
369 fmt
.format((int32_t)1234567, str
);
370 fmt
.format(1.41421, str
, fpos
);
371 fmt
.format((int32_t)9876543, str
, fpos
);
372 fmt
.parse(str
, obj
, ppos
);
373 fmt
.parse(str
, obj
, status
);
376 // MessageFormat calling Format API
378 logln("MessageFormat");
379 status
= U_ZERO_ERROR
;
380 MessageFormat
fmt("", status
);
382 // We use dateObj, which MessageFormat should reject.
383 // We're testing name hiding, not the format method.
384 fmt
.format(dateObj
, str
, status
);
385 fmt
.format(dateObj
, str
, fpos
, status
);
389 #endif /* #if !UCONFIG_NO_FORMATTING */