Search in sources :

Example 6 with Normalizer

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

the class BasicTest method TestPreviousNextJCI.

// Only in ICU4j
@Test
public void TestPreviousNextJCI() {
    // src and expect strings
    char[] src = { UTF16.getLeadSurrogate(0x2f999), UTF16.getTrailSurrogate(0x2f999), UTF16.getLeadSurrogate(0x1d15f), UTF16.getTrailSurrogate(0x1d15f), 0xc4, 0x1ed0 };
    int[] expect = { 0x831d, 0x1d158, 0x1d165, 0x41, 0x308, 0x4f, 0x302, 0x301 };
    // expected src indexes corresponding to expect indexes
    int[] expectIndex = { 0, 2, 2, 4, 4, 5, 5, 5, // behind last character
    6 };
    // initial indexes into the src and expect strings
    final int SRC_MIDDLE = 4;
    final int EXPECT_MIDDLE = 3;
    // movement vector
    // - for previous(), 0 for current(), + for next()
    // not const so that we can terminate it below for the error message
    String moves = "0+0+0--0-0-+++0--+++++++0--------";
    // iterators
    StringCharacterIterator text = new StringCharacterIterator(new String(src));
    Normalizer iter = new Normalizer(text, Normalizer.NFD, 0);
    UCharIterator iter32 = new UCharIterator(expect, expect.length, EXPECT_MIDDLE);
    int c1, c2;
    char m;
    // initially set the indexes into the middle of the strings
    iter.setIndexOnly(SRC_MIDDLE);
    // move around and compare the iteration code points with
    // the expected ones
    int movesIndex = 0;
    while (movesIndex < moves.length()) {
        m = moves.charAt(movesIndex++);
        if (m == '-') {
            c1 = iter.previous();
            c2 = iter32.previous();
        } else if (m == '0') {
            c1 = iter.current();
            c2 = iter32.current();
        } else /* m=='+' */
        {
            c1 = iter.next();
            c2 = iter32.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 (iter.getIndex() != expectIndex[iter32.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 " + iter.getIndex() + " expected " + expectIndex[iter32.getIndex()]);
            break;
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 7 with Normalizer

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

the class BasicTest method TestDebugIter.

@Test
public void TestDebugIter() {
    String src = Utility.unescape("\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e");
    String expected = Utility.unescape("\\U0001d15e\\U0001d157\\U0001d165\\U0001d15e");
    Normalizer iter = new Normalizer(new StringCharacterIterator(Utility.unescape(src)), Normalizer.NONE, 0);
    int index = 0;
    int ch;
    UCharacterIterator cIter = UCharacterIterator.getInstance(expected);
    while ((ch = iter.next()) != Normalizer.DONE) {
        if (index >= expected.length()) {
            errln("FAIL: " + "Unexpected character '" + (char) ch + "' (" + hex(ch) + ")" + " at index " + index);
            break;
        }
        int want = UTF16.charAt(expected, index);
        if (ch != want) {
            errln("FAIL: " + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
        index += UTF16.getCharCount(ch);
    }
    if (index < expected.length()) {
        errln("FAIL: " + "Only got " + index + " chars, expected " + expected.length());
    }
    cIter.setToLimit();
    while ((ch = iter.previous()) != Normalizer.DONE) {
        int want = cIter.previousCodePoint();
        if (ch != want) {
            errln("FAIL: " + "got '" + (char) ch + "' (" + hex(ch) + ")" + " but expected '" + want + "' (" + hex(want) + ")" + " at index " + index);
        }
    }
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 8 with Normalizer

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

the class TestDeprecatedNormalizerAPI method TestNormalizerAPI.

@Test
public void TestNormalizerAPI() {
    // instantiate a Normalizer from a CharacterIterator
    String s = Utility.unescape("a\u0308\uac00\\U0002f800");
    // make s a bit longer and more interesting
    java.text.CharacterIterator iter = new StringCharacterIterator(s + s);
    // test deprecated constructors
    Normalizer norm = new Normalizer(iter, Normalizer.NFC, 0);
    if (norm.next() != 0xe4) {
        errln("error in Normalizer(CharacterIterator).next()");
    }
    Normalizer norm2 = new Normalizer(s, Normalizer.NFC, 0);
    if (norm2.next() != 0xe4) {
        errln("error in Normalizer(CharacterIterator).next()");
    }
    // test clone(), ==, and hashCode()
    Normalizer clone = (Normalizer) norm.clone();
    if (clone.getBeginIndex() != norm.getBeginIndex()) {
        errln("error in Normalizer.getBeginIndex()");
    }
    if (clone.getEndIndex() != norm.getEndIndex()) {
        errln("error in Normalizer.getEndIndex()");
    }
    // test setOption() and getOption()
    clone.setOption(0xaa0000, true);
    clone.setOption(0x20000, false);
    if (clone.getOption(0x880000) == 0 || clone.getOption(0x20000) == 1) {
        errln("error in Normalizer::setOption() or Normalizer::getOption()");
    }
    // test deprecated normalize method
    Normalizer.normalize(s, Normalizer.NFC, 0);
    // test deprecated compose method
    Normalizer.compose(s, false, 0);
    // test deprecated decompose method
    Normalizer.decompose(s, false, 0);
}
Also used : StringCharacterIterator(android.icu.text.StringCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 9 with Normalizer

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

the class BasicTest method TestNone.

@Test
public void TestNone() throws Exception {
    Normalizer norm = new Normalizer("", Normalizer.NONE, 0);
    iterateTest(norm, canonTests, 0);
    staticTest(Normalizer.NONE, canonTests, 0);
}
Also used : Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 10 with Normalizer

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

the class BasicTest method TestHangulDecomp.

@Test
public void TestHangulDecomp() throws Exception {
    // Make sure that the static decomposition methods work
    logln("Canonical decomposition...");
    staticTest(Normalizer.NFD, hangulCanon, 1);
    logln("Compatibility decomposition...");
    staticTest(Normalizer.NFKD, hangulCompat, 1);
    // Now the iterative decomposition methods...
    logln("Iterative decomposition...");
    Normalizer norm = new Normalizer("", Normalizer.NFD, 0);
    iterateTest(norm, hangulCanon, 1);
    norm.setMode(Normalizer.NFKD);
    iterateTest(norm, hangulCompat, 1);
    // And finally, make sure you can do it in reverse too
    logln("Reverse iteration...");
    norm.setMode(Normalizer.NFD);
    backAndForth(norm, hangulCanon);
}
Also used : Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Aggregations

Normalizer (android.icu.text.Normalizer)14 Test (org.junit.Test)14 UCharacterIterator (android.icu.text.UCharacterIterator)3 StringCharacterIterator (java.text.StringCharacterIterator)3 StringCharacterIterator (android.icu.text.StringCharacterIterator)1