001: /***************************************************************************************
002: *
003: * Copyright (C) 1996-2006, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: */
006:
007: /**
008: * Port From: JDK 1.4b1 : java.text.Format.IntlTestDateFormat
009: * Source File: java/text/format/IntlTestDateFormat.java
010: **/package com.ibm.icu.dev.test.format;
011:
012: import com.ibm.icu.text.DateFormat;
013: import com.ibm.icu.text.SimpleDateFormat;
014: import com.ibm.icu.util.ULocale;
015: import java.text.FieldPosition;
016: import java.text.ParseException;
017: import java.util.Random;
018: import java.util.Date;
019:
020: public class IntlTestDateFormat extends com.ibm.icu.dev.test.TestFmwk {
021: // Values in milliseconds (== Date)
022: private static final long ONESECOND = 1000;
023: private static final long ONEMINUTE = 60 * ONESECOND;
024: private static final long ONEHOUR = 60 * ONEMINUTE;
025: private static final long ONEDAY = 24 * ONEHOUR;
026: //private static final double ONEYEAR = 365.25 * ONEDAY; // Approximate //The variable is never used
027:
028: // EModes
029: //private static final byte GENERIC = 0;
030: //private static final byte TIME = GENERIC + 1; //The variable is never used
031: //private static final byte DATE = TIME + 1; //The variable is never used
032: //private static final byte DATE_TIME = DATE + 1; //The variable is never used
033:
034: private DateFormat fFormat = null;
035: private String fTestName = new String("getInstance");
036: private int fLimit = 3; // How many iterations it should take to reach convergence
037: private Random random; // initialized in randDouble
038:
039: public IntlTestDateFormat() {
040: //Constructure
041: }
042:
043: protected void init() throws Exception {
044: fFormat = DateFormat.getInstance();
045: }
046:
047: public static void main(String[] args) throws Exception {
048: new IntlTestDateFormat().run(args);
049: }
050:
051: public void TestULocale() {
052: localeTest(ULocale.getDefault(), "Default Locale");
053: }
054:
055: // This test does round-trip testing (format -> parse -> format -> parse -> etc.) of DateFormat.
056: public void localeTest(final ULocale locale, final String localeName) {
057: int timeStyle, dateStyle;
058:
059: // For patterns including only time information and a timezone, it may take
060: // up to three iterations, since the timezone may shift as the year number
061: // is determined. For other patterns, 2 iterations should suffice.
062: fLimit = 3;
063:
064: for (timeStyle = 0; timeStyle < 4; timeStyle++) {
065: fTestName = new String("Time test " + timeStyle + " ("
066: + localeName + ")");
067: try {
068: fFormat = DateFormat.getTimeInstance(timeStyle, locale);
069: } catch (StringIndexOutOfBoundsException e) {
070: errln("FAIL: localeTest time getTimeInstance exception");
071: throw e;
072: }
073: TestFormat();
074: }
075:
076: fLimit = 2;
077:
078: for (dateStyle = 0; dateStyle < 4; dateStyle++) {
079: fTestName = new String("Date test " + dateStyle + " ("
080: + localeName + ")");
081: try {
082: fFormat = DateFormat.getDateInstance(dateStyle, locale);
083: } catch (StringIndexOutOfBoundsException e) {
084: errln("FAIL: localeTest date getTimeInstance exception");
085: throw e;
086: }
087: TestFormat();
088: }
089:
090: for (dateStyle = 0; dateStyle < 4; dateStyle++) {
091: for (timeStyle = 0; timeStyle < 4; timeStyle++) {
092: fTestName = new String("DateTime test " + dateStyle
093: + "/" + timeStyle + " (" + localeName + ")");
094: try {
095: fFormat = DateFormat.getDateTimeInstance(dateStyle,
096: timeStyle, locale);
097: } catch (StringIndexOutOfBoundsException e) {
098: errln("FAIL: localeTest date/time getDateTimeInstance exception");
099: throw e;
100: }
101: TestFormat();
102: }
103: }
104: }
105:
106: public void TestFormat() {
107: if (fFormat == null) {
108: errln("FAIL: DateFormat creation failed");
109: return;
110: }
111: // logln("TestFormat: " + fTestName);
112: Date now = new Date();
113: tryDate(new Date(0));
114: tryDate(new Date((long) 1278161801778.0));
115: tryDate(now);
116: // Shift 6 months into the future, AT THE SAME TIME OF DAY.
117: // This will test the DST handling.
118: tryDate(new Date(now.getTime() + 6 * 30 * ONEDAY));
119:
120: Date limit = new Date(now.getTime() * 10); // Arbitrary limit
121: for (int i = 0; i < 2; ++i)
122: // tryDate(new Date(floor(randDouble() * limit)));
123: tryDate(new Date((long) (randDouble() * limit.getTime())));
124: }
125:
126: private void describeTest() {
127: if (fFormat == null) {
128: errln("FAIL: no DateFormat");
129: return;
130: }
131:
132: // Assume it's a SimpleDateFormat and get some info
133: SimpleDateFormat s = (SimpleDateFormat) fFormat;
134: logln(fTestName + " Pattern " + s.toPattern());
135: }
136:
137: private void tryDate(Date theDate) {
138: final int DEPTH = 10;
139: Date[] date = new Date[DEPTH];
140: StringBuffer[] string = new StringBuffer[DEPTH];
141:
142: int dateMatch = 0;
143: int stringMatch = 0;
144: boolean dump = false;
145: int i;
146: for (i = 0; i < DEPTH; ++i)
147: string[i] = new StringBuffer();
148: for (i = 0; i < DEPTH; ++i) {
149: if (i == 0)
150: date[i] = theDate;
151: else {
152: try {
153: date[i] = fFormat.parse(string[i - 1].toString());
154: } catch (ParseException e) {
155: describeTest();
156: errln("********** FAIL: Parse of " + string[i - 1]
157: + " failed for locale: "
158: + fFormat.getLocale(ULocale.ACTUAL_LOCALE));
159: dump = true;
160: break;
161: }
162: }
163: FieldPosition position = new FieldPosition(0);
164: fFormat.format(date[i], string[i], position);
165: if (i > 0) {
166: if (dateMatch == 0 && date[i] == date[i - 1])
167: dateMatch = i;
168: else if (dateMatch > 0 && date[i] != date[i - 1]) {
169: describeTest();
170: errln("********** FAIL: Date mismatch after match.");
171: dump = true;
172: break;
173: }
174: if (stringMatch == 0 && string[i] == string[i - 1])
175: stringMatch = i;
176: else if (stringMatch > 0 && string[i] != string[i - 1]) {
177: describeTest();
178: errln("********** FAIL: String mismatch after match.");
179: dump = true;
180: break;
181: }
182: }
183: if (dateMatch > 0 && stringMatch > 0)
184: break;
185: }
186: if (i == DEPTH)
187: --i;
188:
189: if (stringMatch > fLimit || dateMatch > fLimit) {
190: describeTest();
191: errln("********** FAIL: No string and/or date match within "
192: + fLimit + " iterations.");
193: dump = true;
194: }
195:
196: if (dump) {
197: for (int k = 0; k <= i; ++k) {
198: logln("" + k + ": " + date[k] + " F> " + string[k]
199: + " P> ");
200: }
201: }
202: }
203:
204: // Return a random double from 0.01 to 1, inclusive
205: private double randDouble() {
206: if (random == null) {
207: random = createRandom();
208: }
209: // Assume 8-bit (or larger) rand values. Also assume
210: // that the system rand() function is very poor, which it always is.
211: // double d;
212: // int i;
213: // do {
214: // for (i=0; i < sizeof(double); ++i)
215: // {
216: // char poke = (char*)&d;
217: // poke[i] = (rand() & 0xFF);
218: // }
219: // } while (TPlatformUtilities.isNaN(d) || TPlatformUtilities.isInfinite(d));
220:
221: // if (d < 0.0) d = -d;
222: // if (d > 0.0)
223: // {
224: // double e = floor(log10(d));
225: // if (e < -2.0) d *= pow(10.0, -e-2);
226: // else if (e > -1.0) d /= pow(10.0, e+1);
227: // }
228: // return d;
229: return random.nextDouble();
230: }
231:
232: public void TestAvailableLocales() {
233: final ULocale[] locales = DateFormat.getAvailableULocales();
234: long count = locales.length;
235: logln("" + count + " available locales");
236: if (locales != null && count != 0) {
237: StringBuffer all = new StringBuffer();
238: for (int i = 0; i < count; ++i) {
239: if (i != 0)
240: all.append(", ");
241: all.append(locales[i].getDisplayName());
242: }
243: logln(all.toString());
244: } else
245: errln("********** FAIL: Zero available locales or null array pointer");
246: }
247:
248: public void TestMonster() {
249: if (isQuick()) {
250: logln("Skipping test (use -e for exhaustive)");
251: return;
252: }
253: final ULocale[] locales = DateFormat.getAvailableULocales();
254: long count = locales.length;
255: if (locales != null && count != 0) {
256: for (int i = 0; i < count; ++i) {
257: String name = locales[i].getDisplayName();
258: logln("Testing " + name + "...");
259: try {
260: localeTest(locales[i], name);
261: } catch (Exception e) {
262: errln("FAIL: TestMonster localeTest exception" + e);
263: }
264: }
265: }
266: }
267: }
268:
269: //eof
|