ICU-4051 verify locale aliases

X-SVN-Rev: 18433
This commit is contained in:
Ram Viswanadha 2005-08-15 16:11:39 +00:00
parent 9f158c5eef
commit c68ff6e555
5 changed files with 326 additions and 10 deletions

View File

@ -31,7 +31,7 @@ CPPFLAGS += -I$(top_builddir)/common -I$(top_srcdir)/common -I$(top_srcdir)/i18n
DEFS += -D'U_TOPSRCDIR="$(top_srcdir)/"' -D'U_TOPBUILDDIR="$(BUILDDIR)"' DEFS += -D'U_TOPSRCDIR="$(top_srcdir)/"' -D'U_TOPBUILDDIR="$(BUILDDIR)"'
LIBS = $(LIBCTESTFW) $(LIBICUI18N) $(LIBICUUC) $(LIBICUTOOLUTIL) $(DEFAULT_LIBS) $(LIB_M) LIBS = $(LIBCTESTFW) $(LIBICUI18N) $(LIBICUUC) $(LIBICUTOOLUTIL) $(DEFAULT_LIBS) $(LIB_M)
OBJECTS = allcoll.o apicoll.o astrotst.o callimts.o calregts.o caltest.o \ OBJECTS = aliastst.o allcoll.o apicoll.o astrotst.o callimts.o calregts.o caltest.o \
caltztst.o canittst.o citrtest.o cntabcol.o convtest.o currcoll.o \ caltztst.o canittst.o citrtest.o cntabcol.o convtest.o currcoll.o \
dadrcoll.o dcfmapts.o decoll.o dtfmapts.o dtfmrgts.o dtfmtrtts.o dtfmttst.o \ dadrcoll.o dcfmapts.o decoll.o dtfmapts.o dtfmrgts.o dtfmtrtts.o dtfmttst.o \
encoll.o escoll.o ficoll.o frcoll.o g7coll.o intltest.o \ encoll.o escoll.o ficoll.o frcoll.o g7coll.o intltest.o \

View File

