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