1 /********************************************************************* 2 * COPYRIGHT: 3 * Copyright (c) 2010, 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 #endif 77 default: 78 name = ""; 79 break; 80 } 81 } 82 83 #if !UCONFIG_NO_FORMATTING 84 void LocaleDisplayNamesTest::TestCreate() { 85 UnicodeString temp; 86 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getGermany()); 87 ldn->localeDisplayName("de_DE", temp); 88 delete ldn; 89 test_assert_equal("Deutsch (Deutschland)", temp); 90 } 91 92 void LocaleDisplayNamesTest::TestCreateDialect() { 93 UnicodeString temp; 94 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS(), ULDN_DIALECT_NAMES); 95 ldn->localeDisplayName("en_GB", temp); 96 delete ldn; 97 test_assert_equal("British English", temp); 98 } 99 100 void LocaleDisplayNamesTest::TestWithKeywordsAndEverything() { 101 UnicodeString temp; 102 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS()); 103 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook"; 104 const char *target = "English (Traditional Han, United States, VALLEY, " 105 "Calendar=Gregorian Calendar, Collation=Phonebook Sort Order)"; 106 ldn->localeDisplayName(locname, temp); 107 delete ldn; 108 test_assert_equal(target, temp); 109 } 110 111 void LocaleDisplayNamesTest::TestUldnOpen() { 112 UErrorCode status = U_ZERO_ERROR; 113 const int32_t kMaxResultSize = 150; // long enough 114 UChar result[150]; 115 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status); 116 int32_t len = uldn_localeDisplayName(ldn, "de_DE", result, kMaxResultSize, &status); 117 uldn_close(ldn); 118 test_assert(U_SUCCESS(status)); 119 120 UnicodeString str(result, len, kMaxResultSize); 121 test_assert_equal("Deutsch (Deutschland)", str); 122 123 // make sure that NULL gives us the default locale as usual 124 ldn = uldn_open(NULL, ULDN_STANDARD_NAMES, &status); 125 const char *locale = uldn_getLocale(ldn); 126 if(0 != uprv_strcmp(uloc_getDefault(), locale)) { 127 errln("uldn_getLocale(uldn_open(NULL))=%s != default locale %s\n", locale, uloc_getDefault()); 128 } 129 uldn_close(ldn); 130 test_assert(U_SUCCESS(status)); 131 } 132 133 void LocaleDisplayNamesTest::TestUldnOpenDialect() { 134 UErrorCode status = U_ZERO_ERROR; 135 const int32_t kMaxResultSize = 150; // long enough 136 UChar result[150]; 137 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_DIALECT_NAMES, &status); 138 int32_t len = uldn_localeDisplayName(ldn, "en_GB", result, kMaxResultSize, &status); 139 uldn_close(ldn); 140 test_assert(U_SUCCESS(status)); 141 142 UnicodeString str(result, len, kMaxResultSize); 143 test_assert_equal("British English", str); 144 } 145 146 void LocaleDisplayNamesTest::TestUldnWithKeywordsAndEverything() { 147 UErrorCode status = U_ZERO_ERROR; 148 const int32_t kMaxResultSize = 150; // long enough 149 UChar result[150]; 150 const char *locname = "en_Hant_US_VALLEY@calendar=gregorian;collation=phonebook"; 151 const char *target = "English (Traditional Han, United States, VALLEY, " 152 "Calendar=Gregorian Calendar, Collation=Phonebook Sort Order)"; 153 ULocaleDisplayNames *ldn = uldn_open(Locale::getUS().getName(), ULDN_STANDARD_NAMES, &status); 154 int32_t len = uldn_localeDisplayName(ldn, locname, result, kMaxResultSize, &status); 155 uldn_close(ldn); 156 test_assert(U_SUCCESS(status)); 157 158 UnicodeString str(result, len, kMaxResultSize); 159 test_assert_equal(target, str); 160 } 161 162 void LocaleDisplayNamesTest::TestUldnComponents() { 163 UErrorCode status = U_ZERO_ERROR; 164 const int32_t kMaxResultSize = 150; // long enough 165 UChar result[150]; 166 167 ULocaleDisplayNames *ldn = uldn_open(Locale::getGermany().getName(), ULDN_STANDARD_NAMES, &status); 168 test_assert(U_SUCCESS(status)); 169 if (U_FAILURE(status)) { 170 return; 171 } 172 173 // "en_Hant_US_PRE_EURO@calendar=gregorian"; 174 175 { 176 int32_t len = uldn_languageDisplayName(ldn, "en", result, kMaxResultSize, &status); 177 UnicodeString str(result, len, kMaxResultSize); 178 test_assert_equal("Englisch", str); 179 } 180 181 182 { 183 int32_t len = uldn_scriptDisplayName(ldn, "Hant", result, kMaxResultSize, &status); 184 UnicodeString str(result, len, kMaxResultSize); 185 test_assert_equal("Traditionelle Chinesische Schrift", str); 186 } 187 188 { 189 int32_t len = uldn_scriptCodeDisplayName(ldn, USCRIPT_TRADITIONAL_HAN, result, kMaxResultSize, 190 &status); 191 UnicodeString str(result, len, kMaxResultSize); 192 test_assert_equal("Traditionelle Chinesische Schrift", str); 193 } 194 195 { 196 int32_t len = uldn_regionDisplayName(ldn, "US", result, kMaxResultSize, &status); 197 UnicodeString str(result, len, kMaxResultSize); 198 test_assert_equal("Vereinigte Staaten", str); 199 } 200 201 { 202 int32_t len = uldn_variantDisplayName(ldn, "PRE_EURO", result, kMaxResultSize, &status); 203 UnicodeString str(result, len, kMaxResultSize); 204 test_assert_equal("PRE_EURO", str); 205 } 206 207 { 208 int32_t len = uldn_keyDisplayName(ldn, "calendar", result, kMaxResultSize, &status); 209 UnicodeString str(result, len, kMaxResultSize); 210 test_assert_equal("Kalender", str); 211 } 212 213 { 214 int32_t len = uldn_keyValueDisplayName(ldn, "calendar", "gregorian", result, 215 kMaxResultSize, &status); 216 UnicodeString str(result, len, kMaxResultSize); 217 test_assert_equal("Gregorianischer Kalender", str); 218 } 219 220 uldn_close(ldn); 221 } 222 223 void LocaleDisplayNamesTest::TestRootEtc() { 224 UnicodeString temp; 225 LocaleDisplayNames *ldn = LocaleDisplayNames::createInstance(Locale::getUS()); 226 const char *locname = "@collation=phonebook"; 227 const char *target = "Root (Collation=Phonebook Sort Order)"; 228 ldn->localeDisplayName(locname, temp); 229 test_assert_equal(target, temp); 230 231 ldn->languageDisplayName("root", temp); 232 test_assert_equal("root", temp); 233 234 ldn->languageDisplayName("en_GB", temp); 235 test_assert_equal("en_GB", temp); 236 237 delete ldn; 238 } 239 240 #endif /* UCONFIG_NO_FORMATTING */ 241 242