Search in sources :

Example 41 with Collator

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

the class LocaleAliasCollationTest method TestCollation.

@Test
public void TestCollation() {
    ULocale defLoc = ULocale.getDefault();
    ULocale.setDefault(_DEFAULT_LOCALE);
    for (int i = 0; i < _LOCALE_NUMBER; i++) {
        ULocale oldLoc = _LOCALES[i][0];
        ULocale newLoc = _LOCALES[i][1];
        if (availableMap.get(_LOCALES[i][1]) == null) {
            logln(_LOCALES[i][1] + " is not available. Skipping!");
            continue;
        }
        Collator c1 = Collator.getInstance(oldLoc);
        Collator c2 = Collator.getInstance(newLoc);
        if (!c1.equals(c2)) {
            errln("CollationTest: c1!=c2: newLoc= " + newLoc + " oldLoc= " + oldLoc);
        }
        logln("Collation old:" + oldLoc + "   new:" + newLoc);
    }
    ULocale.setDefault(defLoc);
}
Also used : ULocale(android.icu.util.ULocale) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 42 with Collator

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

the class ULocaleCollationTest method TestIllformedLocale.

@Test
public void TestIllformedLocale() {
    ULocale french = ULocale.FRENCH;
    Collator collator = Collator.getInstance(french);
    LocaleDisplayNames names = LocaleDisplayNames.getInstance(french, DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    for (String malformed : Arrays.asList("en-a", "$", "ü--a", "en--US")) {
        try {
            Set<ULocale> supported = Collections.singleton(new ULocale(malformed));
            names.getUiList(supported, false, collator);
            assertNull("Failed to detect bogus locale «" + malformed + "»", supported);
        } catch (IllformedLocaleException e) {
            logln("Successfully detected ill-formed locale «" + malformed + "»:" + e.getMessage());
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) IllformedLocaleException(android.icu.util.IllformedLocaleException) Collator(android.icu.text.Collator) LocaleDisplayNames(android.icu.text.LocaleDisplayNames) Test(org.junit.Test)

Example 43 with Collator

use of android.icu.text.Collator in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class RegionSearchPicker method createAdapterItem.

private List<RegionItem> createAdapterItem(Set<String> regionIds) {
    final Collator collator = Collator.getInstance(getLocale());
    final TreeSet<RegionItem> items = new TreeSet<>(new RegionInfoComparator(collator));
    final LocaleDisplayNames localeDisplayNames = LocaleDisplayNames.getInstance(getLocale());
    long i = 0;
    for (String regionId : regionIds) {
        String name = localeDisplayNames.regionDisplayName(regionId);
        items.add(new RegionItem(i++, regionId, name));
    }
    return new ArrayList<>(items);
}
Also used : TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Collator(android.icu.text.Collator) LocaleDisplayNames(android.icu.text.LocaleDisplayNames)

Example 44 with Collator

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

the class CollationAPITest method TestElemIter.

/**
 * This tests the CollationElementIterator related APIs.
 * - creation of a CollationElementIterator object
 * - == and != operators
 * - iterating forward
 * - reseting the iterator index
 * - requesting the order properties(primary, secondary or tertiary)
 */
@Test
public void TestElemIter() {
    // logln("testing sortkey begins...");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
    String testString2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
    // logln("Constructors and comparison testing....");
    CollationElementIterator iterator1 = ((RuleBasedCollator) col).getCollationElementIterator(testString1);
    CharacterIterator chariter = new StringCharacterIterator(testString1);
    // copy ctor
    CollationElementIterator iterator2 = ((RuleBasedCollator) col).getCollationElementIterator(chariter);
    UCharacterIterator uchariter = UCharacterIterator.getInstance(testString2);
    CollationElementIterator iterator3 = ((RuleBasedCollator) col).getCollationElementIterator(uchariter);
    int offset = 0;
    offset = iterator1.getOffset();
    if (offset != 0) {
        errln("Error in getOffset for collation element iterator");
        return;
    }
    iterator1.setOffset(6);
    iterator1.setOffset(0);
    int order1, order2, order3;
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // Code coverage for dummy "not designed" hashCode() which does "assert false".
    try {
        // We don't expect any particular value.
        iterator1.hashCode();
    } catch (AssertionError ignored) {
    // Expected to be thrown if assertions are enabled.
    }
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // invalid test wrong in UCA
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    iterator1.reset();
    iterator2.reset();
    iterator3.reset();
    order1 = iterator1.next();
    doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
    order2 = iterator2.next();
    // In ICU 52 and earlier we had iterator1.equals(iterator2)
    // but in ICU 53 this fails because the iterators differ (String vs. CharacterIterator).
    // doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
    doAssert(iterator1.getOffset() == iterator2.getOffset(), "The second iterator advance failed");
    doAssert((order1 == order2), "The order result should be the same");
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");
    doAssert((CollationElementIterator.secondaryOrder(order1) == CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");
    doAssert((CollationElementIterator.tertiaryOrder(order1) == CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");
    order1 = iterator1.next();
    order2 = iterator2.next();
    order3 = iterator3.next();
    doAssert((CollationElementIterator.primaryOrder(order1) == CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");
    doAssert((CollationElementIterator.tertiaryOrder(order1) != CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");
    order1 = iterator1.next();
    order3 = iterator3.next();
    // obsolete invalid test, removed
    // doAssert((CollationElementIterator.secondaryOrder(order1) !=
    // CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");
    doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
    doAssert(!(iterator2.equals(iterator3)), "The iterators should be different");
    logln("testing CollationElementIterator ends...");
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CharacterIterator(java.text.CharacterIterator) StringCharacterIterator(java.text.StringCharacterIterator) UCharacterIterator(android.icu.text.UCharacterIterator) CollationElementIterator(android.icu.text.CollationElementIterator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 45 with Collator

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

the class CollationMonkeyTest method TestRules.

@Test
public void TestRules() {
    String[] testSourceCases = { "\u0061\u0062\u007a", "\u0061\u0062\u007a" };
    String[] testTargetCases = { "\u0061\u0062\u00e4", "\u0061\u0062\u0061\u0308" };
    int i = 0;
    logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
    Collator col = Collator.getInstance(new Locale("en", "US"));
    String baseRules = ((RuleBasedCollator) col).getRules();
    String newRules = " & z < ";
    newRules = baseRules + newRules + String.valueOf(0x00e4);
    RuleBasedCollator myCollation = null;
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        warnln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
    logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
    newRules = "";
    newRules = baseRules + " & z < a" + String.valueOf(0x0308);
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        errln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
}
Also used : Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) 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