001: //##header
002: //#header
003: //#ifndef FOUNDATION
004: /*
005: *******************************************************************************
006: * Copyright (C) 2006, Google, International Business Machines Corporation and *
007: * others. All Rights Reserved. *
008: *******************************************************************************
009: */
010:
011: package com.ibm.icu.dev.test.format;
012:
013: import com.ibm.icu.dev.test.TestFmwk;
014: import com.ibm.icu.impl.PatternTokenizer;
015: import com.ibm.icu.impl.Utility;
016: import com.ibm.icu.text.DateTimePatternGenerator;
017: import com.ibm.icu.text.SimpleDateFormat;
018: import com.ibm.icu.text.UTF16;
019: import com.ibm.icu.text.UnicodeSet;
020: import com.ibm.icu.util.ULocale;
021: import com.ibm.icu.util.UResourceBundle;
022:
023: import java.util.Date;
024: import java.util.Enumeration;
025: import java.util.Random;
026: import java.util.ResourceBundle;
027:
028: public class DateTimeGeneratorTest extends TestFmwk {
029: public static boolean GENERATE_TEST_DATA = System
030: .getProperty("GENERATE_TEST_DATA") != null;
031: public static int RANDOM_COUNT = 1000;
032: public static boolean DEBUG = false;
033:
034: public static void main(String[] args) throws Exception {
035: new DateTimeGeneratorTest().run(args);
036: }
037:
038: public void TestPatternParser() {
039: StringBuffer buffer = new StringBuffer();
040: PatternTokenizer pp = new PatternTokenizer()
041: .setIgnorableCharacters(new UnicodeSet("[-]"))
042: .setSyntaxCharacters(new UnicodeSet("[a-zA-Z]"))
043: .setEscapeCharacters(new UnicodeSet("[b#]"))
044: .setUsingQuote(true);
045: logln("Using Quote");
046: for (int i = 0; i < patternTestData.length; ++i) {
047: String patternTest = (String) patternTestData[i];
048: CheckPattern(buffer, pp, patternTest);
049: }
050: String[] randomSet = { "abcdef", "$12!@#-", "'\\" };
051: for (int i = 0; i < RANDOM_COUNT; ++i) {
052: String patternTest = getRandomString(randomSet, 0, 10);
053: CheckPattern(buffer, pp, patternTest);
054: }
055: logln("Using Backslash");
056: pp.setUsingQuote(false).setUsingSlash(true);
057: for (int i = 0; i < patternTestData.length; ++i) {
058: String patternTest = (String) patternTestData[i];
059: CheckPattern(buffer, pp, patternTest);
060: }
061: for (int i = 0; i < RANDOM_COUNT; ++i) {
062: String patternTest = getRandomString(randomSet, 0, 10);
063: CheckPattern(buffer, pp, patternTest);
064: }
065: }
066:
067: Random random = new java.util.Random(-1);
068:
069: private String getRandomString(String[] randomList, int minLen,
070: int maxLen) {
071: StringBuffer result = new StringBuffer();
072: int len = random.nextInt(maxLen + 1 - minLen) + minLen;
073: for (int i = minLen; i < len; ++i) {
074: String source = randomList[random
075: .nextInt(randomList.length)]; // don't bother with surrogates
076: char ch = source.charAt(random.nextInt(source.length()));
077: UTF16.append(result, ch);
078: }
079: return result.toString();
080: }
081:
082: private void CheckPattern(StringBuffer buffer, PatternTokenizer pp,
083: String patternTest) {
084: pp.setPattern(patternTest);
085: if (DEBUG && isVerbose()) {
086: showItems(buffer, pp, patternTest);
087: }
088: String normalized = pp.setStart(0).normalize();
089: logln("input:\t<" + patternTest + ">" + "\tnormalized:\t<"
090: + normalized + ">");
091: String doubleNormalized = pp.setPattern(normalized).normalize();
092: if (!normalized.equals(doubleNormalized)) {
093: errln("Normalization not idempotent:\t" + patternTest
094: + "\tnormalized: " + normalized + "\tnormalized2: "
095: + doubleNormalized);
096: // allow for debugging at the point of failure
097: if (DEBUG) {
098: pp.setPattern(patternTest);
099: normalized = pp.setStart(0).normalize();
100: pp.setPattern(normalized);
101: showItems(buffer, pp, normalized);
102: doubleNormalized = pp.normalize();
103: }
104: }
105: }
106:
107: private void showItems(StringBuffer buffer, PatternTokenizer pp,
108: String patternTest) {
109: logln("input:\t<" + patternTest + ">");
110: while (true) {
111: buffer.setLength(0);
112: int status = pp.next(buffer);
113: if (status == pp.DONE)
114: break;
115: String lit = "";
116: if (status != pp.SYNTAX) {
117: lit = "\t<" + pp.quoteLiteral(buffer) + ">";
118: }
119: logln("\t" + statusName[status] + "\t<" + buffer + ">"
120: + lit);
121: }
122: }
123:
124: static final String[] statusName = { "DONE", "SYNTAX", "LITERAL",
125: "BROKEN_QUOTE", "BROKEN_ESCAPE", "UNKNOWN" };
126:
127: public void TestBasic() {
128: ULocale uLocale = null;
129: DateTimePatternGenerator dtfg = null;
130: Date date = null;
131: for (int i = 0; i < dateTestData.length; ++i) {
132: if (dateTestData[i] instanceof ULocale) {
133: uLocale = (ULocale) dateTestData[i];
134: dtfg = DateTimePatternGenerator.getInstance(uLocale);
135: if (GENERATE_TEST_DATA)
136: logln("new ULocale(\"" + uLocale.toString()
137: + "\"),");
138: } else if (dateTestData[i] instanceof Date) {
139: date = (Date) dateTestData[i];
140: if (GENERATE_TEST_DATA)
141: logln("new Date(" + date.getYear() + ", "
142: + date.getMonth() + ", " + date.getDate()
143: + ", " + date.getHours() + ", "
144: + date.getMinutes() + ", "
145: + date.getSeconds() + "),");
146: } else if (dateTestData[i] instanceof String) {
147: String testSkeleton = (String) dateTestData[i];
148: String pattern = dtfg.getBestPattern(testSkeleton);
149: SimpleDateFormat sdf = new SimpleDateFormat(pattern,
150: uLocale);
151: String formatted = sdf.format(date);
152: if (GENERATE_TEST_DATA)
153: logln("new String[] {\"" + testSkeleton + "\", \""
154: + Utility.escape(formatted) + "\"},");
155: //logln(uLocale + "\t" + testSkeleton + "\t" + pattern + "\t" + sdf.format(date));
156: } else {
157: String[] testPair = (String[]) dateTestData[i];
158: String testSkeleton = testPair[0];
159: String testFormatted = testPair[1];
160: String pattern = dtfg.getBestPattern(testSkeleton);
161: SimpleDateFormat sdf = new SimpleDateFormat(pattern,
162: uLocale);
163: String formatted = sdf.format(date);
164: if (GENERATE_TEST_DATA) {
165: logln("new String[] {\"" + testSkeleton + "\", \""
166: + Utility.escape(formatted) + "\"},");
167: } else if (!formatted.equals(testFormatted)) {
168: errln(uLocale
169: + "\tformatted string doesn't match test case: "
170: + testSkeleton + "\t generated: " + pattern
171: + "\t expected: " + testFormatted
172: + "\t got: " + formatted);
173: if (true) { // debug
174: pattern = dtfg.getBestPattern(testSkeleton);
175: sdf = new SimpleDateFormat(pattern, uLocale);
176: formatted = sdf.format(date);
177: }
178: }
179: //logln(uLocale + "\t" + testSkeleton + "\t" + pattern + "\t" + sdf.format(date));
180: }
181: }
182: }
183:
184: static final Object[] patternTestData = { "'$f''#c", "'' 'a",
185: "'.''.'", "\\u0061\\\\", "mm.dd 'dd ' x", "'' ''", };
186:
187: // can be generated by using GENERATE_TEST_DATA. Must be reviewed before adding
188: static final Object[] dateTestData = {
189: new Date(99, 0, 13, 23, 58, 59),
190: new ULocale("en_US"),
191: new String[] { "yM", "1/1999" },
192: new String[] { "yMMM", "Jan 1999" },
193: new String[] { "yMd", "1/13/1999" },
194: new String[] { "yMMMd", "Jan/13/1999" },
195: new String[] { "Md", "1/13" },
196: new String[] { "MMMd", "Jan 13" },
197: new String[] { "yQQQ", "Q1 1999" },
198: new String[] { "hhmm", "11:58 PM" },
199: new String[] { "HHmm", "23:58" },
200: new String[] { "mmss", "58:59" },
201: new ULocale("zh_Hans_CN"),
202: new String[] { "yM", "1999-1" },
203: new String[] { "yMMM", "1999-\u4E00\u6708" },
204: new String[] { "yMd", "1999\u5E741\u670813\u65E5" },
205: new String[] { "yMMMd",
206: "1999\u5E74\u4E00\u6708\u670813\u65E5" },
207: new String[] { "Md", "1-13" },
208: new String[] { "MMMd", "\u4E00\u6708-13" },
209: new String[] { "yQQQ", "1\u5B63 1999" },
210: new String[] { "hhmm", "\u4E0B\u534811:58" },
211: new String[] { "HHmm", "\u4E0B\u534811:58" },
212: new String[] { "mmss", "58:59" }, new ULocale("de_DE"),
213: new String[] { "yM", "1.1999" },
214: new String[] { "yMMM", "Jan 1999" },
215: new String[] { "yMd", "13.1.1999" },
216: new String[] { "yMMMd", "13. Jan 1999" },
217: new String[] { "Md", "13.1" },
218: new String[] { "MMMd", "13. Jan" },
219: new String[] { "yQQQ", "Q1 1999" },
220: new String[] { "hhmm", "11:58 nachm." },
221: new String[] { "HHmm", "23:58" },
222: new String[] { "mmss", "58:59" }, new ULocale("fi"),
223: new String[] { "yM", "1.1999" },
224: new String[] { "yMMM", "tammita 1999" },
225: new String[] { "yMd", "13.1.1999" },
226: new String[] { "yMMMd", "13. tammita 1999" },
227: new String[] { "Md", "13.1" },
228: new String[] { "MMMd", "13. tammita" },
229: new String[] { "yQQQ", "1. nelj. 1999" },
230: new String[] { "hhmm", "23.58" },
231: new String[] { "HHmm", "23.58" },
232: new String[] { "mmss", "58.59" }, };
233: }
234: //#endif
235: //eof
|