Search in sources :

Example 31 with UCharacterIterator

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

the class TestUCharacterIterator method TestPreviousNext.

@Test
public void TestPreviousNext() {
    // src and expect strings
    char[] src = { UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999), UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f), 0xc4, 0x1ed0 };
    // iterators
    UCharacterIterator iter1 = UCharacterIterator.getInstance(new ReplaceableString(new String(src)));
    UCharacterIterator iter2 = UCharacterIterator.getInstance(src);
    UCharacterIterator iter3 = UCharacterIterator.getInstance(new StringCharacterIterator(new String(src)));
    UCharacterIterator iter4 = UCharacterIterator.getInstance(new StringBuffer(new String(src)));
    previousNext(iter1);
    previousNext(iter2);
    previousNext(iter3);
    previousNext(iter4);
    getText(iter1, new String(src));
    getText(iter2, new String(src));
    getText(iter3, new String(src));
    /* getCharacterIterator */
    CharacterIterator citer1 = iter1.getCharacterIterator();
    CharacterIterator citer2 = iter2.getCharacterIterator();
    CharacterIterator citer3 = iter3.getCharacterIterator();
    if (citer1.first() != iter1.current()) {
        errln("getCharacterIterator for iter1 failed");
    }
    if (citer2.first() != iter2.current()) {
        errln("getCharacterIterator for iter2 failed");
    }
    if (citer3.first() != iter3.current()) {
        errln("getCharacterIterator for iter3 failed");
    }
    /* Test clone()  && moveIndex()*/
    try {
        UCharacterIterator clone1 = (UCharacterIterator) iter1.clone();
        UCharacterIterator clone2 = (UCharacterIterator) iter2.clone();
        UCharacterIterator clone3 = (UCharacterIterator) iter3.clone();
        if (clone1.moveIndex(3) != iter1.moveIndex(3)) {
            errln("moveIndex for iter1 failed");
        }
        if (clone2.moveIndex(3) != iter2.moveIndex(3)) {
            errln("moveIndex for iter2 failed");
        }
        if (clone3.moveIndex(3) != iter3.moveIndex(3)) {
            errln("moveIndex for iter1 failed");
        }
    } catch (Exception e) {
        errln("could not clone the iterator");
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) ReplaceableString(android.icu.text.ReplaceableString) Test(org.junit.Test)

Example 32 with UCharacterIterator

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

the class TestUCharacterIterator method TestUCharacterIteratorWrapper.

