Search in sources :

Example 21 with RuleBasedCollator

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

the class CollationAPITest method TestDuplicate.

/**
 * This tests the duplication of a collator object.
 */
@Test
public void TestDuplicate() {
    // Clone does not be implemented
    Collator col1 = Collator.getInstance(Locale.ENGLISH);
    // Collator col2 = (Collator)col1.clone();
    // doAssert(col1.equals(col2), "Cloned object is not equal to the orginal");
    String ruleset = "&9 < a, A < b, B < c, C < d, D, e, E";
    RuleBasedCollator col3 = null;
    try {
        col3 = new RuleBasedCollator(ruleset);
    } catch (Exception e) {
        errln("Failure creating RuleBasedCollator with rule: \"" + ruleset + "\"\n" + e);
        return;
    }
    doAssert(!col1.equals(col3), "Cloned object is equal to some dummy");
    col3 = (RuleBasedCollator) col1;
    doAssert(col1.equals(col3), "Copied object is not equal to the orginal");
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 22 with RuleBasedCollator

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

the class CollationAPITest method TestMaxVariable.

@Test
public void TestMaxVariable() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
    try {
        coll.setMaxVariable(Collator.ReorderCodes.OTHERS);
        errln("setMaxVariable(others) did not detect illegal argument");
    } catch (IllegalArgumentException expected) {
    }
    coll.setMaxVariable(Collator.ReorderCodes.CURRENCY);
    if (Collator.ReorderCodes.CURRENCY != coll.getMaxVariable()) {
        errln("setMaxVariable(currency) != following getMaxVariable()");
    }
    coll.setAlternateHandlingShifted(true);
    // UCOL_EQUAL
    assertEquals("empty==dollar", 0, coll.compare("", "$"));
    // UCOL_EQUAL
    assertEquals("empty==euro", 0, coll.compare("", "\u20AC"));
    // UCOL_LESS
    assertEquals("dollar<zero", -1, coll.compare("$", "0"));
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 23 with RuleBasedCollator

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

the class CollationMiscTest method TestNumericCollation.

/**
 * Test for CollationElementIterator previous and next for the whole set of
 * unicode characters with normalization on.
 */
@Test
public void TestNumericCollation() {
    String[] basicTestStrings = { "hello1", "hello2", "hello123456" };
    String[] preZeroTestStrings = { "avery1", "avery01", "avery001", "avery0001" };
    String[] thirtyTwoBitNumericStrings = { "avery42949672960", "avery42949672961", "avery42949672962", "avery429496729610" };
    String[] supplementaryDigits = { // 0
    "\uD835\uDFCE", // 1
    "\uD835\uDFCF", // 2
    "\uD835\uDFD0", // 3
    "\uD835\uDFD1", // 10
    "\uD835\uDFCF\uD835\uDFCE", // 11
    "\uD835\uDFCF\uD835\uDFCF", // 12
    "\uD835\uDFCF\uD835\uDFD0", // 20
    "\uD835\uDFD0\uD835\uDFCE", // 21
    "\uD835\uDFD0\uD835\uDFCF", // 22
    "\uD835\uDFD0\uD835\uDFD0" };
    String[] foreignDigits = { "\u0661", "\u0662", "\u0663", "\u0661\u0660", "\u0661\u0662", "\u0661\u0663", "\u0662\u0660", "\u0662\u0662", "\u0662\u0663", "\u0663\u0660", "\u0663\u0662", "\u0663\u0663" };
    // Additional tests to cover bug reported in #9476
    String[] lastDigitDifferent = { "2004", "2005", "110005", "110006", "11005", "11006", "100000000005", "100000000006" };
    // Open our collator.
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.ENGLISH);
    String[] att = { "NumericCollation" };
    Boolean[] val = { Boolean.TRUE };
    genericLocaleStarterWithOptions(Locale.ENGLISH, basicTestStrings, att, val);
    genericLocaleStarterWithOptions(Locale.ENGLISH, thirtyTwoBitNumericStrings, att, val);
    genericLocaleStarterWithOptions(Locale.ENGLISH, foreignDigits, att, val);
    genericLocaleStarterWithOptions(Locale.ENGLISH, supplementaryDigits, att, val);
    // Setting up our collator to do digits.
    coll.setNumericCollation(true);
    // We expect that every element in our strings array will be equal.
    for (int i = 0; i < preZeroTestStrings.length - 1; i++) {
        for (int j = i + 1; j < preZeroTestStrings.length; j++) {
            CollationTest.doTest(this, coll, preZeroTestStrings[i], preZeroTestStrings[j], 0);
        }
    }
    // We expect comparisons between adjacent pairs will result in -1
    for (int i = 0; i < lastDigitDifferent.length - 1; i = i + 2) {
        CollationTest.doTest(this, coll, lastDigitDifferent[i], lastDigitDifferent[i + 1], -1);
    }
    // cover setNumericCollationDefault, getNumericCollation
    assertTrue("The Numeric Collation setting is on", coll.getNumericCollation());
    coll.setNumericCollationDefault();
    logln("After set Numeric to default, the setting is: " + coll.getNumericCollation());
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 24 with RuleBasedCollator

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

the class CollationMiscTest method TestIncompleteCnt.

@Test
public void TestIncompleteCnt() {
    String[] cnt1 = { "AA", "AC", "AZ", "AQ", "AB", "ABZ", "ABQ", "Z", "ABC", "Q", "B" };
    String[] cnt2 = { "DA", "DAD", "DAZ", "MAR", "Z", "DAVIS", "MARK", "DAV", "DAVI" };
    RuleBasedCollator coll = null;
    String temp = " & Z < ABC < Q < B";
    try {
        coll = new RuleBasedCollator(temp);
    } catch (Exception e) {
        warnln("fail to create RuleBasedCollator");
        return;
    }
    int size = cnt1.length;
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            String t1 = cnt1[i];
            String t2 = cnt1[j];
            CollationTest.doTest(this, coll, t1, t2, -1);
        }
    }
    temp = " & Z < DAVIS < MARK <DAV";
    try {
        coll = new RuleBasedCollator(temp);
    } catch (Exception e) {
        warnln("fail to create RuleBasedCollator");
        return;
    }
    size = cnt2.length;
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            String t1 = cnt2[i];
            String t2 = cnt2[j];
            CollationTest.doTest(this, coll, t1, t2, -1);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 25 with RuleBasedCollator

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

