Search in sources :

Example 21 with StringSearch

use of android.icu.text.StringSearch 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 22 with StringSearch

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

Example 23 with StringSearch

use of android.icu.text.StringSearch 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 24 with StringSearch

use of android.icu.text.StringSearch 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 25 with StringSearch

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

the class SearchTest method TestGetSetAttribute.

@Test
public void TestGetSetAttribute() {
    String pattern = "pattern";
    String text = "text";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error opening search");
        return;
    }
    if (strsrch.isOverlapping()) {
        errln("Error default overlaping should be false");
    }
    strsrch.setOverlapping(true);
    if (!strsrch.isOverlapping()) {
        errln("Error setting overlap true");
    }
    strsrch.setOverlapping(false);
    if (strsrch.isOverlapping()) {
        errln("Error setting overlap false");
    }
    strsrch.setCanonical(true);
    if (!strsrch.isCanonical()) {
        errln("Error setting canonical match true");
    }
    strsrch.setCanonical(false);
    if (strsrch.isCanonical()) {
        errln("Error setting canonical match false");
    }
    if (strsrch.getElementComparisonType() != STANDARD_ELEMENT_COMPARISON) {
        errln("Error default element comparison type should be STANDARD_ELEMENT_COMPARISON");
    }
    strsrch.setElementComparisonType(ElementComparisonType.PATTERN_BASE_WEIGHT_IS_WILDCARD);
    if (strsrch.getElementComparisonType() != ElementComparisonType.PATTERN_BASE_WEIGHT_IS_WILDCARD) {
        errln("Error setting element comparison type PATTERN_BASE_WEIGHT_IS_WILDCARD");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Aggregations

StringSearch (android.icu.text.StringSearch)27 StringCharacterIterator (java.text.StringCharacterIterator)27 Test (org.junit.Test)24 RuleBasedCollator (android.icu.text.RuleBasedCollator)14 BreakIterator (android.icu.text.BreakIterator)7 Collator (android.icu.text.Collator)4 ULocale (android.icu.util.ULocale)2 Locale (java.util.Locale)1