@ -0,0 +1,281 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2005, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "aliastst.h"
#include <unicode/calendar.h>
#include <unicode/smpdtfmt.h>
#include <unicode/datefmt.h>
#include <unicode/unistr.h>
#include <unicode/coll.h>
#include <unicode/resbund.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char* _LOCALE_ALIAS[][2] = {
{"in", "id"},
{"in_ID", "id_ID"},
{"iw", "he"},
{"iw_IL", "he_IL"},
{"ji", "yi"},
{"en_BU", "en_MM"},
{"en_DY", "en_BJ"},
{"en_HV", "en_BF"},
{"en_NH", "en_VU"},
{"en_RH", "en_ZW"},
{"en_TP", "en_TL"},
{"en_ZR", "en_CD"}
};
const int _LOCALE_NUMBER = 12;
const Locale DEFAULT_LOCALE = Locale::getUS();
void LocaleAliasTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ){
switch (index) {
TESTCASE(0, TestCalendar);
TESTCASE(1, TestDateFormat);
TESTCASE(2, TestCollation);
TESTCASE(3, TestULocale);
TESTCASE(4, TestUResourceBundle);
TESTCASE(5, TestDisplayName);
// keep the last index in sync with the condition in default:
default:
if (index <= 5) { // keep this in sync with the last index!
name = "(test omitted by !UCONFIG_NO_FORMATTING)";
} else {
name = "";
}
break; //needed to end loop
}
}
void LocaleAliasTest::TestCalendar() {
UErrorCode status = U_ZERO_ERROR;
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
logln("\nold locale:%s new locale:%s",oldLoc->getName(),newLoc->getName());
Calendar* c1 = Calendar::createInstance(*oldLoc, status);
Calendar* c2 = Calendar::createInstance(*newLoc, status);
//Test function "getLocale(ULocale.VALID_LOCALE)"
const char* l1 = c1->getLocaleID(ULOC_VALID_LOCALE, status);
const char* l2 = c2->getLocaleID(ULOC_VALID_LOCALE, status);
if (strcmp(newLoc->getName(), l1)!=0) {
errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc->getName()) +" l1= "+UnicodeString(l1));
}
if (strcmp(l1, l2)!=0) {
errln("CalendarTest: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
}
if(!(c1==c2)){
errln("CalendarTest: c1!=c2. newLoc= "+UnicodeString(newLoc->getName()) +" oldLoc= "+UnicodeString(oldLoc->getName()));
}
logln("Calendar(getLocale) old:"+UnicodeString(l1)+" new:"+UnicodeString(l2));
delete c1;
delete c2;
}
}
void LocaleAliasTest::TestDateFormat() {
UErrorCode status = U_ZERO_ERROR;
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
logln("\nold locale:%s new locale:%s",oldLoc->getName(),newLoc->getName());
DateFormat* df1 = DateFormat::createDateInstance(DateFormat::FULL, *oldLoc);
DateFormat* df2 = DateFormat::createDateInstance(DateFormat::FULL, *newLoc);
//Test function "getLocale"
const char* l1 = df1->getLocaleID(ULOC_VALID_LOCALE, status);
const char* l2 = df2->getLocaleID(ULOC_VALID_LOCALE, status);
if (strcmp(newLoc->getName(), l1)!=0) {
errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc->getName()) +" l1= "+UnicodeString(l1));
}
if (strcmp(l1, l2)!=0) {
errln("TestDateFormat: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
}
if(!(df1==df2)){
errln("TestDateFormat: c1!=c2. newLoc= "+UnicodeString(newLoc->getName()) +" oldLoc= "+UnicodeString(oldLoc->getName()));
}
logln("DateFormat(getLocale) old:%s new:%s",l1,l2);
delete df1;
delete df2;
}
}
void LocaleAliasTest::TestCollation() {
UErrorCode status = U_ZERO_ERROR;
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
logln("\nold locale:%s new locale:%s",oldLoc->getName(),newLoc->getName());
Collator* c1 = Collator::createInstance(*oldLoc, status);
Collator* c2 = Collator::createInstance(*newLoc, status);
Locale l1 = c1->getLocale(ULOC_VALID_LOCALE, status);
Locale l2 = c2->getLocale(ULOC_VALID_LOCALE, status);
if (strcmp(newLoc->getName(), l1.getName())!=0) {
errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc->getName()) +" l1= "+UnicodeString(l1.getName()));
}
if (strcmp(l1.getName(), l2.getName())!=0) {
errln("CollationTest: l1!=l2: l1= "+UnicodeString(l1.getName()) +" l2= "+UnicodeString(l2.getName()));
}
if(!(c1==c2)){
errln("CollationTest: c1!=c2. newLoc= "+UnicodeString(newLoc->getName()) +" oldLoc= "+UnicodeString(oldLoc->getName()));
}
logln("Collator(getLocale) old:%s new:%s", l1.getName(), l2.getName());
delete c1;
delete c2;
}
}
void LocaleAliasTest::TestULocale() {
UErrorCode status = U_ZERO_ERROR;
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
logln("\nold locale:%s new locale:%s",oldLoc->getName(),newLoc->getName());
UnicodeString name1, name2;
oldLoc->getDisplayName(name1);
newLoc->getDisplayName(name2);
if (name1!=name2) {
}
log("ULocale(getDisplayName) old:");
log(name1);
log(" new:");
logln(name2);
}
}
LocaleAliasTest::LocaleAliasTest(){
UErrorCode status = U_ZERO_ERROR;
resIndex = ures_open(NULL,"res_index", &status);
if(U_FAILURE(status)){
errln("Could not open res_index.res. Exiting. Error: %s\n", u_errorName(status));
resIndex=NULL;
}
defLocale = Locale::getDefault();
Locale::setDefault(DEFAULT_LOCALE, status);
}
LocaleAliasTest::~LocaleAliasTest(){
/* reset the default locale */
UErrorCode status = U_ZERO_ERROR;
Locale::setDefault(defLocale, status);
ures_close(resIndex);
if(U_FAILURE(status)){
errln("Could not reset the default locale. Exiting. Error: %s\n", u_errorName(status));
}
}
UBool LocaleAliasTest::isLocaleAvailable(const char* loc){
if(resIndex==NULL){
return FALSE;
}
UErrorCode status = U_ZERO_ERROR;
int32_t len = 0;
ures_getStringByKey(resIndex, loc,&len, &status);
if(U_FAILURE(status)){
return FALSE;
}
return TRUE;
}
void LocaleAliasTest::TestDisplayName() {
UErrorCode status = U_ZERO_ERROR;
int32_t availableNum =0;
const Locale* available = Locale::getAvailableLocales(availableNum);
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
for(int j=0; j<availableNum; j++){
UnicodeString dipLocName = UnicodeString(available[j].getName());
const UnicodeString oldCountry = oldLoc->getDisplayCountry(dipLocName);
const UnicodeString newCountry = newLoc->getDisplayCountry(dipLocName);
const UnicodeString oldLang = oldLoc->getDisplayLanguage(dipLocName);
const UnicodeString newLang = newLoc->getDisplayLanguage(dipLocName);
// is there display name for the current country ID
if(newCountry != newLoc->getCountry()){
if(oldCountry!=newCountry){
errln("getCountry() failed for "+ UnicodeString(oldLoc->getName()) +" oldCountry= "+ prettify(oldCountry) +" newCountry = "+prettify(newCountry)+ " in display locale "+ UnicodeString(available[j].getName()));
}
}
//there is a display name for the current lang ID
if(newLang != newLoc->getLanguage()){
if(oldLang != newLang){
errln("getLanguage() failed for " + UnicodeString(oldLoc->getName()) + " oldLang = "+ prettify(oldLang) +" newLang = "+prettify(newLang)+ " in display locale "+UnicodeString(available[j].getName()));
}
}
}
}
}
void LocaleAliasTest::TestUResourceBundle() {
UErrorCode status = U_ZERO_ERROR;
for (int i=0; i<_LOCALE_NUMBER; i++) {
Locale* oldLoc = new Locale(_LOCALE_ALIAS[i][0]);
Locale* newLoc = new Locale(_LOCALE_ALIAS[i][1]);
if(!isLocaleAvailable(_LOCALE_ALIAS[i][1])){
logln(UnicodeString(newLoc->getName())+" is not available. Skipping!");
continue;
}
logln("\nold locale:%s new locale:%s",oldLoc->getName(),newLoc->getName());
ResourceBundle* rb1 = NULL;
ResourceBundle* rb2 = NULL;
const char* testdatapath=loadTestData(status);
UnicodeString us1("NULL");
UnicodeString us2("NULL");
rb1 = new ResourceBundle(testdatapath, *oldLoc, status);
if (U_FAILURE(U_ZERO_ERROR)) {
} else {
us1 = rb1->getStringEx("locale", status);
}
rb2 = new ResourceBundle(testdatapath, *newLoc, status);
if (U_FAILURE(U_ZERO_ERROR)){
} else {
us2 = rb2->getStringEx("locale", status);
}
UnicodeString uNewLoc(newLoc->getName());
if (us1.compare(uNewLoc)!=0 || us1.compare(us2)!=0 || status!=U_ZERO_ERROR) {
}
log("UResourceBundle(getStringEx) old:");
log(us1);
log(" new:");
logln(us2);
if (rb1!=NULL) {
delete rb1;
}
if (rb2!=NULL) {
delete rb2;
}
}
}

