1 /*********************************************************************
3 * Copyright (c) 2010-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *********************************************************************/
12 test_assert( Test (should be TRUE) )
17 the macro is ugly but makes the tests pretty.
20 #define test_assert(test) \
23 errln("FAIL: " #test " was not true. In " __FILE__ " on line %d", __LINE__ ); \
25 logln("PASS: asserted " #test); \
30 test_assert_print( Test (should be TRUE), printable )
33 test_assert(i==3, toString(i));
35 the macro is ugly but makes the tests pretty.
38 #define test_assert_print(test,print) \
41 errln("FAIL: " #test " was not true. " + UnicodeString(print) ); \
43 logln("PASS: asserted " #test "-> " + UnicodeString(print)); \
46 #define test_assert_equal(target,value) \
48 if (UnicodeString(target)!=(value)) { \
49 logln("unexpected value '" + (value) + "'"); \
50 dataerrln("FAIL: " #target " == " #value " was not true. In " __FILE__ " on line %d", __LINE__); \
52 logln("PASS: asserted " #target " == " #value); \
56 #define test_dumpLocale(l) { logln(#l " = " + UnicodeString(l.getName(), "")); }
58 LocaleDisplayNamesTest::LocaleDisplayNamesTest() {
61 LocaleDisplayNamesTest::~LocaleDisplayNamesTest() {
64 void LocaleDisplayNamesTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
,
67 #if !UCONFIG_NO_FORMATTING
68 TESTCASE(0, TestCreate
);
69 TESTCASE(1, TestCreateDialect
);
70 TESTCASE(2, TestWithKeywordsAndEverything
);
71 TESTCASE(3, TestUldnOpen
);
72 TESTCASE(4, TestUldnOpenDialect
);
73 TESTCASE(5, TestUldnWithKeywordsAndEverything
);
74 TESTCASE(6, TestUldnComponents
);
75 TESTCASE(7, TestRootEtc
);
76 TESTCASE(8, TestKeywords
);
77 TESTCASE(9, TestUntranslatedKeywords
);
78 TESTCASE(10, TestPrivateUse
);
79 TESTCASE(11, TestUldnDisplayContext
);
87 #if !UCONFIG_NO_FORMATTING
88 void LocaleDisplayNamesTest::TestCreate() {
90 LocaleDisplayNames
*ldn
= LocaleDisplayNames::createInstance(Locale::getGermany());
91 ldn
->localeDisplayName("de_DE", temp
);
93 test_assert_equal("Deutsch (Deutschland)", temp
);
96 void LocaleDisplayNamesTest::TestCreateDialect() {
98 LocaleDisplayNames
*ldn
= LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES
);
99 ldn
->localeDisplayName("en_GB", temp
);
101 test_assert_equal("British English", temp
);
104 void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
106 LocaleDisplayNames
*ldn
= LocaleDisplayNames::createInstance(Locale::getUS());
107 const char *locname
= "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
108 const char *target
= "English (Traditional, United States, VALLEY, "
109 "Gregorian Calendar, Phonebook Sort Order)";
110 ldn
->localeDisplayName(locname
, temp
);
112 test_assert_equal(target
, temp
);
115 void LocaleDisplayNamesTest::TestKeywords() {
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
);
122 test_assert_equal(target
, temp
);
125 void LocaleDisplayNamesTest::TestUntranslatedKeywords() {
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
);
132 test_assert_equal(target
, temp
);
135 void LocaleDisplayNamesTest::TestPrivateUse() {
137 LocaleDisplayNames
*ldn
= LocaleDisplayNames::createInstance(Locale::getUS());
138 const char *locname
= "de@x=foobar";
139 const char *target
= "German (Private-Use: foobar)";
140 ldn
->localeDisplayName(locname
, temp
);
142 test_assert_equal(target
, temp
);
145 void LocaleDisplayNamesTest::TestUldnOpen() {
146 UErrorCode status
= U_ZERO_ERROR
;
147 const int32_t kMaxResultSize
= 150; // long enough
149 ULocaleDisplayNames
*ldn
= uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES
, &status
);
150 int32_t len
= uldn_localeDisplayName(ldn
, "de_DE", result
, kMaxResultSize
, &status
);
152 test_assert(U_SUCCESS(status
));
154 UnicodeString
str(result
, len
, kMaxResultSize
);
155 test_assert_equal("Deutsch (Deutschland)", str
);
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());
164 test_assert(U_SUCCESS(status
));
167 void LocaleDisplayNamesTest::TestUldnOpenDialect() {
168 UErrorCode status
= U_ZERO_ERROR
;
169 const int32_t kMaxResultSize
= 150; // long enough
171 ULocaleDisplayNames
*ldn
= uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES
, &status
);
172 int32_t len
= uldn_localeDisplayName(ldn
, "en_GB", result
, kMaxResultSize
, &status
);
174 test_assert(U_SUCCESS(status
));
176 UnicodeString
str(result
, len
, kMaxResultSize
);
177 test_assert_equal("British English", str
);
180 void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
181 UErrorCode status
= U_ZERO_ERROR
;
182 const int32_t kMaxResultSize
= 150; // long enough
184 const char *locname
= "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
185 const char *target
= "English (Traditional, United States, VALLEY, "
186 "Gregorian Calendar, Phonebook Sort Order)";
187 ULocaleDisplayNames
*ldn
= uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES
, &status
);
188 int32_t len
= uldn_localeDisplayName(ldn
, locname
, result
, kMaxResultSize
, &status
);
190 test_assert(U_SUCCESS(status
));
192 UnicodeString
str(result
, len
, kMaxResultSize
);
193 test_assert_equal(target
, str
);
196 void LocaleDisplayNamesTest::TestUldnComponents() {
197 UErrorCode status
= U_ZERO_ERROR
;
198 const int32_t kMaxResultSize
= 150; // long enough
201 ULocaleDisplayNames
*ldn
= uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES
, &status
);
202 test_assert(U_SUCCESS(status
));
203 if (U_FAILURE(status
)) {
207 // "en_Hant_US_PRE_EURO@calendar=gregorian";
210 int32_t len
= uldn_languageDisplayName(ldn
, "en", result
, kMaxResultSize
, &status
);
211 UnicodeString
str(result
, len
, kMaxResultSize
);
212 test_assert_equal("Englisch", str
);
217 int32_t len
= uldn_scriptDisplayName(ldn
, "Hant", result
, kMaxResultSize
, &status
);
218 UnicodeString
str(result
, len
, kMaxResultSize
);
219 test_assert_equal("Traditionell", str
);
223 int32_t len
= uldn_scriptCodeDisplayName(ldn
, USCRIPT_TRADITIONAL_HAN
, result
, kMaxResultSize
,
225 UnicodeString
str(result
, len
, kMaxResultSize
);
226 test_assert_equal("Traditionell", str
);
230 int32_t len
= uldn_regionDisplayName(ldn
, "US", result
, kMaxResultSize
, &status
);
231 UnicodeString
str(result
, len
, kMaxResultSize
);
232 test_assert_equal("Vereinigte Staaten", str
);
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
);
242 int32_t len
= uldn_keyDisplayName(ldn
, "calendar", result
, kMaxResultSize
, &status
);
243 UnicodeString
str(result
, len
, kMaxResultSize
);
244 test_assert_equal("Kalender", str
);
248 int32_t len
= uldn_keyValueDisplayName(ldn
, "calendar", "gregorian", result
,
249 kMaxResultSize
, &status
);
250 UnicodeString
str(result
, len
, kMaxResultSize
);
251 test_assert_equal("Gregorianischer Kalender", str
);
259 const char * displayLocale
;
260 UDisplayContext dialectHandling
;
261 UDisplayContext capitalization
;
262 const char * localeToBeNamed
;
263 const UChar
* result
;
264 } LocNameDispContextItem
;
266 static char en
[] = "en";
267 static char en_US
[] = "en_US";
269 static UChar daFor_en
[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"engelsk"
270 static UChar daFor_en_US
[] = {0x65,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x53,0x41,0x29,0}; //"engelsk (USA)"
271 static 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"
272 static UChar esFor_en
[] = {0x69,0x6E,0x67,0x6C,0xE9,0x73,0}; //"ingles" with acute on e
273 static 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)" ...
274 static 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
276 static UChar daFor_en_T
[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0}; //"Engelsk"
277 static UChar daFor_en_US_T
[] = {0x45,0x6E,0x67,0x65,0x6C,0x73,0x6B,0x20,0x28,0x55,0x53,0x41,0x29,0}; //"Engelsk (USA)"
278 static 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"
279 static UChar esFor_en_T
[] = {0x49,0x6E,0x67,0x6C,0xE9,0x73,0}; //"Ingles" ...
280 static 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)" ...
281 static 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 */
284 static 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
},
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
}
309 void 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
);
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");
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
);
342 void LocaleDisplayNamesTest::TestRootEtc() {
344 LocaleDisplayNames
*ldn
= LocaleDisplayNames::createInstance(Locale::getUS());
345 const char *locname
= "@collation=phonebook";
346 const char *target
= "Root (Phonebook Sort Order)";
347 ldn
->localeDisplayName(locname
, temp
);
348 test_assert_equal(target
, temp
);
350 ldn
->languageDisplayName("root", temp
);
351 test_assert_equal("root", temp
);
353 ldn
->languageDisplayName("en_GB", temp
);
354 test_assert_equal("en_GB", temp
);
359 #endif /* UCONFIG_NO_FORMATTING */