Search in sources :

Example 46 with Collator

use of android.icu.text.Collator in project j2objc by google.

the class CollationMonkeyTest method TestCollationKey.

@Test
public void TestCollationKey() {
    if (source.length() == 0) {
        errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
        return;
    }
    Collator myCollator;
    try {
        myCollator = Collator.getInstance(new Locale("en", "US"));
    } catch (Exception e) {
        warnln("ERROR: in creation of collator of ENGLISH locale");
        return;
    }
    // use test framework's random seed
    Random rand = createRandom();
    int s = rand.nextInt(0x7fff) % source.length();
    int t = rand.nextInt(0x7fff) % source.length();
    int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
    String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
    CollationKey collationKey1, collationKey2;
    myCollator.setStrength(Collator.TERTIARY);
    collationKey1 = myCollator.getCollationKey(subs);
    collationKey2 = myCollator.getCollationKey(subt);
    // Tertiary
    int result = collationKey1.compareTo(collationKey2);
    // Tertiary
    int revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    myCollator.setStrength(Collator.SECONDARY);
    collationKey1 = myCollator.getCollationKey(subs);
    collationKey2 = myCollator.getCollationKey(subt);
    // Secondary
    result = collationKey1.compareTo(collationKey2);
    // Secondary
    revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    myCollator.setStrength(Collator.PRIMARY);
    collationKey1 = myCollator.getCollationKey(subs);
    collationKey2 = myCollator.getCollationKey(subt);
    // Primary
    result = collationKey1.compareTo(collationKey2);
    // Primary
    revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    String msg = "";
    String addOne = subs + String.valueOf(0xE000);
    collationKey1 = myCollator.getCollationKey(subs);
    collationKey2 = myCollator.getCollationKey(addOne);
    result = collationKey1.compareTo(collationKey2);
    if (result != -1) {
        msg += "CollationKey(";
        msg += subs;
        msg += ") .LT. CollationKey(";
        msg += addOne;
        msg += ") Failed.";
        errln(msg);
    }
    msg = "";
    result = collationKey2.compareTo(collationKey1);
    if (result != 1) {
        msg += "CollationKey(";
        msg += addOne;
        msg += ") .GT. CollationKey(";
        msg += subs;
        msg += ") Failed.";
        errln(msg);
    }
}
Also used : Locale(java.util.Locale) Random(java.util.Random) CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 47 with Collator

use of android.icu.text.Collator in project j2objc by google.

the class CollationServiceTest method TestRegisterFactory.

