Search in sources :

Example 16 with StringSearch

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

the class SearchTest method TestDiactricMatch.

// Test for ticket 5024
@Test
public void TestDiactricMatch() {
    String pattern = "pattern";
    String text = "text";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, text);
    } catch (Exception e) {
        errln("Error opening string search ");
        return;
    }
    for (int count = 0; count < DIACTRICMATCH.length; count++) {
        strsrch.setCollator(getCollator(DIACTRICMATCH[count].collator));
        strsrch.getCollator().setStrength(DIACTRICMATCH[count].strength);
        strsrch.setBreakIterator(getBreakIterator(DIACTRICMATCH[count].breaker));
        strsrch.reset();
        text = DIACTRICMATCH[count].text;
        pattern = DIACTRICMATCH[count].pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        if (!assertEqualWithStringSearch(strsrch, DIACTRICMATCH[count])) {
            errln("Error at test number " + count);
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 17 with StringSearch

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

the class SearchTest method assertEqual.

boolean assertEqual(SearchData search) {
    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.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 18 with StringSearch

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

the class SearchTest method TestGetSetOffset.

@Test
public void TestGetSetOffset() {
    String pattern = "1234567890123456";
    String text = "12345678901234567890123456789012";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error opening search");
        return;
    }
    /* testing out of bounds error */
    try {
        strsrch.setIndex(-1);
        errln("Error expecting set offset error");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: strsrch.setIndex(-1) failed as expected");
    }
    try {
        strsrch.setIndex(128);
        errln("Error expecting set offset error");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: strsrch.setIndex(128) failed as expected");
    }
    for (int index = 0; index < BASIC.length; index++) {
        SearchData search = BASIC[index];
        text = search.text;
        pattern = search.pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        strsrch.getCollator().setStrength(search.strength);
        strsrch.reset();
        int count = 0;
        int matchindex = search.offset[count];
        while (matchindex >= 0) {
            int matchlength = search.size[count];
            strsrch.next();
            if (matchindex != strsrch.getMatchStart() || matchlength != strsrch.getMatchLength()) {
                errln("Text: " + text);
                errln("Pattern: " + strsrch.getPattern());
                errln("Error match found at " + strsrch.getMatchStart() + ", " + strsrch.getMatchLength());
                return;
            }
            matchindex = search.offset[count + 1] == -1 ? -1 : search.offset[count + 2];
            if (search.offset[count + 1] != -1) {
                strsrch.setIndex(search.offset[count + 1] + 1);
                if (strsrch.getIndex() != search.offset[count + 1] + 1) {
                    errln("Error setting offset\n");
                    return;
                }
            }
            count += 2;
        }
        strsrch.next();
        if (strsrch.getMatchStart() != StringSearch.DONE) {
            errln("Text: " + text);
            errln("Pattern: " + strsrch.getPattern());
            errln("Error match found at " + strsrch.getMatchStart() + ", " + strsrch.getMatchLength());
            return;
        }
    }
    strsrch.getCollator().setStrength(TERTIARY);
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 19 with StringSearch

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

the class SearchTest method TestPattern.

@Test
public void TestPattern() {
    m_en_us_.setStrength(PATTERN[0].strength);
    StringSearch strsrch = new StringSearch(PATTERN[0].pattern, new StringCharacterIterator(PATTERN[0].text), m_en_us_, null);
    if (strsrch.getPattern() != PATTERN[0].pattern) {
        errln("Error setting pattern");
    }
    if (!assertEqualWithStringSearch(strsrch, PATTERN[0])) {
        m_en_us_.setStrength(TERTIARY);
        if (strsrch != null) {
            strsrch = null;
        }
        return;
    }
    strsrch.setPattern(PATTERN[1].pattern);
    if (PATTERN[1].pattern != strsrch.getPattern()) {
        errln("Error setting pattern");
        m_en_us_.setStrength(TERTIARY);
        if (strsrch != null) {
            strsrch = null;
        }
        return;
    }
    strsrch.reset();
    if (!assertEqualWithStringSearch(strsrch, PATTERN[1])) {
        m_en_us_.setStrength(TERTIARY);
        if (strsrch != null) {
            strsrch = null;
        }
        return;
    }
    strsrch.setPattern(PATTERN[0].pattern);
    if (PATTERN[0].pattern != strsrch.getPattern()) {
        errln("Error setting pattern");
        m_en_us_.setStrength(TERTIARY);
        if (strsrch != null) {
            strsrch = null;
        }
        return;
    }
    strsrch.reset();
    if (!assertEqualWithStringSearch(strsrch, PATTERN[0])) {
        m_en_us_.setStrength(TERTIARY);
        if (strsrch != null) {
            strsrch = null;
        }
        return;
    }
    /* enormous pattern size to see if this crashes */
    String pattern = "";
    for (int templength = 0; templength != 512; templength++) {
        pattern += 0x61;
    }
    try {
        strsrch.setPattern(pattern);
    } catch (Exception e) {
        errln("Error setting pattern with size 512");
    }
    m_en_us_.setStrength(TERTIARY);
    if (strsrch != null) {
        strsrch = null;
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 20 with StringSearch

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

the class SearchTest method TestOpenClose.

@Test
public void TestOpenClose() {
    StringSearch result;
    BreakIterator breakiter = m_en_wordbreaker_;
    String pattern = "";
    String text = "";
    String temp = "a";
    StringCharacterIterator chariter = new StringCharacterIterator(text);
    /* testing null arguments */
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), null, null);
        errln("Error: null arguments should produce an error");
    } catch (Exception e) {
        logln("PASS: null arguments failed as expected");
    }
    chariter.setText(text);
    try {
        result = new StringSearch(pattern, chariter, null, null);
        errln("Error: null arguments should produce an error");
    } catch (Exception e) {
        logln("PASS: null arguments failed as expected");
    }
    text = String.valueOf(0x1);
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), null, null);
        errln("Error: Empty pattern should produce an error");
    } catch (Exception e) {
        logln("PASS: Empty pattern failed as expected");
    }
    chariter.setText(text);
    try {
        result = new StringSearch(pattern, chariter, null, null);
        errln("Error: Empty pattern should produce an error");
    } catch (Exception e) {
        logln("PASS: Empty pattern failed as expected");
    }
    text = "";
    pattern = temp;
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), null, null);
        errln("Error: Empty text should produce an error");
    } catch (Exception e) {
        logln("PASS: Empty text failed as expected");
    }
    chariter.setText(text);
    try {
        result = new StringSearch(pattern, chariter, null, null);
        errln("Error: Empty text should produce an error");
    } catch (Exception e) {
        logln("PASS: Empty text failed as expected");
    }
    text += temp;
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), null, null);
        errln("Error: null arguments should produce an error");
    } catch (Exception e) {
        logln("PASS: null arguments failed as expected");
    }
    chariter.setText(text);
    try {
        result = new StringSearch(pattern, chariter, null, null);
        errln("Error: null arguments should produce an error");
    } catch (Exception e) {
        logln("PASS: null arguments failed as expected");
    }
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, null);
    } catch (Exception e) {
        errln("Error: null break iterator is valid for opening search");
    }
    try {
        result = new StringSearch(pattern, chariter, m_en_us_, null);
    } catch (Exception e) {
        errln("Error: null break iterator is valid for opening search");
    }
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), Locale.ENGLISH);
    } catch (Exception e) {
        errln("Error: null break iterator is valid for opening search");
    }
    try {
        result = new StringSearch(pattern, chariter, Locale.ENGLISH);
    } catch (Exception e) {
        errln("Error: null break iterator is valid for opening search");
    }
    try {
        result = new StringSearch(pattern, new StringCharacterIterator(text), m_en_us_, breakiter);
    } catch (Exception e) {
        errln("Error: Break iterator is valid for opening search");
    }
    try {
        result = new StringSearch(pattern, chariter, m_en_us_, null);
        logln("pattern:" + result.getPattern());
    } catch (Exception e) {
        errln("Error: Break iterator is valid for opening search");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) BreakIterator(android.icu.text.BreakIterator) 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