Search in sources :

Example 1 with Collator

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

the class CollationServiceTest method TestRegister.

@Test
public void TestRegister() {
    // register a singleton
    Collator frcol = Collator.getInstance(ULocale.FRANCE);
    Collator uscol = Collator.getInstance(ULocale.US);
    {
        // try override en_US collator
        Object key = Collator.registerInstance(frcol, ULocale.US);
        Collator ncol = Collator.getInstance(ULocale.US);
        if (!frcol.equals(ncol)) {
            errln("register of french collator for en_US failed");
        }
        // coverage
        // CollatorFactory.handleCreate
        Collator test = Collator.getInstance(ULocale.GERMANY);
        if (!test.getLocale(ULocale.VALID_LOCALE).equals(ULocale.GERMAN)) {
            errln("Collation from Germany is really " + test.getLocale(ULocale.VALID_LOCALE));
        }
        if (!Collator.unregister(key)) {
            errln("failed to unregister french collator");
        }
        ncol = Collator.getInstance(ULocale.US);
        if (!uscol.equals(ncol)) {
            errln("collator after unregister does not match original");
        }
    }
    ULocale fu_FU = new ULocale("fu_FU_FOO");
    {
        // try create collator for new locale
        Collator fucol = Collator.getInstance(fu_FU);
        Object key = Collator.registerInstance(frcol, fu_FU);
        Collator ncol = Collator.getInstance(fu_FU);
        if (!frcol.equals(ncol)) {
            errln("register of fr collator for fu_FU failed");
        }
        ULocale[] locales = Collator.getAvailableULocales();
        boolean found = false;
        for (int i = 0; i < locales.length; ++i) {
            if (locales[i].equals(fu_FU)) {
                found = true;
                break;
            }
        }
        if (!found) {
            errln("new locale fu_FU not reported as supported locale");
        }
        try {
            String name = Collator.getDisplayName(fu_FU);
            if (!"fu (FU, FOO)".equals(name) && !"fu_FU_FOO".equals(name)) /* no LocaleDisplayNamesImpl */
            {
                errln("found " + name + " for fu_FU");
            }
        } catch (MissingResourceException ex) {
            warnln("Could not load locale data.");
        }
        try {
            String name = Collator.getDisplayName(fu_FU, fu_FU);
            if (!"fu (FU, FOO)".equals(name) && !"fu_FU_FOO".equals(name)) /* no LocaleDisplayNamesImpl */
            {
                errln("found " + name + " for fu_FU");
            }
        } catch (MissingResourceException ex) {
            warnln("Could not load locale data.");
        }
        if (!Collator.unregister(key)) {
            errln("failed to unregister french collator");
        }
        ncol = Collator.getInstance(fu_FU);
        if (!fucol.equals(ncol)) {
            errln("collator after unregister does not match original fu_FU");
        }
    }
    {
        // coverage after return to default
        ULocale[] locales = Collator.getAvailableULocales();
        for (int i = 0; i < locales.length; ++i) {
            if (locales[i].equals(fu_FU)) {
                errln("new locale fu_FU not reported as supported locale");
                break;
            }
        }
        Collator ncol = Collator.getInstance(ULocale.US);
        if (!ncol.getLocale(ULocale.VALID_LOCALE).equals(ULocale.US)) {
            errln("Collation from US is really " + ncol.getLocale(ULocale.VALID_LOCALE));
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 2 with Collator

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

the class G7CollationTest method TestDemo2.

// perorm test with added rules "& C < ch , cH, Ch, CH"
@Test
public void TestDemo2() {
    logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String baseRules = ((RuleBasedCollator) col).getRules();
    String newRules = "& C < ch , cH, Ch, CH";
    newRules = baseRules + newRules;
    RuleBasedCollator myCollation = null;
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        errln("Fail to create RuleBasedCollator with rules:" + newRules);
        return;
    }
    int j, n;
    for (j = 0; j < TOTALTESTSET; j++) {
        for (n = j + 1; n < TOTALTESTSET; n++) {
            doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 3 with Collator

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

the class CollationThreadTest method testFrozen.

@org.junit.Test
public void testFrozen() {
    final Collator theCollator = Collator.getInstance(new Locale("pl", "", ""));
    theCollator.freeze();
    final Random r = new Random();
    Control control = new Control();
    Thread[] threads = new Thread[10];
    for (int i = 0; i < threads.length; ++i) {
        Test test = new Test("Frozen collation test thread " + i, threadTestData.clone(), theCollator, r, control);
        threads[i] = new Thread(test);
    }
    runThreads(threads, control);
}
Also used : Locale(java.util.Locale) Random(java.util.Random) Collator(android.icu.text.Collator)

Example 4 with Collator

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

the class CollationThreadTest method testThreads.

@org.junit.Test
public void testThreads() {
    final Collator theCollator = Collator.getInstance(new Locale("pl", "", ""));
    final Random r = new Random();
    final Control control = new Control();
    Thread[] threads = new Thread[10];
    for (int i = 0; i < threads.length; ++i) {
        Collator coll;
        try {
            coll = (Collator) theCollator.clone();
        } catch (CloneNotSupportedException e) {
            // should not happen, if it does we'll get an exception right away
            errln("could not clone");
            return;
        }
        Test test = new Test("Collation test thread" + i, threadTestData.clone(), coll, r, control);
        threads[i] = new Thread(test);
    }
    runThreads(threads, control);
}
Also used : Locale(java.util.Locale) Random(java.util.Random) Collator(android.icu.text.Collator)

Example 5 with Collator

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

the class SearchTest method TestConstructor.

@Test
public void TestConstructor() {
    String pattern = "pattern";
    String text = "text";
    StringCharacterIterator textiter = new StringCharacterIterator(text);
    Collator defaultcollator = Collator.getInstance();
    BreakIterator breaker = BreakIterator.getCharacterInstance();
    breaker.setText(text);
    StringSearch search = new StringSearch(pattern, text);
    if (!search.getPattern().equals(pattern) || !search.getTarget().equals(textiter) || !search.getCollator().equals(defaultcollator)) /*|| !search.getBreakIterator().equals(breaker)*/
    {
        errln("StringSearch(String, String) error");
    }
    search = new StringSearch(pattern, textiter, m_fr_fr_);
    if (!search.getPattern().equals(pattern) || !search.getTarget().equals(textiter) || !search.getCollator().equals(m_fr_fr_)) /*|| !search.getBreakIterator().equals(breaker)*/
    {
        errln("StringSearch(String, StringCharacterIterator, " + "RuleBasedCollator) error");
    }
    Locale de = new Locale("de", "DE");
    breaker = BreakIterator.getCharacterInstance(de);
    breaker.setText(text);
    search = new StringSearch(pattern, textiter, de);
    if (!search.getPattern().equals(pattern) || !search.getTarget().equals(textiter) || !search.getCollator().equals(Collator.getInstance(de))) /*|| !search.getBreakIterator().equals(breaker)*/
    {
        errln("StringSearch(String, StringCharacterIterator, Locale) " + "error");
    }
    search = new StringSearch(pattern, textiter, m_fr_fr_, m_en_wordbreaker_);
    if (!search.getPattern().equals(pattern) || !search.getTarget().equals(textiter) || !search.getCollator().equals(m_fr_fr_) || !search.getBreakIterator().equals(m_en_wordbreaker_)) {
        errln("StringSearch(String, StringCharacterIterator, Locale) " + "error");
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) StringCharacterIterator(java.text.StringCharacterIterator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator) 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