Search in sources :

Example 81 with RuleBasedCollator

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

the class CollationMonkeyTest method TestRules.

@Test
public void TestRules() {
    String[] testSourceCases = { "\u0061\u0062\u007a", "\u0061\u0062\u007a" };
    String[] testTargetCases = { "\u0061\u0062\u00e4", "\u0061\u0062\u0061\u0308" };
    int i = 0;
    logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
    Collator col = Collator.getInstance(new Locale("en", "US"));
    String baseRules = ((RuleBasedCollator) col).getRules();
    String newRules = " & z < ";
    newRules = baseRules + newRules + String.valueOf(0x00e4);
    RuleBasedCollator myCollation = null;
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        warnln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
    logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
    newRules = "";
    newRules = baseRules + " & z < a" + String.valueOf(0x0308);
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        errln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
}
Also used : Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 82 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator 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 83 with RuleBasedCollator

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

the class SearchTest method TestOverlapCanonical.

@Test
public void TestOverlapCanonical() {
    int count;
    for (count = 0; count < OVERLAPCANONICAL.length; count++) {
        if (!assertEqualWithAttribute(OVERLAPCANONICAL[count], true, true)) {
            errln("Error at overlap test number %d" + count);
        }
    }
    for (count = 0; count < NONOVERLAP.length; count++) {
        if (!assertCanonicalEqual(NONOVERLAPCANONICAL[count])) {
            errln("Error at non overlap test number %d" + count);
        }
    }
    for (count = 0; count < OVERLAPCANONICAL.length && count < NONOVERLAPCANONICAL.length; count++) {
        SearchData search = OVERLAPCANONICAL[count];
        RuleBasedCollator collator = getCollator(search.collator);
        StringSearch strsrch = new StringSearch(search.pattern, new StringCharacterIterator(search.text), collator, null);
        strsrch.setCanonical(true);
        strsrch.setOverlapping(true);
        if (strsrch.isOverlapping() != true) {
            errln("Error setting overlap option");
        }
        if (!assertEqualWithStringSearch(strsrch, search)) {
            strsrch = null;
            return;
        }
        search = NONOVERLAPCANONICAL[count];
        strsrch.setOverlapping(false);
        if (strsrch.isOverlapping() != false) {
            errln("Error setting overlap option");
        }
        strsrch.reset();
        if (!assertEqualWithStringSearch(strsrch, search)) {
            strsrch = null;
            errln("Error at test number %d" + count);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 84 with RuleBasedCollator

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

the class SearchTest method init.

@Before
public void init() throws Exception {
    m_en_us_ = (RuleBasedCollator) Collator.getInstance(Locale.US);
    m_fr_fr_ = (RuleBasedCollator) Collator.getInstance(Locale.FRANCE);
    m_de_ = (RuleBasedCollator) Collator.getInstance(new Locale("de", "DE"));
    m_es_ = (RuleBasedCollator) Collator.getInstance(new Locale("es", "ES"));
    m_en_wordbreaker_ = BreakIterator.getWordInstance();
    m_en_characterbreaker_ = BreakIterator.getCharacterInstance();
    String rules = m_de_.getRules() + EXTRACOLLATIONRULE;
    m_de_ = new RuleBasedCollator(rules);
    rules = m_es_.getRules() + EXTRACOLLATIONRULE;
    m_es_ = new RuleBasedCollator(rules);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Before(org.junit.Before)

Example 85 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator 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

RuleBasedCollator (android.icu.text.RuleBasedCollator)140 Test (org.junit.Test)124 Collator (android.icu.text.Collator)42 ULocale (android.icu.util.ULocale)26 CollationElementIterator (android.icu.text.CollationElementIterator)25 Locale (java.util.Locale)22 CollationKey (android.icu.text.CollationKey)17 StringCharacterIterator (java.text.StringCharacterIterator)16 StringSearch (android.icu.text.StringSearch)14 RawCollationKey (android.icu.text.RawCollationKey)11 ParseException (java.text.ParseException)10 UnicodeSet (android.icu.text.UnicodeSet)8 AlphabeticIndex (android.icu.text.AlphabeticIndex)6 BreakIterator (android.icu.text.BreakIterator)6 MissingResourceException (java.util.MissingResourceException)5 IOException (java.io.IOException)4 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)3 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 ArrayList (java.util.ArrayList)2