]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/cintltst/cdateintervalformattest.c
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / test / cintltst / cdateintervalformattest.c
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2011-2016, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST FOR DATE INTERVAL FORMAT */
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "unicode/udateintervalformat.h"
14 #include "unicode/udat.h"
15 #include "unicode/ucal.h"
16 #include "unicode/ustring.h"
17 #include "unicode/udisplaycontext.h"
18 #include "cintltst.h"
19 #include "cmemory.h"
20 #include "cformtst.h"
21
22 static void TestDateIntervalFormat(void);
23 static void TestFPos_SkelWithSeconds(void);
24 static void TestFormatToResult(void);
25 static void TestOpen(void);
26
27 void addDateIntervalFormatTest(TestNode** root);
28
29 #define TESTCASE(x) addTest(root, &x, "tsformat/cdateintervalformattest/" #x)
30
31 void addDateIntervalFormatTest(TestNode** root)
32 {
33 TESTCASE(TestDateIntervalFormat);
34 TESTCASE(TestFPos_SkelWithSeconds);
35 TESTCASE(TestFormatToResult);
36 TESTCASE(TestOpen);
37 }
38
39 static const char tzUSPacific[] = "US/Pacific";
40 static const char tzAsiaTokyo[] = "Asia/Tokyo";
41 #define Date201103021030 1299090600000.0 /* 2011-Mar-02 Wed 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo */
42 #define Date201009270800 1285599629000.0 /* 2010-Sep-27 Mon 0800 in US/Pacific */
43 #define Date201712300900 1514653200000.0 /* 2017-Dec-30 Sat 0900 in US/Pacific */
44 #define _MINUTE (60.0*1000.0)
45 #define _HOUR (60.0*60.0*1000.0)
46 #define _DAY (24.0*60.0*60.0*1000.0)
47 #define MIN_NONE UDTITVFMT_MINIMIZE_NONE
48 #define MIN_MONTHS UDTITVFMT_MINIMIZE_ADJACENT_MONTHS
49 #define MIN_DAYS UDTITVFMT_MINIMIZE_ADJACENT_DAYS
50 #define C_NONE UDISPCTX_CAPITALIZATION_NONE
51 #define C_MID UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE
52 #define C_BEGIN UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE
53 #define C_MENU UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU
54 #define C_ALONE UDISPCTX_CAPITALIZATION_FOR_STANDALONE
55
56 typedef struct {
57 const char * locale;
58 const char * skeleton;
59 const char * tzid;
60 UDateIntervalFormatAttributeValue minimizeType;
61 UDisplayContext context;
62 const UDate from;
63 const UDate to;
64 const char * resultExpected;
65 } DateIntervalFormatTestItem;
66
67 /* Just a small set of tests for now, the real functionality is tested in the C++ tests */
68 static const DateIntervalFormatTestItem testItems[] = {
69 { "en", "MMMdHHmm", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 7.0*_HOUR, "Mar 2, 10:30\\u2009\\u2013\\u200917:30" },
70 { "en", "MMMdHHmm", tzAsiaTokyo, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 7.0*_HOUR, "Mar 3, 03:30\\u2009\\u2013\\u200910:30" },
71 { "en", "yMMMEd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 12.0*_HOUR, "Mon, Sep 27, 2010" },
72 { "en", "yMMMEd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 31.0*_DAY, "Mon, Sep 27\\u2009\\u2013\\u2009Thu, Oct 28, 2010" },
73 { "en", "yMMMEd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 410.0*_DAY, "Mon, Sep 27, 2010\\u2009\\u2013\\u2009Fri, Nov 11, 2011" },
74 { "de", "Hm", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 12.0*_HOUR, "08:00\\u201320:00 Uhr" },
75 { "de", "Hm", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 31.0*_DAY, "27.9.2010, 08:00\\u2009\\u2013\\u200928.10.2010, 08:00" },
76 { "ja", "MMMd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 1.0*_DAY, "9\\u670827\\u65E5\\uFF5E28\\u65E5" },
77 { "en", "jm", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 1.0*_HOUR, "10:30 AM\\u2009\\u2013\\u200911:30 AM" },
78 { "en", "jm", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 12.0*_HOUR, "10:30 AM\\u2009\\u2013\\u200910:30 PM" },
79 { "it", "yMMMMd", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 15.0*_DAY, "2\\u201317 marzo 2011" },
80 { "en_SA", "MMMd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "18\\u2009\\u2013\\u200924 Shaw." },
81 { "en@calendar=islamic-umalqura", "MMMd", tzUSPacific, MIN_NONE, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "Shaw. 18\\u2009\\u2013\\u200924" },
82 { "fr", "E", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 3.0*_DAY, "mer.\\u2009\\u2013\\u2009sam." },
83 { "fr", "E", tzUSPacific, MIN_NONE, C_BEGIN, Date201103021030, Date201103021030 + 3.0*_DAY, "Mer.\\u2009\\u2013\\u2009sam." },
84 { "fr", "E", tzUSPacific, MIN_NONE, C_MENU, Date201103021030, Date201103021030 + 3.0*_DAY, "mer.\\u2009\\u2013\\u2009sam." },
85 { "fr", "E", tzUSPacific, MIN_NONE, C_ALONE, Date201103021030, Date201103021030 + 3.0*_DAY, "Mer.\\u2009\\u2013\\u2009sam." },
86 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 3.0*_DAY, "mars 2011" },
87 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_BEGIN, Date201103021030, Date201103021030 + 3.0*_DAY, "Mars 2011" },
88 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_MENU, Date201103021030, Date201103021030 + 3.0*_DAY, "mars 2011" },
89 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_ALONE, Date201103021030, Date201103021030 + 3.0*_DAY, "Mars 2011" },
90 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_NONE, Date201103021030, Date201103021030 + 40.0*_DAY, "mars\\u2009\\u2013\\u2009avril 2011" },
91 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_BEGIN, Date201103021030, Date201103021030 + 40.0*_DAY, "Mars\\u2009\\u2013\\u2009avril 2011" },
92 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_MENU, Date201103021030, Date201103021030 + 40.0*_DAY, "mars\\u2009\\u2013\\u2009avril 2011" },
93 { "fr", "yMMMM", tzUSPacific, MIN_NONE, C_ALONE, Date201103021030, Date201103021030 + 40.0*_DAY, "Mars\\u2009\\u2013\\u2009avril 2011" },
94 // Apple-specific
95 { "en", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "Sep 27\\u2009\\u2013\\u20093" },
96 { "en", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 32.0*_DAY, "Sep 27\\u2009\\u2013\\u2009Oct 29" },
97 { "en", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 6.0*_DAY, "Dec 30\\u2009\\u2013\\u20095" }, // across year boundary
98 { "en", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 32.0*_DAY, "Dec 30, 2017\\u2009\\u2013\\u2009Jan 31, 2018" }, // across year boundary but > 1 month
99 { "fr", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "27\\u20133 oct." },
100 { "fr", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 32.0*_DAY, "27 sept.\\u2009\\u2013\\u200929 oct." },
101 { "fr", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 6.0*_DAY, "30\\u20135 janv." }, // across year boundary
102 { "fr", "MMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 32.0*_DAY, "30 d\\u00E9c. 2017\\u2009\\u2013\\u200931 janv. 2018" }, // across year boundary but > 1 month
103
104 { "en", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "Sep 27\\u2009\\u2013\\u20093, 2010" },
105 { "en", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 32.0*_DAY, "Sep 27\\u2009\\u2013\\u2009Oct 29, 2010" },
106 { "en", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 6.0*_DAY, "Dec 30, 2017\\u2009\\u2013\\u2009Jan 5, 2018" }, // across year boundary
107 { "en", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 32.0*_DAY, "Dec 30, 2017\\u2009\\u2013\\u2009Jan 31, 2018" }, // across year boundary but > 1 month
108 { "fr", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 6.0*_DAY, "27\\u20133 oct. 2010" },
109 { "fr", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201009270800, Date201009270800 + 32.0*_DAY, "27 sept.\\u2009\\u2013\\u200929 oct. 2010" },
110 { "fr", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 6.0*_DAY, "30 d\\u00E9c. 2017\\u2009\\u2013\\u20095 janv. 2018" }, // across year boundary
111 { "fr", "yMMMd", tzUSPacific, MIN_MONTHS, C_NONE, Date201712300900, Date201712300900 + 32.0*_DAY, "30 d\\u00E9c. 2017\\u2009\\u2013\\u200931 janv. 2018" }, // across year boundary but > 1 month
112
113 { "en", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800, Date201009270800 + 10.0*_HOUR, "Sep 27, 8:00 AM\\u2009\\u2013\\u20096:00 PM" },
114 { "en", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800, Date201009270800 + 17.0*_HOUR, "Sep 27, 8:00 AM\\u2009\\u2013\\u2009Sep 28, 1:00 AM" },
115 { "en", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 17.0*_HOUR, "Sep 27, 8:00 PM\\u2009\\u2013\\u20091:00 AM" },
116 { "en", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 26.0*_HOUR, "Sep 27, 8:00 PM\\u2009\\u2013\\u2009Sep 28, 10:00 AM" },
117 { "en", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 35.0*_HOUR, "Sep 27, 8:00 PM\\u2009\\u2013\\u2009Sep 28, 7:00 PM" },
118 { "fr", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800, Date201009270800 + 10.0*_HOUR, "27 sept. \\u00E0 08:00\\u2009\\u2013\\u200918:00" },
119 { "fr", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800, Date201009270800 + 17.0*_HOUR, "27 sept. \\u00E0 08:00\\u2009\\u2013\\u200928 sept. \\u00E0 01:00" },
120 { "fr", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 17.0*_HOUR, "27 sept. \\u00E0 20:00\\u2009\\u2013\\u200901:00" },
121 { "fr", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 26.0*_HOUR, "27 sept. \\u00E0 20:00\\u2009\\u2013\\u200928 sept. \\u00E0 10:00" },
122 { "fr", "MMMdjmm", tzUSPacific, MIN_DAYS, C_NONE, Date201009270800 + 12.0*_HOUR, Date201009270800 + 35.0*_HOUR, "27 sept. \\u00E0 20:00\\u2009\\u2013\\u200928 sept. \\u00E0 19:00" },
123
124 { NULL, NULL, NULL, MIN_NONE, C_NONE, 0, 0, NULL }
125 };
126
127 enum {
128 kSkelBufLen = 32,
129 kTZIDBufLen = 96,
130 kFormatBufLen = 128
131 };
132
133 static void TestDateIntervalFormat()
134 {
135 const DateIntervalFormatTestItem * testItemPtr;
136 UErrorCode status = U_ZERO_ERROR;
137 ctest_setTimeZone(NULL, &status);
138 log_verbose("\nTesting udtitvfmt_open() and udtitvfmt_format() with various parameters\n");
139 for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
140 UDateIntervalFormat* udtitvfmt;
141 int32_t tzidLen;
142 UChar skelBuf[kSkelBufLen];
143 UChar tzidBuf[kTZIDBufLen];
144 const char * tzidForLog = (testItemPtr->tzid)? testItemPtr->tzid: "NULL";
145
146 status = U_ZERO_ERROR;
147 u_unescape(testItemPtr->skeleton, skelBuf, kSkelBufLen);
148 if ( testItemPtr->tzid ) {
149 u_unescape(testItemPtr->tzid, tzidBuf, kTZIDBufLen);
150 tzidLen = -1;
151 } else {
152 tzidLen = 0;
153 }
154 udtitvfmt = udtitvfmt_open(testItemPtr->locale, skelBuf, -1, tzidBuf, tzidLen, &status);
155 if ( U_SUCCESS(status) ) {
156 UChar result[kFormatBufLen];
157 UChar resultExpected[kFormatBufLen];
158 udtitvfmt_setAttribute(udtitvfmt, UDTITVFMT_MINIMIZE_TYPE, testItemPtr->minimizeType, &status);
159 if ( U_FAILURE(status) ) {
160 log_err("FAIL: udtitvfmt_setAttribute for locale %s, skeleton %s, tzid %s, minimizeType %d: %s\n",
161 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->minimizeType, myErrorName(status) );
162 continue;
163 }
164 udtitvfmt_setContext(udtitvfmt, testItemPtr->context, &status);
165 if ( U_FAILURE(status) ) {
166 log_err("FAIL: udtitvfmt_setContext for locale %s, skeleton %s, tzid %s, context %04X: %s\n",
167 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->context, myErrorName(status) );
168 continue;
169 }
170 UDisplayContext getContext = udtitvfmt_getContext(udtitvfmt, UDISPCTX_TYPE_CAPITALIZATION, &status);
171 if ( U_FAILURE(status) ) {
172 log_err("FAIL: udtitvfmt_getContext for locale %s, skeleton %s, tzid %s, context %04X: %s\n",
173 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->context, myErrorName(status) );
174 continue;
175 } else if (getContext != testItemPtr->context) {
176 log_err("FAIL: udtitvfmt_getContext for locale %s, skeleton %s, tzid %s, set context %04X but got %04X\n",
177 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->context, getContext );
178 continue;
179 }
180
181 int32_t fmtLen = udtitvfmt_format(udtitvfmt, testItemPtr->from, testItemPtr->to, result, kFormatBufLen, NULL, &status);
182 if (fmtLen >= kFormatBufLen) {
183 result[kFormatBufLen-1] = 0;
184 }
185 if ( U_SUCCESS(status) ) {
186 u_unescape(testItemPtr->resultExpected, resultExpected, kFormatBufLen);
187 if ( u_strcmp(result, resultExpected) != 0 ) {
188 char bcharBuf[kFormatBufLen];
189 #if 0
190 log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, minimizeType %d, from %.1f, to %.1f: expect %s, get %s\n",
191 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->minimizeType,
192 testItemPtr->from, testItemPtr->to, testItemPtr->resultExpected, u_austrcpy(bcharBuf,result) );
193 #else
194 // Apple-specific version
195 char bexpbuf[kFormatBufLen];
196 u_strToUTF8(bexpbuf, kFormatBufLen, NULL, resultExpected, -1, &status);
197 u_strToUTF8(bcharBuf, kFormatBufLen, NULL, result, fmtLen, &status);
198 log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, minimizeType %d, context %04X, from %.1f, to %.1f: expect %s, get %s\n",
199 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->minimizeType, (int)testItemPtr->context,
200 testItemPtr->from, testItemPtr->to, bexpbuf, bcharBuf );
201 #endif
202 }
203 } else {
204 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, minimizeType %d, from %.1f, to %.1f: %s\n",
205 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, (int)testItemPtr->minimizeType,
206 testItemPtr->from, testItemPtr->to, myErrorName(status) );
207 }
208 udtitvfmt_close(udtitvfmt);
209 } else {
210 log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n",
211 testItemPtr->locale, testItemPtr->skeleton, tzidForLog, myErrorName(status) );
212 }
213 }
214 ctest_resetTimeZone();
215 }
216
217 /********************************************************************
218 * TestFPos_SkelWithSeconds and related data
219 ********************************************************************
220 */
221
222 static UChar zoneGMT[] = { 0x47,0x4D,0x54,0 }; // GMT
223 static const UDate startTime = 1416474000000.0; // 2014 Nov 20 09:00 GMT
224
225 static const double deltas[] = {
226 0.0, // none
227 200.0, // 200 millisec
228 20000.0, // 20 sec
229 1200000.0, // 20 min
230 7200000.0, // 2 hrs
231 43200000.0, // 12 hrs
232 691200000.0, // 8 days
233 1382400000.0, // 16 days,
234 8640000000.0, // 100 days
235 -1.0
236 };
237 enum { kNumDeltas = UPRV_LENGTHOF(deltas) - 1 };
238
239 typedef struct {
240 int32_t posBegin;
241 int32_t posEnd;
242 const char * format;
243 } ExpectPosAndFormat;
244
245 static const ExpectPosAndFormat exp_en_HHmm[kNumDeltas] = {
246 { 3, 5, "09:00" },
247 { 3, 5, "09:00" },
248 { 3, 5, "09:00" },
249 { 3, 5, "09:00\\u2009\\u2013\\u200909:20" },
250 { 3, 5, "09:00\\u2009\\u2013\\u200911:00" },
251 { 3, 5, "09:00\\u2009\\u2013\\u200921:00" },
252 { 15, 17, "11/20/2014, 09:00\\u2009\\u2013\\u200911/28/2014, 09:00" },
253 { 15, 17, "11/20/2014, 09:00\\u2009\\u2013\\u200912/6/2014, 09:00" },
254 { 15, 17, "11/20/2014, 09:00\\u2009\\u2013\\u20092/28/2015, 09:00" }
255 };
256
257 static const ExpectPosAndFormat exp_en_HHmmss[kNumDeltas] = {
258 { 3, 5, "09:00:00" },
259 { 3, 5, "09:00:00" },
260 { 3, 5, "09:00:00\\u2009\\u2013\\u200909:00:20" },
261 { 3, 5, "09:00:00\\u2009\\u2013\\u200909:20:00" },
262 { 3, 5, "09:00:00\\u2009\\u2013\\u200911:00:00" },
263 { 3, 5, "09:00:00\\u2009\\u2013\\u200921:00:00" },
264 { 15, 17, "11/20/2014, 09:00:00\\u2009\\u2013\\u200911/28/2014, 09:00:00" },
265 { 15, 17, "11/20/2014, 09:00:00\\u2009\\u2013\\u200912/6/2014, 09:00:00" },
266 { 15, 17, "11/20/2014, 09:00:00\\u2009\\u2013\\u20092/28/2015, 09:00:00" }
267 };
268
269 static const ExpectPosAndFormat exp_en_yyMMdd[kNumDeltas] = {
270 { 0, 0, "11/20/14" },
271 { 0, 0, "11/20/14" },
272 { 0, 0, "11/20/14" },
273 { 0, 0, "11/20/14" },
274 { 0, 0, "11/20/14" },
275 { 0, 0, "11/20/14" },
276 { 0, 0, "11/20/14\\u2009\\u2013\\u200911/28/14" },
277 { 0, 0, "11/20/14\\u2009\\u2013\\u200912/6/14" },
278 { 0, 0, "11/20/14\\u2009\\u2013\\u20092/28/15" }
279 };
280
281 static const ExpectPosAndFormat exp_en_yyMMddHHmm[kNumDeltas] = {
282 { 13, 15, "11/20/14, 09:00" },
283 { 13, 15, "11/20/14, 09:00" },
284 { 13, 15, "11/20/14, 09:00" },
285 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200909:20" },
286 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200911:00" },
287 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200921:00" },
288 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200911/28/14, 09:00" },
289 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200912/06/14, 09:00" },
290 { 13, 15, "11/20/14, 09:00\\u2009\\u2013\\u200902/28/15, 09:00" }
291 };
292
293 static const ExpectPosAndFormat exp_en_yyMMddHHmmss[kNumDeltas] = {
294 { 13, 15, "11/20/14, 09:00:00" },
295 { 13, 15, "11/20/14, 09:00:00" },
296 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200909:00:20" },
297 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200909:20:00" },
298 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200911:00:00" },
299 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200921:00:00" },
300 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200911/28/14, 09:00:00" },
301 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200912/06/14, 09:00:00" },
302 { 13, 15, "11/20/14, 09:00:00\\u2009\\u2013\\u200902/28/15, 09:00:00" }
303 };
304
305 static const ExpectPosAndFormat exp_en_yMMMdhmmssz[kNumDeltas] = {
306 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
307 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
308 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u20099:00:20 AM GMT" },
309 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u20099:20:00 AM GMT" },
310 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u200911:00:00 AM GMT" },
311 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u20099:00:00 PM GMT" },
312 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u2009Nov 28, 2014, 9:00:00 AM GMT" },
313 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u2009Dec 6, 2014, 9:00:00 AM GMT" },
314 { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT\\u2009\\u2013\\u2009Feb 28, 2015, 9:00:00 AM GMT" }
315 };
316
317 static const ExpectPosAndFormat exp_ja_yyMMddHHmm[kNumDeltas] = {
318 { 11, 13, "14/11/20 9:00" },
319 { 11, 13, "14/11/20 9:00" },
320 { 11, 13, "14/11/20 9:00" },
321 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E9\\u664220\\u5206" },
322 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E11\\u664200\\u5206" },
323 { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E21\\u664200\\u5206" },
324 { 11, 13, "14/11/20 9:00\\uFF5E14/11/28 9:00" },
325 { 11, 13, "14/11/20 9:00\\uFF5E14/12/06 9:00" },
326 { 11, 13, "14/11/20 9:00\\uFF5E15/02/28 9:00" }
327 };
328
329 static const ExpectPosAndFormat exp_ja_yyMMddHHmmss[kNumDeltas] = {
330 { 11, 13, "14/11/20 9:00:00" },
331 { 11, 13, "14/11/20 9:00:00" },
332 { 11, 13, "14/11/20 9:00:00\\uFF5E9:00:20" },
333 { 11, 13, "14/11/20 9:00:00\\uFF5E9:20:00" },
334 { 11, 13, "14/11/20 9:00:00\\uFF5E11:00:00" },
335 { 11, 13, "14/11/20 9:00:00\\uFF5E21:00:00" },
336 { 11, 13, "14/11/20 9:00:00\\uFF5E14/11/28 9:00:00" },
337 { 11, 13, "14/11/20 9:00:00\\uFF5E14/12/06 9:00:00" },
338 { 11, 13, "14/11/20 9:00:00\\uFF5E15/02/28 9:00:00" }
339 };
340
341 static const ExpectPosAndFormat exp_ja_yMMMdHHmmss[kNumDeltas] = {
342 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
343 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
344 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:00:20" },
345 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:20:00" },
346 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E11:00:00" },
347 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E21:00:00" },
348 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7411\\u670828\\u65E5 9:00:00" },
349 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7412\\u67086\\u65E5 9:00:00" },
350 { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2015\\u5E742\\u670828\\u65E5 9:00:00" }
351 };
352
353 typedef struct {
354 const char * locale;
355 const char * skeleton;
356 UDateFormatField fieldToCheck;
357 const ExpectPosAndFormat * expected;
358 } LocaleAndSkeletonItem;
359
360 static const LocaleAndSkeletonItem locSkelItems[] = {
361 { "en", "HHmm", UDAT_MINUTE_FIELD, exp_en_HHmm },
362 { "en", "HHmmss", UDAT_MINUTE_FIELD, exp_en_HHmmss },
363 { "en", "yyMMdd", UDAT_MINUTE_FIELD, exp_en_yyMMdd },
364 { "en", "yyMMddHHmm", UDAT_MINUTE_FIELD, exp_en_yyMMddHHmm },
365 { "en", "yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_en_yyMMddHHmmss },
366 { "en", "yMMMdhmmssz", UDAT_MINUTE_FIELD, exp_en_yMMMdhmmssz },
367 { "ja", "yyMMddHHmm", UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmm },
368 { "ja", "yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmmss },
369 { "ja", "yMMMdHHmmss", UDAT_MINUTE_FIELD, exp_ja_yMMMdHHmmss },
370 { NULL, NULL, (UDateFormatField)0, NULL }
371 };
372
373 enum { kSizeUBuf = 96, kSizeBBuf = 192 };
374
375 static void TestFPos_SkelWithSeconds()
376 {
377 const LocaleAndSkeletonItem * locSkelItemPtr;
378 for (locSkelItemPtr = locSkelItems; locSkelItemPtr->locale != NULL; locSkelItemPtr++) {
379 UDateIntervalFormat* udifmt;
380 UChar ubuf[kSizeUBuf];
381 int32_t ulen, uelen;
382 UErrorCode status = U_ZERO_ERROR;
383 ulen = u_unescape(locSkelItemPtr->skeleton, ubuf, kSizeUBuf);
384 udifmt = udtitvfmt_open(locSkelItemPtr->locale, ubuf, ulen, zoneGMT, -1, &status);
385 if ( U_FAILURE(status) ) {
386 log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s: %s\n",
387 locSkelItemPtr->locale, locSkelItemPtr->skeleton, u_errorName(status));
388 } else {
389 const double * deltasPtr = deltas;
390 const ExpectPosAndFormat * expectedPtr = locSkelItemPtr->expected;
391 for (; *deltasPtr >= 0.0; deltasPtr++, expectedPtr++) {
392 UFieldPosition fpos = { locSkelItemPtr->fieldToCheck, 0, 0 };
393 UChar uebuf[kSizeUBuf];
394 char bbuf[kSizeBBuf];
395 char bebuf[kSizeBBuf];
396 status = U_ZERO_ERROR;
397 uelen = u_unescape(expectedPtr->format, uebuf, kSizeUBuf);
398 ulen = udtitvfmt_format(udifmt, startTime, startTime + *deltasPtr, ubuf, kSizeUBuf, &fpos, &status);
399 if ( U_FAILURE(status) ) {
400 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %.1f: %s\n",
401 locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr, u_errorName(status));
402 } else if ( ulen != uelen || u_strncmp(ubuf,uebuf,uelen) != 0 ||
403 fpos.beginIndex != expectedPtr->posBegin || fpos.endIndex != expectedPtr->posEnd ) {
404 u_strToUTF8(bbuf, kSizeBBuf, NULL, ubuf, ulen, &status);
405 u_strToUTF8(bebuf, kSizeBBuf, NULL, uebuf, uelen, &status); // convert back to get unescaped string
406 log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %12.1f, expect %d-%d \"%s\", get %d-%d \"%s\"\n",
407 locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr,
408 expectedPtr->posBegin, expectedPtr->posEnd, bebuf,
409 fpos.beginIndex, fpos.endIndex, bbuf);
410 }
411 }
412 udtitvfmt_close(udifmt);
413 }
414 }
415 }
416
417 static void TestFormatToResult() {
418 UErrorCode ec = U_ZERO_ERROR;
419 UDateIntervalFormat* fmt = udtitvfmt_open("de", u"dMMMMyHHmm", -1, zoneGMT, -1, &ec);
420 UFormattedDateInterval* fdi = udtitvfmt_openResult(&ec);
421 assertSuccess("Opening", &ec);
422
423 {
424 const char* message = "Field position test 1";
425 const UChar* expectedString = u"27. September 2010, 15:00\u2009\u20092. März 2011, 18:30";
426 udtitvfmt_formatToResult(fmt, fdi, Date201009270800, Date201103021030, &ec);
427 assertSuccess("Formatting", &ec);
428 static const UFieldPositionWithCategory expectedFieldPositions[] = {
429 // category, field, begin index, end index
430 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 0, 25},
431 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
432 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
433 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
434 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
435 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
436 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 28, 47},
437 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 28, 29},
438 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 31, 35},
439 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 36, 40},
440 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 42, 44},
441 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 45, 47}};
442 checkMixedFormattedValue(
443 message,
444 udtitvfmt_resultAsValue(fdi, &ec),
445 expectedString,
446 expectedFieldPositions,
447 UPRV_LENGTHOF(expectedFieldPositions));
448 }
449 {
450 const char* message = "Field position test 1";
451 const UChar* expectedString = u"27. September 2010, 15:00–22:00 Uhr";
452 udtitvfmt_formatToResult(fmt, fdi, Date201009270800, Date201009270800 + 7*_HOUR, &ec);
453 assertSuccess("Formatting", &ec);
454 static const UFieldPositionWithCategory expectedFieldPositions[] = {
455 // category, field, begin index, end index
456 {UFIELD_CATEGORY_DATE, UDAT_DATE_FIELD, 0, 2},
457 {UFIELD_CATEGORY_DATE, UDAT_MONTH_FIELD, 4, 13},
458 {UFIELD_CATEGORY_DATE, UDAT_YEAR_FIELD, 14, 18},
459 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 0, 20, 25},
460 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 20, 22},
461 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 23, 25},
462 {UFIELD_CATEGORY_DATE_INTERVAL_SPAN, 1, 26, 31},
463 {UFIELD_CATEGORY_DATE, UDAT_HOUR_OF_DAY0_FIELD, 26, 28},
464 {UFIELD_CATEGORY_DATE, UDAT_MINUTE_FIELD, 29, 31},
465 {UFIELD_CATEGORY_DATE, UDAT_AM_PM_FIELD, 32, 35}};
466 checkMixedFormattedValue(
467 message,
468 udtitvfmt_resultAsValue(fdi, &ec),
469 expectedString,
470 expectedFieldPositions,
471 UPRV_LENGTHOF(expectedFieldPositions));
472 }
473
474 udtitvfmt_close(fmt);
475 udtitvfmt_closeResult(fdi);
476 }
477
478 static const char* openLocales[] = {
479 "en",
480 "en@calendar=japanese",
481 "en@calendar=coptic",
482 "en@calendar=chinese",
483 "en_001",
484 "en_001@calendar=japanese",
485 "en_001@calendar=coptic",
486 "en_001@calendar=chinese",
487 "en_AU",
488 "en_AU@calendar=japanese", // had problems
489 "en_AU@calendar=coptic", // had problems
490 "en_AU@calendar=chinese",
491 "en_CA",
492 "en_CA@calendar=japanese", // had problems
493 "en_CA@calendar=coptic", // had problems
494 "en_CA@calendar=chinese",
495 "en_CN",
496 "en_CN@calendar=japanese", // had problems
497 "en_CN@calendar=coptic", // had problems
498 "en_CN@calendar=chinese",
499 "en_DE@calendar=japanese", // had problems
500 "en_DE@calendar=coptic", // had problems
501 "en_GB",
502 "en_GB@calendar=japanese", // had problems
503 "en_GB@calendar=coptic", // had problems
504 "en_GB@calendar=chinese",
505 "en_HK@calendar=japanese", // had problems
506 "en_HK@calendar=coptic", // had problems
507 "en_IE@calendar=japanese", // had problems
508 "en_IE@calendar=coptic", // had problems
509 "en_IN@calendar=japanese", // had problems
510 "en_IN@calendar=coptic", // had problems
511 "en_JP",
512 "en_JP@calendar=japanese",
513 "en_JP@calendar=coptic",
514 "en_JP@calendar=chinese",
515 "en_NZ",
516 "en_NZ@calendar=japanese", // had problems
517 "en_NZ@calendar=coptic", // had problems
518 "en_NZ@calendar=chinese",
519 "en_SG@calendar=japanese", // had problems
520 "en_SG@calendar=coptic", // had problems
521 "es",
522 "es@calendar=japanese",
523 "es@calendar=coptic",
524 "es@calendar=chinese",
525 "es_419",
526 "es_419@calendar=japanese", // had problems
527 "es_419@calendar=coptic", // had problems
528 "es_419@calendar=chinese",
529 "es_MX",
530 "es_MX@calendar=japanese",
531 "es_MX@calendar=coptic",
532 "es_MX@calendar=chinese",
533 "es_US",
534 "es_US@calendar=japanese",
535 "es_US@calendar=coptic",
536 "es_US@calendar=chinese",
537 "fr",
538 "fr@calendar=japanese",
539 "fr@calendar=coptic",
540 "fr@calendar=chinese",
541 "fr_CA",
542 "fr_CA@calendar=japanese", // had problems
543 "fr_CA@calendar=coptic", // had problems
544 "fr_CA@calendar=chinese",
545 "fr_CH",
546 "fr_CH@calendar=japanese",
547 "fr_CH@calendar=coptic",
548 "fr_CH@calendar=chinese",
549 "fr_BE",
550 "fr_BE@calendar=japanese",
551 "fr_BE@calendar=coptic",
552 "fr_BE@calendar=chinese",
553 "nl_BE@calendar=japanese", // had problems
554 "nl_BE@calendar=coptic", // had problems
555 "pt",
556 "pt@calendar=japanese",
557 "pt@calendar=coptic",
558 "pt@calendar=chinese",
559 "pt_PT",
560 "pt_PT@calendar=japanese", // had problems
561 "pt_PT@calendar=coptic", // had problems
562 "pt_PT@calendar=chinese",
563 "zh_Hant",
564 "zh_Hant@calendar=japanese",
565 "zh_Hant@calendar=coptic",
566 "zh_Hant@calendar=chinese",
567 "zh_Hant_HK",
568 "zh_Hant_HK@calendar=japanese", // had problems
569 "zh_Hant_HK@calendar=coptic", // had problems
570 "zh_Hant_HK@calendar=chinese",
571 NULL
572 };
573 static const UChar* openSkeleton = u"zzzzyMMMMEEEEdhmmss";
574 static const UChar* openZone = u"America/Vancouver";
575
576 static void TestOpen()
577 {
578 const char* locale;
579 const char** localesPtr = openLocales;
580 while ((locale = *localesPtr++) != NULL) {
581 UErrorCode status = U_ZERO_ERROR;
582 UDateIntervalFormat* udatintv = udtitvfmt_open(locale, openSkeleton, -1, openZone, -1, &status);
583 if ( U_FAILURE(status) ) {
584 log_err("FAIL: udtitvfmt_open for locale %s: %s\n", locale, u_errorName(status));
585 } else {
586 udtitvfmt_close(udatintv);
587 }
588 }
589 }
590
591 #endif /* #if !UCONFIG_NO_FORMATTING */