Search in sources :

Example 51 with Collator

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

the class ULocaleCollationTest method TestNameList.

@Test
public void TestNameList() {
    String[][][] tests = { /* name in French, name in self, minimized, modified */
    { { "fr-Cyrl-BE", "fr-Cyrl-CA" }, { "Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE" }, { "Français (cyrillique, Canada)", "Français (cyrillique, Canada)", "fr_Cyrl_CA", "fr_Cyrl_CA" } }, { { "en", "de", "fr", "zh" }, { "Allemand", "Deutsch", "de", "de" }, { "Anglais", "English", "en", "en" }, { "Chinois", "中文", "zh", "zh" }, { "Français", "Français", "fr", "fr" } }, // some non-canonical names
    { { "iw", "iw-US", "no", "no-Cyrl", "in", "in-YU" }, { "Hébreu (États-Unis)", "עברית (ארצות הברית)", "iw_US", "iw_US" }, { "Hébreu (Israël)", "עברית (ישראל)", "iw", "iw_IL" }, { "Indonésien (Indonésie)", "Indonesia (Indonesia)", "in", "in_ID" }, { "Indonésien (Serbie)", "Indonesia (Serbia)", "in_YU", "in_YU" }, { "Norvégien (cyrillique)", "Norsk (kyrillisk)", "no_Cyrl", "no_Cyrl" }, { "Norvégien (latin)", "Norsk (latinsk)", "no", "no_Latn" } }, { { "zh-Hant-TW", "en", "en-gb", "fr", "zh-Hant", "de", "de-CH", "zh-TW" }, { "Allemand (Allemagne)", "Deutsch (Deutschland)", "de", "de_DE" }, { "Allemand (Suisse)", "Deutsch (Schweiz)", "de_CH", "de_CH" }, { "Anglais (États-Unis)", "English (United States)", "en", "en_US" }, { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" }, { "Chinois (traditionnel)", "中文(繁體)", "zh_Hant", "zh_Hant" }, { "Français", "Français", "fr", "fr" } }, { { "zh", "en-gb", "en-CA", "fr-Latn-FR" }, { "Anglais (Canada)", "English (Canada)", "en_CA", "en_CA" }, { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" }, { "Chinois", "中文", "zh", "zh" }, { "Français", "Français", "fr", "fr" } }, { { "en-gb", "fr", "zh-Hant", "zh-SG", "sr", "sr-Latn" }, { "Anglais (Royaume-Uni)", "English (United Kingdom)", "en_GB", "en_GB" }, { "Chinois (simplifié, Singapour)", "中文(简体,新加坡)", "zh_SG", "zh_Hans_SG" }, { "Chinois (traditionnel, Taïwan)", "中文(繁體,台灣)", "zh_Hant", "zh_Hant_TW" }, { "Français", "Français", "fr", "fr" }, { "Serbe (cyrillique)", "Српски (ћирилица)", "sr", "sr_Cyrl" }, { "Serbe (latin)", "Srpski (latinica)", "sr_Latn", "sr_Latn" } }, { { "fr-Cyrl", "fr-Arab" }, { "Français (arabe)", "Français (arabe)", "fr_Arab", "fr_Arab" }, { "Français (cyrillique)", "Français (cyrillique)", "fr_Cyrl", "fr_Cyrl" } }, { { "fr-Cyrl-BE", "fr-Arab-CA" }, { "Français (arabe, Canada)", "Français (arabe, Canada)", "fr_Arab_CA", "fr_Arab_CA" }, { "Français (cyrillique, Belgique)", "Français (cyrillique, Belgique)", "fr_Cyrl_BE", "fr_Cyrl_BE" } } };
    ULocale french = ULocale.FRENCH;
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    for (Type type : DisplayContext.Type.values()) {
        logln("Contexts: " + names.getContext(type).toString());
    }
    Collator collator = Collator.getInstance(french);
    for (String[][] test : tests) {
        Set<ULocale> list = new LinkedHashSet<ULocale>();
        List<UiListItem> expected = new ArrayList<UiListItem>();
        for (String item : test[0]) {
            list.add(new ULocale(item));
        }
        for (int i = 1; i < test.length; ++i) {
            String[] rawRow = test[i];
            expected.add(new UiListItem(new ULocale(rawRow[2]), new ULocale(rawRow[3]), rawRow[0], rawRow[1]));
        }
        List<UiListItem> newList = names.getUiList(list, false, collator);
        if (!expected.equals(newList)) {
            if (expected.size() != newList.size()) {
                errln(list.toString() + ": wrong size" + expected + ", " + newList);
            } else {
                errln(list.toString());
                for (int i = 0; i < expected.size(); ++i) {
                    assertEquals(i + "", expected.get(i), newList.get(i));
                }
            }
        } else {
            assertEquals(list.toString(), expected, newList);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ULocale(android.icu.util.ULocale) ArrayList(java.util.ArrayList) Collator(android.icu.text.Collator) Type(android.icu.text.DisplayContext.Type) UiListItem(android.icu.text.LocaleDisplayNames.UiListItem) LocaleDisplayNames(android.icu.text.LocaleDisplayNames) Test(org.junit.Test)

Example 52 with Collator

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

the class CollationAPITest method TestBounds.

@Test
public void TestBounds() {
    Collator coll = Collator.getInstance(new Locale("sh", ""));
    String[] test = { "John Smith", "JOHN SMITH", "john SMITH", "j\u00F6hn sm\u00EFth", "J\u00F6hn Sm\u00EFth", "J\u00D6HN SM\u00CFTH", "john smithsonian", "John Smithsonian" };
    String[] testStr = { "\u010CAKI MIHALJ", "\u010CAKI MIHALJ", "\u010CAKI PIRO\u0160KA", "\u010CABAI ANDRIJA", "\u010CABAI LAJO\u0160", "\u010CABAI MARIJA", "\u010CABAI STEVAN", "\u010CABAI STEVAN", "\u010CABARKAPA BRANKO", "\u010CABARKAPA MILENKO", "\u010CABARKAPA MIROSLAV", "\u010CABARKAPA SIMO", "\u010CABARKAPA STANKO", "\u010CABARKAPA TAMARA", "\u010CABARKAPA TOMA\u0160", "\u010CABDARI\u0106 NIKOLA", "\u010CABDARI\u0106 ZORICA", "\u010CABI NANDOR", "\u010CABOVI\u0106 MILAN", "\u010CABRADI AGNEZIJA", "\u010CABRADI IVAN", "\u010CABRADI JELENA", "\u010CABRADI LJUBICA", "\u010CABRADI STEVAN", "\u010CABRDA MARTIN", "\u010CABRILO BOGDAN", "\u010CABRILO BRANISLAV", "\u010CABRILO LAZAR", "\u010CABRILO LJUBICA", "\u010CABRILO SPASOJA", "\u010CADE\u0160 ZDENKA", "\u010CADESKI BLAGOJE", "\u010CADOVSKI VLADIMIR", "\u010CAGLJEVI\u0106 TOMA", "\u010CAGOROVI\u0106 VLADIMIR", "\u010CAJA VANKA", "\u010CAJI\u0106 BOGOLJUB", "\u010CAJI\u0106 BORISLAV", "\u010CAJI\u0106 RADOSLAV", "\u010CAK\u0160IRAN MILADIN", "\u010CAKAN EUGEN", "\u010CAKAN EVGENIJE", "\u010CAKAN IVAN", "\u010CAKAN JULIJAN", "\u010CAKAN MIHAJLO", "\u010CAKAN STEVAN", "\u010CAKAN VLADIMIR", "\u010CAKAN VLADIMIR", "\u010CAKAN VLADIMIR", "\u010CAKARA ANA", "\u010CAKAREVI\u0106 MOMIR", "\u010CAKAREVI\u0106 NEDELJKO", "\u010CAKI \u0160ANDOR", "\u010CAKI AMALIJA", "\u010CAKI ANDRA\u0160", "\u010CAKI LADISLAV", "\u010CAKI LAJO\u0160", "\u010CAKI LASLO" };
    CollationKey[] testKey = new CollationKey[testStr.length];
    for (int i = 0; i < testStr.length; i++) {
        testKey[i] = coll.getCollationKey(testStr[i]);
    }
    Arrays.sort(testKey);
    for (int i = 0; i < testKey.length - 1; i++) {
        CollationKey lower = testKey[i].getBound(CollationKey.BoundMode.LOWER, Collator.SECONDARY);
        for (int j = i + 1; j < testKey.length; j++) {
            CollationKey upper = testKey[j].getBound(CollationKey.BoundMode.UPPER, Collator.SECONDARY);
            for (int k = i; k <= j; k++) {
                if (lower.compareTo(testKey[k]) > 0) {
                    errln("Problem with lower bound at i = " + i + " j = " + j + " k = " + k);
                }
                if (upper.compareTo(testKey[k]) <= 0) {
                    errln("Problem with upper bound at i = " + i + " j = " + j + " k = " + k);
                }
            }
        }
    }
    for (int i = 0; i < test.length; i++) {
        CollationKey key = coll.getCollationKey(test[i]);
        CollationKey lower = key.getBound(CollationKey.BoundMode.LOWER, Collator.SECONDARY);
        CollationKey upper = key.getBound(CollationKey.BoundMode.UPPER_LONG, Collator.SECONDARY);
        for (int j = i + 1; j < test.length; j++) {
            key = coll.getCollationKey(test[j]);
            if (lower.compareTo(key) > 0) {
                errln("Problem with lower bound i = " + i + " j = " + j);
            }
            if (upper.compareTo(key) <= 0) {
                errln("Problem with upper bound i = " + i + " j = " + j);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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 53 with Collator

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

the class CollationAPITest method TestSetStrength.

/*
     * Tests the method public void setStrength(int newStrength)
     */
@Test
public void TestSetStrength() {
    // Tests when if ((newStrength != PRIMARY) && ... ) is true
    int[] cases = { -1, 4, 5 };
    for (int i = 0; i < cases.length; i++) {
        try {
            // Assuming -1 is not one of the values
            Collator c = Collator.getInstance();
            c.setStrength(cases[i]);
            errln("Collator.setStrength(int) is suppose to return " + "an exception for an invalid newStrength value of " + cases[i]);
        } catch (Exception e) {
        }
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 54 with Collator

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

the class CollationAPITest method TestCollationKey.

/**
 * This tests the collation key related APIs.
 * - constructor/destructor
 * - Collator.getCollationKey
 * - == and != operators
 * - comparison between collation keys
 * - creating collation key with a byte array and vice versa
 */
@Test
public void TestCollationKey() {
    logln("testing CollationKey begins...");
    Collator col = Collator.getInstance();
    col.setStrength(Collator.TERTIARY);
    String test1 = "Abcda";
    String test2 = "abcda";
    logln("Testing weird arguments");
    CollationKey sortk1 = col.getCollationKey("");
    // key gets reset here
    byte[] bytes = sortk1.toByteArray();
    doAssert(bytes.length == 3 && bytes[0] == 1 && bytes[1] == 1 && bytes[2] == 0, "Empty string should return a collation key with empty levels");
    // Most control codes and CGJ are completely ignorable.
    // A string with only completely ignorables must compare equal to an empty string.
    CollationKey sortkIgnorable = col.getCollationKey("\u0001\u034f");
    doAssert(sortkIgnorable != null && sortkIgnorable.toByteArray().length == 3, "Completely ignorable string should return a collation key with empty levels");
    doAssert(sortkIgnorable.compareTo(sortk1) == 0, "Completely ignorable string should compare equal to empty string");
    // bogus key returned here
    sortk1 = col.getCollationKey(null);
    doAssert(sortk1 == null, "Error code should return bogus collation key");
    logln("Use tertiary comparison level testing ....");
    sortk1 = col.getCollationKey(test1);
    CollationKey sortk2 = col.getCollationKey(test2);
    doAssert((sortk1.compareTo(sortk2)) > 0, "Result should be \"Abcda\" >>> \"abcda\"");
    CollationKey sortkNew;
    sortkNew = sortk1;
    doAssert(!(sortk1.equals(sortk2)), "The sort keys should be different");
    doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");
    doAssert((sortk1.equals(sortkNew)), "The sort keys assignment failed");
    doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");
    // port from apicoll
    try {
        col = Collator.getInstance();
    } catch (Exception e) {
        errln("Collator.getInstance() failed");
    }
    if (col.getStrength() != Collator.TERTIARY) {
        errln("Default collation did not have tertiary strength");
    }
    // Need to use identical strength
    col.setStrength(Collator.IDENTICAL);
    CollationKey key1 = col.getCollationKey(test1);
    CollationKey key2 = col.getCollationKey(test2);
    CollationKey key3 = col.getCollationKey(test2);
    doAssert(key1.compareTo(key2) > 0, "Result should be \"Abcda\" > \"abcda\"");
    doAssert(key2.compareTo(key1) < 0, "Result should be \"abcda\" < \"Abcda\"");
    doAssert(key2.compareTo(key3) == 0, "Result should be \"abcda\" ==  \"abcda\"");
    byte[] key2identical = key2.toByteArray();
    logln("Use secondary comparision level testing ...");
    col.setStrength(Collator.SECONDARY);
    key1 = col.getCollationKey(test1);
    key2 = col.getCollationKey(test2);
    key3 = col.getCollationKey(test2);
    doAssert(key1.compareTo(key2) == 0, "Result should be \"Abcda\" == \"abcda\"");
    doAssert(key2.compareTo(key3) == 0, "Result should be \"abcda\" ==  \"abcda\"");
    byte[] tempkey = key2.toByteArray();
    byte[] subkey2compat = new byte[tempkey.length];
    System.arraycopy(key2identical, 0, subkey2compat, 0, tempkey.length);
    subkey2compat[subkey2compat.length - 1] = 0;
    doAssert(Arrays.equals(tempkey, subkey2compat), "Binary format for 'abcda' sortkey different for secondary strength!");
    logln("testing sortkey ends...");
}
Also used : 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)

Example 55 with Collator

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

the class CollationAPITest method TestGetKeywordValues.

/* Tests the method
     * public static final String[] getKeywordValues(String keyword)
     */
@SuppressWarnings("static-access")
@Test
public void TestGetKeywordValues() {
    // Tests when "if (!keyword.equals(KEYWORDS[0]))" is true
    String[] cases = { "", "dummy" };
    for (int i = 0; i < cases.length; i++) {
        try {
            Collator c = Collator.getInstance();
            @SuppressWarnings("unused") String[] s = c.getKeywordValues(cases[i]);
            errln("Collator.getKeywordValues(String) is suppose to return " + "an exception for an invalid keyword.");
        } catch (Exception e) {
        }
    }
}
Also used : 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