Imported Upstream version 58.1
[platform/upstream/icu.git] / source / test / intltest / aliastst.cpp
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT: 
5  * Copyright (c) 2005-2006, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8
9 #include "aliastst.h"
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"
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19
20 const char* _LOCALE_ALIAS[][2] = {
21     {"in", "id"},
22     {"in_ID", "id_ID"},
23     {"iw", "he"},
24     {"iw_IL", "he_IL"},
25     {"ji", "yi"},
26     {"en_BU", "en_MM"},
27     {"en_DY", "en_BJ"},
28     {"en_HV", "en_BF"},
29     {"en_NH", "en_VU"},
30     {"en_RH", "en_ZW"},
31     {"en_TP", "en_TL"},
32     {"en_ZR", "en_CD"}
33 };
34
35 const int _LOCALE_NUMBER = 12;
36
37 void LocaleAliasTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ){
38     switch (index) {
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:
46
47         default:
48             if (index <= 5) { // keep this in sync with the last index!
49                 name = "(test omitted by !UCONFIG_NO_FORMATTING)";
50             } else {
51                 name = "";
52             }
53             break; //needed to end loop
54     }
55 }
56
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!");
65             continue;
66         }
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);
70
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));
76         }
77         if (strcmp(l1, l2)!=0) {
78             errln("CalendarTest: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
79         }
80         if(!(c1==c2)){
81             errln("CalendarTest: c1!=c2.  newLoc= "+UnicodeString(newLoc.getName())  +" oldLoc= "+UnicodeString(oldLoc.getName()));
82         }
83         logln("Calendar(getLocale) old:"+UnicodeString(l1)+"   new:"+UnicodeString(l2));   
84         delete c1;
85         delete c2;
86     }
87 #endif
88 }
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!");
97             continue;
98         }
99         logln("\nold locale:%s   new locale:%s",oldLoc.getName(),newLoc.getName());
100
101         DateFormat* df1 = DateFormat::createDateInstance(DateFormat::FULL, oldLoc);
102         DateFormat* df2 = DateFormat::createDateInstance(DateFormat::FULL, newLoc);
103
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));
109         }
110         if (strcmp(l1, l2)!=0) {
111             errln("TestDateFormat: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
112         }
113         if(!(df1==df2)){
114             errln("TestDateFormat: c1!=c2.  newLoc= "+UnicodeString(newLoc.getName())  +" oldLoc= "+UnicodeString(oldLoc.getName()));
115         }
116         logln("DateFormat(getLocale) old:%s   new:%s",l1,l2);
117
118         delete df1;
119         delete df2;
120     }
121 #endif
122 }
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!");
131             continue;
132         }
133         logln("\nold locale:%s   new locale:%s",oldLoc.getName(),newLoc.getName());
134
135         Collator* c1 = Collator::createInstance(oldLoc, status);
136         Collator* c2 = Collator::createInstance(newLoc, status);
137
138         Locale l1 = c1->getLocale(ULOC_VALID_LOCALE, status);
139         Locale l2 = c2->getLocale(ULOC_VALID_LOCALE, status);
140
141         if (strcmp(newLoc.getName(), l1.getName())!=0) {
142             errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc.getName()) +" l1= "+UnicodeString(l1.getName()));
143         }
144         if (strcmp(l1.getName(), l2.getName())!=0) {
145             errln("CollationTest: l1!=l2: l1= "+UnicodeString(l1.getName()) +" l2= "+UnicodeString(l2.getName()));
146         }
147         if(!(c1==c2)){
148             errln("CollationTest: c1!=c2.  newLoc= "+UnicodeString(newLoc.getName())  +" oldLoc= "+UnicodeString(oldLoc.getName()));
149         }
150         logln("Collator(getLocale) old:%s   new:%s", l1.getName(), l2.getName());
151         delete c1;
152         delete c2;
153     }
154 #endif
155 }
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!");
162             continue;
163         }
164         logln("\nold locale:%s   new locale:%s",oldLoc.getName(),newLoc.getName());
165
166         UnicodeString name1, name2;
167         oldLoc.getDisplayName(name1);
168         newLoc.getDisplayName(name2);
169         if (name1!=name2) {
170             errln("DisplayNames are not equal.  newLoc= "+UnicodeString(newLoc.getName())  +" oldLoc= "+UnicodeString(oldLoc.getName()));
171         }
172         log("ULocale(getDisplayName) old:");
173         log(name1);
174         log("   new:");
175         logln(name2);
176     }
177 }
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));
183         resIndex=NULL;
184     }
185     defLocale = Locale::getDefault();
186     Locale::setDefault(Locale::getUS(), status); 
187 }
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));
195     }
196 }
197 UBool LocaleAliasTest::isLocaleAvailable(const char* loc){
198     if(resIndex==NULL){
199         return FALSE;
200     }
201     UErrorCode status = U_ZERO_ERROR;
202     int32_t len = 0;
203     ures_getStringByKey(resIndex, loc,&len, &status);
204     if(U_FAILURE(status)){
205         return FALSE; 
206     }
207     return TRUE;
208 }
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!");
217             continue;
218         }
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);
225
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()));
230                 }
231             }
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()));
236                 }
237             }
238         }
239     }
240 }
241 void LocaleAliasTest::TestUResourceBundle() {
242
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!");
249             continue;
250         }
251         logln("\nold locale:%s   new locale:%s",oldLoc.getName(),newLoc.getName());
252
253         ResourceBundle* rb1 = NULL;
254         ResourceBundle* rb2 = NULL;
255
256         const char* testdatapath=loadTestData(status);
257
258         UnicodeString us1("NULL");
259         UnicodeString us2("NULL");
260         rb1 = new ResourceBundle(testdatapath, oldLoc, status);
261         if (U_FAILURE(U_ZERO_ERROR)) {
262
263         } else {
264             us1 = rb1->getStringEx("locale", status);
265         }
266         rb2 = new ResourceBundle(testdatapath, newLoc, status);
267         if (U_FAILURE(U_ZERO_ERROR)){
268
269         } else {
270             us2 = rb2->getStringEx("locale", status);
271         }
272         UnicodeString uNewLoc(newLoc.getName());
273         if (us1.compare(uNewLoc)!=0 || us1.compare(us2)!=0 || status!=U_ZERO_ERROR) {
274
275         }
276         log("UResourceBundle(getStringEx) old:");
277         log(us1);
278         log("   new:");
279         logln(us2);
280
281         if (rb1!=NULL) {
282             delete rb1;
283         }
284         if (rb2!=NULL) {
285             delete rb2;
286         }
287     }
288
289 }