Search in sources :

Example 71 with RuleBasedCollator

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

the class CollationIteratorTest method TestDiscontiguous.

/**
 * Testing the discontiguous contractions
 */
@Test
public void TestDiscontiguous() {
    String rulestr = "&z < AB < X\u0300 < ABC < X\u0300\u0315";
    String[] src = { "ADB", "ADBC", "A\u0315B", "A\u0315BC", // base character blocked
    "XD\u0300", "XD\u0300\u0315", // non blocking combining character
    "X\u0319\u0300", "X\u0319\u0300\u0315", // blocking combining character
    "X\u0314\u0300", "X\u0314\u0300\u0315", // contraction prefix
    "ABDC", "AB\u0315C", "X\u0300D\u0315", "X\u0300\u0319\u0315", "X\u0300\u031A\u0315", // ends not with a contraction character
    "X\u0319\u0300D", "X\u0319\u0300\u0315D", "X\u0300D\u0315D", "X\u0300\u0319\u0315D", "X\u0300\u031A\u0315D" };
    String[] tgt = { // non blocking combining character
    "A D B", "A D BC", "A \u0315 B", "A \u0315 BC", // base character blocked
    "X D \u0300", "X D \u0300\u0315", // non blocking combining character
    "X\u0300 \u0319", "X\u0300\u0315 \u0319", // blocking combining character
    "X \u0314 \u0300", "X \u0314 \u0300\u0315", // contraction prefix
    "AB DC", "AB \u0315 C", "X\u0300 D \u0315", "X\u0300\u0315 \u0319", "X\u0300 \u031A \u0315", // ends not with a contraction character
    "X\u0300 \u0319D", "X\u0300\u0315 \u0319D", "X\u0300 D\u0315D", "X\u0300\u0315 \u0319D", "X\u0300 \u031A\u0315D" };
    int count = 0;
    try {
        RuleBasedCollator coll = new RuleBasedCollator(rulestr);
        CollationElementIterator iter = coll.getCollationElementIterator("");
        CollationElementIterator resultiter = coll.getCollationElementIterator("");
        while (count < src.length) {
            iter.setText(src[count]);
            int s = 0;
            while (s < tgt[count].length()) {
                int e = tgt[count].indexOf(' ', s);
                if (e < 0) {
                    e = tgt[count].length();
                }
                String resultstr = tgt[count].substring(s, e);
                resultiter.setText(resultstr);
                int ce = resultiter.next();
                while (ce != CollationElementIterator.NULLORDER) {
                    if (ce != iter.next()) {
                        errln("Discontiguos contraction test mismatch at" + count);
                        return;
                    }
                    ce = resultiter.next();
                }
                s = e + 1;
            }
            iter.reset();
            CollationTest.backAndForth(this, iter);
            count++;
        }
    } catch (Exception e) {
        warnln("Error running discontiguous tests " + e.toString());
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 72 with RuleBasedCollator

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

the class CollationIteratorTest method TestPrevious.

/**
 * Test for CollationElementIterator.previous()
 *
 * @bug 4108758 - Make sure it works with contracting characters
 */
@Test
public void TestPrevious() /* char* par */
{
    RuleBasedCollator en_us = (RuleBasedCollator) Collator.getInstance(Locale.US);
    CollationElementIterator iter = en_us.getCollationElementIterator(test1);
    // A basic test to see if it's working at all
    CollationTest.backAndForth(this, iter);
    // Test with a contracting character sequence
    String source;
    RuleBasedCollator c1 = null;
    try {
        c1 = new RuleBasedCollator("&a,A < b,B < c,C, d,D < z,Z < ch,cH,Ch,CH");
    } catch (Exception e) {
        errln("Couldn't create a RuleBasedCollator with a contracting sequence.");
        return;
    }
    source = "abchdcba";
    iter = c1.getCollationElementIterator(source);
    CollationTest.backAndForth(this, iter);
    // Test with an expanding character sequence
    RuleBasedCollator c2 = null;
    try {
        c2 = new RuleBasedCollator("&a < b < c/abd < d");
    } catch (Exception e) {
        errln("Couldn't create a RuleBasedCollator with an expanding sequence.");
        return;
    }
    source = "abcd";
    iter = c2.getCollationElementIterator(source);
    CollationTest.backAndForth(this, iter);
    // Now try both
    RuleBasedCollator c3 = null;
    try {
        c3 = new RuleBasedCollator("&a < b < c/aba < d < z < ch");
    } catch (Exception e) {
        errln("Couldn't create a RuleBasedCollator with both an expanding and a contracting sequence.");
        return;
    }
    source = "abcdbchdc";
    iter = c3.getCollationElementIterator(source);
    CollationTest.backAndForth(this, iter);
    source = "\u0e41\u0e02\u0e41\u0e02\u0e27abc";
    Collator c4 = null;
    try {
        c4 = Collator.getInstance(new Locale("th", "TH", ""));
    } catch (Exception e) {
        errln("Couldn't create a collator");
        return;
    }
    iter = ((RuleBasedCollator) c4).getCollationElementIterator(source);
    CollationTest.backAndForth(this, iter);
    source = "\u0061\u30CF\u3099\u30FC";
    Collator c5 = null;
    try {
        c5 = Collator.getInstance(new Locale("ja", "JP", ""));
    } catch (Exception e) {
        errln("Couldn't create Japanese collator\n");
        return;
    }
    iter = ((RuleBasedCollator) c5).getCollationElementIterator(source);
    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) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 73 with RuleBasedCollator

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

the class CollationCurrencyTest method TestCurrency.

@Test
public void TestCurrency() {
    // All the currency symbols, in collation order
    char[][] currency = { { 0x00A4 }, /*00A4; L; [14 36, 03, 03]    # [082B.0020.0002] # CURRENCY SIGN*/
    { 0x00A2 }, /*00A2; L; [14 38, 03, 03]    # [082C.0020.0002] # CENT SIGN*/
    { 0xFFE0 }, /*FFE0; L; [14 38, 03, 05]    # [082C.0020.0003] # FULLWIDTH CENT SIGN*/
    { 0x0024 }, /*0024; L; [14 3A, 03, 03]    # [082D.0020.0002] # DOLLAR SIGN*/
    { 0xFF04 }, /*FF04; L; [14 3A, 03, 05]    # [082D.0020.0003] # FULLWIDTH DOLLAR SIGN*/
    { 0xFE69 }, /*FE69; L; [14 3A, 03, 1D]    # [082D.0020.000F] # SMALL DOLLAR SIGN*/
    { 0x00A3 }, /*00A3; L; [14 3C, 03, 03]    # [082E.0020.0002] # POUND SIGN*/
    { 0xFFE1 }, /*FFE1; L; [14 3C, 03, 05]    # [082E.0020.0003] # FULLWIDTH POUND SIGN*/
    { 0x00A5 }, /*00A5; L; [14 3E, 03, 03]    # [082F.0020.0002] # YEN SIGN*/
    { 0xFFE5 }, /*FFE5; L; [14 3E, 03, 05]    # [082F.0020.0003] # FULLWIDTH YEN SIGN*/
    { 0x09F2 }, /*09F2; L; [14 40, 03, 03]    # [0830.0020.0002] # BENGALI RUPEE MARK*/
    { 0x09F3 }, /*09F3; L; [14 42, 03, 03]    # [0831.0020.0002] # BENGALI RUPEE SIGN*/
    { 0x0E3F }, /*0E3F; L; [14 44, 03, 03]    # [0832.0020.0002] # THAI CURRENCY SYMBOL BAHT*/
    { 0x17DB }, /*17DB; L; [14 46, 03, 03]    # [0833.0020.0002] # KHMER CURRENCY SYMBOL RIEL*/
    { 0x20A0 }, /*20A0; L; [14 48, 03, 03]    # [0834.0020.0002] # EURO-CURRENCY SIGN*/
    { 0x20A1 }, /*20A1; L; [14 4A, 03, 03]    # [0835.0020.0002] # COLON SIGN*/
    { 0x20A2 }, /*20A2; L; [14 4C, 03, 03]    # [0836.0020.0002] # CRUZEIRO SIGN*/
    { 0x20A3 }, /*20A3; L; [14 4E, 03, 03]    # [0837.0020.0002] # FRENCH FRANC SIGN*/
    { 0x20A4 }, /*20A4; L; [14 50, 03, 03]    # [0838.0020.0002] # LIRA SIGN*/
    { 0x20A5 }, /*20A5; L; [14 52, 03, 03]    # [0839.0020.0002] # MILL SIGN*/
    { 0x20A6 }, /*20A6; L; [14 54, 03, 03]    # [083A.0020.0002] # NAIRA SIGN*/
    { 0x20A7 }, /*20A7; L; [14 56, 03, 03]    # [083B.0020.0002] # PESETA SIGN*/
    { 0x20A9 }, /*20A9; L; [14 58, 03, 03]    # [083C.0020.0002] # WON SIGN*/
    { 0xFFE6 }, /*FFE6; L; [14 58, 03, 05]    # [083C.0020.0003] # FULLWIDTH WON SIGN*/
    { 0x20AA }, /*20AA; L; [14 5A, 03, 03]    # [083D.0020.0002] # NEW SHEQEL SIGN*/
    { 0x20AB }, /*20AB; L; [14 5C, 03, 03]    # [083E.0020.0002] # DONG SIGN*/
    { 0x20AC }, /*20AC; L; [14 5E, 03, 03]    # [083F.0020.0002] # EURO SIGN*/
    { 0x20AD }, /*20AD; L; [14 60, 03, 03]    # [0840.0020.0002] # KIP SIGN*/
    { 0x20AE }, /*20AE; L; [14 62, 03, 03]    # [0841.0020.0002] # TUGRIK SIGN*/
    { 0x20AF } /*20AF; L; [14 64, 03, 03]    # [0842.0020.0002] # DRACHMA SIGN*/
    };
    int i, j;
    int expectedResult = 0;
    RuleBasedCollator c = (RuleBasedCollator) Collator.getInstance(Locale.ENGLISH);
    // Compare each currency symbol against all the
    // currency symbols, including itself
    String source;
    String target;
    for (i = 0; i < currency.length; i += 1) {
        for (j = 0; j < currency.length; j += 1) {
            source = new String(currency[i]);
            target = new String(currency[j]);
            if (i < j) {
                expectedResult = -1;
            } else if (i == j) {
                expectedResult = 0;
            } else {
                expectedResult = 1;
            }
            int compareResult = c.compare(source, target);
            CollationKey sourceKey = null;
            sourceKey = c.getCollationKey(source);
            if (sourceKey == null) {
                errln("Couldn't get collationKey for source");
                continue;
            }
            CollationKey targetKey = null;
            targetKey = c.getCollationKey(target);
            if (targetKey == null) {
                errln("Couldn't get collationKey for source");
                continue;
            }
            int keyResult = sourceKey.compareTo(targetKey);
            reportCResult(source, target, sourceKey, targetKey, compareResult, keyResult, compareResult, expectedResult);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) Test(org.junit.Test)

Example 74 with RuleBasedCollator

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

the class CollationFrenchTest method TestContinuationReordering.

@Test
public void TestContinuationReordering() {
    String rule = "&0x2f00 << 0x2f01";
    try {
        RuleBasedCollator collator = new RuleBasedCollator(rule);
        collator.setFrenchCollation(true);
        CollationKey key1 = collator.getCollationKey("a\u0325\u2f00\u2f01b\u0325");
        CollationKey key2 = collator.getCollationKey("a\u0325\u2f01\u2f01b\u0325");
        if (key1.compareTo(key2) >= 0) {
            errln("Error comparing continuation strings");
        }
    } catch (Exception e) {
        errln(e.toString());
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) Test(org.junit.Test)

Example 75 with RuleBasedCollator

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

the class CollationKanaTest method TestCommonCharacters.

/*
     * Test common Hiragana and Katakana characters (e.g. 0x3099) (ticket:6140)
     */
@Test
public void TestCommonCharacters() {
    char[] tmp1 = { 0x3058, 0x30B8 };
    char[] tmp2 = { 0x3057, 0x3099, 0x30B7, 0x3099 };
    CollationKey key1, key2;
    int result;
    String string1 = new String(tmp1);
    String string2 = new String(tmp2);
    RuleBasedCollator rb = (RuleBasedCollator) Collator.getInstance(ULocale.JAPANESE);
    rb.setStrength(Collator.QUATERNARY);
    rb.setAlternateHandlingShifted(false);
    result = rb.compare(string1, string2);
    key1 = rb.getCollationKey(string1);
    key2 = rb.getCollationKey(string2);
    if (result != 0 || !key1.equals(key2)) {
        errln("Failed Hiragana and Katakana common characters test. Expected results to be equal.");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) Test(org.junit.Test)

Aggregations

RuleBasedCollator (android.icu.text.RuleBasedCollator)140 Test (org.junit.Test)124 Collator (android.icu.text.Collator)42 ULocale (android.icu.util.ULocale)26 CollationElementIterator (android.icu.text.CollationElementIterator)25 Locale (java.util.Locale)22 CollationKey (android.icu.text.CollationKey)17 StringCharacterIterator (java.text.StringCharacterIterator)16 StringSearch (android.icu.text.StringSearch)14 RawCollationKey (android.icu.text.RawCollationKey)11 ParseException (java.text.ParseException)10 UnicodeSet (android.icu.text.UnicodeSet)8 AlphabeticIndex (android.icu.text.AlphabeticIndex)6 BreakIterator (android.icu.text.BreakIterator)6 MissingResourceException (java.util.MissingResourceException)5 IOException (java.io.IOException)4 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)3 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 ArrayList (java.util.ArrayList)2