1 /********************************************************************
3 * Copyright (c) 2008-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
15 #include "unicode/calendar.h"
16 #include "unicode/smpdtfmt.h"
17 #include "unicode/dtfmtsym.h"
18 #include "unicode/dtptngen.h"
22 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
23 // try to test the full functionality. It just calls each function in the class and
24 // verifies that it works on a basic level.
26 void IntlTestDateTimePatternGeneratorAPI::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
28 if (exec
) logln("TestSuite DateTimePatternGeneratorAPI");
31 TESTCASE(1, testOptions
);
32 default: name
= ""; break;
39 * Test various generic API methods of DateTimePatternGenerator for API coverage.
41 void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
43 UnicodeString patternData
[] = {
44 UnicodeString("yM"), // 00
45 UnicodeString("yMMM"), // 01
46 UnicodeString("yMd"), // 02
47 UnicodeString("yMMMd"), // 03
48 UnicodeString("Md"), // 04
49 UnicodeString("MMMd"), // 05
50 UnicodeString("MMMMd"), // 06
51 UnicodeString("yQQQ"), // 07
52 UnicodeString("hhmm"), // 08
53 UnicodeString("HHmm"), // 09
54 UnicodeString("jjmm"), // 10
55 UnicodeString("mmss"), // 11
56 UnicodeString("yyyyMMMM"), // 12
57 UnicodeString("MMMEd"), // 13
58 UnicodeString("Ed"), // 14
59 UnicodeString("jmmssSSS"), // 15
63 const char* testLocale
[MAX_LOCALE
][4] = {
64 {"en", "US", "", ""}, // 0
65 {"en", "US", "", "calendar=japanese"}, // 1
66 {"de", "DE", "", ""}, // 2
67 {"fi", "", "", ""}, // 3
68 {"es", "", "", ""}, // 4
69 {"ja", "", "", ""}, // 5
70 {"ja", "", "", "calendar=japanese"}, // 6
71 {"zh", "Hans", "CN", ""}, // 7
72 {"zh", "TW", "", "calendar=roc"}, // 8
73 {"ru", "", "", ""}, // 9
74 {"zh", "", "", "calendar=chinese"}, // 10
77 // For Weds, Jan 13, 1999, 23:58:59
78 UnicodeString patternResults
[] = {
80 UnicodeString("1/1999"), // 00: yM
81 UnicodeString("Jan 1999"), // 01: yMMM
82 UnicodeString("1/13/1999"), // 02: yMd
83 UnicodeString("Jan 13, 1999"), // 03: yMMMd
84 UnicodeString("1/13"), // 04: Md
85 UnicodeString("Jan 13"), // 05: MMMd
86 UnicodeString("January 13"), // 06: MMMMd
87 UnicodeString("Q1 1999"), // 07: yQQQ
88 UnicodeString("11:58 PM"), // 08: hhmm
89 UnicodeString("23:58"), // 09: HHmm
90 UnicodeString("11:58 PM"), // 10: jjmm
91 UnicodeString("58:59"), // 11: mmss
92 UnicodeString("January 1999"), // 12: yyyyMMMM
93 UnicodeString("Wed, Jan 13"), // 13: MMMEd -> EEE, MMM d
94 UnicodeString("13 Wed"), // 14: Ed -> d EEE
95 UnicodeString("11:58:59.123 PM"), // 15: jmmssSSS -> "h:mm:ss.SSS a"
97 // en_US@calendar=japanese // 1 en_US@calendar=japanese
98 UnicodeString("1/11 H"), // 0: yM
99 UnicodeString("Jan 11 Heisei"), // 1: yMMM
100 UnicodeString("1/13/11 H"), // 2: yMd
101 UnicodeString("Jan 13, 11 Heisei"), // 3: yMMMd
102 UnicodeString("1/13"), // 4: Md
103 UnicodeString("Jan 13"), // 5: MMMd
104 UnicodeString("January 13"), // 6: MMMMd
105 UnicodeString("Q1 11 Heisei"), // 7: yQQQ
106 UnicodeString("11:58 PM"), // 8: hhmm
107 UnicodeString("23:58"), // 9: HHmm
108 UnicodeString("11:58 PM"), // 10: jjmm
109 UnicodeString("58:59"), // 11: mmss
110 UnicodeString("January 11 Heisei"), // 12: yyyyMMMM
111 UnicodeString("Wed, Jan 13"), // 13: MMMEd -> EEE, MMM d"
112 UnicodeString("13 Wed"), // 14: Ed -> d EEE
113 UnicodeString("11:58:59.123 PM"), // 15: jmmssSSS -> "h:mm:ss.SSS a"
116 UnicodeString("1.1999"), // 00: yM
117 UnicodeString("Jan. 1999"), // 01: yMMM
118 UnicodeString("13.1.1999"), // 02: yMd
119 UnicodeString("13. Jan. 1999"), // 03: yMMMd
120 UnicodeString("13.1."), // 04: Md
121 UnicodeString("13. Jan."), // 05: MMMd
122 UnicodeString("13. Januar"), // 06: MMMMd
123 UnicodeString("Q1 1999"), // 07: yQQQ
124 UnicodeString("11:58 nachm."), // 08: hhmm
125 UnicodeString("23:58"), // 09: HHmm
126 UnicodeString("23:58"), // 10: jjmm
127 UnicodeString("58:59"), // 11: mmss
128 UnicodeString("Januar 1999"), // 12: yyyyMMMM
129 UnicodeString("Mi., 13. Jan."), // 13: MMMEd -> EEE, d. MMM
130 UnicodeString("Mi., 13."), // 14: Ed -> EEE d.
131 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "HH:mm:ss,SSS"
134 UnicodeString("1.1999"), // 00: yM (fixed expected result per ticket:6626:)
135 UnicodeString("tammi 1999"), // 01: yMMM
136 UnicodeString("13.1.1999"), // 02: yMd
137 UnicodeString("13. tammikuuta 1999"), // 03: yMMMd
138 UnicodeString("13.1."), // 04: Md
139 UnicodeString("13.1."), // 05: MMMd
140 UnicodeString("13. tammikuuta"), // 06: MMMMd
141 UnicodeString("1. nelj. 1999"), // 07: yQQQ
142 UnicodeString("11.58 ip."), // 08: hhmm
143 UnicodeString("23.58"), // 09: HHmm
144 UnicodeString("23.58"), // 10: jjmm
145 UnicodeString("58.59"), // 11: mmss
146 UnicodeString("tammikuu 1999"), // 12: yyyyMMMM
147 UnicodeString("ke 13. tammikuuta"), // 13: MMMEd -> EEE d. MMM
148 UnicodeString("ke 13."), // 14: Ed -> ccc d.
149 UnicodeString("23.58.59,123"), // 15: jmmssSSS -> "H.mm.ss,SSS"
152 UnicodeString("1/1999"), // 00: yM -> "M/y"
153 UnicodeString("ene 1999"), // 01: yMMM -> "MMM y"
154 UnicodeString("13/1/1999"), // 02: yMd -> "d/M/y"
155 UnicodeString("13 ene 1999"), // 03: yMMMd -> "d MMM y"
156 UnicodeString("13/1"), // 04: Md -> "d/M"
157 UnicodeString("13 ene"), // 05: MMMd -> "d MMM"
158 UnicodeString("13 de enero"), // 06: MMMMd -> "d 'de' MMMM"
159 UnicodeString("T1 1999"), // 07: yQQQ -> "QQQ y"
160 UnicodeString("11:58 p.m."), // 08: hhmm -> "hh:mm a"
161 UnicodeString("23:58"), // 09: HHmm -> "HH:mm"
162 UnicodeString("23:58"), // 10: jjmm -> "HH:mm"
163 UnicodeString("58:59"), // 11: mmss -> "mm:ss"
164 UnicodeString("enero de 1999"), // 12: yyyyMMMM -> "MMMM 'de' yyyy"
165 CharsToUnicodeString("mi\\u00E9 13 ene"), // 13: MMMEd -> "E d MMM"
166 CharsToUnicodeString("mi\\u00E9 13"), // 14: Ed -> "EEE d"
167 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "H:mm:ss,SSS"
170 UnicodeString("1999/1"), // 00: yM -> y/M
171 CharsToUnicodeString("1999\\u5E741\\u6708"), // 01: yMMM -> y\u5E74M\u6708
172 UnicodeString("1999/1/13"), // 02: yMd -> y/M/d
173 CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> y\u5E74M\u6708d\u65E5
174 UnicodeString("1/13"), // 04: Md -> M/d
175 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5
176 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
177 UnicodeString("1999/1Q"), // 07: yQQQ -> yQQQ
178 CharsToUnicodeString("\\u5348\\u5F8C11:58"), // 08: hhmm
179 UnicodeString("23:58"), // 09: HHmm -> HH:mm
180 UnicodeString("23:58"), // 10: jjmm
181 UnicodeString("58:59"), // 11: mmss -> mm:ss
182 CharsToUnicodeString("1999\\u5E741\\u6708"), // 12: yyyyMMMM -> y\u5E74M\u6708
183 CharsToUnicodeString("1\\u670813\\u65E5(\\u6C34)"), // 13: MMMEd -> M\u6708d\u65E5(EEE)
184 CharsToUnicodeString("13\\u65E5(\\u6C34)"), // 14: Ed -> d\u65E5(EEE)
185 UnicodeString("23:58:59.123"), // 15: jmmssSSS -> "H:mm:ss.SSS"
187 // ja@calendar=japanese // 6 ja@calendar=japanese
188 CharsToUnicodeString("H11/1"), // 00: yM -> GGGGGy/m
189 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u6708"), // 01: yMMM -> Gy\u5E74M\u6708
190 CharsToUnicodeString("H11/1/13"), // 02: yMd -> GGGGGy/m/d
191 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> Gy\u5E74M\u6708d\u65E5
192 UnicodeString("1/13"), // 04: Md -> M/d
193 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5
194 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
195 CharsToUnicodeString("\\u5E73\\u621011/1Q"), // 07: yQQQ -> Gy/QQQ
196 CharsToUnicodeString("\\u5348\\u5F8C11:58"), // 08: hhmm ->
197 UnicodeString("23:58"), // 09: HHmm -> HH:mm (as for ja)
198 UnicodeString("23:58"), // 10: jjmm
199 UnicodeString("58:59"), // 11: mmss -> mm:ss (as for ja)
200 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u6708"), // 12: yyyyMMMM -> Gyyyy\u5E74M\u6708
201 CharsToUnicodeString("1\\u670813\\u65E5(\\u6C34)"), // 13: MMMEd -> M\u6708d\u65E5(EEE)
202 CharsToUnicodeString("13\\u65E5(\\u6C34)"), // 14: Ed -> d\u65E5(EEE)
203 UnicodeString("23:58:59.123"), // 15: jmmssSSS -> "H:mm:ss.SSS"
205 // zh_Hans_CN // 7 zh_Hans_CN
206 UnicodeString("1999/1", -1, US_INV
), // 00: yM
207 CharsToUnicodeString("1999\\u5E741\\u6708"), // 01: yMMM -> yyyy\u5E74MMM (fixed expected result per ticket:6626:)
208 CharsToUnicodeString("1999/1/13"), // 02: yMd
209 CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> yyyy\u5E74MMMd\u65E5 (fixed expected result per ticket:6626:)
210 UnicodeString("1/13"), // 04: Md
211 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5 (fixed expected result per ticket:6626:)
212 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
213 CharsToUnicodeString("1999\\u5E74\\u7B2C1\\u5B63\\u5EA6"), // 07: yQQQ
214 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm
215 UnicodeString("23:58"), // 09: HHmm
216 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
217 UnicodeString("58:59"), // 11: mmss
218 CharsToUnicodeString("1999\\u5E741\\u6708"), // 12: yyyyMMMM -> yyyy\u5E74MMM
219 CharsToUnicodeString("1\\u670813\\u65E5 \\u5468\\u4E09"), // 13: MMMEd -> MMMd\u65E5EEE
220 CharsToUnicodeString("13\\u65E5\\u5468\\u4E09"), // 14: Ed -> d\u65E5EEE
221 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSSS -> "ah:mm:ss.SSS"
223 // zh_TW@calendar=roc // 8 zh_TW@calendar=roc
224 CharsToUnicodeString("\\u6C11\\u570B88/1"), // 00: yM -> Gy/M
225 CharsToUnicodeString("\\u6C11\\u570B88\\u5E741\\u6708"), // 01: yMMM -> Gy\u5E74M\u6708
226 CharsToUnicodeString("\\u6C11\\u570B88/1/13"), // 02: yMd -> Gy/M/d
227 CharsToUnicodeString("\\u6C11\\u570B88\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> Gy\u5E74M\u6708d\u65E5
228 UnicodeString("1/13"), // 04: Md -> M/d
229 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd ->M\u6708d\u65E5
230 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd ->M\u6708d\u65E5
231 CharsToUnicodeString("\\u6C11\\u570B88\\u5E741\\u5B63"), // 07: yQQQ -> Gy QQQ
232 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm ->
233 UnicodeString("23:58"), // 09: HHmm ->
234 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
235 UnicodeString("58:59"), // 11: mmss ->
236 CharsToUnicodeString("\\u6C11\\u570B88\\u5E741\\u6708"), // 12: yyyyMMMM -> Gy\u5E74M\u670
237 CharsToUnicodeString("1\\u670813\\u65E5\\u9031\\u4E09"), // 13: MMMEd -> M\u6708d\u65E5EEE
238 CharsToUnicodeString("13\\u65E5\\uff08\\u9031\\u4E09\\uff09"), // 14: Ed -> d\u65E5\\uff08EEEi\\uff09
239 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSSS -> "ah:mm:ss.SSS"
242 UnicodeString("01.1999"), // 00: yM -> MM.y
243 CharsToUnicodeString("\\u042F\\u043D\\u0432. 1999"), // 01: yMMM -> LLL y
244 UnicodeString("13.01.1999"), // 02: yMd -> dd.MM.y
245 CharsToUnicodeString("13 \\u044F\\u043D\\u0432. 1999 \\u0433."), // 03: yMMMd -> d MMM y
246 UnicodeString("13.01"), // 04: Md -> dd.MM
247 CharsToUnicodeString("13 \\u044F\\u043D\\u0432."), // 05: MMMd -> d MMM
248 CharsToUnicodeString("13 \\u044F\\u043D\\u0432\\u0430\\u0440\\u044F"), // 06: MMMMd -> d MMMM
249 CharsToUnicodeString("1-\\u0439 \\u043A\\u0432. 1999 \\u0433."), // 07: yQQQ -> y QQQ
250 CharsToUnicodeString("11:58 \\u043F\\u043E\\u0441\\u043B\\u0435 \\u043F\\u043E\\u043B\\u0443\\u0434\\u043D\\u044F"), // 07: hhmm -> hh:mm a
251 UnicodeString("23:58"), // 09: HHmm -> HH:mm
252 UnicodeString("23:58"), // 10: jjmm -> HH:mm
253 UnicodeString("58:59"), // 11: mmss -> mm:ss
254 CharsToUnicodeString("\\u042F\\u043D\\u0432\\u0430\\u0440\\u044C 1999"), // 12: yyyyMMMM -> LLLL y
255 CharsToUnicodeString("\\u0421\\u0440, 13 \\u044F\\u043D\\u0432."), // 13: MMMEd -> ccc, d MMM
256 CharsToUnicodeString("\\u0421\\u0440, 13"), // 14: Ed -> EEE, d
257 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "H:mm:ss,SSS"
259 // zh@calendar=chinese // 10 zh@calendar=chinese
260 CharsToUnicodeString("\\u620A\\u5BC5\\u5E7411\\u6708"), // 00: yM
261 CharsToUnicodeString("\\u620A\\u5BC5\\u5E74\\u5341\\u4E00\\u6708"), // 01: yMMM
262 CharsToUnicodeString("\\u620A\\u5BC5\\u5E7411\\u670826\\u65E5"), // 02: yMd
263 CharsToUnicodeString("\\u620A\\u5BC5\\u5E74\\u5341\\u4E00\\u670826\\u65E5"), // 03: yMMMd
264 UnicodeString("11-26"), // 04: Md
265 CharsToUnicodeString("\\u5341\\u4E00\\u670826\\u65E5"), // 05: MMMd
266 CharsToUnicodeString("\\u5341\\u4E00\\u670826\\u65E5"), // 06: MMMMd
267 CharsToUnicodeString("\\u620A\\u5BC5\\u5E74\\u7b2c\\u56db\\u5B63\\u5EA6"), // 07: yQQQ
268 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm
269 UnicodeString("23:58"), // 09: HHmm
270 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
271 UnicodeString("58:59"), // 11: mmss
272 CharsToUnicodeString("\\u620A\\u5BC5\\u5E74\\u5341\\u4E00\\u6708"), // 12: yyyyMMMM
273 CharsToUnicodeString("\\u5341\\u4E00\\u670826\\u65E5\\u5468\\u4E09"), // 13: MMMEd
274 CharsToUnicodeString("26\\u65E5\\u5468\\u4E09"), // 14: Ed -> d\u65E5EEE
275 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSS
280 UnicodeString patternTests2
[] = {
281 UnicodeString("yyyyMMMdd"),
282 UnicodeString("yyyyqqqq"),
283 UnicodeString("yMMMdd"),
284 UnicodeString("EyyyyMMMdd"),
285 UnicodeString("yyyyMMdd"),
286 UnicodeString("yyyyMMM"),
287 UnicodeString("yyyyMM"),
288 UnicodeString("yyMM"),
289 UnicodeString("yMMMMMd"),
290 UnicodeString("EEEEEMMMMMd"),
291 UnicodeString("MMMd"),
292 UnicodeString("MMMdhmm"),
293 UnicodeString("EMMMdhmms"),
294 UnicodeString("MMdhmm"),
295 UnicodeString("EEEEMMMdhmms"),
296 UnicodeString("yyyyMMMddhhmmss"),
297 UnicodeString("EyyyyMMMddhhmmss"),
298 UnicodeString("hmm"),
299 UnicodeString("hhmm"),
300 UnicodeString("hhmmVVVV"),
303 UnicodeString patternResults2
[] = {
304 UnicodeString("Oct 14, 1999"),
305 UnicodeString("4th quarter 1999"),
306 UnicodeString("Oct 14, 1999"),
307 UnicodeString("Thu, Oct 14, 1999"),
308 UnicodeString("10/14/1999"),
309 UnicodeString("Oct 1999"),
310 UnicodeString("10/1999"),
311 UnicodeString("10/99"),
312 UnicodeString("O 14, 1999"),
313 UnicodeString("T, O 14"),
314 UnicodeString("Oct 14"),
315 UnicodeString("Oct 14, 6:58 AM"),
316 UnicodeString("Thu, Oct 14, 6:58:59 AM"),
317 UnicodeString("10/14, 6:58 AM"),
318 UnicodeString("Thursday, Oct 14, 6:58:59 AM"),
319 UnicodeString("Oct 14, 1999, 6:58:59 AM"),
320 UnicodeString("Thu, Oct 14, 1999, 6:58:59 AM"),
321 UnicodeString("6:58 AM"),
322 UnicodeString("6:58 AM"),
323 UnicodeString("6:58 AM GMT"),
327 // results for getSkeletons() and getPatternForSkeleton()
328 const UnicodeString testSkeletonsResults
[] = {
329 UnicodeString("HH:mm"),
330 UnicodeString("MMMMd"),
331 UnicodeString("MMMMMdd"),
334 const UnicodeString testBaseSkeletonsResults
[] = {
336 UnicodeString("MMMMd"),
337 UnicodeString("MMMMMd"),
340 UnicodeString
newDecimal(" "); // space
341 UnicodeString
newAppendItemName("hrs.");
342 UnicodeString
newAppendItemFormat("{1} {0}");
343 UnicodeString
newDateTimeFormat("{1} {0}");
344 UErrorCode status
= U_ZERO_ERROR
;
345 UnicodeString conflictingPattern
;
346 UDateTimePatternConflict conflictingStatus
= UDATPG_NO_CONFLICT
;
347 (void)conflictingStatus
; // Suppress set but not used warning.
349 // ======= Test CreateInstance with default locale
350 logln("Testing DateTimePatternGenerator createInstance from default locale");
352 DateTimePatternGenerator
*instFromDefaultLocale
=DateTimePatternGenerator::createInstance(status
);
353 if (U_FAILURE(status
)) {
354 dataerrln("ERROR: Could not create DateTimePatternGenerator (default) - exitting");
358 delete instFromDefaultLocale
;
361 // ======= Test CreateInstance with given locale
362 logln("Testing DateTimePatternGenerator createInstance from French locale");
363 status
= U_ZERO_ERROR
;
364 DateTimePatternGenerator
*instFromLocale
=DateTimePatternGenerator::createInstance(Locale::getFrench(), status
);
365 if (U_FAILURE(status
)) {
366 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting");
370 // ======= Test clone DateTimePatternGenerator
371 logln("Testing DateTimePatternGenerator::clone()");
372 status
= U_ZERO_ERROR
;
375 UnicodeString decimalSymbol
= instFromLocale
->getDecimal();
376 UnicodeString newDecimalSymbol
= UnicodeString("*");
377 decimalSymbol
= instFromLocale
->getDecimal();
378 instFromLocale
->setDecimal(newDecimalSymbol
);
379 DateTimePatternGenerator
*cloneDTPatternGen
=instFromLocale
->clone();
380 decimalSymbol
= cloneDTPatternGen
->getDecimal();
381 if (decimalSymbol
!= newDecimalSymbol
) {
382 errln("ERROR: inconsistency is found in cloned object.");
384 if ( !(*cloneDTPatternGen
== *instFromLocale
) ) {
385 errln("ERROR: inconsistency is found in cloned object.");
388 if ( *cloneDTPatternGen
!= *instFromLocale
) {
389 errln("ERROR: inconsistency is found in cloned object.");
392 delete instFromLocale
;
393 delete cloneDTPatternGen
;
395 // ======= Test simple use cases
396 logln("Testing simple use cases");
397 status
= U_ZERO_ERROR
;
398 Locale deLocale
=Locale::getGermany();
399 UDate sampleDate
=LocaleTest::date(99, 9, 13, 23, 58, 59);
400 DateTimePatternGenerator
*gen
= DateTimePatternGenerator::createInstance(deLocale
, status
);
401 if (U_FAILURE(status
)) {
402 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting");
405 UnicodeString findPattern
= gen
->getBestPattern(UnicodeString("MMMddHmm"), status
);
406 SimpleDateFormat
*format
= new SimpleDateFormat(findPattern
, deLocale
, status
);
407 if (U_FAILURE(status
)) {
408 dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())");
412 TimeZone
*zone
= TimeZone::createTimeZone(UnicodeString("ECT"));
414 dataerrln("ERROR: Could not create TimeZone ECT");
419 format
->setTimeZone(*zone
);
420 UnicodeString dateReturned
, expectedResult
;
421 dateReturned
.remove();
422 dateReturned
= format
->format(sampleDate
, dateReturned
, status
);
423 expectedResult
=UnicodeString("14. Okt. 08:58", -1, US_INV
);
424 if ( dateReturned
!= expectedResult
) {
425 errln("ERROR: Simple test in getBestPattern with Locale::getGermany()).");
428 status
= U_ZERO_ERROR
;
429 conflictingStatus
= gen
->addPattern(UnicodeString("d'. von' MMMM", -1, US_INV
), true, conflictingPattern
, status
);
430 if (U_FAILURE(status
)) {
431 errln("ERROR: Could not addPattern - d\'. von\' MMMM");
433 status
= U_ZERO_ERROR
;
434 UnicodeString testPattern
=gen
->getBestPattern(UnicodeString("MMMMdd"), status
);
435 testPattern
=gen
->getBestPattern(UnicodeString("MMMddHmm"), status
);
436 format
->applyPattern(gen
->getBestPattern(UnicodeString("MMMMdHmm"), status
));
437 dateReturned
.remove();
438 dateReturned
= format
->format(sampleDate
, dateReturned
, status
);
439 expectedResult
=UnicodeString("14. von Oktober 08:58", -1, US_INV
);
440 if ( dateReturned
!= expectedResult
) {
441 errln(UnicodeString("ERROR: Simple test addPattern failed!: d\'. von\' MMMM Got: ") + dateReturned
+ UnicodeString(" Expected: ") + expectedResult
);
445 // get a pattern and modify it
446 format
= (SimpleDateFormat
*)DateFormat::createDateTimeInstance(DateFormat::kFull
, DateFormat::kFull
,
448 format
->setTimeZone(*zone
);
449 UnicodeString pattern
;
450 pattern
= format
->toPattern(pattern
);
451 dateReturned
.remove();
452 dateReturned
= format
->format(sampleDate
, dateReturned
, status
);
453 expectedResult
=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Mitteleurop\\u00E4ische Sommerzeit");
454 if ( dateReturned
!= expectedResult
) {
455 errln("ERROR: Simple test uses full date format.");
456 errln(UnicodeString(" Got: ") + dateReturned
+ UnicodeString(" Expected: ") + expectedResult
);
459 // modify it to change the zone.
460 UnicodeString newPattern
= gen
->replaceFieldTypes(pattern
, UnicodeString("vvvv"), status
);
461 format
->applyPattern(newPattern
);
462 dateReturned
.remove();
463 dateReturned
= format
->format(sampleDate
, dateReturned
, status
);
464 expectedResult
=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Mitteleurop\\u00E4ische Zeit");
465 if ( dateReturned
!= expectedResult
) {
466 errln("ERROR: Simple test modify the timezone!");
467 errln(UnicodeString(" Got: ")+ dateReturned
+ UnicodeString(" Expected: ") + expectedResult
);
470 // setDeciaml(), getDeciaml()
471 gen
->setDecimal(newDecimal
);
472 if (newDecimal
!= gen
->getDecimal()) {
473 errln("ERROR: unexpected result from setDecimal() and getDecimal()!.\n");
476 // setAppenItemName() , getAppendItemName()
477 gen
->setAppendItemName(UDATPG_HOUR_FIELD
, newAppendItemName
);
478 if (newAppendItemName
!= gen
->getAppendItemName(UDATPG_HOUR_FIELD
)) {
479 errln("ERROR: unexpected result from setAppendItemName() and getAppendItemName()!.\n");
482 // setAppenItemFormat() , getAppendItemFormat()
483 gen
->setAppendItemFormat(UDATPG_HOUR_FIELD
, newAppendItemFormat
);
484 if (newAppendItemFormat
!= gen
->getAppendItemFormat(UDATPG_HOUR_FIELD
)) {
485 errln("ERROR: unexpected result from setAppendItemFormat() and getAppendItemFormat()!.\n");
488 // setDateTimeFormat() , getDateTimeFormat()
489 gen
->setDateTimeFormat(newDateTimeFormat
);
490 if (newDateTimeFormat
!= gen
->getDateTimeFormat()) {
491 errln("ERROR: unexpected result from setDateTimeFormat() and getDateTimeFormat()!.\n");
494 // ======== Test getSkeleton and getBaseSkeleton
495 status
= U_ZERO_ERROR
;
496 pattern
= UnicodeString("dd-MMM");
497 UnicodeString expectedSkeleton
= UnicodeString("MMMdd");
498 UnicodeString expectedBaseSkeleton
= UnicodeString("MMMd");
499 UnicodeString retSkeleton
= gen
->getSkeleton(pattern
, status
);
500 if(U_FAILURE(status
) || retSkeleton
!= expectedSkeleton
) {
501 errln("ERROR: Unexpected result from getSkeleton().\n");
502 errln(UnicodeString(" Got: ") + retSkeleton
+ UnicodeString(" Expected: ") + expectedSkeleton
);
504 retSkeleton
= gen
->getBaseSkeleton(pattern
, status
);
505 if(U_FAILURE(status
) || retSkeleton
!= expectedBaseSkeleton
) {
506 errln("ERROR: Unexpected result from getBaseSkeleton().\n");
507 errln(UnicodeString(" Got: ") + retSkeleton
+ UnicodeString(" Expected:")+ expectedBaseSkeleton
);
510 pattern
= UnicodeString("dd/MMMM/yy");
511 expectedSkeleton
= UnicodeString("yyMMMMdd");
512 expectedBaseSkeleton
= UnicodeString("yMMMMd");
513 retSkeleton
= gen
->getSkeleton(pattern
, status
);
514 if(U_FAILURE(status
) || retSkeleton
!= expectedSkeleton
) {
515 errln("ERROR: Unexpected result from getSkeleton().\n");
516 errln(UnicodeString(" Got: ") + retSkeleton
+ UnicodeString(" Expected: ") + expectedSkeleton
);
518 retSkeleton
= gen
->getBaseSkeleton(pattern
, status
);
519 if(U_FAILURE(status
) || retSkeleton
!= expectedBaseSkeleton
) {
520 errln("ERROR: Unexpected result from getBaseSkeleton().\n");
521 errln(UnicodeString(" Got: ") + retSkeleton
+ UnicodeString(" Expected:")+ expectedBaseSkeleton
);
529 status
= U_ZERO_ERROR
;
530 pattern
= UnicodeString("YYYYMMM");
531 UnicodeString expR
= CharsToUnicodeString("1999\\u5E741\\u6708"); // fixed expected result per ticket:6626:
533 UDate testDate1
= LocaleTest::date(99, 0, 13, 23, 58, 59);
534 DateTimePatternGenerator
*patGen
=DateTimePatternGenerator::createInstance(loc
, status
);
535 if(U_FAILURE(status
)) {
536 dataerrln("ERROR: Could not create DateTimePatternGenerator");
539 UnicodeString bPattern
= patGen
->getBestPattern(pattern
, status
);
541 SimpleDateFormat
sdf(bPattern
, loc
, status
);
543 rDate
= sdf
.format(testDate1
, rDate
);
545 logln(UnicodeString(" ja locale with skeleton: YYYYMMM Best Pattern:") + bPattern
);
546 logln(UnicodeString(" Formatted date:") + rDate
);
548 if ( expR
!= rDate
) {
549 errln(UnicodeString("\nERROR: Test Japanese month hack Got: ") + rDate
+
550 UnicodeString(" Expected: ") + expR
);
557 UnicodeString expR
= CharsToUnicodeString("1999\\u5E741\\u6708"); // fixed expected result per ticket:6626:
558 UDate testDate1
= LocaleTest::date(99, 0, 13, 23, 58, 59);
559 DateTimePatternGenerator
*patGen
=DateTimePatternGenerator::createInstance(loc
, status
);
560 if(U_FAILURE(status
)) {
561 dataerrln("ERROR: Could not create DateTimePatternGenerator");
564 UnicodeString bPattern
= patGen
->getBestPattern(pattern
, status
);
566 SimpleDateFormat
sdf(bPattern
, loc
, status
);
568 rDate
= sdf
.format(testDate1
, rDate
);
570 logln(UnicodeString(" zh locale with skeleton: YYYYMMM Best Pattern:") + bPattern
);
571 logln(UnicodeString(" Formatted date:") + rDate
);
572 if ( expR
!= rDate
) {
573 errln(UnicodeString("\nERROR: Test Chinese month hack Got: ") + rDate
+
574 UnicodeString(" Expected: ") + expR
);
580 // Trac# 6172 duplicate time pattern
581 status
= U_ZERO_ERROR
;
582 pattern
= UnicodeString("hmv");
583 UnicodeString expR
= UnicodeString("h:mm a v"); // avail formats has hm -> "h:mm a" (fixed expected result per ticket:6626:)
585 DateTimePatternGenerator
*patGen
=DateTimePatternGenerator::createInstance(loc
, status
);
586 if(U_FAILURE(status
)) {
587 dataerrln("ERROR: Could not create DateTimePatternGenerator");
590 UnicodeString bPattern
= patGen
->getBestPattern(pattern
, status
);
591 logln(UnicodeString(" en locale with skeleton: hmv Best Pattern:") + bPattern
);
593 if ( expR
!= bPattern
) {
594 errln(UnicodeString("\nERROR: Test EN time format Got: ") + bPattern
+
595 UnicodeString(" Expected: ") + expR
);
602 // ======= Test various skeletons.
603 logln("Testing DateTimePatternGenerator with various skeleton");
605 status
= U_ZERO_ERROR
;
606 int32_t localeIndex
=0;
607 int32_t resultIndex
=0;
608 UnicodeString resultDate
;
609 UDate testDate
= LocaleTest::date(99, 0, 13, 23, 58, 59) + 123.0;
610 while (localeIndex
< MAX_LOCALE
)
613 UnicodeString bestPattern
;
615 Locale
loc(testLocale
[localeIndex
][0], testLocale
[localeIndex
][1], testLocale
[localeIndex
][2], testLocale
[localeIndex
][3]);
616 logln("\n\n Locale: %s_%s_%s@%s", testLocale
[localeIndex
][0], testLocale
[localeIndex
][1], testLocale
[localeIndex
][2], testLocale
[localeIndex
][3]);
617 DateTimePatternGenerator
*patGen
=DateTimePatternGenerator::createInstance(loc
, status
);
618 if(U_FAILURE(status
)) {
619 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale index:%d . - exitting\n", localeIndex
);
622 while (patternData
[dataIndex
].length() > 0) {
623 log(patternData
[dataIndex
]);
624 bestPattern
= patGen
->getBestPattern(patternData
[dataIndex
++], status
);
625 logln(UnicodeString(" -> ") + bestPattern
);
627 SimpleDateFormat
sdf(bestPattern
, loc
, status
);
629 resultDate
= sdf
.format(testDate
, resultDate
);
630 if ( resultDate
!= patternResults
[resultIndex
] ) {
631 errln(UnicodeString("\nERROR: Test various skeletons[") + (dataIndex
-1) + UnicodeString("], localeIndex ") + localeIndex
+
632 UnicodeString(". Got: \"") + resultDate
+ UnicodeString("\" Expected: \"") + patternResults
[resultIndex
] + "\"" );
641 // ======= More tests ticket#6110
642 logln("Testing DateTimePatternGenerator with various skeleton");
644 status
= U_ZERO_ERROR
;
647 testDate
= LocaleTest::date(99, 9, 13, 23, 58, 59);
650 UnicodeString bestPattern
;
651 logln("\n\n Test various skeletons for English locale...");
652 DateTimePatternGenerator
*patGen
=DateTimePatternGenerator::createInstance(Locale::getEnglish(), status
);
653 if(U_FAILURE(status
)) {
654 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale English . - exitting\n");
657 TimeZone
*enZone
= TimeZone::createTimeZone(UnicodeString("ECT/GMT"));
659 dataerrln("ERROR: Could not create TimeZone ECT");
663 SimpleDateFormat
*enFormat
= (SimpleDateFormat
*)DateFormat::createDateTimeInstance(DateFormat::kFull
,
664 DateFormat::kFull
, Locale::getEnglish());
665 enFormat
->setTimeZone(*enZone
);
666 while (patternTests2
[dataIndex
].length() > 0) {
667 logln(patternTests2
[dataIndex
]);
668 bestPattern
= patGen
->getBestPattern(patternTests2
[dataIndex
], status
);
669 logln(UnicodeString(" -> ") + bestPattern
);
670 enFormat
->applyPattern(bestPattern
);
672 resultDate
= enFormat
->format(testDate
, resultDate
);
673 if ( resultDate
!= patternResults2
[resultIndex
] ) {
674 errln(UnicodeString("\nERROR: Test various skeletons[") + dataIndex
675 + UnicodeString("]. Got: ") + resultDate
+ UnicodeString(" Expected: ") +
676 patternResults2
[resultIndex
] );
688 // ======= Test random skeleton
689 DateTimePatternGenerator
*randDTGen
= DateTimePatternGenerator::createInstance(status
);
690 if (U_FAILURE(status
)) {
691 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting");
696 for (i
=0; i
<10; ++i
) {
697 UnicodeString randomSkeleton
;
698 int32_t len
= rand() % 20;
699 for (int32_t j
=0; j
<len
; ++j
) {
700 while ((newChar
= (UChar
)(rand()%0x7f
))>=(UChar
)0x20) {
701 randomSkeleton
+= newChar
;
704 UnicodeString bestPattern
= randDTGen
->getBestPattern(randomSkeleton
, status
);
708 // UnicodeString randomString=Unicode
709 // ======= Test getStaticClassID()
711 logln("Testing getStaticClassID()");
712 status
= U_ZERO_ERROR
;
713 DateTimePatternGenerator
*test
= DateTimePatternGenerator::createInstance(status
);
715 if(test
->getDynamicClassID() != DateTimePatternGenerator::getStaticClassID()) {
716 errln("ERROR: getDynamicClassID() didn't return the expected value");
720 // ====== Test createEmptyInstance()
722 logln("Testing createEmptyInstance()");
723 status
= U_ZERO_ERROR
;
725 test
= DateTimePatternGenerator::createEmptyInstance(status
);
726 if(U_FAILURE(status
)) {
727 errln("ERROR: Fail to create an empty instance ! - exitting.\n");
732 conflictingStatus
= test
->addPattern(UnicodeString("MMMMd"), true, conflictingPattern
, status
);
733 status
= U_ZERO_ERROR
;
734 testPattern
=test
->getBestPattern(UnicodeString("MMMMdd"), status
);
735 conflictingStatus
= test
->addPattern(UnicodeString("HH:mm"), true, conflictingPattern
, status
);
736 conflictingStatus
= test
->addPattern(UnicodeString("MMMMMdd"), true, conflictingPattern
, status
); //duplicate pattern
737 StringEnumeration
*output
=NULL
;
738 output
= test
->getRedundants(status
);
739 expectedResult
=UnicodeString("MMMMd");
740 if (output
!= NULL
) {
741 output
->reset(status
);
742 const UnicodeString
*dupPattern
=output
->snext(status
);
743 if ( (dupPattern
==NULL
) || (*dupPattern
!= expectedResult
) ) {
744 errln("ERROR: Fail in getRedundants !\n");
748 // ======== Test getSkeletons and getBaseSkeletons
749 StringEnumeration
* ptrSkeletonEnum
= test
->getSkeletons(status
);
750 if(U_FAILURE(status
)) {
751 errln("ERROR: Fail to get skeletons !\n");
753 UnicodeString returnPattern
, *ptrSkeleton
;
754 ptrSkeletonEnum
->reset(status
);
755 int32_t count
=ptrSkeletonEnum
->count(status
);
756 for (i
=0; i
<count
; ++i
) {
757 ptrSkeleton
= (UnicodeString
*)ptrSkeletonEnum
->snext(status
);
758 returnPattern
= test
->getPatternForSkeleton(*ptrSkeleton
);
759 if ( returnPattern
!= testSkeletonsResults
[i
] ) {
760 errln(UnicodeString("ERROR: Unexpected result from getSkeletons and getPatternForSkeleton\nGot: ") + returnPattern
761 + UnicodeString("\nExpected: ") + testSkeletonsResults
[i
]
762 + UnicodeString("\n"));
765 StringEnumeration
* ptrBaseSkeletonEnum
= test
->getBaseSkeletons(status
);
766 if(U_FAILURE(status
)) {
767 errln("ERROR: Fail to get base skeletons !\n");
769 count
=ptrBaseSkeletonEnum
->count(status
);
770 for (i
=0; i
<count
; ++i
) {
771 ptrSkeleton
= (UnicodeString
*)ptrBaseSkeletonEnum
->snext(status
);
772 if ( *ptrSkeleton
!= testBaseSkeletonsResults
[i
] ) {
773 errln("ERROR: Unexpected result from getBaseSkeletons() !\n");
777 // ========= DateTimePatternGenerator sample code in Userguide
778 // set up the generator
779 Locale locale
= Locale::getFrench();
780 status
= U_ZERO_ERROR
;
781 DateTimePatternGenerator
*generator
= DateTimePatternGenerator::createInstance( locale
, status
);
783 // get a pattern for an abbreviated month and day
784 pattern
= generator
->getBestPattern(UnicodeString("MMMd"), status
);
785 SimpleDateFormat
formatter(pattern
, locale
, status
);
787 zone
= TimeZone::createTimeZone(UnicodeString("GMT"));
788 formatter
.setTimeZone(*zone
);
789 // use it to format (or parse)
790 UnicodeString formatted
;
791 formatted
= formatter
.format(Calendar::getNow(), formatted
, status
);
792 // for French, the result is "13 sept."
794 // cannot use the result from getNow() because the value change evreyday.
795 testDate
= LocaleTest::date(99, 0, 13, 23, 58, 59);
796 formatted
= formatter
.format(testDate
, formatted
, status
);
797 expectedResult
=UnicodeString("14 janv.");
798 if ( formatted
!= expectedResult
) {
799 errln("ERROR: Userguide sample code result!");
800 errln(UnicodeString(" Got: ")+ formatted
+ UnicodeString(" Expected: ") + expectedResult
);
805 delete ptrSkeletonEnum
;
806 delete ptrBaseSkeletonEnum
;
812 * Test handling of options
814 * For reference, as of ICU 4.3.3,
830 typedef struct DTPtnGenOptionsData
{
833 const char *expectedPattern
;
834 UDateTimePatternMatchOptions options
;
835 } DTPtnGenOptionsData
;
836 void IntlTestDateTimePatternGeneratorAPI::testOptions(/*char *par*/)
838 DTPtnGenOptionsData testData
[] = {
839 // locale skel expectedPattern options
840 { "en", "Hmm", "HH:mm", UDATPG_MATCH_NO_OPTIONS
},
841 { "en", "HHmm", "HH:mm", UDATPG_MATCH_NO_OPTIONS
},
842 { "en", "hhmm", "h:mm a", UDATPG_MATCH_NO_OPTIONS
},
843 { "en", "Hmm", "HH:mm", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
844 { "en", "HHmm", "HH:mm", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
845 { "en", "hhmm", "hh:mm a", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
846 { "be", "Hmm", "HH.mm", UDATPG_MATCH_NO_OPTIONS
},
847 { "be", "HHmm", "HH.mm", UDATPG_MATCH_NO_OPTIONS
},
848 { "be", "hhmm", "h.mm a", UDATPG_MATCH_NO_OPTIONS
},
849 { "be", "Hmm", "H.mm", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
850 { "be", "HHmm", "HH.mm", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
851 { "be", "hhmm", "hh.mm a", UDATPG_MATCH_HOUR_FIELD_LENGTH
},
853 { "en", "yyyy", "yyyy", UDATPG_MATCH_NO_OPTIONS
},
854 { "en", "YYYY", "YYYY", UDATPG_MATCH_NO_OPTIONS
},
855 { "en", "U", "y", UDATPG_MATCH_NO_OPTIONS
},
856 { "en@calendar=japanese", "yyyy", "y G", UDATPG_MATCH_NO_OPTIONS
},
857 { "en@calendar=japanese", "YYYY", "Y G", UDATPG_MATCH_NO_OPTIONS
},
858 { "en@calendar=japanese", "U", "y G", UDATPG_MATCH_NO_OPTIONS
},
859 { "en@calendar=chinese", "yyyy", "U", UDATPG_MATCH_NO_OPTIONS
},
860 { "en@calendar=chinese", "YYYY", "Y", UDATPG_MATCH_NO_OPTIONS
},
861 { "en@calendar=chinese", "U", "U", UDATPG_MATCH_NO_OPTIONS
},
864 int count
= sizeof(testData
) / sizeof(testData
[0]);
865 const DTPtnGenOptionsData
* testDataPtr
= testData
;
867 for (; count
-- > 0; ++testDataPtr
) {
868 UErrorCode status
= U_ZERO_ERROR
;
870 Locale
locale(testDataPtr
->locale
);
871 UnicodeString
skel(testDataPtr
->skel
);
872 UnicodeString
expectedPattern(testDataPtr
->expectedPattern
);
873 UDateTimePatternMatchOptions options
= testDataPtr
->options
;
875 DateTimePatternGenerator
* dtpgen
= DateTimePatternGenerator::createInstance(locale
, status
);
876 if (U_FAILURE(status
)) {
877 dataerrln("Unable to create DateTimePatternGenerator instance for locale(%s): %s", locale
.getName(), u_errorName(status
));
881 UnicodeString pattern
= dtpgen
->getBestPattern(skel
, options
, status
);
882 if (pattern
.compare(expectedPattern
) != 0) {
883 errln( UnicodeString("ERROR in getBestPattern, locale ") + UnicodeString(testDataPtr
->locale
) +
884 UnicodeString(", skeleton ") + skel
+
885 ((options
)?UnicodeString(", options!=0"):UnicodeString(", options==0")) +
886 UnicodeString(", expected pattern ") + expectedPattern
+
887 UnicodeString(", got ") + pattern
);
893 #endif /* #if !UCONFIG_NO_FORMATTING */