]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dtfmapts.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / dtfmapts.cpp
1 /***********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "dtfmapts.h"
12
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"
18
19
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.
23
24 void IntlTestDateFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
25 {
26 if (exec) logln("TestSuite DateFormatAPI");
27 switch (index) {
28 case 0: name = "DateFormat API test";
29 if (exec) {
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");
35 }
36 testAPI(/*par*/);
37 }
38 break;
39
40 case 1: name = "TestEquals";
41 if (exec) {
42 logln("TestEquals---"); logln("");
43 TestEquals();
44 }
45 break;
46
47 case 2: name = "TestNameHiding";
48 if (exec) {
49 logln("TestNameHiding---"); logln("");
50 TestNameHiding();
51 }
52 break;
53
54 default: name = ""; break;
55 }
56 }
57
58 /**
59 * Test that the equals method works correctly.
60 */
61 void IntlTestDateFormatAPI::TestEquals(void)
62 {
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();
69
70 if (!(*a == *b))
71 errln("FAIL: DateFormat objects created at different times are unequal.");
72
73 if (b->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
74 {
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.");
79 else if (*a == *b)
80 errln("FAIL: DateFormat objects with different two digit start dates are equal.");
81 }
82 delete a;
83 delete b;
84 }
85
86 /**
87 * This test checks various generic API methods in DateFormat to achieve 100%
88 * API coverage.
89 */
90 void IntlTestDateFormatAPI::testAPI(/* char* par */)
91 {
92 UErrorCode status = U_ZERO_ERROR;
93
94 // ======= Test constructors
95
96 logln("Testing DateFormat constructors");
97
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());
102
103 // ======= Test equality
104
105 logln("Testing equality operator");
106
107 if( *fr == *it ) {
108 errln("ERROR: == failed");
109 }
110
111 // ======= Test various format() methods
112
113 logln("Testing various format() methods");
114
115 UDate d = 837039928046.0;
116 Formattable fD(d, Formattable::kIsDate);
117
118 UnicodeString res1, res2, res3;
119 FieldPosition pos1(0), pos2(0);
120
121 status = U_ZERO_ERROR;
122 res1 = fr->format(d, res1, pos1, status);
123 if(U_FAILURE(status)) {
124 errln("ERROR: format() failed (French)");
125 }
126 logln( (UnicodeString) "" + d + " formatted to " + res1);
127
128 res2 = it->format(d, res2, pos2);
129 logln( (UnicodeString) "" + d + " formatted to " + res2);
130
131 res3 = de->format(d, res3);
132 logln( (UnicodeString) "" + d + " formatted to " + res3);
133
134 // ======= Test parse()
135
136 logln("Testing parse()");
137
138 UnicodeString text("02/03/76 2:50 AM, CST");
139 Formattable result1;
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);
145 }
146 logln(text + " parsed into " + result1.getDate());
147
148 status = U_ZERO_ERROR;
149 result2 = def->parse(text, status);
150 if(U_FAILURE(status)) {
151 errln("ERROR: parse() failed, stopping testing");
152 return;
153 }
154 logln(text + " parsed into " + result2);
155
156 result3 = def->parse(text, pos01);
157 logln(text + " parsed into " + result3);
158
159
160 // ======= Test getters and setters
161
162 logln("Testing getters and setters");
163
164 int32_t count = 0;
165 const Locale *locales = DateFormat::getAvailableLocales(count);
166 logln((UnicodeString) "Got " + count + " locales" );
167 for(int32_t i = 0; i < count; i++) {
168 UnicodeString name;
169 name = locales[i].getName();
170 logln(name);
171 }
172
173 fr->setLenient(it->isLenient());
174 if(fr->isLenient() != it->isLenient()) {
175 errln("ERROR: setLenient() failed");
176 }
177
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");
184 }
185
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");
192 }
193
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");
200 }
201
202 // ======= Test getStaticClassID()
203
204 logln("Testing getStaticClassID()");
205
206 status = U_ZERO_ERROR;
207 DateFormat *test = new SimpleDateFormat(status);
208 if(U_FAILURE(status)) {
209 errln("ERROR: Couldn't create a DateFormat");
210 }
211
212 if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
213 errln("ERROR: getDynamicClassID() didn't return the expected value");
214 }
215
216 delete test;
217 delete def;
218 delete fr;
219 delete it;
220 delete de;
221 }
222
223 /**
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.
227 */
228 void
229 IntlTestDateFormatAPI::TestNameHiding(void) {
230
231 // N.B.: This test passes if it COMPILES, since it's a test of
232 // compile-time name hiding.
233
234 UErrorCode status = U_ZERO_ERROR;
235 Formattable dateObj(0, Formattable::kIsDate);
236 Formattable numObj(3.1415926535897932384626433832795);
237 Formattable obj;
238 UnicodeString str;
239 FieldPosition fpos;
240 ParsePosition ppos;
241
242 // DateFormat calling Format API
243 {
244 logln("DateFormat");
245 DateFormat *dateFmt = DateFormat::createInstance();
246 if (dateFmt) {
247 dateFmt->format(dateObj, str, status);
248 dateFmt->format(dateObj, str, fpos, status);
249 delete dateFmt;
250 } else {
251 errln("FAIL: Can't create DateFormat");
252 }
253 }
254
255 // SimpleDateFormat calling Format & DateFormat API
256 {
257 logln("SimpleDateFormat");
258 status = U_ZERO_ERROR;
259 SimpleDateFormat sdf(status);
260 // Format API
261 sdf.format(dateObj, str, status);
262 sdf.format(dateObj, str, fpos, status);
263 // DateFormat API
264 sdf.format((UDate)0, str, fpos);
265 sdf.format((UDate)0, str);
266 sdf.parse(str, status);
267 sdf.parse(str, ppos);
268 }
269
270 // NumberFormat calling Format API
271 {
272 logln("NumberFormat");
273 status = U_ZERO_ERROR;
274 NumberFormat *fmt = NumberFormat::createInstance(status);
275 if (fmt) {
276 fmt->format(numObj, str, status);
277 fmt->format(numObj, str, fpos, status);
278 delete fmt;
279 } else {
280 errln("FAIL: Can't create NumberFormat()");
281 }
282 }
283
284 // DecimalFormat calling Format & NumberFormat API
285 {
286 logln("DecimalFormat");
287 status = U_ZERO_ERROR;
288 DecimalFormat fmt(status);
289 if(U_SUCCESS(status)) {
290 // Format API
291 fmt.format(numObj, str, status);
292 fmt.format(numObj, str, fpos, status);
293 // NumberFormat API
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);
300 } else {
301 errln("FAIL: Couldn't instantiate DecimalFormat, error %s. Quitting test", u_errorName(status));
302 }
303 }
304
305 // ChoiceFormat calling Format & NumberFormat API
306 {
307 logln("ChoiceFormat");
308 status = U_ZERO_ERROR;
309 ChoiceFormat fmt("0#foo|1#foos|2#foos", status);
310 // Format API
311 fmt.format(numObj, str, status);
312 fmt.format(numObj, str, fpos, status);
313 // NumberFormat API
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);
320 }
321
322 // MessageFormat calling Format API
323 {
324 logln("MessageFormat");
325 status = U_ZERO_ERROR;
326 MessageFormat fmt("", status);
327 // Format API
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);
332 }
333 }
334
335 #endif /* #if !UCONFIG_NO_FORMATTING */