Search in sources :

Example 16 with CollationKey

use of android.icu.text.CollationKey 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 17 with CollationKey

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

the class CollationMiscTest method TestBocsuCoverage.

@Test
public void TestBocsuCoverage() {
    String test = "\u0041\u0441\u4441\\U00044441\u4441\u0441\u0041";
    Collator coll = Collator.getInstance();
    coll.setStrength(Collator.IDENTICAL);
    CollationKey key = coll.getCollationKey(test);
    logln("source:" + key.getSourceString());
}
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 18 with CollationKey

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

the class CollationRegressionTest method Test4139572.

// @bug 4139572
// 
// getCollationKey throws exception for spanish text
// Cannot reproduce this bug on 1.2, however it DOES fail on 1.1.6
// 
@Test
public void Test4139572() /* char* par */
{
    // 
    // Code pasted straight from the bug report
    // (and then translated to C++ ;-)
    // 
    // create spanish locale and collator
    Locale l = new Locale("es", "es");
    Collator col = null;
    try {
        col = Collator.getInstance(l);
    } catch (Exception e) {
        errln("Failed to create a collator for es_es locale.");
        return;
    }
    CollationKey key = null;
    // this spanish phrase kills it!
    try {
        key = col.getCollationKey("Nombre De Objeto");
        logln("source:" + key.getSourceString());
    } catch (Exception e) {
        errln("Error creating CollationKey for \"Nombre De Ojbeto\"");
    }
}
Also used : Locale(java.util.Locale) CollationKey(android.icu.text.CollationKey) ParseException(java.text.ParseException) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 19 with CollationKey

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

the class CollationRegressionTest method Test8484.

// Fixing the infinite loop for surrogates
@Test
public void Test8484() {
    String s = "\u9FE1\uCEF3\u2798\uAAB6\uDA7C";
    Collator coll = Collator.getInstance();
    CollationKey collKey = coll.getCollationKey(s);
    logln("Pass: " + collKey.toString() + " generated OK.");
}
Also used : CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 20 with CollationKey

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

the class CollationRegressionTest method compareArray.

void compareArray(Collator c, String[] tests) {
    int expectedResult = 0;
    for (int i = 0; i < tests.length; i += 3) {
        String source = tests[i];
        String comparison = tests[i + 1];
        String target = tests[i + 2];
        if (comparison.equals("<")) {
            expectedResult = -1;
        } else if (comparison.equals(">")) {
            expectedResult = 1;
        } else if (comparison.equals("=")) {
            expectedResult = 0;
        } else {
            errln("Bogus comparison string \"" + comparison + "\"");
        }
        int compareResult = 0;
        logln("i = " + i);
        logln(source);
        logln(target);
        try {
            compareResult = c.compare(source, target);
        } catch (Exception e) {
            errln(e.toString());
        }
        CollationKey sourceKey = null, targetKey = null;
        try {
            sourceKey = c.getCollationKey(source);
        } catch (Exception e) {
            errln("Couldn't get collationKey for source");
            continue;
        }
        try {
            targetKey = c.getCollationKey(target);
        } catch (Exception e) {
            errln("Couldn't get collationKey for target");
            continue;
        }
        int keyResult = sourceKey.compareTo(targetKey);
        reportCResult(source, target, sourceKey, targetKey, compareResult, keyResult, compareResult, expectedResult);
    }
}
Also used : CollationKey(android.icu.text.CollationKey) ParseException(java.text.ParseException)

Aggregations

CollationKey (android.icu.text.CollationKey)47 RuleBasedCollator (android.icu.text.RuleBasedCollator)28 Test (org.junit.Test)27 RawCollationKey (android.icu.text.RawCollationKey)19 Collator (android.icu.text.Collator)15 Locale (java.util.Locale)8 ULocale (android.icu.util.ULocale)7 UnicodeSet (android.icu.text.UnicodeSet)3 ParseException (java.text.ParseException)3 Random (java.util.Random)3 CollationElementIterator (android.icu.text.CollationElementIterator)2 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)2 MissingResourceException (java.util.MissingResourceException)2 Output (android.icu.util.Output)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1