View File

@ -0,0 +1,30 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2005, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#ifndef _ALIASTST
#define _ALIASTST
#include "intltest.h"
#include "unicode/locid.h"
#include "unicode/ures.h"
class LocaleAliasTest: public IntlTest {
public:
void TestCalendar();
void TestDateFormat();
void TestCollation();
void TestULocale();
void TestUResourceBundle();
void TestDisplayName();
void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL );
LocaleAliasTest();
~LocaleAliasTest();
private:
UResourceBundle* resIndex;
UBool isLocaleAvailable(const char*);
Locale defLocale;
};
#endif

View File

@ -605,6 +605,12 @@
<Filter <Filter
Name="locales &amp; resources" Name="locales &amp; resources"
Filter=""> Filter="">
<File
RelativePath=".\aliastst.cpp">
</File>
<File
RelativePath=".\aliastst.h">
</File>
<File <File
RelativePath=".\loctest.cpp"> RelativePath=".\loctest.cpp">
</File> </File>

View File

@ -23,7 +23,8 @@
#include "uobjtest.h" #include "uobjtest.h"
#include "utxttest.h" #include "utxttest.h"
#include "v32test.h" #include "v32test.h"
#include "uvectest.h" #include "uvectest.h"
#include "aliastst.h"
//#include "custrtest.h" //#include "custrtest.h"
//#include "ccitrtst.h" //#include "ccitrtst.h"
//#include "cloctest.h" //#include "cloctest.h"
@ -148,19 +149,17 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &
UTextTest test; UTextTest test;
callTest( test, par ); callTest( test, par );
} }
break;; break;
case 13:
/* name = "LocaleAliasTest";
case 8:
name = "LocaleTest";
if (exec) { if (exec) {
logln("LocaleTest---"); logln(""); logln("LocaleAliasTest---"); logln("");
CLocaleTest test; LocaleAliasTest test;
callTest( test, par ); callTest( test, par );
} }
break; break;
*/
default: name = ""; break; //needed to end loop default: name = ""; break; //needed to end loop
} }
} }