Search in sources :

Example 1 with StringSearch

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

the class SearchTest method TestCollator.

@Test
public void TestCollator() {
    // test collator that thinks "o" and "p" are the same thing
    String text = COLLATOR[0].text;
    String pattern = COLLATOR[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;
    }
    if (!assertEqualWithStringSearch(strsrch, COLLATOR[0])) {
        return;
    }
    String rules = TESTCOLLATORRULE;
    RuleBasedCollator tailored = null;
    try {
        tailored = new RuleBasedCollator(rules);
        tailored.setStrength(COLLATOR[1].strength);
    } catch (Exception e) {
        errln("Error opening rule based collator ");
        return;
    }
    strsrch.setCollator(tailored);
    if (!strsrch.getCollator().equals(tailored)) {
        errln("Error setting rule based collator");
    }
    strsrch.reset();
    if (!assertEqualWithStringSearch(strsrch, COLLATOR[1])) {
        return;
    }
    strsrch.setCollator(m_en_us_);
    strsrch.reset();
    if (!strsrch.getCollator().equals(m_en_us_)) {
        errln("Error setting rule based collator");
    }
    if (!assertEqualWithStringSearch(strsrch, COLLATOR[0])) {
        errln("Error searching collator test");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 2 with StringSearch

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

the class SearchTest method TestSetMatch.

@Test
public void TestSetMatch() {
    for (int count = 0; count < MATCH.length; count++) {
        SearchData search = MATCH[count];
        StringSearch strsrch = new StringSearch(search.pattern, new StringCharacterIterator(search.text), m_en_us_, null);
        int size = 0;
        while (search.offset[size] != -1) {
            size++;
        }
        if (strsrch.first() != search.offset[0]) {
            errln("Error getting first match");
        }
        if (strsrch.last() != search.offset[size - 1]) {
            errln("Error getting last match");
        }
        int index = 0;
        while (index < size) {
            if (index + 2 < size) {
                if (strsrch.following(search.offset[index + 2] - 1) != search.offset[index + 2]) {
                    errln("Error getting following match at index " + (search.offset[index + 2] - 1));
                }
            }
            if (index + 1 < size) {
                if (strsrch.preceding(search.offset[index + 1] + search.size[index + 1] + 1) != search.offset[index + 1]) {
                    errln("Error getting preceeding match at index " + (search.offset[index + 1] + 1));
                }
            }
            index += 2;
        }
        if (strsrch.following(search.text.length()) != SearchIterator.DONE) {
            errln("Error expecting out of bounds match");
        }
        if (strsrch.preceding(0) != SearchIterator.DONE) {
            errln("Error expecting out of bounds match");
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 3 with StringSearch

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

the class SearchTest method TestContraction.

@Test
public void TestContraction() {
    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);
    } catch (Exception e) {
        errln("Error opening string search ");
    }
    for (int count = 0; count < CONTRACTION.length; count++) {
        text = CONTRACTION[count].text;
        pattern = CONTRACTION[count].pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        if (!assertEqualWithStringSearch(strsrch, CONTRACTION[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 4 with StringSearch

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

the class SearchTest method TestPatternCanonical.

@Test
public void TestPatternCanonical() {
    // StringCharacterIterator text = new StringCharacterIterator(PATTERNCANONICAL[0].text);
    m_en_us_.setStrength(PATTERNCANONICAL[0].strength);
    StringSearch strsrch = new StringSearch(PATTERNCANONICAL[0].pattern, new StringCharacterIterator(PATTERNCANONICAL[0].text), m_en_us_, null);
    strsrch.setCanonical(true);
    if (PATTERNCANONICAL[0].pattern != strsrch.getPattern()) {
        errln("Error setting pattern");
    }
    if (!assertEqualWithStringSearch(strsrch, PATTERNCANONICAL[0])) {
        m_en_us_.setStrength(TERTIARY);
        strsrch = null;
        return;
    }
    strsrch.setPattern(PATTERNCANONICAL[1].pattern);
    if (PATTERNCANONICAL[1].pattern != strsrch.getPattern()) {
        errln("Error setting pattern");
        m_en_us_.setStrength(TERTIARY);
        strsrch = null;
        return;
    }
    strsrch.reset();
    strsrch.setCanonical(true);
    if (!assertEqualWithStringSearch(strsrch, PATTERNCANONICAL[1])) {
        m_en_us_.setStrength(TERTIARY);
        strsrch = null;
        return;
    }
    strsrch.setPattern(PATTERNCANONICAL[0].pattern);
    if (PATTERNCANONICAL[0].pattern != strsrch.getPattern()) {
        errln("Error setting pattern");
        m_en_us_.setStrength(TERTIARY);
        strsrch = null;
        return;
    }
    strsrch.reset();
    strsrch.setCanonical(true);
    if (!assertEqualWithStringSearch(strsrch, PATTERNCANONICAL[0])) {
        m_en_us_.setStrength(TERTIARY);
        strsrch = null;
        return;
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Test(org.junit.Test)

Example 5 with StringSearch

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

the class SearchTest method TestUsingSearchCollator.

@Test
public void TestUsingSearchCollator() {
    String scKoText = " " + /*01*/
    "\uAC00 " + /*03*/
    "\uAC01 " + /*05*/
    "\uAC0F " + /*07*/
    "\uAFFF " + /*09*/
    "\u1100\u1161\u11A8 " + /*13*/
    "\u1100\u1161\u1100 " + /*17*/
    "\u3131\u314F\u3131 " + /*21*/
    "\u1100\u1161\u11B6 " + /*25*/
    "\u1100\u1161\u1105\u1112 " + /*30*/
    "\u1101\u1170\u11B6 " + /*34*/
    "\u00E6 " + /*36*/
    "\u1E4D " + // small letter o with tilde and acute, decomposes
    "";
    String scKoPat0 = "\uAC01";
    // 0xAC01 as conjoining jamo
    String scKoPat1 = "\u1100\u1161\u11A8";
    String scKoPat2 = "\uAC0F";
    // 0xAC0F as basic conjoining jamo
    String scKoPat3 = "\u1100\u1161\u1105\u1112";
    String scKoPat4 = "\uAFFF";
    // 0xAFFF as conjoining jamo
    String scKoPat5 = "\u1101\u1170\u11B6";
    int[] scKoSrchOff01 = { 3, 9, 13 };
    int[] scKoSrchOff23 = { 5, 21, 25 };
    int[] scKoSrchOff45 = { 7, 30 };
    int[] scKoStndOff01 = { 3, 9 };
    int[] scKoStndOff2 = { 5, 21 };
    int[] scKoStndOff3 = { 25 };
    int[] scKoStndOff45 = { 7, 30 };
    class PatternAndOffsets {

        private String pattern;

        private int[] offsets;

        PatternAndOffsets(String pat, int[] offs) {
            pattern = pat;
            offsets = offs;
        }

        public String getPattern() {
            return pattern;
        }

        public int[] getOffsets() {
            return offsets;
        }
    }
    final PatternAndOffsets[] scKoSrchPatternsOffsets = { new PatternAndOffsets(scKoPat0, scKoSrchOff01), new PatternAndOffsets(scKoPat1, scKoSrchOff01), new PatternAndOffsets(scKoPat2, scKoSrchOff23), new PatternAndOffsets(scKoPat3, scKoSrchOff23), new PatternAndOffsets(scKoPat4, scKoSrchOff45), new PatternAndOffsets(scKoPat5, scKoSrchOff45) };
    final PatternAndOffsets[] scKoStndPatternsOffsets = { new PatternAndOffsets(scKoPat0, scKoStndOff01), new PatternAndOffsets(scKoPat1, scKoStndOff01), new PatternAndOffsets(scKoPat2, scKoStndOff2), new PatternAndOffsets(scKoPat3, scKoStndOff3), new PatternAndOffsets(scKoPat4, scKoStndOff45), new PatternAndOffsets(scKoPat5, scKoStndOff45) };
    class TUSCItem {

        private String localeString;

        private String text;

        private PatternAndOffsets[] patternsAndOffsets;

        TUSCItem(String locStr, String txt, PatternAndOffsets[] patsAndOffs) {
            localeString = locStr;
            text = txt;
            patternsAndOffsets = patsAndOffs;
        }

        public String getLocaleString() {
            return localeString;
        }

        public String getText() {
            return text;
        }

        public PatternAndOffsets[] getPatternsAndOffsets() {
            return patternsAndOffsets;
        }
    }
    final TUSCItem[] tuscItems = { new TUSCItem("root", scKoText, scKoStndPatternsOffsets), new TUSCItem("root@collation=search", scKoText, scKoSrchPatternsOffsets), new TUSCItem("ko@collation=search", scKoText, scKoSrchPatternsOffsets) };
    String dummyPat = "a";
    for (TUSCItem tuscItem : tuscItems) {
        String localeString = tuscItem.getLocaleString();
        ULocale uloc = new ULocale(localeString);
        RuleBasedCollator col = null;
        try {
            col = (RuleBasedCollator) Collator.getInstance(uloc);
        } catch (Exception e) {
            errln("Error: in locale " + localeString + ", err in Collator.getInstance");
            continue;
        }
        StringCharacterIterator ci = new StringCharacterIterator(tuscItem.getText());
        StringSearch srch = new StringSearch(dummyPat, ci, col);
        for (PatternAndOffsets patternAndOffsets : tuscItem.getPatternsAndOffsets()) {
            srch.setPattern(patternAndOffsets.getPattern());
            int[] offsets = patternAndOffsets.getOffsets();
            int ioff, noff = offsets.length;
            int offset;
            srch.reset();
            ioff = 0;
            while (true) {
                offset = srch.next();
                if (offset == SearchIterator.DONE) {
                    break;
                }
                if (ioff < noff) {
                    if (offset != offsets[ioff]) {
                        errln("Error: in locale " + localeString + ", expected SearchIterator.next() " + offsets[ioff] + ", got " + offset);
                    // ioff = noff;
                    // break;
                    }
                    ioff++;
                } else {
                    errln("Error: in locale " + localeString + ", SearchIterator.next() returned more matches than expected");
                }
            }
            if (ioff < noff) {
                errln("Error: in locale " + localeString + ", SearchIterator.next() returned fewer matches than expected");
            }
            srch.reset();
            ioff = noff;
            while (true) {
                offset = srch.previous();
                if (offset == SearchIterator.DONE) {
                    break;
                }
                if (ioff > 0) {
                    ioff--;
                    if (offset != offsets[ioff]) {
                        errln("Error: in locale " + localeString + ", expected SearchIterator.previous() " + offsets[ioff] + ", got " + offset);
                    // ioff = 0;
                    // break;
                    }
                } else {
                    errln("Error: in locale " + localeString + ", expected SearchIterator.previous() returned more matches than expected");
                }
            }
            if (ioff > 0) {
                errln("Error: in locale " + localeString + ", expected SearchIterator.previous() returned fewer matches than expected");
            }
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) ULocale(android.icu.util.ULocale) 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