Search in sources :

Example 16 with CollationElementIterator

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

the class CollationIteratorTest method TestNormalizedUnicodeChar.

/**
 * Test for CollationElementIterator previous and next for the whole set of
 * unicode characters with normalization on.
 */
@Test
public void TestNormalizedUnicodeChar() {
    // thai should have normalization on
    RuleBasedCollator th_th = null;
    try {
        th_th = (RuleBasedCollator) Collator.getInstance(new Locale("th", "TH"));
    } catch (Exception e) {
        warnln("Error creating Thai collator");
        return;
    }
    StringBuffer source = new StringBuffer();
    source.append('\uFDFA');
    CollationElementIterator iter = th_th.getCollationElementIterator(source.toString());
    CollationTest.backAndForth(this, iter);
    for (char codepoint = 0x1; codepoint < 0xfffe; ) {
        source.delete(0, source.length());
        while (codepoint % 0xFF != 0) {
            if (UCharacter.isDefined(codepoint)) {
                source.append(codepoint);
            }
            codepoint++;
        }
        if (UCharacter.isDefined(codepoint)) {
            source.append(codepoint);
        }
        if (codepoint != 0xFFFF) {
            codepoint++;
        }
        /*if (((int)codepoint) >= 0xfe00) {
                String str = source.substring(185, 190);
                System.out.println(android.icu.impl.Utility.escape(str));
                System.out.println("codepoint " 
                                   + Integer.toHexString(codepoint) 
                                   + "length " + str.length());
                iter = th_th.getCollationElementIterator(str);
                CollationTest.backAndForth(this, iter);
            */
        iter = th_th.getCollationElementIterator(source.toString());
        // A basic test to see if it's working at all
        CollationTest.backAndForth(this, iter);
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 17 with CollationElementIterator

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

the class CollationIteratorTest method TestMaxExpansion.

/**
 * @bug 4108762
 * Test for getMaxExpansion()
 */
@Test
public void TestMaxExpansion() /* char* par */
{
    int unassigned = 0xEFFFD;
    String rule = "&a < ab < c/aba < d < z < ch";
    RuleBasedCollator coll = null;
    try {
        coll = new RuleBasedCollator(rule);
    } catch (Exception e) {
        warnln("Fail to create RuleBasedCollator");
        return;
    }
    char ch = 0;
    String str = String.valueOf(ch);
    CollationElementIterator iter = coll.getCollationElementIterator(str);
    while (ch < 0xFFFF) {
        int count = 1;
        ch++;
        str = String.valueOf(ch);
        iter.setText(str);
        int order = iter.previous();
        // thai management
        if (order == 0) {
            order = iter.previous();
        }
        while (iter.previous() != CollationElementIterator.NULLORDER) {
            count++;
        }
        if (iter.getMaxExpansion(order) < count) {
            errln("Failure at codepoint " + ch + ", maximum expansion count < " + count);
        }
    }
    // testing for exact max expansion
    ch = 0;
    while (ch < 0x61) {
        str = String.valueOf(ch);
        iter.setText(str);
        int order = iter.previous();
        if (iter.getMaxExpansion(order) != 1) {
            errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count == 1");
        }
        ch++;
    }
    ch = 0x63;
    str = String.valueOf(ch);
    iter.setText(str);
    int temporder = iter.previous();
    if (iter.getMaxExpansion(temporder) != 3) {
        errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count == 3");
    }
    ch = 0x64;
    str = String.valueOf(ch);
    iter.setText(str);
    temporder = iter.previous();
    if (iter.getMaxExpansion(temporder) != 1) {
        errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count == 1");
    }
    str = UCharacter.toString(unassigned);
    iter.setText(str);
    temporder = iter.previous();
    if (iter.getMaxExpansion(temporder) != 2) {
        errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count == 2");
    }
    // testing jamo
    ch = 0x1165;
    str = String.valueOf(ch);
    iter.setText(str);
    temporder = iter.previous();
    if (iter.getMaxExpansion(temporder) > 3) {
        errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count < 3");
    }
    // testing special jamo &a<\u1165
    rule = "\u0026\u0071\u003c\u1165\u002f\u0071\u0071\u0071\u0071";
    try {
        coll = new RuleBasedCollator(rule);
    } catch (Exception e) {
        errln("Fail to create RuleBasedCollator");
        return;
    }
    iter = coll.getCollationElementIterator(str);
    temporder = iter.previous();
    if (iter.getMaxExpansion(temporder) != 6) {
        errln("Failure at codepoint 0x" + Integer.toHexString(ch) + " maximum expansion count == 6");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 18 with CollationElementIterator

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

the class CollationIteratorTest method TestNormalization.

/**
 * Test the incremental normalization
 */
@Test
public void TestNormalization() {
    String rules = "&a < \u0300\u0315 < A\u0300\u0315 < \u0316\u0315B < \u0316\u0300\u0315";
    String[] testdata = { "\u1ED9", "o\u0323\u0302", "\u0300\u0315", "\u0315\u0300", "A\u0300\u0315B", "A\u0315\u0300B", "A\u0316\u0315B", "A\u0315\u0316B", "\u0316\u0300\u0315", "\u0315\u0300\u0316", "A\u0316\u0300\u0315B", "A\u0315\u0300\u0316B", "\u0316\u0315\u0300", "A\u0316\u0315\u0300B" };
    RuleBasedCollator coll = null;
    try {
        coll = new RuleBasedCollator(rules);
        coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (Exception e) {
        warnln("ERROR: in creation of collator using rules " + rules);
        return;
    }
    CollationElementIterator iter = coll.getCollationElementIterator("testing");
    for (int count = 0; count < testdata.length; count++) {
        iter.setText(testdata[count]);
        CollationTest.backAndForth(this, iter);
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 19 with CollationElementIterator

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

the class CollationIteratorTest method TestOffset.

/**
 * Test for getOffset() and setOffset()
 */
@Test
public void TestOffset() /* char* par */
{
    RuleBasedCollator en_us;
    try {
        en_us = (RuleBasedCollator) Collator.getInstance(Locale.US);
    } catch (Exception e) {
        warnln("ERROR: in creation of collator of ENGLISH locale");
        return;
    }
    CollationElementIterator iter = en_us.getCollationElementIterator(test1);
    // testing boundaries
    iter.setOffset(0);
    if (iter.previous() != CollationElementIterator.NULLORDER) {
        errln("Error: After setting offset to 0, we should be at the end " + "of the backwards iteration");
    }
    iter.setOffset(test1.length());
    if (iter.next() != CollationElementIterator.NULLORDER) {
        errln("Error: After setting offset to the end of the string, we " + "should be at the end of the forwards iteration");
    }
    // Run all the way through the iterator, then get the offset
    int[] orders = CollationTest.getOrders(iter);
    logln("orders.length = " + orders.length);
    int offset = iter.getOffset();
    if (offset != test1.length()) {
        String msg1 = "offset at end != length: ";
        String msg2 = " vs ";
        errln(msg1 + offset + msg2 + test1.length());
    }
    // Now set the offset back to the beginning and see if it works
    CollationElementIterator pristine = en_us.getCollationElementIterator(test1);
    try {
        iter.setOffset(0);
    } catch (Exception e) {
        errln("setOffset failed.");
    }
    assertEqual(iter, pristine);
    // setting offset in the middle of a contraction
    String contraction = "change";
    RuleBasedCollator tailored = null;
    try {
        tailored = new RuleBasedCollator("& a < ch");
    } catch (Exception e) {
        errln("Error: in creation of Spanish collator");
        return;
    }
    iter = tailored.getCollationElementIterator(contraction);
    int[] order = CollationTest.getOrders(iter);
    // sets offset in the middle of ch
    iter.setOffset(1);
    int[] order2 = CollationTest.getOrders(iter);
    if (!Arrays.equals(order, order2)) {
        errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
    }
    contraction = "peache";
    iter = tailored.getCollationElementIterator(contraction);
    iter.setOffset(3);
    order = CollationTest.getOrders(iter);
    // sets offset in the middle of ch
    iter.setOffset(4);
    order2 = CollationTest.getOrders(iter);
    if (!Arrays.equals(order, order2)) {
        errln("Error: setting offset in the middle of a contraction should be the same as setting it to the start of the contraction");
    }
    // setting offset in the middle of a surrogate pair
    String surrogate = "\ud800\udc00str";
    iter = tailored.getCollationElementIterator(surrogate);
    order = CollationTest.getOrders(iter);
    // sets offset in the middle of surrogate
    iter.setOffset(1);
    order2 = CollationTest.getOrders(iter);
    if (!Arrays.equals(order, order2)) {
        errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
    }
    surrogate = "simple\ud800\udc00str";
    iter = tailored.getCollationElementIterator(surrogate);
    iter.setOffset(6);
    order = CollationTest.getOrders(iter);
    // sets offset in the middle of surrogate
    iter.setOffset(7);
    order2 = CollationTest.getOrders(iter);
    if (!Arrays.equals(order, order2)) {
        errln("Error: setting offset in the middle of a surrogate pair should be the same as setting it to the start of the surrogate pair");
    }
// TODO: try iterating halfway through a messy string.
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 20 with CollationElementIterator

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

the class CollationMiscTest method TestHungarianTailoring.

/* Track7223: CollationElementIterator does not return correct order for Hungarian */
@Test
public void TestHungarianTailoring() {
    String rules = new String("&DZ<dzs<<<Dzs<<<DZS" + "&G<gy<<<Gy<<<GY" + "&L<ly<<<Ly<<<LY" + "&N<ny<<<Ny<<<NY" + "&S<sz<<<Sz<<<SZ" + "&T<ty<<<Ty<<<TY" + "&Z<zs<<<Zs<<<ZS" + "&O<\u00f6<<<\u00d6<<\u0151<<<\u0150" + "&U<\u00fc<<<\u00dc<<\u0171<<<\u0171" + "&cs<<<ccs/cs" + "&Cs<<<Ccs/cs" + "&CS<<<CCS/CS" + "&dz<<<ddz/dz" + "&Dz<<<Ddz/dz" + "&DZ<<<DDZ/DZ" + "&dzs<<<ddzs/dzs" + "&Dzs<<<Ddzs/dzs" + "&DZS<<<DDZS/DZS" + "&gy<<<ggy/gy" + "&Gy<<<Ggy/gy" + "&GY<<<GGY/GY");
    RuleBasedCollator coll;
    try {
        String str1 = "ggy";
        String str2 = "GGY";
        coll = new RuleBasedCollator(rules);
        if (coll.compare("ggy", "GGY") >= 0) {
            errln("TestHungarianTailoring.compare(" + str1 + "," + str2 + ") was suppose to return -1 ");
        }
        CollationKey sortKey1 = coll.getCollationKey(str1);
        CollationKey sortKey2 = coll.getCollationKey(str2);
        if (sortKey1.compareTo(sortKey2) >= 0) {
            errln("TestHungarianTailoring getCollationKey(\"" + str1 + "\") was suppose " + "less than getCollationKey(\"" + str2 + "\").");
            errln("  getCollationKey(\"ggy\"):" + CollationTest.prettify(sortKey1) + "  getCollationKey(\"GGY\"):" + CollationTest.prettify(sortKey2));
        }
        CollationElementIterator iter1 = coll.getCollationElementIterator(str1);
        CollationElementIterator iter2 = coll.getCollationElementIterator(str2);
        int ce1, ce2;
        while ((ce1 = iter1.next()) != CollationElementIterator.NULLORDER && (ce2 = iter2.next()) != CollationElementIterator.NULLORDER) {
            if (ce1 > ce2) {
                errln("TestHungarianTailoring.CollationElementIterator(" + str1 + "," + str2 + ") was suppose to return -1 ");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Aggregations

CollationElementIterator (android.icu.text.CollationElementIterator)26 RuleBasedCollator (android.icu.text.RuleBasedCollator)25 Test (org.junit.Test)25 ULocale (android.icu.util.ULocale)4 Collator (android.icu.text.Collator)3 Locale (java.util.Locale)3 CollationKey (android.icu.text.CollationKey)2 RawCollationKey (android.icu.text.RawCollationKey)2 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 StringCharacterIterator (java.text.StringCharacterIterator)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1