Search in sources :

Example 91 with RuleBasedCollator

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

the class CollationAPITest method TestVariableTopSetting.

@Test
public void TestVariableTopSetting() {
    // Use the root collator, not the default collator.
    // This test fails with en_US_POSIX which tailors the dollar sign after 'A'.
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
    int oldVarTop = coll.getVariableTop();
    // and the variable top is pinned to the end of that group.
    try {
        coll.setVariableTop("A");
        errln("setVariableTop(letter) did not detect illegal argument");
    } catch (IllegalArgumentException expected) {
    }
    // dollar sign (currency symbol)
    int newVarTop = coll.setVariableTop("$");
    if (newVarTop != coll.getVariableTop()) {
        errln("setVariableTop(dollar sign) != following getVariableTop()");
    }
    String dollar = "$";
    String euro = "\u20AC";
    int newVarTop2 = coll.setVariableTop(euro);
    assertEquals("setVariableTop(Euro sign) == following getVariableTop()", newVarTop2, coll.getVariableTop());
    assertEquals("setVariableTop(Euro sign) == setVariableTop(dollar sign) (should pin to top of currency group)", newVarTop2, newVarTop);
    coll.setAlternateHandlingShifted(true);
    // UCOL_EQUAL
    assertEquals("empty==dollar", 0, coll.compare("", dollar));
    // UCOL_EQUAL
    assertEquals("empty==euro", 0, coll.compare("", euro));
    // UCOL_LESS
    assertEquals("dollar<zero", -1, coll.compare(dollar, "0"));
    coll.setVariableTop(oldVarTop);
    int newerVarTop = coll.setVariableTop("$");
    if (newVarTop != newerVarTop) {
        errln("Didn't set vartop properly from String!\n");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 92 with RuleBasedCollator

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

the class CollationAPITest method TestRuleBasedColl.

/**
 * This tests the RuleBasedCollator
 * - constructor/destructor
 * - getRules
 */
@Test
public void TestRuleBasedColl() {
    RuleBasedCollator col1 = null, col2 = null, col3 = null, col4 = null;
    String ruleset1 = "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E";
    String ruleset2 = "&9 < a, A < b, B < c, C < d, D, e, E";
    String ruleset3 = "&";
    try {
        col1 = new RuleBasedCollator(ruleset1);
    } catch (Exception e) {
        // only first error needs to be a warning since we exit function
        warnln("RuleBased Collator creation failed.");
        return;
    }
    try {
        col2 = new RuleBasedCollator(ruleset2);
    } catch (Exception e) {
        errln("RuleBased Collator creation failed.");
        return;
    }
    try {
        // empty rules fail
        col3 = new RuleBasedCollator(ruleset3);
        errln("Failure: Empty rules for the collator should fail");
        return;
    } catch (MissingResourceException e) {
        warnln(e.getMessage());
    } catch (Exception e) {
        logln("PASS: Empty rules for the collator failed as expected");
    }
    Locale locale = new Locale("aa", "AA");
    try {
        col3 = (RuleBasedCollator) Collator.getInstance(locale);
    } catch (Exception e) {
        errln("Fallback Collator creation failed.: %s");
        return;
    }
    try {
        col3 = (RuleBasedCollator) Collator.getInstance();
    } catch (Exception e) {
        errln("Default Collator creation failed.: %s");
        return;
    }
    String rule1 = col1.getRules();
    String rule2 = col2.getRules();
    String rule3 = col3.getRules();
    doAssert(!rule1.equals(rule2), "Default collator getRules failed");
    doAssert(!rule2.equals(rule3), "Default collator getRules failed");
    doAssert(!rule1.equals(rule3), "Default collator getRules failed");
    try {
        col4 = new RuleBasedCollator(rule2);
    } catch (Exception e) {
        errln("RuleBased Collator creation failed.");
        return;
    }
    String rule4 = col4.getRules();
    doAssert(rule2.equals(rule4), "Default collator getRules failed");
    // tests that modifier ! is always ignored
    String exclamationrules = "!&a<b";
    // java does not allow ! to be the start of the rule
    String thaistr = "\u0e40\u0e01\u0e2d";
    try {
        RuleBasedCollator col5 = new RuleBasedCollator(exclamationrules);
        RuleBasedCollator encol = (RuleBasedCollator) Collator.getInstance(Locale.ENGLISH);
        CollationElementIterator col5iter = col5.getCollationElementIterator(thaistr);
        CollationElementIterator encoliter = encol.getCollationElementIterator(thaistr);
        while (true) {
            // testing with en since thai has its own tailoring
            int ce = col5iter.next();
            int ce2 = encoliter.next();
            if (ce2 != ce) {
                errln("! modifier test failed");
            }
            if (ce == CollationElementIterator.NULLORDER) {
                break;
            }
        }
    } catch (Exception e) {
        errln("RuleBased Collator creation failed for ! modifier.");
        return;
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) MissingResourceException(java.util.MissingResourceException) CollationElementIterator(android.icu.text.CollationElementIterator) MissingResourceException(java.util.MissingResourceException) Test(org.junit.Test)

Example 93 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator 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 94 with RuleBasedCollator

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

the class CollationCreationMethodTest method TestRuleVsLocaleCreationMonkey.

@Test
public void TestRuleVsLocaleCreationMonkey() {
    // create a RBC from a collator reader by reading in a locale collation file
    // also create one simply from a rules string (which should be
    // pulled from the locale collation file)
    // and then do crazy monkey testing on it to make sure they are the same.
    int x, y, z;
    Random r = createRandom();
    String randString1;
    CollationKey key1;
    CollationKey key2;
    Locale[] locales = Collator.getAvailableLocales();
    RuleBasedCollator localeCollator;
    RuleBasedCollator ruleCollator;
    for (z = 0; z < 60; z++) {
        x = r.nextInt(locales.length);
        Locale locale = locales[x];
        try {
            // this is making the assumption that the only type of collator that will be made is RBC
            localeCollator = (RuleBasedCollator) Collator.getInstance(locale);
            logln("Rules for " + locale + " are: " + localeCollator.getRules());
            ruleCollator = new RuleBasedCollator(localeCollator.getRules());
        } catch (Exception e) {
            warnln("ERROR: in creation of collator of locale " + locale.getDisplayName() + ": " + e);
            return;
        }
        // do it several times for each collator
        int n = 3;
        for (y = 0; y < n; y++) {
            randString1 = generateNewString(r);
            key1 = localeCollator.getCollationKey(randString1);
            key2 = ruleCollator.getCollationKey(randString1);
            report(locale.getDisplayName(), randString1, key1, key2);
        }
    }
}
Also used : Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Random(java.util.Random) CollationKey(android.icu.text.CollationKey) Test(org.junit.Test)

Example 95 with RuleBasedCollator

use of android.icu.text.RuleBasedCollator 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)

Aggregations

RuleBasedCollator (android.icu.text.RuleBasedCollator)140 Test (org.junit.Test)124 Collator (android.icu.text.Collator)42 ULocale (android.icu.util.ULocale)26 CollationElementIterator (android.icu.text.CollationElementIterator)25 Locale (java.util.Locale)22 CollationKey (android.icu.text.CollationKey)17 StringCharacterIterator (java.text.StringCharacterIterator)16 StringSearch (android.icu.text.StringSearch)14 RawCollationKey (android.icu.text.RawCollationKey)11 ParseException (java.text.ParseException)10 UnicodeSet (android.icu.text.UnicodeSet)8 AlphabeticIndex (android.icu.text.AlphabeticIndex)6 BreakIterator (android.icu.text.BreakIterator)6 MissingResourceException (java.util.MissingResourceException)5 IOException (java.io.IOException)4 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)3 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 ArrayList (java.util.ArrayList)2