1/* GENERATED SOURCE. DO NOT MODIFY. */ 2// © 2016 and later: Unicode, Inc. and others. 3// License & terms of use: http://www.unicode.org/copyright.html#License 4/* 5 ****************************************************************************************** 6 * Copyright (C) 2009-2015, Google, Inc.; International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ****************************************************************************************** 9 */ 10 11package android.icu.dev.test.util; 12 13import java.util.Set; 14import java.util.TreeSet; 15 16import org.junit.Test; 17 18import android.icu.dev.test.TestFmwk; 19import android.icu.util.LocaleMatcher; 20import android.icu.util.LocaleMatcher.LanguageMatcherData; 21import android.icu.util.LocalePriorityList; 22import android.icu.util.ULocale; 23 24/** 25 * Test the LocaleMatcher. 26 * 27 * @author markdavis 28 */ 29@SuppressWarnings("deprecation") 30public class LocaleMatcherTest extends TestFmwk { 31 32 33 private static final ULocale ZH_MO = new ULocale("zh_MO"); 34 private static final ULocale ZH_HK = new ULocale("zh_HK"); 35 static LanguageMatcherData LANGUAGE_MATCHER_DATA = LocaleMatcherShim.load(); 36 37 private LocaleMatcher newLocaleMatcher(LocalePriorityList build) { 38 return new LocaleMatcher(build, LANGUAGE_MATCHER_DATA); 39 } 40 41 private LocaleMatcher newLocaleMatcher(LocalePriorityList build, LanguageMatcherData data) { 42 return new LocaleMatcher(build, data == null ? LANGUAGE_MATCHER_DATA : data); 43 } 44 45 private LocaleMatcher newLocaleMatcher(LocalePriorityList lpl, LanguageMatcherData data, double d) { 46 return new LocaleMatcher(lpl, data == null ? LANGUAGE_MATCHER_DATA : data, d); 47 } 48 49 private LocaleMatcher newLocaleMatcher(String string) { 50 return new LocaleMatcher(LocalePriorityList.add(string).build(), LANGUAGE_MATCHER_DATA); 51 } 52 53 // public LocaleMatcher(LocalePriorityList languagePriorityList, 54 // LocaleMatcherData matcherData, double threshold) 55 56 @Test 57 public void testParentLocales() { 58 assertCloser("es_AR", "es_419", "es_ES"); 59 assertCloser("es_AR", "es_419", "es"); 60 61 assertCloser("es_AR", "es_MX", "es"); 62 assertCloser("es_AR", "es_MX", "es"); 63 64 assertCloser("en_AU", "en_GB", "en_US"); 65 assertCloser("en_AU", "en_GB", "en"); 66 67 assertCloser("en_AU", "en_NZ", "en_US"); 68 assertCloser("en_AU", "en_NZ", "en"); 69 70 assertCloser("pt_AO", "pt_PT", "pt_BR"); 71 assertCloser("pt_AO", "pt_PT", "pt"); 72 73 assertCloser("zh_HK", "zh_MO", "zh_TW"); 74 assertCloser("zh_HK", "zh_MO", "zh_CN"); 75 assertCloser("zh_HK", "zh_MO", "zh"); 76 } 77 78 private void assertCloser(String a, String closer, String further) { 79 LocaleMatcher matcher = newLocaleMatcher(further + ", " + closer); 80 assertEquals("test " + a + " is closer to " + closer + " than to " + further, new ULocale(closer), matcher.getBestMatch(a)); 81 matcher = newLocaleMatcher(closer + ", " + further); 82 assertEquals("test " + a + " is closer to " + closer + " than to " + further, new ULocale(closer), matcher.getBestMatch(a)); 83 } 84 85 // public void testParentLocales() { 86 // // find all the regions that have a closer relation because of an explicit parent 87 // Set<String> explicitParents = new HashSet<>(INFO.getExplicitParents()); 88 // explicitParents.remove("root"); 89 // Set<String> otherParents = new HashSet<>(INFO.getExplicitParents()); 90 // for (String locale : explicitParents) { 91 // while (true) { 92 // locale = LocaleIDParser.getParent(locale); 93 // if (locale == null || locale.equals("root")) { 94 // break; 95 // } 96 // otherParents.add(locale); 97 // } 98 // } 99 // otherParents.remove("root"); 100 // 101 // for (String locale : CONFIG.getCldrFactory().getAvailable()) { 102 // String parentId = LocaleIDParser.getParent(locale); 103 // String parentIdSimple = LocaleIDParser.getSimpleParent(locale); 104 // if (!explicitParents.contains(parentId) && !otherParents.contains(parentIdSimple)) { 105 // continue; 106 // } 107 // System.out.println(locale + "\t" + CONFIG.getEnglish().getName(locale) + "\t" + parentId + "\t" + parentIdSimple); 108 // } 109 // } 110 111 @Test 112 public void testChinese() { 113 LocaleMatcher matcher = newLocaleMatcher("zh_CN, zh_TW, iw"); 114 ULocale taiwanChinese = new ULocale("zh_TW"); 115 ULocale chinaChinese = new ULocale("zh_CN"); 116 assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant_TW")); 117 assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant")); 118 assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_TW")); 119 assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh_Hans_CN")); 120 assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh_CN")); 121 assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh")); 122 assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant_HK")); 123 } 124 125 @Test 126 public void testenGB() { 127 final LocaleMatcher matcher = newLocaleMatcher("fr, en, en_GB, es_MX, es_419, es"); 128 assertEquals("en_GB", matcher.getBestMatch("en_NZ").toString()); 129 assertEquals("es", matcher.getBestMatch("es_ES").toString()); 130 assertEquals("es_419", matcher.getBestMatch("es_AR").toString()); 131 assertEquals("es_MX", matcher.getBestMatch("es_MX").toString()); 132 } 133 134 @Test 135 public void testFallbacks() { 136 LocalePriorityList lpl = LocalePriorityList.add("en, hi").build(); 137 final LocaleMatcher matcher = newLocaleMatcher(lpl, null, 0.09); 138 assertEquals("hi", matcher.getBestMatch("sa").toString()); 139 } 140 141 @Test 142 public void testOverrideData() { 143 double threshold = 0.05; 144 LanguageMatcherData localeMatcherData = new LanguageMatcherData() 145 .addDistance("br", "fr", 10, true) 146 .addDistance("es", "cy", 10, true); 147 logln(localeMatcherData.toString()); 148 149 final LocaleMatcher matcher = newLocaleMatcher( 150 LocalePriorityList 151 .add(ULocale.ENGLISH) 152 .add(ULocale.FRENCH) 153 .add(ULocale.UK) 154 .build(), localeMatcherData, threshold); 155 logln(matcher.toString()); 156 157 assertEquals(ULocale.FRENCH, matcher.getBestMatch(new ULocale("br"))); 158 assertEquals(ULocale.ENGLISH, matcher.getBestMatch(new ULocale("es"))); // one 159 // way 160 } 161 162 @Test 163 public void testBasics() { 164 final LocaleMatcher matcher = newLocaleMatcher(LocalePriorityList.add(ULocale.FRENCH).add(ULocale.UK) 165 .add(ULocale.ENGLISH).build()); 166 logln(matcher.toString()); 167 168 assertEquals(ULocale.UK, matcher.getBestMatch(ULocale.UK)); 169 assertEquals(ULocale.ENGLISH, matcher.getBestMatch(ULocale.US)); 170 assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.FRANCE)); 171 assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.JAPAN)); 172 } 173 174 @Test 175 public void testFallback() { 176 // check that script fallbacks are handled right 177 final LocaleMatcher matcher = newLocaleMatcher("zh_CN, zh_TW, iw"); 178 assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant")); 179 assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh")); 180 assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh_Hans_CN")); 181 assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant_HK")); 182 assertEquals(new ULocale("he"), matcher.getBestMatch("iw_IT")); 183 } 184 185 @Test 186 public void testSpecials() { 187 // check that nearby languages are handled 188 final LocaleMatcher matcher = newLocaleMatcher("en, fil, ro, nn"); 189 assertEquals(new ULocale("fil"), matcher.getBestMatch("tl")); 190 assertEquals(new ULocale("ro"), matcher.getBestMatch("mo")); 191 assertEquals(new ULocale("nn"), matcher.getBestMatch("no")); // Google patch 192 // make sure default works 193 assertEquals(new ULocale("en"), matcher.getBestMatch("ja")); 194 } 195 196 @Test 197 public void testRegionalSpecials() { 198 // verify that en_AU is closer to en_GB than to en (which is en_US) 199 final LocaleMatcher matcher = newLocaleMatcher("en, en_GB, es, es_419"); 200 assertEquals("es_MX in {en, en_GB, es, es_419}", new ULocale("es_419"), matcher.getBestMatch("es_MX")); 201 assertEquals("en_AU in {en, en_GB, es, es_419}", new ULocale("en_GB"), matcher.getBestMatch("en_AU")); 202 assertEquals("es_ES in {en, en_GB, es, es_419}", new ULocale("es"), matcher.getBestMatch("es_ES")); 203 } 204 205 @Test 206 public void testHK() { 207 // HK and MO are closer to each other for Hant than to TW 208 final LocaleMatcher matcher = newLocaleMatcher("zh, zh_TW, zh_MO"); 209 assertEquals("zh_HK in {zh, zh_TW, zh_MO}", ZH_MO, matcher.getBestMatch("zh_HK")); 210 final LocaleMatcher matcher2 = newLocaleMatcher("zh, zh_TW, zh_HK"); 211 assertEquals("zh_MO in {zh, zh_TW, zh_HK}", ZH_HK, matcher2.getBestMatch("zh_MO")); 212 } 213 214 @Test 215 public void TestLocaleMatcherCoverage() { 216 // Add tests for better code coverage 217 LocaleMatcher matcher = newLocaleMatcher(LocalePriorityList.add(null, 0).build(), null); 218 logln(matcher.toString()); 219 220 LanguageMatcherData data = new LanguageMatcherData(); 221 222 LanguageMatcherData clone = data.cloneAsThawed(); 223 224 if (clone.equals(data)) { 225 errln("Error cloneAsThawed() is equal."); 226 } 227 228 if (data.isFrozen()) { 229 errln("Error LocaleMatcherData is frozen!"); 230 } 231 } 232 233 private void assertEquals(Object expected, Object string) { 234 assertEquals("", expected, string); 235 } 236 237 private void assertNull(Object bestMatch) { 238 assertNull("", bestMatch); 239 } 240 241 @Test 242 public void testEmpty() { 243 final LocaleMatcher matcher = newLocaleMatcher(""); 244 assertNull(matcher.getBestMatch(ULocale.FRENCH)); 245 } 246 247 static final ULocale ENGLISH_CANADA = new ULocale("en_CA"); 248 249 @Test 250 public void testMatch_exact() { 251 assertEquals(1.0, 252 LocaleMatcher.match(ENGLISH_CANADA, ENGLISH_CANADA)); 253 } 254 255 @Test 256 public void testMatch_none() { 257 double match = LocaleMatcher.match( 258 new ULocale("ar_MK"), 259 ENGLISH_CANADA); 260 assertTrue("Actual < 0: " + match, 0 <= match); 261 assertTrue("Actual > 0.15 (~ language + script distance): " + match, 0.2 > match); 262 } 263 264 @Test 265 public void testMatch_matchOnMazimized() { 266 ULocale undTw = new ULocale("und_TW"); 267 ULocale zhHant = new ULocale("zh_Hant"); 268 double matchZh = LocaleMatcher.match(undTw, new ULocale("zh")); 269 double matchZhHant = LocaleMatcher.match(undTw, zhHant); 270 assertTrue("und_TW should be closer to zh_Hant (" + matchZhHant + 271 ") than to zh (" + matchZh + ")", 272 matchZh < matchZhHant); 273 double matchEnHantTw = LocaleMatcher.match(new ULocale("en_Hant_TW"), 274 zhHant); 275 assertTrue("zh_Hant should be closer to und_TW (" + matchZhHant + 276 ") than to en_Hant_TW (" + matchEnHantTw + ")", 277 matchEnHantTw < matchZhHant); 278 assertTrue("zh should be closer to und_TW (" + matchZh + 279 ") than to en_Hant_TW (" + matchEnHantTw + ")", 280 matchEnHantTw < matchZh); 281 } 282 283 @Test 284 public void testMatchGrandfatheredCode() { 285 final LocaleMatcher matcher = newLocaleMatcher("fr, i_klingon, en_Latn_US"); 286 assertEquals("en_Latn_US", matcher.getBestMatch("en_GB_oed").toString()); 287 // assertEquals("tlh", matcher.getBestMatch("i_klingon").toString()); 288 } 289 290 @Test 291 public void testGetBestMatchForList_exactMatch() { 292 final LocaleMatcher matcher = newLocaleMatcher("fr, en_GB, ja, es_ES, es_MX"); 293 assertEquals("ja", matcher.getBestMatch("ja, de").toString()); 294 } 295 296 @Test 297 public void testGetBestMatchForList_simpleVariantMatch() { 298 final LocaleMatcher matcher = newLocaleMatcher("fr, en_GB, ja, es_ES, es_MX"); 299 // Intentionally avoiding a perfect_match or two candidates for variant 300 // matches. 301 assertEquals("en_GB", matcher.getBestMatch("de, en_US").toString()); 302 // Fall back. 303 assertEquals("fr", matcher.getBestMatch("de, zh").toString()); 304 } 305 306 @Test 307 public void testGetBestMatchForList_matchOnMaximized() { 308 final LocaleMatcher matcher = newLocaleMatcher("en, ja"); 309 // final LocaleMatcher matcher = 310 // newLocaleMatcher("fr, en, ja, es_ES, es_MX"); 311 // Check that if the preference is maximized already, it works as well. 312 assertEquals("Match for ja_Jpan_JP (maximized already)", 313 "ja", matcher.getBestMatch("ja_Jpan_JP, en-AU").toString()); 314 if (true) 315 return; 316 // ja_JP matches ja on likely subtags, and it's listed first, thus it 317 // wins over 318 // thus it wins over the second preference en_GB. 319 assertEquals("Match for ja_JP, with likely region subtag", 320 "ja", matcher.getBestMatch("ja_JP, en_US").toString()); 321 // Check that if the preference is maximized already, it works as well. 322 assertEquals("Match for ja_Jpan_JP (maximized already)", 323 "ja", matcher.getBestMatch("ja_Jpan_JP, en_US").toString()); 324 } 325 326 @Test 327 public void testGetBestMatchForList_noMatchOnMaximized() { 328 // Regression test for http://b/5714572 . 329 final LocaleMatcher matcher = newLocaleMatcher("en, de, fr, ja"); 330 // de maximizes to de_DE. Pick the exact match for the secondary 331 // language instead. 332 assertEquals("de", matcher.getBestMatch("de_CH, fr").toString()); 333 } 334 335 @Test 336 public void testBestMatchForTraditionalChinese() { 337 // Scenario: An application that only supports Simplified Chinese (and 338 // some other languages), 339 // but does not support Traditional Chinese. zh_Hans_CN could be 340 // replaced with zh_CN, zh, or 341 // zh_Hans, it wouldn't make much of a difference. 342 final LocaleMatcher matcher = newLocaleMatcher("fr, zh_Hans_CN, en_US"); 343 344 // The script distance (simplified vs. traditional Han) is considered 345 // small enough 346 // to be an acceptable match. The regional difference is considered 347 // almost insignificant. 348 assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_TW").toString()); 349 assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_Hant").toString()); 350 351 // For geo_political reasons, you might want to avoid a zh_Hant -> 352 // zh_Hans match. 353 // In this case, if zh_TW, zh_HK or a tag starting with zh_Hant is 354 // requested, you can 355 // change your call to getBestMatch to include a 2nd language 356 // preference. 357 // "en" is a better match since its distance to "en_US" is closer than 358 // the distance 359 // from "zh_TW" to "zh_CN" (script distance). 360 assertEquals("en_US", matcher.getBestMatch("zh_TW, en").toString()); 361 assertEquals("en_US", matcher.getBestMatch("zh_Hant_CN, en").toString()); 362 assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_Hans, en").toString()); 363 } 364 365 @Test 366 public void testUndefined() { 367 // When the undefined language doesn't match anything in the list, 368 // getBestMatch returns 369 // the default, as usual. 370 LocaleMatcher matcher = newLocaleMatcher("it,fr"); 371 assertEquals("it", matcher.getBestMatch("und").toString()); 372 373 // When it *does* occur in the list, BestMatch returns it, as expected. 374 matcher = newLocaleMatcher("it,und"); 375 assertEquals("und", matcher.getBestMatch("und").toString()); 376 377 // The unusual part: 378 // max("und") = "en_Latn_US", and since matching is based on maximized 379 // tags, the undefined 380 // language would normally match English. But that would produce the 381 // counterintuitive results 382 // that getBestMatch("und", LocaleMatcher("it,en")) would be "en", and 383 // getBestMatch("en", LocaleMatcher("it,und")) would be "und". 384 // 385 // To avoid that, we change the matcher's definitions of max 386 // (AddLikelySubtagsWithDefaults) 387 // so that max("und")="und". That produces the following, more desirable 388 // results: 389 matcher = newLocaleMatcher("it,en"); 390 assertEquals("it", matcher.getBestMatch("und").toString()); 391 matcher = newLocaleMatcher("it,und"); 392 assertEquals("it", matcher.getBestMatch("en").toString()); 393 } 394 395 // public void testGetBestMatch_emptyList() { 396 // final LocaleMatcher matcher = newLocaleMatcher( 397 // new LocalePriorityList(new HashMap())); 398 // assertNull(matcher.getBestMatch(ULocale.ENGLISH)); 399 // } 400 401 @Test 402 public void testGetBestMatch_googlePseudoLocales() { 403 // Google pseudo locales are primarily based on variant subtags. 404 // See http://sites/intl_eng/pseudo_locales. 405 // (See below for the region code based fall back options.) 406 final LocaleMatcher matcher = newLocaleMatcher( 407 "fr, pt"); 408 assertEquals("fr", matcher.getBestMatch("de").toString()); 409 assertEquals("fr", matcher.getBestMatch("en_US").toString()); 410 assertEquals("fr", matcher.getBestMatch("en").toString()); 411 assertEquals("pt", matcher.getBestMatch("pt_BR").toString()); 412 } 413 414 @Test 415 public void testGetBestMatch_regionDistance() { 416 LocaleMatcher matcher = newLocaleMatcher("es_AR, es"); 417 assertEquals("es_AR", matcher.getBestMatch("es_MX").toString()); 418 419 matcher = newLocaleMatcher("fr, en, en_GB"); 420 assertEquals("en_GB", matcher.getBestMatch("en_CA").toString()); 421 422 matcher = newLocaleMatcher("de_AT, de_DE, de_CH"); 423 assertEquals("de_DE", matcher.getBestMatch("de").toString()); 424 425 showDistance(matcher, "en", "en_CA"); 426 showDistance(matcher, "en_CA", "en"); 427 showDistance(matcher, "en_US", "en_CA"); 428 showDistance(matcher, "en_CA", "en_US"); 429 showDistance(matcher, "en_GB", "en_CA"); 430 showDistance(matcher, "en_CA", "en_GB"); 431 showDistance(matcher, "en", "en_UM"); 432 showDistance(matcher, "en_UM", "en"); 433 } 434 435 private void showDistance(LocaleMatcher matcher, String desired, String supported) { 436 ULocale desired2 = new ULocale(desired); 437 ULocale supported2 = new ULocale(supported); 438 double distance = matcher.match(desired2, ULocale.addLikelySubtags(desired2), supported2, ULocale.addLikelySubtags(supported2)); 439 logln(desired + " to " + supported + " :\t" + distance); 440 } 441 442 /** 443 * If all the base languages are the same, then each sublocale matches 444 * itself most closely 445 */ 446 @Test 447 public void testExactMatches() { 448 String lastBase = ""; 449 TreeSet<ULocale> sorted = new TreeSet<ULocale>(); 450 for (ULocale loc : ULocale.getAvailableLocales()) { 451 String language = loc.getLanguage(); 452 if (!lastBase.equals(language)) { 453 check(sorted); 454 sorted.clear(); 455 lastBase = language; 456 } 457 sorted.add(loc); 458 } 459 check(sorted); 460 } 461 462 private void check(Set<ULocale> sorted) { 463 if (sorted.isEmpty()) { 464 return; 465 } 466 check2(sorted); 467 ULocale first = sorted.iterator().next(); 468 ULocale max = ULocale.addLikelySubtags(first); 469 sorted.add(max); 470 check2(sorted); 471 } 472 473 /** 474 * @param sorted 475 */ 476 private void check2(Set<ULocale> sorted) { 477 // TODO Auto-generated method stub 478 logln("Checking: " + sorted); 479 LocaleMatcher matcher = newLocaleMatcher( 480 LocalePriorityList.add( 481 sorted.toArray(new ULocale[sorted.size()])) 482 .build()); 483 for (ULocale loc : sorted) { 484 String stringLoc = loc.toString(); 485 assertEquals(stringLoc, matcher.getBestMatch(stringLoc).toString()); 486 } 487 } 488 489 @Test 490 public void testAsymmetry() { 491 LocaleMatcher matcher; 492 matcher = new LocaleMatcher("mul, nl"); 493 assertEquals("nl", matcher.getBestMatch("af").toString()); // af => nl 494 495 matcher = new LocaleMatcher("mul, af"); 496 assertEquals("mul", matcher.getBestMatch("nl").toString()); // but nl !=> af 497 } 498 499 500 // public void testComputeDistance_monkeyTest() { 501 // RegionCode[] codes = RegionCode.values(); 502 // Random random = new Random(); 503 // for (int i = 0; i < 1000; ++i) { 504 // RegionCode x = codes[random.nextInt(codes.length)]; 505 // RegionCode y = codes[random.nextInt(codes.length)]; 506 // double d = LocaleMatcher.getRegionDistance(x, y, null, null); 507 // if (x == RegionCode.ZZ || y == RegionCode.ZZ) { 508 // assertEquals(LocaleMatcher.REGION_DISTANCE, d); 509 // } else if (x == y) { 510 // assertEquals(0.0, d); 511 // } else { 512 // assertTrue(d > 0); 513 // assertTrue(d <= LocaleMatcher.REGION_DISTANCE); 514 // } 515 // } 516 // } 517 518 @Test 519 public void testGetBestMatchForList_matchOnMaximized2() { 520// if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) { 521// return; 522// } 523 final LocaleMatcher matcher = newLocaleMatcher("fr, en-GB, ja, es-ES, es-MX"); 524 // ja-JP matches ja on likely subtags, and it's listed first, thus it wins over 525 // thus it wins over the second preference en-GB. 526 assertEquals("Match for ja-JP, with likely region subtag", 527 "ja", matcher.getBestMatch("ja-JP, en-GB").toString()); 528 // Check that if the preference is maximized already, it works as well. 529 assertEquals("Match for ja-Jpan-JP (maximized already)", 530 "ja", matcher.getBestMatch("ja-Jpan-JP, en-GB").toString()); 531 } 532 533 @Test 534 public void testGetBestMatchForList_closeEnoughMatchOnMaximized() { 535// if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) { 536// return; 537// } 538 final LocaleMatcher matcher = newLocaleMatcher("en-GB, en, de, fr, ja"); 539 assertEquals("de", matcher.getBestMatch("de-CH, fr").toString()); 540 assertEquals("en", matcher.getBestMatch("en-US, ar, nl, de, ja").toString()); 541 } 542 543 @Test 544 public void testGetBestMatchForPortuguese() { 545 546// if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) { 547// return; 548// } 549 550 final LocaleMatcher withPTExplicit = newLocaleMatcher("pt_PT, pt_BR, es, es_419"); 551 final LocaleMatcher withPTImplicit = newLocaleMatcher("pt_PT, pt, es, es_419"); 552 // Could happen because "pt_BR" is a tier_1 language and "pt_PT" is tier_2. 553 554 final LocaleMatcher withoutPT = newLocaleMatcher("pt_BR, es, es_419"); 555 // European user who prefers Spanish over Brazillian Portuguese as a fallback. 556 557 assertEquals("pt_PT", withPTExplicit.getBestMatch("pt_PT, es, pt").toString()); 558 assertEquals("pt_PT", withPTImplicit.getBestMatch("pt_PT, es, pt").toString()); 559 assertEquals("es", withoutPT.getBestMatch("pt_PT, es, pt").toString()); 560 561 // Brazillian user who prefers South American Spanish over European Portuguese as a fallback. 562 // The asymmetry between this case and above is because it's "pt_PT" that's missing between the 563 // matchers as "pt_BR" is a much more common language. 564 assertEquals("pt_BR", withPTExplicit.getBestMatch("pt, es_419, pt_PT").toString()); 565 assertEquals("pt", withPTImplicit.getBestMatch("pt, es_419, pt_PT").toString()); 566 assertEquals("pt_BR", withoutPT.getBestMatch("pt, es_419, pt_PT").toString()); 567 568 // Code that adds the user's country can get "pt_US" for a user's language. 569 // That should fall back to "pt_BR". 570 assertEquals("pt_BR", withPTExplicit.getBestMatch("pt_US, pt_PT").toString()); 571 assertEquals("pt", withPTImplicit.getBestMatch("pt_US, pt_PT").toString()); 572 } 573 574 @Test 575 public void testVariantWithScriptMatch() { 576// if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) { 577// return; 578// } 579 final LocaleMatcher matcher = newLocaleMatcher("fr, en, sv"); 580 assertEquals("en", matcher.getBestMatch("en-GB").toString()); 581 assertEquals("en", matcher.getBestMatch("en-GB, sv").toString()); 582 } 583 584 @Test 585 public void testVariantWithScriptMatch2() { 586// if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) { 587// return; 588// } 589 final LocaleMatcher matcher = newLocaleMatcher("en, sv"); 590 assertEquals("en", matcher.getBestMatch("en-GB, sv").toString()); 591 } 592 593 @Test 594 public void testPerf() { 595 if (LANGUAGE_MATCHER_DATA == null) { 596 return; // skip except when testing data 597 } 598 final String desired = "sv, en"; 599 600 final LocaleMatcher matcherShort = newLocaleMatcher(desired); 601 final LocaleMatcher matcherLong = newLocaleMatcher("af, am, ar, az, be, bg, bn, bs, ca, cs, cy, cy, da, de, el, en, en-GB, es, es-419, et, eu, fa, fi, fil, fr, ga, gl, gu, hi, hr, hu, hy, id, is, it, iw, ja, ka, kk, km, kn, ko, ky, lo, lt, lv, mk, ml, mn, mr, ms, my, ne, nl, no, pa, pl, pt, pt-PT, ro, ru, si, sk, sl, sq, sr, sr-Latn, sv, sw, ta, te, th, tr, uk, ur, uz, vi, zh-CN, zh-TW, zu"); 602 final LocaleMatcher matcherVeryLong = newLocaleMatcher("af, af_NA, af_ZA, agq, agq_CM, ak, ak_GH, am, am_ET, ar, ar_001, ar_AE, ar_BH, ar_DJ, ar_DZ, ar_EG, ar_EH, ar_ER, ar_IL, ar_IQ, ar_JO, ar_KM, ar_KW, ar_LB, ar_LY, ar_MA, ar_MR, ar_OM, ar_PS, ar_QA, ar_SA, ar_SD, ar_SO, ar_SS, ar_SY, ar_TD, ar_TN, ar_YE, as, as_IN, asa, asa_TZ, ast, ast_ES, az, az_Cyrl, az_Cyrl_AZ, az_Latn, az_Latn_AZ, bas, bas_CM, be, be_BY, bem, bem_ZM, bez, bez_TZ, bg, bg_BG, bm, bm_ML, bn, bn_BD, bn_IN, bo, bo_CN, bo_IN, br, br_FR, brx, brx_IN, bs, bs_Cyrl, bs_Cyrl_BA, bs_Latn, bs_Latn_BA, ca, ca_AD, ca_ES, ca_ES_VALENCIA, ca_FR, ca_IT, ce, ce_RU, cgg, cgg_UG, chr, chr_US, ckb, ckb_IQ, ckb_IR, cs, cs_CZ, cu, cu_RU, cy, cy_GB, da, da_DK, da_GL, dav, dav_KE, de, de_AT, de_BE, de_CH, de_DE, de_LI, de_LU, dje, dje_NE, dsb, dsb_DE, dua, dua_CM, dyo, dyo_SN, dz, dz_BT, ebu, ebu_KE, ee, ee_GH, ee_TG, el, el_CY, el_GR, en, en_001, en_150, en_AG, en_AI, en_AS, en_AT, en_AU, en_BB, en_BE, en_BI, en_BM, en_BS, en_BW, en_BZ, en_CA, en_CC, en_CH, en_CK, en_CM, en_CX, en_CY, en_DE, en_DG, en_DK, en_DM, en_ER, en_FI, en_FJ, en_FK, en_FM, en_GB, en_GD, en_GG, en_GH, en_GI, en_GM, en_GU, en_GY, en_HK, en_IE, en_IL, en_IM, en_IN, en_IO, en_JE, en_JM, en_KE, en_KI, en_KN, en_KY, en_LC, en_LR, en_LS, en_MG, en_MH, en_MO, en_MP, en_MS, en_MT, en_MU, en_MW, en_MY, en_NA, en_NF, en_NG, en_NL, en_NR, en_NU, en_NZ, en_PG, en_PH, en_PK, en_PN, en_PR, en_PW, en_RW, en_SB, en_SC, en_SD, en_SE, en_SG, en_SH, en_SI, en_SL, en_SS, en_SX, en_SZ, en_TC, en_TK, en_TO, en_TT, en_TV, en_TZ, en_UG, en_UM, en_US, en_US_POSIX, en_VC, en_VG, en_VI, en_VU, en_WS, en_ZA, en_ZM, en_ZW, eo, eo_001, es, es_419, es_AR, es_BO, es_CL, es_CO, es_CR, es_CU, es_DO, es_EA, es_EC, es_ES, es_GQ, es_GT, es_HN, es_IC, es_MX, es_NI, es_PA, es_PE, es_PH, es_PR, es_PY, es_SV, es_US, es_UY, es_VE, et, et_EE, eu, eu_ES, ewo, ewo_CM, fa, fa_AF, fa_IR, ff, ff_CM, ff_GN, ff_MR, ff_SN, fi, fi_FI, fil, fil_PH, fo, fo_DK, fo_FO, fr, fr_BE, fr_BF, fr_BI, fr_BJ, fr_BL, fr_CA, fr_CD, fr_CF, fr_CG, fr_CH, fr_CI, fr_CM, fr_DJ, fr_DZ, fr_FR, fr_GA, fr_GF, fr_GN, fr_GP, fr_GQ, fr_HT, fr_KM, fr_LU, fr_MA, fr_MC, fr_MF, fr_MG, fr_ML, fr_MQ, fr_MR, fr_MU, fr_NC, fr_NE, fr_PF, fr_PM, fr_RE, fr_RW, fr_SC, fr_SN, fr_SY, fr_TD, fr_TG, fr_TN, fr_VU, fr_WF, fr_YT, fur, fur_IT, fy, fy_NL, ga, ga_IE, gd, gd_GB, gl, gl_ES, gsw, gsw_CH, gsw_FR, gsw_LI, gu, gu_IN, guz, guz_KE, gv, gv_IM, ha, ha_GH, ha_NE, ha_NG, haw, haw_US, he, he_IL, hi, hi_IN, hr, hr_BA, hr_HR, hsb, hsb_DE, hu, hu_HU, hy, hy_AM, id, id_ID, ig, ig_NG, ii, ii_CN, is, is_IS, it, it_CH, it_IT, it_SM, ja, ja_JP, jgo, jgo_CM, jmc, jmc_TZ, ka, ka_GE, kab, kab_DZ, kam, kam_KE, kde, kde_TZ, kea, kea_CV, khq, khq_ML, ki, ki_KE, kk, kk_KZ, kkj, kkj_CM, kl, kl_GL, kln, kln_KE, km, km_KH, kn, kn_IN, ko, ko_KP, ko_KR, kok, kok_IN, ks, ks_IN, ksb, ksb_TZ, ksf, ksf_CM, ksh, ksh_DE, kw, kw_GB, ky, ky_KG, lag, lag_TZ, lb, lb_LU, lg, lg_UG, lkt, lkt_US, ln, ln_AO, ln_CD, ln_CF, ln_CG, lo, lo_LA, lrc, lrc_IQ, lrc_IR, lt, lt_LT, lu, lu_CD, luo, luo_KE, luy, luy_KE, lv, lv_LV, mas, mas_KE, mas_TZ, mer, mer_KE, mfe, mfe_MU, mg, mg_MG, mgh, mgh_MZ, mgo, mgo_CM, mk, mk_MK, ml, ml_IN, mn, mn_MN, mr, mr_IN, ms, ms_BN, ms_MY, ms_SG, mt, mt_MT, mua, mua_CM, my, my_MM, mzn, mzn_IR, naq, naq_NA, nb, nb_NO, nb_SJ, nd, nd_ZW, ne, ne_IN, ne_NP, nl, nl_AW, nl_BE, nl_BQ, nl_CW, nl_NL, nl_SR, nl_SX, nmg, nmg_CM, nn, nn_NO, nnh, nnh_CM, nus, nus_SS, nyn, nyn_UG, om, om_ET, om_KE, or, or_IN, os, os_GE, os_RU, pa, pa_Arab, pa_Arab_PK, pa_Guru, pa_Guru_IN, pl, pl_PL, prg, prg_001, ps, ps_AF, pt, pt_AO, pt_BR, pt_CV, pt_GW, pt_MO, pt_MZ, pt_PT, pt_ST, pt_TL, qu, qu_BO, qu_EC, qu_PE, rm, rm_CH, rn, rn_BI, ro, ro_MD, ro_RO, rof, rof_TZ, root, ru, ru_BY, ru_KG, ru_KZ, ru_MD, ru_RU, ru_UA, rw, rw_RW, rwk, rwk_TZ, sah, sah_RU, saq, saq_KE, sbp, sbp_TZ, se, se_FI, se_NO, se_SE, seh, seh_MZ, ses, ses_ML, sg, sg_CF, shi, shi_Latn, shi_Latn_MA, shi_Tfng, shi_Tfng_MA, si, si_LK, sk, sk_SK, sl, sl_SI, smn, smn_FI, sn, sn_ZW, so, so_DJ, so_ET, so_KE, so_SO, sq, sq_AL, sq_MK, sq_XK, sr, sr_Cyrl, sr_Cyrl_BA, sr_Cyrl_ME, sr_Cyrl_RS, sr_Cyrl_XK, sr_Latn, sr_Latn_BA, sr_Latn_ME, sr_Latn_RS, sr_Latn_XK, sv, sv_AX, sv_FI, sv_SE, sw, sw_CD, sw_KE, sw_TZ, sw_UG, ta, ta_IN, ta_LK, ta_MY, ta_SG, te, te_IN, teo, teo_KE, teo_UG, th, th_TH, ti, ti_ER, ti_ET, tk, tk_TM, to, to_TO, tr, tr_CY, tr_TR, twq, twq_NE, tzm, tzm_MA, ug, ug_CN, uk, uk_UA, ur, ur_IN, ur_PK, uz, uz_Arab, uz_Arab_AF, uz_Cyrl, uz_Cyrl_UZ, uz_Latn, uz_Latn_UZ, vai, vai_Latn, vai_Latn_LR, vai_Vaii, vai_Vaii_LR, vi, vi_VN, vo, vo_001, vun, vun_TZ, wae, wae_CH, xog, xog_UG, yav, yav_CM, yi, yi_001, yo, yo_BJ, yo_NG, zgh, zgh_MA, zh, zh_Hans, zh_Hans_CN, zh_Hans_HK, zh_Hans_MO, zh_Hans_SG, zh_Hant, zh_Hant_HK, zh_Hant_MO, zh_Hant_TW, zu, zu_ZA"); 603 604 //LocaleMatcher.DEBUG = true; 605 ULocale expected = new ULocale("sv"); 606 assertEquals(expected, matcherShort.getBestMatch(desired)); 607 assertEquals(expected, matcherLong.getBestMatch(desired)); 608 assertEquals(expected, matcherVeryLong.getBestMatch(desired)); 609 //LocaleMatcher.DEBUG = false; 610 611 for (int i = 0; i < 2; ++i) { 612 int iterations = i == 0 ? 1000 : 100000; 613 boolean showMessage = i != 0; 614 long timeShort = timeLocaleMatcher("Duration (few supported):\t", desired, matcherShort, showMessage, iterations, 0); 615 @SuppressWarnings("unused") 616 long timeMedium = timeLocaleMatcher("Duration (med. supported):\t", desired, matcherLong, showMessage, iterations, timeShort); 617 @SuppressWarnings("unused") 618 long timeLong = timeLocaleMatcher("Duration (many supported):\t", desired, matcherVeryLong, showMessage, iterations, timeShort); 619 } 620 } 621 622 private long timeLocaleMatcher(String title, String desired, LocaleMatcher matcher, 623 boolean showmessage, int iterations, long comparisonTime) { 624 long start = System.nanoTime(); 625 for (int i = iterations; i > 0; --i) { 626 matcher.getBestMatch(desired); 627 } 628 long delta = System.nanoTime() - start; 629 if (showmessage) warnln(title + (delta / iterations) + " nanos, " 630 + (comparisonTime > 0 ? (delta * 100 / comparisonTime - 100) + "% longer" : "")); 631 return delta; 632 } 633 634 @Test 635 public void Test8288() { 636 final LocaleMatcher matcher = newLocaleMatcher("it, en"); 637 assertEquals("it", matcher.getBestMatch("und").toString()); 638 assertEquals("en", matcher.getBestMatch("und, en").toString()); 639 } 640} 641