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