Skip to content

Commit

Permalink
ICU-22668 Added unit tests to verify that CLDR-17892 addresses the or…
Browse files Browse the repository at this point in the history
…iginal problem reported here and took out an

earlier hack in the Java code to work around the original problem.
  • Loading branch information
richgillam committed Sep 20, 2024
1 parent 1c2a1c5 commit 9adcf8b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
2 changes: 2 additions & 0 deletions icu4c/source/test/cintltst/crelativedateformattest.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ static const RelDateTimeFormatTestItem fmtTestItems[] = {
ak_decDef_long_stdAlon_sec, ak_attrDef_long_stdAlon_sec},
{ "en_IN", -1, UDAT_STYLE_SHORT, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_WEDNESDAY,
enIN_decDef_short_midSent_weds, enIN_attrDef_short_midSent_weds},
{ "en@calendar=iso8601", -1, UDAT_STYLE_LONG, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDAT_REL_UNIT_SECOND,
en_decDef_long_midSent_sec, en_attrDef_long_midSent_sec },
{ NULL, 0, (UDateRelativeDateTimeFormatterStyle)0, (UDisplayContext)0, (URelativeDateTimeUnit)0, NULL, NULL } /* terminator */
};

Expand Down
46 changes: 46 additions & 0 deletions icu4c/source/test/cintltst/udatpg_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#if !UCONFIG_NO_FORMATTING

#include <stdbool.h>
#include <stdio.h> // for sprintf()

#include "unicode/udat.h"
#include "unicode/udatpg.h"
Expand All @@ -51,7 +52,9 @@ static void TestGetDefaultHourCycleOnEmptyInstance(void);
static void TestEras(void);
static void TestDateTimePatterns(void);
static void TestRegionOverride(void);
static void TestISO8601(void);


