1 /***********************************************************************
2 * Copyright (c) 1997-2011, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ***********************************************************************/
6 #include "unicode/utypes.h"
8 #if !UCONFIG_NO_FORMATTING
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"
23 // *****************************************************************************
24 // class DateFormatMiscTests
25 // *****************************************************************************
27 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
30 DateFormatMiscTests::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
32 // if (exec) logln((UnicodeString)"TestSuite DateFormatMiscTests");
38 default: name
= ""; break;
43 DateFormatMiscTests::failure(UErrorCode status
, const char* msg
)
45 if(U_FAILURE(status
)) {
46 errcheckln(status
, UnicodeString("FAIL: ") + msg
+ " failed, error " + u_errorName(status
));
57 DateFormatMiscTests::test4097450()
60 // Date parse requiring 4 digit year.
62 UnicodeString dstring
[] = {
64 UnicodeString("1997"),
66 UnicodeString("1997"),
68 UnicodeString("2001"),
70 UnicodeString("2001"),
79 UnicodeString dformat
[] = {
82 UnicodeString("yyyy"),
83 UnicodeString("yyyy"),
86 UnicodeString("yyyy"),
87 UnicodeString("yyyy"),
89 UnicodeString("yyyy"),
91 UnicodeString("yyyy"),
96 /* UBool dresult [] = {
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
));
121 logln("Format\tSource\tResult");
122 logln("-------\t-------\t-------");
123 for (int i
= 0; i
< 14/*dstring.length*/; i
++)
125 log(dformat
[i
] + "\t" + dstring
[i
] + "\t");
126 formatter
= new SimpleDateFormat(dformat
[i
], status
);
127 if(failure(status
, "new SimpleDateFormat")) return;
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!");
135 catch (ParseException exception) {
136 //if ( dresult[i] ) System.out.print(" <-- error!");
137 System.out.print("exception --> " + exception);
143 delete resultFormatter
;
150 DateFormatMiscTests::test4099975()
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.
158 UDate d
= Calendar::getNow();
160 UErrorCode status
= U_ZERO_ERROR
;
161 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
162 if (U_FAILURE(status
)) {
163 dataerrln("Unable to create DateFormatSymbols - %s", u_errorName(status
));
166 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols
, status
);
167 if(failure(status
, "new SimpleDateFormat")) return;
168 UnicodeString format0
;
169 format0
= df
->format(d
, format0
);
170 UnicodeString localizedPattern0
;
171 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
172 failure(status
, "df->toLocalizedPattern");
173 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
174 UnicodeString format1
;
175 format1
= df
->format(d
, format1
);
176 if (format0
!= format1
) {
177 errln(UnicodeString("Formats are different. format0: ") + format0
178 + UnicodeString("; format1: ") + format1
);
180 UnicodeString localizedPattern1
;
181 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
182 failure(status
, "df->toLocalizedPattern");
183 if (localizedPattern0
!= localizedPattern1
) {
184 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
185 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
191 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
192 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
195 UErrorCode status
= U_ZERO_ERROR
;
196 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
197 if(failure(status
, "new DateFormatSymbols")) return;
198 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
199 if(failure(status
, "new SimpleDateFormat")) return;
200 df
->setDateFormatSymbols(*symbols
);
201 UnicodeString format0
;
202 format0
= df
->format(d
, format0
);
203 UnicodeString localizedPattern0
;
204 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
205 failure(status
, "df->toLocalizedPattern");
206 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
207 UnicodeString format1
;
208 format1
= df
->format(d
, format1
);
209 if (format0
!= format1
) {
210 errln(UnicodeString("Formats are different. format0: ") + format0
211 + UnicodeString("; format1: ") + format1
);
213 UnicodeString localizedPattern1
;
214 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
215 failure(status
, "df->toLocalizedPattern");
216 if (localizedPattern0
!= localizedPattern1
) {
217 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
218 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
224 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
226 UErrorCode status
= U_ZERO_ERROR
;
227 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
228 if(failure(status
, "new DateFormatSymbols")) return;
229 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), symbols
, status
);
230 if(failure(status
, "new SimpleDateFormat")) return;
231 UnicodeString format0
;
232 format0
= df
->format(d
, format0
);
233 UnicodeString localizedPattern0
;
234 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
235 failure(status
, "df->toLocalizedPattern");
236 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
237 UnicodeString format1
;
238 format1
= df
->format(d
, format1
);
239 if (format0
!= format1
) {
240 errln(UnicodeString("Formats are different. format0: ") + format0
241 + UnicodeString("; format1: ") + format1
);
243 UnicodeString localizedPattern1
;
244 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
245 failure(status
, "df->toLocalizedPattern");
246 if (localizedPattern0
== localizedPattern1
) {
247 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
248 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
250 //delete symbols; the caller is no longer responsible for deleting the symbols
255 UErrorCode status
= U_ZERO_ERROR
;
256 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
257 failure(status
, "new DateFormatSymbols");
258 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
259 if(failure(status
, "new SimpleDateFormat")) return;
260 df
-> adoptDateFormatSymbols(symbols
);
261 UnicodeString format0
;
262 format0
= df
->format(d
, format0
);
263 UnicodeString localizedPattern0
;
264 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
265 failure(status
, "df->toLocalizedPattern");
266 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
267 UnicodeString format1
;
268 format1
= df
->format(d
, format1
);
269 if (format0
!= format1
) {
270 errln(UnicodeString("Formats are different. format0: ") + format0
271 + UnicodeString("; format1: ") + format1
);
273 UnicodeString localizedPattern1
;
274 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
275 failure(status
, "df->toLocalizedPattern");
276 if (localizedPattern0
== localizedPattern1
) {
277 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
278 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
280 //delete symbols; the caller is no longer responsible for deleting the symbols
286 * @test @(#)bug4117335.java 1.1 3/5/98
291 DateFormatMiscTests::test4117335()
293 //UnicodeString bc = "\u7d00\u5143\u524d";
299 UnicodeString
bc(bcC
, 3, 3);
301 //UnicodeString ad = "\u897f\u66a6";
306 UnicodeString
ad(adC
, 2, 2);
308 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
309 UChar jstLongC
[] = {
316 UChar jdtLongC
[] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
318 UnicodeString
jstLong(jstLongC
, 5, 5);
320 // UnicodeString jstShort = "JST";
322 UnicodeString tzID
= "Asia/Tokyo";
324 UnicodeString
jdtLong(jdtLongC
, 5, 5);
326 // UnicodeString jdtShort = "JDT";
327 UErrorCode status
= U_ZERO_ERROR
;
328 DateFormatSymbols
*symbols
= new DateFormatSymbols(Locale::getJapan(), status
);
329 if(U_FAILURE(status
)) {
330 dataerrln("Failure creating DateFormatSymbols, %s", u_errorName(status
));
334 failure(status
, "new DateFormatSymbols");
335 int32_t eraCount
= 0;
336 const UnicodeString
*eras
= symbols
->getEraNames(eraCount
);
338 logln(UnicodeString("BC = ") + eras
[0]);
340 errln("*** Should have been " + bc
);
341 //throw new Exception("Error in BC");
344 logln(UnicodeString("AD = ") + eras
[1]);
346 errln("*** Should have been " + ad
);
347 //throw new Exception("Error in AD");
350 int32_t rowCount
, colCount
;
351 const UnicodeString
**zones
= symbols
->getZoneStrings(rowCount
, colCount
);
352 //don't hard code the index .. compute it.
354 for (int32_t i
= 0; i
< rowCount
; ++i
) {
355 if (tzID
== (zones
[i
][0])) {
360 logln(UnicodeString("Long zone name = ") + zones
[index
][1]);
361 if (zones
[index
][1] != jstLong
) {
362 errln("*** Should have been " + prettify(jstLong
)+ " but it is: " + prettify(zones
[index
][1]));
363 //throw new Exception("Error in long TZ name");
365 // logln(UnicodeString("Short zone name = ") + zones[index][2]);
366 // if (zones[index][2] != jstShort) {
367 // errln("*** Should have been " + prettify(jstShort) + " but it is: " + prettify(zones[index][2]));
368 // //throw new Exception("Error in short TZ name");
370 logln(UnicodeString("Long zone name = ") + zones
[index
][3]);
371 if (zones
[index
][3] != jdtLong
) {
372 errln("*** Should have been " + prettify(jstLong
) + " but it is: " + prettify(zones
[index
][3]));
373 //throw new Exception("Error in long TZ name");
375 // logln(UnicodeString("SHORT zone name = ") + zones[index][4]);
376 // if (zones[index][4] != jdtShort) {
377 // errln("*** Should have been " + prettify(jstShort)+ " but it is: " + prettify(zones[index][4]));
378 // //throw new Exception("Error in short TZ name");
384 #endif /* #if !UCONFIG_NO_FORMATTING */