]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/aliastst.cpp
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / test / intltest / aliastst.cpp
CommitLineData
73c04bcf
A
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2005-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "aliastst.h"
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"
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17
18const char* _LOCALE_ALIAS[][2] = {
19 {"in", "id"},
20 {"in_ID", "id_ID"},
21 {"iw", "he"},
22 {"iw_IL", "he_IL"},
23 {"ji", "yi"},
24 {"en_BU", "en_MM"},
25 {"en_DY", "en_BJ"},
26 {"en_HV", "en_BF"},
27 {"en_NH", "en_VU"},
28 {"en_RH", "en_ZW"},
29 {"en_TP", "en_TL"},
30 {"en_ZR", "en_CD"}
31};
32
33const int _LOCALE_NUMBER = 12;
34
35void LocaleAliasTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ){
36 switch (index) {
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:
44
45 default:
46 if (index <= 5) { // keep this in sync with the last index!
47 name = "(test omitted by !UCONFIG_NO_FORMATTING)";
48 } else {
49 name = "";
50 }
51 break; //needed to end loop
52 }
53}
54
55void 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!");
63 continue;
64 }
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);
68
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));
74 }
75 if (strcmp(l1, l2)!=0) {
76 errln("CalendarTest: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
77 }
78 if(!(c1==c2)){
79 errln("CalendarTest: c1!=c2. newLoc= "+UnicodeString(newLoc.getName()) +" oldLoc= "+UnicodeString(oldLoc.getName()));
80 }
81 logln("Calendar(getLocale) old:"+UnicodeString(l1)+" new:"+UnicodeString(l2));
82 delete c1;
83 delete c2;
84 }
85#endif
86}
87void 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!");
95 continue;
96 }
97 logln("\nold locale:%s new locale:%s",oldLoc.getName(),newLoc.getName());
98
99 DateFormat* df1 = DateFormat::createDateInstance(DateFormat::FULL, oldLoc);
100 DateFormat* df2 = DateFormat::createDateInstance(DateFormat::FULL, newLoc);
101
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));
107 }
108 if (strcmp(l1, l2)!=0) {
109 errln("TestDateFormat: l1!=l2: l1= "+UnicodeString(l1) +" l2= "+UnicodeString(l2));
110 }
111 if(!(df1==df2)){
112 errln("TestDateFormat: c1!=c2. newLoc= "+UnicodeString(newLoc.getName()) +" oldLoc= "+UnicodeString(oldLoc.getName()));
113 }
114 logln("DateFormat(getLocale) old:%s new:%s",l1,l2);
115
116 delete df1;
117 delete df2;
118 }
119#endif
120}
121void 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!");
129 continue;
130 }
131 logln("\nold locale:%s new locale:%s",oldLoc.getName(),newLoc.getName());
132
133 Collator* c1 = Collator::createInstance(oldLoc, status);
134 Collator* c2 = Collator::createInstance(newLoc, status);
135
136 Locale l1 = c1->getLocale(ULOC_VALID_LOCALE, status);
137 Locale l2 = c2->getLocale(ULOC_VALID_LOCALE, status);
138
139 if (strcmp(newLoc.getName(), l1.getName())!=0) {
140 errln("CalendarTest: newLoc!=l1: newLoc= "+UnicodeString(newLoc.getName()) +" l1= "+UnicodeString(l1.getName()));
141 }
142 if (strcmp(l1.getName(), l2.getName())!=0) {
143 errln("CollationTest: l1!=l2: l1= "+UnicodeString(l1.getName()) +" l2= "+UnicodeString(l2.getName()));
144 }
145 if(!(c1==c2)){
146 errln("CollationTest: c1!=c2. newLoc= "+UnicodeString(newLoc.getName()) +" oldLoc= "+UnicodeString(oldLoc.getName()));
147 }
148 logln("Collator(getLocale) old:%s new:%s", l1.getName(), l2.getName());
149 delete c1;
150 delete c2;
151 }
152#endif
153}
154void 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!");
160 continue;
161 }
162 logln("\nold locale:%s new locale:%s",oldLoc.getName(),newLoc.getName());
163
164 UnicodeString name1, name2;
165 oldLoc.getDisplayName(name1);
166 newLoc.getDisplayName(name2);
167 if (name1!=name2) {
168 errln("DisplayNames are not equal. newLoc= "+UnicodeString(newLoc.getName()) +" oldLoc= "+UnicodeString(oldLoc.getName()));
169 }
170 log("ULocale(getDisplayName) old:");
171 log(name1);
172 log(" new:");
173 logln(name2);
174 }
175}
176LocaleAliasTest::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));
181 resIndex=NULL;
182 }
183 defLocale = Locale::getDefault();
184 Locale::setDefault(Locale::getUS(), status);
185}
186LocaleAliasTest::~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));
193 }
194}
195UBool LocaleAliasTest::isLocaleAvailable(const char* loc){
196 if(resIndex==NULL){
197 return FALSE;
198 }
199 UErrorCode status = U_ZERO_ERROR;
200 int32_t len = 0;
201 ures_getStringByKey(resIndex, loc,&len, &status);
202 if(U_FAILURE(status)){
203 return FALSE;
204 }
205 return TRUE;
206}
207void 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!");
215 continue;
216 }
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);
223
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()));
228 }
229 }
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()));
234 }
235 }
236 }
237 }
238}
239void LocaleAliasTest::TestUResourceBundle() {
240
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!");
247 continue;
248 }
249 logln("\nold locale:%s new locale:%s",oldLoc.getName(),newLoc.getName());
250
251 ResourceBundle* rb1 = NULL;
252 ResourceBundle* rb2 = NULL;
253
254 const char* testdatapath=loadTestData(status);
255
256 UnicodeString us1("NULL");
257 UnicodeString us2("NULL");
258 rb1 = new ResourceBundle(testdatapath, oldLoc, status);
259 if (U_FAILURE(U_ZERO_ERROR)) {
260
261 } else {
262 us1 = rb1->getStringEx("locale", status);
263 }
264 rb2 = new ResourceBundle(testdatapath, newLoc, status);
265 if (U_FAILURE(U_ZERO_ERROR)){
266
267 } else {
268 us2 = rb2->getStringEx("locale", status);
269 }
270 UnicodeString uNewLoc(newLoc.getName());
271 if (us1.compare(uNewLoc)!=0 || us1.compare(us2)!=0 || status!=U_ZERO_ERROR) {
272
273 }
274 log("UResourceBundle(getStringEx) old:");
275 log(us1);
276 log(" new:");
277 logln(us2);
278
279 if (rb1!=NULL) {
280 delete rb1;
281 }
282 if (rb2!=NULL) {
283 delete rb2;
284 }
285 }
286
287}