]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestDateFormat.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / format / IntlTestDateFormat.java
1 /***************************************************************************************\r
2  *\r
3  *   Copyright (C) 1996-2010, International Business Machines\r
4  *   Corporation and others.  All Rights Reserved.\r
5  */\r
6 \r
7 /** \r
8  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDateFormat\r
9  * Source File: java/text/format/IntlTestDateFormat.java\r
10  **/\r
11 \r
12 /*\r
13     @test 1.4 98/03/06\r
14     @summary test International Date Format\r
15 */\r
16 \r
17 package com.ibm.icu.dev.test.format;\r
18 \r
19 import java.text.FieldPosition;\r
20 import java.text.ParseException;\r
21 import java.util.Date;\r
22 import java.util.Random;\r
23 \r
24 import com.ibm.icu.text.DateFormat;\r
25 import com.ibm.icu.text.SimpleDateFormat;\r
26 import com.ibm.icu.util.ULocale;\r
27 \r
28 public class IntlTestDateFormat extends com.ibm.icu.dev.test.TestFmwk {\r
29     // Values in milliseconds (== Date)\r
30     private static final long ONESECOND = 1000;\r
31     private static final long ONEMINUTE = 60 * ONESECOND;\r
32     private static final long ONEHOUR = 60 * ONEMINUTE;\r
33     private static final long ONEDAY = 24 * ONEHOUR;\r
34     //private static final double ONEYEAR = 365.25 * ONEDAY; // Approximate //The variable is never used\r
35 \r
36     // EModes\r
37     //private static final byte GENERIC = 0;\r
38     //private static final byte TIME = GENERIC + 1; //The variable is never used\r
39     //private static final byte DATE = TIME + 1; //The variable is never used\r
40     //private static final byte DATE_TIME = DATE + 1; //The variable is never used\r
41 \r
42     private DateFormat fFormat = null;\r
43     private String fTestName = new String("getInstance");\r
44     private int fLimit = 3; // How many iterations it should take to reach convergence\r
45     private Random random; // initialized in randDouble\r
46 \r
47     public IntlTestDateFormat() {\r
48         //Constructure\r
49     } \r
50     protected void init() throws Exception{\r
51         fFormat = DateFormat.getInstance();\r
52     }\r
53     \r
54     public static void main(String[] args) throws Exception {\r
55         new IntlTestDateFormat().run(args);\r
56     }\r
57 \r
58     public void TestULocale() {\r
59         localeTest(ULocale.getDefault(), "Default Locale");\r
60     }\r
61 \r
62     // This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.\r
63     public void localeTest(final ULocale locale, final String localeName) {\r
64         int timeStyle, dateStyle;\r
65 \r
66         // For patterns including only time information and a timezone, it may take\r
67         // up to three iterations, since the timezone may shift as the year number\r
68         // is determined.  For other patterns, 2 iterations should suffice.\r
69         fLimit = 3;\r
70 \r
71         for(timeStyle = 0; timeStyle < 4; timeStyle++) {\r
72             fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");\r
73             try {\r
74                 fFormat = DateFormat.getTimeInstance(timeStyle, locale);\r
75             }\r
76             catch(StringIndexOutOfBoundsException e) {\r
77                 errln("FAIL: localeTest time getTimeInstance exception");\r
78                 throw e;\r
79             }\r
80             TestFormat();\r
81         }\r
82 \r
83         fLimit = 2;\r
84 \r
85         for(dateStyle = 0; dateStyle < 4; dateStyle++) {\r
86             fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");\r
87             try {\r
88                 fFormat = DateFormat.getDateInstance(dateStyle, locale);\r
89             }\r
90             catch(StringIndexOutOfBoundsException e) {\r
91                 errln("FAIL: localeTest date getTimeInstance exception");\r
92                 throw e;\r
93             }\r
94             TestFormat();\r
95         }\r
96 \r
97         for(dateStyle = 0; dateStyle < 4; dateStyle++) {\r
98             for(timeStyle = 0; timeStyle < 4; timeStyle++) {\r
99                 fTestName = new String("DateTime test " + dateStyle + "/" + timeStyle + " (" + localeName + ")");\r
100                 try {\r
101                     fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);\r
102                 }\r
103                 catch(StringIndexOutOfBoundsException e) {\r
104                     errln("FAIL: localeTest date/time getDateTimeInstance exception");\r
105                     throw e;\r
106                 }\r
107                 TestFormat();\r
108             }\r
109         }\r
110     }\r
111 \r
112     public void TestFormat() {\r
113         if (fFormat == null) {\r
114             errln("FAIL: DateFormat creation failed");\r
115             return;\r
116         }\r
117         //        logln("TestFormat: " + fTestName);\r
118         Date now = new Date();\r
119         tryDate(new Date(0));\r
120         tryDate(new Date((long) 1278161801778.0));\r
121         tryDate(now);\r
122         // Shift 6 months into the future, AT THE SAME TIME OF DAY.\r
123         // This will test the DST handling.\r
124         tryDate(new Date(now.getTime() + 6*30*ONEDAY));\r
125 \r
126         Date limit = new Date(now.getTime() * 10); // Arbitrary limit\r
127         for (int i=0; i<2; ++i)\r
128             //            tryDate(new Date(floor(randDouble() * limit)));\r
129             tryDate(new Date((long) (randDouble() * limit.getTime())));\r
130     }\r
131 \r
132     private void describeTest() {\r
133         if (fFormat == null) {\r
134             errln("FAIL: no DateFormat");\r
135             return;\r
136         }\r
137 \r
138         // Assume it's a SimpleDateFormat and get some info\r
139         SimpleDateFormat s = (SimpleDateFormat) fFormat;\r
140         logln(fTestName + " Pattern " + s.toPattern());\r
141     }\r
142     \r
143     private void tryDate(Date theDate) {\r
144         final int DEPTH = 10;\r
145         Date[] date = new Date[DEPTH];\r
146         StringBuffer[] string = new StringBuffer[DEPTH];\r
147 \r
148         int dateMatch = 0;\r
149         int stringMatch = 0;\r
150         boolean dump = false;\r
151         int i;\r
152         for (i=0; i<DEPTH; ++i) string[i] = new StringBuffer();\r
153         for (i=0; i<DEPTH; ++i) {\r
154             if (i == 0) date[i] = theDate;\r
155             else {\r
156                 try {\r
157                     date[i] = fFormat.parse(string[i-1].toString());\r
158                 }\r
159                 catch (ParseException e) {\r
160                     describeTest();\r
161                     errln("********** FAIL: Parse of " + string[i-1] + " failed for locale: "+fFormat.getLocale(ULocale.ACTUAL_LOCALE));\r
162                     dump = true;\r
163                     break;\r
164                 }\r
165             }\r
166             FieldPosition position = new FieldPosition(0);\r
167             fFormat.format(date[i], string[i], position);\r
168             if (i > 0) {\r
169                 if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;\r
170                 else if (dateMatch > 0 && date[i] != date[i-1]) {\r
171                     describeTest();\r
172                     errln("********** FAIL: Date mismatch after match.");\r
173                     dump = true;\r
174                     break;\r
175                 }\r
176                 if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;\r
177                 else if (stringMatch > 0 && string[i] != string[i-1]) {\r
178                     describeTest();\r
179                     errln("********** FAIL: String mismatch after match.");\r
180                     dump = true;\r
181                     break;\r
182                 }\r
183             }\r
184             if (dateMatch > 0 && stringMatch > 0) break;\r
185         }\r
186         if (i == DEPTH) --i;\r
187 \r
188         if (stringMatch > fLimit || dateMatch > fLimit) {\r
189             describeTest();\r
190             errln("********** FAIL: No string and/or date match within " + fLimit + " iterations.");\r
191             dump = true;\r
192         }\r
193 \r
194         if (dump) {\r
195             for (int k=0; k<=i; ++k) {\r
196                 logln("" + k + ": " + date[k] + " F> " + string[k] + " P> ");\r
197             }\r
198         }\r
199     }\r
200 \r
201     // Return a random double from 0.01 to 1, inclusive\r
202     private double randDouble() {\r
203     if (random == null) {\r
204         random = createRandom();\r
205     }\r
206         // Assume 8-bit (or larger) rand values.  Also assume\r
207         // that the system rand() function is very poor, which it always is.\r
208         //        double d;\r
209         //        int i;\r
210         //        do {\r
211         //            for (i=0; i < sizeof(double); ++i)\r
212         //            {\r
213         //                char poke = (char*)&d;\r
214         //                poke[i] = (rand() & 0xFF);\r
215         //            }\r
216         //        } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));\r
217 \r
218         //        if (d < 0.0) d = -d;\r
219         //        if (d > 0.0)\r
220         //        {\r
221         //            double e = floor(log10(d));\r
222         //            if (e < -2.0) d *= pow(10.0, -e-2);\r
223         //            else if (e > -1.0) d /= pow(10.0, e+1);\r
224         //        }\r
225         //        return d;\r
226         return random.nextDouble();\r
227     }\r
228 \r
229     public void TestAvailableLocales() {\r
230         final ULocale[] locales = DateFormat.getAvailableULocales();\r
231         long count = locales.length;\r
232         logln("" + count + " available locales");\r
233         if (locales != null  &&  count != 0) {\r
234             StringBuffer all = new StringBuffer();\r
235             for (int i=0; i<count; ++i) {\r
236                 if (i!=0) all.append(", ");\r
237                 all.append(locales[i].getDisplayName());\r
238             }\r
239             logln(all.toString());\r
240         }\r
241         else errln("********** FAIL: Zero available locales or null array pointer");\r
242     }\r
243 \r
244     public void TestRoundtrip() {\r
245         ULocale[] locales;\r
246         if (isQuick()) {\r
247             locales = new ULocale[] {\r
248                     new ULocale("bg_BG"),\r
249                     new ULocale("fr_CA"),\r
250                     new ULocale("zh_TW"),\r
251             };\r
252         } else {\r
253             locales = DateFormat.getAvailableULocales();\r
254         }\r
255         long count = locales.length;\r
256         if (locales != null  &&  count != 0) {\r
257             for (int i=0; i<count; ++i) {\r
258                 String name = locales[i].getDisplayName();\r
259                 logln("Testing " + name + "...");\r
260                 try {\r
261                     localeTest(locales[i], name);\r
262                 }\r
263                 catch(Exception e) {\r
264                     errln("FAIL: TestMonster localeTest exception" + e);\r
265                 }\r
266             }\r
267         }\r
268     }\r
269 }\r
270 \r
271 //eof\r