Search in sources :

Example 21 with Collator

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

the class CollationDummyTest method TestVariableTop.

// TestVariableTop() is ported from cintltst/callcoll.c
/**
 * Tests the [variable top] tag in rule syntax. Since the default [alternate]
 * tag has the value shifted, any codepoints before [variable top] should give
 * a primary ce of 0.
 */
@Test
public void TestVariableTop() {
    /*
         * Starting with ICU 53, setting the variable top via a pseudo relation string
         * is not supported any more.
         * It was replaced by the [maxVariable symbol] setting.
         * See ICU tickets #9958 and #8032.
         */
    if (!SUPPORT_VARIABLE_TOP_RELATION) {
        return;
    }
    String rule = "&z = [variable top]";
    Collator myColl;
    Collator enColl;
    char[] source = new char[1];
    char ch;
    int[] expected = { 0 };
    try {
        enColl = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for ENGLISH");
        return;
    }
    try {
        myColl = new RuleBasedCollator(rule);
    } catch (Exception e) {
        errln("Fail to create RuleBasedCollator with rules:" + rule);
        return;
    }
    enColl.setStrength(Collator.PRIMARY);
    myColl.setStrength(Collator.PRIMARY);
    ((RuleBasedCollator) enColl).setAlternateHandlingShifted(true);
    ((RuleBasedCollator) myColl).setAlternateHandlingShifted(true);
    if (((RuleBasedCollator) enColl).isAlternateHandlingShifted() != true) {
        errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");
    }
    // space is supposed to be a variable
    CollationKey key = enColl.getCollationKey(" ");
    byte[] result = key.toByteArray();
    for (int i = 0; i < result.length; i++) {
        if (result[i] != expected[i]) {
            errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
            break;
        }
    }
    ch = 'a';
    while (ch < 'z') {
        source[0] = ch;
        key = myColl.getCollationKey(new String(source));
        result = key.toByteArray();
        for (int i = 0; i < result.length; i++) {
            if (result[i] != expected[i]) {
                errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
                break;
            }
        }
        ch++;
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 22 with Collator

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

the class CollationMiscTest method TestCase.

@Test
public void TestCase() {
    String gRules = "\u0026\u0030\u003C\u0031\u002C\u2460\u003C\u0061\u002C\u0041";
    String[] testCase = { "1a", "1A", "\u2460a", "\u2460A" };
    int[][] caseTestResults = { { -1, -1, -1, 0, -1, -1, 0, 0, -1 }, { 1, -1, -1, 0, -1, -1, 0, 0, 1 }, { -1, -1, -1, 0, 1, -1, 0, 0, -1 }, { 1, -1, 1, 0, -1, -1, 0, 0, 1 } };
    boolean[][] caseTestAttributes = { { false, false }, { true, false }, { false, true }, { true, true } };
    int i, j, k;
    Collator myCollation;
    try {
        myCollation = Collator.getInstance(new Locale("en", "US"));
    } catch (Exception e) {
        warnln("ERROR: in creation of rule based collator ");
        return;
    }
    // logln("Testing different case settings");
    myCollation.setStrength(Collator.TERTIARY);
    for (k = 0; k < 4; k++) {
        if (caseTestAttributes[k][0] == true) {
            // upper case first
            ((RuleBasedCollator) myCollation).setUpperCaseFirst(true);
        } else {
            // upper case first
            ((RuleBasedCollator) myCollation).setLowerCaseFirst(true);
        }
        ((RuleBasedCollator) myCollation).setCaseLevel(caseTestAttributes[k][1]);
        // logln("Case first = " + caseTestAttributes[k][0] + ", Case level = " + caseTestAttributes[k][1]);
        for (i = 0; i < 3; i++) {
            for (j = i + 1; j < 4; j++) {
                CollationTest.doTest(this, (RuleBasedCollator) myCollation, testCase[i], testCase[j], caseTestResults[k][3 * i + j - 1]);
            }
        }
    }
    try {
        myCollation = new RuleBasedCollator(gRules);
    } catch (Exception e) {
        warnln("ERROR: in creation of rule based collator");
        return;
    }
    // logln("Testing different case settings with custom rules");
    myCollation.setStrength(Collator.TERTIARY);
    for (k = 0; k < 4; k++) {
        if (caseTestAttributes[k][0] == true) {
            ((RuleBasedCollator) myCollation).setUpperCaseFirst(true);
        } else {
            ((RuleBasedCollator) myCollation).setUpperCaseFirst(false);
        }
        ((RuleBasedCollator) myCollation).setCaseLevel(caseTestAttributes[k][1]);
        for (i = 0; i < 3; i++) {
            for (j = i + 1; j < 4; j++) {
                CollationTest.doTest(this, (RuleBasedCollator) myCollation, testCase[i], testCase[j], caseTestResults[k][3 * i + j - 1]);
            }
        }
    }
    {
        String[] lowerFirst = { "h", "H", "ch", "Ch", "CH", "cha", "chA", "Cha", "ChA", "CHa", "CHA", "i", "I" };
        String[] upperFirst = { "H", "h", "CH", "Ch", "ch", "CHA", "CHa", "ChA", "Cha", "chA", "cha", "I", "i" };
        // logln("mixed case test");
        // logln("lower first, case level off");
        genericRulesStarter("[caseFirst lower]&H<ch<<<Ch<<<CH", lowerFirst);
        // logln("upper first, case level off");
        genericRulesStarter("[caseFirst upper]&H<ch<<<Ch<<<CH", upperFirst);
        // logln("lower first, case level on");
        genericRulesStarter("[caseFirst lower][caseLevel on]&H<ch<<<Ch<<<CH", lowerFirst);
        // logln("upper first, case level on");
        genericRulesStarter("[caseFirst upper][caseLevel on]&H<ch<<<Ch<<<CH", upperFirst);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 23 with Collator

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

the class CollationMiscTest method TestExpansion.

@Test
public void TestExpansion() {
    String[] testrules = { /*
             * This seems to have tested that M was not mapped to an expansion.
             * I believe the old builder just did that because it computed the extension CEs
             * at the very end, which was a bug.
             * Among other problems, it violated the core tailoring principle
             * by making an earlier rule depend on a later one.
             * And, of course, if M did not get an expansion, then it was primary different from K,
             * unlike what the rule &K<<M says.
            "&J << K / B & K << M",
             */
    "&J << K / B << M" };
    String[] testdata = { "JA", "MA", "KA", "KC", "JC", "MC" };
    Collator coll;
    for (int i = 0; i < testrules.length; i++) {
        // logln("Rule " + testrules[i] + " for testing\n");
        String rule = testrules[i];
        try {
            coll = new RuleBasedCollator(rule);
        } catch (Exception e) {
            warnln("Collator creation failed " + testrules[i]);
            return;
        }
        for (int j = 0; j < 5; j++) {
            CollationTest.doTest(this, (RuleBasedCollator) coll, testdata[j], testdata[j + 1], -1);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 24 with Collator

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

the class CollationMiscTest method TestMergeSortKeys.

@Test
public void TestMergeSortKeys() {
    String[] cases = { "abc", "abcd", "abcde" };
    String prefix = "foo";
    String suffix = "egg";
    CollationKey[] mergedPrefixKeys = new CollationKey[cases.length];
    CollationKey[] mergedSuffixKeys = new CollationKey[cases.length];
    Collator coll = Collator.getInstance(Locale.ENGLISH);
    genericLocaleStarter(Locale.ENGLISH, cases);
    int strength = Collator.PRIMARY;
    while (strength <= Collator.IDENTICAL) {
        coll.setStrength(strength);
        CollationKey prefixKey = coll.getCollationKey(prefix);
        CollationKey suffixKey = coll.getCollationKey(suffix);
        for (int i = 0; i < cases.length; i++) {
            CollationKey key = coll.getCollationKey(cases[i]);
            mergedPrefixKeys[i] = prefixKey.merge(key);
            mergedSuffixKeys[i] = suffixKey.merge(key);
            if (mergedPrefixKeys[i].getSourceString() != null || mergedSuffixKeys[i].getSourceString() != null) {
                errln("Merged source string error: expected null");
            }
            if (i > 0) {
                if (mergedPrefixKeys[i - 1].compareTo(mergedPrefixKeys[i]) >= 0) {
                    errln("Error while comparing prefixed keys @ strength " + strength);
                    errln(CollationTest.prettify(mergedPrefixKeys[i - 1]));
                    errln(CollationTest.prettify(mergedPrefixKeys[i]));
                }
                if (mergedSuffixKeys[i - 1].compareTo(mergedSuffixKeys[i]) >= 0) {
                    errln("Error while comparing suffixed keys @ strength " + strength);
                    errln(CollationTest.prettify(mergedSuffixKeys[i - 1]));
                    errln(CollationTest.prettify(mergedSuffixKeys[i]));
                }
            }
        }
        if (strength == Collator.QUATERNARY) {
            strength = Collator.IDENTICAL;
        } else {
            strength++;
        }
    }
}
Also used : CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 25 with Collator

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

the class CollationMiscTest method TestJ3347.

@Test
public void TestJ3347() {
    try {
        Collator coll = Collator.getInstance(Locale.FRENCH);
        ((RuleBasedCollator) coll).setAlternateHandlingShifted(true);
        if (coll.compare("6", "!6") != 0) {
            errln("Jitterbug 3347 failed");
        }
    } catch (Exception e) {
        warnln("Error creating UCA collator");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Aggregations

Collator (android.icu.text.Collator)81 Test (org.junit.Test)72 RuleBasedCollator (android.icu.text.RuleBasedCollator)70 Locale (java.util.Locale)25 ULocale (android.icu.util.ULocale)22 CollationKey (android.icu.text.CollationKey)15 MissingResourceException (java.util.MissingResourceException)11 RawCollationKey (android.icu.text.RawCollationKey)7 ParseException (java.text.ParseException)6 Random (java.util.Random)6 BreakIterator (android.icu.text.BreakIterator)5 StringCharacterIterator (java.text.StringCharacterIterator)5 LocaleDisplayNames (android.icu.text.LocaleDisplayNames)4 StringSearch (android.icu.text.StringSearch)4 CollationElementIterator (android.icu.text.CollationElementIterator)3 ArrayList (java.util.ArrayList)3 UnicodeSet (android.icu.text.UnicodeSet)2 GlobalizationPreferences (android.icu.util.GlobalizationPreferences)2 HashSet (java.util.HashSet)2 TreeSet (java.util.TreeSet)2