@Test
public void TestUCharacterIteratorWrapper() {
    String source = "asdfasdfjoiuyoiuy2341235679886765";
    UCharacterIterator it = UCharacterIterator.getInstance(source);
    CharacterIterator wrap_ci = it.getCharacterIterator();
    CharacterIterator ci = new StringCharacterIterator(source);
    wrap_ci.setIndex(10);
    ci.setIndex(10);
    String moves = "0+0+0--0-0-+++0--+++++++0--------++++0000----0-";
    int c1, c2;
    char m;
    int movesIndex = 0;
    while (movesIndex < moves.length()) {
        m = moves.charAt(movesIndex++);
        if (m == '-') {
            c1 = wrap_ci.previous();
            c2 = ci.previous();
        } else if (m == '0') {
            c1 = wrap_ci.current();
            c2 = ci.current();
        } else {
            // m=='+'
            c1 = wrap_ci.next();
            c2 = ci.next();
        }
        // compare results
        if (c1 != c2) {
            // copy the moves until the current (m) move, and terminate
            String history = moves.substring(0, movesIndex);
            errln("error: mismatch in Normalizer iteration at " + history + ": " + "got c1= " + hex(c1) + " != expected c2= " + hex(c2));
            break;
        }
        // compare indexes
        if (wrap_ci.getIndex() != ci.getIndex()) {
            // copy the moves until the current (m) move, and terminate
            String history = moves.substring(0, movesIndex);
            errln("error: index mismatch in Normalizer iteration at " + history + " : " + "Normalizer index " + wrap_ci.getIndex() + " expected " + ci.getIndex());
            break;
        }
    }
    if (ci.first() != wrap_ci.first()) {
        errln("CharacterIteratorWrapper.first() failed. expected: " + ci.first() + " got: " + wrap_ci.first());
    }
    if (ci.last() != wrap_ci.last()) {
        errln("CharacterIteratorWrapper.last() failed expected: " + ci.last() + " got: " + wrap_ci.last());
    }
    if (ci.getBeginIndex() != wrap_ci.getBeginIndex()) {
        errln("CharacterIteratorWrapper.getBeginIndex() failed expected: " + ci.getBeginIndex() + " got: " + wrap_ci.getBeginIndex());
    }
    if (ci.getEndIndex() != wrap_ci.getEndIndex()) {
        errln("CharacterIteratorWrapper.getEndIndex() failed expected: " + ci.getEndIndex() + " got: " + wrap_ci.getEndIndex());
    }
    try {
        CharacterIterator cloneWCI = (CharacterIterator) wrap_ci.clone();
        if (wrap_ci.getIndex() != cloneWCI.getIndex()) {
            errln("CharacterIteratorWrapper.clone() failed expected: " + wrap_ci.getIndex() + " got: " + cloneWCI.getIndex());
        }
    } catch (Exception e) {
        errln("CharacterIterator.clone() failed");
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) ReplaceableString(android.icu.text.ReplaceableString) Test(org.junit.Test)

Example 33 with UCharacterIterator

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

the class TestUCharacterIterator method TestIterationUChar32.

// Tests for new API for utf-16 support
@Test
public void TestIterationUChar32() {
    String text = "\u0061\u0062\ud841\udc02\u20ac\ud7ff\ud842\udc06\ud801\udc00\u0061";
    int c;
    int i;
    {
        UCharacterIterator iter = UCharacterIterator.getInstance(text);
        String iterText = iter.getText();
        if (!iterText.equals(text))
            errln("iter.getText() failed");
        iter.setIndex(1);
        if (iter.currentCodePoint() != UTF16.charAt(text, 1))
            errln("Iterator didn't start out in the right place.");
        iter.setToStart();
        c = iter.currentCodePoint();
        i = 0;
        i = iter.moveCodePointIndex(1);
        c = iter.currentCodePoint();
        if (c != UTF16.charAt(text, 1) || i != 1)
            errln("moveCodePointIndex(1) didn't work correctly expected " + hex(c) + " got " + hex(UTF16.charAt(text, 1)) + " i= " + i);
        i = iter.moveCodePointIndex(2);
        c = iter.currentCodePoint();
        if (c != UTF16.charAt(text, 4) || i != 4)
            errln("moveCodePointIndex(2) didn't work correctly expected " + hex(c) + " got " + hex(UTF16.charAt(text, 4)) + " i= " + i);
        i = iter.moveCodePointIndex(-2);
        c = iter.currentCodePoint();
        if (c != UTF16.charAt(text, 1) || i != 1)
            errln("moveCodePointIndex(-2) didn't work correctly expected " + hex(c) + " got " + hex(UTF16.charAt(text, 1)) + " i= " + i);
        iter.setToLimit();
        i = iter.moveCodePointIndex(-2);
        c = iter.currentCodePoint();
        if (c != UTF16.charAt(text, (text.length() - 3)) || i != (text.length() - 3))
            errln("moveCodePointIndex(-2) didn't work correctly expected " + hex(c) + " got " + hex(UTF16.charAt(text, (text.length() - 3))) + " i= " + i);
        iter.setToStart();
        c = iter.currentCodePoint();
        i = 0;
        // testing first32PostInc, nextCodePointPostInc, setTostart
        i = 0;
        iter.setToStart();
        c = iter.next();
        if (c != UTF16.charAt(text, i))
            errln("first32PostInc failed.  Expected->" + hex(UTF16.charAt(text, i)) + " Got-> " + hex(c));
        if (iter.getIndex() != UTF16.getCharCount(c) + i)
            errln("getIndex() after first32PostInc() failed");
        iter.setToStart();
        i = 0;
        if (iter.getIndex() != 0)
            errln("setToStart failed");
        logln("Testing forward iteration...");
        do {
            if (c != UCharacterIterator.DONE)
                c = iter.nextCodePoint();
            if (c != UTF16.charAt(text, i))
                errln("Character mismatch at position " + i + ", iterator has " + hex(c) + ", string has " + hex(UTF16.charAt(text, i)));
            i += UTF16.getCharCount(c);
            if (iter.getIndex() != i)
                errln("getIndex() aftr nextCodePointPostInc() isn't working right");
            c = iter.currentCodePoint();
            if (c != UCharacterIterator.DONE && c != UTF16.charAt(text, i))
                errln("current() after nextCodePointPostInc() isn't working right");
        } while (c != UCharacterIterator.DONE);
        c = iter.nextCodePoint();
        if (c != UCharacterIterator.DONE)
            errln("nextCodePointPostInc() didn't return DONE at the beginning");
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) ReplaceableString(android.icu.text.ReplaceableString) Test(org.junit.Test)

Aggregations

UCharacterIterator (android.icu.text.UCharacterIterator)33 StringPrepParseException (android.icu.text.StringPrepParseException)17 Test (org.junit.Test)15 StringCharacterIterator (java.text.StringCharacterIterator)5 CharacterIterator (java.text.CharacterIterator)4 Normalizer (android.icu.text.Normalizer)3 ReplaceableString (android.icu.text.ReplaceableString)3 CollationElementIterator (android.icu.text.CollationElementIterator)2 RuleBasedCollator (android.icu.text.RuleBasedCollator)2 CollationData (android.icu.impl.coll.CollationData)1 FCDIterCollationIterator (android.icu.impl.coll.FCDIterCollationIterator)1 FCDUTF16CollationIterator (android.icu.impl.coll.FCDUTF16CollationIterator)1 Collator (android.icu.text.Collator)1 StringPrep (android.icu.text.StringPrep)1