Search in sources :

Example 56 with Collator

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

the class CollationAPITest method TestCompare.

/**
 * This tests the comparison convenience methods of a collator object.
 * - greater than
 * - greater than or equal to
 * - equal to
 */
@Test
public void TestCompare() {
    logln("The compare tests begin : ");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String test1 = "Abcda";
    String test2 = "abcda";
    logln("Use tertiary comparison level testing ....");
    doAssert((!col.equals(test1, test2)), "Result should be \"Abcda\" != \"abcda\"");
    doAssert((col.compare(test1, test2) > 0), "Result should be \"Abcda\" >>> \"abcda\"");
    col.setStrength(Collator.SECONDARY);
    logln("Use secondary comparison level testing ....");
    doAssert((col.equals(test1, test2)), "Result should be \"Abcda\" == \"abcda\"");
    doAssert((col.compare(test1, test2) == 0), "Result should be \"Abcda\" == \"abcda\"");
    col.setStrength(Collator.PRIMARY);
    logln("Use primary comparison level testing ....");
    doAssert((col.equals(test1, test2)), "Result should be \"Abcda\" == \"abcda\"");
    doAssert((col.compare(test1, test2) == 0), "Result should be \"Abcda\" == \"abcda\"");
    logln("The compare tests end.");
}
Also used : Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 57 with Collator

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

the class CollationAPITest method TestDecomposition.

/**
 * Tests decomposition setting
 */
@Test
public void TestDecomposition() {
    Collator en_US = null, el_GR = null, vi_VN = null;
    en_US = Collator.getInstance(new Locale("en", "US"));
    el_GR = Collator.getInstance(new Locale("el", "GR"));
    vi_VN = Collator.getInstance(new Locale("vi", "VN"));
    // there is no reason to have canonical decomposition in en_US OR default locale */
    if (vi_VN.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
        errln("vi_VN collation did not have cannonical decomposition for normalization!");
    }
    if (el_GR.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
        errln("el_GR collation did not have cannonical decomposition for normalization!");
    }
    if (en_US.getDecomposition() != Collator.NO_DECOMPOSITION) {
        errln("en_US collation had cannonical decomposition for normalization!");
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 58 with Collator

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

the class CollationAPITest method TestJunkCollator.

@Test
public void TestJunkCollator() {
    logln("Create junk collation: ");
    Locale abcd = new Locale("ab", "CD", "");
    Collator junk = Collator.getInstance(abcd);
    Collator col = Collator.getInstance();
    String colrules = ((RuleBasedCollator) col).getRules();
    String junkrules = ((RuleBasedCollator) junk).getRules();
    doAssert(colrules == junkrules || colrules.equals(junkrules), "The default collation should be returned.");
    Collator frCol = null;
    try {
        frCol = Collator.getInstance(Locale.CANADA_FRENCH);
    } catch (Exception e) {
        errln("Creating fr_CA collator failed.");
        return;
    }
    doAssert(!(frCol.equals(junk)), "The junk is the same as the fr_CA collator.");
    logln("Collator property test ended.");
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 59 with Collator

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

the class CollationDummyTest method TestJB1401.

@Test
public void TestJB1401() {
    Collator myCollator = null;
    char[] NFD_UnsafeStartChars = { // Tibetan Vowel Sign II
    0x0f73, // Tibetan Vowel Sign UU
    0x0f75, // Tibetan Vowel Sign Reversed II
    0x0f81, 0 };
    int i;
    try {
        myCollator = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for ENGLISH");
        return;
    }
    myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    for (i = 0; ; i++) {
        // Get the next funny character to be tested, and set up the
        // three test strings X, Y, Z, consisting of an A-grave + test char,
        // in original form, NFD, and then NFC form.
        char c = NFD_UnsafeStartChars[i];
        if (c == 0) {
            break;
        }
        // \u00C0 is A Grave
        String x = "\u00C0" + c;
        String y;
        String z;
        try {
            y = Normalizer.decompose(x, false);
            z = Normalizer.decompose(y, true);
        } catch (Exception e) {
            errln("ERROR: Failed to normalize test of character" + c);
            return;
        }
        // Collation test.  All three strings should be equal.
        // doTest does both strcoll and sort keys, with params in both orders.
        doTest(myCollator, x, y, 0);
        doTest(myCollator, x, z, 0);
        doTest(myCollator, y, z, 0);
        // Run collation element iterators over the three strings.  Results should be same for each.
        {
            CollationElementIterator ceiX, ceiY, ceiZ;
            int ceX, ceY, ceZ;
            int j;
            try {
                ceiX = ((RuleBasedCollator) myCollator).getCollationElementIterator(x);
                ceiY = ((RuleBasedCollator) myCollator).getCollationElementIterator(y);
                ceiZ = ((RuleBasedCollator) myCollator).getCollationElementIterator(z);
            } catch (Exception e) {
                errln("ERROR: getCollationElementIterator failed");
                return;
            }
            for (j = 0; ; j++) {
                try {
                    ceX = ceiX.next();
                    ceY = ceiY.next();
                    ceZ = ceiZ.next();
                } catch (Exception e) {
                    errln("ERROR: CollationElementIterator.next failed for iteration " + j);
                    break;
                }
                if (ceX != ceY || ceY != ceZ) {
                    errln("ERROR: ucol_next failed for iteration " + j);
                    break;
                }
                if (ceX == CollationElementIterator.NULLORDER) {
                    break;
                }
            }
        }
    }
}
Also used : 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 60 with Collator

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

the class CollationDummyTest method TestSurrogates.

// TestSurrogates() is ported from cintltst/callcoll.c
/**
 * Tests surrogate support.
 */
@Test
public void TestSurrogates() {
    String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";
    String[] source = { "z", "\uD800\uDC00", "\ud800\udc0a\u0308", "\ud800\udc02" };
    String[] target = { "\uD800\uDC00", "\ud800\udc0a\u0308", "A", "\ud800\udc03" };
    // this test is to verify the supplementary sort key order in the english
    // collator
    Collator enCollation;
    try {
        enCollation = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for ENGLISH");
        return;
    }
    myCollation.setStrength(Collator.TERTIARY);
    int count = 0;
    // logln("start of english collation supplementary characters test\n");
    while (count < 2) {
        doTest(enCollation, source[count], target[count], -1);
        count++;
    }
    doTest(enCollation, source[count], target[count], 1);
    // logln("start of tailored collation supplementary characters test\n");
    count = 0;
    Collator newCollation;
    try {
        newCollation = new RuleBasedCollator(rules);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for rules");
        return;
    }
    // tests getting collation elements for surrogates for tailored rules
    while (count < 4) {
        doTest(newCollation, source[count], target[count], -1);
        count++;
    }
    // tests that \uD801\uDC01 still has the same value, not changed
    CollationKey enKey = enCollation.getCollationKey(source[3]);
    CollationKey newKey = newCollation.getCollationKey(source[3]);
    int keyResult = enKey.compareTo(newKey);
    if (keyResult != 0) {
        errln("Failed : non-tailored supplementary characters should have the same value\n");
    }
}
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)

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