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 */
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/udateintervalformat.h"
12 #include "unicode/udat.h"
13 #include "unicode/ucal.h"
14 #include "unicode/ustring.h"
18 static void TestDateIntervalFormat(void);
20 #define LEN(a) (sizeof(a)/sizeof(a[0]))
22 void addDateIntervalFormatTest(TestNode
** root
);
24 #define TESTCASE(x) addTest(root, &x, "tsformat/cdateintervalformattest/" #x)
26 void addDateIntervalFormatTest(TestNode
** root
)
28 TESTCASE(TestDateIntervalFormat
);
31 static const char tzUSPacific
[] = "US/Pacific";
32 static 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)
41 const char * skeleton
;
45 const char * resultExpected
;
46 } DateIntervalFormatTestItem
;
48 /* Just a small set of tests for now, the real functionality is tested in the C++ tests */
49 static const DateIntervalFormatTestItem testItems
[] = {
50 { "en", "MMMdHHmm", tzUSPacific
, Date201103021030
, Date201103021030
+ 7.0*_HOUR
, "Mar 2, 10:30 - 17:30" },
51 { "en", "MMMdHHmm", tzAsiaTokyo
, Date201103021030
, Date201103021030
+ 7.0*_HOUR
, "Mar 3, 03:30 - 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 - Thu, Oct 28, 2010" },
54 { "en", "yMMMEd", tzUSPacific
, Date201009270800
, Date201009270800
+ 410.0*_DAY
, "Mon, Sep 27, 2010 - 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
}
67 static void TestDateIntervalFormat()
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
;
76 UChar skelBuf
[kSkelBufLen
];
77 UChar tzidBuf
[kTZIDBufLen
];
78 const char * tzidForLog
= (testItemPtr
->tzid
)? testItemPtr
->tzid
: "NULL";
80 status
= U_ZERO_ERROR
;
81 u_unescape(testItemPtr
->skeleton
, skelBuf
, kSkelBufLen
);
82 if ( testItemPtr
->tzid
) {
83 u_unescape(testItemPtr
->tzid
, tzidBuf
, kTZIDBufLen
);
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;
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
) );
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
) );
108 udtitvfmt_close(udtitvfmt
);
110 log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n",
111 testItemPtr
->locale
, testItemPtr
->skeleton
, tzidForLog
, myErrorName(status
) );
114 ctest_resetTimeZone();
117 #endif /* #if !UCONFIG_NO_FORMATTING */