1 /***********************************************************************
2 * Copyright (c) 1997-2007, 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 errln(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 failure(status
, "new SimpleDateFormat");
118 logln("Format\tSource\tResult");
119 logln("-------\t-------\t-------");
120 for (int i
= 0; i
< 14/*dstring.length*/; i
++)
122 log(dformat
[i
] + "\t" + dstring
[i
] + "\t");
123 formatter
= new SimpleDateFormat(dformat
[i
], status
);
124 if(failure(status
, "new SimpleDateFormat")) return;
127 FieldPosition
pos(FieldPosition::DONT_CARE
);
128 logln(resultFormatter
->format(formatter
->parse(dstring
[i
], status
), str
, pos
));
129 failure(status
, "resultFormatter->format");
130 //if ( !dresult[i] ) System.out.print(" <-- error!");
132 catch (ParseException exception) {
133 //if ( dresult[i] ) System.out.print(" <-- error!");
134 System.out.print("exception --> " + exception);
140 delete resultFormatter
;
147 DateFormatMiscTests::test4099975()
150 * Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern,
151 * const DateFormatSymbols & formatData, UErrorCode & status )
152 * The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols
153 * should not change the SimpleDateFormat's behavior.
155 UDate d
= Calendar::getNow();
157 UErrorCode status
= U_ZERO_ERROR
;
158 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
159 if(failure(status
, "new DateFormatSymbols")) return;
160 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols
, status
);
161 if(failure(status
, "new SimpleDateFormat")) return;
162 UnicodeString format0
;
163 format0
= df
->format(d
, format0
);
164 UnicodeString localizedPattern0
;
165 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
166 failure(status
, "df->toLocalizedPattern");
167 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
168 UnicodeString format1
;
169 format1
= df
->format(d
, format1
);
170 if (format0
!= format1
) {
171 errln(UnicodeString("Formats are different. format0: ") + format0
172 + UnicodeString("; format1: ") + format1
);
174 UnicodeString localizedPattern1
;
175 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
176 failure(status
, "df->toLocalizedPattern");
177 if (localizedPattern0
!= localizedPattern1
) {
178 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
179 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
185 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
186 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
189 UErrorCode status
= U_ZERO_ERROR
;
190 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
191 if(failure(status
, "new DateFormatSymbols")) return;
192 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
193 if(failure(status
, "new SimpleDateFormat")) return;
194 df
->setDateFormatSymbols(*symbols
);
195 UnicodeString format0
;
196 format0
= df
->format(d
, format0
);
197 UnicodeString localizedPattern0
;
198 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
199 failure(status
, "df->toLocalizedPattern");
200 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
201 UnicodeString format1
;
202 format1
= df
->format(d
, format1
);
203 if (format0
!= format1
) {
204 errln(UnicodeString("Formats are different. format0: ") + format0
205 + UnicodeString("; format1: ") + format1
);
207 UnicodeString localizedPattern1
;
208 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
209 failure(status
, "df->toLocalizedPattern");
210 if (localizedPattern0
!= localizedPattern1
) {
211 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
212 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
218 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
220 UErrorCode status
= U_ZERO_ERROR
;
221 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
222 if(failure(status
, "new DateFormatSymbols")) return;
223 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), symbols
, status
);
224 if(failure(status
, "new SimpleDateFormat")) return;
225 UnicodeString format0
;
226 format0
= df
->format(d
, format0
);
227 UnicodeString localizedPattern0
;
228 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
229 failure(status
, "df->toLocalizedPattern");
230 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
231 UnicodeString format1
;
232 format1
= df
->format(d
, format1
);
233 if (format0
!= format1
) {
234 errln(UnicodeString("Formats are different. format0: ") + format0
235 + UnicodeString("; format1: ") + format1
);
237 UnicodeString localizedPattern1
;
238 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
239 failure(status
, "df->toLocalizedPattern");
240 if (localizedPattern0
== localizedPattern1
) {
241 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
242 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
244 //delete symbols; the caller is no longer responsible for deleting the symbols
249 UErrorCode status
= U_ZERO_ERROR
;
250 DateFormatSymbols
* symbols
= new DateFormatSymbols(Locale::getUS(), status
);
251 failure(status
, "new DateFormatSymbols");
252 SimpleDateFormat
*df
= new SimpleDateFormat(UnicodeString("E hh:mm"), status
);
253 if(failure(status
, "new SimpleDateFormat")) return;
254 df
-> adoptDateFormatSymbols(symbols
);
255 UnicodeString format0
;
256 format0
= df
->format(d
, format0
);
257 UnicodeString localizedPattern0
;
258 localizedPattern0
= df
->toLocalizedPattern(localizedPattern0
, status
);
259 failure(status
, "df->toLocalizedPattern");
260 symbols
->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
261 UnicodeString format1
;
262 format1
= df
->format(d
, format1
);
263 if (format0
!= format1
) {
264 errln(UnicodeString("Formats are different. format0: ") + format0
265 + UnicodeString("; format1: ") + format1
);
267 UnicodeString localizedPattern1
;
268 localizedPattern1
= df
->toLocalizedPattern(localizedPattern1
, status
);
269 failure(status
, "df->toLocalizedPattern");
270 if (localizedPattern0
== localizedPattern1
) {
271 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
272 + UnicodeString("; localizedPattern1: ") + localizedPattern1
);
274 //delete symbols; the caller is no longer responsible for deleting the symbols
280 * @test @(#)bug4117335.java 1.1 3/5/98
285 DateFormatMiscTests::test4117335()
287 //UnicodeString bc = "\u7d00\u5143\u524d";
293 UnicodeString
bc(bcC
, 3, 3);
295 //UnicodeString ad = "\u897f\u66a6";
300 UnicodeString
ad(adC
, 2, 2);
302 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642";
303 UChar jstLongC
[] = {
310 UChar jdtLongC
[] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593};
312 UnicodeString
jstLong(jstLongC
, 5, 5);
314 UnicodeString jstShort
= "JST";
316 UnicodeString tzID
= "Asia/Tokyo";
318 UnicodeString
jdtLong(jdtLongC
, 5, 5);
320 UnicodeString jdtShort
= "JDT";
321 UErrorCode status
= U_ZERO_ERROR
;
322 DateFormatSymbols
*symbols
= new DateFormatSymbols(Locale::getJapan(), status
);
323 if(U_FAILURE(status
)) {
324 errln("Failure creating DateFormatSymbols, %s", u_errorName(status
));
328 failure(status
, "new DateFormatSymbols");
329 int32_t eraCount
= 0;
330 const UnicodeString
*eras
= symbols
->getEras(eraCount
);
332 logln(UnicodeString("BC = ") + eras
[0]);
334 errln("*** Should have been " + bc
);
335 //throw new Exception("Error in BC");
338 logln(UnicodeString("AD = ") + eras
[1]);
340 errln("*** Should have been " + ad
);
341 //throw new Exception("Error in AD");
344 int32_t rowCount
, colCount
;
345 const UnicodeString
**zones
= symbols
->getZoneStrings(rowCount
, colCount
);
346 //don't hard code the index .. compute it.
348 for (int32_t i
= 0; i
< rowCount
; ++i
) {
349 if (tzID
== (zones
[i
][0])) {
354 logln(UnicodeString("Long zone name = ") + zones
[index
][1]);
355 if (zones
[index
][1] != jstLong
) {
356 errln("*** Should have been " + prettify(jstLong
)+ " but it is: " + prettify(zones
[index
][1]));
357 //throw new Exception("Error in long TZ name");
359 logln(UnicodeString("Short zone name = ") + zones
[index
][2]);
360 if (zones
[index
][2] != jstShort
) {
361 errln("*** Should have been " + prettify(jstShort
) + " but it is: " + prettify(zones
[index
][2]));
362 //throw new Exception("Error in short TZ name");
364 logln(UnicodeString("Long zone name = ") + zones
[index
][3]);
365 if (zones
[index
][3] != jdtLong
) {
366 errln("*** Should have been " + prettify(jstLong
) + " but it is: " + prettify(zones
[index
][3]));
367 //throw new Exception("Error in long TZ name");
369 logln(UnicodeString("SHORT zone name = ") + zones
[index
][4]);
370 if (zones
[index
][4] != jdtShort
) {
371 errln("*** Should have been " + prettify(jstShort
)+ " but it is: " + prettify(zones
[index
][4]));
372 //throw new Exception("Error in short TZ name");
378 #endif /* #if !UCONFIG_NO_FORMATTING */