]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/locnmtst.cpp
ICU-491.11.2.tar.gz
[apple/icu.git] / icuSources / test / intltest / locnmtst.cpp
1 /*********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2010-2012, International Business Machines Corporation and
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 { \
52 logln("PASS: asserted " #target " == " #value); \
53 } \
54 }
55
56 #define test_dumpLocale(l) { logln(#l " = " + UnicodeString(l.getName(), "")); }
57
58 LocaleDisplayNamesTest::LocaleDisplayNamesTest() {
59 }
60
61 LocaleDisplayNamesTest::~LocaleDisplayNamesTest() {
62 }
63
64 void LocaleDisplayNamesTest::runIndexedTest(int32_t index, UBool exec, const char* &name,
65 char* /*par*/) {
66 switch (index) {
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 #endif
80 default:
81 name = "";
82 break;
83 }
84 }
85
86 #if !UCONFIG_NO_FORMATTING
87 void LocaleDisplayNamesTest::TestCreate() {
88 UnicodeString temp;
89 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getGermany());
90 ldn->localeDisplayName("de_DE", temp);
91 delete ldn;
92 test_assert_equal("Deutsch (Deutschland)", temp);
93 }
94
95 void LocaleDisplayNamesTest::TestCreateDialect() {
96 UnicodeString temp;
97 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES);
98 ldn->localeDisplayName("en_GB", temp);
99 delete ldn;
100 test_assert_equal("British English", temp);
101 }
102
103 void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() {
104 UnicodeString temp;
105 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
106 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
107 const char *target = "English (Traditional, United States, VALLEY, "
108 "Gregorian Calendar, Phonebook Sort Order)";
109 ldn->localeDisplayName(locname, temp);
110 delete ldn;
111 test_assert_equal(target, temp);
112 }
113
114 void LocaleDisplayNamesTest::TestKeywords() {
115 UnicodeString temp;
116 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
117 const char *locname = "de@currency=XYZ";
118 const char *target = "German (Currency: XYZ)";
119 ldn->localeDisplayName(locname, temp);
120 delete ldn;
121 test_assert_equal(target, temp);
122 }
123
124 void LocaleDisplayNamesTest::TestUntranslatedKeywords() {
125 UnicodeString temp;
126 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
127 const char *locname = "de@foo=bar";
128 const char *target = "German (foo=bar)";
129 ldn->localeDisplayName(locname, temp);
130 delete ldn;
131 test_assert_equal(target, temp);
132 }
133
134 void LocaleDisplayNamesTest::TestPrivateUse() {
135 UnicodeString temp;
136 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
137 const char *locname = "de@x=foobar";
138 const char *target = "German (Private-Use: foobar)";
139 ldn->localeDisplayName(locname, temp);
140 delete ldn;
141 test_assert_equal(target, temp);
142 }
143
144 void LocaleDisplayNamesTest::TestUldnOpen() {
145 UErrorCode status = U_ZERO_ERROR;
146 const int32_t kMaxResultSize = 150; // long enough
147 UChar result[150];
148 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
149 int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status);
150 uldn_close(ldn);
151 test_assert(U_SUCCESS(status));
152
153 UnicodeString str(result, len, kMaxResultSize);
154 test_assert_equal("Deutsch (Deutschland)", str);
155
156 // make sure that NULL gives us the default locale as usual
157 ldn = uldn_open(NULL, ULDN_STANDARD_NAMES, &status);
158 const char *locale = uldn_getLocale(ldn);
159 if(0 != uprv_strcmp(uloc_getDefault(), locale)) {
160 errln("uldn_getLocale(uldn_open(NULL))=%s != default locale %s\n", locale, uloc_getDefault());
161 }
162 uldn_close(ldn);
163 test_assert(U_SUCCESS(status));
164 }
165
166 void LocaleDisplayNamesTest::TestUldnOpenDialect() {
167 UErrorCode status = U_ZERO_ERROR;
168 const int32_t kMaxResultSize = 150; // long enough
169 UChar result[150];
170 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status);
171 int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status);
172 uldn_close(ldn);
173 test_assert(U_SUCCESS(status));
174
175 UnicodeString str(result, len, kMaxResultSize);
176 test_assert_equal("British English", str);
177 }
178
179 void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() {
180 UErrorCode status = U_ZERO_ERROR;
181 const int32_t kMaxResultSize = 150; // long enough
182 UChar result[150];
183 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook";
184 const char *target = "English (Traditional, United States, VALLEY, "
185 "Gregorian Calendar, Phonebook Sort Order)";
186 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status);
187 int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status);
188 uldn_close(ldn);
189 test_assert(U_SUCCESS(status));
190
191 UnicodeString str(result, len, kMaxResultSize);
192 test_assert_equal(target, str);
193 }
194
195 void LocaleDisplayNamesTest::TestUldnComponents() {
196 UErrorCode status = U_ZERO_ERROR;
197 const int32_t kMaxResultSize = 150; // long enough
198 UChar result[150];
199
200 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status);
201 test_assert(U_SUCCESS(status));
202 if (U_FAILURE(status)) {
203 return;
204 }
205
206 // "en_Hant_US_PRE_EURO@calendar=gregorian";
207
208 {
209 int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status);
210 UnicodeString str(result, len, kMaxResultSize);
211 test_assert_equal("Englisch", str);
212 }
213
214
215 {
216 int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status);
217 UnicodeString str(result, len, kMaxResultSize);
218 test_assert_equal("Traditionell", str);
219 }
220
221 {
222 int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize,
223 &status);
224 UnicodeString str(result, len, kMaxResultSize);
225 test_assert_equal("Traditionell", str);
226 }
227
228 {
229 int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status);
230 UnicodeString str(result, len, kMaxResultSize);
231 test_assert_equal("Vereinigte Staaten", str);
232 }
233
234 {
235 int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status);
236 UnicodeString str(result, len, kMaxResultSize);
237 test_assert_equal("PRE_EURO", str);
238 }
239
240 {
241 int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status);
242 UnicodeString str(result, len, kMaxResultSize);
243 test_assert_equal("Kalender", str);
244 }
245
246 {
247 int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result,
248 kMaxResultSize, &status);
249 UnicodeString str(result, len, kMaxResultSize);
250 test_assert_equal("Gregorianischer Kalender", str);
251 }
252
253 uldn_close(ldn);
254 }
255
256 void LocaleDisplayNamesTest::TestRootEtc() {
257 UnicodeString temp;
258 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS());
259 const char *locname = "@collation=phonebook";
260 const char *target = "Root (Phonebook Sort Order)";
261 ldn->localeDisplayName(locname, temp);
262 test_assert_equal(target, temp);
263
264 ldn->languageDisplayName("root", temp);
265 test_assert_equal("root", temp);
266
267 ldn->languageDisplayName("en_GB", temp);
268 test_assert_equal("en_GB", temp);
269
270 delete ldn;
271 }
272
273 #endif /* UCONFIG_NO_FORMATTING */