]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/aliastst.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2005-2006, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
10 #include "unicode/calendar.h"
11 #include "unicode/smpdtfmt.h"
12 #include "unicode/datefmt.h"
13 #include "unicode/unistr.h"
14 #include "unicode/coll.h"
15 #include "unicode/resbund.h"
20 const char* _LOCALE_ALIAS
[][2] = {
35 const int _LOCALE_NUMBER
= 12;
37 void LocaleAliasTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ ){
39 TESTCASE(0, TestCalendar
);
40 TESTCASE(1, TestDateFormat
);
41 TESTCASE(2, TestCollation
);
42 TESTCASE(3, TestULocale
);
43 TESTCASE(4, TestUResourceBundle
);
44 TESTCASE(5, TestDisplayName
);
45 // keep the last index in sync with the condition in default:
48 if (index
<= 5) { // keep this in sync with the last index!
49 name
= "(test omitted by !UCONFIG_NO_FORMATTING)";
53 break; //needed to end loop
57 void LocaleAliasTest::TestCalendar() {
58 #if !UCONFIG_NO_FORMATTING
59 UErrorCode status
= U_ZERO_ERROR
;
60 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
61 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
62 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
63 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
64 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
67 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
68 Calendar
* c1
= Calendar::createInstance(oldLoc
, status
);
69 Calendar
* c2
= Calendar::createInstance(newLoc
, status
);
71 //Test function "getLocale(ULocale.VALID_LOCALE)"
72 const char* l1
= c1
->getLocaleID(ULOC_VALID_LOCALE
, status
);
73 const char* l2
= c2
->getLocaleID(ULOC_VALID_LOCALE
, status
);
74 if (strcmp(newLoc
.getName(), l1
)!=0) {
75 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
));
77 if (strcmp(l1
, l2
)!=0) {
78 errln("CalendarTest: l1!=l2: l1= "+UnicodeString(l1
) +" l2= "+UnicodeString(l2
));
81 errln("CalendarTest: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
83 logln("Calendar(getLocale) old:"+UnicodeString(l1
)+" new:"+UnicodeString(l2
));
89 void LocaleAliasTest::TestDateFormat() {
90 #if !UCONFIG_NO_FORMATTING
91 UErrorCode status
= U_ZERO_ERROR
;
92 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
93 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
94 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
95 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
96 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
99 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
101 DateFormat
* df1
= DateFormat::createDateInstance(DateFormat::FULL
, oldLoc
);
102 DateFormat
* df2
= DateFormat::createDateInstance(DateFormat::FULL
, newLoc
);
104 //Test function "getLocale"
105 const char* l1
= df1
->getLocaleID(ULOC_VALID_LOCALE
, status
);
106 const char* l2
= df2
->getLocaleID(ULOC_VALID_LOCALE
, status
);
107 if (strcmp(newLoc
.getName(), l1
)!=0) {
108 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
));
110 if (strcmp(l1
, l2
)!=0) {
111 errln("TestDateFormat: l1!=l2: l1= "+UnicodeString(l1
) +" l2= "+UnicodeString(l2
));
114 errln("TestDateFormat: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
116 logln("DateFormat(getLocale) old:%s new:%s",l1
,l2
);
123 void LocaleAliasTest::TestCollation() {
124 #if !UCONFIG_NO_COLLATION
125 UErrorCode status
= U_ZERO_ERROR
;
126 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
127 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
128 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
129 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
130 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
133 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
135 Collator
* c1
= Collator::createInstance(oldLoc
, status
);
136 Collator
* c2
= Collator::createInstance(newLoc
, status
);
138 Locale l1
= c1
->getLocale(ULOC_VALID_LOCALE
, status
);
139 Locale l2
= c2
->getLocale(ULOC_VALID_LOCALE
, status
);
141 if (strcmp(newLoc
.getName(), l1
.getName())!=0) {
142 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc
.getName()) +" l1= "+UnicodeString(l1
.getName()));
144 if (strcmp(l1
.getName(), l2
.getName())!=0) {
145 errln("CollationTest: l1!=l2: l1= "+UnicodeString(l1
.getName()) +" l2= "+UnicodeString(l2
.getName()));
148 errln("CollationTest: c1!=c2. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
150 logln("Collator(getLocale) old:%s new:%s", l1
.getName(), l2
.getName());
156 void LocaleAliasTest::TestULocale() {
157 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
158 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
159 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
160 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
161 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
164 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
166 UnicodeString name1
, name2
;
167 oldLoc
.getDisplayName(name1
);
168 newLoc
.getDisplayName(name2
);
170 errln("DisplayNames are not equal. newLoc= "+UnicodeString(newLoc
.getName()) +" oldLoc= "+UnicodeString(oldLoc
.getName()));
172 log("ULocale(getDisplayName) old:");
178 LocaleAliasTest::LocaleAliasTest(){
179 UErrorCode status
= U_ZERO_ERROR
;
180 resIndex
= ures_open(NULL
,"res_index", &status
);
181 if(U_FAILURE(status
)){
182 errln("Could not open res_index.res. Exiting. Error: %s\n", u_errorName(status
));
185 defLocale
= Locale::getDefault();
186 Locale::setDefault(Locale::getUS(), status
);
188 LocaleAliasTest::~LocaleAliasTest(){
189 /* reset the default locale */
190 UErrorCode status
= U_ZERO_ERROR
;
191 Locale::setDefault(defLocale
, status
);
192 ures_close(resIndex
);
193 if(U_FAILURE(status
)){
194 errln("Could not reset the default locale. Exiting. Error: %s\n", u_errorName(status
));
197 UBool
LocaleAliasTest::isLocaleAvailable(const char* loc
){
201 UErrorCode status
= U_ZERO_ERROR
;
203 ures_getStringByKey(resIndex
, loc
,&len
, &status
);
204 if(U_FAILURE(status
)){
209 void LocaleAliasTest::TestDisplayName() {
210 int32_t availableNum
=0;
211 const Locale
* available
= Locale::getAvailableLocales(availableNum
);
212 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
213 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
214 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
215 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
216 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
219 for(int j
=0; j
<availableNum
; j
++){
220 UnicodeString dipLocName
= UnicodeString(available
[j
].getName());
221 const UnicodeString oldCountry
= oldLoc
.getDisplayCountry(dipLocName
);
222 const UnicodeString newCountry
= newLoc
.getDisplayCountry(dipLocName
);
223 const UnicodeString oldLang
= oldLoc
.getDisplayLanguage(dipLocName
);
224 const UnicodeString newLang
= newLoc
.getDisplayLanguage(dipLocName
);
226 // is there display name for the current country ID
227 if(newCountry
!= newLoc
.getCountry()){
228 if(oldCountry
!=newCountry
){
229 errln("getCountry() failed for "+ UnicodeString(oldLoc
.getName()) +" oldCountry= "+ prettify(oldCountry
) +" newCountry = "+prettify(newCountry
)+ " in display locale "+ UnicodeString(available
[j
].getName()));
232 //there is a display name for the current lang ID
233 if(newLang
!= newLoc
.getLanguage()){
234 if(oldLang
!= newLang
){
235 errln("getLanguage() failed for " + UnicodeString(oldLoc
.getName()) + " oldLang = "+ prettify(oldLang
) +" newLang = "+prettify(newLang
)+ " in display locale "+UnicodeString(available
[j
].getName()));
241 void LocaleAliasTest::TestUResourceBundle() {
243 UErrorCode status
= U_ZERO_ERROR
;
244 for (int i
=0; i
<_LOCALE_NUMBER
; i
++) {
245 Locale
oldLoc(_LOCALE_ALIAS
[i
][0]);
246 Locale
newLoc(_LOCALE_ALIAS
[i
][1]);
247 if(!isLocaleAvailable(_LOCALE_ALIAS
[i
][1])){
248 logln(UnicodeString(newLoc
.getName())+" is not available. Skipping!");
251 logln("\nold locale:%s new locale:%s",oldLoc
.getName(),newLoc
.getName());
253 ResourceBundle
* rb1
= NULL
;
254 ResourceBundle
* rb2
= NULL
;
256 const char* testdatapath
=loadTestData(status
);
258 UnicodeString
us1("NULL");
259 UnicodeString
us2("NULL");
260 rb1
= new ResourceBundle(testdatapath
, oldLoc
, status
);
261 if (U_FAILURE(U_ZERO_ERROR
)) {
264 us1
= rb1
->getStringEx("locale", status
);
266 rb2
= new ResourceBundle(testdatapath
, newLoc
, status
);
267 if (U_FAILURE(U_ZERO_ERROR
)){
270 us2
= rb2
->getStringEx("locale", status
);
272 UnicodeString
uNewLoc(newLoc
.getName());
273 if (us1
.compare(uNewLoc
)!=0 || us1
.compare(us2
)!=0 || status
!=U_ZERO_ERROR
) {
276 log("UResourceBundle(getStringEx) old:");