Search in sources :

Example 86 with RuleBasedCollator

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

the class SearchTest method TestCollatorCanonical.

@Test
public void TestCollatorCanonical() {
    /* test collator that thinks "o" and "p" are the same thing */
    String text = COLLATORCANONICAL[0].text;
    String pattern = COLLATORCANONICAL[0].pattern;
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
        strsrch.setCanonical(true);
    } catch (Exception e) {
        errln("Error opening string search ");
    }
    if (!assertEqualWithStringSearch(strsrch, COLLATORCANONICAL[0])) {
        return;
    }
    String rules = TESTCOLLATORRULE;
    RuleBasedCollator tailored = null;
    try {
        tailored = new RuleBasedCollator(rules);
        tailored.setStrength(COLLATORCANONICAL[1].strength);
        tailored.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (Exception e) {
        errln("Error opening rule based collator ");
    }
    strsrch.setCollator(tailored);
    if (!strsrch.getCollator().equals(tailored)) {
        errln("Error setting rule based collator");
    }
    strsrch.reset();
    strsrch.setCanonical(true);
    if (!assertEqualWithStringSearch(strsrch, COLLATORCANONICAL[1])) {
        // Error should already be reported.
        logln("COLLATORCANONICAL[1] failed");
    }
    strsrch.setCollator(m_en_us_);
    strsrch.reset();
    if (!strsrch.getCollator().equals(m_en_us_)) {
        errln("Error setting rule based collator");
    }
    if (!assertEqualWithStringSearch(strsrch, COLLATORCANONICAL[0])) {
        // Error should already be reported.
        logln("COLLATORCANONICAL[0] failed");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 87 with RuleBasedCollator

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

the class SearchTest method TestBreakIterator.

@Test
public void TestBreakIterator() {
    String text = BREAKITERATOREXACT[0].text;
    String pattern = BREAKITERATOREXACT[0].pattern;
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error opening string search");
        return;
    }
    strsrch.setBreakIterator(null);
    if (strsrch.getBreakIterator() != null) {
        errln("Error usearch_getBreakIterator returned wrong object");
    }
    strsrch.setBreakIterator(m_en_characterbreaker_);
    if (!strsrch.getBreakIterator().equals(m_en_characterbreaker_)) {
        errln("Error usearch_getBreakIterator returned wrong object");
    }
    strsrch.setBreakIterator(m_en_wordbreaker_);
    if (!strsrch.getBreakIterator().equals(m_en_wordbreaker_)) {
        errln("Error usearch_getBreakIterator returned wrong object");
    }
    int count = 0;
    while (count < 4) {
        // special purposes for tests numbers 0-3
        SearchData search = BREAKITERATOREXACT[count];
        RuleBasedCollator collator = getCollator(search.collator);
        BreakIterator breaker = getBreakIterator(search.breaker);
        // StringSearch      strsrch;
        text = search.text;
        pattern = search.pattern;
        if (breaker != null) {
            breaker.setText(text);
        }
        collator.setStrength(search.strength);
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), collator, breaker);
        if (strsrch.getBreakIterator() != breaker) {
            errln("Error setting break iterator");
        }
        if (!assertEqualWithStringSearch(strsrch, search)) {
            collator.setStrength(TERTIARY);
        }
        search = BREAKITERATOREXACT[count + 1];
        breaker = getBreakIterator(search.breaker);
        if (breaker != null) {
            breaker.setText(text);
        }
        strsrch.setBreakIterator(breaker);
        if (strsrch.getBreakIterator() != breaker) {
            errln("Error setting break iterator");
        }
        strsrch.reset();
        if (!assertEqualWithStringSearch(strsrch, search)) {
            errln("Error at test number " + count);
        }
        count += 2;
    }
    for (count = 0; count < BREAKITERATOREXACT.length; count++) {
        if (!assertEqual(BREAKITERATOREXACT[count])) {
            errln("Error at test number " + count);
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) BreakIterator(android.icu.text.BreakIterator) Test(org.junit.Test)

Example 88 with RuleBasedCollator

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

the class SearchTest method TestContractionCanonical.

@Test
public void TestContractionCanonical() {
    String rules = CONTRACTIONRULE;
    RuleBasedCollator collator = null;
    try {
        collator = new RuleBasedCollator(rules);
        collator.setStrength(TERTIARY);
        collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (Exception e) {
        errln("Error opening collator ");
    }
    String text = "text";
    String pattern = "pattern";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), collator, null);
        strsrch.setCanonical(true);
    } catch (Exception e) {
        errln("Error opening string search");
    }
    for (int count = 0; count < CONTRACTIONCANONICAL.length; count++) {
        text = CONTRACTIONCANONICAL[count].text;
        pattern = CONTRACTIONCANONICAL[count].pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        if (!assertEqualWithStringSearch(strsrch, CONTRACTIONCANONICAL[count])) {
            errln("Error at test number " + count);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 89 with RuleBasedCollator

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

the class CollationAPITest method TestSetGet.

/**
 * Simple test the collator setter and getters.
 * Similar to C++ apicoll.cpp TestAttribute().
 */
@Test
public void TestSetGet() {
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance();
    int decomp = collator.getDecomposition();
    int strength = collator.getStrength();
    boolean alt = collator.isAlternateHandlingShifted();
    boolean caselevel = collator.isCaseLevel();
    boolean french = collator.isFrenchCollation();
    boolean hquart = collator.isHiraganaQuaternary();
    boolean lowercase = collator.isLowerCaseFirst();
    boolean uppercase = collator.isUpperCaseFirst();
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    if (collator.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
        errln("Setting decomposition failed");
    }
    collator.setStrength(Collator.QUATERNARY);
    if (collator.getStrength() != Collator.QUATERNARY) {
        errln("Setting strength failed");
    }
    collator.setAlternateHandlingShifted(!alt);
    if (collator.isAlternateHandlingShifted() == alt) {
        errln("Setting alternate handling failed");
    }
    collator.setCaseLevel(!caselevel);
    if (collator.isCaseLevel() == caselevel) {
        errln("Setting case level failed");
    }
    collator.setFrenchCollation(!french);
    if (collator.isFrenchCollation() == french) {
        errln("Setting french collation failed");
    }
    collator.setHiraganaQuaternary(!hquart);
    if (collator.isHiraganaQuaternary() != hquart) {
        errln("Setting hiragana quartenary worked but should be a no-op since ICU 50");
    }
    collator.setLowerCaseFirst(!lowercase);
    if (collator.isLowerCaseFirst() == lowercase) {
        errln("Setting lower case first failed");
    }
    collator.setUpperCaseFirst(!uppercase);
    if (collator.isUpperCaseFirst() == uppercase) {
        errln("Setting upper case first failed");
    }
    collator.setDecompositionDefault();
    if (collator.getDecomposition() != decomp) {
        errln("Setting decomposition default failed");
    }
    collator.setStrengthDefault();
    if (collator.getStrength() != strength) {
        errln("Setting strength default failed");
    }
    collator.setAlternateHandlingDefault();
    if (collator.isAlternateHandlingShifted() != alt) {
        errln("Setting alternate handling default failed");
    }
    collator.setCaseLevelDefault();
    if (collator.isCaseLevel() != caselevel) {
        errln("Setting case level default failed");
    }
    collator.setFrenchCollationDefault();
    if (collator.isFrenchCollation() != french) {
        errln("Setting french handling default failed");
    }
    collator.setHiraganaQuaternaryDefault();
    if (collator.isHiraganaQuaternary() != hquart) {
        errln("Setting Hiragana Quartenary default failed");
    }
    collator.setCaseFirstDefault();
    if (collator.isLowerCaseFirst() != lowercase || collator.isUpperCaseFirst() != uppercase) {
        errln("Setting case first handling default failed");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 90 with RuleBasedCollator

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

the class CollationAPITest method TestClone.

@Test
public void TestClone() {
    logln("\ninit c0");
    RuleBasedCollator c0 = (RuleBasedCollator) Collator.getInstance();
    c0.setStrength(Collator.TERTIARY);
    dump("c0", c0);
    logln("\ninit c1");
    RuleBasedCollator c1 = (RuleBasedCollator) Collator.getInstance();
    c1.setStrength(Collator.TERTIARY);
    c1.setUpperCaseFirst(!c1.isUpperCaseFirst());
    dump("c0", c0);
    dump("c1", c1);
    try {
        logln("\ninit c2");
        RuleBasedCollator c2 = (RuleBasedCollator) c1.clone();
        c2.setUpperCaseFirst(!c2.isUpperCaseFirst());
        dump("c0", c0);
        dump("c1", c1);
        dump("c2", c2);
        if (c1.equals(c2)) {
            errln("The cloned objects refer to same data");
        }
    } catch (CloneNotSupportedException ex) {
        errln("Could not clone the collator");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

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