@Test
public void TestRegisterFactory() {
    class CollatorInfo {

        ULocale locale;

        Collator collator;

        // locale -> string
        Map displayNames;

        CollatorInfo(ULocale locale, Collator collator, Map displayNames) {
            this.locale = locale;
            this.collator = collator;
            this.displayNames = displayNames;
        }

        String getDisplayName(ULocale displayLocale) {
            String name = null;
            if (displayNames != null) {
                name = (String) displayNames.get(displayLocale);
            }
            if (name == null) {
                name = locale.getDisplayName(displayLocale);
            }
            return name;
        }
    }
    class TestFactory extends CollatorFactory {

        private Map map;

        private Set ids;

        TestFactory(CollatorInfo[] info) {
            map = new HashMap();
            for (int i = 0; i < info.length; ++i) {
                CollatorInfo ci = info[i];
                map.put(ci.locale, ci);
            }
        }

        public Collator createCollator(ULocale loc) {
            CollatorInfo ci = (CollatorInfo) map.get(loc);
            if (ci != null) {
                return ci.collator;
            }
            return null;
        }

        public String getDisplayName(ULocale objectLocale, ULocale displayLocale) {
            CollatorInfo ci = (CollatorInfo) map.get(objectLocale);
            if (ci != null) {
                return ci.getDisplayName(displayLocale);
            }
            return null;
        }

        public Set getSupportedLocaleIDs() {
            if (ids == null) {
                HashSet set = new HashSet();
                Iterator iter = map.keySet().iterator();
                while (iter.hasNext()) {
                    ULocale locale = (ULocale) iter.next();
                    String id = locale.toString();
                    set.add(id);
                }
                ids = Collections.unmodifiableSet(set);
            }
            return ids;
        }
    }
    class TestFactoryWrapper extends CollatorFactory {

        CollatorFactory delegate;

        TestFactoryWrapper(CollatorFactory delegate) {
            this.delegate = delegate;
        }

        public Collator createCollator(ULocale loc) {
            return delegate.createCollator(loc);
        }

        // use CollatorFactory getDisplayName(ULocale, ULocale) for coverage
        public Set getSupportedLocaleIDs() {
            return delegate.getSupportedLocaleIDs();
        }
    }
    ULocale fu_FU = new ULocale("fu_FU");
    ULocale fu_FU_FOO = new ULocale("fu_FU_FOO");
    Map fuFUNames = new HashMap();
    fuFUNames.put(fu_FU, "ze leetle bunny Fu-Fu");
    fuFUNames.put(fu_FU_FOO, "zee leetel bunny Foo-Foo");
    fuFUNames.put(ULocale.US, "little bunny Foo Foo");
    Collator frcol = Collator.getInstance(ULocale.FRANCE);
    /* Collator uscol = */
    Collator.getInstance(ULocale.US);
    Collator gecol = Collator.getInstance(ULocale.GERMANY);
    Collator jpcol = Collator.getInstance(ULocale.JAPAN);
    Collator fucol = Collator.getInstance(fu_FU);
    CollatorInfo[] info = { new CollatorInfo(ULocale.US, frcol, null), new CollatorInfo(ULocale.FRANCE, gecol, null), new CollatorInfo(fu_FU, jpcol, fuFUNames) };
    TestFactory factory = null;
    try {
        factory = new TestFactory(info);
    } catch (MissingResourceException ex) {
        warnln("Could not load locale data.");
    }
    // coverage
    {
        // in java, gc lets us easily multiply reference!
        TestFactoryWrapper wrapper = new TestFactoryWrapper(factory);
        Object key = Collator.registerFactory(wrapper);
        String name = null;
        try {
            name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
        } catch (MissingResourceException ex) {
            warnln("Could not load locale data.");
        }
        logln("*** default name: " + name);
        Collator.unregister(key);
        ULocale bar_BAR = new ULocale("bar_BAR");
        Collator col = Collator.getInstance(bar_BAR);
        ULocale valid = col.getLocale(ULocale.VALID_LOCALE);
        String validName = valid.getName();
        if (validName.length() != 0 && !validName.equals("root")) {
            errln("Collation from bar_BAR is really \"" + validName + "\" but should be root");
        }
    }
    int n1 = checkAvailable("before registerFactory");
    {
        Object key = Collator.registerFactory(factory);
        int n2 = checkAvailable("after registerFactory");
        Collator ncol = Collator.getInstance(ULocale.US);
        if (!frcol.equals(ncol)) {
            errln("frcoll for en_US failed");
        }
        ncol = Collator.getInstance(fu_FU_FOO);
        if (!jpcol.equals(ncol)) {
            errln("jpcol for fu_FU_FOO failed, got: " + ncol);
        }
        ULocale[] locales = Collator.getAvailableULocales();
        boolean found = false;
        for (int i = 0; i < locales.length; ++i) {
            if (locales[i].equals(fu_FU)) {
                found = true;
                break;
            }
        }
        if (!found) {
            errln("new locale fu_FU not reported as supported locale");
        }
        String name = Collator.getDisplayName(fu_FU);
        if (!"little bunny Foo Foo".equals(name)) {
            errln("found " + name + " for fu_FU");
        }
        name = Collator.getDisplayName(fu_FU, fu_FU_FOO);
        if (!"zee leetel bunny Foo-Foo".equals(name)) {
            errln("found " + name + " for fu_FU in fu_FU_FOO");
        }
        if (!Collator.unregister(key)) {
            errln("failed to unregister factory");
        }
        int n3 = checkAvailable("after unregister");
        assertTrue("register increases count", n2 > n1);
        assertTrue("unregister restores count", n3 == n1);
        ncol = Collator.getInstance(fu_FU);
        if (!fucol.equals(ncol)) {
            errln("collator after unregister does not match original fu_FU");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) Iterator(java.util.Iterator) CollatorFactory(android.icu.text.Collator.CollatorFactory) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with Collator

use of android.icu.text.Collator in project j2objc by google.

the class CollationTest method TestTailoredElements.

@Test
public void TestTailoredElements() {
    CollationData root = CollationRoot.getData();
    CollationRootElements rootElements = new CollationRootElements(root.rootElements);
    Set<String> prevLocales = new HashSet<String>();
    prevLocales.add("");
    prevLocales.add("root");
    prevLocales.add("root@collation=standard");
    long[] ces;
    ULocale[] locales = Collator.getAvailableULocales();
    String localeID = "root";
    int locIdx = 0;
    for (; locIdx < locales.length; localeID = locales[locIdx++].getName()) {
        ULocale locale = new ULocale(localeID);
        String[] types = Collator.getKeywordValuesForLocale("collation", locale, false);
        for (int typeIdx = 0; typeIdx < types.length; ++typeIdx) {
            // first: default type
            String type = types[typeIdx];
            if (type.startsWith("private-")) {
                errln("Collator.getKeywordValuesForLocale(" + localeID + ") returns private collation keyword: " + type);
            }
            ULocale localeWithType = locale.setKeywordValue("collation", type);
            Collator coll = Collator.getInstance(localeWithType);
            ULocale actual = coll.getLocale(ULocale.ACTUAL_LOCALE);
            if (prevLocales.contains(actual.getName())) {
                continue;
            }
            prevLocales.add(actual.getName());
            logln("TestTailoredElements(): requested " + localeWithType.getName() + " -> actual " + actual.getName());
            if (!(coll instanceof RuleBasedCollator)) {
                continue;
            }
            RuleBasedCollator rbc = (RuleBasedCollator) coll;
            // Note: It would be better to get tailored strings such that we can
            // identify the prefix, and only get the CEs for the prefix+string,
            // not also for the prefix.
            // There is currently no API for that.
            // It would help in an unusual case where a contraction starting in the prefix
            // extends past its end, and we do not see the intended mapping.
            // For example, for a mapping p|st, if there is also a contraction ps,
            // then we get CEs(ps)+CEs(t), rather than CEs(p|st).
            UnicodeSet tailored = coll.getTailoredSet();
            UnicodeSetIterator iter = new UnicodeSetIterator(tailored);
            while (iter.next()) {
                String s = iter.getString();
                ces = rbc.internalGetCEs(s);
                for (int i = 0; i < ces.length; ++i) {
                    long ce = ces[i];
                    if (!isValidCE(rootElements, root, ce)) {
                        logln(prettify(s));
                        errln("invalid tailored CE 0x" + Utility.hex(ce, 16) + " at CE index " + i + " from string:");
                    }
                }
            }
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationRootElements(android.icu.impl.coll.CollationRootElements) UnicodeSetIterator(android.icu.text.UnicodeSetIterator) CollationData(android.icu.impl.coll.CollationData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with Collator

use of android.icu.text.Collator in project j2objc by google.

the class GlobalizationPreferencesTest method TestDefault.

@Test
public void TestDefault() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    ULocale defLocale = new ULocale("en_US");
    ULocale defFallbackLocale = new ULocale("en");
    if (!defLocale.equals(ULocale.getDefault())) {
        // Locale.US is always used as the default locale in the test environment
        // If not, some test cases will fail...
        errln("FAIL: The default locale of the test environment must be en_US");
    }
    logln("Default locale: " + defLocale.toString());
    // First locale is en_US
    ULocale gpLocale0 = gp.getLocale(0);
    logln("Primary locale: " + gpLocale0.toString());
    if (!gpLocale0.equals(defLocale)) {
        errln("FAIL: The primary locale is not en_US");
    }
    // Second locale is en
    ULocale gpLocale1 = gp.getLocale(1);
    logln("Secondary locale: " + gpLocale1.toString());
    if (!gpLocale1.equals(defFallbackLocale)) {
        errln("FAIL: The secondary locale is not en");
    }
    // Third locale is null
    ULocale gpLocale2 = gp.getLocale(2);
    if (gpLocale2 != null) {
        errln("FAIL: Number of locales must be 2");
    }
    // Calendar locale
    Calendar cal = gp.getCalendar();
    ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE);
    logln("Calendar locale: " + calLocale.toString());
    if (!calLocale.equals(defLocale)) {
        errln("FAIL: The calendar locale must match with the default JVM locale");
    }
    // Collator locale
    Collator coll = gp.getCollator();
    ULocale collLocale = coll.getLocale(ULocale.VALID_LOCALE);
    logln("Collator locale: " + collLocale.toString());
    if (!collLocale.equals(defLocale)) {
        errln("FAIL: The collator locale must match with the default JVM locale");
    }
    // BreakIterator locale
    BreakIterator brk = gp.getBreakIterator(GlobalizationPreferences.BI_CHARACTER);
    ULocale brkLocale = brk.getLocale(ULocale.VALID_LOCALE);
    logln("BreakIterator locale: " + brkLocale.toString());
    if (!brkLocale.equals(defLocale)) {
        errln("FAIL: The break iterator locale must match with the default JVM locale");
    }
    /* Skip - Bug#5209
        // DateFormat locale
        DateFormat df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
        ULocale dfLocale = df.getLocale(ULocale.VALID_LOCALE);
        logln("DateFormat locale: " + dfLocale.toString());
        if (!dfLocale.equals(defLocale)) {
            errln("FAIL: The date format locale must match with the default JVM locale");
        }
        */
    // NumberFormat locale
    NumberFormat nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER);
    ULocale nfLocale = nf.getLocale(ULocale.VALID_LOCALE);
    logln("NumberFormat locale: " + nfLocale.toString());
    if (!nfLocale.equals(defLocale)) {
        errln("FAIL: The number format locale must match with the default JVM locale");
    }
}
Also used : GlobalizationPreferences(android.icu.util.GlobalizationPreferences) ULocale(android.icu.util.ULocale) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Collator(android.icu.text.Collator) BreakIterator(android.icu.text.BreakIterator) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 50 with Collator

use of android.icu.text.Collator in project j2objc by google.

the class SearchTest method assertEqualWithAttribute.

boolean assertEqualWithAttribute(SearchData search, boolean canonical, boolean overlap) {
    Collator collator = getCollator(search.collator);
    BreakIterator breaker = getBreakIterator(search.breaker);
    StringSearch strsrch;
    String text = search.text;
    String pattern = search.pattern;
    if (breaker != null) {
        breaker.setText(text);
    }
    collator.setStrength(search.strength);
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
        strsrch.setCanonical(canonical);
        strsrch.setOverlapping(overlap);
        strsrch.setElementComparisonType(search.cmpType);
    } catch (Exception e) {
        errln("Error opening string search " + e.getMessage());
        return false;
    }
    if (!assertEqualWithStringSearch(strsrch, search)) {
        collator.setStrength(TERTIARY);
        return false;
    }
    collator.setStrength(TERTIARY);
    return true;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator)

Aggregations

Collator (android.icu.text.Collator)81 Test (org.junit.Test)72 RuleBasedCollator (android.icu.text.RuleBasedCollator)70 Locale (java.util.Locale)25 ULocale (android.icu.util.ULocale)22 CollationKey (android.icu.text.CollationKey)15 MissingResourceException (java.util.MissingResourceException)11 RawCollationKey (android.icu.text.RawCollationKey)7 ParseException (java.text.ParseException)6 Random (java.util.Random)6 BreakIterator (android.icu.text.BreakIterator)5 StringCharacterIterator (java.text.StringCharacterIterator)5 LocaleDisplayNames (android.icu.text.LocaleDisplayNames)4 StringSearch (android.icu.text.StringSearch)4 CollationElementIterator (android.icu.text.CollationElementIterator)3 ArrayList (java.util.ArrayList)3 UnicodeSet (android.icu.text.UnicodeSet)2 GlobalizationPreferences (android.icu.util.GlobalizationPreferences)2 HashSet (java.util.HashSet)2 TreeSet (java.util.TreeSet)2