]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/locnmtst.cpp
ICU-511.35.tar.gz
[apple/icu.git] / icuSources / test / intltest / locnmtst.cpp
CommitLineData
729e4ab9
A
1/*********************************************************************
2 * COPYRIGHT:
4388f060 3 * Copyright (c) 2010-2012, International Business Machines Corporation and
729e4ab9
A
4 * others. All Rights Reserved.
5 *********************************************************************/
6
7#include "locnmtst.h"
8#include "cstring.h"
9
10/*
11 Usage:
12 test_assert( Test (should be TRUE) )
13
14 Example:
15 test_assert(i==3);
16
17 the macro is ugly but makes the tests pretty.
18*/
19
20#define test_assert(test) \
21 { \
22 if(!(test)) \
23 errln("FAIL: " #test " was not true. In " __FILE__ " on line %d", __LINE__ ); \
24 else \
25 logln("PASS: asserted " #test); \
26 }
27
28/*
29 Usage:
30 test_assert_print( Test (should be TRUE), printable )
31
32 Example:
33 test_assert(i==3, toString(i));
34
35 the macro is ugly but makes the tests pretty.
36*/
37
38#define test_assert_print(test,print) \
39 { \
40 if(!(test)) \
41 errln("FAIL: " #test " was not true. " + UnicodeString(print) ); \
42 else \
43 logln("PASS: asserted " #test "-> " + UnicodeString(print)); \
44 }
45
46#define test_assert_equal(target,value) \
47 { \
48 if (UnicodeString(target)!=(value)) { \
49 logln("unexpected value '" + (value) + "'"); \
50 dataerrln("FAIL: " #target " == " #value " was not true. In " __FILE__ " on line %d", __LINE__); \
51 } else { \
51004dcb 52 logln("PASS: asserted " #target " == " #value); \
729e4ab9
A
53 } \
54 }
55
56#define test_dumpLocale(l) { logln(#l " = " + UnicodeString(l.getName(), "")); }
57
58LocaleDisplayNamesTest::LocaleDisplayNamesTest() {
59}
60
61LocaleDisplayNamesTest::~LocaleDisplayNamesTest() {
62}
63
64void LocaleDisplayNamesTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
51004dcb 65 char* /*par*/) {
729e4ab9
A
66 switch (index) {
67#if !UCONFIG_NO_FORMATTING
68 TESTCASE(0, TestCreate);
69 TESTCASE(1, TestCreateDialect);
51004dcb
A
70 TESTCASE(2, TestWithKeywordsAndEverything);
71 TESTCASE(3, TestUldnOpen);
72 TESTCASE(4, TestUldnOpenDialect);
73 TESTCASE(5, TestUldnWithKeywordsAndEverything);
74 TESTCASE(6, TestUldnComponents);
75 TESTCASE(7, TestRootEtc);
4388f060
A
76 TESTCASE(8, TestKeywords);
77 TESTCASE(9, TestUntranslatedKeywords);
78 TESTCASE(10, TestPrivateUse);
51004dcb 79 TESTCASE(11, TestUldnDisplayContext);
729e4ab9
A
80#endif
81 default:
51004dcb
A
82 name = "";
83 break;
729e4ab9
A
84 }
85}
86
87#if !UCONFIG_NO_FORMATTING
88void LocaleDisplayNamesTest::TestCreate() {
89 UnicodeString temp;
90 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getGermany());
91 ldn->localeDisplayName("de_DE", temp);
92 delete ldn;
93 test_assert_equal("Deutsch (Deutschland)", temp);
94}
95
96void LocaleDisplayNamesTest::TestCreateDialect() {
97 UnicodeString temp;
98 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES);
99 ldn->localeDisplayName("en_GB", temp);
100 delete ldn;
101 test_assert_equal("British English", temp);
102}
103
104void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
105 UnicodeString temp;
106 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
107 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
4388f060
A
108 const char *target = "English (Traditional, United States, VALLEY, "
109 "Gregorian Calendar, Phonebook Sort Order)";
110 ldn->localeDisplayName(locname, temp);
111 delete ldn;
112 test_assert_equal(target, temp);
113}
114
115void LocaleDisplayNamesTest::TestKeywords() {
116 UnicodeString temp;
117 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
118 const char *locname = "de@currency=XYZ";
119 const char *target = "German (Currency: XYZ)";
120 ldn->localeDisplayName(locname, temp);
121 delete ldn;
122 test_assert_equal(target, temp);
123}
124
125void LocaleDisplayNamesTest::TestUntranslatedKeywords() {
126 UnicodeString temp;
127 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
128 const char *locname = "de@foo=bar";
129 const char *target = "German (foo=bar)";
130 ldn->localeDisplayName(locname, temp);
131 delete ldn;
132 test_assert_equal(target, temp);
133}
134
135void LocaleDisplayNamesTest::TestPrivateUse() {
136 UnicodeString temp;
137 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
138 const char *locname = "de@x=foobar";
139 const char *target = "German (Private-Use: foobar)";
729e4ab9
A
140 ldn->localeDisplayName(locname, temp);
141 delete ldn;
142 test_assert_equal(target, temp);
143}
144
145void LocaleDisplayNamesTest::TestUldnOpen() {
146 UErrorCode status = U_ZERO_ERROR;
147 const int32_t kMaxResultSize = 150; // long enough
148 UChar result[150];
149 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
150 int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status);
151 uldn_close(ldn);
152 test_assert(U_SUCCESS(status));
153
154 UnicodeString str(result, len, kMaxResultSize);
155 test_assert_equal("Deutsch (Deutschland)", str);
156
157 // make sure that NULL gives us the default locale as usual
158 ldn = uldn_open(NULL, ULDN_STANDARD_NAMES, &status);
159 const char *locale = uldn_getLocale(ldn);
160 if(0 != uprv_strcmp(uloc_getDefault(), locale)) {
161 errln("uldn_getLocale(uldn_open(NULL))=%s != default locale %s\n", locale, uloc_getDefault());
162 }
163 uldn_close(ldn);
164 test_assert(U_SUCCESS(status));
165}
166
167void LocaleDisplayNamesTest::TestUldnOpenDialect() {
168 UErrorCode status = U_ZERO_ERROR;
169 const int32_t kMaxResultSize = 150; // long enough
170 UChar result[150];
171 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
172 int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status);
173 uldn_close(ldn);
174 test_assert(U_SUCCESS(status));
175
176 UnicodeString str(result, len, kMaxResultSize);
177 test_assert_equal("British English", str);
178}
179
180void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
181 UErrorCode status = U_ZERO_ERROR;
182 const int32_t kMaxResultSize = 150; // long enough
183 UChar result[150];
184 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
4388f060
A
185 const char *target = "English (Traditional, United States, VALLEY, "
186 "Gregorian Calendar, Phonebook Sort Order)";
729e4ab9
A
187 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status);
188 int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status);
189 uldn_close(ldn);
190 test_assert(U_SUCCESS(status));
191
192 UnicodeString str(result, len, kMaxResultSize);
193 test_assert_equal(target, str);
194}
195
196void LocaleDisplayNamesTest::TestUldnComponents() {
197 UErrorCode status = U_ZERO_ERROR;
198 const int32_t kMaxResultSize = 150; // long enough
199 UChar result[150];
200
201 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
202 test_assert(U_SUCCESS(status));
203 if (U_FAILURE(status)) {
204 return;
205 }
206
207 // "en_Hant_US_PRE_EURO@calendar=gregorian";
208
209 {
210 int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status);
211 UnicodeString str(result, len, kMaxResultSize);
212 test_assert_equal("Englisch", str);
213 }
214
215
216 {
217 int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status);
218 UnicodeString str(result, len, kMaxResultSize);
4388f060 219 test_assert_equal("Traditionell", str);
729e4ab9
A
220 }
221
222 {
223 int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize,
51004dcb 224 &status);
729e4ab9 225 UnicodeString str(result, len, kMaxResultSize);
4388f060 226 test_assert_equal("Traditionell", str);
729e4ab9
A
227 }
228
229 {
230 int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status);
231 UnicodeString str(result, len, kMaxResultSize);
232 test_assert_equal("Vereinigte Staaten", str);
233 }
234
235 {
236 int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status);
237 UnicodeString str(result, len, kMaxResultSize);
238 test_assert_equal("PRE_EURO", str);
239 }
240
241 {
242 int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status);
243 UnicodeString str(result, len, kMaxResultSize);
244 test_assert_equal("Kalender", str);
245 }
246
247 {
248 int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result,
51004dcb 249 kMaxResultSize, &status);
729e4ab9
A
250 UnicodeString str(result, len, kMaxResultSize);
251 test_assert_equal("Gregorianischer Kalender", str);
252 }
253
254 uldn_close(ldn);
255}
256
51004dcb
A
257
258typedef struct {
259 const char * displayLocale;
260 UDisplayContext dialectHandling;
261 UDisplayContext capitalization;
262 const char * localeToBeNamed;
263 const UChar * result;
264} LocNameDispContextItem;
265
266static char en[] = "en";
267static char en_US[] = "en_US";
268
269static UChar daFor_en[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"engelsk"
270static UChar daFor_en_US[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x53,0x41,0x29,0}; //"engelsk (USA)"
271static UChar daFor_en_US_D[] = {0x61,0x6D,0x65,0x72,0x69,0x6B,0x61,0x6E,0x73,0x6B,0x20,0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"amerikansk engelsk"
272static UChar esFor_en[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0}; //"ingles" with acute on e
273static UChar esFor_en_US[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x45,0x73,0x74,0x61,0x64,0x6F,0x73,0x20,0x55,0x6E,0x69,0x64,0x6F,0x73,0x29,0}; //"ingles (Estados Unidos)" ...
274static UChar esFor_en_US_D[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x65,0x73,0x74,0x61,0x64,0x6F,0x75,0x6E,0x69,0x64,0x65,0x6E,0x73,0x65,0}; //"ingles estadounidense" ...
275#if !UCONFIG_NO_BREAK_ITERATION
276static UChar daFor_en_T[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Engelsk"
277static UChar daFor_en_US_T[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x53,0x41,0x29,0}; //"Engelsk (USA)"
278static UChar daFor_en_US_DT[] = {0x41,0x6D,0x65,0x72,0x69,0x6B,0x61,0x6E,0x73,0x6B,0x20,0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Amerikansk engelsk"
279static UChar esFor_en_T[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0}; //"Ingles" ...
280static UChar esFor_en_US_T[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x45,0x73,0x74,0x61,0x64,0x6F,0x73,0x20,0x55,0x6E,0x69,0x64,0x6F,0x73,0x29,0}; //"Ingles (Estados Unidos)" ...
281static UChar esFor_en_US_DT[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x65,0x73,0x74,0x61,0x64,0x6F,0x75,0x6E,0x69,0x64,0x65,0x6E,0x73,0x65,0}; //"Ingles estadounidense" ...
282#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
283
284static const LocNameDispContextItem ctxtItems[] = {
285 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en, daFor_en },
286 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en_US, daFor_en_US },
287 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en_US, daFor_en_US_D },
288 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en, esFor_en },
289 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en_US, esFor_en_US },
290 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, en_US, esFor_en_US_D },
291#if !UCONFIG_NO_BREAK_ITERATION
292 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en, daFor_en_T },
293 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en_US, daFor_en_US_T },
294 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en_US, daFor_en_US_DT },
295 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en, esFor_en_T },
296 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en_US, esFor_en_US_T },
297 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, en_US, esFor_en_US_DT },
298
299 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, en, daFor_en },
300 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, en_US, daFor_en_US },
301 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, en_US, daFor_en_US_D },
302 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, en, esFor_en_T },
303 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, en_US, esFor_en_US_T },
304 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, en_US, esFor_en_US_DT },
305 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
306 { NULL, (UDisplayContext)0, (UDisplayContext)0, NULL, NULL }
307};
308
309void LocaleDisplayNamesTest::TestUldnDisplayContext() {
310 const LocNameDispContextItem * ctxtItemPtr;
311 for (ctxtItemPtr = ctxtItems; ctxtItemPtr->displayLocale != NULL; ctxtItemPtr++) {
312 UDisplayContext contexts[2] = {ctxtItemPtr->dialectHandling, ctxtItemPtr->capitalization};
313 UErrorCode status = U_ZERO_ERROR;
314 ULocaleDisplayNames * uldn = uldn_openForContext(ctxtItemPtr->displayLocale, contexts, 2, &status);
315 if (U_FAILURE(status)) {
316 errln(UnicodeString("FAIL: uldn_openForContext failed for locale ") + ctxtItemPtr->displayLocale +
317 ", dialectHandling " + ctxtItemPtr->dialectHandling + ", capitalization " + ctxtItemPtr->capitalization);
318 } else {
319 UDisplayContext dialectHandling = uldn_getContext(uldn, UDISPCTX_TYPE_DIALECT_HANDLING, &status);
320 UDisplayContext capitalization = uldn_getContext(uldn, UDISPCTX_TYPE_CAPITALIZATION, &status);
321 if (U_FAILURE(status)) {
322 errln(UnicodeString("FAIL: uldn_getContext status ") + (int)status);
323 } else if (dialectHandling != ctxtItemPtr->dialectHandling || capitalization != ctxtItemPtr->capitalization) {
324 errln("FAIL: uldn_getContext retrieved incorrect dialectHandling or capitalization");
325 } else {
326 UChar nameBuf[ULOC_FULLNAME_CAPACITY];
327 int32_t len = uldn_localeDisplayName(uldn, ctxtItemPtr->localeToBeNamed, nameBuf, ULOC_FULLNAME_CAPACITY, &status);
328 if (U_FAILURE(status)) {
329 dataerrln(UnicodeString("FAIL: uldn_localeDisplayName status: ") + u_errorName(status));
330 } else if (u_strcmp(ctxtItemPtr->result, nameBuf) != 0) {
331 UnicodeString exp(ctxtItemPtr->result, u_strlen(ctxtItemPtr->result));
332 UnicodeString got(nameBuf, len);
333 dataerrln(UnicodeString("FAIL: uldn_localeDisplayName, capitalization ") + ctxtItemPtr->capitalization +
334 ", expected " + exp + ", got " + got );
335 }
336 }
337 uldn_close(uldn);
338 }
339 }
340}
341
729e4ab9
A
342void LocaleDisplayNamesTest::TestRootEtc() {
343 UnicodeString temp;
344 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
345 const char *locname = "@collation=phonebook";
4388f060 346 const char *target = "Root (Phonebook Sort Order)";
729e4ab9
A
347 ldn->localeDisplayName(locname, temp);
348 test_assert_equal(target, temp);
349
350 ldn->languageDisplayName("root", temp);
351 test_assert_equal("root", temp);
352
353 ldn->languageDisplayName("en_GB", temp);
354 test_assert_equal("en_GB", temp);
355
356 delete ldn;
357}
358
359#endif /* UCONFIG_NO_FORMATTING */