From b15fd37fa9a73f808f75edaf55f3126e2247dc4d Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Fri, 22 Jan 2016 21:37:44 +0000 Subject: [PATCH] ICU-12072 Flesh out tests for ureldatefmt_ APIs X-SVN-Rev: 38192 --- .../test/cintltst/crelativedateformattest.c | 222 +++++++++++++++--- 1 file changed, 191 insertions(+), 31 deletions(-) diff --git a/icu4c/source/test/cintltst/crelativedateformattest.c b/icu4c/source/test/cintltst/crelativedateformattest.c index f0ef247f57a..e3ca70d57cc 100644 --- a/icu4c/source/test/cintltst/crelativedateformattest.c +++ b/icu4c/source/test/cintltst/crelativedateformattest.c @@ -15,7 +15,8 @@ #include "cintltst.h" #include "cmemory.h" -static void TestRelDateFmtx(void); +static void TestRelDateFmt(void); +static void TestCombineDateTime(void); #define LEN(a) (sizeof(a)/sizeof(a[0])) @@ -25,25 +26,92 @@ void addRelativeDateFormatTest(TestNode** root); void addRelativeDateFormatTest(TestNode** root) { - TESTCASE(TestRelDateFmtx); + TESTCASE(TestRelDateFmt); + TESTCASE(TestCombineDateTime); } static const double offsets[] = { -5.0, -2.2, -1.0, -0.7, 0.0, 0.7, 1.0, 2.2, 5.0 }; enum { kNumOffsets = UPRV_LENGTHOF(offsets) }; -static const char* en_defNum_long_midSent_week[kNumOffsets*2] = { -/* text numeric */ - "5 weeks ago", "5 weeks ago", - "2.2 weeks ago", "2.2 weeks ago", - "last week", "1 week ago", - "last week", "0.7 weeks ago", - "this week", "in 0 weeks", - "next week", "in 0.7 weeks", - "next week", "in 1 week", - "in 2.2 weeks", "in 2.2 weeks", - "in 5 weeks", "in 5 weeks", +static const char* en_decDef_long_midSent_week[kNumOffsets*2] = { +/* text numeric */ + "5 weeks ago", "5 weeks ago", /* -5 */ + "2.2 weeks ago", "2.2 weeks ago", /* -2.2 */ + "last week", "1 week ago", /* -1 */ + "last week", "0.7 weeks ago", /* -0.7 */ + "this week", "in 0 weeks", /* 0 */ + "next week", "in 0.7 weeks", /* 0.7 */ + "next week", "in 1 week", /* 1 */ + "in 2.2 weeks", "in 2.2 weeks", /* 2.2 */ + "in 5 weeks", "in 5 weeks" /* 5 */ }; +static const char* en_dec0_long_midSent_week[kNumOffsets*2] = { +/* text numeric */ + "5 weeks ago", "5 weeks ago", /* -5 */ + "2 weeks ago", "2 weeks ago", /* -2.2 */ + "last week", "1 week ago", /* -1 */ + "last week", "0 weeks ago", /* -0.7 */ + "this week", "in 0 weeks", /* 0 */ + "next week", "in 0 weeks", /* 0.7 */ + "next week", "in 1 week", /* 1 */ + "in 2 weeks", "in 2 weeks", /* 2.2 */ + "in 5 weeks", "in 5 weeks" /* 5 */ +}; + +static const char* en_decDef_short_midSent_week[kNumOffsets*2] = { +/* text numeric */ + "5 wk. ago", "5 wk. ago", /* -5 */ + "2.2 wk. ago", "2.2 wk. ago", /* -2.2 */ + "last wk.", "1 wk. ago", /* -1 */ + "last wk.", "0.7 wk. ago", /* -0.7 */ + "this wk.", "in 0 wk.", /* 0 */ + "next wk.", "in 0.7 wk.", /* 0.7 */ + "next wk.", "in 1 wk.", /* 1 */ + "in 2.2 wk.", "in 2.2 wk.", /* 2.2 */ + "in 5 wk.", "in 5 wk." /* 5 */ +}; + +static const char* en_decDef_long_midSent_min[kNumOffsets*2] = { +/* text numeric */ + "5 minutes ago", "5 minutes ago", /* -5 */ + "2.2 minutes ago", "2.2 minutes ago", /* -2.2 */ + "1 minute ago", "1 minute ago", /* -1 */ + "0.7 minutes ago", "0.7 minutes ago", /* -0.7 */ + "now", "in 0 minutes", /* 0 */ + "in 0.7 minutes", "in 0.7 minutes", /* 0.7 */ + "in 1 minute", "in 1 minute", /* 1 */ + "in 2.2 minutes", "in 2.2 minutes", /* 2.2 */ + "in 5 minutes", "in 5 minutes" /* 5 */ +}; + +static const char* en_dec0_long_midSent_tues[kNumOffsets*2] = { +/* text numeric */ + ""/*no data */, ""/*no data */, /* -5 */ + ""/*no data */, ""/*no data */, /* -2.2 */ + "last Tuesday", ""/*no data */, /* -1 */ + "last Tuesday", ""/*no data */, /* -0.7 */ + "this Tuesday", ""/*no data */, /* 0 */ + "next Tuesday", ""/*no data */, /* 0.7 */ + "next Tuesday", ""/*no data */, /* 1 */ + ""/*no data */, ""/*no data */, /* 2.2 */ + ""/*no data */, ""/*no data */, /* 5 */ +}; + +static const char* fr_decDef_long_midSent_day[kNumOffsets*2] = { +/* text numeric */ + "il y a 5 jours", "il y a 5 jours", /* -5 */ + "avant-hier", "il y a 2,2 jours", /* -2.2 */ + "hier", "il y a 1 jour", /* -1 */ + "hier", "il y a 0,7 jour", /* -0.7 */ + "aujourd\\u2019hui", "dans 0 jour", /* 0 */ + "demain", "dans 0,7 jour", /* 0.7 */ + "demain", "dans 1 jour", /* 1 */ + "apr\\u00E8s-demain", "dans 2,2 jours", /* 2.2 */ + "dans 5 jours", "dans 5 jours" /* 5 */ +}; + + typedef struct { const char* locale; int32_t decPlaces; /* fixed decimal places; -1 to use default num formatter */ @@ -53,18 +121,23 @@ typedef struct { const char ** expectedResults; /* for the various offsets */ } RelDateTimeFormatTestItem; -static const RelDateTimeFormatTestItem testItems[] = { - { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK, en_defNum_long_midSent_week }, - { NULL, 0, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, (URelativeDateTimeUnit)0, NULL } /* terminator */ +static const RelDateTimeFormatTestItem fmtTestItems[] = { + { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK, en_decDef_long_midSent_week }, + { "en", 0, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK, en_dec0_long_midSent_week }, + { "en", -1, UDAT_STYLE_SHORT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEEK, en_decDef_short_midSent_week }, + { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_MINUTE, en_decDef_long_midSent_min }, + { "en", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_TUESDAY, en_dec0_long_midSent_tues }, + { "fr", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_DAY, fr_decDef_long_midSent_day }, + { NULL, 0, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, (URelativeDateTimeUnit)0, NULL } /* terminator */ }; -enum { kMaxUBuf = 64 }; +enum { kUBufMax = 64, kBBufMax = 256 }; -static void TestRelDateFmtx() +static void TestRelDateFmt() { const RelDateTimeFormatTestItem *itemPtr; log_verbose("\nTesting ureldatefmt_open(), ureldatefmt_format(), ureldatefmt_formatNumeric() with various parameters\n"); - for (itemPtr = testItems; itemPtr->locale != NULL; itemPtr++) { + for (itemPtr = fmtTestItems; itemPtr->locale != NULL; itemPtr++) { URelativeDateTimeFormatter *reldatefmt = NULL; UNumberFormat* nfToAdopt = NULL; UErrorCode status = U_ZERO_ERROR; @@ -74,6 +147,7 @@ static void TestRelDateFmtx() nfToAdopt = unum_open(UNUM_DECIMAL, NULL, 0, itemPtr->locale, NULL, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: unum_open(UNUM_DECIMAL, ...) for locale %s: %s\n", itemPtr->locale, myErrorName(status)); + continue; } unum_setAttribute(nfToAdopt, UNUM_MIN_FRACTION_DIGITS, itemPtr->decPlaces); unum_setAttribute(nfToAdopt, UNUM_MAX_FRACTION_DIGITS, itemPtr->decPlaces); @@ -81,31 +155,117 @@ static void TestRelDateFmtx() } reldatefmt = ureldatefmt_open(itemPtr->locale, nfToAdopt, itemPtr->width, itemPtr->capContext, &status); if ( U_FAILURE(status) ) { - log_data_err("FAIL: ureldatefmt_open() for locale %s: %s\n", itemPtr->locale, myErrorName(status)); + log_data_err("FAIL: ureldatefmt_open() for locale %s, decPlaces %d, width %d, capContext %d: %s\n", + itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, + myErrorName(status) ); + continue; } for (iOffset = 0; iOffset < kNumOffsets; iOffset++) { - UChar ubuf[kMaxUBuf]; - int32_t ulen; + UChar ubufget[kUBufMax]; + int32_t ulenget; + + if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY && (offsets[iOffset] < -1.0 || offsets[iOffset] > 1.0)) { + continue; /* we do not currently have data for this */ + } status = U_ZERO_ERROR; - ulen = ureldatefmt_format(reldatefmt, offsets[iOffset], itemPtr->unit, ubuf, kMaxUBuf, &status); - /* check results */ + ulenget = ureldatefmt_format(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n", + itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, + offsets[iOffset], (int)itemPtr->unit, myErrorName(status) ); + } else { + UChar ubufexp[kUBufMax]; + int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2], ubufexp, kUBufMax); + if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { + char bbufget[kBBufMax]; + u_austrncpy(bbufget, ubufget, kUBufMax); + log_err("ERROR: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n", + itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, + offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2], bbufget ); + } + } + + if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY) { + continue; /* we do not currently have numeric-style data for this */ + } status = U_ZERO_ERROR; - ulen = ureldatefmt_formatNumeric(reldatefmt, offsets[iOffset], itemPtr->unit, ubuf, kMaxUBuf, &status); - /* check results */ + ulenget = ureldatefmt_formatNumeric(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n", + itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, + offsets[iOffset], (int)itemPtr->unit, myErrorName(status) ); + } else { + UChar ubufexp[kUBufMax]; + int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2 + 1], ubufexp, kUBufMax); + if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { + char bbufget[kBBufMax]; + u_austrncpy(bbufget, ubufget, kUBufMax); + log_err("ERROR: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n", + itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, + offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2 + 1], bbufget ); + } + } } ureldatefmt_close(reldatefmt); } +} +typedef struct { + const char* locale; + UDateRelativeDateTimeFormatterStyle width; + UDisplayContext capContext; + const char * relativeDateString; + const char * timeString; + const char * expectedResult; +} CombineDateTimeTestItem; + +static const CombineDateTimeTestItem combTestItems[] = { + { "en", UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "yesterday", "3:45 PM", "yesterday, 3:45 PM" }, + { NULL, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, NULL, NULL, NULL } /* terminator */ +}; + +static void TestCombineDateTime() +{ + const CombineDateTimeTestItem *itemPtr; + log_verbose("\nTesting ureldatefmt_combineDateAndTime() with various parameters\n"); + for (itemPtr = combTestItems; itemPtr->locale != NULL; itemPtr++) { + URelativeDateTimeFormatter *reldatefmt = NULL; + UErrorCode status = U_ZERO_ERROR; + UChar ubufreldate[kUBufMax]; + UChar ubuftime[kUBufMax]; + UChar ubufget[kUBufMax]; + int32_t ulenreldate, ulentime, ulenget; -/* -log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: expect %s, get %s\n", -log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: %s\n", -log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n", -*/ + reldatefmt = ureldatefmt_open(itemPtr->locale, NULL, itemPtr->width, itemPtr->capContext, &status); + if ( U_FAILURE(status) ) { + log_data_err("FAIL: ureldatefmt_open() for locale %s, width %d, capContext %d: %s\n", + itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); + continue; + } + + ulenreldate = u_unescape(itemPtr->relativeDateString, ubufreldate, kUBufMax); + ulentime = u_unescape(itemPtr->timeString, ubuftime, kUBufMax); + ulenget = ureldatefmt_combineDateAndTime(reldatefmt, ubufreldate, ulenreldate, ubuftime, ulentime, ubufget, kUBufMax, &status); + if ( U_FAILURE(status) ) { + log_err("FAIL: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d: %s\n", + itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); + } else { + UChar ubufexp[kUBufMax]; + int32_t ulenexp = u_unescape(itemPtr->expectedResult, ubufexp, kUBufMax); + if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { + char bbufget[kBBufMax]; + u_austrncpy(bbufget, ubufget, kUBufMax); + log_err("ERROR: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d;\n expected %s\n get %s\n", + itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, itemPtr->expectedResult, bbufget ); + } + } + + ureldatefmt_close(reldatefmt); + } } #endif /* #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION */ -- 2.40.0