1 /***********************************************************************
2 * Copyright (c) 1997-2010, 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(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
);
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
);
188 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
189 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
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
);
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
);
221 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
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
);
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
);
247 //delete symbols; the caller is no longer responsible for deleting the symbols
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
);
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
);
277 //delete symbols; the caller is no longer responsible for deleting the symbols
283 * @test @(#)bug4117335.java 1.1 3/5/98
288 DateFormatMiscTests::test4117335()
290 //UnicodeString bc = "\u7d00\u5143\u524d";
296 UnicodeString
bc(bcC
, 3, 3);
298 //UnicodeString ad = "\u897f\u66a6";
303 UnicodeString
ad(adC
, 2, 2);
305 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
306 UChar jstLongC
[] = {
313 UChar jdtLongC
[] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
315 UnicodeString
jstLong(jstLongC
, 5, 5);
317 UnicodeString jstShort
= "JST";
319 UnicodeString tzID
= "Asia/Tokyo";
321 UnicodeString
jdtLong(jdtLongC
, 5, 5);
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
));
331 failure(status
, "new DateFormatSymbols");
332 int32_t eraCount
= 0;
333 const UnicodeString
*eras
= symbols
->getEraNames(eraCount
);
335 logln(UnicodeString("BC = ") + eras
[0]);
337 errln("*** Should have been " + bc
);
338 //throw new Exception("Error in BC");
341 logln(UnicodeString("AD = ") + eras
[1]);
343 errln("*** Should have been " + ad
);
344 //throw new Exception("Error in AD");
347 int32_t rowCount
, colCount
;
348 const UnicodeString
**zones
= symbols
->getZoneStrings(rowCount
, colCount
);
349 //don't hard code the index .. compute it.
351 for (int32_t i
= 0; i
< rowCount
; ++i
) {
352 if (tzID
== (zones
[i
][0])) {
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");
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");
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");
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");
381 #endif /* #if !UCONFIG_NO_FORMATTING */