void addDateTimePatternGeneratorTest(TestNode** root) {
TESTCASE(TestOpenClose);
TESTCASE(TestUsage);
Expand All @@ -63,6 +66,7 @@ void addDateTimePatternGeneratorTest(TestNode** root) {
TESTCASE(TestEras);
TESTCASE(TestDateTimePatterns);
TESTCASE(TestRegionOverride);
TESTCASE(TestISO8601);
}

/*
Expand Down Expand Up @@ -836,4 +840,46 @@ static void TestRegionOverride(void) {
udatpg_close(dtpg);
}
}

static void TestISO8601(void) {
typedef struct TestCase {
const char* locale;
const UChar* skeleton;
const UChar* expectedPattern;
} TestCase;

const TestCase testCases[] = {
{ "en_GB@calendar=iso8601;rg=uszzzz", u"EEEEyMMMMdjmm", u"y MMMM d, EEEE 'at' h:mm a" },
{ "en_GB@calendar=iso8601;rg=uszzzz", u"EEEEyMMMMdHmm", u"y MMMM d, EEEE 'at' HH:mm" },
{ "en_GB@calendar=iso8601;rg=uszzzz", u"Edjmm", u"d, EEE, h:mm a" },
{ "en_GB@calendar=iso8601;rg=uszzzz", u"EdHmm", u"d, EEE, HH:mm" },

{ "en_US@calendar=iso8601", u"EEEEyMMMMdjmm", u"y MMMM d, EEEE 'at' h:mm a" },
{ "en_US@calendar=iso8601", u"EEEEyMMMMdHmm", u"y MMMM d, EEEE 'at' HH:mm" },
{ "en_US@calendar=iso8601", u"Edjmm", u"d, EEE, h:mm a" },
{ "en_US@calendar=iso8601", u"EdHmm", u"d, EEE, HH:mm" },

{ "en_US", u"EEEEyMMMMdjmm", u"EEEE, MMMM d, y 'at' h:mm a" },
{ "en_US", u"EEEEyMMMMdHmm", u"EEEE, MMMM d, y 'at' HH:mm" },
{ "en_US", u"Edjmm", u"d EEE, h:mm a" },
{ "en_US", u"EdHmm", u"d EEE, HH:mm" },
};

for (int32_t i = 0; i < UPRV_LENGTHOF(testCases); i++) {
UErrorCode err = U_ZERO_ERROR;
UDateTimePatternGenerator* dtpg = udatpg_open(testCases[i].locale, &err);

if (assertSuccess("Error creating dtpg", &err)) {
UChar actualPattern[200];

udatpg_getBestPatternWithOptions(dtpg, testCases[i].skeleton, -1, 0, actualPattern, UPRV_LENGTHOF(actualPattern), &err);
if (assertSuccess("Error getting best pattern", &err)) {
char errorMessage[200];
snprintf(errorMessage, UPRV_LENGTHOF(errorMessage), "Wrong pattern for %s and %s", testCases[i].locale, austrdup(testCases[i].skeleton));
assertUEquals(errorMessage, testCases[i].expectedPattern, actualPattern);
}
}
udatpg_close(dtpg);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,6 @@ private void addICUPatterns(PatternInfo returnInfo, ULocale uLocale) {
// first load with the ICU patterns
ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, uLocale);
String calendarTypeToUse = getCalendarTypeToUse(uLocale);
// TODO: this is a HACK to work around ICU-22668/CLDR-17892! Remove once they're fixed!
// (Without this hack, DateTimeGeneratorTest.testISO8601() fails. The corresponding test
// on the C++ side still _passes_, so I have NOT added this hack on the C++ side. --rtg 8/21/24)
if (calendarTypeToUse.equals("iso8601")) {
calendarTypeToUse = "gregorian";
}
// TODO: See ICU-22867
ICUResourceBundle dateTimePatterns = rb.getWithFallback("calendar/" + calendarTypeToUse + "/DateTimePatterns");
if (dateTimePatterns.getType() != UResourceBundle.ARRAY || dateTimePatterns.getSize() < 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2066,4 +2066,38 @@ public void testRegionOverride() {
assertEquals("Wrong pattern", expectedPattern, actualPattern);
}
}

@Test
public void testISO8601More() {
final String[][] testCases = {
{ "en_GB@calendar=iso8601;rg=uszzzz", "EEEEyMMMMdjmm", "y MMMM d, EEEE 'at' h:mm a" },
{ "en_GB@calendar=iso8601;rg=uszzzz", "EEEEyMMMMdHmm", "y MMMM d, EEEE 'at' HH:mm" },
{ "en_GB@calendar=iso8601;rg=uszzzz", "Edjmm", "d, EEE, h:mm a" },
{ "en_GB@calendar=iso8601;rg=uszzzz", "EdHmm", "d, EEE, HH:mm" },

{ "en_US@calendar=iso8601", "EEEEyMMMMdjmm", "y MMMM d, EEEE 'at' h:mm a" },
{ "en_US@calendar=iso8601", "EEEEyMMMMdHmm", "y MMMM d, EEEE 'at' HH:mm" },
{ "en_US@calendar=iso8601", "Edjmm", "d, EEE, h:mm a" },
{ "en_US@calendar=iso8601", "EdHmm", "d, EEE, HH:mm" },

{ "en_US", "EEEEyMMMMdjmm", "EEEE, MMMM d, y 'at' h:mm a" },
{ "en_US", "EEEEyMMMMdHmm", "EEEE, MMMM d, y 'at' HH:mm" },
{ "en_US", "Edjmm", "d EEE, h:mm a" },
{ "en_US", "EdHmm", "d EEE, HH:mm" },
};

for (String[] testCase : testCases) {
String localeID = testCase[0];
String skeleton = testCase[1];
String expectedPattern = testCase[2];

DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(new ULocale(localeID));

String actualPattern = dtpg.getBestPattern(skeleton);
assertEquals("Wrong pattern for " + localeID + " and " + skeleton, expectedPattern, actualPattern);
// if (!expectedPattern.equals(actualPattern)) {
// System.out.println("Wrong pattern for " + localeID + " and " + skeleton + ": expected \"" + expectedPattern + "\", got \'" + actualPattern + "\"");
// }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,9 @@ public TestRelativeDateTimeUnitItem(String locID, int decP, RelativeDateTimeForm
RelativeDateTimeUnit.FRIDAY, enIN_decDef_short_midSent_friday),
new TestRelativeDateTimeUnitItem("en_IN", -1, Style.SHORT, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,
RelativeDateTimeUnit.SATURDAY, enIN_decDef_short_midSent_saturday),

new TestRelativeDateTimeUnitItem("en@calendar=iso8601", -1, Style.LONG, DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,
RelativeDateTimeUnit.SECOND, en_decDef_long_midSent_sec),
};
for (TestRelativeDateTimeUnitItem item: items) {
ULocale uloc = new ULocale(item.localeID);
Expand Down

0 comments on commit 9adcf8b

Please sign in to comment.