Search in sources :

Example 16 with UCharacterIterator

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

the class CollationAPITest method TestElemIter.

/**
 * This tests the CollationElementIterator related APIs.
 * - creation of a CollationElementIterator object
 * - == and != operators
 * - iterating forward
 * - reseting the iterator index
 * - requesting the order properties(primary, secondary or tertiary)
 */
@Test
public void TestElemIter() {
    // logln("testing sortkey begins...");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
    String testString2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
    // logln("Constructors and comparison testing....");
    CollationElementIterator iterator1 = ((RuleBasedCollator) col).getCollationElementIterator(testString1);
    CharacterIterator chariter = new StringCharacterIterator(testString1);
    // copy ctor
    CollationElementIterator iterator2 = ((RuleBasedCollator) col).getCollationElementIterator(chariter);
    UCharacterIterator uchariter = UCharacterIterator.getInstance(testString2);
    CollationElementIterator iterator3 = ((RuleBasedCollator) col).getCollationElementIterator(uchariter);
    int offset = 0;
    offset = iterator1.getOffset();
    if (offset != 0) {
        errln("Error in getOffset for collation element iterator");
        return;
    }
    iterator1.setOffset(6);
    iterator1.setOffset(0);
    int order1, order2, order3;
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // Code coverage for dummy "not designed" hashCode() which does "assert false".
    try {
        // We don't expect any particular value.
        iterator1.hashCode();
    } catch (AssertionError ignored) {
    // Expected to be thrown if assertions are enabled.
    }
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // invalid test wrong in UCA
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    iterator1.reset();
    iterator2.reset();
    iterator3.reset();
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order2 = iterator2.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // obsolete invalid test, removed
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    doAssert(!(iterator2.equals(iterator3)), "The iterators should be different");
    logln("testing CollationElementIterator ends...");
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CollationElementIterator(android.icu.text.CollationElementIterator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 17 with UCharacterIterator

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

the class BasicTest method TestNormalizerAPI.

// test APIs that are not otherwise used - improve test coverage
@Test
public void TestNormalizerAPI() throws Exception {
    try {
        // instantiate a Normalizer from a CharacterIterator
        String s = Utility.unescape("a\u0308\uac00\\U0002f800");
        // make s a bit longer and more interesting
        UCharacterIterator iter = UCharacterIterator.getInstance(s + s);
        Normalizer norm = new Normalizer(iter, Normalizer.NFC, 0);
        if (norm.next() != 0xe4) {
            errln("error in Normalizer(CharacterIterator).next()");
        }
        // test clone(), ==, and hashCode()
        Normalizer clone = (Normalizer) norm.clone();
        if (clone.equals(norm)) {
            errln("error in Normalizer(Normalizer(CharacterIterator)).clone()!=norm");
        }
        if (clone.getLength() != norm.getLength()) {
            errln("error in Normalizer.getBeginIndex()");
        }
        // }
        if (clone.next() != 0xac00) {
            errln("error in Normalizer(Normalizer(CharacterIterator)).next()");
        }
        int ch = clone.next();
        if (ch != 0x4e3d) {
            errln("error in Normalizer(Normalizer(CharacterIterator)).clone().next()");
        }
        // position changed, must change hashCode()
        if (clone.hashCode() == norm.hashCode()) {
            errln("error in Normalizer(Normalizer(CharacterIterator)).clone().next().hashCode()==copy.hashCode()");
        }
        // test compose() and decompose()
        StringBuffer tel;
        String nfkc, nfkd;
        tel = new StringBuffer("\u2121\u2121\u2121\u2121\u2121\u2121\u2121\u2121\u2121\u2121");
        tel.insert(1, (char) 0x0301);
        nfkc = Normalizer.compose(tel.toString(), true);
        nfkd = Normalizer.decompose(tel.toString(), true);
        if (!nfkc.equals(Utility.unescape("TE\u0139TELTELTELTELTELTELTELTELTEL")) || !nfkd.equals(Utility.unescape("TEL\u0301TELTELTELTELTELTELTELTELTEL"))) {
            errln("error in Normalizer::(de)compose(): wrong result(s)");
        }
        // test setIndex()
        ch = norm.setIndex(3);
        if (ch != 0x4e3d) {
            errln("error in Normalizer(CharacterIterator).setIndex(3)");
        }
        // test setText(CharacterIterator) and getText()
        String out, out2;
        clone.setText(iter);
        out = clone.getText();
        out2 = iter.getText();
        if (!out.equals(out2) || clone.startIndex() != 0 || clone.endIndex() != iter.getLength()) {
            errln("error in Normalizer::setText() or Normalizer::getText()");
        }
        char[] fillIn1 = new char[clone.getLength()];
        char[] fillIn2 = new char[iter.getLength()];
        int len = clone.getText(fillIn1);
        iter.getText(fillIn2, 0);
        if (!Utility.arrayRegionMatches(fillIn1, 0, fillIn2, 0, len)) {
            errln("error in Normalizer.getText(). Normalizer: " + Utility.hex(new String(fillIn1)) + " Iter: " + Utility.hex(new String(fillIn2)));
        }
        clone.setText(fillIn1);
        len = clone.getText(fillIn2);
        if (!Utility.arrayRegionMatches(fillIn1, 0, fillIn2, 0, len)) {
            errln("error in Normalizer.setText() or Normalizer.getText()" + Utility.hex(new String(fillIn1)) + " Iter: " + Utility.hex(new String(fillIn2)));
        }
        // test setText(UChar *), getUMode() and setMode()
        clone.setText(s);
        clone.setIndexOnly(1);
        clone.setMode(Normalizer.NFD);
        if (clone.getMode() != Normalizer.NFD) {
            errln("error in Normalizer::setMode() or Normalizer::getMode()");
        }
        if (clone.next() != 0x308 || clone.next() != 0x1100) {
            errln("error in Normalizer::setText() or Normalizer::setMode()");
        }
        // test last()/previous() with an internal buffer overflow
        StringBuffer buf = new StringBuffer("aaaaaaaaaa");
        buf.setCharAt(10 - 1, '\u0308');
        clone.setText(buf);
        if (clone.last() != 0x308) {
            errln("error in Normalizer(10*U+0308).last()");
        }
        // test UNORM_NONE
        norm.setMode(Normalizer.NONE);
        if (norm.first() != 0x61 || norm.next() != 0x308 || norm.last() != 0x2f800) {
            errln("error in Normalizer(UNORM_NONE).first()/next()/last()");
        }
        out = Normalizer.normalize(s, Normalizer.NONE);
        if (!out.equals(s)) {
            errln("error in Normalizer::normalize(UNORM_NONE)");
        }
        ch = 0x1D15E;
        String exp = "\\U0001D157\\U0001D165";
        String ns = Normalizer.normalize(ch, Normalizer.NFC);
        if (!ns.equals(Utility.unescape(exp))) {
            errln("error in Normalizer.normalize(int,Mode)");
        }
        ns = Normalizer.normalize(ch, Normalizer.NFC, 0);
        if (!ns.equals(Utility.unescape(exp))) {
            errln("error in Normalizer.normalize(int,Mode,int)");
        }
    } catch (Exception e) {
        throw e;
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 18 with UCharacterIterator

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

the class BasicTest method TestBugJ2068.

@Test
public void TestBugJ2068() {
    String sample = "The quick brown fox jumped over the lazy dog";
    UCharacterIterator text = UCharacterIterator.getInstance(sample);
    Normalizer norm = new Normalizer(text, Normalizer.NFC, 0);
    text.setIndex(4);
    if (text.current() == norm.current()) {
        errln("Normalizer is not cloning the UCharacterIterator");
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) Normalizer(android.icu.text.Normalizer) Test(org.junit.Test)

Example 19 with UCharacterIterator

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

the class TestIDNARef method doTestToUnicode.

private void doTestToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestToUnicode.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertToUnicode(src, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + prettify(out));
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToUnicode(inBuf, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToUnicode(inIter, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + prettify(out));
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("Did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("Did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 20 with UCharacterIterator

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

the class TestIDNARef method doTestToASCII.

private void doTestToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestToASCII.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertToASCII(src, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToASCII(inBuf, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToASCII(inIter, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

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