]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dtfmapts.cpp
1 /***********************************************************************
3 * Copyright (c) 1997-2004, 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
;
32 Locale::setDefault(Locale::getEnglish(), status
);
33 if(U_FAILURE(status
)) {
34 errln("ERROR: Could not set default locale, test may not give correct results");
40 case 1: name
= "TestEquals";
42 logln("TestEquals---"); logln("");
47 case 2: name
= "TestNameHiding";
49 logln("TestNameHiding---"); logln("");
54 default: name
= ""; break;
59 * Test that the equals method works correctly.
61 void IntlTestDateFormatAPI::TestEquals(void)
63 UErrorCode status
= U_ZERO_ERROR
;
64 // Create two objects at different system times
65 DateFormat
*a
= DateFormat::createInstance();
66 UDate start
= Calendar::getNow();
67 while (Calendar::getNow() == start
) ; // Wait for time to change
68 DateFormat
*b
= DateFormat::createInstance();
71 errln("FAIL: DateFormat objects created at different times are unequal.");
73 if (b
->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
75 double ONE_YEAR
= 365*24*60*60*1000.0;
76 ((SimpleDateFormat
*)b
)->set2DigitYearStart(start
+ 50*ONE_YEAR
, status
);
77 if (U_FAILURE(status
))
78 errln("FAIL: setTwoDigitStartDate failed.");
80 errln("FAIL: DateFormat objects with different two digit start dates are equal.");
87 * This test checks various generic API methods in DateFormat to achieve 100%
90 void IntlTestDateFormatAPI::testAPI(/* char* par */)
92 UErrorCode status
= U_ZERO_ERROR
;
94 // ======= Test constructors
96 logln("Testing DateFormat constructors");
98 DateFormat
*def
= DateFormat::createInstance();
99 DateFormat
*fr
= DateFormat::createTimeInstance(DateFormat::FULL
, Locale::getFrench());
100 DateFormat
*it
= DateFormat::createDateInstance(DateFormat::MEDIUM
, Locale::getItalian());
101 DateFormat
*de
= DateFormat::createDateTimeInstance(DateFormat::LONG
, DateFormat::LONG
, Locale::getGerman());
103 // ======= Test equality
105 logln("Testing equality operator");
108 errln("ERROR: == failed");
111 // ======= Test various format() methods
113 logln("Testing various format() methods");
115 UDate d
= 837039928046.0;
116 Formattable
fD(d
, Formattable::kIsDate
);
118 UnicodeString res1
, res2
, res3
;
119 FieldPosition
pos1(0), pos2(0);
121 status
= U_ZERO_ERROR
;
122 res1
= fr
->format(d
, res1
, pos1
, status
);
123 if(U_FAILURE(status
)) {
124 errln("ERROR: format() failed (French)");
126 logln( (UnicodeString
) "" + d
+ " formatted to " + res1
);
128 res2
= it
->format(d
, res2
, pos2
);
129 logln( (UnicodeString
) "" + d
+ " formatted to " + res2
);
131 res3
= de
->format(d
, res3
);
132 logln( (UnicodeString
) "" + d
+ " formatted to " + res3
);
134 // ======= Test parse()
136 logln("Testing parse()");
138 UnicodeString
text("02/03/76 2:50 AM, CST");
140 UDate result2
, result3
;
141 ParsePosition
pos(0), pos01(0);
142 def
->parseObject(text
, result1
, pos
);
143 if(result1
.getType() != Formattable::kDate
) {
144 errln("ERROR: parseObject() failed for " + text
);
146 logln(text
+ " parsed into " + result1
.getDate());
148 status
= U_ZERO_ERROR
;
149 result2
= def
->parse(text
, status
);
150 if(U_FAILURE(status
)) {
151 errln("ERROR: parse() failed, stopping testing");
154 logln(text
+ " parsed into " + result2
);
156 result3
= def
->parse(text
, pos01
);
157 logln(text
+ " parsed into " + result3
);
160 // ======= Test getters and setters
162 logln("Testing getters and setters");
165 const Locale
*locales
= DateFormat::getAvailableLocales(count
);
166 logln((UnicodeString
) "Got " + count
+ " locales" );
167 for(int32_t i
= 0; i
< count
; i
++) {
169 name
= locales
[i
].getName();
173 fr
->setLenient(it
->isLenient());
174 if(fr
->isLenient() != it
->isLenient()) {
175 errln("ERROR: setLenient() failed");
178 const Calendar
*cal
= def
->getCalendar();
179 Calendar
*newCal
= cal
->clone();
180 de
->adoptCalendar(newCal
);
181 it
->setCalendar(*newCal
);
182 if( *(de
->getCalendar()) != *(it
->getCalendar())) {
183 errln("ERROR: adopt or set Calendar() failed");
186 const NumberFormat
*nf
= def
->getNumberFormat();
187 NumberFormat
*newNf
= (NumberFormat
*) nf
->clone();
188 de
->adoptNumberFormat(newNf
);
189 it
->setNumberFormat(*newNf
);
190 if( *(de
->getNumberFormat()) != *(it
->getNumberFormat())) {
191 errln("ERROR: adopt or set NumberFormat() failed");
194 const TimeZone
& tz
= def
->getTimeZone();
195 TimeZone
*newTz
= tz
.clone();
196 de
->adoptTimeZone(newTz
);
197 it
->setTimeZone(*newTz
);
198 if( de
->getTimeZone() != it
->getTimeZone()) {
199 errln("ERROR: adopt or set TimeZone() failed");
202 // ======= Test getStaticClassID()
204 logln("Testing getStaticClassID()");
206 status
= U_ZERO_ERROR
;
207 DateFormat
*test
= new SimpleDateFormat(status
);
208 if(U_FAILURE(status
)) {
209 errln("ERROR: Couldn't create a DateFormat");
212 if(test
->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
213 errln("ERROR: getDynamicClassID() didn't return the expected value");
224 * Test hiding of parse() and format() APIs in the Format hierarchy.
225 * We test the entire hierarchy, even though this test is located in
226 * the DateFormat API test.
229 IntlTestDateFormatAPI::TestNameHiding(void) {
231 // N.B.: This test passes if it COMPILES, since it's a test of
232 // compile-time name hiding.
234 UErrorCode status
= U_ZERO_ERROR
;
235 Formattable
dateObj(0, Formattable::kIsDate
);
236 Formattable
numObj(3.1415926535897932384626433832795);
242 // DateFormat calling Format API
245 DateFormat
*dateFmt
= DateFormat::createInstance();
247 dateFmt
->format(dateObj
, str
, status
);
248 dateFmt
->format(dateObj
, str
, fpos
, status
);
251 errln("FAIL: Can't create DateFormat");
255 // SimpleDateFormat calling Format & DateFormat API
257 logln("SimpleDateFormat");
258 status
= U_ZERO_ERROR
;
259 SimpleDateFormat
sdf(status
);
261 sdf
.format(dateObj
, str
, status
);
262 sdf
.format(dateObj
, str
, fpos
, status
);
264 sdf
.format((UDate
)0, str
, fpos
);
265 sdf
.format((UDate
)0, str
);
266 sdf
.parse(str
, status
);
267 sdf
.parse(str
, ppos
);
270 // NumberFormat calling Format API
272 logln("NumberFormat");
273 status
= U_ZERO_ERROR
;
274 NumberFormat
*fmt
= NumberFormat::createInstance(status
);
276 fmt
->format(numObj
, str
, status
);
277 fmt
->format(numObj
, str
, fpos
, status
);
280 errln("FAIL: Can't create NumberFormat()");
284 // DecimalFormat calling Format & NumberFormat API
286 logln("DecimalFormat");
287 status
= U_ZERO_ERROR
;
288 DecimalFormat
fmt(status
);
289 if(U_SUCCESS(status
)) {
291 fmt
.format(numObj
, str
, status
);
292 fmt
.format(numObj
, str
, fpos
, status
);
294 fmt
.format(2.71828, str
);
295 fmt
.format((int32_t)1234567, str
);
296 fmt
.format(1.41421, str
, fpos
);
297 fmt
.format((int32_t)9876543, str
, fpos
);
298 fmt
.parse(str
, obj
, ppos
);
299 fmt
.parse(str
, obj
, status
);
301 errln("FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status
));
305 // ChoiceFormat calling Format & NumberFormat API
307 logln("ChoiceFormat");
308 status
= U_ZERO_ERROR
;
309 ChoiceFormat
fmt("0#foo|1#foos|2#foos", status
);
311 fmt
.format(numObj
, str
, status
);
312 fmt
.format(numObj
, str
, fpos
, status
);
314 fmt
.format(2.71828, str
);
315 fmt
.format((int32_t)1234567, str
);
316 fmt
.format(1.41421, str
, fpos
);
317 fmt
.format((int32_t)9876543, str
, fpos
);
318 fmt
.parse(str
, obj
, ppos
);
319 fmt
.parse(str
, obj
, status
);
322 // MessageFormat calling Format API
324 logln("MessageFormat");
325 status
= U_ZERO_ERROR
;
326 MessageFormat
fmt("", status
);
328 // We use dateObj, which MessageFormat should reject.
329 // We're testing name hiding, not the format method.
330 fmt
.format(dateObj
, str
, status
);
331 fmt
.format(dateObj
, str
, fpos
, status
);
335 #endif /* #if !UCONFIG_NO_FORMATTING */