]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/dtptngts.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / dtptngts.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
46f4442e 3/********************************************************************
2ca993e8
A
4 * COPYRIGHT:
5 * Copyright (c) 2008-2016, International Business Machines Corporation and
46f4442e
A
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9#include "unicode/utypes.h"
10
11#if !UCONFIG_NO_FORMATTING
12
13#include <stdio.h>
14#include <stdlib.h>
2ca993e8 15#include "dtptngts.h"
46f4442e
A
16
17#include "unicode/calendar.h"
18#include "unicode/smpdtfmt.h"
19#include "unicode/dtfmtsym.h"
20#include "unicode/dtptngen.h"
0f5d89e8 21#include "unicode/ustring.h"
3d1f044b 22#include "unicode/datefmt.h"
2ca993e8 23#include "cmemory.h"
3d1f044b 24#include "cstring.h"
46f4442e
A
25#include "loctest.h"
26
27
28// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
29// try to test the full functionality. It just calls each function in the class and
30// verifies that it works on a basic level.
31
32void IntlTestDateTimePatternGeneratorAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
33{
34 if (exec) logln("TestSuite DateTimePatternGeneratorAPI");
35 switch (index) {
36 TESTCASE(0, testAPI);
729e4ab9 37 TESTCASE(1, testOptions);
57a6839d 38 TESTCASE(2, testAllFieldPatterns);
2ca993e8
A
39 TESTCASE(3, testStaticGetSkeleton);
40 TESTCASE(4, testC);
0f5d89e8
A
41 TESTCASE(5, testSkeletonsWithDayPeriods);
42 TESTCASE(6, testGetFieldDisplayNames);
3d1f044b 43 TESTCASE(7, testJjMapping);
46f4442e
A
44 default: name = ""; break;
45 }
46}
47
f3c0d7a5 48#define MAX_LOCALE 12
46f4442e
A
49
50/**
51 * Test various generic API methods of DateTimePatternGenerator for API coverage.
52 */
53void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
54{
55 UnicodeString patternData[] = {
729e4ab9
A
56 UnicodeString("yM"), // 00
57 UnicodeString("yMMM"), // 01
58 UnicodeString("yMd"), // 02
59 UnicodeString("yMMMd"), // 03
60 UnicodeString("Md"), // 04
61 UnicodeString("MMMd"), // 05
4388f060
A
62 UnicodeString("MMMMd"), // 06
63 UnicodeString("yQQQ"), // 07
64 UnicodeString("hhmm"), // 08
65 UnicodeString("HHmm"), // 09
66 UnicodeString("jjmm"), // 10
67 UnicodeString("mmss"), // 11
68 UnicodeString("yyyyMMMM"), // 12
69 UnicodeString("MMMEd"), // 13
70 UnicodeString("Ed"), // 14
71 UnicodeString("jmmssSSS"), // 15
57a6839d 72 UnicodeString("JJmm"), // 16
46f4442e
A
73 UnicodeString(),
74 };
2ca993e8 75
46f4442e 76 const char* testLocale[MAX_LOCALE][4] = {
729e4ab9
A
77 {"en", "US", "", ""}, // 0
78 {"en", "US", "", "calendar=japanese"}, // 1
79 {"de", "DE", "", ""}, // 2
80 {"fi", "", "", ""}, // 3
4388f060
A
81 {"es", "", "", ""}, // 4
82 {"ja", "", "", ""}, // 5
83 {"ja", "", "", "calendar=japanese"}, // 6
84 {"zh", "Hans", "CN", ""}, // 7
85 {"zh", "TW", "", "calendar=roc"}, // 8
86 {"ru", "", "", ""}, // 9
57a6839d 87 {"zh", "", "", "calendar=chinese;numbers=hanidays"}, // 10
f3c0d7a5 88 {"ar", "", "", ""}, // 11
46f4442e 89 };
2ca993e8 90
729e4ab9 91 // For Weds, Jan 13, 1999, 23:58:59
46f4442e 92 UnicodeString patternResults[] = {
729e4ab9
A
93 // en_US // 0 en_US
94 UnicodeString("1/1999"), // 00: yM
95 UnicodeString("Jan 1999"), // 01: yMMM
96 UnicodeString("1/13/1999"), // 02: yMd
97 UnicodeString("Jan 13, 1999"), // 03: yMMMd
98 UnicodeString("1/13"), // 04: Md
99 UnicodeString("Jan 13"), // 05: MMMd
4388f060
A
100 UnicodeString("January 13"), // 06: MMMMd
101 UnicodeString("Q1 1999"), // 07: yQQQ
102 UnicodeString("11:58 PM"), // 08: hhmm
103 UnicodeString("23:58"), // 09: HHmm
104 UnicodeString("11:58 PM"), // 10: jjmm
105 UnicodeString("58:59"), // 11: mmss
106 UnicodeString("January 1999"), // 12: yyyyMMMM
107 UnicodeString("Wed, Jan 13"), // 13: MMMEd -> EEE, MMM d
108 UnicodeString("13 Wed"), // 14: Ed -> d EEE
109 UnicodeString("11:58:59.123 PM"), // 15: jmmssSSS -> "h:mm:ss.SSS a"
57a6839d 110 UnicodeString("11:58"), // 16: JJmm
729e4ab9
A
111
112 // en_US@calendar=japanese // 1 en_US@calendar=japanese
4388f060 113 UnicodeString("1/11 H"), // 0: yM
729e4ab9 114 UnicodeString("Jan 11 Heisei"), // 1: yMMM
4388f060
A
115 UnicodeString("1/13/11 H"), // 2: yMd
116 UnicodeString("Jan 13, 11 Heisei"), // 3: yMMMd
729e4ab9
A
117 UnicodeString("1/13"), // 4: Md
118 UnicodeString("Jan 13"), // 5: MMMd
4388f060
A
119 UnicodeString("January 13"), // 6: MMMMd
120 UnicodeString("Q1 11 Heisei"), // 7: yQQQ
121 UnicodeString("11:58 PM"), // 8: hhmm
122 UnicodeString("23:58"), // 9: HHmm
123 UnicodeString("11:58 PM"), // 10: jjmm
124 UnicodeString("58:59"), // 11: mmss
125 UnicodeString("January 11 Heisei"), // 12: yyyyMMMM
126 UnicodeString("Wed, Jan 13"), // 13: MMMEd -> EEE, MMM d"
127 UnicodeString("13 Wed"), // 14: Ed -> d EEE
128 UnicodeString("11:58:59.123 PM"), // 15: jmmssSSS -> "h:mm:ss.SSS a"
57a6839d 129 UnicodeString("11:58"), // 16: JJmm
729e4ab9
A
130
131 // de_DE // 2 de_DE
132 UnicodeString("1.1999"), // 00: yM
51004dcb 133 UnicodeString("Jan. 1999"), // 01: yMMM
729e4ab9 134 UnicodeString("13.1.1999"), // 02: yMd
51004dcb 135 UnicodeString("13. Jan. 1999"), // 03: yMMMd
729e4ab9 136 UnicodeString("13.1."), // 04: Md
51004dcb 137 UnicodeString("13. Jan."), // 05: MMMd
4388f060
A
138 UnicodeString("13. Januar"), // 06: MMMMd
139 UnicodeString("Q1 1999"), // 07: yQQQ
d25163bf 140 UnicodeString("11:58 PM"), // 08: hhmm
4388f060
A
141 UnicodeString("23:58"), // 09: HHmm
142 UnicodeString("23:58"), // 10: jjmm
143 UnicodeString("58:59"), // 11: mmss
144 UnicodeString("Januar 1999"), // 12: yyyyMMMM
2ca993e8 145 UnicodeString("Mi. 13. Jan."), // 13: MMMEd -> E d. MMM
d25163bf 146 UnicodeString("Mi. 13."), // 14: Ed -> E d.
4388f060 147 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "HH:mm:ss,SSS"
57a6839d 148 UnicodeString("23:58"), // 16: JJmm
729e4ab9
A
149
150 // fi // 3 fi
151 UnicodeString("1.1999"), // 00: yM (fixed expected result per ticket:6626:)
152 UnicodeString("tammi 1999"), // 01: yMMM
153 UnicodeString("13.1.1999"), // 02: yMd
f3c0d7a5 154 UnicodeString("13.1.1999"), // 03: yMMMd
729e4ab9 155 UnicodeString("13.1."), // 04: Md
4388f060
A
156 UnicodeString("13.1."), // 05: MMMd
157 UnicodeString("13. tammikuuta"), // 06: MMMMd
158 UnicodeString("1. nelj. 1999"), // 07: yQQQ
159 UnicodeString("11.58 ip."), // 08: hhmm
160 UnicodeString("23.58"), // 09: HHmm
161 UnicodeString("23.58"), // 10: jjmm
162 UnicodeString("58.59"), // 11: mmss
163 UnicodeString("tammikuu 1999"), // 12: yyyyMMMM
f3c0d7a5 164 UnicodeString("ke 13.1."), // 13: MMMEd -> EEE d.M.
4388f060
A
165 UnicodeString("ke 13."), // 14: Ed -> ccc d.
166 UnicodeString("23.58.59,123"), // 15: jmmssSSS -> "H.mm.ss,SSS"
57a6839d 167 UnicodeString("23.58"), // 16: JJmm
4388f060
A
168
169 // es // 4 es
170 UnicodeString("1/1999"), // 00: yM -> "M/y"
171 UnicodeString("ene 1999"), // 01: yMMM -> "MMM y"
172 UnicodeString("13/1/1999"), // 02: yMd -> "d/M/y"
173 UnicodeString("13 ene 1999"), // 03: yMMMd -> "d MMM y"
174 UnicodeString("13/1"), // 04: Md -> "d/M"
175 UnicodeString("13 ene"), // 05: MMMd -> "d MMM"
176 UnicodeString("13 de enero"), // 06: MMMMd -> "d 'de' MMMM"
177 UnicodeString("T1 1999"), // 07: yQQQ -> "QQQ y"
3d1f044b 178 CharsToUnicodeString("11:58 p.\\u00A0m."), // 08: hhmm -> "hh:mm a"
4388f060
A
179 UnicodeString("23:58"), // 09: HHmm -> "HH:mm"
180 UnicodeString("23:58"), // 10: jjmm -> "HH:mm"
181 UnicodeString("58:59"), // 11: mmss -> "mm:ss"
182 UnicodeString("enero de 1999"), // 12: yyyyMMMM -> "MMMM 'de' yyyy"
b331163b 183 CharsToUnicodeString("mi\\u00E9, 13 ene"), // 13: MMMEd -> "E d MMM"
4388f060
A
184 CharsToUnicodeString("mi\\u00E9 13"), // 14: Ed -> "EEE d"
185 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "H:mm:ss,SSS"
57a6839d 186 UnicodeString("23:58"), // 16: JJmm
4388f060
A
187
188 // ja // 5 ja
729e4ab9
A
189 UnicodeString("1999/1"), // 00: yM -> y/M
190 CharsToUnicodeString("1999\\u5E741\\u6708"), // 01: yMMM -> y\u5E74M\u6708
191 UnicodeString("1999/1/13"), // 02: yMd -> y/M/d
192 CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> y\u5E74M\u6708d\u65E5
193 UnicodeString("1/13"), // 04: Md -> M/d
194 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5
4388f060 195 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
57a6839d 196 CharsToUnicodeString("1999/Q1"), // 07: yQQQ -> y/QQQ
4388f060
A
197 CharsToUnicodeString("\\u5348\\u5F8C11:58"), // 08: hhmm
198 UnicodeString("23:58"), // 09: HHmm -> HH:mm
199 UnicodeString("23:58"), // 10: jjmm
200 UnicodeString("58:59"), // 11: mmss -> mm:ss
201 CharsToUnicodeString("1999\\u5E741\\u6708"), // 12: yyyyMMMM -> y\u5E74M\u6708
2ca993e8
A
202 CharsToUnicodeString("1\\u670813\\u65E5(\\u6C34)"), // 13: MMMEd -> M\u6708d\u65E5(E)
203 CharsToUnicodeString("13\\u65E5(\\u6C34)"), // 14: Ed -> d\u65E5(E)
4388f060 204 UnicodeString("23:58:59.123"), // 15: jmmssSSS -> "H:mm:ss.SSS"
57a6839d 205 UnicodeString("23:58"), // 16: JJmm
4388f060
A
206
207 // ja@calendar=japanese // 6 ja@calendar=japanese
3d1f044b 208 UnicodeString("H11/01"), // 00: yM -> GGGGGy/MM
729e4ab9 209 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u6708"), // 01: yMMM -> Gy\u5E74M\u6708
3d1f044b 210 UnicodeString("H11/01/13"), // 02: yMd -> GGGGGy/MM/dd
729e4ab9 211 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> Gy\u5E74M\u6708d\u65E5
3d1f044b 212 UnicodeString("1/13"), // 04: Md -> M/dd
729e4ab9 213 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5
4388f060 214 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
b331163b 215 CharsToUnicodeString("\\u5E73\\u621011/Q1"), // 07: yQQQ -> Gy/QQQ
4388f060
A
216 CharsToUnicodeString("\\u5348\\u5F8C11:58"), // 08: hhmm ->
217 UnicodeString("23:58"), // 09: HHmm -> HH:mm (as for ja)
218 UnicodeString("23:58"), // 10: jjmm
219 UnicodeString("58:59"), // 11: mmss -> mm:ss (as for ja)
220 CharsToUnicodeString("\\u5E73\\u621011\\u5E741\\u6708"), // 12: yyyyMMMM -> Gyyyy\u5E74M\u6708
2ca993e8
A
221 CharsToUnicodeString("1\\u670813\\u65E5(\\u6C34)"), // 13: MMMEd -> M\u6708d\u65E5(E)
222 CharsToUnicodeString("13\\u65E5(\\u6C34)"), // 14: Ed -> d\u65E5(E)
4388f060 223 UnicodeString("23:58:59.123"), // 15: jmmssSSS -> "H:mm:ss.SSS"
57a6839d 224 UnicodeString("23:58"), // 16: JJmm
4388f060
A
225
226 // zh_Hans_CN // 7 zh_Hans_CN
b331163b 227 CharsToUnicodeString("1999\\u5E741\\u6708"), // 00: yM -> y\u5E74M\u6708
729e4ab9 228 CharsToUnicodeString("1999\\u5E741\\u6708"), // 01: yMMM -> yyyy\u5E74MMM (fixed expected result per ticket:6626:)
51004dcb 229 CharsToUnicodeString("1999/1/13"), // 02: yMd
729e4ab9 230 CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> yyyy\u5E74MMMd\u65E5 (fixed expected result per ticket:6626:)
51004dcb 231 UnicodeString("1/13"), // 04: Md
4388f060
A
232 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd -> M\u6708d\u65E5 (fixed expected result per ticket:6626:)
233 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd -> M\u6708d\u65E5
51004dcb 234 CharsToUnicodeString("1999\\u5E74\\u7B2C1\\u5B63\\u5EA6"), // 07: yQQQ
4388f060
A
235 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm
236 UnicodeString("23:58"), // 09: HHmm
237 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
238 UnicodeString("58:59"), // 11: mmss
239 CharsToUnicodeString("1999\\u5E741\\u6708"), // 12: yyyyMMMM -> yyyy\u5E74MMM
57a6839d
A
240 CharsToUnicodeString("1\\u670813\\u65E5 \\u5468\\u4E09"), // 13: MMMEd -> MMMd\u65E5 EEE
241 CharsToUnicodeString("13 \\u5468\\u4E09"), // 14: Ed -> d\u65E5EEE
4388f060 242 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSSS -> "ah:mm:ss.SSS"
57a6839d 243 UnicodeString("11:58"), // 16: JJmm
4388f060 244
2ca993e8 245 // zh_TW@calendar=roc // 8 zh_TW@calendar=roc
3d1f044b
A
246 CharsToUnicodeString("\\u6C11\\u570B 88/1"), // 00: yM -> G y/M
247 CharsToUnicodeString("\\u6C11\\u570B 88\\u5E741\\u6708"), // 01: yMMM -> G y\u5E74M\u6708
248 CharsToUnicodeString("\\u6C11\\u570B 88/1/13"), // 02: yMd -> G y/M/d
249 CharsToUnicodeString("\\u6C11\\u570B 88\\u5E741\\u670813\\u65E5"), // 03: yMMMd -> G y\u5E74M\u6708d\u65E5
729e4ab9
A
250 UnicodeString("1/13"), // 04: Md -> M/d
251 CharsToUnicodeString("1\\u670813\\u65E5"), // 05: MMMd ->M\u6708d\u65E5
4388f060 252 CharsToUnicodeString("1\\u670813\\u65E5"), // 06: MMMMd ->M\u6708d\u65E5
3d1f044b 253 CharsToUnicodeString("\\u6C11\\u570B 88\\u5E741\\u5B63"), // 07: yQQQ -> G yQQQ
4388f060
A
254 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm ->
255 UnicodeString("23:58"), // 09: HHmm ->
256 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
257 UnicodeString("58:59"), // 11: mmss ->
3d1f044b 258 CharsToUnicodeString("\\u6C11\\u570B 88\\u5E741\\u6708"), // 12: yyyyMMMM -> G y\u5E74M\u670
57a6839d
A
259 CharsToUnicodeString("1\\u670813\\u65E5 \\u9031\\u4E09"), // 13: MMMEd -> M\u6708d\u65E5 E
260 CharsToUnicodeString("13 \\u9031\\u4E09"), // 14: Ed -> d E
4388f060 261 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSSS -> "ah:mm:ss.SSS"
57a6839d 262 UnicodeString("11:58"), // 16: JJmm
4388f060
A
263
264 // ru // 9 ru
265 UnicodeString("01.1999"), // 00: yM -> MM.y
2ca993e8 266 CharsToUnicodeString("\\u044F\\u043D\\u0432. 1999 \\u0433."), // 01: yMMM -> LLL y
51004dcb
A
267 UnicodeString("13.01.1999"), // 02: yMd -> dd.MM.y
268 CharsToUnicodeString("13 \\u044F\\u043D\\u0432. 1999 \\u0433."), // 03: yMMMd -> d MMM y
269 UnicodeString("13.01"), // 04: Md -> dd.MM
729e4ab9 270 CharsToUnicodeString("13 \\u044F\\u043D\\u0432."), // 05: MMMd -> d MMM
4388f060 271 CharsToUnicodeString("13 \\u044F\\u043D\\u0432\\u0430\\u0440\\u044F"), // 06: MMMMd -> d MMMM
51004dcb 272 CharsToUnicodeString("1-\\u0439 \\u043A\\u0432. 1999 \\u0433."), // 07: yQQQ -> y QQQ
3d1f044b 273 CharsToUnicodeString("11:58 PM"), // 08: hhmm -> hh:mm a
4388f060
A
274 UnicodeString("23:58"), // 09: HHmm -> HH:mm
275 UnicodeString("23:58"), // 10: jjmm -> HH:mm
276 UnicodeString("58:59"), // 11: mmss -> mm:ss
2ca993e8
A
277 CharsToUnicodeString("\\u044F\\u043D\\u0432\\u0430\\u0440\\u044C 1999 \\u0433."), // 12: yyyyMMMM -> LLLL y
278 CharsToUnicodeString("\\u0441\\u0440, 13 \\u044F\\u043D\\u0432."), // 13: MMMEd -> ccc, d MMM
279 CharsToUnicodeString("\\u0441\\u0440, 13"), // 14: Ed -> EEE, d
4388f060 280 UnicodeString("23:58:59,123"), // 15: jmmssSSS -> "H:mm:ss,SSS"
57a6839d 281 UnicodeString("23:58"), // 16: JJmm
4388f060 282
57a6839d
A
283 // zh@calendar=chinese,numbers=hanidays // 10 zh@calendar=chinese,numbers=hanidays
284 CharsToUnicodeString("1998\\u620A\\u5BC5\\u5E74\\u51AC\\u6708"), // 00: yMMM
285 CharsToUnicodeString("1998\\u620A\\u5BC5\\u5E74\\u51AC\\u6708"), // 01: yMMM
286 CharsToUnicodeString("1998\\u5E74\\u51AC\\u6708\\u5EFF\\u516D"), // 02: yMMMd
287 CharsToUnicodeString("1998\\u5E74\\u51AC\\u6708\\u5EFF\\u516D"), // 03: yMMMd
288 CharsToUnicodeString("11-\\u5EFF\\u516D"), // 04: Md // hmm
289 CharsToUnicodeString("\\u51AC\\u6708\\u5EFF\\u516D"), // 05: MMMd
290 CharsToUnicodeString("\\u51AC\\u6708\\u5EFF\\u516D"), // 06: MMMMd
291 CharsToUnicodeString("1998\\u620A\\u5BC5\\u5E74\\u7b2c\\u56db\\u5B63\\u5EA6"), // 07: yQQQ
4388f060
A
292 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 08: hhmm
293 UnicodeString("23:58"), // 09: HHmm
294 CharsToUnicodeString("\\u4E0B\\u534811:58"), // 10: jjmm
295 UnicodeString("58:59"), // 11: mmss
57a6839d
A
296 CharsToUnicodeString("1998\\u620A\\u5BC5\\u5E74\\u51AC\\u6708"), // 12: yyyyMMMM
297 CharsToUnicodeString("\\u51AC\\u6708\\u5EFF\\u516D\\u5468\\u4E09"), // 13: MMMEd
298 CharsToUnicodeString("\\u5EFF\\u516D\\u5468\\u4E09"), // 14: Ed -> dE
4388f060 299 CharsToUnicodeString("\\u4E0B\\u534811:58:59.123"), // 15: jmmssSS
57a6839d 300 UnicodeString("11:58"), // 16: JJmm
46f4442e 301
f3c0d7a5
A
302 // ar // 11 ar, for Weds, Jan 13, 1999, 23:58:59
303 CharsToUnicodeString("\\u0661\\u200F/\\u0661\\u0669\\u0669\\u0669"), // 00: yM
304 CharsToUnicodeString("\\u064A\\u0646\\u0627\\u064A\\u0631 \\u0661\\u0669\\u0669\\u0669"), // 01: yMMM
305 CharsToUnicodeString("\\u0661\\u0663\\u200F/\\u0661\\u200F/\\u0661\\u0669\\u0669\\u0669"), // 02: yMd
306 CharsToUnicodeString("\\u0661\\u0663 \\u064A\\u0646\\u0627\\u064A\\u0631\\u060C \\u0661\\u0669\\u0669\\u0669"), // 03: yMMMd
307 CharsToUnicodeString("\\u0661\\u0663/\\u200F\\u0661"), // 04: Md
308 CharsToUnicodeString("\\u0661\\u0663 \\u064A\\u0646\\u0627\\u064A\\u0631"), // 05: MMMd
309 CharsToUnicodeString("\\u0661\\u0663 \\u064A\\u0646\\u0627\\u064A\\u0631"), // 06: MMMMd
310 CharsToUnicodeString("\\u0627\\u0644\\u0631\\u0628\\u0639 \\u0627\\u0644\\u0623\\u0648\\u0644 \\u0661\\u0669\\u0669\\u0669"), // 07: yQQQ
311 CharsToUnicodeString("\\u0661\\u0661:\\u0665\\u0668\\u00A0\\u0645"), // 08: hhmm
312 CharsToUnicodeString("\\u0662\\u0663:\\u0665\\u0668"), // 09: HHmm
313 CharsToUnicodeString("\\u0661\\u0661:\\u0665\\u0668\\u00A0\\u0645"), // 10: jjmm
314 CharsToUnicodeString("\\u0665\\u0668:\\u0665\\u0669"), // 11: mmss
315 CharsToUnicodeString("\\u064A\\u0646\\u0627\\u064A\\u0631 \\u0661\\u0669\\u0669\\u0669"), // 12: yyyyMMMM
316 CharsToUnicodeString("\\u0623\\u0631\\u0628\\u0639\\u0627\\u0621\\u060C \\u0661\\u0663 \\u064A\\u0646\\u0627\\u064A\\u0631"), // 13: MMMEd
317 CharsToUnicodeString("\\u0623\\u0631\\u0628\\u0639\\u0627\\u0621\\u060C \\u0661\\u0663"), // 14: Ed
318 CharsToUnicodeString("\\u0661\\u0661:\\u0665\\u0668:\\u0665\\u0669\\u066B\\u0661\\u0662\\u0663\\u00A0\\u0645"), // 15: jmmssSSS
319 CharsToUnicodeString("\\u0661\\u0661:\\u0665\\u0668"), // 16: JJmm
320
46f4442e
A
321 UnicodeString(),
322 };
323
324 UnicodeString patternTests2[] = {
325 UnicodeString("yyyyMMMdd"),
729e4ab9 326 UnicodeString("yyyyqqqq"),
46f4442e
A
327 UnicodeString("yMMMdd"),
328 UnicodeString("EyyyyMMMdd"),
329 UnicodeString("yyyyMMdd"),
330 UnicodeString("yyyyMMM"),
331 UnicodeString("yyyyMM"),
332 UnicodeString("yyMM"),
333 UnicodeString("yMMMMMd"),
334 UnicodeString("EEEEEMMMMMd"),
335 UnicodeString("MMMd"),
336 UnicodeString("MMMdhmm"),
337 UnicodeString("EMMMdhmms"),
338 UnicodeString("MMdhmm"),
339 UnicodeString("EEEEMMMdhmms"),
340 UnicodeString("yyyyMMMddhhmmss"),
341 UnicodeString("EyyyyMMMddhhmmss"),
342 UnicodeString("hmm"),
343 UnicodeString("hhmm"),
729e4ab9 344 UnicodeString("hhmmVVVV"),
46f4442e
A
345 UnicodeString(""),
346 };
347 UnicodeString patternResults2[] = {
348 UnicodeString("Oct 14, 1999"),
729e4ab9 349 UnicodeString("4th quarter 1999"),
46f4442e
A
350 UnicodeString("Oct 14, 1999"),
351 UnicodeString("Thu, Oct 14, 1999"),
352 UnicodeString("10/14/1999"),
353 UnicodeString("Oct 1999"),
354 UnicodeString("10/1999"),
355 UnicodeString("10/99"),
356 UnicodeString("O 14, 1999"),
357 UnicodeString("T, O 14"),
358 UnicodeString("Oct 14"),
51004dcb
A
359 UnicodeString("Oct 14, 6:58 AM"),
360 UnicodeString("Thu, Oct 14, 6:58:59 AM"),
361 UnicodeString("10/14, 6:58 AM"),
362 UnicodeString("Thursday, Oct 14, 6:58:59 AM"),
363 UnicodeString("Oct 14, 1999, 6:58:59 AM"),
364 UnicodeString("Thu, Oct 14, 1999, 6:58:59 AM"),
46f4442e 365 UnicodeString("6:58 AM"),
729e4ab9 366 UnicodeString("6:58 AM"),
4388f060 367 UnicodeString("6:58 AM GMT"),
46f4442e
A
368 UnicodeString(""),
369 };
2ca993e8 370
46f4442e 371 // results for getSkeletons() and getPatternForSkeleton()
2ca993e8
A
372 const UnicodeString testSkeletonsResults[] = {
373 UnicodeString("HH:mm"),
374 UnicodeString("MMMMd"),
375 UnicodeString("MMMMMdd"),
46f4442e 376 };
2ca993e8
A
377
378 const UnicodeString testBaseSkeletonsResults[] = {
379 UnicodeString("Hm"),
380 UnicodeString("MMMMd"),
4388f060 381 UnicodeString("MMMMMd"),
46f4442e
A
382 };
383
0f5d89e8
A
384 const char* testGetSkeletonAndBase[][3] = {
385 // pattern skeleton baseSkeleton
386 { "dd-MMM", "MMMdd", "MMMd" },
387 { "dd/MMMM/yy", "yyMMMMdd", "yMMMMd" },
388 { "h", "h", "h" },
389 { "ah", "ah", "ah" },
390 { "aaaah", "aaaah", "aaaah" },
391 { "Bh", "Bh", "Bh" }
392 };
393
46f4442e
A
394 UnicodeString newDecimal(" "); // space
395 UnicodeString newAppendItemName("hrs.");
396 UnicodeString newAppendItemFormat("{1} {0}");
397 UnicodeString newDateTimeFormat("{1} {0}");
398 UErrorCode status = U_ZERO_ERROR;
399 UnicodeString conflictingPattern;
51004dcb
A
400 UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
401 (void)conflictingStatus; // Suppress set but not used warning.
46f4442e
A
402
403 // ======= Test CreateInstance with default locale
404 logln("Testing DateTimePatternGenerator createInstance from default locale");
2ca993e8 405
46f4442e
A
406 DateTimePatternGenerator *instFromDefaultLocale=DateTimePatternGenerator::createInstance(status);
407 if (U_FAILURE(status)) {
408 dataerrln("ERROR: Could not create DateTimePatternGenerator (default) - exitting");
409 return;
410 }
411 else {
412 delete instFromDefaultLocale;
413 }
414
2ca993e8 415 // ======= Test CreateInstance with given locale
46f4442e
A
416 logln("Testing DateTimePatternGenerator createInstance from French locale");
417 status = U_ZERO_ERROR;
418 DateTimePatternGenerator *instFromLocale=DateTimePatternGenerator::createInstance(Locale::getFrench(), status);
419 if (U_FAILURE(status)) {
420 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting");
421 return;
422 }
423
2ca993e8 424 // ======= Test clone DateTimePatternGenerator
46f4442e
A
425 logln("Testing DateTimePatternGenerator::clone()");
426 status = U_ZERO_ERROR;
2ca993e8 427
46f4442e
A
428
429 UnicodeString decimalSymbol = instFromLocale->getDecimal();
430 UnicodeString newDecimalSymbol = UnicodeString("*");
431 decimalSymbol = instFromLocale->getDecimal();
432 instFromLocale->setDecimal(newDecimalSymbol);
433 DateTimePatternGenerator *cloneDTPatternGen=instFromLocale->clone();
434 decimalSymbol = cloneDTPatternGen->getDecimal();
435 if (decimalSymbol != newDecimalSymbol) {
436 errln("ERROR: inconsistency is found in cloned object.");
437 }
438 if ( !(*cloneDTPatternGen == *instFromLocale) ) {
439 errln("ERROR: inconsistency is found in cloned object.");
440 }
2ca993e8 441
46f4442e
A
442 if ( *cloneDTPatternGen != *instFromLocale ) {
443 errln("ERROR: inconsistency is found in cloned object.");
444 }
2ca993e8 445
46f4442e
A
446 delete instFromLocale;
447 delete cloneDTPatternGen;
2ca993e8
A
448
449 // ======= Test simple use cases
46f4442e
A
450 logln("Testing simple use cases");
451 status = U_ZERO_ERROR;
452 Locale deLocale=Locale::getGermany();
453 UDate sampleDate=LocaleTest::date(99, 9, 13, 23, 58, 59);
454 DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(deLocale, status);
455 if (U_FAILURE(status)) {
456 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting");
457 return;
458 }
459 UnicodeString findPattern = gen->getBestPattern(UnicodeString("MMMddHmm"), status);
460 SimpleDateFormat *format = new SimpleDateFormat(findPattern, deLocale, status);
461 if (U_FAILURE(status)) {
462 dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())");
463 delete gen;
464 return;
465 }
466 TimeZone *zone = TimeZone::createTimeZone(UnicodeString("ECT"));
467 if (zone==NULL) {
468 dataerrln("ERROR: Could not create TimeZone ECT");
469 delete gen;
470 delete format;
471 return;
472 }
473 format->setTimeZone(*zone);
474 UnicodeString dateReturned, expectedResult;
475 dateReturned.remove();
476 dateReturned = format->format(sampleDate, dateReturned, status);
b331163b 477 expectedResult=UnicodeString("14. Okt., 08:58", -1, US_INV);
46f4442e
A
478 if ( dateReturned != expectedResult ) {
479 errln("ERROR: Simple test in getBestPattern with Locale::getGermany()).");
480 }
481 // add new pattern
482 status = U_ZERO_ERROR;
2ca993e8 483 conflictingStatus = gen->addPattern(UnicodeString("d'. von' MMMM", -1, US_INV), true, conflictingPattern, status);
46f4442e
A
484 if (U_FAILURE(status)) {
485 errln("ERROR: Could not addPattern - d\'. von\' MMMM");
486 }
487 status = U_ZERO_ERROR;
488 UnicodeString testPattern=gen->getBestPattern(UnicodeString("MMMMdd"), status);
489 testPattern=gen->getBestPattern(UnicodeString("MMMddHmm"), status);
729e4ab9 490 format->applyPattern(gen->getBestPattern(UnicodeString("MMMMdHmm"), status));
46f4442e
A
491 dateReturned.remove();
492 dateReturned = format->format(sampleDate, dateReturned, status);
b331163b 493 expectedResult=UnicodeString("14. von Oktober, 08:58", -1, US_INV);
46f4442e 494 if ( dateReturned != expectedResult ) {
729e4ab9 495 errln(UnicodeString("ERROR: Simple test addPattern failed!: d\'. von\' MMMM Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult);
46f4442e
A
496 }
497 delete format;
2ca993e8 498
46f4442e 499 // get a pattern and modify it
2ca993e8 500 format = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull,
46f4442e
A
501 deLocale);
502 format->setTimeZone(*zone);
503 UnicodeString pattern;
504 pattern = format->toPattern(pattern);
505 dateReturned.remove();
506 dateReturned = format->format(sampleDate, dateReturned, status);
b331163b 507 expectedResult=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\\u00E4ische Sommerzeit");
46f4442e
A
508 if ( dateReturned != expectedResult ) {
509 errln("ERROR: Simple test uses full date format.");
510 errln(UnicodeString(" Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult);
511 }
2ca993e8
A
512
513 // modify it to change the zone.
46f4442e
A
514 UnicodeString newPattern = gen->replaceFieldTypes(pattern, UnicodeString("vvvv"), status);
515 format->applyPattern(newPattern);
516 dateReturned.remove();
517 dateReturned = format->format(sampleDate, dateReturned, status);
b331163b 518 expectedResult=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\\u00E4ische Zeit");
46f4442e
A
519 if ( dateReturned != expectedResult ) {
520 errln("ERROR: Simple test modify the timezone!");
521 errln(UnicodeString(" Got: ")+ dateReturned + UnicodeString(" Expected: ") + expectedResult);
522 }
2ca993e8 523
46f4442e
A
524 // setDeciaml(), getDeciaml()
525 gen->setDecimal(newDecimal);
526 if (newDecimal != gen->getDecimal()) {
527 errln("ERROR: unexpected result from setDecimal() and getDecimal()!.\n");
528 }
2ca993e8 529
46f4442e
A
530 // setAppenItemName() , getAppendItemName()
531 gen->setAppendItemName(UDATPG_HOUR_FIELD, newAppendItemName);
532 if (newAppendItemName != gen->getAppendItemName(UDATPG_HOUR_FIELD)) {
533 errln("ERROR: unexpected result from setAppendItemName() and getAppendItemName()!.\n");
534 }
2ca993e8 535
46f4442e
A
536 // setAppenItemFormat() , getAppendItemFormat()
537 gen->setAppendItemFormat(UDATPG_HOUR_FIELD, newAppendItemFormat);
538 if (newAppendItemFormat != gen->getAppendItemFormat(UDATPG_HOUR_FIELD)) {
539 errln("ERROR: unexpected result from setAppendItemFormat() and getAppendItemFormat()!.\n");
540 }
2ca993e8 541
46f4442e
A
542 // setDateTimeFormat() , getDateTimeFormat()
543 gen->setDateTimeFormat(newDateTimeFormat);
544 if (newDateTimeFormat != gen->getDateTimeFormat()) {
545 errln("ERROR: unexpected result from setDateTimeFormat() and getDateTimeFormat()!.\n");
546 }
2ca993e8 547
46f4442e 548 // ======== Test getSkeleton and getBaseSkeleton
0f5d89e8
A
549
550 int32_t i, count = UPRV_LENGTHOF(testGetSkeletonAndBase);
551 for (i = 0; i < count; i++) {
552 status = U_ZERO_ERROR;
553 pattern = UnicodeString(testGetSkeletonAndBase[i][0]);
554 UnicodeString expectedSkeleton = UnicodeString(testGetSkeletonAndBase[i][1]);
555 UnicodeString expectedBaseSkeleton = UnicodeString(testGetSkeletonAndBase[i][2]);
556 UnicodeString retSkeleton = gen->getSkeleton(pattern, status);
557 if(U_FAILURE(status) || retSkeleton != expectedSkeleton ) {
558 errln("ERROR: Unexpected result from getSkeleton().\n");
559 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected: ") + expectedSkeleton );
560 }
561 retSkeleton = gen->getBaseSkeleton(pattern, status);
562 if(U_FAILURE(status) || retSkeleton != expectedBaseSkeleton) {
563 errln("ERROR: Unexpected result from getBaseSkeleton().\n");
564 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected:")+ expectedBaseSkeleton);
565 }
46f4442e
A
566 }
567
46f4442e
A
568 delete format;
569 delete zone;
570 delete gen;
2ca993e8 571
46f4442e
A
572 {
573 // Trac# 6104
574 status = U_ZERO_ERROR;
575 pattern = UnicodeString("YYYYMMM");
576 UnicodeString expR = CharsToUnicodeString("1999\\u5E741\\u6708"); // fixed expected result per ticket:6626:
577 Locale loc("ja");
578 UDate testDate1= LocaleTest::date(99, 0, 13, 23, 58, 59);
579 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status);
580 if(U_FAILURE(status)) {
581 dataerrln("ERROR: Could not create DateTimePatternGenerator");
582 return;
583 }
584 UnicodeString bPattern = patGen->getBestPattern(pattern, status);
585 UnicodeString rDate;
586 SimpleDateFormat sdf(bPattern, loc, status);
587 rDate.remove();
588 rDate = sdf.format(testDate1, rDate);
589
590 logln(UnicodeString(" ja locale with skeleton: YYYYMMM Best Pattern:") + bPattern);
591 logln(UnicodeString(" Formatted date:") + rDate);
592
593 if ( expR!= rDate ) {
2ca993e8 594 errln(UnicodeString("\nERROR: Test Japanese month hack Got: ") + rDate +
46f4442e
A
595 UnicodeString(" Expected: ") + expR );
596 }
2ca993e8 597
46f4442e
A
598 delete patGen;
599 }
600 { // Trac# 6104
601 Locale loc("zh");
602 UnicodeString expR = CharsToUnicodeString("1999\\u5E741\\u6708"); // fixed expected result per ticket:6626:
603 UDate testDate1= LocaleTest::date(99, 0, 13, 23, 58, 59);
604 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status);
605 if(U_FAILURE(status)) {
606 dataerrln("ERROR: Could not create DateTimePatternGenerator");
607 return;
608 }
609 UnicodeString bPattern = patGen->getBestPattern(pattern, status);
610 UnicodeString rDate;
611 SimpleDateFormat sdf(bPattern, loc, status);
612 rDate.remove();
613 rDate = sdf.format(testDate1, rDate);
614
615 logln(UnicodeString(" zh locale with skeleton: YYYYMMM Best Pattern:") + bPattern);
616 logln(UnicodeString(" Formatted date:") + rDate);
617 if ( expR!= rDate ) {
2ca993e8 618 errln(UnicodeString("\nERROR: Test Chinese month hack Got: ") + rDate +
46f4442e
A
619 UnicodeString(" Expected: ") + expR );
620 }
2ca993e8 621 delete patGen;
46f4442e
A
622 }
623
624 {
625 // Trac# 6172 duplicate time pattern
626 status = U_ZERO_ERROR;
627 pattern = UnicodeString("hmv");
628 UnicodeString expR = UnicodeString("h:mm a v"); // avail formats has hm -> "h:mm a" (fixed expected result per ticket:6626:)
629 Locale loc("en");
630 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status);
631 if(U_FAILURE(status)) {
632 dataerrln("ERROR: Could not create DateTimePatternGenerator");
633 return;
634 }
635 UnicodeString bPattern = patGen->getBestPattern(pattern, status);
636 logln(UnicodeString(" en locale with skeleton: hmv Best Pattern:") + bPattern);
637
638 if ( expR!= bPattern ) {
2ca993e8 639 errln(UnicodeString("\nERROR: Test EN time format Got: ") + bPattern +
46f4442e
A
640 UnicodeString(" Expected: ") + expR );
641 }
2ca993e8 642
46f4442e
A
643 delete patGen;
644 }
2ca993e8
A
645
646
46f4442e
A
647 // ======= Test various skeletons.
648 logln("Testing DateTimePatternGenerator with various skeleton");
2ca993e8 649
46f4442e
A
650 status = U_ZERO_ERROR;
651 int32_t localeIndex=0;
652 int32_t resultIndex=0;
653 UnicodeString resultDate;
729e4ab9 654 UDate testDate= LocaleTest::date(99, 0, 13, 23, 58, 59) + 123.0;
46f4442e 655 while (localeIndex < MAX_LOCALE )
2ca993e8 656 {
46f4442e
A
657 int32_t dataIndex=0;
658 UnicodeString bestPattern;
2ca993e8 659
46f4442e
A
660 Locale loc(testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2], testLocale[localeIndex][3]);
661 logln("\n\n Locale: %s_%s_%s@%s", testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2], testLocale[localeIndex][3]);
662 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status);
663 if(U_FAILURE(status)) {
664 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale index:%d . - exitting\n", localeIndex);
665 return;
666 }
667 while (patternData[dataIndex].length() > 0) {
668 log(patternData[dataIndex]);
669 bestPattern = patGen->getBestPattern(patternData[dataIndex++], status);
670 logln(UnicodeString(" -> ") + bestPattern);
2ca993e8 671
46f4442e
A
672 SimpleDateFormat sdf(bestPattern, loc, status);
673 resultDate.remove();
674 resultDate = sdf.format(testDate, resultDate);
675 if ( resultDate != patternResults[resultIndex] ) {
676 errln(UnicodeString("\nERROR: Test various skeletons[") + (dataIndex-1) + UnicodeString("], localeIndex ") + localeIndex +
729e4ab9 677 UnicodeString(". Got: \"") + resultDate + UnicodeString("\" Expected: \"") + patternResults[resultIndex] + "\"" );
46f4442e 678 }
2ca993e8 679
46f4442e
A
680 resultIndex++;
681 }
682 delete patGen;
683 localeIndex++;
684 }
2ca993e8 685
46f4442e
A
686 // ======= More tests ticket#6110
687 logln("Testing DateTimePatternGenerator with various skeleton");
2ca993e8 688
46f4442e
A
689 status = U_ZERO_ERROR;
690 localeIndex=0;
691 resultIndex=0;
692 testDate= LocaleTest::date(99, 9, 13, 23, 58, 59);
2ca993e8 693 {
46f4442e
A
694 int32_t dataIndex=0;
695 UnicodeString bestPattern;
696 logln("\n\n Test various skeletons for English locale...");
697 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(Locale::getEnglish(), status);
698 if(U_FAILURE(status)) {
699 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale English . - exitting\n");
700 return;
701 }
702 TimeZone *enZone = TimeZone::createTimeZone(UnicodeString("ECT/GMT"));
703 if (enZone==NULL) {
704 dataerrln("ERROR: Could not create TimeZone ECT");
705 delete patGen;
706 return;
707 }
2ca993e8 708 SimpleDateFormat *enFormat = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull,
46f4442e
A
709 DateFormat::kFull, Locale::getEnglish());
710 enFormat->setTimeZone(*enZone);
711 while (patternTests2[dataIndex].length() > 0) {
712 logln(patternTests2[dataIndex]);
713 bestPattern = patGen->getBestPattern(patternTests2[dataIndex], status);
714 logln(UnicodeString(" -> ") + bestPattern);
715 enFormat->applyPattern(bestPattern);
716 resultDate.remove();
717 resultDate = enFormat->format(testDate, resultDate);
718 if ( resultDate != patternResults2[resultIndex] ) {
719 errln(UnicodeString("\nERROR: Test various skeletons[") + dataIndex
2ca993e8 720 + UnicodeString("]. Got: ") + resultDate + UnicodeString(" Expected: ") +
46f4442e
A
721 patternResults2[resultIndex] );
722 }
723 dataIndex++;
724 resultIndex++;
725 }
726 delete patGen;
727 delete enZone;
728 delete enFormat;
729 }
730
731
732
2ca993e8 733 // ======= Test random skeleton
46f4442e
A
734 DateTimePatternGenerator *randDTGen= DateTimePatternGenerator::createInstance(status);
735 if (U_FAILURE(status)) {
736 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting");
737 return;
738 }
739 UChar newChar;
46f4442e
A
740 for (i=0; i<10; ++i) {
741 UnicodeString randomSkeleton;
742 int32_t len = rand() % 20;
743 for (int32_t j=0; j<len; ++j ) {
744 while ((newChar = (UChar)(rand()%0x7f))>=(UChar)0x20) {
745 randomSkeleton += newChar;
746 }
747 }
748 UnicodeString bestPattern = randDTGen->getBestPattern(randomSkeleton, status);
749 }
750 delete randDTGen;
2ca993e8 751
46f4442e
A
752 // UnicodeString randomString=Unicode
753 // ======= Test getStaticClassID()
754
755 logln("Testing getStaticClassID()");
756 status = U_ZERO_ERROR;
757 DateTimePatternGenerator *test= DateTimePatternGenerator::createInstance(status);
2ca993e8 758
46f4442e
A
759 if(test->getDynamicClassID() != DateTimePatternGenerator::getStaticClassID()) {
760 errln("ERROR: getDynamicClassID() didn't return the expected value");
761 }
762 delete test;
2ca993e8 763
46f4442e 764 // ====== Test createEmptyInstance()
2ca993e8 765
46f4442e
A
766 logln("Testing createEmptyInstance()");
767 status = U_ZERO_ERROR;
2ca993e8 768
46f4442e
A
769 test = DateTimePatternGenerator::createEmptyInstance(status);
770 if(U_FAILURE(status)) {
771 errln("ERROR: Fail to create an empty instance ! - exitting.\n");
772 delete test;
773 return;
774 }
2ca993e8
A
775
776 conflictingStatus = test->addPattern(UnicodeString("MMMMd"), true, conflictingPattern, status);
46f4442e
A
777 status = U_ZERO_ERROR;
778 testPattern=test->getBestPattern(UnicodeString("MMMMdd"), status);
2ca993e8 779 conflictingStatus = test->addPattern(UnicodeString("HH:mm"), true, conflictingPattern, status);
4388f060 780 conflictingStatus = test->addPattern(UnicodeString("MMMMMdd"), true, conflictingPattern, status); //duplicate pattern
46f4442e
A
781 StringEnumeration *output=NULL;
782 output = test->getRedundants(status);
783 expectedResult=UnicodeString("MMMMd");
784 if (output != NULL) {
785 output->reset(status);
786 const UnicodeString *dupPattern=output->snext(status);
787 if ( (dupPattern==NULL) || (*dupPattern != expectedResult) ) {
788 errln("ERROR: Fail in getRedundants !\n");
789 }
790 }
2ca993e8 791
46f4442e
A
792 // ======== Test getSkeletons and getBaseSkeletons
793 StringEnumeration* ptrSkeletonEnum = test->getSkeletons(status);
794 if(U_FAILURE(status)) {
795 errln("ERROR: Fail to get skeletons !\n");
796 }
797 UnicodeString returnPattern, *ptrSkeleton;
798 ptrSkeletonEnum->reset(status);
0f5d89e8 799 count=ptrSkeletonEnum->count(status);
46f4442e
A
800 for (i=0; i<count; ++i) {
801 ptrSkeleton = (UnicodeString *)ptrSkeletonEnum->snext(status);
802 returnPattern = test->getPatternForSkeleton(*ptrSkeleton);
803 if ( returnPattern != testSkeletonsResults[i] ) {
804 errln(UnicodeString("ERROR: Unexpected result from getSkeletons and getPatternForSkeleton\nGot: ") + returnPattern
805 + UnicodeString("\nExpected: ") + testSkeletonsResults[i]
806 + UnicodeString("\n"));
807 }
808 }
809 StringEnumeration* ptrBaseSkeletonEnum = test->getBaseSkeletons(status);
810 if(U_FAILURE(status)) {
811 errln("ERROR: Fail to get base skeletons !\n");
2ca993e8 812 }
46f4442e
A
813 count=ptrBaseSkeletonEnum->count(status);
814 for (i=0; i<count; ++i) {
815 ptrSkeleton = (UnicodeString *)ptrBaseSkeletonEnum->snext(status);
816 if ( *ptrSkeleton != testBaseSkeletonsResults[i] ) {
817 errln("ERROR: Unexpected result from getBaseSkeletons() !\n");
818 }
819 }
820
821 // ========= DateTimePatternGenerator sample code in Userguide
822 // set up the generator
823 Locale locale = Locale::getFrench();
824 status = U_ZERO_ERROR;
825 DateTimePatternGenerator *generator = DateTimePatternGenerator::createInstance( locale, status);
2ca993e8 826
46f4442e 827 // get a pattern for an abbreviated month and day
2ca993e8
A
828 pattern = generator->getBestPattern(UnicodeString("MMMd"), status);
829 SimpleDateFormat formatter(pattern, locale, status);
46f4442e
A
830
831 zone = TimeZone::createTimeZone(UnicodeString("GMT"));
832 formatter.setTimeZone(*zone);
833 // use it to format (or parse)
834 UnicodeString formatted;
2ca993e8 835 formatted = formatter.format(Calendar::getNow(), formatted, status);
46f4442e
A
836 // for French, the result is "13 sept."
837 formatted.remove();
838 // cannot use the result from getNow() because the value change evreyday.
839 testDate= LocaleTest::date(99, 0, 13, 23, 58, 59);
840 formatted = formatter.format(testDate, formatted, status);
841 expectedResult=UnicodeString("14 janv.");
842 if ( formatted != expectedResult ) {
843 errln("ERROR: Userguide sample code result!");
844 errln(UnicodeString(" Got: ")+ formatted + UnicodeString(" Expected: ") + expectedResult);
845 }
846
847 delete zone;
848 delete output;
849 delete ptrSkeletonEnum;
850 delete ptrBaseSkeletonEnum;
851 delete test;
852 delete generator;
853}
854
729e4ab9
A
855/**
856 * Test handling of options
857 *
858 * For reference, as of ICU 4.3.3,
859 * root/gregorian has
860 * Hm{"H:mm"}
861 * Hms{"H:mm:ss"}
862 * hm{"h:mm a"}
863 * hms{"h:mm:ss a"}
864 * en/gregorian has
865 * Hm{"H:mm"}
866 * Hms{"H:mm:ss"}
867 * hm{"h:mm a"}
868 * be/gregorian has
869 * HHmmss{"HH.mm.ss"}
870 * Hm{"HH.mm"}
871 * hm{"h.mm a"}
872 * hms{"h.mm.ss a"}
873 */
874typedef struct DTPtnGenOptionsData {
875 const char *locale;
876 const char *skel;
877 const char *expectedPattern;
878 UDateTimePatternMatchOptions options;
879} DTPtnGenOptionsData;
880void IntlTestDateTimePatternGeneratorAPI::testOptions(/*char *par*/)
881{
882 DTPtnGenOptionsData testData[] = {
883 // locale skel expectedPattern options
884 { "en", "Hmm", "HH:mm", UDATPG_MATCH_NO_OPTIONS },
885 { "en", "HHmm", "HH:mm", UDATPG_MATCH_NO_OPTIONS },
886 { "en", "hhmm", "h:mm a", UDATPG_MATCH_NO_OPTIONS },
887 { "en", "Hmm", "HH:mm", UDATPG_MATCH_HOUR_FIELD_LENGTH },
888 { "en", "HHmm", "HH:mm", UDATPG_MATCH_HOUR_FIELD_LENGTH },
889 { "en", "hhmm", "hh:mm a", UDATPG_MATCH_HOUR_FIELD_LENGTH },
2ca993e8
A
890 { "da", "Hmm", "HH.mm", UDATPG_MATCH_NO_OPTIONS },
891 { "da", "HHmm", "HH.mm", UDATPG_MATCH_NO_OPTIONS },
892 { "da", "hhmm", "h.mm a", UDATPG_MATCH_NO_OPTIONS },
893 { "da", "Hmm", "H.mm", UDATPG_MATCH_HOUR_FIELD_LENGTH },
894 { "da", "HHmm", "HH.mm", UDATPG_MATCH_HOUR_FIELD_LENGTH },
895 { "da", "hhmm", "hh.mm a", UDATPG_MATCH_HOUR_FIELD_LENGTH },
51004dcb 896 //
57a6839d
A
897 { "en", "yyyy", "yyyy", UDATPG_MATCH_NO_OPTIONS },
898 { "en", "YYYY", "YYYY", UDATPG_MATCH_NO_OPTIONS },
899 { "en", "U", "y", UDATPG_MATCH_NO_OPTIONS },
900 { "en@calendar=japanese", "yyyy", "y G", UDATPG_MATCH_NO_OPTIONS },
901 { "en@calendar=japanese", "YYYY", "Y G", UDATPG_MATCH_NO_OPTIONS },
902 { "en@calendar=japanese", "U", "y G", UDATPG_MATCH_NO_OPTIONS },
903 { "en@calendar=chinese", "yyyy", "r(U)", UDATPG_MATCH_NO_OPTIONS },
b331163b
A
904 { "en@calendar=chinese", "YYYY", "Y(Y)", UDATPG_MATCH_NO_OPTIONS }, // not a good result, want r(Y) or r(U)
905 { "en@calendar=chinese", "U", "r(U)", UDATPG_MATCH_NO_OPTIONS },
906 { "en@calendar=chinese", "Gy", "r(U)", UDATPG_MATCH_NO_OPTIONS },
907 { "en@calendar=chinese", "GU", "r(U)", UDATPG_MATCH_NO_OPTIONS },
908 { "en@calendar=chinese", "ULLL", "MMM U", UDATPG_MATCH_NO_OPTIONS },
57a6839d
A
909 { "en@calendar=chinese", "yMMM", "MMM r(U)", UDATPG_MATCH_NO_OPTIONS },
910 { "en@calendar=chinese", "GUMMM", "MMM r(U)", UDATPG_MATCH_NO_OPTIONS },
b331163b
A
911 { "zh@calendar=chinese", "yyyy", "U\\u5E74", UDATPG_MATCH_NO_OPTIONS },
912 { "zh@calendar=chinese", "YYYY", "Y\\u5E74", UDATPG_MATCH_NO_OPTIONS }, // not a great result, may want r(Y) or r(U)
913 { "zh@calendar=chinese", "U", "U\\u5E74", UDATPG_MATCH_NO_OPTIONS },
914 { "zh@calendar=chinese", "Gy", "rU\\u5E74", UDATPG_MATCH_NO_OPTIONS },
915 { "zh@calendar=chinese", "GU", "rU\\u5E74", UDATPG_MATCH_NO_OPTIONS },
916 { "zh@calendar=chinese", "ULLL", "U\\u5E74MMM", UDATPG_MATCH_NO_OPTIONS },
57a6839d
A
917 { "zh@calendar=chinese", "yMMM", "rU\\u5E74MMM", UDATPG_MATCH_NO_OPTIONS },
918 { "zh@calendar=chinese", "GUMMM", "rU\\u5E74MMM", UDATPG_MATCH_NO_OPTIONS },
729e4ab9 919 };
2ca993e8
A
920
921 int count = UPRV_LENGTHOF(testData);
729e4ab9 922 const DTPtnGenOptionsData * testDataPtr = testData;
2ca993e8 923
729e4ab9
A
924 for (; count-- > 0; ++testDataPtr) {
925 UErrorCode status = U_ZERO_ERROR;
926
927 Locale locale(testDataPtr->locale);
928 UnicodeString skel(testDataPtr->skel);
57a6839d 929 UnicodeString expectedPattern(UnicodeString(testDataPtr->expectedPattern).unescape());
729e4ab9
A
930 UDateTimePatternMatchOptions options = testDataPtr->options;
931
932 DateTimePatternGenerator * dtpgen = DateTimePatternGenerator::createInstance(locale, status);
933 if (U_FAILURE(status)) {
934 dataerrln("Unable to create DateTimePatternGenerator instance for locale(%s): %s", locale.getName(), u_errorName(status));
935 delete dtpgen;
936 continue;
937 }
938 UnicodeString pattern = dtpgen->getBestPattern(skel, options, status);
939 if (pattern.compare(expectedPattern) != 0) {
940 errln( UnicodeString("ERROR in getBestPattern, locale ") + UnicodeString(testDataPtr->locale) +
941 UnicodeString(", skeleton ") + skel +
942 ((options)?UnicodeString(", options!=0"):UnicodeString(", options==0")) +
943 UnicodeString(", expected pattern ") + expectedPattern +
944 UnicodeString(", got ") + pattern );
945 }
946 delete dtpgen;
947 }
948}
949
57a6839d
A
950/**
951 * Test that DTPG can handle all valid pattern character / length combinations
952 *
953 */
954#define FIELD_LENGTHS_COUNT 6
955#define FIELD_LENGTH_MAX 8
956#define MUST_INCLUDE_COUNT 5
957
958typedef struct AllFieldsTestItem {
959 char patternChar;
960 int8_t fieldLengths[FIELD_LENGTHS_COUNT+1]; // up to FIELD_LENGTHS_COUNT lengths to try
961 // (length <=FIELD_LENGTH_MAX) plus 0 terminator
962 char mustIncludeOneOf[MUST_INCLUDE_COUNT+1];// resulting pattern must include at least one of
963 // these as a pattern char (0-terminated list)
964} AllFieldsTestItem;
965
966void IntlTestDateTimePatternGeneratorAPI::testAllFieldPatterns(/*char *par*/)
967{
968 const char * localeNames[] = {
969 "root",
970 "root@calendar=japanese",
971 "root@calendar=chinese",
972 "en",
973 "en@calendar=japanese",
974 "en@calendar=chinese",
975 NULL // terminator
976 };
977 AllFieldsTestItem testData[] = {
978 //pat fieldLengths generated pattern must
979 //chr to test include one of these
980 { 'G', {1,2,3,4,5,0}, "G" }, // era
981 // year
982 { 'y', {1,2,3,4,0}, "yU" }, // year
983 { 'Y', {1,2,3,4,0}, "Y" }, // year for week of year
984 { 'u', {1,2,3,4,5,0}, "yuU" }, // extended year
985 { 'U', {1,2,3,4,5,0}, "yU" }, // cyclic year name
986 // quarter
987 { 'Q', {1,2,3,4,0}, "Qq" }, // x
988 { 'q', {1,2,3,4,0}, "Qq" }, // standalone
989 // month
990 { 'M', {1,2,3,4,5,0}, "ML" }, // x
991 { 'L', {1,2,3,4,5,0}, "ML" }, // standalone
992 // week
993 { 'w', {1,2,0}, "w" }, // week of year
994 { 'W', {1,0}, "W" }, // week of month
995 // day
996 { 'd', {1,2,0}, "d" }, // day of month
997 { 'D', {1,2,3,0}, "D" }, // day of year
998 { 'F', {1,0}, "F" }, // day of week in month
999 { 'g', {7,0}, "g" }, // modified julian day
1000 // weekday
1001 { 'E', {1,2,3,4,5,6}, "Eec" }, // day of week
1002 { 'e', {1,2,3,4,5,6}, "Eec" }, // local day of week
1003 { 'c', {1,2,3,4,5,6}, "Eec" }, // standalone local day of week
1004 // day period
0f5d89e8
A
1005 { 'a', {1,2,3,4,5,0}, "a" }, // am or pm
1006 { 'b', {1,2,3,4,5,0}, "b" }, // dayPeriod AM/PM/noon
1007 { 'B', {1,2,3,4,5,0}, "B" }, // dayPeriod ranges
57a6839d
A
1008 // hour
1009 { 'h', {1,2,0}, "hK" }, // 12 (1-12)
1010 { 'H', {1,2,0}, "Hk" }, // 24 (0-23)
1011 { 'K', {1,2,0}, "hK" }, // 12 (0-11)
1012 { 'k', {1,2,0}, "Hk" }, // 24 (1-24)
1013 { 'j', {1,2,0}, "hHKk" }, // locale default
0f5d89e8
A
1014 { 'J', {1,2,0}, "hHKk" }, // locale default, without any dayPeriod
1015 { 'C', {1,2,0}, "hHKk" }, // locale allowed first entry, possibly with b or B
57a6839d
A
1016 // minute
1017 { 'm', {1,2,0}, "m" }, // x
1018 // second & fractions
1019 { 's', {1,2,0}, "s" }, // x
1020 { 'S', {1,2,3,4,0}, "S" }, // fractional second
1021 { 'A', {8,0}, "A" }, // milliseconds in day
1022 // zone
1023 { 'z', {1,2,3,4,0}, "z" }, // x
1024 { 'Z', {1,2,3,4,5,0}, "Z" }, // x
1025 { 'O', {1,4,0}, "O" }, // x
1026 { 'v', {1,4,0}, "v" }, // x
1027 { 'V', {1,2,3,4,0}, "V" }, // x
1028 { 'X', {1,2,3,4,5,0}, "X" }, // x
1029 { 'x', {1,2,3,4,5,0}, "x" }, // x
1030 };
2ca993e8 1031
57a6839d
A
1032 const char ** localeNamesPtr = localeNames;
1033 const char * localeName;
1034 while ( (localeName = *localeNamesPtr++) != NULL) {
1035 UErrorCode status = U_ZERO_ERROR;
1036 Locale locale = Locale::createFromName(localeName);
1037 DateTimePatternGenerator * dtpg = DateTimePatternGenerator::createInstance(locale, status);
1038 if (U_SUCCESS(status)) {
1039 const AllFieldsTestItem * testDataPtr = testData;
2ca993e8 1040 int itemCount = UPRV_LENGTHOF(testData);
57a6839d
A
1041 for (; itemCount-- > 0; ++testDataPtr) {
1042 char skelBuf[FIELD_LENGTH_MAX];
1043 int32_t chrIndx, lenIndx;
1044 for (chrIndx = 0; chrIndx < FIELD_LENGTH_MAX; chrIndx++) {
1045 skelBuf[chrIndx] = testDataPtr->patternChar;
1046 }
1047 for (lenIndx = 0; lenIndx < FIELD_LENGTHS_COUNT; lenIndx++) {
1048 int32_t skelLen = testDataPtr->fieldLengths[lenIndx];
1049 if (skelLen <= 0) {
1050 break;
1051 }
1052 if (skelLen > FIELD_LENGTH_MAX) {
1053 continue;
1054 }
1055 UnicodeString skeleton(skelBuf, skelLen, US_INV);
1056 UnicodeString pattern = dtpg->getBestPattern(skeleton, status);
1057 if (U_FAILURE(status)) {
1058 errln("DateTimePatternGenerator getBestPattern for locale %s, skelChar %c skelLength %d fails: %s",
1059 locale.getName(), testDataPtr->patternChar, skelLen, u_errorName(status));
1060 } else if (pattern.length() <= 0) {
1061 errln("DateTimePatternGenerator getBestPattern for locale %s, skelChar %c skelLength %d produces 0-length pattern",
1062 locale.getName(), testDataPtr->patternChar, skelLen);
1063 } else {
1064 // test that resulting pattern has at least one char in mustIncludeOneOf
1065 UnicodeString mustIncludeOneOf(testDataPtr->mustIncludeOneOf, -1, US_INV);
1066 int32_t patIndx, patLen = pattern.length();
1067 UBool inQuoted = FALSE;
1068 for (patIndx = 0; patIndx < patLen; patIndx++) {
1069 UChar c = pattern.charAt(patIndx);
1070 if (c == 0x27) {
1071 inQuoted = !inQuoted;
1072 } else if (!inQuoted && c <= 0x007A && c >= 0x0041) {
1073 if (mustIncludeOneOf.indexOf(c) >= 0) {
1074 break;
1075 }
1076 }
1077 }
1078 if (patIndx >= patLen) {
1079 errln(UnicodeString("DateTimePatternGenerator getBestPattern for locale ") +
1080 UnicodeString(locale.getName(),-1,US_INV) +
1081 ", skeleton " + skeleton +
1082 ", produces pattern without required chars: " + pattern);
1083 }
2ca993e8 1084
57a6839d
A
1085 }
1086 }
1087 }
1088 delete dtpg;
1089 } else {
1090 dataerrln("Create DateTimePatternGenerator instance for locale(%s) fails: %s",
1091 locale.getName(), u_errorName(status));
1092 }
1093 }
1094}
2ca993e8
A
1095
1096void IntlTestDateTimePatternGeneratorAPI::testStaticGetSkeleton(/*char *par*/)
1097{
1098 // Verify that staticGetSkeleton() doesn't mangle skeletons. (Ticket #11985)
1099 static const char* const testData[] = {
1100 "jmm",
1101 "jjmm",
1102 "Jmm",
1103 "JJmm"
1104 };
1105
1106 for (size_t i = 0; i < UPRV_LENGTHOF(testData); i++) {
1107 UErrorCode status = U_ZERO_ERROR;
1108 UnicodeString skeleton = DateTimePatternGenerator::staticGetSkeleton(testData[i], status);
1109 if (!assertSuccess("staticGetSkeleton", status)) {
1110 return;
1111 }
1112 assertEquals("Skeleton", testData[i], skeleton);
1113 }
1114}
1115
1116void IntlTestDateTimePatternGeneratorAPI::testC() {
0f5d89e8
A
1117 const char* tests[][3] = {
1118 // These may change with actual data for Bhmm/bhmm skeletons
1119 {"zh", "Cm", "Bh:mm"},
1120 {"zh", "CCm", "Bhh:mm"},
1121 {"zh", "CCCm", "BBBBh:mm"},
1122 {"zh", "CCCCm", "BBBBhh:mm"},
1123 {"zh", "CCCCCm", "BBBBBh:mm"},
1124 {"zh", "CCCCCCm", "BBBBBhh:mm"},
1125 {"de", "Cm", "HH:mm"},
1126 {"de", "CCm", "HH:mm"},
1127 {"de", "CCCm", "HH:mm"},
1128 {"de", "CCCCm", "HH:mm"},
1129 {"en", "Cm", "h:mm a"},
1130 {"en", "CCm", "hh:mm a"},
1131 {"en", "CCCm", "h:mm aaaa"},
1132 {"en", "CCCCm", "hh:mm aaaa"},
1133 {"en", "CCCCCm", "h:mm aaaaa"},
1134 {"en", "CCCCCCm", "hh:mm aaaaa"},
1135 {"en-BN", "Cm", "h:mm b"},
1136 {"gu-IN", "Cm", "h:mm B"},
3d1f044b 1137 {"und-IN", "Cm", "h:mm B"}
2ca993e8
A
1138 };
1139
0f5d89e8
A
1140 UErrorCode status = U_ZERO_ERROR;
1141 int32_t numTests = UPRV_LENGTHOF(tests);
1142 for (int32_t i = 0; i < numTests; ++i) {
3d1f044b
A
1143 DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(
1144 Locale::forLanguageTag(tests[i][0], status), status);
f3c0d7a5
A
1145 if (gen == NULL) {
1146 dataerrln("FAIL: DateTimePatternGenerator::createInstance failed for %s", tests[i][0]);
1147 return;
1148 }
0f5d89e8
A
1149 UDateTimePatternMatchOptions options = UDATPG_MATCH_HOUR_FIELD_LENGTH;
1150 UnicodeString pattern = gen->getBestPattern(tests[i][1], options, status);
2ca993e8
A
1151 UnicodeString expectedPattern = tests[i][2];
1152
1153 char message[100] = "\0";
1154 strcat(message, tests[i][0]);
1155 strcat(message, "/");
1156 strcat(message, tests[i][1]);
1157 assertEquals(message, expectedPattern, pattern);
1158 delete gen;
1159 }
1160}
1161
0f5d89e8
A
1162enum { kCharBufMax = 31 };
1163void IntlTestDateTimePatternGeneratorAPI::testSkeletonsWithDayPeriods() {
1164 const char * patterns[] = {
1165 // since icu4c getEmptyInstance does not call addCanonicalItems (unlike J), set these here:
1166 "a", // should get internal skeleton a
1167 "H", // should get internalskeleton H
1168 "m", // should get internalskeleton m
1169 "s", // should get internalskeleton s
1170 // patterns from which to construct sample data for a locale
1171 //"H", // should get internalskeleton H
1172 "h a", // should get internalskeleton ah
1173 "B h", // should get internalskeleton Bh
1174 };
1175 const char* testItems[][2] = {
1176 // sample requested skeletons and results
1177 // skel pattern
1178 { "H", "H"},
1179 { "HH", "HH"},
1180 { "aH", "H"},
1181 { "aHH", "HH"},
1182 { "BH", "H"},
1183 { "BHH", "HH"},
1184 { "BBBBH", "H"},
1185 { "h", "h a"},
1186 { "hh", "hh a"},
1187 { "ah", "h a"},
1188 { "ahh", "hh a"},
1189 { "aaaah", "h aaaa"},
1190 { "aaaahh", "hh aaaa"},
1191 { "bh", "h b"},
1192 { "bhh", "hh b"},
1193 { "bbbbh", "h bbbb"},
1194 { "Bh", "B h"},
1195 { "Bhh", "B hh"},
1196 { "BBBBh", "BBBB h"},
1197 { "BBBBhh", "BBBB hh"},
1198 { "a", "a"},
1199 { "aaaaa", "aaaaa"},
1200 { "b", "b"},
1201 { "bbbb", "bbbb"},
1202 { "B", "B"},
1203 { "BBBB", "BBBB"},
1204 };
1205 UErrorCode status = U_ZERO_ERROR;
1206 DateTimePatternGenerator *gen = DateTimePatternGenerator::createEmptyInstance(status);
1207 if (U_FAILURE(status)) {
1208 errln("ERROR: createEmptyInstance fails, status: %s", u_errorName(status));
1209 } else {
1210 int32_t i, len = UPRV_LENGTHOF(patterns);
1211 for (i = 0; i < len; i++) {
1212 UnicodeString conflictingPattern;
1213 (void)gen->addPattern(UnicodeString(patterns[i]), TRUE, conflictingPattern, status);
1214 if (U_FAILURE(status)) {
1215 errln("ERROR: addPattern %s fail, status: %s", patterns[i], u_errorName(status));
1216 break;
1217 }
1218 }
1219 if (U_SUCCESS(status)) {
1220 len = UPRV_LENGTHOF(testItems);
1221 for (i = 0; i < len; i++) {
1222 status = U_ZERO_ERROR;
1223 UDateTimePatternMatchOptions options = UDATPG_MATCH_HOUR_FIELD_LENGTH;
1224 UnicodeString result = gen->getBestPattern(UnicodeString(testItems[i][0]), options, status);
1225 if (U_FAILURE(status)) {
1226 errln("ERROR: getBestPattern %s fail, status: %s", testItems[i][0], u_errorName(status));
1227 } else if (result != UnicodeString(testItems[i][1])) {
1228 char charResult[kCharBufMax+1];
1229 result.extract(0, result.length(), charResult, kCharBufMax);
1230 charResult[kCharBufMax] = 0; // ensure termination
1231 errln("ERROR: getBestPattern %s, expected %s, got %s", testItems[i][0], testItems[i][1], charResult);
1232 }
1233 }
1234 }
1235 }
1236 delete gen;
1237}
1238
1239typedef struct FieldDisplayNameData {
1240 const char * locale;
1241 UDateTimePatternField field;
1242 UDateTimePGDisplayWidth width;
1243 const char * expected; // can have escapes such as \\u00E0
1244} FieldDisplayNameData;
1245enum { kFieldDisplayNameMax = 32 };
1246
1247void IntlTestDateTimePatternGeneratorAPI::testGetFieldDisplayNames() {
1248 const FieldDisplayNameData testData[] = {
1249 /*loc field width expectedName */
1250 { "de", UDATPG_QUARTER_FIELD, UDATPG_WIDE, "Quartal" },
1251 { "de", UDATPG_QUARTER_FIELD, UDATPG_ABBREVIATED, "Quart." },
1252 { "de", UDATPG_QUARTER_FIELD, UDATPG_NARROW, "Q" },
1253 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
1254 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday. of mo." },
1255 { "en", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday. of mo." }, // fallback
1256 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_WIDE, "weekday of the month" },
1257 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_ABBREVIATED, "wkday of mo" }, // override
1258 { "en_GB", UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, UDATPG_NARROW, "wkday of mo" },
1259 { "it", UDATPG_SECOND_FIELD, UDATPG_WIDE, "secondo" },
1260 { "it", UDATPG_SECOND_FIELD, UDATPG_ABBREVIATED, "s" },
1261 { "it", UDATPG_SECOND_FIELD, UDATPG_NARROW, "s" },
1262 };
1263
1264 int count = UPRV_LENGTHOF(testData);
1265 const FieldDisplayNameData * testDataPtr = testData;
1266 for (; count-- > 0; ++testDataPtr) {
1267 UErrorCode status = U_ZERO_ERROR;
1268 Locale locale(testDataPtr->locale);
1269 DateTimePatternGenerator * dtpg = DateTimePatternGenerator::createInstance(locale, status);
1270 if (U_FAILURE(status)) {
1271 dataerrln("FAIL: DateTimePatternGenerator::createInstance failed for locale %s", testDataPtr->locale);
1272 } else {
1273 UChar expName[kFieldDisplayNameMax+1];
1274 u_unescape(testDataPtr->expected, expName, kFieldDisplayNameMax);
1275 expName[kFieldDisplayNameMax] = 0; // ensure 0 termination
1276 UnicodeString getName = dtpg->getFieldDisplayName(testDataPtr->field, testDataPtr->width);
1277 if (getName.compare(expName, u_strlen(expName)) != 0) {
1278 errln("ERROR: locale %s field %d width %d, expected %s\n",
1279 testDataPtr->locale, testDataPtr->field, testDataPtr->width, testDataPtr->expected);
1280 }
1281 delete dtpg;
1282 }
1283 }
1284}
1285
3d1f044b
A
1286static const UChar timeCycleChars[] = { (UChar)0x0048, (UChar)0x0068, (UChar)0x004B, (UChar)0x006B, (UChar)0 };
1287
1288void IntlTestDateTimePatternGeneratorAPI::testJjMapping() {
1289 UErrorCode status = U_ZERO_ERROR;
1290 UnicodeString jSkeleton("j");
1291 // First test that j maps correctly by region in a locale for which we do not have data.
1292 {
1293 const char* testLocaleID = "de_US"; // short patterns from fallback locale "de" have "HH"
1294 Locale testLocale(testLocaleID);
1295 LocalPointer<DateTimePatternGenerator> dtpg(DateTimePatternGenerator::createInstance(testLocale, status));
1296 if (U_FAILURE(status)) {
1297 dataerrln("FAIL: DateTimePatternGenerator::createInstance failed for locale %s: %s", testLocaleID, u_errorName(status));
1298 } else {
1299 UnicodeString jPattern = dtpg->getBestPattern(jSkeleton, UDATPG_MATCH_ALL_FIELDS_LENGTH, status); // get pattern with h e.g. "h 'Uhr' a"
1300 if (U_FAILURE(status)) {
1301 errln("FAIL: DateTimePatternGenerator::getBestPattern locale %s, pattern j: %s", testLocaleID, u_errorName(status));
1302 } else {
1303 UnicodeString jPatSkeleton = DateTimePatternGenerator::staticGetSkeleton(jPattern, status); // strip literals, get e.g. "ah"
1304 if (U_FAILURE(status)) {
1305 errln("FAIL: DateTimePatternGenerator::staticGetSkeleton locale %s: %s", testLocaleID, u_errorName(status));
1306 } else if (jPatSkeleton.indexOf(u'h') < 0) { // expect US preferred cycle 'h', not H or other cycle
1307 errln("ERROR: DateTimePatternGenerator::getBestPattern locale %s, pattern j did not use 'h'", testLocaleID);
1308 }
1309 }
1310 }
1311 }
1312
1313 // Next test that in all available Locales, the actual short time pattern uses the same cycle as produced by 'j'
1314 int32_t locCount;
1315 const Locale* localePtr = DateFormat::getAvailableLocales(locCount);
1316 for (; locCount-- > 0; localePtr++) {
1317 const char* localeID = localePtr->getName();
1318 status = U_ZERO_ERROR;
1319 LocalPointer<DateTimePatternGenerator> dtpg(DateTimePatternGenerator::createInstance(*localePtr, status));
1320 if (U_FAILURE(status)) {
1321 dataerrln("FAIL: DateTimePatternGenerator::createInstance failed for locale %s: %s", localeID, u_errorName(status));
1322 continue;
1323 }
1324 LocalPointer<DateFormat> dfmt(DateFormat::createTimeInstance(DateFormat::kShort, *localePtr));
1325 if (U_FAILURE(status)) {
1326 dataerrln("FAIL: DateFormat::createTimeInstance kShort failed for locale %s: %s", localeID, u_errorName(status));
1327 continue;
1328 }
1329 const SimpleDateFormat* sdfmt;
1330 if ((sdfmt = dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(dfmt.getAlias()))) == NULL) {
1331 continue;
1332 }
1333 UnicodeString shortPattern;
1334 shortPattern = sdfmt->toPattern(shortPattern);
1335 UnicodeString jPattern = dtpg->getBestPattern(jSkeleton, status);
1336 if (U_FAILURE(status)) {
1337 errln("FAIL: DateTimePatternGenerator::getBestPattern locale %s, pattern j: %s", localeID, u_errorName(status));
1338 continue;
1339 }
1340 // Now check that shortPattern and jPattern use the same hour cycle
1341 UnicodeString jPatSkeleton = DateTimePatternGenerator::staticGetSkeleton(jPattern, status);
1342 UnicodeString shortPatSkeleton = DateTimePatternGenerator::staticGetSkeleton(shortPattern, status);
1343 if (U_FAILURE(status)) {
1344 errln("FAIL: DateTimePatternGenerator::staticGetSkeleton locale %s: %s", localeID, u_errorName(status));
1345 continue;
1346 }
1347 const UChar* charPtr = timeCycleChars;
1348 for (; *charPtr != (UChar)0; charPtr++) {
1349 if (jPatSkeleton.indexOf(*charPtr) >= 0) {
1350 if (shortPatSkeleton.indexOf(*charPtr) < 0) {
1351 char jcBuf[2], spBuf[32];
1352 u_austrncpy(jcBuf, charPtr, 1);
1353 jcBuf[1] = 0;
1354 shortPattern.extract(0, shortPattern.length(), spBuf, 32);
1355 const char* dfmtCalType = (dfmt->getCalendar())->getType();
1356 errln("ERROR: locale %s, expected j resolved char %s to occur in short time pattern %s for %s", localeID, jcBuf, spBuf, dfmtCalType);
1357 }
1358 break;
1359 }
1360 }
1361 }
1362}
1363
46f4442e 1364#endif /* #if !UCONFIG_NO_FORMATTING */