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