Search in sources :

Example 6 with CollationKey

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

the class CollationAPITest method TestHashCode.

/**
 * This tests the hashCode method of a collator object.
 */
@Test
public void TestHashCode() {
    logln("hashCode tests begin.");
    Collator col1 = Collator.getInstance(Locale.ENGLISH);
    Collator col2 = null;
    Locale dk = new Locale("da", "DK", "");
    try {
        col2 = Collator.getInstance(dk);
    } catch (Exception e) {
        errln("Danish collation creation failed.");
        return;
    }
    Collator col3 = null;
    try {
        col3 = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("2nd default collation creation failed.");
        return;
    }
    logln("Collator.hashCode() testing ...");
    doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect");
    doAssert(!(col1.hashCode() == col2.hashCode()), "Hash test2 result incorrect");
    doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal");
    logln("hashCode tests end.");
    String test1 = "Abcda";
    String test2 = "abcda";
    CollationKey sortk1, sortk2, sortk3;
    sortk1 = col3.getCollationKey(test1);
    sortk2 = col3.getCollationKey(test2);
    sortk3 = col3.getCollationKey(test2);
    doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
    doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal");
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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 7 with CollationKey

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

the class CollationMiscTest method TestVariableTop.

@Test
public void TestVariableTop() {
    // ICU 53+: The character must be in a supported reordering group,
    // and the variable top is pinned to the end of that group.
    // parseNextToken is not released as public so i create my own rules
    String rules = "& ' ' < b < c < de < fg & hi = j";
    try {
        RuleBasedCollator coll = new RuleBasedCollator(rules);
        String[] tokens = { " ", "b", "c", "de", "fg", "hi", "j", "ab" };
        coll.setAlternateHandlingShifted(true);
        for (int i = 0; i < tokens.length; i++) {
            int varTopOriginal = coll.getVariableTop();
            try {
                int varTop = coll.setVariableTop(tokens[i]);
                if (i > 4) {
                    errln("Token " + tokens[i] + " expected to fail");
                }
                if (varTop != coll.getVariableTop()) {
                    errln("Error setting and getting variable top");
                }
                CollationKey key1 = coll.getCollationKey(tokens[i]);
                for (int j = 0; j < i; j++) {
                    CollationKey key2 = coll.getCollationKey(tokens[j]);
                    if (key2.compareTo(key1) < 0) {
                        errln("Setting variable top shouldn't change the comparison sequence");
                    }
                    byte[] sortorder = key2.toByteArray();
                    if (sortorder.length > 0 && (key2.toByteArray())[0] > 1) {
                        errln("Primary sort order should be 0");
                    }
                }
            } catch (Exception e) {
                CollationElementIterator iter = coll.getCollationElementIterator(tokens[i]);
                /*int ce =*/
                iter.next();
                int ce2 = iter.next();
                if (ce2 == CollationElementIterator.NULLORDER) {
                    errln("Token " + tokens[i] + " not expected to fail");
                }
                if (coll.getVariableTop() != varTopOriginal) {
                    errln("When exception is thrown variable top should " + "not be changed");
                }
            }
            coll.setVariableTop(varTopOriginal);
            if (varTopOriginal != coll.getVariableTop()) {
                errln("Couldn't restore old variable top\n");
            }
        }
        // Testing calling with error set
        try {
            coll.setVariableTop("");
            errln("Empty string should throw an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            logln("PASS: Empty string failed as expected");
        }
        try {
            coll.setVariableTop(null);
            errln("Null string should throw an IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            logln("PASS: null string failed as expected");
        }
    } catch (Exception e) {
        warnln("Error creating RuleBasedCollator");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 8 with CollationKey

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

the class CollationMiscTest method TestGetBound.

/*
     * Tests the method public CollationKey getBound(int boundType, int noOfLevels)
     */
@Test
public void TestGetBound() {
    CollationKey ck = new CollationKey("", (byte[]) null);
    // Tests when "default: " is true for "switch (boundType)"
    try {
        ck.getBound(BoundMode.COUNT, -1);
        errln("CollationKey.getBound(int,int) was suppose to return an " + "exception for an invalid boundType value.");
    } catch (Exception e) {
    }
    // Tests when "if (noOfLevels > 0)"
    byte[] b = {};
    CollationKey ck1 = new CollationKey("", b);
    try {
        ck1.getBound(0, 1);
        errln("CollationKey.getBound(int,int) was suppose to return an " + "exception a value of noOfLevels that exceeds expected.");
    } catch (Exception e) {
    }
}
Also used : CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Test(org.junit.Test)

Example 9 with CollationKey

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

the class CollationMiscTest method TestMerge.

/*
     * Tests the method public CollationKey merge(CollationKey source)
     */
@Test
public void TestMerge() {
    byte[] b = {};
    CollationKey ck = new CollationKey("", b);
    // Tests when "if (source == null || source.getLength() == 0)" is true
    try {
        ck.merge(null);
        errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");
    } catch (Exception e) {
    }
    try {
        ck.merge(ck);
        errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");
    } catch (Exception e) {
    }
}
Also used : CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Test(org.junit.Test)

Example 10 with CollationKey

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

the class CollationMiscTest method TestUCAPrecontext.

@Test
public void TestUCAPrecontext() {
    String[] rules = { "& \u00B7<a ", // 'a' is an expansion.
    "& L\u00B7 << a" };
    String[] cases = { "\u00B7", "\u0387", "a", "l", "L\u0332", "l\u00B7", "l\u0387", "L\u0387", "la\u0387", "La\u00b7" };
    // Test en sort
    RuleBasedCollator en = null;
    logln("EN sort:");
    try {
        en = (RuleBasedCollator) Collator.getInstance(new Locale("en", ""));
        for (int j = 0; j < cases.length; j++) {
            CollationKey key = en.getCollationKey(cases[j]);
            if (j > 0) {
                CollationKey prevKey = en.getCollationKey(cases[j - 1]);
                if (key.compareTo(prevKey) < 0) {
                    errln("Error! EN test[" + j + "]:source:" + cases[j] + " is not >= previous test string.");
                }
            }
            /*
                if ( key.compareTo(expectingKey)!=0) {
                    errln("Error! Test case["+i+"]:"+"source:" + key.getSourceString());
                    errln("expecting:"+prettify(expectingKey)+ "got:"+  prettify(key));
                }
                */
            logln("String:" + cases[j] + "   Key:" + CollationTest.prettify(key));
        }
    } catch (Exception e) {
        warnln("Error creating English collator");
        return;
    }
    // Test ja sort
    RuleBasedCollator ja = null;
    logln("JA sort:");
    try {
        ja = (RuleBasedCollator) Collator.getInstance(new Locale("ja", ""));
        for (int j = 0; j < cases.length; j++) {
            CollationKey key = ja.getCollationKey(cases[j]);
            if (j > 0) {
                CollationKey prevKey = ja.getCollationKey(cases[j - 1]);
                if (key.compareTo(prevKey) < 0) {
                    errln("Error! JA test[" + j + "]:source:" + cases[j] + " is not >= previous test string.");
                }
            }
            logln("String:" + cases[j] + "   Key:" + CollationTest.prettify(key));
        }
    } catch (Exception e) {
        warnln("Error creating Japanese collator");
        return;
    }
    for (int i = 0; i < rules.length; i++) {
        RuleBasedCollator coll = null;
        logln("Tailoring rule:" + rules[i]);
        try {
            coll = new RuleBasedCollator(rules[i]);
        } catch (Exception e) {
            warnln("Unable to open collator with rules " + rules[i]);
            continue;
        }
        for (int j = 0; j < cases.length; j++) {
            CollationKey key = coll.getCollationKey(cases[j]);
            if (j > 0) {
                CollationKey prevKey = coll.getCollationKey(cases[j - 1]);
                if (i == 1 && j == 3) {
                    if (key.compareTo(prevKey) > 0) {
                        errln("Error! Rule:" + rules[i] + " test[" + j + "]:source:" + cases[j] + " is not <= previous test string.");
                    }
                } else {
                    if (key.compareTo(prevKey) < 0) {
                        errln("Error! Rule:" + rules[i] + " test[" + j + "]:source:" + cases[j] + " is not >= previous test string.");
                    }
                }
            }
            logln("String:" + cases[j] + "   Key:" + CollationTest.prettify(key));
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Test(org.junit.Test)

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