]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/aliastst.cpp
1 /********************************************************************
3 * Copyright (c) 2005-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 #include "unicode/calendar.h"
9 #include "unicode/smpdtfmt.h"
10 #include "unicode/datefmt.h"
11 #include "unicode/unistr.h"
12 #include "unicode/coll.h"
13 #include "unicode/resbund.h"
18 const char* _LOCALE_ALIAS
[][2] = {
33 const int _LOCALE_NUMBER
= 12;
35 void LocaleAliasTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ ){
37 TESTCASE(0, TestCalendar
);
38 TESTCASE(1, TestDateFormat
);
39 TESTCASE(2, TestCollation
);
40 TESTCASE(3, TestULocale
);
41 TESTCASE(4, TestUResourceBundle
);
42 TESTCASE(5, TestDisplayName
);
43 // keep the last index in sync with the condition in default:
46 if (index
<= 5) { // keep this in sync with the last index!
47 name
= "(test omitted by !UCONFIG_NO_FORMATTING)";
51 break; //needed to end loop
55 void LocaleAliasTest::TestCalendar() {
56 #if !UCONFIG_NO_FORMATTING
57 UErrorCode status
= U_ZERO_ERROR
;
58 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
59 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
60 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
61 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
62 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
65 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
66 Calendar
* c1
= Calendar::createInstance(oldLoc
, status
);
67 Calendar
* c2
= Calendar::createInstance(newLoc
, status
);
69 //Test function "getLocale(ULocale.VALID_LOCALE)"
70 const char* l1
= c1
->getLocaleID(ULOC_VALID_LOCALE
, status
);
71 const char* l2
= c2
->getLocaleID(ULOC_VALID_LOCALE
, status
);
72 if (strcmp(newLoc
.getName(), l1
)!=0) {
73 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
));
75 if (strcmp(l1
, l2
)!=0) {
76 errln("CalendarTest: l1!=l2: l1= "+UnicodeString(l1
) +" l2= "+UnicodeString(l2
));
79 errln("CalendarTest: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
81 logln("Calendar(getLocale) old:"+UnicodeString(l1
)+" new:"+UnicodeString(l2
));
87 void LocaleAliasTest::TestDateFormat() {
88 #if !UCONFIG_NO_FORMATTING
89 UErrorCode status
= U_ZERO_ERROR
;
90 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
91 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
92 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
93 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
94 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
97 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
99 DateFormat
* df1
= DateFormat::createDateInstance(DateFormat::FULL
, oldLoc
);
100 DateFormat
* df2
= DateFormat::createDateInstance(DateFormat::FULL
, newLoc
);
102 //Test function "getLocale"
103 const char* l1
= df1
->getLocaleID(ULOC_VALID_LOCALE
, status
);
104 const char* l2
= df2
->getLocaleID(ULOC_VALID_LOCALE
, status
);
105 if (strcmp(newLoc
.getName(), l1
)!=0) {
106 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
));
108 if (strcmp(l1
, l2
)!=0) {
109 errln("TestDateFormat: l1!=l2: l1= "+UnicodeString(l1
) +" l2= "+UnicodeString(l2
));
112 errln("TestDateFormat: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
114 logln("DateFormat(getLocale) old:%s new:%s",l1
,l2
);
121 void LocaleAliasTest::TestCollation() {
122 #if !UCONFIG_NO_COLLATION
123 UErrorCode status
= U_ZERO_ERROR
;
124 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
125 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
126 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
127 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
128 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
131 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
133 Collator
* c1
= Collator::createInstance(oldLoc
, status
);
134 Collator
* c2
= Collator::createInstance(newLoc
, status
);
136 Locale l1
= c1
->getLocale(ULOC_VALID_LOCALE
, status
);
137 Locale l2
= c2
->getLocale(ULOC_VALID_LOCALE
, status
);
139 if (strcmp(newLoc
.getName(), l1
.getName())!=0) {
140 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
.getName()));
142 if (strcmp(l1
.getName(), l2
.getName())!=0) {
143 errln("CollationTest: l1!=l2: l1= "+UnicodeString(l1
.getName()) +" l2= "+UnicodeString(l2
.getName()));
146 errln("CollationTest: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
148 logln("Collator(getLocale) old:%s new:%s", l1
.getName(), l2
.getName());
154 void LocaleAliasTest::TestULocale() {
155 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
156 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
157 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
158 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
159 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
162 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
164 UnicodeString name1
, name2
;
165 oldLoc
.getDisplayName(name1
);
166 newLoc
.getDisplayName(name2
);
168 errln("DisplayNames are not equal. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
170 log("ULocale(getDisplayName) old:");
176 LocaleAliasTest::LocaleAliasTest(){
177 UErrorCode status
= U_ZERO_ERROR
;
178 resIndex
= ures_open(NULL
,"res_index", &status
);
179 if(U_FAILURE(status
)){
180 errln("Could not open res_index.res. Exiting. Error: %s\n", u_errorName(status
));
183 defLocale
= Locale::getDefault();
184 Locale::setDefault(Locale::getUS(), status
);
186 LocaleAliasTest::~LocaleAliasTest(){
187 /* reset the default locale */
188 UErrorCode status
= U_ZERO_ERROR
;
189 Locale::setDefault(defLocale
, status
);
190 ures_close(resIndex
);
191 if(U_FAILURE(status
)){
192 errln("Could not reset the default locale. Exiting. Error: %s\n", u_errorName(status
));
195 UBool
LocaleAliasTest::isLocaleAvailable(const char* loc
){
199 UErrorCode status
= U_ZERO_ERROR
;
201 ures_getStringByKey(resIndex
, loc
,&len
, &status
);
202 if(U_FAILURE(status
)){
207 void LocaleAliasTest::TestDisplayName() {
208 int32_t availableNum
=0;
209 const Locale
* available
= Locale::getAvailableLocales(availableNum
);
210 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
211 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
212 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
213 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
214 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
217 for(int j
=0; j
<availableNum
; j
++){
218 UnicodeString dipLocName
= UnicodeString(available
[j
].getName());
219 const UnicodeString oldCountry
= oldLoc
.getDisplayCountry(dipLocName
);
220 const UnicodeString newCountry
= newLoc
.getDisplayCountry(dipLocName
);
221 const UnicodeString oldLang
= oldLoc
.getDisplayLanguage(dipLocName
);
222 const UnicodeString newLang
= newLoc
.getDisplayLanguage(dipLocName
);
224 // is there display name for the current country ID
225 if(newCountry
!= newLoc
.getCountry()){
226 if(oldCountry
!=newCountry
){
227 errln("getCountry() failed for "+ UnicodeString(oldLoc
.getName()) +" oldCountry= "+ prettify(oldCountry
) +" newCountry = "+prettify(newCountry
)+ " in display locale "+ UnicodeString(available
[j
].getName()));
230 //there is a display name for the current lang ID
231 if(newLang
!= newLoc
.getLanguage()){
232 if(oldLang
!= newLang
){
233 errln("getLanguage() failed for " + UnicodeString(oldLoc
.getName()) + " oldLang = "+ prettify(oldLang
) +" newLang = "+prettify(newLang
)+ " in display locale "+UnicodeString(available
[j
].getName()));
239 void LocaleAliasTest::TestUResourceBundle() {
241 UErrorCode status
= U_ZERO_ERROR
;
242 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
243 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
244 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
245 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
246 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
249 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
251 ResourceBundle
* rb1
= NULL
;
252 ResourceBundle
* rb2
= NULL
;
254 const char* testdatapath
=loadTestData(status
);
256 UnicodeString
us1("NULL");
257 UnicodeString
us2("NULL");
258 rb1
= new ResourceBundle(testdatapath
, oldLoc
, status
);
259 if (U_FAILURE(U_ZERO_ERROR
)) {
262 us1
= rb1
->getStringEx("locale", status
);
264 rb2
= new ResourceBundle(testdatapath
, newLoc
, status
);
265 if (U_FAILURE(U_ZERO_ERROR
)){
268 us2
= rb2
->getStringEx("locale", status
);
270 UnicodeString
uNewLoc(newLoc
.getName());
271 if (us1
.compare(uNewLoc
)!=0 || us1
.compare(us2
)!=0 || status
!=U_ZERO_ERROR
) {
274 log("UResourceBundle(getStringEx) old:");