1/* GENERATED SOURCE. DO NOT MODIFY. */
2/*
3 ******************************************************************************************
4 * Copyright (C) 2009-2015, Google, Inc.; International Business Machines Corporation and *
5 * others. All Rights Reserved.                                                           *
6 ******************************************************************************************
7 */
8
9package android.icu.dev.test.util;
10
11import java.util.Set;
12import java.util.TreeSet;
13
14import android.icu.dev.test.TestFmwk;
15import android.icu.util.LocaleMatcher;
16import android.icu.util.LocaleMatcher.LanguageMatcherData;
17import android.icu.util.LocalePriorityList;
18import android.icu.util.ULocale;
19import org.junit.runner.RunWith;
20import android.icu.junit.IcuTestFmwkRunner;
21
22/**
23 * Test the LocaleMatcher.
24 *
25 * @author markdavis
26 */
27@RunWith(IcuTestFmwkRunner.class)
28@SuppressWarnings("deprecation")
29public class LocaleMatcherTest extends TestFmwk {
30
31
32    private static final ULocale ZH_MO = new ULocale("zh_MO");
33    private static final ULocale ZH_HK = new ULocale("zh_HK");
34    static LanguageMatcherData LANGUAGE_MATCHER_DATA = LocaleMatcherShim.load();
35
36    private LocaleMatcher newLocaleMatcher(LocalePriorityList build) {
37        return new LocaleMatcher(build, LANGUAGE_MATCHER_DATA);
38    }
39
40    private LocaleMatcher newLocaleMatcher(LocalePriorityList build, LanguageMatcherData data) {
41        return new LocaleMatcher(build, data == null ? LANGUAGE_MATCHER_DATA : data);
42    }
43
44    private LocaleMatcher newLocaleMatcher(LocalePriorityList lpl, LanguageMatcherData data, double d) {
45        return new LocaleMatcher(lpl, data == null ? LANGUAGE_MATCHER_DATA : data, d);
46    }
47
48    private LocaleMatcher newLocaleMatcher(String string) {
49        return new LocaleMatcher(LocalePriorityList.add(string).build(), LANGUAGE_MATCHER_DATA);
50    }
51
52    // public LocaleMatcher(LocalePriorityList languagePriorityList,
53    // LocaleMatcherData matcherData, double threshold)
54
55    public static void main(String[] args) throws Exception {
56        new LocaleMatcherTest().run(args);
57    }
58
59    public void testParentLocales() {
60        assertCloser("es_AR", "es_419", "es_ES");
61        assertCloser("es_AR", "es_419", "es");
62
63        assertCloser("es_AR", "es_MX", "es");
64        assertCloser("es_AR", "es_MX", "es");
65
66        assertCloser("en_AU", "en_GB", "en_US");
67        assertCloser("en_AU", "en_GB", "en");
68
69        assertCloser("en_AU", "en_NZ", "en_US");
70        assertCloser("en_AU", "en_NZ", "en");
71
72        assertCloser("pt_AO", "pt_PT", "pt_BR");
73        assertCloser("pt_AO", "pt_PT", "pt");
74
75        assertCloser("zh_HK", "zh_MO", "zh_TW");
76        assertCloser("zh_HK", "zh_MO", "zh_CN");
77        assertCloser("zh_HK", "zh_MO", "zh");
78    }
79
80    private void assertCloser(String a, String closer, String further) {
81        LocaleMatcher matcher = newLocaleMatcher(further + ", " + closer);
82        assertEquals("test " + a + " is closer to " + closer + " than to " + further, new ULocale(closer), matcher.getBestMatch(a));
83        matcher = newLocaleMatcher(closer + ", " + further);
84        assertEquals("test " + a + " is closer to " + closer + " than to " + further, new ULocale(closer), matcher.getBestMatch(a));
85    }
86
87    //    public void testParentLocales() {
88    //        // find all the regions that have a closer relation because of an explicit parent
89    //        Set<String> explicitParents = new HashSet<>(INFO.getExplicitParents());
90    //        explicitParents.remove("root");
91    //        Set<String> otherParents = new HashSet<>(INFO.getExplicitParents());
92    //        for (String locale : explicitParents) {
93    //            while (true) {
94    //                locale = LocaleIDParser.getParent(locale);
95    //                if (locale == null || locale.equals("root")) {
96    //                    break;
97    //                }
98    //                otherParents.add(locale);
99    //            }
100    //        }
101    //        otherParents.remove("root");
102    //
103    //        for (String locale : CONFIG.getCldrFactory().getAvailable()) {
104    //            String parentId = LocaleIDParser.getParent(locale);
105    //            String parentIdSimple = LocaleIDParser.getSimpleParent(locale);
106    //            if (!explicitParents.contains(parentId) && !otherParents.contains(parentIdSimple)) {
107    //                continue;
108    //            }
109    //            System.out.println(locale + "\t" + CONFIG.getEnglish().getName(locale) + "\t" + parentId + "\t" + parentIdSimple);
110    //        }
111    //    }
112
113    public void testChinese() {
114        LocaleMatcher matcher = newLocaleMatcher("zh_CN, zh_TW, iw");
115        ULocale taiwanChinese = new ULocale("zh_TW");
116        ULocale chinaChinese = new ULocale("zh_CN");
117        assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant_TW"));
118        assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant"));
119        assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_TW"));
120        assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh_Hans_CN"));
121        assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh_CN"));
122        assertEquals("zh_CN, zh_TW, iw;", chinaChinese, matcher.getBestMatch("zh"));
123        assertEquals("zh_CN, zh_TW, iw;", taiwanChinese, matcher.getBestMatch("zh_Hant_HK"));
124    }
125
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    public void testFallbacks() {
135        LocalePriorityList lpl = LocalePriorityList.add("en, hi").build();
136        final LocaleMatcher matcher = newLocaleMatcher(lpl, null, 0.09);
137        assertEquals("hi", matcher.getBestMatch("sa").toString());
138    }
139
140    public void testOverrideData() {
141        double threshold = 0.05;
142        LanguageMatcherData localeMatcherData = new LanguageMatcherData()
143        .addDistance("br", "fr", 10, true)
144        .addDistance("es", "cy", 10, true);
145        logln(localeMatcherData.toString());
146
147        final LocaleMatcher matcher = newLocaleMatcher(
148            LocalePriorityList
149            .add(ULocale.ENGLISH)
150            .add(ULocale.FRENCH)
151            .add(ULocale.UK)
152            .build(), localeMatcherData, threshold);
153        logln(matcher.toString());
154
155        assertEquals(ULocale.FRENCH, matcher.getBestMatch(new ULocale("br")));
156        assertEquals(ULocale.ENGLISH, matcher.getBestMatch(new ULocale("es"))); // one
157        // way
158    }
159
160    public void testBasics() {
161        final LocaleMatcher matcher = newLocaleMatcher(LocalePriorityList.add(ULocale.FRENCH).add(ULocale.UK)
162            .add(ULocale.ENGLISH).build());
163        logln(matcher.toString());
164
165        assertEquals(ULocale.UK, matcher.getBestMatch(ULocale.UK));
166        assertEquals(ULocale.ENGLISH, matcher.getBestMatch(ULocale.US));
167        assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.FRANCE));
168        assertEquals(ULocale.FRENCH, matcher.getBestMatch(ULocale.JAPAN));
169    }
170
171    public void testFallback() {
172        // check that script fallbacks are handled right
173        final LocaleMatcher matcher = newLocaleMatcher("zh_CN, zh_TW, iw");
174        assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant"));
175        assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh"));
176        assertEquals(new ULocale("zh_CN"), matcher.getBestMatch("zh_Hans_CN"));
177        assertEquals(new ULocale("zh_TW"), matcher.getBestMatch("zh_Hant_HK"));
178        assertEquals(new ULocale("he"), matcher.getBestMatch("iw_IT"));
179    }
180
181    public void testSpecials() {
182        // check that nearby languages are handled
183        final LocaleMatcher matcher = newLocaleMatcher("en, fil, ro, nn");
184        assertEquals(new ULocale("fil"), matcher.getBestMatch("tl"));
185        assertEquals(new ULocale("ro"), matcher.getBestMatch("mo"));
186        assertEquals(new ULocale("nn"), matcher.getBestMatch("nb"));
187        // make sure default works
188        assertEquals(new ULocale("en"), matcher.getBestMatch("ja"));
189    }
190
191    public void testRegionalSpecials() {
192        // verify that en_AU is closer to en_GB than to en (which is en_US)
193        final LocaleMatcher matcher = newLocaleMatcher("en, en_GB, es, es_419");
194        assertEquals("es_MX in {en, en_GB, es, es_419}", new ULocale("es_419"), matcher.getBestMatch("es_MX"));
195        assertEquals("en_AU in {en, en_GB, es, es_419}", new ULocale("en_GB"), matcher.getBestMatch("en_AU"));
196        assertEquals("es_ES in {en, en_GB, es, es_419}", new ULocale("es"), matcher.getBestMatch("es_ES"));
197    }
198
199    public void testHK() {
200        // HK and MO are closer to each other for Hant than to TW
201        final LocaleMatcher matcher = newLocaleMatcher("zh, zh_TW, zh_MO");
202        assertEquals("zh_HK in {zh, zh_TW, zh_MO}", ZH_MO, matcher.getBestMatch("zh_HK"));
203        final LocaleMatcher matcher2 = newLocaleMatcher("zh, zh_TW, zh_HK");
204        assertEquals("zh_MO in {zh, zh_TW, zh_HK}", ZH_HK, matcher2.getBestMatch("zh_MO"));
205    }
206
207    public void TestLocaleMatcherCoverage() {
208        // Add tests for better code coverage
209        LocaleMatcher matcher = newLocaleMatcher(LocalePriorityList.add(null, 0).build(), null);
210        logln(matcher.toString());
211
212        LanguageMatcherData data = new LanguageMatcherData();
213
214        LanguageMatcherData clone = data.cloneAsThawed();
215
216        if (clone.equals(data)) {
217            errln("Error cloneAsThawed() is equal.");
218        }
219
220        if (data.isFrozen()) {
221            errln("Error LocaleMatcherData is frozen!");
222        }
223    }
224
225    private void assertEquals(Object expected, Object string) {
226        assertEquals("", expected, string);
227    }
228
229    private void assertNull(Object bestMatch) {
230        assertNull("", bestMatch);
231    }
232
233    public void testEmpty() {
234        final LocaleMatcher matcher = newLocaleMatcher("");
235        assertNull(matcher.getBestMatch(ULocale.FRENCH));
236    }
237
238    static final ULocale ENGLISH_CANADA = new ULocale("en_CA");
239
240    public void testMatch_exact() {
241        assertEquals(1.0,
242            LocaleMatcher.match(ENGLISH_CANADA, ENGLISH_CANADA));
243    }
244
245    public void testMatch_none() {
246        double match = LocaleMatcher.match(
247            new ULocale("ar_MK"),
248            ENGLISH_CANADA);
249        assertTrue("Actual < 0: " + match, 0 <= match);
250        assertTrue("Actual > 0.15 (~ language + script distance): " + match, 0.2 > match);
251    }
252
253    public void testMatch_matchOnMazimized() {
254        ULocale undTw = new ULocale("und_TW");
255        ULocale zhHant = new ULocale("zh_Hant");
256        double matchZh = LocaleMatcher.match(undTw, new ULocale("zh"));
257        double matchZhHant = LocaleMatcher.match(undTw, zhHant);
258        assertTrue("und_TW should be closer to zh_Hant (" + matchZhHant +
259            ") than to zh (" + matchZh + ")",
260            matchZh < matchZhHant);
261        double matchEnHantTw = LocaleMatcher.match(new ULocale("en_Hant_TW"),
262            zhHant);
263        assertTrue("zh_Hant should be closer to und_TW (" + matchZhHant +
264            ") than to en_Hant_TW (" + matchEnHantTw + ")",
265            matchEnHantTw < matchZhHant);
266        assertTrue("zh should be closer to und_TW (" + matchZh +
267            ") than to en_Hant_TW (" + matchEnHantTw + ")",
268            matchEnHantTw < matchZh);
269    }
270
271    public void testMatchGrandfatheredCode() {
272        final LocaleMatcher matcher = newLocaleMatcher("fr, i_klingon, en_Latn_US");
273        assertEquals("en_Latn_US", matcher.getBestMatch("en_GB_oed").toString());
274        // assertEquals("tlh", matcher.getBestMatch("i_klingon").toString());
275    }
276
277    public void testGetBestMatchForList_exactMatch() {
278        final LocaleMatcher matcher = newLocaleMatcher("fr, en_GB, ja, es_ES, es_MX");
279        assertEquals("ja", matcher.getBestMatch("ja, de").toString());
280    }
281
282    public void testGetBestMatchForList_simpleVariantMatch() {
283        final LocaleMatcher matcher = newLocaleMatcher("fr, en_GB, ja, es_ES, es_MX");
284        // Intentionally avoiding a perfect_match or two candidates for variant
285        // matches.
286        assertEquals("en_GB", matcher.getBestMatch("de, en_US").toString());
287        // Fall back.
288        assertEquals("fr", matcher.getBestMatch("de, zh").toString());
289    }
290
291    public void testGetBestMatchForList_matchOnMaximized() {
292        final LocaleMatcher matcher = newLocaleMatcher("en, ja");
293        // final LocaleMatcher matcher =
294        // newLocaleMatcher("fr, en, ja, es_ES, es_MX");
295        // Check that if the preference is maximized already, it works as well.
296        assertEquals("Match for ja_Jpan_JP (maximized already)",
297            "ja", matcher.getBestMatch("ja_Jpan_JP, en-AU").toString());
298        if (true)
299            return;
300        // ja_JP matches ja on likely subtags, and it's listed first, thus it
301        // wins over
302        // thus it wins over the second preference en_GB.
303        assertEquals("Match for ja_JP, with likely region subtag",
304            "ja", matcher.getBestMatch("ja_JP, en_US").toString());
305        // Check that if the preference is maximized already, it works as well.
306        assertEquals("Match for ja_Jpan_JP (maximized already)",
307            "ja", matcher.getBestMatch("ja_Jpan_JP, en_US").toString());
308    }
309
310    public void testGetBestMatchForList_noMatchOnMaximized() {
311        // Regression test for http://b/5714572 .
312        final LocaleMatcher matcher = newLocaleMatcher("en, de, fr, ja");
313        // de maximizes to de_DE. Pick the exact match for the secondary
314        // language instead.
315        assertEquals("de", matcher.getBestMatch("de_CH, fr").toString());
316    }
317
318    public void testBestMatchForTraditionalChinese() {
319        // Scenario: An application that only supports Simplified Chinese (and
320        // some other languages),
321        // but does not support Traditional Chinese. zh_Hans_CN could be
322        // replaced with zh_CN, zh, or
323        // zh_Hans, it wouldn't make much of a difference.
324        final LocaleMatcher matcher = newLocaleMatcher("fr, zh_Hans_CN, en_US");
325
326        // The script distance (simplified vs. traditional Han) is considered
327        // small enough
328        // to be an acceptable match. The regional difference is considered
329        // almost insignificant.
330        assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_TW").toString());
331        assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_Hant").toString());
332
333        // For geo_political reasons, you might want to avoid a zh_Hant ->
334        // zh_Hans match.
335        // In this case, if zh_TW, zh_HK or a tag starting with zh_Hant is
336        // requested, you can
337        // change your call to getBestMatch to include a 2nd language
338        // preference.
339        // "en" is a better match since its distance to "en_US" is closer than
340        // the distance
341        // from "zh_TW" to "zh_CN" (script distance).
342        assertEquals("en_US", matcher.getBestMatch("zh_TW, en").toString());
343        assertEquals("en_US", matcher.getBestMatch("zh_Hant_CN, en").toString());
344        assertEquals("zh_Hans_CN", matcher.getBestMatch("zh_Hans, en").toString());
345    }
346
347    public void testUndefined() {
348        // When the undefined language doesn't match anything in the list,
349        // getBestMatch returns
350        // the default, as usual.
351        LocaleMatcher matcher = newLocaleMatcher("it,fr");
352        assertEquals("it", matcher.getBestMatch("und").toString());
353
354        // When it *does* occur in the list, BestMatch returns it, as expected.
355        matcher = newLocaleMatcher("it,und");
356        assertEquals("und", matcher.getBestMatch("und").toString());
357
358        // The unusual part:
359        // max("und") = "en_Latn_US", and since matching is based on maximized
360        // tags, the undefined
361        // language would normally match English. But that would produce the
362        // counterintuitive results
363        // that getBestMatch("und", LocaleMatcher("it,en")) would be "en", and
364        // getBestMatch("en", LocaleMatcher("it,und")) would be "und".
365        //
366        // To avoid that, we change the matcher's definitions of max
367        // (AddLikelySubtagsWithDefaults)
368        // so that max("und")="und". That produces the following, more desirable
369        // results:
370        matcher = newLocaleMatcher("it,en");
371        assertEquals("it", matcher.getBestMatch("und").toString());
372        matcher = newLocaleMatcher("it,und");
373        assertEquals("it", matcher.getBestMatch("en").toString());
374    }
375
376    // public void testGetBestMatch_emptyList() {
377    // final LocaleMatcher matcher = newLocaleMatcher(
378    // new LocalePriorityList(new HashMap()));
379    // assertNull(matcher.getBestMatch(ULocale.ENGLISH));
380    // }
381
382    public void testGetBestMatch_googlePseudoLocales() {
383        // Google pseudo locales are primarily based on variant subtags.
384        // See http://sites/intl_eng/pseudo_locales.
385        // (See below for the region code based fall back options.)
386        final LocaleMatcher matcher = newLocaleMatcher(
387            "fr, pt");
388        assertEquals("fr", matcher.getBestMatch("de").toString());
389        assertEquals("fr", matcher.getBestMatch("en_US").toString());
390        assertEquals("fr", matcher.getBestMatch("en").toString());
391        assertEquals("pt", matcher.getBestMatch("pt_BR").toString());
392    }
393
394    public void testGetBestMatch_regionDistance() {
395        LocaleMatcher matcher = newLocaleMatcher("es_AR, es");
396        assertEquals("es_AR", matcher.getBestMatch("es_MX").toString());
397
398        matcher = newLocaleMatcher("fr, en, en_GB");
399        assertEquals("en_GB", matcher.getBestMatch("en_CA").toString());
400
401        matcher = newLocaleMatcher("de_AT, de_DE, de_CH");
402        assertEquals("de_DE", matcher.getBestMatch("de").toString());
403
404        showDistance(matcher, "en", "en_CA");
405        showDistance(matcher, "en_CA", "en");
406        showDistance(matcher, "en_US", "en_CA");
407        showDistance(matcher, "en_CA", "en_US");
408        showDistance(matcher, "en_GB", "en_CA");
409        showDistance(matcher, "en_CA", "en_GB");
410        showDistance(matcher, "en", "en_UM");
411        showDistance(matcher, "en_UM", "en");
412    }
413
414    private void showDistance(LocaleMatcher matcher, String desired, String supported) {
415        ULocale desired2 = new ULocale(desired);
416        ULocale supported2 = new ULocale(supported);
417        double distance = matcher.match(desired2, ULocale.addLikelySubtags(desired2), supported2, ULocale.addLikelySubtags(supported2));
418        logln(desired + " to " + supported + " :\t" + distance);
419    }
420
421    /**
422     * If all the base languages are the same, then each sublocale matches
423     * itself most closely
424     */
425    public void testExactMatches() {
426        String lastBase = "";
427        TreeSet<ULocale> sorted = new TreeSet<ULocale>();
428        for (ULocale loc : ULocale.getAvailableLocales()) {
429            String language = loc.getLanguage();
430            if (!lastBase.equals(language)) {
431                check(sorted);
432                sorted.clear();
433                lastBase = language;
434            }
435            sorted.add(loc);
436        }
437        check(sorted);
438    }
439
440    private void check(Set<ULocale> sorted) {
441        if (sorted.isEmpty()) {
442            return;
443        }
444        check2(sorted);
445        ULocale first = sorted.iterator().next();
446        ULocale max = ULocale.addLikelySubtags(first);
447        sorted.add(max);
448        check2(sorted);
449    }
450
451    /**
452     * @param sorted
453     */
454    private void check2(Set<ULocale> sorted) {
455        // TODO Auto-generated method stub
456        logln("Checking: " + sorted);
457        LocaleMatcher matcher = newLocaleMatcher(
458            LocalePriorityList.add(
459                sorted.toArray(new ULocale[sorted.size()]))
460                .build());
461        for (ULocale loc : sorted) {
462            String stringLoc = loc.toString();
463            assertEquals(stringLoc, matcher.getBestMatch(stringLoc).toString());
464        }
465    }
466
467    public void testAsymmetry() {
468        LocaleMatcher matcher;
469        matcher = new LocaleMatcher("mul, nl");
470        assertEquals("nl", matcher.getBestMatch("af").toString()); // af => nl
471
472        matcher = new LocaleMatcher("mul, af");
473        assertEquals("mul", matcher.getBestMatch("nl").toString()); // but nl !=> af
474    }
475
476
477    // public void testComputeDistance_monkeyTest() {
478    // RegionCode[] codes = RegionCode.values();
479    // Random random = new Random();
480    // for (int i = 0; i < 1000; ++i) {
481    // RegionCode x = codes[random.nextInt(codes.length)];
482    // RegionCode y = codes[random.nextInt(codes.length)];
483    // double d = LocaleMatcher.getRegionDistance(x, y, null, null);
484    // if (x == RegionCode.ZZ || y == RegionCode.ZZ) {
485    // assertEquals(LocaleMatcher.REGION_DISTANCE, d);
486    // } else if (x == y) {
487    // assertEquals(0.0, d);
488    // } else {
489    // assertTrue(d > 0);
490    // assertTrue(d <= LocaleMatcher.REGION_DISTANCE);
491    // }
492    // }
493    // }
494
495    public void testGetBestMatchForList_matchOnMaximized2() {
496//        if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) {
497//            return;
498//        }
499        final LocaleMatcher matcher = newLocaleMatcher("fr, en-GB, ja, es-ES, es-MX");
500        // ja-JP matches ja on likely subtags, and it's listed first, thus it wins over
501        // thus it wins over the second preference en-GB.
502        assertEquals("Match for ja-JP, with likely region subtag",
503            "ja", matcher.getBestMatch("ja-JP, en-GB").toString());
504        // Check that if the preference is maximized already, it works as well.
505        assertEquals("Match for ja-Jpan-JP (maximized already)",
506            "ja", matcher.getBestMatch("ja-Jpan-JP, en-GB").toString());
507    }
508
509    public void testGetBestMatchForList_closeEnoughMatchOnMaximized() {
510//        if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) {
511//            return;
512//        }
513        final LocaleMatcher matcher = newLocaleMatcher("en-GB, en, de, fr, ja");
514        assertEquals("de", matcher.getBestMatch("de-CH, fr").toString());
515        assertEquals("en", matcher.getBestMatch("en-US, ar, nl, de, ja").toString());
516    }
517
518    public void testGetBestMatchForPortuguese() {
519
520//        if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) {
521//            return;
522//        }
523
524        final LocaleMatcher withPTExplicit = newLocaleMatcher("pt_PT, pt_BR, es, es_419");
525        final LocaleMatcher withPTImplicit = newLocaleMatcher("pt_PT, pt, es, es_419");
526        // Could happen because "pt_BR" is a tier_1 language and "pt_PT" is tier_2.
527
528        final LocaleMatcher withoutPT = newLocaleMatcher("pt_BR, es, es_419");
529        // European user who prefers Spanish over Brazillian Portuguese as a fallback.
530
531        assertEquals("pt_PT", withPTExplicit.getBestMatch("pt_PT, es, pt").toString());
532        assertEquals("pt_PT", withPTImplicit.getBestMatch("pt_PT, es, pt").toString());
533        assertEquals("es", withoutPT.getBestMatch("pt_PT, es, pt").toString());
534
535        // Brazillian user who prefers South American Spanish over European Portuguese as a fallback.
536        // The asymmetry between this case and above is because it's "pt_PT" that's missing between the
537        // matchers as "pt_BR" is a much more common language.
538        assertEquals("pt_BR", withPTExplicit.getBestMatch("pt, es_419, pt_PT").toString());
539        assertEquals("pt", withPTImplicit.getBestMatch("pt, es_419, pt_PT").toString());
540        assertEquals("pt_BR", withoutPT.getBestMatch("pt, es_419, pt_PT").toString());
541
542        // Code that adds the user's country can get "pt_US" for a user's language.
543        // That should fall back to "pt_BR".
544        assertEquals("pt_BR", withPTExplicit.getBestMatch("pt_US, pt_PT").toString());
545        assertEquals("pt", withPTImplicit.getBestMatch("pt_US, pt_PT").toString());
546    }
547
548    public void testVariantWithScriptMatch() {
549//        if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) {
550//            return;
551//        }
552        final LocaleMatcher matcher = newLocaleMatcher("fr, en, sv");
553        assertEquals("en", matcher.getBestMatch("en-GB").toString());
554        assertEquals("en", matcher.getBestMatch("en-GB, sv").toString());
555    }
556
557    public void testVariantWithScriptMatch2() {
558//        if (logKnownIssue("Cldrbug:8811", "Problems with LocaleMatcher test")) {
559//            return;
560//        }
561        final LocaleMatcher matcher = newLocaleMatcher("en, sv");
562        assertEquals("en", matcher.getBestMatch("en-GB, sv").toString());
563    }
564
565    public void testPerf() {
566        if (LANGUAGE_MATCHER_DATA == null) {
567            return; // skip except when testing data
568        }
569        final String desired = "sv, en";
570
571        final LocaleMatcher matcherShort = newLocaleMatcher(desired);
572        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");
573        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");
574
575        //LocaleMatcher.DEBUG = true;
576        ULocale expected = new ULocale("sv");
577        assertEquals(expected, matcherShort.getBestMatch(desired));
578        assertEquals(expected, matcherLong.getBestMatch(desired));
579        assertEquals(expected, matcherVeryLong.getBestMatch(desired));
580        //LocaleMatcher.DEBUG = false;
581
582        for (int i = 0; i < 2; ++i) {
583            int iterations = i == 0 ? 1000 : 100000;
584            boolean showMessage = i != 0;
585            long timeShort = timeLocaleMatcher("Duration (few  supported):\t", desired, matcherShort, showMessage, iterations, 0);
586            @SuppressWarnings("unused")
587            long timeMedium = timeLocaleMatcher("Duration (med. supported):\t", desired, matcherLong, showMessage, iterations, timeShort);
588            @SuppressWarnings("unused")
589            long timeLong = timeLocaleMatcher("Duration (many supported):\t", desired, matcherVeryLong, showMessage, iterations, timeShort);
590        }
591    }
592
593    private long timeLocaleMatcher(String title, String desired, LocaleMatcher matcher,
594        boolean showmessage, int iterations, long comparisonTime) {
595        long start = System.nanoTime();
596        for (int i = iterations; i > 0; --i) {
597            matcher.getBestMatch(desired);
598        }
599        long delta = System.nanoTime() - start;
600        if (showmessage) warnln(title + (delta / iterations) + " nanos, "
601            + (comparisonTime > 0 ? (delta * 100 / comparisonTime - 100) + "% longer" : ""));
602        return delta;
603    }
604
605    public void Test8288() {
606        final LocaleMatcher matcher = newLocaleMatcher("it, en");
607        assertEquals("it", matcher.getBestMatch("und").toString());
608        assertEquals("en", matcher.getBestMatch("und, en").toString());
609    }
610}
611