ICU-5787 1. Added test cases. 2. code clean up.

X-SVN-Rev: 22295
This commit is contained in:
Claire Ho 2007-08-07 03:14:19 +00:00
parent 3d687c5e02
commit 887eafb2c8
5 changed files with 226 additions and 262 deletions

View file

@ -10,7 +10,6 @@
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/datefmt.h"
@ -115,16 +114,13 @@ static const char* CLDR_FIELD_NAME[] = {
"hour", "minute", "second", "*", "zone"
};
// TODO: add 'quarter' and MAX_RESOURCE_FIELD field after CLDR fix the bug
static const char* Resource_Fields[] = {
"day", "dayperiod", "era", "hour", "minute", "month", "second", "week",
"weekday", "year", "zone" };
static const char* CLDR_AVAILABLE_FORMAT[MAX_AVAILABLE_FORMATS] = {
"Ed", "EMMMd", "H", "HHmm", "HHmmss", "Md", "MMMMd", "MMyy", "Qyy",
"mmss", "yyMMM", "yyyy",
}; // binary ascending order
static const UnicodeString quotingPattern= UNICODE_STRING_SIMPLE("[[[:script=Latin:][:script=Cyrl:]]&[[:L:][:M:]]]");
// For appendItems
static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
@ -180,7 +176,6 @@ DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
fStatus = U_ZERO_ERROR;
pLocale = other.pLocale;
*fp = *(other.fp);
fp->setFilter(fStatus);
dtMatcher->copyFrom(other.dtMatcher->skeleton);
*distanceInfo = *(other.distanceInfo);
dateTimeFormat = other.dateTimeFormat;
@ -208,7 +203,19 @@ DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) cons
if (this == &other) {
return TRUE;
}
return (UBool)((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)));
if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
(dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
if ((appendItemFormats[i] != other.appendItemFormats[i]) ||
(appendItemNames[i] != other.appendItemNames[i]) ) {
return FALSE;
}
}
return TRUE;
}
else {
return FALSE;
}
}
DateTimePatternGenerator::~DateTimePatternGenerator() {
@ -231,50 +238,49 @@ DateTimePatternGenerator::initData(const Locale& locale) {
fAvailableFormatKeyHash=NULL;
addCanonicalItems();
addICUPatterns(locale);
addICUPatterns(locale, fStatus);
if (U_FAILURE(fStatus)) {
return;
}
addCLDRData(locale);
setDateTimeFromCalendar(locale);
setDateTimeFromCalendar(locale, fStatus);
setDecimalSymbols(locale, fStatus);
} // DateTimePatternGenerator::initData
UnicodeString
DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode& status) {
PtnSkeleton *ptrSkeleton;
UnicodeString result;
dtMatcher->set(pattern, fp);
ptrSkeleton=dtMatcher->getSkeletonPtr();
result = ptrSkeleton->getSkeleton();
return result;
return dtMatcher->getSkeletonPtr()->getSkeleton();
}
UnicodeString
DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& status) {
PtnSkeleton *ptrSkeleton;
UnicodeString result;
dtMatcher->set(pattern, fp);
ptrSkeleton=dtMatcher->getSkeletonPtr();
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
if (ptrSkeleton->baseOriginal[i].length()!=0) {
result += ptrSkeleton->baseOriginal[i];
}
}
return result;
return dtMatcher->getSkeletonPtr()->getBaseSkeleton();
}
void
DateTimePatternGenerator::addICUPatterns(const Locale& locale) {
DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
UnicodeString dfPattern;
UnicodeString conflictingString;
UDateTimePatternConflict conflictingStatus;
SimpleDateFormat* df;
UErrorCode status = U_ZERO_ERROR;
// Load with ICU patterns
for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
if ((df = (SimpleDateFormat*)DateFormat::createDateInstance((DateFormat::EStyle)i, locale))!= NULL) {
conflictingStatus = addPattern(df->toPattern(dfPattern), FALSE, conflictingString, status);
if (U_FAILURE(status)) {
delete df;
return;
}
// TODO remove the printf after picking up CLDR 1.5 data
/*
printf("\n ICU Date format:");
for (int32_t j=0; j < newPattern.length(); ++j) {
printf("%c", newPattern.charAt(j));
}
*/
delete df;
}
@ -350,10 +356,7 @@ DateTimePatternGenerator::addCLDRData(const Locale& locale) {
UDateTimePatternConflict conflictingStatus;
const char *key=NULL;
// Initialize appendItems
static const UChar itemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
UnicodeString defaultItemFormat(TRUE, itemFormat, LENGTHOF(itemFormat)-1); // Read-only alias.
UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
appendItemNames[i]=CAP_F;
@ -439,6 +442,12 @@ DateTimePatternGenerator::addCLDRData(const Locale& locale) {
UnicodeString retKey=UnicodeString(key, -1, US_INV);
setAvailableFormat(retKey, err);
conflictingStatus = addPattern(format, FALSE, conflictingPattern, err);
// TODO remove the printf after picking up CLDR 1.5 data
/*
printf("\n Available format=>");
for (int32_t j=0; j<format.length(); ++j)
printf("%c", format.charAt(j));
*/
}
}
ures_close(patBundle);
@ -470,6 +479,12 @@ DateTimePatternGenerator::addCLDRData(const Locale& locale) {
if ( !isAvailableFormatSet(retKey) ) {
setAvailableFormat(retKey, err);
conflictingStatus = addPattern(format, FALSE, conflictingPattern, err);
// TODO remove the printf after picking up CLDR 1.5 data
/*
printf("\n Available format=>");
for (int32_t j=0; j<format.length(); ++j)
printf("%c", format.charAt(j));
*/
}
}
}
@ -555,13 +570,11 @@ DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErro
resultPattern.remove();
}
else {
resultPattern=timePattern;
return timePattern;
}
return resultPattern;
}
if (timePattern.length()==0) {
resultPattern=datePattern;
return resultPattern;
return datePattern;
}
resultPattern.remove();
dtFormat=getDateTimeFormat();
@ -615,14 +628,14 @@ DateTimePatternGenerator::getDateTimeFormat() const {
}
void
DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale) {
UErrorCode err=U_ZERO_ERROR;
DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& err) {
UResourceBundle *patBundle;
UnicodeString rbPattern;
const char *key=NULL;
// Set the datetime pattern
CalendarData calData(locale, NULL, err );
if (U_FAILURE(err)) return;
// load the first data item
patBundle = calData.getByKey("DateTimePatterns", err);
@ -749,13 +762,14 @@ DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
newPattern += quoteLiteral;
}
else {
if (fp->isPatternSeparator(field)) {
newPattern+=field;
continue;
}
int32_t canonicalIndex = fp->getCanonicalIndex(field);
if (canonicalIndex < 0) {
if (fp->isPatternSeparator(field)) {
newPattern+=field;
}
continue; //don't adjust
newPattern+=field;
continue; // don't adjust
}
const dtTypeElem *row = &dtTypes[canonicalIndex];
int32_t typeValue = row->field;
@ -892,27 +906,12 @@ DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
const UnicodeString&
DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
UChar baseChar;
PtnElem *curElem;
if (skeleton.length() ==0) {
return emptyString;
}
baseChar = skeleton.charAt(0);
// the baseChar must be A-Z or a-z
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
curElem = patternMap->boot[26 + (baseChar-CAP_A)];
}
else {
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
curElem = patternMap->boot[baseChar-LOW_A];
}
else {
return emptyString;
}
}
curElem = patternMap->getHeader(skeleton.charAt(0));
while ( curElem != NULL ) {
if ( curElem->skeleton->getSkeleton()==skeleton ) {
return curElem->pattern;
@ -1009,6 +1008,23 @@ PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
}
}
PtnElem*
PatternMap::getHeader(UChar baseChar) {
PtnElem* curElem;
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
curElem = boot[baseChar-CAP_A];
}
else {
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
curElem = boot[26+baseChar-LOW_A];
}
else {
return NULL;
}
}
}
PatternMap::~PatternMap() {
for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
if (boot[i]!=NULL ) {
@ -1071,7 +1087,6 @@ PatternMap::add(const UnicodeString& basePattern,
}
if ( curElem != NULL ) {
curElem->skeleton = new PtnSkeleton(skeleton);
curElem->next = NULL;
}
}
@ -1094,12 +1109,7 @@ PatternMap::add(const UnicodeString& basePattern,
}
curElem=curElem->next;
}
PtnSkeleton* pNewSkeleton = curElem->skeleton = new PtnSkeleton;
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
pNewSkeleton->type[i] = skeleton.type[i];
pNewSkeleton->original[i] = skeleton.original[i];
pNewSkeleton->baseOriginal[i] = skeleton.baseOriginal[i];
}
curElem->skeleton = new PtnSkeleton(skeleton);
curElem->next = NULL;
}
else {
@ -1116,22 +1126,9 @@ PatternMap::add(const UnicodeString& basePattern,
// Find the pattern from the given basePattern string.
const UnicodeString *
PatternMap::getPatternFromBasePattern(UnicodeString& basePattern) { // key to search for
UChar baseChar = basePattern.charAt(0);
PtnElem *curElem;
// the baseChar must be A-Z or a-z
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
curElem = boot[26 + (baseChar-CAP_A)];
}
else {
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
curElem = boot[baseChar-LOW_A];
}
else
return NULL;
}
if ( curElem == NULL ) {
if ((curElem=getHeader(basePattern.charAt(0)))==NULL) {
return NULL; // no match
}
@ -1159,20 +1156,8 @@ PatternMap::getPatternFromSkeleton(PtnSkeleton& skeleton) { // key to search for
break;
}
}
// the baseChar must be A-Z or a-z
if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
curElem = boot[baseChar-CAP_A];
}
else {
if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
curElem = boot[26+baseChar-LOW_A];
}
else
return NULL;
}
if ( curElem == NULL ) {
if ((curElem=getHeader(baseChar))==NULL) {
return NULL; // no match
}
@ -1316,11 +1301,7 @@ DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton
}
{
Mutex mutex;
for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
this->skeleton.type[i] = skeleton.type[i];
this->skeleton.baseOriginal[i] = skeleton.baseOriginal[i];
this->skeleton.original[i] = skeleton.original[i];
}
copyFrom(skeleton);
}
return;
@ -1431,12 +1412,10 @@ DateTimeMatcher::getSkeletonPtr() {
FormatParser::FormatParser () {
status = START;
itemNumber=0;
quoteFilter = NULL;
}
FormatParser::~FormatParser () {
delete quoteFilter;
}
@ -1513,8 +1492,7 @@ FormatParser::getCanonicalIndex(const UnicodeString& s) {
UBool
FormatParser::isQuoteLiteral(UnicodeString s) {
return (UBool)((s.charAt(0)==SINGLE_QUOTE)||(s.charAt(0)==FORWARDSLASH)||(s.charAt(0)==BACKSLASH) ||
(s.charAt(0)==SPACE) ||(s.charAt(0)==COMMA) ||(s.charAt(0)==HYPHEN) ||(s.charAt(0)==DOT) );
return (UBool)(s.charAt(0)==SINGLE_QUOTE);
}
// This function aussumes the current itemIndex points to the quote literal.
@ -1524,14 +1502,6 @@ FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
UErrorCode status = U_ZERO_ERROR;
int32_t i=*itemIndex;
if ( quoteFilter == NULL ) {
setFilter(status);
}
if ((items[i].charAt(0)==FORWARDSLASH) || (items[i].charAt(0)==BACKSLASH) || (items[i].charAt(0)==SPACE) ||
(items[i].charAt(0)==COMMA) ||(items[i].charAt(0)==HYPHEN) ||(items[i].charAt(0)==DOT)) {
quote += items[i];
return;
}
if (items[i].charAt(0)==SINGLE_QUOTE) {
quote += items[i];
++i;
@ -1544,14 +1514,9 @@ FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
continue;
}
else {
// TODO turn off the filter
//if ( quoteFilter->contains(items[i].charAt(0)) ) {
// TODO add error checking here
quote += items[i];
//}
quote += items[i];
break;
}
}
else {
quote += items[i];
@ -1567,7 +1532,7 @@ FormatParser::isPatternSeparator(UnicodeString& field) {
for (int32_t i=0; i<field.length(); ++i ) {
UChar c= field.charAt(i);
if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
(c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ) {
(c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
continue;
}
else {
@ -1577,11 +1542,6 @@ FormatParser::isPatternSeparator(UnicodeString& field) {
return TRUE;
}
void
FormatParser::setFilter(UErrorCode &status) {
quoteFilter = new UnicodeSet(quotingPattern, status);
}
void
DistanceInfo::setTo(DistanceInfo &other) {
missingFieldMask = other.missingFieldMask;
@ -1603,11 +1563,6 @@ PatternMapIterator::~PatternMapIterator() {
void
PatternMapIterator::set(PatternMap& patternMap) {
/*
for (int32_t i=0; i<MAX_PATTERN_ENTRIES; ++i ) {
this->boot[i]=patternMap->boot[i];
}
*/
this->patternMap=&patternMap;
}
@ -1724,6 +1679,18 @@ PtnSkeleton::getSkeleton() {
return result;
}
UnicodeString
PtnSkeleton::getBaseSkeleton() {
UnicodeString result;
for(int32_t i=0; i< UDATPG_FIELD_COUNT; ++i) {
if (baseOriginal[i].length()!=0) {
result += baseOriginal[i];
}
}
return result;
}
PtnSkeleton::~PtnSkeleton() {
}

View file

@ -113,6 +113,7 @@ public:
PtnSkeleton(const PtnSkeleton& other);
UBool equals(const PtnSkeleton& other);
UnicodeString getSkeleton();
UnicodeString getBaseSkeleton();
virtual ~PtnSkeleton();
};
@ -222,6 +223,7 @@ public:
const UnicodeString* getPatternFromBasePattern(UnicodeString& basePattern);
const UnicodeString* getPatternFromSkeleton(PtnSkeleton& skeleton);
void copyFrom(const PatternMap& other, UErrorCode& status);
PtnElem* getHeader(UChar baseChar);
UBool equals(const PatternMap& other);
private:
UBool isDupAllowed;

View file

@ -380,11 +380,11 @@ private:
void initData(const Locale &locale);
void addCanonicalItems();
void addICUPatterns(const Locale& locale);
void addICUPatterns(const Locale& locale, UErrorCode& status);
void hackTimes(UnicodeString& hackPattern, UErrorCode& status);
void addCLDRData(const Locale& locale);
void initHashtable(UErrorCode& status);
void setDateTimeFromCalendar(const Locale& locale);
void setDateTimeFromCalendar(const Locale& locale, UErrorCode& status);
void setDecimalSymbols(const Locale& locale, UErrorCode& status);
UDateTimePatternField getAppendFormatNumber(const char* field);
UDateTimePatternField getAppendNameNumber(const char* field);

View file

@ -61,6 +61,9 @@ static const UChar testFormat[]= {0x7B, 0x31, 0x7D, 0x20, 0x7B, 0x30, 0x7D, 0};
static const UChar appendItemName[]= {0x68, 0x72, 0}; /* hr */
static const UChar testPattern2[]={ 0x48, 0x48, 0x3a, 0x6d, 0x6d, 0x20, 0x76, 0 }; /* HH:mm v */
static const UChar replacedStr[]={ 0x76, 0x76, 0x76, 0x76, 0 }; /* vvvv */
// results for getBaseSkeletons() - {Hmv}, {yMMM}
static const UChar resultBaseSkeletons[2][10] = {{0x48,0x6d, 0x76, 0}, {0x79, 0x4d, 0x4d, 0x4d, 0 } };
static void TestOpenClose() {
UErrorCode errorCode=U_ZERO_ERROR;
@ -261,6 +264,8 @@ static void TestBuilder() {
}
/* Get all skeletons and the crroespong pattern for each skeleton. */
const UChar* ptrResult[2] = {testPattern2, redundantPattern};
int32_t count=0;
en = udatpg_openSkeletons(dtpg, &errorCode);
if (U_FAILURE(errorCode) || (length==0) ) {
log_err("udatpg_openSkeletons failed!\n");
@ -268,15 +273,25 @@ static void TestBuilder() {
}
while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
if (U_FAILURE(errorCode) || p==NULL ) {
if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, ptrResult[count], pLength)!=0 ) {
log_err("udatpg_getPatternForSkeleton failed!\n");
return;
}
count++;
}
uenum_close(en);
/* Get all baseSkeletons */
en = udatpg_openBaseSkeletons(dtpg, &errorCode);
en = udatpg_openBaseSkeletons(dtpg, &errorCode);
count=0;
while ( (s=uenum_unext(en, &length, &errorCode))!= NULL) {
p = udatpg_getPatternForSkeleton(dtpg, s, length, &pLength);
if (U_FAILURE(errorCode) || p==NULL || u_memcmp(p, resultBaseSkeletons[count], pLength)!=0 ) {
log_err("udatpg_getPatternForSkeleton failed!\n");
return;
}
count++;
}
if (U_FAILURE(errorCode) || (length==0) ) {
log_err("udatpg_openSkeletons failed!\n");
return;
@ -286,5 +301,4 @@ static void TestBuilder() {
udatpg_close(dtpg);
}
#endif

View file

@ -39,8 +39,6 @@ static const char* testLocale[MAX_LOCALE][3] = {
{"fi","\0", "\0"},
};
static const UnicodeString patternResults[] = {
UnicodeString("1/1999"), // en_US
UnicodeString("Jan 1999"),
@ -54,11 +52,8 @@ static const UnicodeString patternResults[] = {
UnicodeString("58:59"),
UnicodeString("1999-1"), // zh_Hans_CN
UnicodeString("1999 1"),
UnicodeString("1999113"),
UnicodeString("1999113"),
// TODO: These are diff from CLDR 1.4 to CLDR 1.5. will verify the result soon.
//CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"),
//CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"),
CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"),
CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"),
UnicodeString("1-13"),
UnicodeString("1 13"),
CharsToUnicodeString("1999 Q1"),
@ -86,10 +81,25 @@ static const UnicodeString patternResults[] = {
UnicodeString("23.58"),
UnicodeString("58.59"),
UnicodeString(""),
};
// results for getSkeletons() and getPatternForSkeleton()
static const UnicodeString testSkeletonsResults[] = {
UnicodeString("HH:mm"),
UnicodeString("MMMMd"),
UnicodeString("MMMMMd"),
};
static const UnicodeString testBaseSkeletonsResults[] = {
UnicodeString("Hm"),
UnicodeString("MMMd"),
UnicodeString("MMMd"),
};
static const UnicodeString newDecimal=UnicodeString(" "); // space
static const UnicodeString newAppendItemName = UnicodeString("hrs.");
static const UnicodeString newAppendItemFormat = UnicodeString("{1} {0}");
static const UnicodeString newDateTimeFormat = UnicodeString("{1} {0}");
// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
// try to test the full functionality. It just calls each function in the class and
@ -146,43 +156,44 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
DateTimePatternGenerator *cloneDTPatternGen=instFromLocale->clone();
decimalSymbol = cloneDTPatternGen->getDecimal();
if (decimalSymbol != newDecimalSymbol) {
dataerrln("ERROR: inconsistency is found in cloned object- exitting");
return;
errln("ERROR: inconsistency is found in cloned object.");
}
if (U_FAILURE(status)) {
delete instFromLocale;
dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting");
return;
}
else {
delete instFromLocale;
delete cloneDTPatternGen;
}
delete instFromLocale;
delete cloneDTPatternGen;
// ======= Test simple use cases
logln("Testing simple use cases");
status = U_ZERO_ERROR;
Locale deLocale=Locale::getGermany();
UDate sampleDate=LocaleTest::date(99, 9, 13, 23, 58, 59);
DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(deLocale, status);
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting");
return;
}
UnicodeString findPattern = gen->getBestPattern(UnicodeString("MMMddHmm"), status);
SimpleDateFormat *format = new SimpleDateFormat(findPattern, deLocale, status);
//TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris"));
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())");
}
TimeZone *zone = TimeZone::createTimeZone(UnicodeString("ECT"));
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create TimeZone ECT");
}
format->setTimeZone(*zone);
UnicodeString dateReturned, expectedResult;
dateReturned="";
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("8:58 14. Okt");
if ( dateReturned != expectedResult ) {
if ( format != NULL ) delete format;
if ( zone != NULL ) delete zone;
if ( gen != NULL ) delete gen;
dataerrln("ERROR: Simple test in Locale::getGermany()) - exitting");
return;
errln("ERROR: Simple test in getBestPattern with Locale::getGermany()).");
}
// add new pattern
status = U_ZERO_ERROR;
conflictingStatus = gen->addPattern(UnicodeString("d'. von' MMMM"), true, conflictingPattern, status);
if (U_FAILURE(status)) {
errln("ERROR: Could not addPattern - d\'. von\' MMMM");
}
status = U_ZERO_ERROR;
UnicodeString testPattern=gen->getBestPattern(UnicodeString("MMMMdd"), status);
testPattern=gen->getBestPattern(UnicodeString("MMMddHmm"), status);
@ -191,13 +202,9 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("8:58 14. von Oktober");
if ( dateReturned != expectedResult ) {
if ( format != NULL ) delete format;
if ( zone != NULL ) delete zone;
if ( gen != NULL ) delete gen;
dataerrln("ERROR: Simple test add pattern d\'. von\' MMMM - exitting");
return;
errln("ERROR: Simple test addPattern failed!: d\'. von\' MMMM ");
}
if ( format != NULL ) delete format;
delete format;
// get a pattern and modify it
format = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull,
@ -207,15 +214,10 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
pattern = format->toPattern(pattern);
dateReturned="";
dateReturned = format->format(sampleDate, dateReturned, status);
//expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich");
//The mismatch is caused by the setup of Timezone. The output pattern is same as in Java.
expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 GMT+02:00");
expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich");
if ( dateReturned != expectedResult ) {
if ( format != NULL ) delete format;
if ( zone != NULL ) delete zone;
if ( gen != NULL ) delete gen;
dataerrln("ERROR: Simple test uses full date format.- exitting");
return;
errln("ERROR: Simple test uses full date format.");
errln(UnicodeString(" Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult);
}
// modify it to change the zone.
@ -223,57 +225,39 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
format->applyPattern(newPattern);
dateReturned="";
dateReturned = format->format(sampleDate, dateReturned, status);
expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 GMT+02:00");
// expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich:);
// The mismatch is caused by the setup of Timezone. The output pattern is same as in Java.
expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich");
if ( dateReturned != expectedResult ) {
if ( format != NULL ) delete format;
if ( zone != NULL ) delete zone;
if ( gen != NULL ) delete gen;
dataerrln("ERROR: Simple test modify the timezone - exitting");
return;
errln("ERROR: Simple test modify the timezone!");
errln(UnicodeString(" Got: ")+ dateReturned + UnicodeString(" Expected: ") + expectedResult);
}
/*
printf("\n replace pattern:");
for (int32_t i=0; i<pattern.length(); ++i) {
printf("%c", pattern.charAt(i));
}
printf(" with pattern:");
for (int32_t i=0; i<newPattern.length(); ++i) {
printf("%c", newPattern.charAt(i));
}
printf(" returnedDate:");
for (int32_t i=0; i<dateReturned.length(); ++i) {
printf("%c", dateReturned.charAt(i));
}
*/
if ( format != NULL ) delete format;
if ( zone != NULL ) delete zone;
// ======== Test getSkeletons and getBaseSkeletons
StringEnumeration* ptrSkeletonEnum = gen->getSkeletons(status);
if(U_FAILURE(status)) {
errln("ERROR: Fail to get skeletons !\n");
// setDeciaml(), getDeciaml()
gen->setDecimal(newDecimal);
if (newDecimal != gen->getDecimal()) {
errln("ERROR: unexpected result from setDecimal() and getDecimal()!.\n");
}
UnicodeString returnPattern, *ptrSkeleton;
ptrSkeletonEnum->reset(status);
int32_t count=ptrSkeletonEnum->count(status);
for (int32_t i=0; i<count; ++i) {
ptrSkeleton = (UnicodeString *)ptrSkeletonEnum->snext(status);
returnPattern = gen->getPatternForSkeleton(*ptrSkeleton);
}
delete ptrSkeletonEnum;
StringEnumeration* ptrBaseSkeletonEnum = gen->getBaseSkeletons(status);
if(U_FAILURE(status)) {
errln("ERROR: Fail to get base skeletons !\n");
}
count=ptrBaseSkeletonEnum->count(status);
for (int32_t i=0; i<count; ++i) {
ptrSkeleton = (UnicodeString *)ptrBaseSkeletonEnum->snext(status);
}
delete ptrBaseSkeletonEnum;
if ( gen != NULL ) delete gen;
// setAppenItemName() , getAppendItemName()
gen->setAppendItemName(UDATPG_HOUR_FIELD, newAppendItemName);
if (newAppendItemName != gen->getAppendItemName(UDATPG_HOUR_FIELD)) {
errln("ERROR: unexpected result from setAppendItemName() and getAppendItemName()!.\n");
}
// setAppenItemFormat() , getAppendItemFormat()
gen->setAppendItemFormat(UDATPG_HOUR_FIELD, newAppendItemFormat);
if (newAppendItemFormat != gen->getAppendItemFormat(UDATPG_HOUR_FIELD)) {
errln("ERROR: unexpected result from setAppendItemFormat() and getAppendItemFormat()!.\n");
}
// setDateTimeFormat() , getDateTimeFormat()
gen->setDateTimeFormat(newDateTimeFormat);
if (newDateTimeFormat != gen->getDateTimeFormat()) {
errln("ERROR: unexpected result from setDateTimeFormat() and getDateTimeFormat()!.\n");
}
delete format;
delete zone;
delete gen;
// ======= Test various skeletons.
logln("Testing DateTimePatternGenerator with various skeleton");
@ -290,10 +274,10 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
Locale loc(testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2], "");
//printf("\n\n Locale: %s_%s_%s", testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2]);
//printf("\n Status:%d", status);
DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status);
if(U_FAILURE(status)) {
errln("ERROR: Could not create DateTimePatternGenerator with locale index:%d .\n", localeIndex);
dataerrln("ERROR: Could not create DateTimePatternGenerator with locale index:%d . - exitting\n", localeIndex);
return;
}
while (patternData[dataIndex].length() > 0) {
bestPattern = patGen->getBestPattern(patternData[dataIndex++], status);
@ -304,34 +288,6 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
if ( resultDate != patternResults[resultIndex] ) {
errln(UnicodeString("\nERROR: Test various skeletons[") + (dataIndex-1)
+ UnicodeString("]. Got: ") + resultDate + UnicodeString(" Expected: ") + patternResults[resultIndex] );
// TODO Remove printf once ICU pick up CLDR 1.5
/*
printf("\nUnmatched result!\n TestPattern:");
for (int32_t i=0; i < patternData[dataIndex-1].length(); ++i) {
printf("%c", patternData[dataIndex-1].charAt(i));
}
printf(" BestPattern:");
for (int32_t i=0; i < bestPattern.length(); ++i) {
printf("%c", bestPattern.charAt(i));
}
printf(" expected result:");
for (int32_t i=0; i < patternResults[resultIndex].length(); ++i) {
printf("%c", patternResults[resultIndex].charAt(i));
}
printf("\n expected result in hex:");
for (int32_t i=0; i < patternResults[resultIndex].length(); ++i) {
printf("0x%x ", patternResults[resultIndex].charAt(i));
}
printf("\n running result:");
for (int32_t i=0; i < resultDate.length(); ++i) {
printf("%c", resultDate.charAt(i));
}
printf(" running result in hex:");
for (int32_t i=0; i < resultDate.length(); ++i) {
printf("0x%x ", resultDate.charAt(i));
}
*/
}
resultIndex++;
@ -375,11 +331,7 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
if(test->getDynamicClassID() != DateTimePatternGenerator::getStaticClassID()) {
errln("ERROR: getDynamicClassID() didn't return the expected value");
}
if (test!=NULL) {
delete test;
test=NULL;
}
delete test;
// ====== Test createEmptyInstance()
@ -388,7 +340,9 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
test = DateTimePatternGenerator::createEmptyInstance(status);
if(U_FAILURE(status)) {
errln("ERROR: Fail to create an empty instance !\n");
errln("ERROR: Fail to create an empty instance ! - exitting.\n");
delete test;
return;
}
conflictingStatus = test->addPattern(UnicodeString("MMMMd"), true, conflictingPattern, status);
@ -403,14 +357,41 @@ void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/)
output->reset(status);
const UnicodeString *dupPattern=output->snext(status);
if ( (dupPattern==NULL) || (*dupPattern != expectedResult) ) {
errln("ERROR: Fail in getRedundants !\n");
errln("ERROR: Fail in getRedundants !\n");
}
}
delete output;
if (test!=NULL) {
delete test;
}
// ======== Test getSkeletons and getBaseSkeletons
StringEnumeration* ptrSkeletonEnum = test->getSkeletons(status);
if(U_FAILURE(status)) {
errln("ERROR: Fail to get skeletons !\n");
}
UnicodeString returnPattern, *ptrSkeleton;
ptrSkeletonEnum->reset(status);
int32_t count=ptrSkeletonEnum->count(status);
for (int32_t i=0; i<count; ++i) {
ptrSkeleton = (UnicodeString *)ptrSkeletonEnum->snext(status);
returnPattern = test->getPatternForSkeleton(*ptrSkeleton);
if ( returnPattern != testSkeletonsResults[i] ) {
errln("ERROR: Unexpected result from getSkeletons() and getPatternForSkeleton() !\n");
}
}
StringEnumeration* ptrBaseSkeletonEnum = test->getBaseSkeletons(status);
if(U_FAILURE(status)) {
errln("ERROR: Fail to get base skeletons !\n");
}
count=ptrBaseSkeletonEnum->count(status);
for (int32_t i=0; i<count; ++i) {
ptrSkeleton = (UnicodeString *)ptrBaseSkeletonEnum->snext(status);
if ( *ptrSkeleton != testBaseSkeletonsResults[i] ) {
errln("ERROR: Unexpected result from getBaseSkeletons() !\n");
}
}
delete output;
delete ptrSkeletonEnum;
delete ptrBaseSkeletonEnum;
delete test;
}
#endif /* #if !UCONFIG_NO_FORMATTING */