Search in sources :

Example 6 with Collator

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

the class SearchTest method assertCanonicalEqual.

boolean assertCanonicalEqual(SearchData search) {
    Collator collator = getCollator(search.collator);
    BreakIterator breaker = getBreakIterator(search.breaker);
    StringSearch strsrch;
    String text = search.text;
    String pattern = search.pattern;
    if (breaker != null) {
        breaker.setText(text);
    }
    collator.setStrength(search.strength);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
        strsrch.setElementComparisonType(search.cmpType);
        strsrch.setCanonical(true);
    } catch (Exception e) {
        errln("Error opening string search" + e.getMessage());
        return false;
    }
    if (!assertEqualWithStringSearch(strsrch, search)) {
        collator.setStrength(TERTIARY);
        collator.setDecomposition(Collator.NO_DECOMPOSITION);
        return false;
    }
    collator.setStrength(TERTIARY);
    collator.setDecomposition(Collator.NO_DECOMPOSITION);
    return true;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator)

Example 7 with Collator

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

the class SearchTest method assertEqual.

boolean assertEqual(SearchData search) {
    Collator collator = getCollator(search.collator);
    BreakIterator breaker = getBreakIterator(search.breaker);
    StringSearch strsrch;
    String text = search.text;
    String pattern = search.pattern;
    if (breaker != null) {
        breaker.setText(text);
    }
    collator.setStrength(search.strength);
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
        strsrch.setElementComparisonType(search.cmpType);
    } catch (Exception e) {
        errln("Error opening string search " + e.getMessage());
        return false;
    }
    if (!assertEqualWithStringSearch(strsrch, search)) {
        collator.setStrength(TERTIARY);
        return false;
    }
    collator.setStrength(TERTIARY);
    return true;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator)

Example 8 with Collator

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

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

the class CollationAPITest method TestProperty.

/**
 * This tests the properties of a collator object.
 * - constructor
 * - factory method getInstance
 * - compare and getCollationKey
 * - get/set decomposition mode and comparison level
 */
@Test
public void TestProperty() {
    /*
          All the collations have the same version in an ICU
          version.
          ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};
          ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};
          ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04};
        */
    logln("The property tests begin : ");
    logln("Test ctors : ");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    logln("Test getVersion");
    // Check for a version greater than some value rather than equality
    // so that we need not update the expected version each time.
    // from ICU 4.4/UCA 5.2
    VersionInfo expectedVersion = VersionInfo.getInstance(0x31, 0xC0, 0x00, 0x05);
    doAssert(col.getVersion().compareTo(expectedVersion) >= 0, "Expected minimum version " + expectedVersion.toString() + " got " + col.getVersion().toString());
    logln("Test getUCAVersion");
    // Assume that the UCD and UCA versions are the same,
    // rather than hardcoding (and updating each time) a particular UCA version.
    VersionInfo ucdVersion = UCharacter.getUnicodeVersion();
    VersionInfo ucaVersion = col.getUCAVersion();
    doAssert(ucaVersion.equals(ucdVersion), "Expected UCA version " + ucdVersion.toString() + " got " + col.getUCAVersion().toString());
    doAssert((col.compare("ab", "abc") < 0), "ab < abc comparison failed");
    doAssert((col.compare("ab", "AB") < 0), "ab < AB comparison failed");
    doAssert((col.compare("blackbird", "black-bird") > 0), "black-bird > blackbird comparison failed");
    doAssert((col.compare("black bird", "black-bird") < 0), "black bird > black-bird comparison failed");
    doAssert((col.compare("Hello", "hello") > 0), "Hello > hello comparison failed");
    logln("Test ctors ends.");
    logln("testing Collator.getStrength() method ...");
    doAssert((col.getStrength() == Collator.TERTIARY), "collation object has the wrong strength");
    doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");
    logln("testing Collator.setStrength() method ...");
    col.setStrength(Collator.SECONDARY);
    doAssert((col.getStrength() != Collator.TERTIARY), "collation object's strength is secondary difference");
    doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");
    doAssert((col.getStrength() == Collator.SECONDARY), "collation object has the wrong strength");
    logln("testing Collator.setDecomposition() method ...");
    col.setDecomposition(Collator.NO_DECOMPOSITION);
    doAssert((col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION), "Decomposition mode != Collator.CANONICAL_DECOMPOSITION");
    doAssert((col.getDecomposition() == Collator.NO_DECOMPOSITION), "Decomposition mode = Collator.NO_DECOMPOSITION");
    try {
        col = Collator.getInstance(Locale.FRENCH);
    } catch (Exception e) {
        errln("Creating French collation failed.");
        return;
    }
    col.setStrength(Collator.PRIMARY);
    logln("testing Collator.getStrength() method again ...");
    doAssert((col.getStrength() != Collator.TERTIARY), "collation object has the wrong strength");
    doAssert((col.getStrength() == Collator.PRIMARY), "collation object's strength is not primary difference");
    logln("testing French Collator.setStrength() method ...");
    col.setStrength(Collator.TERTIARY);
    doAssert((col.getStrength() == Collator.TERTIARY), "collation object's strength is not tertiary difference");
    doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");
    doAssert((col.getStrength() != Collator.SECONDARY), "collation object's strength is secondary difference");
}
Also used : VersionInfo(android.icu.util.VersionInfo) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 10 with Collator

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

the class CollationAPITest method TestHashCode.

/**
 * This tests the hashCode method of a collator object.
 */
@Test
public void TestHashCode() {
    logln("hashCode tests begin.");
    Collator col1 = Collator.getInstance(Locale.ENGLISH);
    Collator col2 = null;
    Locale dk = new Locale("da", "DK", "");
    try {
        col2 = Collator.getInstance(dk);
    } catch (Exception e) {
        errln("Danish collation creation failed.");
        return;
    }
    Collator col3 = null;
    try {
        col3 = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("2nd default collation creation failed.");
        return;
    }
    logln("Collator.hashCode() testing ...");
    doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect");
    doAssert(!(col1.hashCode() == col2.hashCode()), "Hash test2 result incorrect");
    doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal");
    logln("hashCode tests end.");
    String test1 = "Abcda";
    String test2 = "abcda";
    CollationKey sortk1, sortk2, sortk3;
    sortk1 = col3.getCollationKey(test1);
    sortk2 = col3.getCollationKey(test2);
    sortk3 = col3.getCollationKey(test2);
    doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
    doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal");
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) MissingResourceException(java.util.MissingResourceException) 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