Search in sources :

Example 66 with Collator

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

the class CollationMiscTest method TestUnknownCollationKeyword.

// Test case for Ticket#9409
// Unknown collation type should be ignored, without printing stack trace
@Test
public void TestUnknownCollationKeyword() {
    Collator coll1 = Collator.getInstance(new ULocale("en_US@collation=bogus"));
    Collator coll2 = Collator.getInstance(new ULocale("en_US"));
    assertEquals("Unknown collation keyword 'bogus' should be ignored", coll1, coll2);
}
Also used : ULocale(android.icu.util.ULocale) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 67 with Collator

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

the class CollationMiscTest method TestReorderingAPIWithRuleCreatedCollator.

/*
     * Test reordering API.
     */
@Test
public void TestReorderingAPIWithRuleCreatedCollator() throws Exception {
    Collator myCollation;
    String rules = "[reorder Hani Grek]";
    int[] rulesReorderCodes = { UScript.HAN, UScript.GREEK };
    int[] reorderCodes = { UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION };
    int[] retrievedReorderCodes;
    /* build collator tertiary */
    myCollation = new RuleBasedCollator(rules);
    myCollation.setStrength(Collator.TERTIARY);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (!Arrays.equals(rulesReorderCodes, retrievedReorderCodes)) {
        errln("ERROR: retrieved reorder codes do not match set reorder codes.");
    }
    /* clear the reordering */
    myCollation.setReorderCodes(null);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (retrievedReorderCodes.length != 0) {
        errln("ERROR: retrieved reorder codes was not null.");
    }
    /* set the reorderding */
    myCollation.setReorderCodes(reorderCodes);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) {
        errln("ERROR: retrieved reorder codes do not match set reorder codes.");
    }
    /* reset the reordering */
    myCollation.setReorderCodes(ReorderCodes.DEFAULT);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (!Arrays.equals(rulesReorderCodes, retrievedReorderCodes)) {
        errln("ERROR: retrieved reorder codes do not match set reorder codes.");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 68 with Collator

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

the class CollationMiscTest method TestReorderingAPI.

/*
     * Test reordering API.
     */
@Test
public void TestReorderingAPI() throws Exception {
    Collator myCollation;
    int[] reorderCodes = { UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION };
    int[] duplicateReorderCodes = { UScript.HIRAGANA, UScript.GREEK, ReorderCodes.CURRENCY, UScript.KATAKANA };
    int[] reorderCodesStartingWithDefault = { ReorderCodes.DEFAULT, UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION };
    int[] retrievedReorderCodes;
    String greekString = "\u03b1";
    String punctuationString = "\u203e";
    /* build collator tertiary */
    myCollation = new RuleBasedCollator("");
    myCollation.setStrength(Collator.TERTIARY);
    /* set the reorderding */
    myCollation.setReorderCodes(reorderCodes);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) {
        errln("ERROR: retrieved reorder codes do not match set reorder codes.");
    }
    if (!(myCollation.compare(greekString, punctuationString) < 0)) {
        errln("ERROR: collation result should have been less.");
    }
    /* clear the reordering */
    myCollation.setReorderCodes(null);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (retrievedReorderCodes.length != 0) {
        errln("ERROR: retrieved reorder codes was not null.");
    }
    if (!(myCollation.compare(greekString, punctuationString) > 0)) {
        errln("ERROR: collation result should have been greater.");
    }
    // do it again with an empty but non-null array
    /* set the reorderding */
    myCollation.setReorderCodes(reorderCodes);
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) {
        errln("ERROR: retrieved reorder codes do not match set reorder codes.");
    }
    if (!(myCollation.compare(greekString, punctuationString) < 0)) {
        errln("ERROR: collation result should have been less.");
    }
    /* clear the reordering */
    myCollation.setReorderCodes(new int[] {});
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (retrievedReorderCodes.length != 0) {
        errln("ERROR: retrieved reorder codes was not null.");
    }
    if (!(myCollation.compare(greekString, punctuationString) > 0)) {
        errln("ERROR: collation result should have been greater.");
    }
    /* clear the reordering using [NONE] */
    myCollation.setReorderCodes(new int[] { ReorderCodes.NONE });
    retrievedReorderCodes = myCollation.getReorderCodes();
    if (retrievedReorderCodes.length != 0) {
        errln("ERROR: [NONE] retrieved reorder codes was not null.");
    }
    boolean gotException = false;
    /* set duplicates in the reorder codes */
    try {
        myCollation.setReorderCodes(duplicateReorderCodes);
    } catch (IllegalArgumentException e) {
        // expect exception on illegal arguments
        gotException = true;
    }
    if (!gotException) {
        errln("ERROR: exception was not thrown for illegal reorder codes argument.");
    }
    /* set duplicate reorder codes */
    gotException = false;
    try {
        myCollation.setReorderCodes(reorderCodesStartingWithDefault);
    } catch (IllegalArgumentException e) {
        gotException = true;
    }
    if (!gotException) {
        errln("ERROR: reorder codes following a 'default' code should have thrown an exception but did not.");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 69 with Collator

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

the class CollationRegressionTest method Test4095316.

// @bug 4095316
// 
@Test
public void Test4095316() /* char* par */
{
    Locale el_GR = new Locale("el", "GR");
    Collator c = null;
    try {
        c = Collator.getInstance(el_GR);
    } catch (Exception e) {
        errln("Failed to create collator for el_GR locale");
        return;
    }
    // These now have tertiary differences in UCA
    // c->setStrength(Collator::TERTIARY);
    // c->setAttribute(UCOL_STRENGTH, UCOL_SECONDARY, status);
    c.setStrength(Collator.SECONDARY);
    String[] tests = { "\u03D4", "\u003d", "\u03AB" };
    compareArray(c, tests);
}
Also used : Locale(java.util.Locale) ParseException(java.text.ParseException) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 70 with Collator

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

the class CollationMiscTest method TestNonLeadBytesDuringCollationReordering.

/*
     * Test that in a primary-compressed sort key all bytes except the first one are unchanged under script reordering.
     */
@Test
public void TestNonLeadBytesDuringCollationReordering() throws Exception {
    Collator myCollation;
    byte[] baseKey;
    byte[] reorderKey;
    int[] reorderCodes = { UScript.GREEK };
    String testString = "\u03b1\u03b2\u03b3";
    /* build collator tertiary */
    myCollation = new RuleBasedCollator("");
    myCollation.setStrength(Collator.TERTIARY);
    baseKey = myCollation.getCollationKey(testString).toByteArray();
    myCollation.setReorderCodes(reorderCodes);
    reorderKey = myCollation.getCollationKey(testString).toByteArray();
    if (baseKey.length != reorderKey.length) {
        errln("Key lengths not the same during reordering.\n");
    }
    for (int i = 1; i < baseKey.length; i++) {
        if (baseKey[i] != reorderKey[i]) {
            errln("Collation key bytes not the same at position " + i);
        }
    }
    /* build collator tertiary */
    myCollation = new RuleBasedCollator("");
    myCollation.setStrength(Collator.QUATERNARY);
    baseKey = myCollation.getCollationKey(testString).toByteArray();
    myCollation.setReorderCodes(reorderCodes);
    reorderKey = myCollation.getCollationKey(testString).toByteArray();
    if (baseKey.length != reorderKey.length) {
        errln("Key lengths not the same during reordering.\n");
    }
    for (int i = 1; i < baseKey.length; i++) {
        if (baseKey[i] != reorderKey[i]) {
            errln("Collation key bytes not the same at position " + i);
        }
    }
}
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