1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2010-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *********************************************************************/
8
9 #include "locnmtst.h"
10 #include "unicode/ustring.h"
11 #include "cstring.h"
12
13 /*
14 Usage:
15 test_assert( Test (should be TRUE) )
16
17 Example:
18 test_assert(i==3);
19
20 the macro is ugly but makes the tests pretty.
21 */
22
23 #define test_assert(test) UPRV_BLOCK_MACRO_BEGIN { \
24 if(!(test)) \
25 errln("FAIL: " #test " was not true. In " __FILE__ " on line %d", __LINE__ ); \
26 else \
27 logln("PASS: asserted " #test); \
28 } UPRV_BLOCK_MACRO_END
29
30 /*
31 Usage:
32 test_assert_print( Test (should be TRUE), printable )
33
34 Example:
35 test_assert(i==3, toString(i));
36
37 the macro is ugly but makes the tests pretty.
38 */
39
40 #define test_assert_print(test,print) UPRV_BLOCK_MACRO_BEGIN { \
41 if(!(test)) \
42 errln("FAIL: " #test " was not true. " + UnicodeString(print) ); \
43 else \
44 logln("PASS: asserted " #test "-> " + UnicodeString(print)); \
45 } UPRV_BLOCK_MACRO_END
46
47 #define test_assert_equal(target,value) UPRV_BLOCK_MACRO_BEGIN { \
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 { \
52 logln("PASS: asserted " #target " == " #value); \
53 } \
54 } UPRV_BLOCK_MACRO_END
55
56 #define test_dumpLocale(l) UPRV_BLOCK_MACRO_BEGIN { \
57 logln(#l " = " + UnicodeString(l.getName(), "")); \
58 } UPRV_BLOCK_MACRO_END
59
LocaleDisplayNamesTest()60 LocaleDisplayNamesTest::LocaleDisplayNamesTest() {
61 }
62
~LocaleDisplayNamesTest()63 LocaleDisplayNamesTest::~LocaleDisplayNamesTest() {
64 }
65
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)66 void LocaleDisplayNamesTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
67 char* /*par*/) {
68 switch (index) {
69 #if !UCONFIG_NO_FORMATTING
70 TESTCASE(0, TestCreate);
71 TESTCASE(1, TestCreateDialect);
72 TESTCASE(2, TestWithKeywordsAndEverything);
73 TESTCASE(3, TestUldnOpen);
74 TESTCASE(4, TestUldnOpenDialect);
75 TESTCASE(5, TestUldnWithKeywordsAndEverything);
76 TESTCASE(6, TestUldnComponents);
77 TESTCASE(7, TestRootEtc);
78 TESTCASE(8, TestCurrencyKeyword);
79 TESTCASE(9, TestUnknownCurrencyKeyword);
80 TESTCASE(10, TestUntranslatedKeywords);
81 TESTCASE(11, TestPrivateUse);
82 TESTCASE(12, TestUldnDisplayContext);
83 TESTCASE(13, TestUldnWithGarbage);
84 TESTCASE(14, TestSubstituteHandling);
85 #endif
86 default:
87 name = "";
88 break;
89 }
90 }
91
92 #if !UCONFIG_NO_FORMATTING
TestCreate()93 void LocaleDisplayNamesTest::TestCreate() {
94 UnicodeString temp;
95 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getGermany());
96 ldn->localeDisplayName("de_DE", temp);
97 delete ldn;
98 test_assert_equal("Deutsch (Deutschland)", temp);
99 }
100
TestCreateDialect()101 void LocaleDisplayNamesTest::TestCreateDialect() {
102 UnicodeString temp;
103 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES);
104 ldn->localeDisplayName("en_GB", temp);
105 delete ldn;
106 test_assert_equal("British English", temp);
107 }
108
TestWithKeywordsAndEverything()109 void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
110 UnicodeString temp;
111 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
112 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
113 const char *target = "English (Traditional, United States, VALLEY, "
114 "Gregorian Calendar, Phonebook Sort Order)";
115 ldn->localeDisplayName(locname, temp);
116 delete ldn;
117 test_assert_equal(target, temp);
118 }
119
TestCurrencyKeyword()120 void LocaleDisplayNamesTest::TestCurrencyKeyword() {
121 UnicodeString temp;
122 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
123 const char *locname = "ja@currency=JPY";
124 const char *target = "Japanese (Japanese Yen)";
125 ldn->localeDisplayName(locname, temp);
126 delete ldn;
127 test_assert_equal(target, temp);
128 }
129
TestUnknownCurrencyKeyword()130 void LocaleDisplayNamesTest::TestUnknownCurrencyKeyword() {
131 UnicodeString temp;
132 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
133 const char *locname = "de@currency=XYZ";
134 const char *target = "German (Currency: XYZ)";
135 ldn->localeDisplayName(locname, temp);
136 delete ldn;
137 test_assert_equal(target, temp);
138 }
139
TestUntranslatedKeywords()140 void LocaleDisplayNamesTest::TestUntranslatedKeywords() {
141 UnicodeString temp;
142 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
143 const char *locname = "de@foo=bar";
144 const char *target = "German (foo=bar)";
145 ldn->localeDisplayName(locname, temp);
146 delete ldn;
147 test_assert_equal(target, temp);
148 }
149
TestPrivateUse()150 void LocaleDisplayNamesTest::TestPrivateUse() {
151 UnicodeString temp;
152 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
153 const char *locname = "de@x=foobar";
154 const char *target = "German (Private-Use: foobar)";
155 ldn->localeDisplayName(locname, temp);
156 delete ldn;
157 test_assert_equal(target, temp);
158 }
159
TestUldnOpen()160 void LocaleDisplayNamesTest::TestUldnOpen() {
161 UErrorCode status = U_ZERO_ERROR;
162 const int32_t kMaxResultSize = 150; // long enough
163 UChar result[150];
164 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
165 int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status);
166 uldn_close(ldn);
167 test_assert(U_SUCCESS(status));
168
169 UnicodeString str(result, len, kMaxResultSize);
170 test_assert_equal("Deutsch (Deutschland)", str);
171
172 // make sure that NULL gives us the default locale as usual
173 ldn = uldn_open(NULL, ULDN_STANDARD_NAMES, &status);
174 const char *locale = uldn_getLocale(ldn);
175 if(0 != uprv_strcmp(uloc_getDefault(), locale)) {
176 errln("uldn_getLocale(uldn_open(NULL))=%s != default locale %s\n", locale, uloc_getDefault());
177 }
178 uldn_close(ldn);
179 test_assert(U_SUCCESS(status));
180 }
181
TestUldnOpenDialect()182 void LocaleDisplayNamesTest::TestUldnOpenDialect() {
183 UErrorCode status = U_ZERO_ERROR;
184 const int32_t kMaxResultSize = 150; // long enough
185 UChar result[150];
186 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
187 int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status);
188 uldn_close(ldn);
189 test_assert(U_SUCCESS(status));
190
191 UnicodeString str(result, len, kMaxResultSize);
192 test_assert_equal("British English", str);
193 }
194
TestUldnWithGarbage()195 void LocaleDisplayNamesTest::TestUldnWithGarbage() {
196 UErrorCode status = U_ZERO_ERROR;
197 const int32_t kMaxResultSize = 150; // long enough
198 UChar result[150];
199 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
200 int32_t len = uldn_localeDisplayName(ldn, "english (United States) [w", result, kMaxResultSize, &status);
201 uldn_close(ldn);
202 test_assert(U_FAILURE(status) && len == 0);
203 }
204
TestUldnWithKeywordsAndEverything()205 void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
206 UErrorCode status = U_ZERO_ERROR;
207 const int32_t kMaxResultSize = 150; // long enough
208 UChar result[150];
209 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
210 const char *target = "English (Traditional, United States, VALLEY, "
211 "Gregorian Calendar, Phonebook Sort Order)";
212 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status);
213 int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status);
214 uldn_close(ldn);
215 test_assert(U_SUCCESS(status));
216
217 UnicodeString str(result, len, kMaxResultSize);
218 test_assert_equal(target, str);
219 }
220
TestUldnComponents()221 void LocaleDisplayNamesTest::TestUldnComponents() {
222 UErrorCode status = U_ZERO_ERROR;
223 const int32_t kMaxResultSize = 150; // long enough
224 UChar result[150];
225
226 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
227 test_assert(U_SUCCESS(status));
228 if (U_FAILURE(status)) {
229 return;
230 }
231
232 // "en_Hant_US_PRE_EURO@calendar=gregorian";
233
234 {
235 int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status);
236 UnicodeString str(result, len, kMaxResultSize);
237 test_assert_equal("Englisch", str);
238 }
239
240
241 {
242 int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status);
243 UnicodeString str(result, len, kMaxResultSize);
244 test_assert_equal("Traditionell", str);
245 }
246
247 {
248 int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize,
249 &status);
250 UnicodeString str(result, len, kMaxResultSize);
251 test_assert_equal("Traditionell", str);
252 }
253
254 {
255 int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status);
256 UnicodeString str(result, len, kMaxResultSize);
257 test_assert_equal("Vereinigte Staaten", str);
258 }
259
260 {
261 int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status);
262 UnicodeString str(result, len, kMaxResultSize);
263 test_assert_equal("PRE_EURO", str);
264 }
265
266 {
267 int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status);
268 UnicodeString str(result, len, kMaxResultSize);
269 test_assert_equal("Kalender", str);
270 }
271
272 {
273 int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result,
274 kMaxResultSize, &status);
275 UnicodeString str(result, len, kMaxResultSize);
276 test_assert_equal("Gregorianischer Kalender", str);
277 }
278
279 uldn_close(ldn);
280 }
281
282
283 typedef struct {
284 const char * displayLocale;
285 UDisplayContext dialectHandling;
286 UDisplayContext capitalization;
287 UDisplayContext displayLength;
288 const char * localeToBeNamed;
289 const UChar * result;
290 } LocNameDispContextItem;
291
292 static char en[] = "en";
293 static char en_cabud[] = "en@calendar=buddhist";
294 static char en_GB[] = "en_GB";
295 static char uz_Latn[] = "uz_Latn";
296
297 static UChar daFor_en[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"engelsk"
298 static UChar daFor_en_cabud[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x62,0x75,0x64,0x64,0x68,0x69,0x73,0x74,0x69,0x73,0x6B,0x20,
299 0x6B,0x61,0x6C,0x65,0x6E,0x64,0x65,0x72,0x29,0}; //"engelsk (buddhistisk kalender)"
300 static UChar daFor_en_GB[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x53,0x74,0x6F,0x72,0x62,0x72,0x69,0x74,0x61,0x6E,0x6E,0x69,0x65,0x6E,0x29,0}; //"engelsk (Storbritannien)"
301 static UChar daFor_en_GB_S[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x4B,0x29,0}; //"engelsk (UK)"
302 static UChar daFor_en_GB_D[] = {0x62,0x72,0x69,0x74,0x69,0x73,0x6B,0x20,0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"britisk engelsk"
303 static UChar esFor_en[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0}; //"ingles" with acute on the e
304 static UChar esFor_en_GB[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x52,0x65,0x69,0x6E,0x6F,0x20,0x55,0x6E,0x69,0x64,0x6F,0x29,0}; //"ingles (Reino Unido)" ...
305 static UChar esFor_en_GB_S[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x52,0x55,0x29,0}; //"ingles (RU)" ...
306 static UChar esFor_en_GB_D[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x62,0x72,0x69,0x74,0xE1,0x6E,0x69,0x63,0x6F,0}; //"ingles britanico" with acute on the e, a
307 static UChar ruFor_uz_Latn[] = {0x0443,0x0437,0x0431,0x0435,0x043A,0x0441,0x043A,0x0438,0x0439,0x20,0x28,0x043B,0x0430,0x0442,0x0438,0x043D,0x0438,0x0446,0x0430,0x29,0}; // all lowercase
308 #if !UCONFIG_NO_BREAK_ITERATION
309 static UChar daFor_en_T[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Engelsk"
310 static UChar daFor_en_cabudT[]= {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x62,0x75,0x64,0x64,0x68,0x69,0x73,0x74,0x69,0x73,0x6B,0x20,
311 0x6B,0x61,0x6C,0x65,0x6E,0x64,0x65,0x72,0x29,0}; //"Engelsk (buddhistisk kalender)"
312 static UChar daFor_en_GB_T[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x53,0x74,0x6F,0x72,0x62,0x72,0x69,0x74,0x61,0x6E,0x6E,0x69,0x65,0x6E,0x29,0}; //"Engelsk (Storbritannien)"
313 static UChar daFor_en_GB_ST[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x4B,0x29,0}; //"Engelsk (UK)"
314 static UChar daFor_en_GB_DT[] = {0x42,0x72,0x69,0x74,0x69,0x73,0x6B,0x20,0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Britisk engelsk"
315 static UChar esFor_en_T[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0}; //"Ingles" with acute on the e
316 static UChar esFor_en_GB_T[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x52,0x65,0x69,0x6E,0x6F,0x20,0x55,0x6E,0x69,0x64,0x6F,0x29,0}; //"Ingles (Reino Unido)" ...
317 static UChar esFor_en_GB_ST[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x52,0x55,0x29,0}; //"Ingles (RU)" ...
318 static UChar esFor_en_GB_DT[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x62,0x72,0x69,0x74,0xE1,0x6E,0x69,0x63,0x6F,0}; //"Ingles britanico" with acute on the e, a
319 static UChar ruFor_uz_Latn_T[]= {0x0423,0x0437,0x0431,0x0435,0x043A,0x0441,0x043A,0x0438,0x0439,0x20,0x28,0x043B,0x0430,0x0442,0x0438,0x043D,0x0438,0x0446,0x0430,0x29,0}; // first char upper
320 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
321
322 static const LocNameDispContextItem ctxtItems[] = {
323 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en, daFor_en },
324 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_cabud, daFor_en_cabud },
325 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB },
326 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_SHORT, en_GB, daFor_en_GB_S },
327 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_D },
328 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en, esFor_en },
329 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB },
330 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_SHORT, en_GB, esFor_en_GB_S },
331 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_D },
332 { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, UDISPCTX_LENGTH_FULL, uz_Latn, ruFor_uz_Latn },
333 #if !UCONFIG_NO_BREAK_ITERATION
334 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en, daFor_en_T },
335 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_cabud, daFor_en_cabudT },
336 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_T },
337 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_SHORT, en_GB, daFor_en_GB_ST },
338 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_DT },
339 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en, esFor_en_T },
340 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_T },
341 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_SHORT, en_GB, esFor_en_GB_ST },
342 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_DT },
343 { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL, uz_Latn, ruFor_uz_Latn_T },
344
345 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en, daFor_en_T },
346 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en_cabud, daFor_en_cabudT },
347 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_T },
348 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_SHORT, en_GB, daFor_en_GB_ST },
349 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_DT },
350 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en, esFor_en_T },
351 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_T },
352 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_SHORT, en_GB, esFor_en_GB_ST },
353 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_DT },
354 { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, UDISPCTX_LENGTH_FULL, uz_Latn, ruFor_uz_Latn_T },
355
356 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en, daFor_en },
357 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en_cabud, daFor_en_cabud },
358 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB },
359 { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_SHORT, en_GB, daFor_en_GB_S },
360 { "da", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en_GB, daFor_en_GB_D },
361 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en, esFor_en_T },
362 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_T },
363 { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_SHORT, en_GB, esFor_en_GB_ST },
364 { "es", UDISPCTX_DIALECT_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, en_GB, esFor_en_GB_DT },
365 { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, UDISPCTX_LENGTH_FULL, uz_Latn, ruFor_uz_Latn_T },
366 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
367 { NULL, (UDisplayContext)0, (UDisplayContext)0, (UDisplayContext)0, NULL, NULL }
368 };
369
TestUldnDisplayContext()370 void LocaleDisplayNamesTest::TestUldnDisplayContext() {
371 const LocNameDispContextItem * ctxtItemPtr;
372 for (ctxtItemPtr = ctxtItems; ctxtItemPtr->displayLocale != NULL; ctxtItemPtr++) {
373 UDisplayContext contexts[3] = {ctxtItemPtr->dialectHandling, ctxtItemPtr->capitalization, ctxtItemPtr->displayLength};
374 UErrorCode status = U_ZERO_ERROR;
375 ULocaleDisplayNames * uldn = uldn_openForContext(ctxtItemPtr->displayLocale, contexts, 3, &status);
376 if (U_FAILURE(status)) {
377 errln(UnicodeString("FAIL: uldn_openForContext failed for locale ") + ctxtItemPtr->displayLocale +
378 ", dialectHandling " + ctxtItemPtr->dialectHandling +
379 ", capitalization " + ctxtItemPtr->capitalization +
380 ", displayLength " + ctxtItemPtr->displayLength);
381 } else {
382 UDisplayContext dialectHandling = uldn_getContext(uldn, UDISPCTX_TYPE_DIALECT_HANDLING, &status);
383 UDisplayContext capitalization = uldn_getContext(uldn, UDISPCTX_TYPE_CAPITALIZATION, &status);
384 UDisplayContext displayLength = uldn_getContext(uldn, UDISPCTX_TYPE_DISPLAY_LENGTH, &status);
385 if (U_FAILURE(status)) {
386 errln(UnicodeString("FAIL: uldn_getContext status ") + (int)status);
387 } else if (dialectHandling != ctxtItemPtr->dialectHandling ||
388 capitalization != ctxtItemPtr->capitalization ||
389 displayLength != ctxtItemPtr->displayLength) {
390 errln("FAIL: uldn_getContext retrieved incorrect dialectHandling, capitalization, or displayLength");
391 } else {
392 UChar nameBuf[ULOC_FULLNAME_CAPACITY];
393 int32_t len = uldn_localeDisplayName(uldn, ctxtItemPtr->localeToBeNamed, nameBuf, ULOC_FULLNAME_CAPACITY, &status);
394 if (U_FAILURE(status)) {
395 dataerrln(UnicodeString("FAIL: uldn_localeDisplayName status: ") + u_errorName(status));
396 } else if (u_strcmp(ctxtItemPtr->result, nameBuf) != 0) {
397 UnicodeString exp(ctxtItemPtr->result, u_strlen(ctxtItemPtr->result));
398 UnicodeString got(nameBuf, len);
399 dataerrln(UnicodeString("FAIL: uldn_localeDisplayName, capitalization ") + ctxtItemPtr->capitalization +
400 ", expected " + exp + ", got " + got );
401 }
402 }
403 uldn_close(uldn);
404 }
405 }
406 }
407
TestRootEtc()408 void LocaleDisplayNamesTest::TestRootEtc() {
409 UnicodeString temp;
410 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
411 const char *locname = "@collation=phonebook";
412 const char *target = "Root (Phonebook Sort Order)";
413 ldn->localeDisplayName(locname, temp);
414 test_assert_equal(target, temp);
415
416 ldn->languageDisplayName("root", temp);
417 test_assert_equal("root", temp);
418
419 ldn->languageDisplayName("en_GB", temp);
420 test_assert_equal("en_GB", temp);
421
422 delete ldn;
423 }
424
425 static const char unknown_region[] = "wx";
426 static const char unknown_lang[] = "xy";
427 static const char unknown_script[] = "wxyz";
428 static const char unknown_variant[] = "abc";
429 static const char unknown_key[] = "efg";
430 static const char unknown_ca_value[] = "ijk";
431 static const char known_lang_unknown_script[] = "en-wxyz";
432 static const char unknown_lang_unknown_script[] = "xy-wxyz";
433 static const char unknown_lang_known_script[] = "xy-Latn";
434 static const char unknown_lang_unknown_region[] = "xy-wx";
435 static const char known_lang_unknown_region[] = "en-wx";
436 static const char unknown_lang_known_region[] = "xy-US";
437 static const char unknown_lang_unknown_script_unknown_region[] = "xy-wxyz-wx";
438 static const char known_lang_unknown_script_unknown_region[] = "en-wxyz-wx";
439 static const char unknown_lang_known_script_unknown_region[] = "xy-Latn-wx";
440 static const char unknown_lang_known_script_known_region[] = "xy-wxyz-US";
441 static const char known_lang[] = "en";
442 static const char known_lang_known_script[] = "en-Latn";
443 static const char known_lang_known_region[] = "en-US";
444 static const char known_lang_known_script_known_region[] = "en-Latn-US";
445
VerifySubstitute(LocaleDisplayNames * ldn)446 void LocaleDisplayNamesTest::VerifySubstitute(LocaleDisplayNames* ldn) {
447 UnicodeString temp;
448 // Ensure the default is UDISPCTX_SUBSTITUTE
449 UDisplayContext context = ldn->getContext(UDISPCTX_TYPE_SUBSTITUTE_HANDLING);
450 test_assert(UDISPCTX_SUBSTITUTE == context);
451
452 ldn->regionDisplayName(unknown_region, temp);
453 test_assert_equal(unknown_region, temp);
454 ldn->languageDisplayName(unknown_lang, temp);
455 test_assert_equal(unknown_lang, temp);
456 ldn->scriptDisplayName(unknown_script, temp);
457 test_assert_equal(unknown_script, temp);
458 ldn->variantDisplayName(unknown_variant, temp);
459 test_assert_equal(unknown_variant, temp);
460 ldn->keyDisplayName(unknown_key, temp);
461 test_assert_equal(unknown_key, temp);
462 ldn->keyValueDisplayName("ca", unknown_ca_value, temp);
463 test_assert_equal(unknown_ca_value, temp);
464
465 ldn->localeDisplayName(unknown_lang, temp);
466 test_assert_equal(unknown_lang, temp);
467 ldn->localeDisplayName(known_lang_unknown_script, temp);
468 test_assert_equal("Englisch (Wxyz)", temp);
469 ldn->localeDisplayName(unknown_lang_unknown_script, temp);
470 test_assert_equal("xy (Wxyz)", temp);
471 ldn->localeDisplayName(unknown_lang_known_script, temp);
472 test_assert_equal("xy (Lateinisch)", temp);
473 ldn->localeDisplayName(unknown_lang_unknown_region, temp);
474 test_assert_equal("xy (WX)", temp);
475 ldn->localeDisplayName(known_lang_unknown_region, temp);
476 test_assert_equal("Englisch (WX)", temp);
477 ldn->localeDisplayName(unknown_lang_known_region, temp);
478 test_assert_equal("xy (Vereinigte Staaten)", temp);
479 ldn->localeDisplayName(unknown_lang_unknown_script_unknown_region, temp);
480 test_assert_equal("xy (Wxyz, WX)", temp);
481 ldn->localeDisplayName(known_lang_unknown_script_unknown_region, temp);
482 test_assert_equal("Englisch (Wxyz, WX)", temp);
483 ldn->localeDisplayName(unknown_lang_known_script_unknown_region, temp);
484 test_assert_equal("xy (Lateinisch, WX)", temp);
485 ldn->localeDisplayName(unknown_lang_known_script_known_region, temp);
486 test_assert_equal("xy (Wxyz, Vereinigte Staaten)", temp);
487
488 ldn->localeDisplayName(known_lang, temp);
489 test_assert_equal("Englisch", temp);
490 ldn->localeDisplayName(known_lang_known_script, temp);
491 test_assert_equal("Englisch (Lateinisch)", temp);
492 ldn->localeDisplayName(known_lang_known_region, temp);
493 test_assert_equal("Englisch (Vereinigte Staaten)", temp);
494 ldn->localeDisplayName(known_lang_known_script_known_region, temp);
495 test_assert_equal("Englisch (Lateinisch, Vereinigte Staaten)", temp);
496 }
497
VerifyNoSubstitute(LocaleDisplayNames * ldn)498 void LocaleDisplayNamesTest::VerifyNoSubstitute(LocaleDisplayNames* ldn) {
499 UnicodeString temp("");
500 std::string utf8;
501 // Ensure the default is UDISPCTX_SUBSTITUTE
502 UDisplayContext context = ldn->getContext(UDISPCTX_TYPE_SUBSTITUTE_HANDLING);
503 test_assert(UDISPCTX_NO_SUBSTITUTE == context);
504
505 ldn->regionDisplayName(unknown_region, temp);
506 test_assert(TRUE == temp.isBogus());
507 ldn->languageDisplayName(unknown_lang, temp);
508 test_assert(TRUE == temp.isBogus());
509 ldn->scriptDisplayName(unknown_script, temp);
510 test_assert(TRUE == temp.isBogus());
511 ldn->variantDisplayName(unknown_variant, temp);
512 test_assert(TRUE == temp.isBogus());
513 ldn->keyDisplayName(unknown_key, temp);
514 test_assert(TRUE == temp.isBogus());
515 ldn->keyValueDisplayName("ca", unknown_ca_value, temp);
516 test_assert(TRUE == temp.isBogus());
517
518 ldn->localeDisplayName(unknown_lang, temp);
519 test_assert(TRUE == temp.isBogus());
520 ldn->localeDisplayName(known_lang_unknown_script, temp);
521 test_assert(TRUE == temp.isBogus());
522 ldn->localeDisplayName(unknown_lang_unknown_script, temp);
523 test_assert(TRUE == temp.isBogus());
524 ldn->localeDisplayName(unknown_lang_known_script, temp);
525 test_assert(TRUE == temp.isBogus());
526 ldn->localeDisplayName(unknown_lang_unknown_region, temp);
527 test_assert(TRUE == temp.isBogus());
528 ldn->localeDisplayName(known_lang_unknown_region, temp);
529 test_assert(TRUE == temp.isBogus());
530 ldn->localeDisplayName(unknown_lang_known_region, temp);
531 test_assert(TRUE == temp.isBogus());
532 ldn->localeDisplayName(unknown_lang_unknown_script_unknown_region, temp);
533 test_assert(TRUE == temp.isBogus());
534 ldn->localeDisplayName(known_lang_unknown_script_unknown_region, temp);
535 test_assert(TRUE == temp.isBogus());
536 ldn->localeDisplayName(unknown_lang_known_script_unknown_region, temp);
537 test_assert(TRUE == temp.isBogus());
538 ldn->localeDisplayName(unknown_lang_known_script_known_region, temp);
539 test_assert(TRUE == temp.isBogus());
540
541 ldn->localeDisplayName(known_lang, temp);
542 test_assert_equal("Englisch", temp);
543 ldn->localeDisplayName(known_lang_known_script, temp);
544 test_assert_equal("Englisch (Lateinisch)", temp);
545 ldn->localeDisplayName(known_lang_known_region, temp);
546 test_assert_equal("Englisch (Vereinigte Staaten)", temp);
547 ldn->localeDisplayName(known_lang_known_script_known_region, temp);
548 test_assert_equal("Englisch (Lateinisch, Vereinigte Staaten)", temp);
549 }
550
TestSubstituteHandling()551 void LocaleDisplayNamesTest::TestSubstituteHandling() {
552 // With substitute as default
553 logln("Context: none\n");
554 std::unique_ptr<LocaleDisplayNames> ldn(LocaleDisplayNames::createInstance(Locale::getGermany()));
555 VerifySubstitute(ldn.get());
556
557 // With explicit set substitute, and standard names
558 logln("Context: UDISPCTX_SUBSTITUTE, UDISPCTX_STANDARD_NAMES\n");
559 UDisplayContext context_1[] = { UDISPCTX_SUBSTITUTE, UDISPCTX_STANDARD_NAMES };
560 ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_1, 2));
561 VerifySubstitute(ldn.get());
562
563 // With explicit set substitute and dialect names
564 logln("Context: UDISPCTX_SUBSTITUTE, UDISPCTX_DIALECT_NAMES\n");
565 UDisplayContext context_2[] = { UDISPCTX_SUBSTITUTE, UDISPCTX_DIALECT_NAMES };
566 ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_2, 2));
567 VerifySubstitute(ldn.get());
568
569 // With explicit set no_substitute, and standard names
570 logln("Context: UDISPCTX_NO_SUBSTITUTE, UDISPCTX_STANDARD_NAMES\n");
571 UDisplayContext context_3[] = { UDISPCTX_NO_SUBSTITUTE, UDISPCTX_STANDARD_NAMES };
572 ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_3, 2));
573 VerifyNoSubstitute(ldn.get());
574
575 // With explicit set no_substitute and dialect names
576 logln("Context: UDISPCTX_NO_SUBSTITUTE, UDISPCTX_DIALECT_NAMES\n");
577 UDisplayContext context_4[] = { UDISPCTX_NO_SUBSTITUTE, UDISPCTX_DIALECT_NAMES };
578 ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_4, 2));
579 VerifyNoSubstitute(ldn.get());
580 }
581
582 #endif /* UCONFIG_NO_FORMATTING */
583