1 /***************************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
8 * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormat
9 * Source File: java/text/format/IntlTestDateFormat.java
14 @summary test International Date Format
17 package com.ibm.icu.dev.test.format;
19 import java.text.FieldPosition;
20 import java.text.ParseException;
21 import java.util.Date;
22 import java.util.Random;
24 import com.ibm.icu.text.DateFormat;
25 import com.ibm.icu.text.SimpleDateFormat;
26 import com.ibm.icu.util.ULocale;
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
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
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
47 public IntlTestDateFormat() {
50 protected void init() throws Exception{
51 fFormat = DateFormat.getInstance();
54 public static void main(String[] args) throws Exception {
55 new IntlTestDateFormat().run(args);
58 public void TestULocale() {
59 localeTest(ULocale.getDefault(), "Default Locale");
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;
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.
71 for(timeStyle = 0; timeStyle < 4; timeStyle++) {
72 fTestName = new String("Time test " + timeStyle + " (" + localeName + ")");
74 fFormat = DateFormat.getTimeInstance(timeStyle, locale);
76 catch(StringIndexOutOfBoundsException e) {
77 errln("FAIL: localeTest time getTimeInstance exception");
85 for(dateStyle = 0; dateStyle < 4; dateStyle++) {
86 fTestName = new String("Date test " + dateStyle + " (" + localeName + ")");
88 fFormat = DateFormat.getDateInstance(dateStyle, locale);
90 catch(StringIndexOutOfBoundsException e) {
91 errln("FAIL: localeTest date getTimeInstance exception");
97 for(dateStyle = 0; dateStyle < 4; dateStyle++) {
98 for(timeStyle = 0; timeStyle < 4; timeStyle++) {
99 fTestName = new String("DateTime test " + dateStyle + "/" + timeStyle + " (" + localeName + ")");
101 fFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
103 catch(StringIndexOutOfBoundsException e) {
104 errln("FAIL: localeTest date/time getDateTimeInstance exception");
112 public void TestFormat() {
113 if (fFormat == null) {
114 errln("FAIL: DateFormat creation failed");
117 // logln("TestFormat: " + fTestName);
118 Date now = new Date();
119 tryDate(new Date(0));
120 tryDate(new Date((long) 1278161801778.0));
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));
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())));
132 private void describeTest() {
133 if (fFormat == null) {
134 errln("FAIL: no DateFormat");
138 // Assume it's a SimpleDateFormat and get some info
139 SimpleDateFormat s = (SimpleDateFormat) fFormat;
140 logln(fTestName + " Pattern " + s.toPattern());
143 private void tryDate(Date theDate) {
144 final int DEPTH = 10;
145 Date[] date = new Date[DEPTH];
146 StringBuffer[] string = new StringBuffer[DEPTH];
150 boolean dump = false;
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;
157 date[i] = fFormat.parse(string[i-1].toString());
159 catch (ParseException e) {
161 errln("********** FAIL: Parse of " + string[i-1] + " failed for locale: "+fFormat.getLocale(ULocale.ACTUAL_LOCALE));
166 FieldPosition position = new FieldPosition(0);
167 fFormat.format(date[i], string[i], position);
169 if (dateMatch == 0 && date[i] == date[i-1]) dateMatch = i;
170 else if (dateMatch > 0 && date[i] != date[i-1]) {
172 errln("********** FAIL: Date mismatch after match.");
176 if (stringMatch == 0 && string[i] == string[i-1]) stringMatch = i;
177 else if (stringMatch > 0 && string[i] != string[i-1]) {
179 errln("********** FAIL: String mismatch after match.");
184 if (dateMatch > 0 && stringMatch > 0) break;
188 if (stringMatch > fLimit || dateMatch > fLimit) {
190 errln("********** FAIL: No string and/or date match within " + fLimit + " iterations.");
195 for (int k=0; k<=i; ++k) {
196 logln("" + k + ": " + date[k] + " F> " + string[k] + " P> ");
201 // Return a random double from 0.01 to 1, inclusive
202 private double randDouble() {
203 if (random == null) {
204 random = createRandom();
206 // Assume 8-bit (or larger) rand values. Also assume
207 // that the system rand() function is very poor, which it always is.
211 // for (i=0; i < sizeof(double); ++i)
213 // char poke = (char*)&d;
214 // poke[i] = (rand() & 0xFF);
216 // } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));
218 // if (d < 0.0) d = -d;
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);
226 return random.nextDouble();
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());
239 logln(all.toString());
241 else errln("********** FAIL: Zero available locales or null array pointer");
244 public void TestRoundtrip() {
247 locales = new ULocale[] {
248 new ULocale("bg_BG"),
249 new ULocale("fr_CA"),
250 new ULocale("zh_TW"),
253 locales = DateFormat.getAvailableULocales();
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 + "...");
261 localeTest(locales[i], name);
264 errln("FAIL: TestMonster localeTest exception" + e);