1/********************************************************************
2 * Copyright (c) 2011-2013, 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);
19
20#define LEN(a) (sizeof(a)/sizeof(a[0]))
21
22void addDateIntervalFormatTest(TestNode** root);
23
24#define TESTCASE(x) addTest(root, &x, "tsformat/cdateintervalformattest/" #x)
25
26void addDateIntervalFormatTest(TestNode** root)
27{
28    TESTCASE(TestDateIntervalFormat);
29}
30
31static const char tzUSPacific[] = "US/Pacific";
32static const char tzAsiaTokyo[] = "Asia/Tokyo";
33#define Date201103021030 1299090600000.0 /* 2011-Mar-02 1030 in US/Pacific, 2011-Mar-03 0330 in Asia/Tokyo */
34#define Date201009270800 1285599629000.0 /* 2010-Sep-27 0800 in US/Pacific */
35#define _MINUTE (60.0*1000.0)
36#define _HOUR   (60.0*60.0*1000.0)
37#define _DAY    (24.0*60.0*60.0*1000.0)
38
39typedef struct {
40    const char * locale;
41    const char * skeleton;
42    const char * tzid;
43    const UDate  from;
44    const UDate  to;
45    const char * resultExpected;
46} DateIntervalFormatTestItem;
47
48/* Just a small set of tests for now, the real functionality is tested in the C++ tests */
49static const DateIntervalFormatTestItem testItems[] = {
50    { "en", "MMMdHHmm", tzUSPacific, Date201103021030, Date201103021030 + 7.0*_HOUR,  "Mar 2, 10:30 \\u2013 17:30" },
51    { "en", "MMMdHHmm", tzAsiaTokyo, Date201103021030, Date201103021030 + 7.0*_HOUR,  "Mar 3, 03:30 \\u2013 10:30" },
52    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "Mon, Sep 27, 2010" },
53    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY,  "Mon, Sep 27 \\u2013 Thu, Oct 28, 2010" },
54    { "en", "yMMMEd",   tzUSPacific, Date201009270800, Date201009270800 + 410.0*_DAY, "Mon, Sep 27, 2010 \\u2013 Fri, Nov 11, 2011" },
55    { "de", "Hm",       tzUSPacific, Date201009270800, Date201009270800 + 12.0*_HOUR, "08:00-20:00" },
56    { "de", "Hm",       tzUSPacific, Date201009270800, Date201009270800 + 31.0*_DAY,  "27.9.2010 08:00 - 28.10.2010 08:00" },
57    { "ja", "MMMd",     tzUSPacific, Date201009270800, Date201009270800 + 1.0*_DAY,   "9\\u670827\\u65E5\\uFF5E28\\u65E5" },
58    { NULL, NULL,       NULL,        0,                0,                             NULL }
59};
60
61enum {
62    kSkelBufLen = 32,
63    kTZIDBufLen = 96,
64    kFormatBufLen = 128
65};
66
67static void TestDateIntervalFormat()
68{
69    const DateIntervalFormatTestItem * testItemPtr;
70    UErrorCode status = U_ZERO_ERROR;
71    ctest_setTimeZone(NULL, &status);
72    log_verbose("\nTesting udtitvfmt_open() and udtitvfmt_format() with various parameters\n");
73    for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
74        UDateIntervalFormat* udtitvfmt;
75        int32_t tzidLen;
76        UChar skelBuf[kSkelBufLen];
77        UChar tzidBuf[kTZIDBufLen];
78        const char * tzidForLog = (testItemPtr->tzid)? testItemPtr->tzid: "NULL";
79
80        status = U_ZERO_ERROR;
81        u_unescape(testItemPtr->skeleton, skelBuf, kSkelBufLen);
82        if ( testItemPtr->tzid ) {
83            u_unescape(testItemPtr->tzid, tzidBuf, kTZIDBufLen);
84            tzidLen = -1;
85        } else {
86            tzidLen = 0;
87        }
88        udtitvfmt = udtitvfmt_open(testItemPtr->locale, skelBuf, -1, tzidBuf, tzidLen, &status);
89        if ( U_SUCCESS(status) ) {
90            UChar result[kFormatBufLen];
91            UChar resultExpected[kFormatBufLen];
92            int32_t fmtLen = udtitvfmt_format(udtitvfmt, testItemPtr->from, testItemPtr->to, result, kFormatBufLen, NULL, &status);
93            if (fmtLen >= kFormatBufLen) {
94                result[kFormatBufLen-1] = 0;
95            }
96            if ( U_SUCCESS(status) ) {
97                u_unescape(testItemPtr->resultExpected, resultExpected, kFormatBufLen);
98                if ( u_strcmp(result, resultExpected) != 0 ) {
99                    char bcharBuf[kFormatBufLen];
100                    log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: expect %s, get %s\n",
101                             testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to,
102                             testItemPtr->resultExpected, u_austrcpy(bcharBuf,result) );
103                }
104            } else {
105                log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: %s\n",
106                        testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to, myErrorName(status) );
107            }
108            udtitvfmt_close(udtitvfmt);
109        } else {
110            log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n",
111                    testItemPtr->locale, testItemPtr->skeleton, tzidForLog, myErrorName(status) );
112        }
113    }
114    ctest_resetTimeZone();
115}
116
117#endif /* #if !UCONFIG_NO_FORMATTING */
118