1 /***********************************************************************
2 * Copyright (c) 1997-2010, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ***********************************************************************/
5
6 #include "unicode/utypes.h"
7
8 #if !UCONFIG_NO_FORMATTING
9
10 #include "miscdtfm.h"
11
12 #include "unicode/format.h"
13 #include "unicode/decimfmt.h"
14 #include "unicode/datefmt.h"
15 #include "unicode/smpdtfmt.h"
16 #include "unicode/dtfmtsym.h"
17 #include "unicode/locid.h"
18 #include "unicode/msgfmt.h"
19 #include "unicode/numfmt.h"
20 #include "unicode/choicfmt.h"
21 #include "unicode/gregocal.h"
22
23 // *****************************************************************************
24 // class DateFormatMiscTests
25 // *****************************************************************************
26
27 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
28
29 void
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)30 DateFormatMiscTests::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
31 {
32 // if (exec) logln((UnicodeString)"TestSuite DateFormatMiscTests");
33 switch (index) {
34 CASE(0, test4097450)
35 CASE(1, test4099975)
36 CASE(2, test4117335)
37
38 default: name = ""; break;
39 }
40 }
41
42 UBool
failure(UErrorCode status,const char * msg)43 DateFormatMiscTests::failure(UErrorCode status, const char* msg)
44 {
45 if(U_FAILURE(status)) {
46 errcheckln(status, UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status));
47 return TRUE;
48 }
49
50 return FALSE;
51 }
52
53 /*
54 * @bug 4097450
55 */
56 void
test4097450()57 DateFormatMiscTests::test4097450()
58 {
59 //
60 // Date parse requiring 4 digit year.
61 //
62 UnicodeString dstring [] = {
63 UnicodeString("97"),
64 UnicodeString("1997"),
65 UnicodeString("97"),
66 UnicodeString("1997"),
67 UnicodeString("01"),
68 UnicodeString("2001"),
69 UnicodeString("01"),
70 UnicodeString("2001"),
71 UnicodeString("1"),
72 UnicodeString("1"),
73 UnicodeString("11"),
74 UnicodeString("11"),
75 UnicodeString("111"),
76 UnicodeString("111")
77 };
78
79 UnicodeString dformat [] = {
80 UnicodeString("yy"),
81 UnicodeString("yy"),
82 UnicodeString("yyyy"),
83 UnicodeString("yyyy"),
84 UnicodeString("yy"),
85 UnicodeString("yy"),
86 UnicodeString("yyyy"),
87 UnicodeString("yyyy"),
88 UnicodeString("yy"),
89 UnicodeString("yyyy"),
90 UnicodeString("yy"),
91 UnicodeString("yyyy"),
92 UnicodeString("yy"),
93 UnicodeString("yyyy")
94 };
95
96 /* UBool dresult [] = {
97 TRUE,
98 FALSE,
99 FALSE,
100 TRUE,
101 TRUE,
102 FALSE,
103 FALSE,
104 TRUE,
105 FALSE,
106 FALSE,
107 TRUE,
108 FALSE,
109 FALSE,
110 FALSE
111 };*/
112
113 UErrorCode status = U_ZERO_ERROR;
114 SimpleDateFormat *formatter;
115 SimpleDateFormat *resultFormatter = new SimpleDateFormat((UnicodeString)"yyyy", status);
116 if (U_FAILURE(status)) {
117 dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
118 return;
119 }
120
121 logln("Format\tSource\tResult");
122 logln("-------\t-------\t-------");
123 for (int i = 0; i < 14/*dstring.length*/; i++)
124 {
125 log(dformat[i] + "\t" + dstring[i] + "\t");
126 formatter = new SimpleDateFormat(dformat[i], status);
127 if(failure(status, "new SimpleDateFormat")) return;
128 //try {
129 UnicodeString str;
130 FieldPosition pos(FieldPosition::DONT_CARE);
131 logln(resultFormatter->format(formatter->parse(dstring[i], status), str, pos));
132 failure(status, "resultFormatter->format");
133 //if ( !dresult[i] ) System.out.print(" <-- error!");
134 /*}
135 catch (ParseException exception) {
136 //if ( dresult[i] ) System.out.print(" <-- error!");
137 System.out.print("exception --> " + exception);
138 }*/
139 delete formatter;
140 logln();
141 }
142
143 delete resultFormatter;
144 }
145
146 /*
147 * @bug 4099975
148 */
149 void
test4099975()150 DateFormatMiscTests::test4099975()
151 {
152 /**
153 * Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern,
154 * const DateFormatSymbols & formatData, UErrorCode & status )
155 * The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols
156 * should not change the SimpleDateFormat's behavior.
157 */
158 UDate d = Calendar::getNow();
159 {
160 UErrorCode status = U_ZERO_ERROR;
161 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
162 if(failure(status, "new DateFormatSymbols")) return;
163 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols, status);
164 if(failure(status, "new SimpleDateFormat")) return;
165 UnicodeString format0;
166 format0 = df->format(d, format0);
167 UnicodeString localizedPattern0;
168 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
169 failure(status, "df->toLocalizedPattern");
170 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
171 UnicodeString format1;
172 format1 = df->format(d, format1);
173 if (format0 != format1) {
174 errln(UnicodeString("Formats are different. format0: ") + format0
175 + UnicodeString("; format1: ") + format1);
176 }
177 UnicodeString localizedPattern1;
178 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
179 failure(status, "df->toLocalizedPattern");
180 if (localizedPattern0 != localizedPattern1) {
181 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
182 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
183 }
184 delete symbols;
185 delete df;
186 }
187 /*
188 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
189 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
190 */
191 {
192 UErrorCode status = U_ZERO_ERROR;
193 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
194 if(failure(status, "new DateFormatSymbols")) return;
195 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status);
196 if(failure(status, "new SimpleDateFormat")) return;
197 df->setDateFormatSymbols(*symbols);
198 UnicodeString format0;
199 format0 = df->format(d, format0);
200 UnicodeString localizedPattern0;
201 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
202 failure(status, "df->toLocalizedPattern");
203 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
204 UnicodeString format1;
205 format1 = df->format(d, format1);
206 if (format0 != format1) {
207 errln(UnicodeString("Formats are different. format0: ") + format0
208 + UnicodeString("; format1: ") + format1);
209 }
210 UnicodeString localizedPattern1;
211 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
212 failure(status, "df->toLocalizedPattern");
213 if (localizedPattern0 != localizedPattern1) {
214 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
215 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
216 }
217 delete symbols;
218 delete df;
219
220 }
221 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
222 {
223 UErrorCode status = U_ZERO_ERROR;
224 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
225 if(failure(status, "new DateFormatSymbols")) return;
226 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), symbols, status);
227 if(failure(status, "new SimpleDateFormat")) return;
228 UnicodeString format0;
229 format0 = df->format(d, format0);
230 UnicodeString localizedPattern0;
231 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
232 failure(status, "df->toLocalizedPattern");
233 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
234 UnicodeString format1;
235 format1 = df->format(d, format1);
236 if (format0 != format1) {
237 errln(UnicodeString("Formats are different. format0: ") + format0
238 + UnicodeString("; format1: ") + format1);
239 }
240 UnicodeString localizedPattern1;
241 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
242 failure(status, "df->toLocalizedPattern");
243 if (localizedPattern0 == localizedPattern1) {
244 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
245 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
246 }
247 //delete symbols; the caller is no longer responsible for deleting the symbols
248 delete df;
249 }
250 //
251 {
252 UErrorCode status = U_ZERO_ERROR;
253 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
254 failure(status, "new DateFormatSymbols");
255 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status);
256 if(failure(status, "new SimpleDateFormat")) return;
257 df-> adoptDateFormatSymbols(symbols);
258 UnicodeString format0;
259 format0 = df->format(d, format0);
260 UnicodeString localizedPattern0;
261 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
262 failure(status, "df->toLocalizedPattern");
263 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
264 UnicodeString format1;
265 format1 = df->format(d, format1);
266 if (format0 != format1) {
267 errln(UnicodeString("Formats are different. format0: ") + format0
268 + UnicodeString("; format1: ") + format1);
269 }
270 UnicodeString localizedPattern1;
271 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
272 failure(status, "df->toLocalizedPattern");
273 if (localizedPattern0 == localizedPattern1) {
274 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
275 + UnicodeString("; localizedPattern1: ") + localizedPattern1);
276 }
277 //delete symbols; the caller is no longer responsible for deleting the symbols
278 delete df;
279 }
280 }
281
282 /*
283 * @test @(#)bug4117335.java 1.1 3/5/98
284 *
285 * @bug 4117335
286 */
287 void
test4117335()288 DateFormatMiscTests::test4117335()
289 {
290 //UnicodeString bc = "\u7d00\u5143\u524d";
291 UChar bcC [] = {
292 0x7D00,
293 0x5143,
294 0x524D
295 };
296 UnicodeString bc(bcC, 3, 3);
297
298 //UnicodeString ad = "\u897f\u66a6";
299 UChar adC [] = {
300 0x897F,
301 0x66A6
302 };
303 UnicodeString ad(adC, 2, 2);
304
305 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
306 UChar jstLongC [] = {
307 0x65e5,
308 0x672c,
309 0x6a19,
310 0x6e96,
311 0x6642
312 };
313 UChar jdtLongC [] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
314
315 UnicodeString jstLong(jstLongC, 5, 5);
316
317 UnicodeString jstShort = "JST";
318
319 UnicodeString tzID = "Asia/Tokyo";
320
321 UnicodeString jdtLong(jdtLongC, 5, 5);
322
323 UnicodeString jdtShort = "JDT";
324 UErrorCode status = U_ZERO_ERROR;
325 DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getJapan(), status);
326 if(U_FAILURE(status)) {
327 errcheckln(status, "Failure creating DateFormatSymbols, %s", u_errorName(status));
328 delete symbols;
329 return;
330 }
331 failure(status, "new DateFormatSymbols");
332 int32_t eraCount = 0;
333 const UnicodeString *eras = symbols->getEraNames(eraCount);
334
335 logln(UnicodeString("BC = ") + eras[0]);
336 if (eras[0] != bc) {
337 errln("*** Should have been " + bc);
338 //throw new Exception("Error in BC");
339 }
340
341 logln(UnicodeString("AD = ") + eras[1]);
342 if (eras[1] != ad) {
343 errln("*** Should have been " + ad);
344 //throw new Exception("Error in AD");
345 }
346
347 int32_t rowCount, colCount;
348 const UnicodeString **zones = symbols->getZoneStrings(rowCount, colCount);
349 //don't hard code the index .. compute it.
350 int32_t index = -1;
351 for (int32_t i = 0; i < rowCount; ++i) {
352 if (tzID == (zones[i][0])) {
353 index = i;
354 break;
355 }
356 }
357 logln(UnicodeString("Long zone name = ") + zones[index][1]);
358 if (zones[index][1] != jstLong) {
359 errln("*** Should have been " + prettify(jstLong)+ " but it is: " + prettify(zones[index][1]));
360 //throw new Exception("Error in long TZ name");
361 }
362 logln(UnicodeString("Short zone name = ") + zones[index][2]);
363 if (zones[index][2] != jstShort) {
364 errln("*** Should have been " + prettify(jstShort) + " but it is: " + prettify(zones[index][2]));
365 //throw new Exception("Error in short TZ name");
366 }
367 logln(UnicodeString("Long zone name = ") + zones[index][3]);
368 if (zones[index][3] != jdtLong) {
369 errln("*** Should have been " + prettify(jstLong) + " but it is: " + prettify(zones[index][3]));
370 //throw new Exception("Error in long TZ name");
371 }
372 logln(UnicodeString("SHORT zone name = ") + zones[index][4]);
373 if (zones[index][4] != jdtShort) {
374 errln("*** Should have been " + prettify(jstShort)+ " but it is: " + prettify(zones[index][4]));
375 //throw new Exception("Error in short TZ name");
376 }
377 delete symbols;
378
379 }
380
381 #endif /* #if !UCONFIG_NO_FORMATTING */
382