1 /***************************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
8 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormat
\r
9 * Source File: java/text/format/IntlTestDateFormat.java
\r
14 @summary test International Date Format
\r
17 package com.ibm.icu.dev.test.format;
\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
24 import com.ibm.icu.text.DateFormat;
\r
25 import com.ibm.icu.text.SimpleDateFormat;
\r
26 import com.ibm.icu.util.ULocale;
\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
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
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
47 public IntlTestDateFormat() {
\r
50 protected void init() throws Exception{
\r
51 fFormat = DateFormat.getInstance();
\r
54 public static void main(String[] args) throws Exception {
\r
55 new IntlTestDateFormat().run(args);
\r
58 public void TestULocale() {
\r
59 localeTest(ULocale.getDefault(), "Default Locale");
\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
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
71 for(timeStyle = 0; timeStyle < 4; timeStyle++) {
\r
72 fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");
\r
74 fFormat = DateFormat.getTimeInstance(timeStyle, locale);
\r
76 catch(StringIndexOutOfBoundsException e) {
\r
77 errln("FAIL: localeTest time getTimeInstance exception");
\r
85 for(dateStyle = 0; dateStyle < 4; dateStyle++) {
\r
86 fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");
\r
88 fFormat = DateFormat.getDateInstance(dateStyle, locale);
\r
90 catch(StringIndexOutOfBoundsException e) {
\r
91 errln("FAIL: localeTest date getTimeInstance exception");
\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
101 fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
\r
103 catch(StringIndexOutOfBoundsException e) {
\r
104 errln("FAIL: localeTest date/time getDateTimeInstance exception");
\r
112 public void TestFormat() {
\r
113 if (fFormat == null) {
\r
114 errln("FAIL: DateFormat creation failed");
\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
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
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
132 private void describeTest() {
\r
133 if (fFormat == null) {
\r
134 errln("FAIL: no DateFormat");
\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
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
149 int stringMatch = 0;
\r
150 boolean dump = false;
\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
157 date[i] = fFormat.parse(string[i-1].toString());
\r
159 catch (ParseException e) {
\r
161 errln("********** FAIL: Parse of " + string[i-1] + " failed for locale: "+fFormat.getLocale(ULocale.ACTUAL_LOCALE));
\r
166 FieldPosition position = new FieldPosition(0);
\r
167 fFormat.format(date[i], string[i], position);
\r
169 if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;
\r
170 else if (dateMatch > 0 && date[i] != date[i-1]) {
\r
172 errln("********** FAIL: Date mismatch after match.");
\r
176 if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;
\r
177 else if (stringMatch > 0 && string[i] != string[i-1]) {
\r
179 errln("********** FAIL: String mismatch after match.");
\r
184 if (dateMatch > 0 && stringMatch > 0) break;
\r
186 if (i == DEPTH) --i;
\r
188 if (stringMatch > fLimit || dateMatch > fLimit) {
\r
190 errln("********** FAIL: No string and/or date match within " + fLimit + " iterations.");
\r
195 for (int k=0; k<=i; ++k) {
\r
196 logln("" + k + ": " + date[k] + " F> " + string[k] + " P> ");
\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
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
211 // for (i=0; i < sizeof(double); ++i)
\r
213 // char poke = (char*)&d;
\r
214 // poke[i] = (rand() & 0xFF);
\r
216 // } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));
\r
218 // if (d < 0.0) d = -d;
\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
226 return random.nextDouble();
\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
239 logln(all.toString());
\r
241 else errln("********** FAIL: Zero available locales or null array pointer");
\r
244 public void TestRoundtrip() {
\r
247 locales = new ULocale[] {
\r
248 new ULocale("bg_BG"),
\r
249 new ULocale("fr_CA"),
\r
250 new ULocale("zh_TW"),
\r
253 locales = DateFormat.getAvailableULocales();
\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
261 localeTest(locales[i], name);
\r
263 catch(Exception e) {
\r
264 errln("FAIL: TestMonster localeTest exception" + e);
\r