ICU-20926 Add tests for identity behavior in DateIntervalFormat

See #2245
This commit is contained in:
Shane F. Carr 2023-03-19 08:42:03 +00:00 committed by Frank Yung-Fong Tang
parent 1e60df0f82
commit 2a2995951b
3 changed files with 160 additions and 1 deletions

View file

@ -66,6 +66,8 @@ void DateIntervalFormatTest::runIndexedTest( int32_t index, UBool exec, const ch
TESTCASE_AUTO(testTicket21222ROCEraDiff);
TESTCASE_AUTO(testTicket21222JapaneseEraDiff);
TESTCASE_AUTO(testTicket21939);
TESTCASE_AUTO(testTicket20710_FieldIdentity);
TESTCASE_AUTO(testTicket20710_IntervalIdentity);
TESTCASE_AUTO_END;
}
@ -2385,4 +2387,79 @@ void DateIntervalFormatTest::testTicket21939() {
}
}
void DateIntervalFormatTest::testTicket20710_FieldIdentity() {
IcuTestErrorCode status(*this, "testTicket20710_FieldIdentity");
LocalPointer<DateIntervalFormat> dtifmt(DateIntervalFormat::createInstance("eeeeMMMddyyhhmma", "de-CH", status));
LocalPointer<Calendar> calendar1(Calendar::createInstance(TimeZone::createTimeZone(u"CET"), status));
calendar1->setTime(UDate(1563235200000), status);
LocalPointer<Calendar> calendar2(Calendar::createInstance(TimeZone::createTimeZone(u"CET"), status));
calendar2->setTime(UDate(1564235200000), status);
{
auto fv = dtifmt->formatToValue(*calendar1, *calendar2, status);
ConstrainedFieldPosition cfp;
cfp.constrainCategory(UFIELD_CATEGORY_DATE_INTERVAL_SPAN);
assertTrue("Span field should be in non-identity formats", fv.nextPosition(cfp, status));
}
{
auto fv = dtifmt->formatToValue(*calendar1, *calendar1, status);
ConstrainedFieldPosition cfp;
cfp.constrainCategory(UFIELD_CATEGORY_DATE_INTERVAL_SPAN);
assertFalse("Span field should not be in identity formats", fv.nextPosition(cfp, status));
}
}
void DateIntervalFormatTest::testTicket20710_IntervalIdentity() {
IcuTestErrorCode status(*this, "testTicket20710_IntervalIdentity");
const char16_t timeZone[] = u"PST";
int32_t count = 0;
const Locale* locales = Locale::getAvailableLocales(count);
const UnicodeString skeletons[] = {
u"EEEEMMMMdyhmmssazzzz",
u"EEEEMMMMdyhhmmssazzzz",
u"EEEEMMMMddyyyyhhmmssvvvva",
u"EEEEMMMMddhmszza",
u"EEMMMMddyyhhzza",
u"eeeeMMMddyyhhmma",
u"MMddyyyyhhmmazzzz",
u"hhmmazzzz",
u"hmmssazzzz",
u"hhmmsszzzz",
u"MMddyyyyhhmmzzzz"
};
Locale quickLocales[] = {
{"en"}, {"es"}, {"sr"}, {"zh"}
};
if (quick) {
locales = quickLocales;
count = UPRV_LENGTHOF(quickLocales);
}
for (int32_t i = 0; i < count; i++) {
const Locale locale = locales[i];
LocalPointer<DateTimePatternGenerator> gen(DateTimePatternGenerator::createInstance(locale, status));
LocalPointer<Calendar> calendar(Calendar::createInstance(TimeZone::createTimeZone(timeZone), status));
calendar->setTime(UDate(1563235200000), status);
for (auto skeleton : skeletons) {
LocalPointer<DateIntervalFormat> dtifmt(DateIntervalFormat::createInstance(skeleton, locale, status));
FieldPosition fposition;
UnicodeString resultIntervalFormat;
dtifmt->format(*calendar, *calendar, resultIntervalFormat, fposition, status);
UnicodeString pattern = gen->getBestPattern(skeleton, status);
SimpleDateFormat dateFormat(pattern, locale, status);
FieldPositionIterator fpositer;
UnicodeString resultDateFormat;
dateFormat.format(*calendar, resultDateFormat, &fpositer, status);
assertEquals("DateIntervalFormat should fall back to DateFormat in the identity format", resultDateFormat, resultIntervalFormat);
}
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View file

@ -90,6 +90,8 @@ public:
void testTicket21222JapaneseEraDiff();
void testTicket21939();
void testTicket20710_FieldIdentity();
void testTicket20710_IntervalIdentity();
private:
/**

View file

@ -30,12 +30,14 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.impl.DontCareFieldPosition;
import com.ibm.icu.impl.Utility;
import com.ibm.icu.text.ConstrainedFieldPosition;
import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.DateIntervalFormat;
import com.ibm.icu.text.DateIntervalFormat.FormattedDateInterval;
import com.ibm.icu.text.DateIntervalInfo;
import com.ibm.icu.text.DateTimePatternGenerator;
import com.ibm.icu.text.DateIntervalInfo.PatternInfo;
import com.ibm.icu.text.DisplayContext;
import com.ibm.icu.text.SimpleDateFormat;
@ -2481,7 +2483,7 @@ public class DateIntervalFormatTest extends TestFmwk {
formatted.toString());
verifyFields(formatted, expectedFields);
}
@Test
public void testTicket21939() {
// the test here is just to check that this particular skeleton doesn't
@ -2491,4 +2493,82 @@ public class DateIntervalFormatTest extends TestFmwk {
SimpleDateFormat sdf = (SimpleDateFormat)df;
assertEquals("Wrong date format", "M/d/r, h:mm\u202Fa", sdf.toPattern());
}
@Test
public void testTicket20710_FieldIdentity() {
DateIntervalFormat dtifmt = DateIntervalFormat.getInstance("eeeeMMMddyyhhmma", new ULocale("de-CH"));
Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("CET"));
calendar1.setTimeInMillis(1563235200000l);
Calendar calendar2 = Calendar.getInstance(TimeZone.getTimeZone("CET"));
calendar2.setTimeInMillis(1564235200000l);
{
FormattedDateInterval fv = dtifmt.formatToValue(calendar1, calendar2);
ConstrainedFieldPosition cfp = new ConstrainedFieldPosition();
cfp.constrainClass(DateIntervalFormat.SpanField.class);
assertTrue("Span field should be in non-identity formats", fv.nextPosition(cfp));
}
{
FormattedDateInterval fv = dtifmt.formatToValue(calendar1, calendar1);
ConstrainedFieldPosition cfp = new ConstrainedFieldPosition();
cfp.constrainClass(DateIntervalFormat.SpanField.class);
assertFalse("Span field should not be in identity formats", fv.nextPosition(cfp));
}
}
@Test
public void testTicket20710_IntervalIdentity() {
boolean quick = getExhaustiveness() <= 5;
String timeZone = "PST";
ULocale[] locales = ULocale.getAvailableLocales();
String[] skeletons = {
"EEEEMMMMdyhmmssazzzz",
"EEEEMMMMdyhhmmssazzzz",
"EEEEMMMMddyyyyhhmmssvvvva",
"EEEEMMMMddhmszza",
"EEMMMMddyyhhzza",
"eeeeMMMddyyhhmma",
"MMddyyyyhhmmazzzz",
"hhmmazzzz",
"hmmssazzzz",
"hhmmsszzzz",
"MMddyyyyhhmmzzzz"
};
ULocale[] quickLocales = {
new ULocale("en"),
new ULocale("es"),
new ULocale("sr"),
new ULocale("zh"),
};
if (quick) {
locales = quickLocales;
}
for (int i = 0; i < locales.length; i++) {
ULocale locale = locales[i];
DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
calendar.setTimeInMillis(1563235200000l);
for (String skeleton : skeletons) {
DateIntervalFormat dtifmt = DateIntervalFormat.getInstance(skeleton, locale);
FieldPosition pos = DontCareFieldPosition.INSTANCE;
StringBuffer resultIntervalFormat = new StringBuffer();
dtifmt.format(calendar, calendar, resultIntervalFormat, pos);
String pattern = gen.getBestPattern(skeleton);
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, locale);
StringBuffer resultDateFormat = new StringBuffer();
dateFormat.format(calendar, resultDateFormat, pos);
assertEquals(
"DateIntervalFormat should fall back to DateFormat in the identity format",
resultDateFormat.toString(),
resultIntervalFormat.toString()
);
}
}
}
}