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