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