ICU-9110 Commit the C tests too

X-SVN-Rev: 31467
This commit is contained in:
Peter Edberg 2012-02-21 09:19:57 +00:00
parent ee88f60df4
commit 733a433be7
3 changed files with 154 additions and 5 deletions

View file

@ -21,6 +21,7 @@
#include "unicode/uloc.h"
#include "unicode/udat.h"
#include "unicode/udatpg.h"
#include "unicode/ucal.h"
#include "unicode/unum.h"
#include "unicode/ustring.h"
@ -34,6 +35,7 @@
static void TestExtremeDates(void);
static void TestAllLocales(void);
static void TestRelativeCrash(void);
static void TestContext(void);
#define LEN(a) (sizeof(a)/sizeof(a[0]))
@ -50,6 +52,7 @@ void addDateForTest(TestNode** root)
TESTCASE(TestExtremeDates);
TESTCASE(TestAllLocales);
TESTCASE(TestRelativeCrash);
TESTCASE(TestContext);
}
/* Testing the DateFormat API */
static void TestDateFormat()
@ -1233,4 +1236,87 @@ static void TestRelativeCrash(void) {
}
}
static const UChar skeleton_yMMMM[] = { 0x79,0x4D,0x4D,0x4D,0x4D,0 }; /* "yMMMM"; fr maps to "MMMM y", cs maps to "LLLL y" */
static const UChar july2008_frDefault[] = { 0x6A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "juillet 2008" */
static const UChar july2008_frTitle[] = { 0x4A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "Juillet 2008" sentence-begin, standalone */
static const UChar july2008_csDefault[] = { 0x10D,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "c(hacek)ervenec 2008" */
static const UChar july2008_csTitle[] = { 0x10C,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "C(hacek)ervenec 2008" sentence-begin, uiListOrMenu */
typedef struct {
const char * locale;
const UChar * skeleton;
UDateFormatContextValue capitalizationContext;
const UChar * expectedFormat;
} TestContextItem;
static const TestContextItem textContextItems[] = {
{ "fr", skeleton_yMMMM, UDAT_CAPITALIZATION_UNKNOWN, july2008_frDefault },
{ "fr", skeleton_yMMMM, UDAT_CAPITALIZATION_MIDDLE_OF_SENTENCE, july2008_frDefault },
{ "fr", skeleton_yMMMM, UDAT_CAPITALIZATION_BEGINNING_OF_SENTENCE, july2008_frTitle },
{ "fr", skeleton_yMMMM, UDAT_CAPITALIZATION_UI_LIST_OR_MENU, july2008_frDefault },
{ "fr", skeleton_yMMMM, UDAT_CAPITALIZATION_STANDALONE, july2008_frTitle },
{ "cs", skeleton_yMMMM, UDAT_CAPITALIZATION_UNKNOWN, july2008_csDefault },
{ "cs", skeleton_yMMMM, UDAT_CAPITALIZATION_MIDDLE_OF_SENTENCE, july2008_csDefault },
{ "cs", skeleton_yMMMM, UDAT_CAPITALIZATION_BEGINNING_OF_SENTENCE, july2008_csTitle },
{ "cs", skeleton_yMMMM, UDAT_CAPITALIZATION_UI_LIST_OR_MENU, july2008_csTitle },
{ "cs", skeleton_yMMMM, UDAT_CAPITALIZATION_STANDALONE, july2008_csDefault },
{ NULL, NULL, 0, NULL }
};
static const UDate july022008 = 1215000001979.0;
enum { kUbufMax = 64, kBbufMax = 3*kUbufMax };
static void TestContext(void) {
const TestContextItem* textContextItemPtr = textContextItems;
for (; textContextItemPtr->locale != NULL; ++textContextItemPtr) {
UErrorCode status = U_ZERO_ERROR;
UDateFormat* udfmt = udat_open(UDAT_NONE, UDAT_MEDIUM, textContextItemPtr->locale, NULL, 0, NULL, 0, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
} else {
UDateTimePatternGenerator* udtpg = udatpg_open(textContextItemPtr->locale, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udatpg_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
} else {
UChar ubuf[kUbufMax];
int32_t len = udatpg_getBestPattern(udtpg, textContextItemPtr->skeleton, -1, ubuf, kUbufMax, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udatpg_getBestPattern for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) );
} else {
udat_applyPattern(udfmt, FALSE, ubuf, len);
udat_setDefaultContext(udfmt, UDAT_CAPITALIZATION, textContextItemPtr->capitalizationContext, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_setDefaultContext for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
} else {
int32_t getContext;
len = udat_format(udfmt, july022008, ubuf, kUbufMax, NULL, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_format for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
status = U_ZERO_ERROR;
} else if (u_strncmp(ubuf, textContextItemPtr->expectedFormat, kUbufMax) != 0) {
char bbuf1[kBbufMax];
char bbuf2[kBbufMax];
log_err("FAIL: udat_format for locale %s, capitalizationContext %d, expected %s, got %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext,
u_austrncpy(bbuf1,textContextItemPtr->expectedFormat,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) );
}
getContext = udat_getDefaultContext(udfmt, UDAT_CAPITALIZATION, &status);
if ( U_FAILURE(status) ) {
log_err("FAIL: udat_getDefaultContext for locale %s, capitalizationContext %d, status %s\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) );
} else if (getContext != (int)textContextItemPtr->capitalizationContext) {
log_err("FAIL: udat_getDefaultContext for locale %s, capitalizationContext %d, got context %d\n",
textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, getContext );
}
}
}
udatpg_close(udtpg);
}
udat_close(udfmt);
}
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View file

@ -85,9 +85,10 @@ void DateFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &nam
TESTCASE(45,TestStandAloneGMTParse);
TESTCASE(46,TestParsePosition);
TESTCASE(47,TestMonthPatterns);
TESTCASE(48,TestContext);
/*
TESTCASE(48,TestRelativeError);
TESTCASE(49,TestRelativeOther);
TESTCASE(49,TestRelativeError);
TESTCASE(50,TestRelativeOther);
*/
default: name = ""; break;
}
@ -3755,8 +3756,8 @@ void DateFormatTest::TestMonthPatterns()
CharsToUnicodeString("\\u58EC\\u8FB0\\u5E74\\u958F\\u56DB\\u6708\\u4E8C\\u65E5"),
CharsToUnicodeString("\\u58EC\\u8FB0\\u5E74\\u4E94\\u6708\\u4E8C\\u65E5") } },
{ "zh_Hant@calendar=chinese", DateFormat::kShort, { CharsToUnicodeString("\\u58EC\\u8FB0/4/2"),
CharsToUnicodeString("\\u58EC\\u8FB0/\\u958F4/2"),
CharsToUnicodeString("\\u58EC\\u8FB0/5/2") } },
CharsToUnicodeString("\\u58EC\\u8FB0/\\u958F4/2"),
CharsToUnicodeString("\\u58EC\\u8FB0/5/2") } },
{ "fr@calendar=chinese", DateFormat::kLong, { CharsToUnicodeString("2 s\\u00ECyu\\u00E8 ren-chen"),
CharsToUnicodeString("2 s\\u00ECyu\\u00E8bis ren-chen"),
CharsToUnicodeString("2 w\\u01D4yu\\u00E8 ren-chen") } },
@ -3828,6 +3829,66 @@ void DateFormatTest::TestMonthPatterns()
}
}
typedef struct {
const char * locale;
UnicodeString pattern;
UDateFormatContextValue capitalizationContext;
UnicodeString expectedFormat;
} TestContextItem;
void DateFormatTest::TestContext()
{
const UDate july022008 = 1215000001979.0;
const TestContextItem items[] = {
//locale pattern capitalizationContext expected formatted date
{ "fr", UnicodeString("MMMM y"), UDAT_CAPITALIZATION_UNKNOWN, UnicodeString("juillet 2008") },
{ "fr", UnicodeString("MMMM y"), UDAT_CAPITALIZATION_MIDDLE_OF_SENTENCE, UnicodeString("juillet 2008") },
{ "fr", UnicodeString("MMMM y"), UDAT_CAPITALIZATION_BEGINNING_OF_SENTENCE, UnicodeString("Juillet 2008") },
{ "fr", UnicodeString("MMMM y"), UDAT_CAPITALIZATION_UI_LIST_OR_MENU, UnicodeString("juillet 2008") },
{ "fr", UnicodeString("MMMM y"), UDAT_CAPITALIZATION_STANDALONE, UnicodeString("Juillet 2008") },
{ "cs", UnicodeString("LLLL y"), UDAT_CAPITALIZATION_UNKNOWN, CharsToUnicodeString("\\u010Dervenec 2008") },
{ "cs", UnicodeString("LLLL y"), UDAT_CAPITALIZATION_MIDDLE_OF_SENTENCE, CharsToUnicodeString("\\u010Dervenec 2008") },
{ "cs", UnicodeString("LLLL y"), UDAT_CAPITALIZATION_BEGINNING_OF_SENTENCE, CharsToUnicodeString("\\u010Cervenec 2008") },
{ "cs", UnicodeString("LLLL y"), UDAT_CAPITALIZATION_UI_LIST_OR_MENU, CharsToUnicodeString("\\u010Cervenec 2008") },
{ "cs", UnicodeString("LLLL y"), UDAT_CAPITALIZATION_STANDALONE, CharsToUnicodeString("\\u010Dervenec 2008") },
// terminator
{ NULL, UnicodeString(""), (UDateFormatContextValue)0, UnicodeString("") }
};
UErrorCode status = U_ZERO_ERROR;
Calendar* cal = Calendar::createInstance(status);
if (U_FAILURE(status)) {
dataerrln(UnicodeString("FAIL: Unable to create Calendar for default timezone and locale."));
} else {
cal->setTime(july022008, status);
const TestContextItem * itemPtr;
for (itemPtr = items; itemPtr->locale != NULL; itemPtr++ ) {
Locale locale = Locale::createFromName(itemPtr->locale);
status = U_ZERO_ERROR;
SimpleDateFormat * sdmft = new SimpleDateFormat(itemPtr->pattern, locale, status);
if (U_FAILURE(status)) {
dataerrln(UnicodeString("FAIL: Unable to create SimpleDateFormat for specified pattern with locale ") + UnicodeString(itemPtr->locale));
} else {
UDateFormatContextType contextType = UDAT_CAPITALIZATION;
UDateFormatContextValue contextValue = itemPtr->capitalizationContext;
UnicodeString result;
FieldPosition pos(0);
sdmft->format(*cal, &contextType, &contextValue, 1, result, pos);
if (result.compare(itemPtr->expectedFormat) != 0) {
errln(UnicodeString("FAIL: format for locale ") + UnicodeString(itemPtr->locale) +
", capitalizationContext " + (int)itemPtr->capitalizationContext +
", expected " + itemPtr->expectedFormat + ", got " + result);
}
}
if (sdmft) {
delete sdmft;
}
}
}
if (cal) {
delete cal;
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */
//eof

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2011, International Business Machines Corporation and
* Copyright (c) 1997-2012, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -177,6 +177,8 @@ public: // package
void TestMonthPatterns(void);
void TestContext(void);
public:
/**
* Test host-specific formatting.