Search in sources :

Example 36 with Collator

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

the class CollationRegressionTest method Test4058613.

// @bug 4058613
// 
// Collator::createInstance() causes an ArrayIndexOutofBoundsException for Korean
// 
@Test
public void Test4058613() /* char* par */
{
    // Creating a default collator doesn't work when Korean is the default
    // locale
    Locale oldDefault = Locale.getDefault();
    Locale.setDefault(new Locale("ko", ""));
    Collator c = null;
    c = Collator.getInstance(new Locale("en", "US"));
    if (c == null) {
        errln("Could not create a Korean collator");
        Locale.setDefault(oldDefault);
        return;
    }
    // ensure that's what we got
    if (c.getDecomposition() != Collator.NO_DECOMPOSITION) {
        errln("Decomposition is not set to NO_DECOMPOSITION for Korean collator");
    }
    Locale.setDefault(oldDefault);
}
Also used : Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 37 with Collator

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

the class CollationDummyTest method TestJB581.

@Test
public void TestJB581() {
    String source = "THISISATEST.";
    String target = "Thisisatest.";
    Collator coll = null;
    try {
        coll = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for : en_US\n");
        return;
    }
    int result = coll.compare(source, target);
    // result is 1, secondary differences only for ignorable space characters
    if (result != 1) {
        errln("Comparing two strings with only secondary differences in C failed.\n");
        return;
    }
    // To compare them with just primary differences
    coll.setStrength(Collator.PRIMARY);
    result = coll.compare(source, target);
    // result is 0
    if (result != 0) {
        errln("Comparing two strings with no differences in C failed.\n");
        return;
    }
    // Now, do the same comparison with keys
    CollationKey sourceKeyOut, targetKeyOut;
    sourceKeyOut = coll.getCollationKey(source);
    targetKeyOut = coll.getCollationKey(target);
    result = sourceKeyOut.compareTo(targetKeyOut);
    if (result != 0) {
        errln("Comparing two strings with sort keys in C failed.\n");
        return;
    }
}
Also used : CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 38 with Collator

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

the class CollationFrozenMonkeyTest method TestCompare.

// perform monkey tests using Collator.compare
@Test
public void TestCompare() {
    if (source.length() == 0) {
        errln("CollationMonkeyTest.TestCompare(): source is empty - ICU_DATA not set or data missing?");
        return;
    }
    Collator myPrimaryCollator;
    Collator mySecondaryCollator;
    Collator myTertiaryCollator;
    try {
        Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
        myCollator.freeze();
        myPrimaryCollator = myCollator.cloneAsThawed();
        myPrimaryCollator.setStrength(Collator.PRIMARY);
        myPrimaryCollator.freeze();
        mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
        mySecondaryCollator.setStrength(Collator.SECONDARY);
        mySecondaryCollator.freeze();
        myTertiaryCollator = mySecondaryCollator.cloneAsThawed();
        myTertiaryCollator.setStrength(Collator.TERTIARY);
        myTertiaryCollator.freeze();
    } catch (Exception e) {
        warnln("ERROR: in creation of collator of ENGLISH locale");
        return;
    }
    /* Seed the random-number generator with current time so that
         * the numbers will be different every time we run.
         */
    // use test framework's random seed
    Random rand = createRandom();
    int s = rand.nextInt(0x7fff) % source.length();
    int t = rand.nextInt(0x7fff) % source.length();
    int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
    String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
    // Tertiary
    int result = myTertiaryCollator.compare(subs, subt);
    // Tertiary
    int revResult = myTertiaryCollator.compare(subt, subs);
    report(subs, subt, result, revResult);
    // Secondary
    result = mySecondaryCollator.compare(subs, subt);
    // Secondary
    revResult = mySecondaryCollator.compare(subt, subs);
    report(subs, subt, result, revResult);
    // Primary
    result = myPrimaryCollator.compare(subs, subt);
    // Primary
    revResult = myPrimaryCollator.compare(subt, subs);
    report(subs, subt, result, revResult);
    String msg = "";
    String addOne = subs + String.valueOf(0xE000);
    result = myPrimaryCollator.compare(subs, addOne);
    if (result != -1) {
        msg += "Test : ";
        msg += subs;
        msg += " .LT. ";
        msg += addOne;
        msg += " Failed.";
        errln(msg);
    }
    msg = "";
    result = myPrimaryCollator.compare(addOne, subs);
    if (result != 1) {
        msg += "Test : ";
        msg += addOne;
        msg += " .GT. ";
        msg += subs;
        msg += " Failed.";
        errln(msg);
    }
}
Also used : Locale(java.util.Locale) Random(java.util.Random) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 39 with Collator

use of android.icu.text.Collator 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 40 with Collator

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

the class GlobalizationPreferencesTest method TestCollator.

@Test
public void TestCollator() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    // Set locale - tr
    logln("Set locale - tr");
    gp.setLocale(new ULocale("tr"));
    Collator coll = gp.getCollator();
    String locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
    if (!locStr.equals("tr")) {
        errln("FAIL: Collator locale is " + locStr + " Expected: tr");
    }
    // Unsupported collator locale - zun
    logln("Set locale - zun");
    gp.setLocale(new ULocale("zun"));
    coll = gp.getCollator();
    locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
    if (!locStr.equals("")) {
        errln("FAIL: Collator locale is \"" + locStr + "\" Expected: \"\"(empty)");
    }
    // Set locales - en_JP, fr, en_US, fr_FR
    logln("Set locale - en_JP, fr, en_US, fr_FR");
    ULocale[] locales = new ULocale[4];
    locales[0] = new ULocale("en_JP");
    locales[1] = new ULocale("fr");
    locales[2] = new ULocale("en_US");
    locales[3] = new ULocale("fr_FR");
    gp.setLocales(locales);
    coll = gp.getCollator();
    locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
    if (!locStr.equals("fr")) {
        errln("FAIL: Collator locale is " + locStr + " Expected: fr");
    }
    // Set explicit Collator
    Collator coll1 = Collator.getInstance(new ULocale("it"));
    coll1.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    logln("Set collator for it in canonical deconposition mode");
    gp.setCollator(coll1);
    coll1.setStrength(Collator.IDENTICAL);
    coll = gp.getCollator();
    locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
    if (!locStr.equals("it")) {
        errln("FAIL: Collator locale is " + locStr + " Expected: it");
    }
    if (coll1.equals(coll)) {
        errln("FAIL: setCollator must use a safe copy of a Collator");
    }
    // Freeze
    logln("Freeze this object");
    boolean isFrozen = false;
    gp.freeze();
    try {
        gp.setCollator(coll1);
    } catch (UnsupportedOperationException uoe) {
        logln("setCollator is blocked");
        isFrozen = true;
    }
    if (!isFrozen) {
        errln("FAIL: setCollator must be blocked after freeze");
    }
    // Modifiable clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
    coll = gp1.getCollator();
    locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
    if (!locStr.equals("it")) {
        errln("FAIL: Collator locale is " + locStr + " Expected: it");
    }
    if (coll.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
        errln("FAIL: Decomposition mode is not CANONICAL_DECOMPOSITION");
    }
    // Set custom collator again
    gp1.setCollator(coll1);
    coll = gp1.getCollator();
    if (coll.getStrength() != Collator.IDENTICAL) {
        errln("FAIL: Strength is not IDENTICAL");
    }
}
Also used : GlobalizationPreferences(android.icu.util.GlobalizationPreferences) ULocale(android.icu.util.ULocale) Collator(android.icu.text.Collator) 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