1/********************************************************************
2 * Copyright (c) 2011-2015, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ********************************************************************/
5/* C API TEST FOR DATE INTERVAL FORMAT */
6
7#include "unicode/utypes.h"
8
9#if !UCONFIG_NO_FORMATTING
10
11#include "unicode/udateintervalformat.h"
12#include "unicode/udat.h"
13#include "unicode/ucal.h"
14#include "unicode/ustring.h"
15#include "cintltst.h"
16#include "cmemory.h"
17
18static void TestDateIntervalFormat(void);
19static void TestFPos_SkelWithSeconds(void);
20
21#define LEN(a) (sizeof(a)/sizeof(a[0]))
22
23void addDateIntervalFormatTest(TestNode** root);
24
25#define TESTCASE(x) addTest(root, &x, "tsformat/cdateintervalformattest/" #x)
26
27void addDateIntervalFormatTest(TestNode** root)
28{
29    TESTCASE(TestDateIntervalFormat);
30    TESTCASE(TestFPos_SkelWithSeconds);
31}
32
33static const char tzUSPacific[] = "US/Pacific";
34static const char tzAsiaTokyo[] = "Asia/Tokyo";
35#define Date201103021030 1299090600000.0 /* 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo */
36#define Date201009270800 1285599629000.0 /* 2010-Sep-27 0800 in US/Pacific */
37#define _MINUTE (60.0*1000.0)
38#define _HOUR   (60.0*60.0*1000.0)
39#define _DAY    (24.0*60.0*60.0*1000.0)
40
41typedef struct {
42    const char * locale;
43    const char * skeleton;
44    const char * tzid;
45    const UDate  from;
46    const UDate  to;
47    const char * resultExpected;
48} DateIntervalFormatTestItem;
49
50/* Just a small set of tests for now, the real functionality is tested in the C++ tests */
51static const DateIntervalFormatTestItem testItems[] = {
52    { "en", "MMMdHHmm", tzUSPacific, Date201103021030, Date201103021030 + 7.0*_HOUR,  "Mar 2, 10:30 \\u2013 17:30" },
53    { "en", "MMMdHHmm", tzAsiaTokyo, Date201103021030, Date201103021030 + 7.0*_HOUR,  "Mar 3, 03:30 \\u2013 10:30" },
54    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "Mon, Sep 27, 2010" },
55    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY,  "Mon, Sep 27 \\u2013 Thu, Oct 28, 2010" },
56    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 410.0*_DAY, "Mon, Sep 27, 2010 \\u2013 Fri, Nov 11, 2011" },
57    { "de", "Hm",       tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "08:00\\u201320:00 Uhr" },
58    { "de", "Hm",       tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY,  "27.9.2010, 08:00 \\u2013 28.10.2010, 08:00" },
59    { "ja", "MMMd",     tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY,   "9\\u670827\\u65E5\\uFF5E28\\u65E5" },
60    { NULL, NULL,       NULL,        0,                0,                             NULL }
61};
62
63enum {
64    kSkelBufLen = 32,
65    kTZIDBufLen = 96,
66    kFormatBufLen = 128
67};
68
69static void TestDateIntervalFormat()
70{
71    const DateIntervalFormatTestItem * testItemPtr;
72    UErrorCode status = U_ZERO_ERROR;
73    ctest_setTimeZone(NULL, &status);
74    log_verbose("\nTesting udtitvfmt_open() and udtitvfmt_format() with various parameters\n");
75    for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
76        UDateIntervalFormat* udtitvfmt;
77        int32_t tzidLen;
78        UChar skelBuf[kSkelBufLen];
79        UChar tzidBuf[kTZIDBufLen];
80        const char * tzidForLog = (testItemPtr->tzid)? testItemPtr->tzid: "NULL";
81
82        status = U_ZERO_ERROR;
83        u_unescape(testItemPtr->skeleton, skelBuf, kSkelBufLen);
84        if ( testItemPtr->tzid ) {
85            u_unescape(testItemPtr->tzid, tzidBuf, kTZIDBufLen);
86            tzidLen = -1;
87        } else {
88            tzidLen = 0;
89        }
90        udtitvfmt = udtitvfmt_open(testItemPtr->locale, skelBuf, -1, tzidBuf, tzidLen, &status);
91        if ( U_SUCCESS(status) ) {
92            UChar result[kFormatBufLen];
93            UChar resultExpected[kFormatBufLen];
94            int32_t fmtLen = udtitvfmt_format(udtitvfmt, testItemPtr->from, testItemPtr->to, result, kFormatBufLen, NULL, &status);
95            if (fmtLen >= kFormatBufLen) {
96                result[kFormatBufLen-1] = 0;
97            }
98            if ( U_SUCCESS(status) ) {
99                u_unescape(testItemPtr->resultExpected, resultExpected, kFormatBufLen);
100                if ( u_strcmp(result, resultExpected) != 0 ) {
101                    char bcharBuf[kFormatBufLen];
102                    log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: expect %s, get %s\n",
103                             testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to,
104                             testItemPtr->resultExpected, u_austrcpy(bcharBuf,result) );
105                }
106            } else {
107                log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: %s\n",
108                        testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to, myErrorName(status) );
109            }
110            udtitvfmt_close(udtitvfmt);
111        } else {
112            log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n",
113                    testItemPtr->locale, testItemPtr->skeleton, tzidForLog, myErrorName(status) );
114        }
115    }
116    ctest_resetTimeZone();
117}
118
119/********************************************************************
120 * TestFPos_SkelWithSeconds and related data
121 ********************************************************************
122 */
123
124static UChar zoneGMT[] = { 0x47,0x4D,0x54,0 }; // GMT
125static const UDate startTime = 1416474000000.0; // 2014 Nov 20 09:00 GMT
126
127static const double deltas[] = {
128	0.0, // none
129	200.0, // 200 millisec
130	20000.0, // 20 sec
131	1200000.0, // 20 min
132	7200000.0, // 2 hrs
133	43200000.0, // 12 hrs
134	691200000.0, // 8 days
135	1382400000.0, // 16 days,
136	8640000000.0, // 100 days
137	-1.0
138};
139enum { kNumDeltas = sizeof(deltas)/sizeof(deltas[0]) - 1 };
140
141typedef struct {
142    int32_t posBegin;
143    int32_t posEnd;
144    const char * format;
145} ExpectPosAndFormat;
146
147static const ExpectPosAndFormat exp_en_HHmm[kNumDeltas] = {
148    {  3,  5, "09:00" },
149    {  3,  5, "09:00" },
150    {  3,  5, "09:00" },
151    {  3,  5, "09:00 \\u2013 09:20" },
152    {  3,  5, "09:00 \\u2013 11:00" },
153    {  3,  5, "09:00 \\u2013 21:00" },
154    { 15, 17, "11/20/2014, 09:00 \\u2013 11/28/2014, 09:00" },
155    { 15, 17, "11/20/2014, 09:00 \\u2013 12/6/2014, 09:00" },
156    { 15, 17, "11/20/2014, 09:00 \\u2013 2/28/2015, 09:00" }
157};
158
159static const ExpectPosAndFormat exp_en_HHmmss[kNumDeltas] = {
160    {  3,  5, "09:00:00" },
161    {  3,  5, "09:00:00" },
162    {  3,  5, "09:00:00 \\u2013 09:00:20" },
163    {  3,  5, "09:00:00 \\u2013 09:20:00" },
164    {  3,  5, "09:00:00 \\u2013 11:00:00" },
165    {  3,  5, "09:00:00 \\u2013 21:00:00" },
166    { 15, 17, "11/20/2014, 09:00:00 \\u2013 11/28/2014, 09:00:00" },
167    { 15, 17, "11/20/2014, 09:00:00 \\u2013 12/6/2014, 09:00:00" },
168    { 15, 17, "11/20/2014, 09:00:00 \\u2013 2/28/2015, 09:00:00" }
169};
170
171static const ExpectPosAndFormat exp_en_yyMMdd[kNumDeltas] = {
172    {  0,  0, "11/20/14" },
173    {  0,  0, "11/20/14" },
174    {  0,  0, "11/20/14" },
175    {  0,  0, "11/20/14" },
176    {  0,  0, "11/20/14" },
177    {  0,  0, "11/20/14" },
178    {  0,  0, "11/20/14 \\u2013 11/28/14" },
179    {  0,  0, "11/20/14 \\u2013 12/6/14" },
180    {  0,  0, "11/20/14 \\u2013 2/28/15" }
181};
182
183static const ExpectPosAndFormat exp_en_yyMMddHHmm[kNumDeltas] = {
184    { 13, 15, "11/20/14, 09:00" },
185    { 13, 15, "11/20/14, 09:00" },
186    { 13, 15, "11/20/14, 09:00" },
187    { 13, 15, "11/20/14, 09:00 \\u2013 09:20" },
188    { 13, 15, "11/20/14, 09:00 \\u2013 11:00" },
189    { 13, 15, "11/20/14, 09:00 \\u2013 21:00" },
190    { 13, 15, "11/20/14, 09:00 \\u2013 11/28/14, 09:00" },
191    { 13, 15, "11/20/14, 09:00 \\u2013 12/06/14, 09:00" },
192    { 13, 15, "11/20/14, 09:00 \\u2013 02/28/15, 09:00" }
193};
194
195static const ExpectPosAndFormat exp_en_yyMMddHHmmss[kNumDeltas] = {
196    { 13, 15, "11/20/14, 09:00:00" },
197    { 13, 15, "11/20/14, 09:00:00" },
198    { 13, 15, "11/20/14, 09:00:00 \\u2013 09:00:20" },
199    { 13, 15, "11/20/14, 09:00:00 \\u2013 09:20:00" },
200    { 13, 15, "11/20/14, 09:00:00 \\u2013 11:00:00" },
201    { 13, 15, "11/20/14, 09:00:00 \\u2013 21:00:00" },
202    { 13, 15, "11/20/14, 09:00:00 \\u2013 11/28/14, 09:00:00" },
203    { 13, 15, "11/20/14, 09:00:00 \\u2013 12/06/14, 09:00:00" },
204    { 13, 15, "11/20/14, 09:00:00 \\u2013 02/28/15, 09:00:00" }
205};
206
207static const ExpectPosAndFormat exp_en_yMMMdhmmssz[kNumDeltas] = {
208    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
209    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT" },
210    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:00:20 AM GMT" },
211    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:20:00 AM GMT" },
212    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 11:00:00 AM GMT" },
213    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 9:00:00 PM GMT" },
214    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Nov 28, 2014, 9:00:00 AM GMT" },
215    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Dec 6, 2014, 9:00:00 AM GMT" },
216    { 16, 18, "Nov 20, 2014, 9:00:00 AM GMT \\u2013 Feb 28, 2015, 9:00:00 AM GMT" }
217};
218
219static const ExpectPosAndFormat exp_ja_yyMMddHHmm[kNumDeltas] = {
220    { 11, 13, "14/11/20 9:00" },
221    { 11, 13, "14/11/20 9:00" },
222    { 11, 13, "14/11/20 9:00" },
223    { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E9\\u664220\\u5206" },
224    { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E11\\u664200\\u5206" },
225    { 11, 13, "14/11/20 9\\u664200\\u5206\\uFF5E21\\u664200\\u5206" },
226    { 11, 13, "14/11/20 9:00\\uFF5E14/11/28 9:00" },
227    { 11, 13, "14/11/20 9:00\\uFF5E14/12/06 9:00" },
228    { 11, 13, "14/11/20 9:00\\uFF5E15/02/28 9:00" }
229};
230
231static const ExpectPosAndFormat exp_ja_yyMMddHHmmss[kNumDeltas] = {
232    { 11, 13, "14/11/20 9:00:00" },
233    { 11, 13, "14/11/20 9:00:00" },
234    { 11, 13, "14/11/20 9:00:00\\uFF5E9:00:20" },
235    { 11, 13, "14/11/20 9:00:00\\uFF5E9:20:00" },
236    { 11, 13, "14/11/20 9:00:00\\uFF5E11:00:00" },
237    { 11, 13, "14/11/20 9:00:00\\uFF5E21:00:00" },
238    { 11, 13, "14/11/20 9:00:00\\uFF5E14/11/28 9:00:00" },
239    { 11, 13, "14/11/20 9:00:00\\uFF5E14/12/06 9:00:00" },
240    { 11, 13, "14/11/20 9:00:00\\uFF5E15/02/28 9:00:00" }
241};
242
243static const ExpectPosAndFormat exp_ja_yMMMdHHmmss[kNumDeltas] = {
244    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
245    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00" },
246    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:00:20" },
247    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E9:20:00" },
248    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E11:00:00" },
249    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E21:00:00" },
250    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7411\\u670828\\u65E5 9:00:00" },
251    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2014\\u5E7412\\u67086\\u65E5 9:00:00" },
252    { 14, 16, "2014\\u5E7411\\u670820\\u65E5 9:00:00\\uFF5E2015\\u5E742\\u670828\\u65E5 9:00:00" }
253};
254
255typedef struct {
256	const char * locale;
257	const char * skeleton;
258	UDateFormatField fieldToCheck;
259	const ExpectPosAndFormat * expected;
260} LocaleAndSkeletonItem;
261
262static const LocaleAndSkeletonItem locSkelItems[] = {
263	{ "en",		"HHmm",         UDAT_MINUTE_FIELD, exp_en_HHmm },
264	{ "en",		"HHmmss",       UDAT_MINUTE_FIELD, exp_en_HHmmss },
265	{ "en",		"yyMMdd",       UDAT_MINUTE_FIELD, exp_en_yyMMdd },
266	{ "en",		"yyMMddHHmm",   UDAT_MINUTE_FIELD, exp_en_yyMMddHHmm },
267	{ "en",		"yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_en_yyMMddHHmmss },
268	{ "en",		"yMMMdhmmssz",  UDAT_MINUTE_FIELD, exp_en_yMMMdhmmssz },
269	{ "ja",		"yyMMddHHmm",   UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmm },
270	{ "ja",		"yyMMddHHmmss", UDAT_MINUTE_FIELD, exp_ja_yyMMddHHmmss },
271	{ "ja",		"yMMMdHHmmss",  UDAT_MINUTE_FIELD, exp_ja_yMMMdHHmmss },
272	{ NULL, NULL, (UDateFormatField)0, NULL }
273};
274
275enum { kSizeUBuf = 96, kSizeBBuf = 192 };
276
277static void TestFPos_SkelWithSeconds()
278{
279	const LocaleAndSkeletonItem * locSkelItemPtr;
280	for (locSkelItemPtr = locSkelItems; locSkelItemPtr->locale != NULL; locSkelItemPtr++) {
281	    UDateIntervalFormat* udifmt;
282	    UChar   ubuf[kSizeUBuf];
283	    int32_t ulen, uelen;
284	    UErrorCode status = U_ZERO_ERROR;
285
286	    u_strFromUTF8(ubuf, kSizeUBuf, &ulen, locSkelItemPtr->skeleton, -1, &status);
287	    udifmt = udtitvfmt_open(locSkelItemPtr->locale, ubuf, ulen, zoneGMT, -1, &status);
288	    if ( U_FAILURE(status) ) {
289           log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s: %s\n",
290                    locSkelItemPtr->locale, locSkelItemPtr->skeleton, u_errorName(status));
291	    } else {
292			const double * deltasPtr = deltas;
293			const ExpectPosAndFormat * expectedPtr = locSkelItemPtr->expected;
294			for (; *deltasPtr >= 0.0; deltasPtr++, expectedPtr++) {
295			    UFieldPosition fpos = { locSkelItemPtr->fieldToCheck, 0, 0 };
296			    UChar uebuf[kSizeUBuf];
297			    char bbuf[kSizeBBuf];
298			    char bebuf[kSizeBBuf];
299			    status = U_ZERO_ERROR;
300			    uelen = u_unescape(expectedPtr->format, uebuf, kSizeUBuf);
301			    ulen = udtitvfmt_format(udifmt, startTime, startTime + *deltasPtr, ubuf, kSizeUBuf, &fpos, &status);
302			    if ( U_FAILURE(status) ) {
303			        log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %.1f: %s\n",
304			             locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr, u_errorName(status));
305			    } else if ( ulen != uelen || u_strncmp(ubuf,uebuf,uelen) != 0 ||
306			                fpos.beginIndex != expectedPtr->posBegin || fpos.endIndex != expectedPtr->posEnd ) {
307			        u_strToUTF8(bbuf, kSizeBBuf, NULL, ubuf, ulen, &status);
308			        u_strToUTF8(bebuf, kSizeBBuf, NULL, uebuf, uelen, &status); // convert back to get unescaped string
309			        log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %12.1f, expect %d-%d \"%s\", get %d-%d \"%s\"\n",
310			             locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr,
311			             expectedPtr->posBegin, expectedPtr->posEnd, bebuf,
312			             fpos.beginIndex, fpos.endIndex, bbuf);
313			    }
314			}
315	        udtitvfmt_close(udifmt);
316	    }
317    }
318}
319
320#endif /* #if !UCONFIG_NO_FORMATTING */
321