Search in sources :

Example 1 with Transliterator

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

the class AnyScriptTest method TestCommonDigits.

@Test
public void TestCommonDigits() {
    UnicodeSet westernDigitSet = new UnicodeSet("[0-9]");
    UnicodeSet westernDigitSetAndMarks = new UnicodeSet("[[0-9][:Mn:]]");
    UnicodeSet arabicDigitSet = new UnicodeSet("[[:Nd:]&[:block=Arabic:]]");
    Transliterator latin = Transliterator.getInstance("Any-Latn");
    Transliterator arabic = Transliterator.getInstance("Any-Arabic");
    String westernDigits = getList(westernDigitSet);
    String arabicDigits = getList(arabicDigitSet);
    String fromArabic = latin.transform(arabicDigits);
    assertContainsAll("Any-Latin transforms Arabic digits", westernDigitSetAndMarks, fromArabic);
    if (false) {
        // we don't require conversion to Arabic digits
        String fromLatin = arabic.transform(westernDigits);
        assertContainsAll("Any-Arabic transforms Western digits", arabicDigitSet, fromLatin);
    }
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 2 with Transliterator

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

the class AnyScriptTest method TestScripts.

@Test
public void TestScripts() {
    // get a couple of characters of each script for testing
    StringBuffer testBuffer = new StringBuffer();
    for (int script = 0; script < UScript.CODE_LIMIT; ++script) {
        UnicodeSet test = new UnicodeSet().applyPropertyAlias("script", UScript.getName(script));
        int count = Math.min(20, test.size());
        for (int i = 0; i < count; ++i) {
            testBuffer.append(UTF16.valueOf(test.charAt(i)));
        }
    }
    String test = testBuffer.toString();
    logln("Test line: " + test);
    int inclusion = TestFmwk.getExhaustiveness();
    boolean testedUnavailableScript = false;
    for (int script = 0; script < UScript.CODE_LIMIT; ++script) {
        if (script == UScript.COMMON || script == UScript.INHERITED) {
            continue;
        }
        // Make sure, however, that we test at least one unavailable script
        if (inclusion < 10 && script != UScript.LATIN && script != UScript.HAN && script != UScript.HIRAGANA && testedUnavailableScript) {
            continue;
        }
        // long name
        String scriptName = UScript.getName(script);
        ULocale locale = new ULocale(scriptName);
        if (locale.getLanguage().equals("new") || locale.getLanguage().equals("pau")) {
            if (logKnownIssue("11171", "long script name loosely looks like a locale ID with a known likely script")) {
                continue;
            }
        }
        Transliterator t;
        try {
            t = Transliterator.getInstance("any-" + scriptName);
        } catch (Exception e) {
            testedUnavailableScript = true;
            logln("Skipping unavailable: " + scriptName);
            // we don't handle all scripts
            continue;
        }
        logln("Checking: " + scriptName);
        if (t != null) {
            // just verify we don't crash
            t.transform(test);
        }
        // 4-letter script code
        String shortScriptName = UScript.getShortName(script);
        try {
            t = Transliterator.getInstance("any-" + shortScriptName);
        } catch (Exception e) {
            errln("Transliterator.getInstance() worked for \"any-" + scriptName + "\" but not for \"any-" + shortScriptName + '\"');
        }
        // just verify we don't crash
        t.transform(test);
    }
}
Also used : ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 3 with Transliterator

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

the class CompoundTransliteratorTest method TestGetTransliterator.

@Test
public void TestGetTransliterator() {
    logln("Testing the getTransliterator() API of CompoundTransliterator");
    String ID = "Latin-Greek;Greek-Latin;Latin-Devanagari;Devanagari-Latin;Latin-Cyrillic;Cyrillic-Latin;Any-Hex;Hex-Any";
    Transliterator ct1 = null;
    try {
        // ct1=new CompoundTransliterator(ID);
        ct1 = Transliterator.getInstance(ID);
    } catch (IllegalArgumentException iae) {
        errln("CompoundTransliterator construction failed for ID=" + ID);
        throw iae;
    }
    // int count=ct1.getCount();
    Transliterator[] elems = ct1.getElements();
    int count = elems.length;
    String[] array = split(ID, ';');
    if (count != array.length) {
        errln("Error: getCount() failed. Expected:" + array.length + " got:" + count);
    }
    for (int i = 0; i < count; i++) {
        // String child= ct1.getTransliterator(i).getID();
        String child = elems[i].getID();
        if (!child.equals(array[i])) {
            errln("Error getTransliterator() failed: Expected->" + array[i] + " Got->" + child);
        } else {
            logln("OK: getTransliterator() passed: Expected->" + array[i] + " Got->" + child);
        }
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 4 with Transliterator

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

the class CompoundTransliteratorTest method expect.

private void expect(Transliterator t, String source, String expectedResult) {
    String result = t.transliterate(source);
    expectAux(t.getID() + ":String", source, result, expectedResult);
    ReplaceableString rsource = new ReplaceableString(source);
    t.transliterate(rsource);
    result = rsource.toString();
    expectAux(t.getID() + ":Replaceable", source, result, expectedResult);
    // Test keyboard (incremental) transliteration -- this result
    // must be the same after we finalize (see below).
    rsource.replace(0, rsource.length(), "");
    Transliterator.Position index = new Transliterator.Position();
    StringBuffer log = new StringBuffer();
    for (int i = 0; i < source.length(); ++i) {
        if (i != 0) {
            log.append(" + ");
        }
        log.append(source.charAt(i)).append(" -> ");
        t.transliterate(rsource, index, String.valueOf(source.charAt(i)));
        // Append the string buffer with a vertical bar '|' where
        // the committed index is.
        String s = rsource.toString();
        log.append(s.substring(0, index.start)).append('|').append(s.substring(index.start));
    }
    // As a final step in keyboard transliteration, we must call
    // transliterate to finish off any pending partial matches that
    // were waiting for more input.
    t.finishTransliteration(rsource, index);
    result = rsource.toString();
    log.append(" => ").append(rsource.toString());
    expectAux(t.getID() + ":Keyboard", log.toString(), result.equals(expectedResult), expectedResult);
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator)

Example 5 with Transliterator

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

the class ErrorTest method TestTransliteratorErrors.

@Test
public void TestTransliteratorErrors() {
    String trans = "Latin-Greek";
    String bogusID = "LATINGREEK-GREEKLATIN";
    String newID = "Bogus-Latin";
    String newIDRules = "zzz > Z; f <> ph";
    String bogusRules = "a } [b-g m-p ";
    ReplaceableString testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
    String insertString = "cats and dogs";
    int stoppedAt = 0, len;
    Transliterator.Position pos = new Transliterator.Position();
    Transliterator t = Transliterator.getInstance(trans, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: construction of Latin-Greek");
        return;
    }
    len = testString.length();
    stoppedAt = t.transliterate(testString, 0, 100);
    if (stoppedAt != -1) {
        errln("FAIL: Out of bounds check failed (1).");
    } else if (testString.length() != len) {
        testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
        errln("FAIL: Transliterate fails and the target string was modified.");
    }
    stoppedAt = t.transliterate(testString, 100, testString.length() - 1);
    if (stoppedAt != -1) {
        errln("FAIL: Out of bounds check failed (2).");
    } else if (testString.length() != len) {
        testString = new ReplaceableString("A quick fox jumped over the lazy dog.");
        errln("FAIL: Transliterate fails and the target string was modified.");
    }
    pos.start = 100;
    pos.limit = testString.length();
    try {
        t.transliterate(testString, pos);
        errln("FAIL: Start offset is out of bounds, error not reported.");
    } catch (IllegalArgumentException e) {
        logln("Start offset is out of bounds and detected.");
    }
    pos.limit = 100;
    pos.start = 0;
    try {
        t.transliterate(testString, pos);
        errln("FAIL: Limit offset is out of bounds, error not reported.\n");
    } catch (IllegalArgumentException e) {
        logln("Start offset is out of bounds and detected.");
    }
    len = pos.contextLimit = testString.length();
    pos.contextStart = 0;
    pos.limit = len - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, insertString);
        if (len == pos.limit) {
            errln("FAIL: Test insertion with string: the transliteration position limit didn't change as expected.");
        }
    } catch (IllegalArgumentException e) {
        errln("Insertion test with string failed for some reason.");
    }
    pos.contextStart = 0;
    pos.contextLimit = testString.length();
    pos.limit = testString.length() - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, 0x0061);
        if (len == pos.limit) {
            errln("FAIL: Test insertion with character: the transliteration position limit didn't change as expected.");
        }
    } catch (IllegalArgumentException e) {
        errln("FAIL: Insertion test with UTF-16 code point failed for some reason.");
    }
    len = pos.limit = testString.length();
    pos.contextStart = 0;
    pos.contextLimit = testString.length() - 1;
    pos.start = 5;
    try {
        t.transliterate(testString, pos, insertString);
        errln("FAIL: Out of bounds check failed (3).");
        if (testString.length() != len) {
            errln("FAIL: The input string was modified though the offsets were out of bounds.");
        }
    } catch (IllegalArgumentException e) {
        logln("Insertion test with out of bounds indexes.");
    }
    Transliterator t1 = null;
    try {
        t1 = Transliterator.getInstance(bogusID, Transliterator.FORWARD);
        if (t1 != null) {
            errln("FAIL: construction of bogus ID \"LATINGREEK-GREEKLATIN\"");
        }
    } catch (IllegalArgumentException e) {
    }
    // try { // unneeded - Exception cannot be thrown
    Transliterator t2 = Transliterator.createFromRules(newID, newIDRules, Transliterator.FORWARD);
    try {
        Transliterator t3 = t2.getInverse();
        errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
        if (t3 != null) {
            errln("FAIL: The newID transliterator was not registered so createInverse should fail.");
        }
    } catch (Exception e) {
    }
    // } catch (Exception e) { }
    try {
        Transliterator t4 = Transliterator.createFromRules(newID, bogusRules, Transliterator.FORWARD);
        if (t4 != null) {
            errln("FAIL: The rules is malformed but error was not reported.");
        }
    } catch (Exception e) {
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Aggregations

Transliterator (android.icu.text.Transliterator)97 Test (org.junit.Test)88 ReplaceableString (android.icu.text.ReplaceableString)66 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)57 UnicodeSet (android.icu.text.UnicodeSet)19 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)5 ULocale (android.icu.util.ULocale)3 Enumeration (java.util.Enumeration)3 UnicodeFilter (android.icu.text.UnicodeFilter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 UnicodeMap (android.icu.dev.util.UnicodeMap)1 CanonicalIterator (android.icu.text.CanonicalIterator)1 Normalizer2 (android.icu.text.Normalizer2)1 Replaceable (android.icu.text.Replaceable)1 BufferedWriter (java.io.BufferedWriter)1 HashSet (java.util.HashSet)1