the class CollationMiscTest method TestVariableTop.

@Test
public void TestVariableTop() {
    // ICU 53+: The character must be in a supported reordering group,
    // and the variable top is pinned to the end of that group.
    // parseNextToken is not released as public so i create my own rules
    String rules = "& ' ' < b < c < de < fg & hi = j";
    try {
        RuleBasedCollator coll = new RuleBasedCollator(rules);
        String[] tokens = { " ", "b", "c", "de", "fg", "hi", "j", "ab" };
        coll.setAlternateHandlingShifted(true);
        for (int i = 0; i < tokens.length; i++) {
            int varTopOriginal = coll.getVariableTop();
            try {
                int varTop = coll.setVariableTop(tokens[i]);
                if (i > 4) {
                    errln("Token " + tokens[i] + " expected to fail");
                }
                if (varTop != coll.getVariableTop()) {
                    errln("Error setting and getting variable top");
                }
                CollationKey key1 = coll.getCollationKey(tokens[i]);
                for (int j = 0; j < i; j++) {
                    CollationKey key2 = coll.getCollationKey(tokens[j]);
                    if (key2.compareTo(key1) < 0) {
                        errln("Setting variable top shouldn't change the comparison sequence");
                    }
                    byte[] sortorder = key2.toByteArray();
                    if (sortorder.length > 0 && (key2.toByteArray())[0] > 1) {
                        errln("Primary sort order should be 0");
                    }
                }
            } catch (Exception e) {
                CollationElementIterator iter = coll.getCollationElementIterator(tokens[i]);
                /*int ce =*/
                iter.next();
                int ce2 = iter.next();
                if (ce2 == CollationElementIterator.NULLORDER) {
                    errln("Token " + tokens[i] + " not expected to fail");
                }
                if (coll.getVariableTop() != varTopOriginal) {
                    errln("When exception is thrown variable top should " + "not be changed");
                }
            }
            coll.setVariableTop(varTopOriginal);
            if (varTopOriginal != coll.getVariableTop()) {
                errln("Couldn't restore old variable top\n");
            }
        }
        // Testing calling with error set
        try {
            coll.setVariableTop("");
            errln("Empty string should throw an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            logln("PASS: Empty string failed as expected");
        }
        try {
            coll.setVariableTop(null);
            errln("Null string should throw an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            logln("PASS: null string failed as expected");
        }
    } catch (Exception e) {
        warnln("Error creating RuleBasedCollator");
    }
}
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

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