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         TESTCASE(15, TestNumericRegionID);
86 #endif
87         default:
88             name = "";
89             break;
90     }
91 }
92 
93 #if !UCONFIG_NO_FORMATTING
TestCreate()94 void LocaleDisplayNamesTest::TestCreate() {
95   UnicodeString temp;
96   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getGermany());
97   ldn->localeDisplayName("de_DE", temp);
98   delete ldn;
99   test_assert_equal("Deutsch (Deutschland)", temp);
100 }
101 
TestCreateDialect()102 void LocaleDisplayNamesTest::TestCreateDialect() {
103   UnicodeString temp;
104   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES);
105   ldn->localeDisplayName("en_GB", temp);
106   delete ldn;
107   test_assert_equal("British English", temp);
108 }
109 
TestWithKeywordsAndEverything()110 void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
111   UnicodeString temp;
112   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
113   const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
114   const char *target = "English (Traditional, United States, VALLEY, "
115     "Gregorian Calendar, Phonebook Sort Order)";
116   ldn->localeDisplayName(locname, temp);
117   delete ldn;
118   test_assert_equal(target, temp);
119 }
120 
TestCurrencyKeyword()121 void LocaleDisplayNamesTest::TestCurrencyKeyword() {
122   UnicodeString temp;
123   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
124   const char *locname = "ja@currency=JPY";
125   const char *target = "Japanese (Japanese Yen)";
126   ldn->localeDisplayName(locname, temp);
127   delete ldn;
128   test_assert_equal(target, temp);
129 }
130 
TestUnknownCurrencyKeyword()131 void LocaleDisplayNamesTest::TestUnknownCurrencyKeyword() {
132   UnicodeString temp;
133   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
134   const char *locname = "de@currency=XYZ";
135   const char *target = "German (Currency: XYZ)";
136   ldn->localeDisplayName(locname, temp);
137   delete ldn;
138   test_assert_equal(target, temp);
139 }
140 
TestUntranslatedKeywords()141 void LocaleDisplayNamesTest::TestUntranslatedKeywords() {
142   UnicodeString temp;
143   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
144   const char *locname = "de@foo=bar";
145   const char *target = "German (foo=bar)";
146   ldn->localeDisplayName(locname, temp);
147   delete ldn;
148   test_assert_equal(target, temp);
149 }
150 
TestPrivateUse()151 void LocaleDisplayNamesTest::TestPrivateUse() {
152   UnicodeString temp;
153   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
154   const char *locname = "de@x=foobar";
155   const char *target = "German (Private-Use: foobar)";
156   ldn->localeDisplayName(locname, temp);
157   delete ldn;
158   test_assert_equal(target, temp);
159 }
160 
TestUldnOpen()161 void LocaleDisplayNamesTest::TestUldnOpen() {
162   UErrorCode status = U_ZERO_ERROR;
163   const int32_t kMaxResultSize = 150;  // long enough
164   UChar result[150];
165   ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
166   int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status);
167   uldn_close(ldn);
168   test_assert(U_SUCCESS(status));
169 
170   UnicodeString str(result, len, kMaxResultSize);
171   test_assert_equal("Deutsch (Deutschland)", str);
172 
173   // make sure that NULL gives us the default locale as usual
174   ldn = uldn_open(NULL, ULDN_STANDARD_NAMES, &status);
175   const char *locale = uldn_getLocale(ldn);
176   if(0 != uprv_strcmp(uloc_getDefault(), locale)) {
177     errln("uldn_getLocale(uldn_open(NULL))=%s != default locale %s\n", locale, uloc_getDefault());
178   }
179   uldn_close(ldn);
180   test_assert(U_SUCCESS(status));
181 }
182 
TestUldnOpenDialect()183 void LocaleDisplayNamesTest::TestUldnOpenDialect() {
184   UErrorCode status = U_ZERO_ERROR;
185   const int32_t kMaxResultSize = 150;  // long enough
186   UChar result[150];
187   ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
188   int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status);
189   uldn_close(ldn);
190   test_assert(U_SUCCESS(status));
191 
192   UnicodeString str(result, len, kMaxResultSize);
193   test_assert_equal("British English", str);
194 }
195 
TestUldnWithGarbage()196 void LocaleDisplayNamesTest::TestUldnWithGarbage() {
197   UErrorCode status = U_ZERO_ERROR;
198   const int32_t kMaxResultSize = 150;  // long enough
199   UChar result[150];
200   ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
201   int32_t len = uldn_localeDisplayName(ldn, "english (United States) [w", result, kMaxResultSize, &status);
202   uldn_close(ldn);
203   test_assert(U_FAILURE(status) && len == 0);
204 }
205 
TestUldnWithKeywordsAndEverything()206 void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
207   UErrorCode status = U_ZERO_ERROR;
208   const int32_t kMaxResultSize = 150;  // long enough
209   UChar result[150];
210   const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
211   const char *target = "English (Traditional, United States, VALLEY, "
212     "Gregorian Calendar, Phonebook Sort Order)";
213   ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status);
214   int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status);
215   uldn_close(ldn);
216   test_assert(U_SUCCESS(status));
217 
218   UnicodeString str(result, len, kMaxResultSize);
219   test_assert_equal(target, str);
220 }
221 
TestUldnComponents()222 void LocaleDisplayNamesTest::TestUldnComponents() {
223   UErrorCode status = U_ZERO_ERROR;
224   const int32_t kMaxResultSize = 150;  // long enough
225   UChar result[150];
226 
227   ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
228   test_assert(U_SUCCESS(status));
229   if (U_FAILURE(status)) {
230     return;
231   }
232 
233   // "en_Hant_US_PRE_EURO@calendar=gregorian";
234 
235   {
236     int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status);
237     UnicodeString str(result, len, kMaxResultSize);
238     test_assert_equal("Englisch", str);
239   }
240 
241 
242   {
243     int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status);
244     UnicodeString str(result, len, kMaxResultSize);
245     test_assert_equal("Traditionell", str);
246   }
247 
248   {
249     int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize,
250                          &status);
251     UnicodeString str(result, len, kMaxResultSize);
252     test_assert_equal("Traditionell", str);
253   }
254 
255   {
256     int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status);
257     UnicodeString str(result, len, kMaxResultSize);
258     test_assert_equal("Vereinigte Staaten", str);
259   }
260 
261   {
262     int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status);
263     UnicodeString str(result, len, kMaxResultSize);
264     test_assert_equal("PRE_EURO", str);
265   }
266 
267   {
268     int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status);
269     UnicodeString str(result, len, kMaxResultSize);
270     test_assert_equal("Kalender", str);
271   }
272 
273   {
274     int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result,
275                        kMaxResultSize, &status);
276     UnicodeString str(result, len, kMaxResultSize);
277     test_assert_equal("Gregorianischer Kalender", str);
278   }
279 
280   uldn_close(ldn);
281 }
282 
283 
284 typedef struct {
285     const char * displayLocale;
286     UDisplayContext dialectHandling;
287     UDisplayContext capitalization;
288     UDisplayContext displayLength;
289     const char * localeToBeNamed;
290     const UChar * result;
291 } LocNameDispContextItem;
292 
293 static char en[] = "en";
294 static char en_cabud[] = "en@calendar=buddhist";
295 static char en_GB[] = "en_GB";
296 static char uz_Latn[] = "uz_Latn";
297 
298 static UChar daFor_en[]       = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"engelsk"
299 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,
300                                  0x6B,0x61,0x6C,0x65,0x6E,0x64,0x65,0x72,0x29,0}; //"engelsk (buddhistisk kalender)"
301 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)"
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_DT[] = {0x42,0x72,0x69,0x74,0x69,0x73,0x6B,0x20,0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Britisk engelsk"
314 static UChar esFor_en_T[]     = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0}; //"Ingles" with acute on the e
315 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)" ...
316 static UChar esFor_en_GB_ST[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0x20,0x28,0x52,0x55,0x29,0}; //"Ingles (RU)" ...
317 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
318 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
319 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
320 
321 static const LocNameDispContextItem ctxtItems[] = {
322     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en,    daFor_en },
323     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en_cabud, daFor_en_cabud },
324     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB },
325     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_SHORT,  en_GB, daFor_en_GB },
326     { "da", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_D },
327     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en,    esFor_en },
328     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB },
329     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_SHORT,  en_GB, esFor_en_GB_S },
330     { "es", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_D },
331     { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE,    UDISPCTX_LENGTH_FULL,   uz_Latn, ruFor_uz_Latn },
332 #if !UCONFIG_NO_BREAK_ITERATION
333     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en,    daFor_en_T },
334     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en_cabud, daFor_en_cabudT },
335     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_T },
336     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_SHORT,  en_GB, daFor_en_GB_T },
337     { "da", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_DT },
338     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en,    esFor_en_T },
339     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_T },
340     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_SHORT,  en_GB, esFor_en_GB_ST },
341     { "es", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_DT },
342     { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, UDISPCTX_LENGTH_FULL,   uz_Latn, ruFor_uz_Latn_T },
343 
344     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en,    daFor_en_T },
345     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en_cabud, daFor_en_cabudT },
346     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_T },
347     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_SHORT,  en_GB, daFor_en_GB_T },
348     { "da", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_DT },
349     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en,    esFor_en_T },
350     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_T },
351     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_SHORT,  en_GB, esFor_en_GB_ST },
352     { "es", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_DT },
353     { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU,       UDISPCTX_LENGTH_FULL,   uz_Latn, ruFor_uz_Latn_T },
354 
355     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en,    daFor_en },
356     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en_cabud, daFor_en_cabud },
357     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB },
358     { "da", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_SHORT,  en_GB, daFor_en_GB },
359     { "da", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en_GB, daFor_en_GB_D },
360     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en,    esFor_en_T },
361     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_T },
362     { "es", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_SHORT,  en_GB, esFor_en_GB_ST },
363     { "es", UDISPCTX_DIALECT_NAMES,  UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   en_GB, esFor_en_GB_DT },
364     { "ru", UDISPCTX_STANDARD_NAMES, UDISPCTX_CAPITALIZATION_FOR_STANDALONE,            UDISPCTX_LENGTH_FULL,   uz_Latn, ruFor_uz_Latn_T },
365  #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
366     { NULL, (UDisplayContext)0,      (UDisplayContext)0,                                (UDisplayContext)0,     NULL,  NULL }
367 };
368 
TestUldnDisplayContext()369 void LocaleDisplayNamesTest::TestUldnDisplayContext() {
370     const LocNameDispContextItem * ctxtItemPtr;
371     for (ctxtItemPtr = ctxtItems; ctxtItemPtr->displayLocale != NULL; ctxtItemPtr++) {
372         UDisplayContext contexts[3] = {ctxtItemPtr->dialectHandling, ctxtItemPtr->capitalization, ctxtItemPtr->displayLength};
373         UErrorCode status = U_ZERO_ERROR;
374         ULocaleDisplayNames * uldn = uldn_openForContext(ctxtItemPtr->displayLocale, contexts, 3, &status);
375         if (U_FAILURE(status)) {
376             errln(UnicodeString("FAIL: uldn_openForContext failed for locale ") + ctxtItemPtr->displayLocale +
377                   ", dialectHandling " + ctxtItemPtr->dialectHandling +
378                   ", capitalization " +  ctxtItemPtr->capitalization +
379                   ", displayLength " +  ctxtItemPtr->displayLength);
380         } else {
381             UDisplayContext dialectHandling = uldn_getContext(uldn, UDISPCTX_TYPE_DIALECT_HANDLING, &status);
382             UDisplayContext capitalization = uldn_getContext(uldn, UDISPCTX_TYPE_CAPITALIZATION, &status);
383             UDisplayContext displayLength = uldn_getContext(uldn, UDISPCTX_TYPE_DISPLAY_LENGTH, &status);
384             if (U_FAILURE(status)) {
385                 errln(UnicodeString("FAIL: uldn_getContext status ") + (int)status);
386             } else if (dialectHandling != ctxtItemPtr->dialectHandling ||
387                        capitalization != ctxtItemPtr->capitalization ||
388                        displayLength != ctxtItemPtr->displayLength) {
389                 errln("FAIL: uldn_getContext retrieved incorrect dialectHandling, capitalization, or displayLength");
390             } else {
391                 UChar nameBuf[ULOC_FULLNAME_CAPACITY];
392                 int32_t len = uldn_localeDisplayName(uldn, ctxtItemPtr->localeToBeNamed, nameBuf, ULOC_FULLNAME_CAPACITY, &status);
393                 if (U_FAILURE(status)) {
394                     dataerrln(UnicodeString("FAIL: uldn_localeDisplayName status: ") + u_errorName(status));
395                 } else if (u_strcmp(ctxtItemPtr->result, nameBuf) != 0) {
396                     UnicodeString exp(ctxtItemPtr->result, u_strlen(ctxtItemPtr->result));
397                     UnicodeString got(nameBuf, len);
398                     dataerrln(UnicodeString("FAIL: uldn_localeDisplayName, capitalization ") + ctxtItemPtr->capitalization +
399                           ", expected " + exp + ", got " + got );
400                 }
401             }
402             uldn_close(uldn);
403         }
404     }
405 }
406 
TestRootEtc()407 void LocaleDisplayNamesTest::TestRootEtc() {
408   UnicodeString temp;
409   LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
410   const char *locname = "@collation=phonebook";
411   const char *target = "root (Phonebook Sort Order)";
412   ldn->localeDisplayName(locname, temp);
413   test_assert_equal(target, temp);
414 
415   ldn->languageDisplayName("root", temp);
416   test_assert_equal("root", temp);
417 
418   ldn->languageDisplayName("en_GB", temp);
419   test_assert_equal("en_GB", temp);
420 
421   delete ldn;
422 }
423 
TestNumericRegionID()424 void LocaleDisplayNamesTest::TestNumericRegionID() {
425     UErrorCode err = U_ZERO_ERROR;
426     ULocaleDisplayNames* ldn = uldn_open("es_MX", ULDN_STANDARD_NAMES, &err);
427     UChar displayName[200];
428     uldn_regionDisplayName(ldn, "019", displayName, 200, &err);
429     test_assert(U_SUCCESS(err));
430     test_assert_equal(UnicodeString(u"América"), UnicodeString(displayName));
431     uldn_close(ldn);
432 
433     err = U_ZERO_ERROR; // reset in case the test above returned an error code
434     ldn = uldn_open("en_AU", ULDN_STANDARD_NAMES, &err);
435     uldn_regionDisplayName(ldn, "002", displayName, 200, &err);
436     test_assert(U_SUCCESS(err));
437     test_assert_equal(UnicodeString(u"Africa"), UnicodeString(displayName));
438     uldn_close(ldn);
439 }
440 
441 static const char unknown_region[] = "wx";
442 static const char unknown_lang[] = "xy";
443 static const char unknown_script[] = "wxyz";
444 static const char unknown_variant[] = "abc";
445 static const char unknown_key[] = "efg";
446 static const char unknown_ca_value[] = "ijk";
447 static const char known_lang_unknown_script[] = "en-wxyz";
448 static const char unknown_lang_unknown_script[] = "xy-wxyz";
449 static const char unknown_lang_known_script[] = "xy-Latn";
450 static const char unknown_lang_unknown_region[] = "xy-wx";
451 static const char known_lang_unknown_region[] = "en-wx";
452 static const char unknown_lang_known_region[] = "xy-US";
453 static const char unknown_lang_unknown_script_unknown_region[] = "xy-wxyz-wx";
454 static const char known_lang_unknown_script_unknown_region[] = "en-wxyz-wx";
455 static const char unknown_lang_known_script_unknown_region[] = "xy-Latn-wx";
456 static const char unknown_lang_known_script_known_region[] = "xy-wxyz-US";
457 static const char known_lang[] = "en";
458 static const char known_lang_known_script[] = "en-Latn";
459 static const char known_lang_known_region[] = "en-US";
460 static const char known_lang_known_script_known_region[] = "en-Latn-US";
461 
VerifySubstitute(LocaleDisplayNames * ldn)462 void LocaleDisplayNamesTest::VerifySubstitute(LocaleDisplayNames* ldn) {
463   UnicodeString temp;
464   // Ensure the default is UDISPCTX_SUBSTITUTE
465   UDisplayContext context = ldn->getContext(UDISPCTX_TYPE_SUBSTITUTE_HANDLING);
466   test_assert(UDISPCTX_SUBSTITUTE == context);
467 
468   ldn->regionDisplayName(unknown_region, temp);
469   test_assert_equal(unknown_region, temp);
470   ldn->languageDisplayName(unknown_lang, temp);
471   test_assert_equal(unknown_lang, temp);
472   ldn->scriptDisplayName(unknown_script, temp);
473   test_assert_equal(unknown_script, temp);
474   ldn->variantDisplayName(unknown_variant, temp);
475   test_assert_equal(unknown_variant, temp);
476   ldn->keyDisplayName(unknown_key, temp);
477   test_assert_equal(unknown_key, temp);
478   ldn->keyValueDisplayName("ca", unknown_ca_value, temp);
479   test_assert_equal(unknown_ca_value, temp);
480 
481   ldn->localeDisplayName(unknown_lang, temp);
482   test_assert_equal(unknown_lang, temp);
483   ldn->localeDisplayName(known_lang_unknown_script, temp);
484   test_assert_equal("Englisch (Wxyz)", temp);
485   ldn->localeDisplayName(unknown_lang_unknown_script, temp);
486   test_assert_equal("xy (Wxyz)", temp);
487   ldn->localeDisplayName(unknown_lang_known_script, temp);
488   test_assert_equal("xy (Lateinisch)", temp);
489   ldn->localeDisplayName(unknown_lang_unknown_region, temp);
490   test_assert_equal("xy (WX)", temp);
491   ldn->localeDisplayName(known_lang_unknown_region, temp);
492   test_assert_equal("Englisch (WX)", temp);
493   ldn->localeDisplayName(unknown_lang_known_region, temp);
494   test_assert_equal("xy (Vereinigte Staaten)", temp);
495   ldn->localeDisplayName(unknown_lang_unknown_script_unknown_region, temp);
496   test_assert_equal("xy (Wxyz, WX)", temp);
497   ldn->localeDisplayName(known_lang_unknown_script_unknown_region, temp);
498   test_assert_equal("Englisch (Wxyz, WX)", temp);
499   ldn->localeDisplayName(unknown_lang_known_script_unknown_region, temp);
500   test_assert_equal("xy (Lateinisch, WX)", temp);
501   ldn->localeDisplayName(unknown_lang_known_script_known_region, temp);
502   test_assert_equal("xy (Wxyz, Vereinigte Staaten)", temp);
503 
504   ldn->localeDisplayName(known_lang, temp);
505   test_assert_equal("Englisch", temp);
506   ldn->localeDisplayName(known_lang_known_script, temp);
507   test_assert_equal("Englisch (Lateinisch)", temp);
508   ldn->localeDisplayName(known_lang_known_region, temp);
509   test_assert_equal("Englisch (Vereinigte Staaten)", temp);
510   ldn->localeDisplayName(known_lang_known_script_known_region, temp);
511   test_assert_equal("Englisch (Lateinisch, Vereinigte Staaten)", temp);
512 }
513 
VerifyNoSubstitute(LocaleDisplayNames * ldn)514 void LocaleDisplayNamesTest::VerifyNoSubstitute(LocaleDisplayNames* ldn) {
515   UnicodeString temp("");
516   std::string utf8;
517   // Ensure the default is UDISPCTX_SUBSTITUTE
518   UDisplayContext context = ldn->getContext(UDISPCTX_TYPE_SUBSTITUTE_HANDLING);
519   test_assert(UDISPCTX_NO_SUBSTITUTE == context);
520 
521   ldn->regionDisplayName(unknown_region, temp);
522   test_assert(TRUE == temp.isBogus());
523   ldn->languageDisplayName(unknown_lang, temp);
524   test_assert(TRUE == temp.isBogus());
525   ldn->scriptDisplayName(unknown_script, temp);
526   test_assert(TRUE == temp.isBogus());
527   ldn->variantDisplayName(unknown_variant, temp);
528   test_assert(TRUE == temp.isBogus());
529   ldn->keyDisplayName(unknown_key, temp);
530   test_assert(TRUE == temp.isBogus());
531   ldn->keyValueDisplayName("ca", unknown_ca_value, temp);
532   test_assert(TRUE == temp.isBogus());
533 
534   ldn->localeDisplayName(unknown_lang, temp);
535   test_assert(TRUE == temp.isBogus());
536   ldn->localeDisplayName(known_lang_unknown_script, temp);
537   test_assert(TRUE == temp.isBogus());
538   ldn->localeDisplayName(unknown_lang_unknown_script, temp);
539   test_assert(TRUE == temp.isBogus());
540   ldn->localeDisplayName(unknown_lang_known_script, temp);
541   test_assert(TRUE == temp.isBogus());
542   ldn->localeDisplayName(unknown_lang_unknown_region, temp);
543   test_assert(TRUE == temp.isBogus());
544   ldn->localeDisplayName(known_lang_unknown_region, temp);
545   test_assert(TRUE == temp.isBogus());
546   ldn->localeDisplayName(unknown_lang_known_region, temp);
547   test_assert(TRUE == temp.isBogus());
548   ldn->localeDisplayName(unknown_lang_unknown_script_unknown_region, temp);
549   test_assert(TRUE == temp.isBogus());
550   ldn->localeDisplayName(known_lang_unknown_script_unknown_region, temp);
551   test_assert(TRUE == temp.isBogus());
552   ldn->localeDisplayName(unknown_lang_known_script_unknown_region, temp);
553   test_assert(TRUE == temp.isBogus());
554   ldn->localeDisplayName(unknown_lang_known_script_known_region, temp);
555   test_assert(TRUE == temp.isBogus());
556 
557   ldn->localeDisplayName(known_lang, temp);
558   test_assert_equal("Englisch", temp);
559   ldn->localeDisplayName(known_lang_known_script, temp);
560   test_assert_equal("Englisch (Lateinisch)", temp);
561   ldn->localeDisplayName(known_lang_known_region, temp);
562   test_assert_equal("Englisch (Vereinigte Staaten)", temp);
563   ldn->localeDisplayName(known_lang_known_script_known_region, temp);
564   test_assert_equal("Englisch (Lateinisch, Vereinigte Staaten)", temp);
565 }
566 
TestSubstituteHandling()567 void LocaleDisplayNamesTest::TestSubstituteHandling() {
568   // With substitute as default
569   logln("Context: none\n");
570   std::unique_ptr<LocaleDisplayNames> ldn(LocaleDisplayNames::createInstance(Locale::getGermany()));
571   VerifySubstitute(ldn.get());
572 
573   // With explicit set substitute, and standard names
574   logln("Context: UDISPCTX_SUBSTITUTE, UDISPCTX_STANDARD_NAMES\n");
575   UDisplayContext context_1[] = { UDISPCTX_SUBSTITUTE, UDISPCTX_STANDARD_NAMES };
576   ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_1, 2));
577   VerifySubstitute(ldn.get());
578 
579   // With explicit set substitute and dialect names
580   logln("Context: UDISPCTX_SUBSTITUTE, UDISPCTX_DIALECT_NAMES\n");
581   UDisplayContext context_2[] = { UDISPCTX_SUBSTITUTE, UDISPCTX_DIALECT_NAMES };
582   ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_2, 2));
583   VerifySubstitute(ldn.get());
584 
585   // With explicit set no_substitute, and standard names
586   logln("Context: UDISPCTX_NO_SUBSTITUTE, UDISPCTX_STANDARD_NAMES\n");
587   UDisplayContext context_3[] = { UDISPCTX_NO_SUBSTITUTE, UDISPCTX_STANDARD_NAMES };
588   ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_3, 2));
589   VerifyNoSubstitute(ldn.get());
590 
591   // With explicit set no_substitute and dialect names
592   logln("Context: UDISPCTX_NO_SUBSTITUTE, UDISPCTX_DIALECT_NAMES\n");
593   UDisplayContext context_4[] = { UDISPCTX_NO_SUBSTITUTE, UDISPCTX_DIALECT_NAMES };
594   ldn.reset(LocaleDisplayNames::createInstance(Locale::getGermany(), context_4, 2));
595   VerifyNoSubstitute(ldn.get());
596 }
597 
598 #endif   /*  UCONFIG_NO_FORMATTING */
599