Search in sources :

Example 31 with CollationKey

use of android.icu.text.CollationKey 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 32 with CollationKey

use of android.icu.text.CollationKey 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 33 with CollationKey

use of android.icu.text.CollationKey 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 34 with CollationKey

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

the class CollationDummyTest method TestSurrogates.

// TestSurrogates() is ported from cintltst/callcoll.c
/**
 * Tests surrogate support.
 */
@Test
public void TestSurrogates() {
    String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";
    String[] source = { "z", "\uD800\uDC00", "\ud800\udc0a\u0308", "\ud800\udc02" };
    String[] target = { "\uD800\uDC00", "\ud800\udc0a\u0308", "A", "\ud800\udc03" };
    // this test is to verify the supplementary sort key order in the english
    // collator
    Collator enCollation;
    try {
        enCollation = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for ENGLISH");
        return;
    }
    myCollation.setStrength(Collator.TERTIARY);
    int count = 0;
    // logln("start of english collation supplementary characters test\n");
    while (count < 2) {
        doTest(enCollation, source[count], target[count], -1);
        count++;
    }
    doTest(enCollation, source[count], target[count], 1);
    // logln("start of tailored collation supplementary characters test\n");
    count = 0;
    Collator newCollation;
    try {
        newCollation = new RuleBasedCollator(rules);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for rules");
        return;
    }
    // tests getting collation elements for surrogates for tailored rules
    while (count < 4) {
        doTest(newCollation, source[count], target[count], -1);
        count++;
    }
    // tests that \uD801\uDC01 still has the same value, not changed
    CollationKey enKey = enCollation.getCollationKey(source[3]);
    CollationKey newKey = newCollation.getCollationKey(source[3]);
    int keyResult = enKey.compareTo(newKey);
    if (keyResult != 0) {
        errln("Failed : non-tailored supplementary characters should have the same value\n");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 35 with CollationKey

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

the class CollationFinnishTest method doTest.

// main test routine, tests rules specific to the finish locale
private void doTest(char[] source, char[] target, int result) {
    String s = new String(source);
    String t = new String(target);
    int compareResult = myCollation.compare(s, t);
    CollationKey sortKey1, sortKey2;
    sortKey1 = myCollation.getCollationKey(s);
    sortKey2 = myCollation.getCollationKey(t);
    int keyResult = sortKey1.compareTo(sortKey2);
    reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
}
Also used : CollationKey(android.icu.text.CollationKey)

Aggregations

CollationKey (android.icu.text.CollationKey)47 RuleBasedCollator (android.icu.text.RuleBasedCollator)28 Test (org.junit.Test)27 RawCollationKey (android.icu.text.RawCollationKey)19 Collator (android.icu.text.Collator)15 Locale (java.util.Locale)8 ULocale (android.icu.util.ULocale)7 UnicodeSet (android.icu.text.UnicodeSet)3 ParseException (java.text.ParseException)3 Random (java.util.Random)3 CollationElementIterator (android.icu.text.CollationElementIterator)2 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)2 MissingResourceException (java.util.MissingResourceException)2 Output (android.icu.util.Output)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1