]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dtfmapts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2005, 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 default: name
= ""; break;
61 * Test that the equals method works correctly.
63 void IntlTestDateFormatAPI::TestEquals(void)
65 UErrorCode status
= U_ZERO_ERROR
;
66 // Create two objects at different system times
67 DateFormat
*a
= DateFormat::createInstance();
68 UDate start
= Calendar::getNow();
69 while (Calendar::getNow() == start
) ; // Wait for time to change
70 DateFormat
*b
= DateFormat::createInstance();
72 if (a
== NULL
|| b
== NULL
){
73 dataerrln("Error calling DateFormat::createInstance()");
80 errln("FAIL: DateFormat objects created at different times are unequal.");
82 if (b
->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
84 double ONE_YEAR
= 365*24*60*60*1000.0;
85 ((SimpleDateFormat
*)b
)->set2DigitYearStart(start
+ 50*ONE_YEAR
, status
);
86 if (U_FAILURE(status
))
87 errln("FAIL: setTwoDigitStartDate failed.");
89 errln("FAIL: DateFormat objects with different two digit start dates are equal.");
96 * This test checks various generic API methods in DateFormat to achieve 100%
99 void IntlTestDateFormatAPI::testAPI(/* char* par */)
101 UErrorCode status
= U_ZERO_ERROR
;
103 // ======= Test constructors
105 logln("Testing DateFormat constructors");
107 DateFormat
*def
= DateFormat::createInstance();
108 DateFormat
*fr
= DateFormat::createTimeInstance(DateFormat::FULL
, Locale::getFrench());
109 DateFormat
*it
= DateFormat::createDateInstance(DateFormat::MEDIUM
, Locale::getItalian());
110 DateFormat
*de
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
, Locale::getGerman());
112 if (def
== NULL
|| fr
== NULL
|| it
== NULL
|| de
== NULL
){
113 dataerrln("Error creating instnaces.");
116 // ======= Test equality
117 if (fr
!= NULL
&& def
!= NULL
)
119 logln("Testing equality operator");
122 errln("ERROR: == failed");
126 // ======= Test various format() methods
127 if (fr
!= NULL
&& it
!= NULL
&& de
!= NULL
)
129 logln("Testing various format() methods");
131 UDate d
= 837039928046.0;
132 Formattable
fD(d
, Formattable::kIsDate
);
134 UnicodeString res1
, res2
, res3
;
135 FieldPosition
pos1(0), pos2(0);
137 status
= U_ZERO_ERROR
;
138 res1
= fr
->format(d
, res1
, pos1
, status
);
139 if(U_FAILURE(status
)) {
140 errln("ERROR: format() failed (French)");
142 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
144 res2
= it
->format(d
, res2
, pos2
);
145 logln( (UnicodeString
) "" + d
+ " formatted to " + res2
);
147 res3
= de
->format(d
, res3
);
148 logln( (UnicodeString
) "" + d
+ " formatted to " + res3
);
151 // ======= Test parse()
154 logln("Testing parse()");
156 UnicodeString
text("02/03/76 2:50 AM, CST");
158 UDate result2
, result3
;
159 ParsePosition
pos(0), pos01(0);
160 def
->parseObject(text
, result1
, pos
);
161 if(result1
.getType() != Formattable::kDate
) {
162 errln("ERROR: parseObject() failed for " + text
);
164 logln(text
+ " parsed into " + result1
.getDate());
166 status
= U_ZERO_ERROR
;
167 result2
= def
->parse(text
, status
);
168 if(U_FAILURE(status
)) {
169 errln("ERROR: parse() failed, stopping testing");
172 logln(text
+ " parsed into " + result2
);
174 result3
= def
->parse(text
, pos01
);
175 logln(text
+ " parsed into " + result3
);
178 // ======= Test getters and setters
179 if (fr
!= NULL
&& it
!= NULL
&& de
!= NULL
)
181 logln("Testing getters and setters");
184 const Locale
*locales
= DateFormat::getAvailableLocales(count
);
185 logln((UnicodeString
) "Got " + count
+ " locales" );
186 for(int32_t i
= 0; i
< count
; i
++) {
188 name
= locales
[i
].getName();
192 fr
->setLenient(it
->isLenient());
193 if(fr
->isLenient() != it
->isLenient()) {
194 errln("ERROR: setLenient() failed");
197 const Calendar
*cal
= def
->getCalendar();
198 Calendar
*newCal
= cal
->clone();
199 de
->adoptCalendar(newCal
);
200 it
->setCalendar(*newCal
);
201 if( *(de
->getCalendar()) != *(it
->getCalendar())) {
202 errln("ERROR: adopt or set Calendar() failed");
205 const NumberFormat
*nf
= def
->getNumberFormat();
206 NumberFormat
*newNf
= (NumberFormat
*) nf
->clone();
207 de
->adoptNumberFormat(newNf
);
208 it
->setNumberFormat(*newNf
);
209 if( *(de
->getNumberFormat()) != *(it
->getNumberFormat())) {
210 errln("ERROR: adopt or set NumberFormat() failed");
213 const TimeZone
& tz
= def
->getTimeZone();
214 TimeZone
*newTz
= tz
.clone();
215 de
->adoptTimeZone(newTz
);
216 it
->setTimeZone(*newTz
);
217 if( de
->getTimeZone() != it
->getTimeZone()) {
218 errln("ERROR: adopt or set TimeZone() failed");
221 // ======= Test getStaticClassID()
223 logln("Testing getStaticClassID()");
225 status
= U_ZERO_ERROR
;
226 DateFormat
*test
= new SimpleDateFormat(status
);
227 if(U_FAILURE(status
)) {
228 errln("ERROR: Couldn't create a DateFormat");
231 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
232 errln("ERROR: getDynamicClassID() didn't return the expected value");
243 * Test hiding of parse() and format() APIs in the Format hierarchy.
244 * We test the entire hierarchy, even though this test is located in
245 * the DateFormat API test.
248 IntlTestDateFormatAPI::TestNameHiding(void) {
250 // N.B.: This test passes if it COMPILES, since it's a test of
251 // compile-time name hiding.
253 UErrorCode status
= U_ZERO_ERROR
;
254 Formattable
dateObj(0, Formattable::kIsDate
);
255 Formattable
numObj(3.1415926535897932384626433832795);
261 // DateFormat calling Format API
264 DateFormat
*dateFmt
= DateFormat::createInstance();
266 dateFmt
->format(dateObj
, str
, status
);
267 dateFmt
->format(dateObj
, str
, fpos
, status
);
270 errln("FAIL: Can't create DateFormat");
274 // SimpleDateFormat calling Format & DateFormat API
276 logln("SimpleDateFormat");
277 status
= U_ZERO_ERROR
;
278 SimpleDateFormat
sdf(status
);
280 sdf
.format(dateObj
, str
, status
);
281 sdf
.format(dateObj
, str
, fpos
, status
);
283 sdf
.format((UDate
)0, str
, fpos
);
284 sdf
.format((UDate
)0, str
);
285 sdf
.parse(str
, status
);
286 sdf
.parse(str
, ppos
);
289 // NumberFormat calling Format API
291 logln("NumberFormat");
292 status
= U_ZERO_ERROR
;
293 NumberFormat
*fmt
= NumberFormat::createInstance(status
);
295 fmt
->format(numObj
, str
, status
);
296 fmt
->format(numObj
, str
, fpos
, status
);
299 errln("FAIL: Can't create NumberFormat()");
303 // DecimalFormat calling Format & NumberFormat API
305 logln("DecimalFormat");
306 status
= U_ZERO_ERROR
;
307 DecimalFormat
fmt(status
);
308 if(U_SUCCESS(status
)) {
310 fmt
.format(numObj
, str
, status
);
311 fmt
.format(numObj
, str
, fpos
, status
);
313 fmt
.format(2.71828, str
);
314 fmt
.format((int32_t)1234567, str
);
315 fmt
.format(1.41421, str
, fpos
);
316 fmt
.format((int32_t)9876543, str
, fpos
);
317 fmt
.parse(str
, obj
, ppos
);
318 fmt
.parse(str
, obj
, status
);
320 errln("FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status
));
324 // ChoiceFormat calling Format & NumberFormat API
326 logln("ChoiceFormat");
327 status
= U_ZERO_ERROR
;
328 ChoiceFormat
fmt("0#foo|1#foos|2#foos", status
);
330 fmt
.format(numObj
, str
, status
);
331 fmt
.format(numObj
, str
, fpos
, status
);
333 fmt
.format(2.71828, str
);
334 fmt
.format((int32_t)1234567, str
);
335 fmt
.format(1.41421, str
, fpos
);
336 fmt
.format((int32_t)9876543, str
, fpos
);
337 fmt
.parse(str
, obj
, ppos
);
338 fmt
.parse(str
, obj
, status
);
341 // MessageFormat calling Format API
343 logln("MessageFormat");
344 status
= U_ZERO_ERROR
;
345 MessageFormat
fmt("", status
);
347 // We use dateObj, which MessageFormat should reject.
348 // We're testing name hiding, not the format method.
349 fmt
.format(dateObj
, str
, status
);
350 fmt
.format(dateObj
, str
, fpos
, status
);
354 #endif /* #if !UCONFIG_NO_FORMATTING */