Search in sources :

Example 26 with UCharacterIterator

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

the class TestIDNA method doTestToUnicode.

private void doTestToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.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 = IDNA.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 = IDNA.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 27 with UCharacterIterator

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

the class TestIDNA method TestIDNACompare.

/* Tests the method public static int compare */
@Test
public void TestIDNACompare() {
    // Testing the method public static int compare(String s1, String s2, int options)
    try {
        IDNA.compare((String) null, (String) null, 0);
        errln("IDNA.compare((String)null,(String)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare((String) null, "dummy", 0);
        errln("IDNA.compare((String)null,'dummy') was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare("dummy", (String) null, 0);
        errln("IDNA.compare('dummy',(String)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        if (IDNA.compare("dummy", "dummy", 0) != 0) {
            errln("IDNA.compare('dummy','dummy') was suppose to return a 0.");
        }
    } catch (Exception e) {
        errln("IDNA.compare('dummy','dummy') was not suppose to return an exception.");
    }
    // Testing the method public static int compare(StringBuffer s1, StringBuffer s2, int options)
    try {
        IDNA.compare((StringBuffer) null, (StringBuffer) null, 0);
        errln("IDNA.compare((StringBuffer)null,(StringBuffer)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare((StringBuffer) null, new StringBuffer("dummy"), 0);
        errln("IDNA.compare((StringBuffer)null,'dummy') was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare(new StringBuffer("dummy"), (StringBuffer) null, 0);
        errln("IDNA.compare('dummy',(StringBuffer)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        if (IDNA.compare(new StringBuffer("dummy"), new StringBuffer("dummy"), 0) != 0) {
            errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was suppose to return a 0.");
        }
    } catch (Exception e) {
        errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was not suppose to return an exception.");
    }
    // Testing the method public static int compare(UCharacterIterator s1, UCharacterIterator s2, int options)
    UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
    try {
        IDNA.compare((UCharacterIterator) null, (UCharacterIterator) null, 0);
        errln("IDNA.compare((UCharacterIterator)null,(UCharacterIterator)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare((UCharacterIterator) null, uci, 0);
        errln("IDNA.compare((UCharacterIterator)null,UCharacterIterator) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        IDNA.compare(uci, (UCharacterIterator) null, 0);
        errln("IDNA.compare(UCharacterIterator,(UCharacterIterator)null) was suppose to return an exception.");
    } catch (Exception e) {
    }
    try {
        if (IDNA.compare(uci, uci, 0) != 0) {
            errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was suppose to return a 0.");
        }
    } catch (Exception e) {
        errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was not suppose to return an exception.");
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) StringPrepParseException(android.icu.text.StringPrepParseException) Test(org.junit.Test)

Example 28 with UCharacterIterator

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

the class NamePrepTransform method map.

private String map(String src, int options) throws StringPrepParseException {
    // map
    boolean allowUnassigned = ((options & ALLOW_UNASSIGNED) > 0);
    // disable test
    String caseMapOut = mapTransform.transliterate(src);
    UCharacterIterator iter = UCharacterIterator.getInstance(caseMapOut);
    int ch;
    while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
        if (transform.unassignedSet.contains(ch) == true && allowUnassigned == false) {
            throw new StringPrepParseException("An unassigned code point was found in the input", StringPrepParseException.UNASSIGNED_ERROR);
        }
    }
    return caseMapOut;
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 29 with UCharacterIterator

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

the class CollationIteratorTest method TestSetText.

/**
 * Test for setText()
 */
@Test
public void TestSetText() /* char* par */
{
    RuleBasedCollator en_us = (RuleBasedCollator) Collator.getInstance(Locale.US);
    CollationElementIterator iter1 = en_us.getCollationElementIterator(test1);
    CollationElementIterator iter2 = en_us.getCollationElementIterator(test2);
    // Run through the second iterator just to exercise it
    int c = iter2.next();
    int i = 0;
    while (++i < 10 && c != CollationElementIterator.NULLORDER) {
        try {
            c = iter2.next();
        } catch (Exception e) {
            errln("iter2.next() returned an error.");
            break;
        }
    }
    // Now set it to point to the same string as the first iterator
    try {
        iter2.setText(test1);
    } catch (Exception e) {
        errln("call to iter2->setText(test1) failed.");
        return;
    }
    assertEqual(iter1, iter2);
    iter1.reset();
    // now use the overloaded setText(ChracterIterator&, UErrorCode) function to set the text
    CharacterIterator chariter = new StringCharacterIterator(test1);
    try {
        iter2.setText(chariter);
    } catch (Exception e) {
        errln("call to iter2->setText(chariter(test1)) failed.");
        return;
    }
    assertEqual(iter1, iter2);
    iter1.reset();
    // now use the overloaded setText(ChracterIterator&, UErrorCode) function to set the text
    UCharacterIterator uchariter = UCharacterIterator.getInstance(test1);
    try {
        iter2.setText(uchariter);
    } catch (Exception e) {
        errln("call to iter2->setText(uchariter(test1)) failed.");
        return;
    }
    assertEqual(iter1, iter2);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) CharacterIterator(java.text.CharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 30 with UCharacterIterator

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

the class TestUCharacterIterator method TestIteration.

/**
 * Testing iteration
 */
@Test
public void TestIteration() {
    UCharacterIterator iterator = UCharacterIterator.getInstance(ITERATION_STRING_);
    UCharacterIterator iterator2 = UCharacterIterator.getInstance(ITERATION_STRING_);
    iterator.setToStart();
    if (iterator.current() != ITERATION_STRING_.charAt(0)) {
        errln("Iterator failed retrieving first character");
    }
    iterator.setToLimit();
    if (iterator.previous() != ITERATION_STRING_.charAt(ITERATION_STRING_.length() - 1)) {
        errln("Iterator failed retrieving last character");
    }
    if (iterator.getLength() != ITERATION_STRING_.length()) {
        errln("Iterator failed determining begin and end index");
    }
    iterator2.setIndex(0);
    iterator.setIndex(0);
    int ch = 0;
    while (ch != UCharacterIterator.DONE) {
        int index = iterator2.getIndex();
        ch = iterator2.nextCodePoint();
        if (index != ITERATION_SUPPLEMENTARY_INDEX) {
            if (ch != (int) iterator.next() && ch != UCharacterIterator.DONE) {
                errln("Error mismatch in next() and nextCodePoint()");
            }
        } else {
            if (UTF16.getLeadSurrogate(ch) != iterator.next() || UTF16.getTrailSurrogate(ch) != iterator.next()) {
                errln("Error mismatch in next and nextCodePoint for " + "supplementary characters");
            }
        }
    }
    iterator.setIndex(ITERATION_STRING_.length());
    iterator2.setIndex(ITERATION_STRING_.length());
    while (ch != UCharacterIterator.DONE) {
        int index = iterator2.getIndex();
        ch = iterator2.previousCodePoint();
        if (index != ITERATION_SUPPLEMENTARY_INDEX) {
            if (ch != (int) iterator.previous() && ch != UCharacterIterator.DONE) {
                errln("Error mismatch in previous() and " + "previousCodePoint()");
            }
        } else {
            if (UTF16.getLeadSurrogate(ch) != iterator.previous() || UTF16.getTrailSurrogate(ch) != iterator.previous()) {
                errln("Error mismatch in previous and " + "previousCodePoint for supplementary characters");
            }
        }
    }
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator) 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