Search in sources :

Example 16 with Collator

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

the class CollationMiscTest method TestHangulTailoring.

@Test
public void TestHangulTailoring() {
    String[] koreanData = { "\uac00", "\u4f3d", "\u4f73", "\u5047", "\u50f9", "\u52a0", "\u53ef", "\u5475", "\u54e5", "\u5609", "\u5ac1", "\u5bb6", "\u6687", "\u67b6", "\u67b7", "\u67ef", "\u6b4c", "\u73c2", "\u75c2", "\u7a3c", "\u82db", "\u8304", "\u8857", "\u8888", "\u8a36", "\u8cc8", "\u8dcf", "\u8efb", "\u8fe6", "\u99d5", "\u4EEE", "\u50A2", "\u5496", "\u54FF", "\u5777", "\u5B8A", "\u659D", "\u698E", "\u6A9F", "\u73C8", "\u7B33", "\u801E", "\u8238", "\u846D", "\u8B0C" };
    String rules = "&\uac00 <<< \u4f3d <<< \u4f73 <<< \u5047 <<< \u50f9 <<< \u52a0 <<< \u53ef <<< \u5475 " + "<<< \u54e5 <<< \u5609 <<< \u5ac1 <<< \u5bb6 <<< \u6687 <<< \u67b6 <<< \u67b7 <<< \u67ef " + "<<< \u6b4c <<< \u73c2 <<< \u75c2 <<< \u7a3c <<< \u82db <<< \u8304 <<< \u8857 <<< \u8888 " + "<<< \u8a36 <<< \u8cc8 <<< \u8dcf <<< \u8efb <<< \u8fe6 <<< \u99d5 " + "<<< \u4EEE <<< \u50A2 <<< \u5496 <<< \u54FF <<< \u5777 <<< \u5B8A <<< \u659D <<< \u698E " + "<<< \u6A9F <<< \u73C8 <<< \u7B33 <<< \u801E <<< \u8238 <<< \u846D <<< \u8B0C";
    String rlz = rules;
    Collator coll = null;
    try {
        coll = new RuleBasedCollator(rlz);
    } catch (Exception e) {
        warnln("Unable to open collator with rules" + rules);
        return;
    }
    // logln("Using start of korean rules\n");
    genericOrderingTest(coll, koreanData);
// no such locale in icu4j
// logln("Using ko__LOTUS locale\n");
// genericLocaleStarter(new Locale("ko__LOTUS", ""), koreanData);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 17 with Collator

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

the class CollationMiscTest method TestNonChars.

@Test
public void TestNonChars() {
    String[] test = { "\u0000", /* ignorable */
    "\uFFFE", /* special merge-sort character with minimum non-ignorable weights */
    "\uFDD0", "\uFDEF", "\\U0001FFFE", "\\U0001FFFF", /* UCA 6.0: noncharacters are treated like unassigned, */
    "\\U0002FFFE", "\\U0002FFFF", /* not like ignorable. */
    "\\U0003FFFE", "\\U0003FFFF", "\\U0004FFFE", "\\U0004FFFF", "\\U0005FFFE", "\\U0005FFFF", "\\U0006FFFE", "\\U0006FFFF", "\\U0007FFFE", "\\U0007FFFF", "\\U0008FFFE", "\\U0008FFFF", "\\U0009FFFE", "\\U0009FFFF", "\\U000AFFFE", "\\U000AFFFF", "\\U000BFFFE", "\\U000BFFFF", "\\U000CFFFE", "\\U000CFFFF", "\\U000DFFFE", "\\U000DFFFF", "\\U000EFFFE", "\\U000EFFFF", "\\U000FFFFE", "\\U000FFFFF", "\\U0010FFFE", "\\U0010FFFF", "\uFFFF" /* special character with maximum primary weight */
    };
    Collator coll = null;
    try {
        coll = Collator.getInstance(new Locale("en", "US"));
    } catch (Exception e) {
        warnln("Unable to open collator");
        return;
    }
    // logln("Test non characters");
    genericOrderingTestWithResult(coll, test, -1);
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 18 with Collator

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

the class CollationMiscTest method TestBlackBird.

@Test
public void TestBlackBird() {
    String[] shifted = { "black bird", "black-bird", "blackbird", "black Bird", "black-Bird", "blackBird", "black birds", "black-birds", "blackbirds" };
    int[] shiftedTert = { 0, 0, 0, -1, 0, 0, -1, 0, 0 };
    String[] nonignorable = { "black bird", "black Bird", "black birds", "black-bird", "black-Bird", "black-birds", "blackbird", "blackBird", "blackbirds" };
    int i = 0, j = 0;
    int size = 0;
    Collator coll = Collator.getInstance(new Locale("en", "US"));
    // ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
    // ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
    ((RuleBasedCollator) coll).setAlternateHandlingShifted(false);
    size = nonignorable.length;
    for (i = 0; i < size - 1; i++) {
        for (j = i + 1; j < size; j++) {
            String t1 = nonignorable[i];
            String t2 = nonignorable[j];
            CollationTest.doTest(this, (RuleBasedCollator) coll, t1, t2, -1);
        }
    }
    ((RuleBasedCollator) coll).setAlternateHandlingShifted(true);
    coll.setStrength(Collator.QUATERNARY);
    size = shifted.length;
    for (i = 0; i < size - 1; i++) {
        for (j = i + 1; j < size; j++) {
            String t1 = shifted[i];
            String t2 = shifted[j];
            CollationTest.doTest(this, (RuleBasedCollator) coll, t1, t2, -1);
        }
    }
    coll.setStrength(Collator.TERTIARY);
    size = shifted.length;
    for (i = 1; i < size; i++) {
        String t1 = shifted[i - 1];
        String t2 = shifted[i];
        CollationTest.doTest(this, (RuleBasedCollator) coll, t1, t2, shiftedTert[i]);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 19 with Collator

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

the class CollationAPITest method TestSubClass.

/**
 * Simple test to see if Collator is subclassable.
 * Also test coverage of base class methods that are overridden by RuleBasedCollator.
 */
@Test
public void TestSubClass() {
    class TestCollator extends Collator {

        @Override
        public boolean equals(Object that) {
            return this == that;
        }

        @Override
        public int hashCode() {
            return 0;
        }

        @Override
        public int compare(String source, String target) {
            return source.compareTo(target);
        }

        @Override
        public CollationKey getCollationKey(String source) {
            return new CollationKey(source, getRawCollationKey(source, new RawCollationKey()));
        }

        @Override
        public RawCollationKey getRawCollationKey(String source, RawCollationKey key) {
            byte[] temp1 = source.getBytes();
            byte[] temp2 = new byte[temp1.length + 1];
            System.arraycopy(temp1, 0, temp2, 0, temp1.length);
            temp2[temp1.length] = 0;
            if (key == null) {
                key = new RawCollationKey();
            }
            key.bytes = temp2;
            key.size = temp2.length;
            return key;
        }

        @Override
        public void setVariableTop(int ce) {
            if (isFrozen()) {
                throw new UnsupportedOperationException("Attempt to modify frozen object");
            }
        }

        @Override
        public int setVariableTop(String str) {
            if (isFrozen()) {
                throw new UnsupportedOperationException("Attempt to modify frozen object");
            }
            return 0;
        }

        @Override
        public int getVariableTop() {
            return 0;
        }

        @Override
        public VersionInfo getVersion() {
            return VersionInfo.getInstance(0);
        }

        @Override
        public VersionInfo getUCAVersion() {
            return VersionInfo.getInstance(0);
        }
    }
    Collator col1 = new TestCollator();
    Collator col2 = new TestCollator();
    if (col1.equals(col2)) {
        errln("2 different instance of TestCollator should fail");
    }
    if (col1.hashCode() != col2.hashCode()) {
        errln("Every TestCollator has the same hashcode");
    }
    String abc = "abc";
    String bcd = "bcd";
    if (col1.compare(abc, bcd) != abc.compareTo(bcd)) {
        errln("TestCollator compare should be the same as the default " + "string comparison");
    }
    CollationKey key = col1.getCollationKey(abc);
    byte[] temp1 = abc.getBytes();
    byte[] temp2 = new byte[temp1.length + 1];
    System.arraycopy(temp1, 0, temp2, 0, temp1.length);
    temp2[temp1.length] = 0;
    if (!java.util.Arrays.equals(key.toByteArray(), temp2) || !key.getSourceString().equals(abc)) {
        errln("TestCollator collationkey API is returning wrong values");
    }
    UnicodeSet set = col1.getTailoredSet();
    if (!set.equals(new UnicodeSet(0, 0x10FFFF))) {
        errln("Error getting default tailored set");
    }
    // Base class code coverage.
    // Most of these methods are dummies;
    // they are overridden by any subclass that supports their features.
    assertEquals("compare(strings as Object)", 0, col1.compare(new StringBuilder("abc"), new StringBuffer("abc")));
    col1.setStrength(Collator.SECONDARY);
    assertNotEquals("getStrength()", Collator.PRIMARY, col1.getStrength());
    // setStrength2() is @internal and returns this.
    // The base class getStrength() always returns the same value,
    // since the base class does not have a field to store the strength.
    assertNotEquals("setStrength2().getStrength()", Collator.PRIMARY, col1.setStrength2(Collator.IDENTICAL).getStrength());
    // (base class).setDecomposition() may or may not be implemented.
    try {
        col1.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (UnsupportedOperationException expected) {
    }
    // don't care about the value
    assertNotEquals("getDecomposition()", -1, col1.getDecomposition());
    // (base class).setMaxVariable() may or may not be implemented.
    try {
        col1.setMaxVariable(Collator.ReorderCodes.CURRENCY);
    } catch (UnsupportedOperationException expected) {
    }
    // don't care about the value
    assertNotEquals("getMaxVariable()", -1, col1.getMaxVariable());
    // (base class).setReorderCodes() may or may not be implemented.
    try {
        col1.setReorderCodes(0, 1, 2);
    } catch (UnsupportedOperationException expected) {
    }
    try {
        col1.getReorderCodes();
    } catch (UnsupportedOperationException expected) {
    }
    assertFalse("getDisplayName()", Collator.getDisplayName(Locale.GERMAN).isEmpty());
    assertFalse("getDisplayName()", Collator.getDisplayName(Locale.GERMAN, Locale.ITALIAN).isEmpty());
    assertNotEquals("getLocale()", ULocale.GERMAN, col1.getLocale(ULocale.ACTUAL_LOCALE));
    // Cover Collator.setLocale() which is only package-visible.
    Object token = Collator.registerInstance(new TestCollator(), new ULocale("de-Japn-419"));
    Collator.unregister(token);
    // Freezable default implementations. freeze() may or may not be implemented.
    assertFalse("not yet frozen", col2.isFrozen());
    try {
        col2.freeze();
        assertTrue("now frozen", col2.isFrozen());
    } catch (UnsupportedOperationException expected) {
    }
    try {
        col2.setStrength(Collator.PRIMARY);
        if (col2.isFrozen()) {
            fail("(frozen Collator).setStrength() should throw an exception");
        }
    } catch (UnsupportedOperationException expected) {
    }
    try {
        Collator col3 = col2.cloneAsThawed();
        assertFalse("!cloneAsThawed().isFrozen()", col3.isFrozen());
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) ULocale(android.icu.util.ULocale) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) UnicodeSet(android.icu.text.UnicodeSet) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 20 with Collator

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

the class CollationAPITest method TestGetLocale.

@Test
public void TestGetLocale() {
    String rules = "&a<x<y<z";
    Collator coll = Collator.getInstance(new ULocale("root"));
    ULocale locale = coll.getLocale(ULocale.ACTUAL_LOCALE);
    if (!locale.equals(ULocale.ROOT)) {
        errln("Collator.getInstance(\"root\").getLocale(actual) != ULocale.ROOT; " + "getLocale().getName() = \"" + locale.getName() + "\"");
    }
    coll = Collator.getInstance(new ULocale(""));
    locale = coll.getLocale(ULocale.ACTUAL_LOCALE);
    if (!locale.equals(ULocale.ROOT)) {
        errln("Collator.getInstance(\"\").getLocale(actual) != ULocale.ROOT; " + "getLocale().getName() = \"" + locale.getName() + "\"");
    }
    int i = 0;
    String[][] testStruct = { // Note: ULocale.ROOT.getName() == "" not "root".
    { "de_DE", "de", "" }, { "sr_RS", "sr_Cyrl_RS", "sr" }, { "en_US_CALIFORNIA", "en_US", "" }, { "fr_FR_NONEXISTANT", "fr", "" }, // pinyin is the default, therefore suppressed.
    { "zh_CN", "zh_Hans_CN", "zh" }, // zh_Hant has default=stroke but the data is in zh.
    { "zh_TW", "zh_Hant_TW", "zh@collation=stroke" }, { "zh_TW@collation=pinyin", "zh_Hant_TW@collation=pinyin", "zh" }, { "zh_CN@collation=stroke", "zh_Hans_CN@collation=stroke", "zh@collation=stroke" } };
    /* test opening collators for different locales */
    for (i = 0; i < testStruct.length; i++) {
        String requestedLocale = testStruct[i][0];
        String validLocale = testStruct[i][1];
        String actualLocale = testStruct[i][2];
        try {
            coll = Collator.getInstance(new ULocale(requestedLocale));
        } catch (Exception e) {
            errln(String.format("Failed to open collator for %s with %s", requestedLocale, e));
            continue;
        }
        // Note: C++ getLocale() recognizes ULOC_REQUESTED_LOCALE
        // which does not exist in Java.
        locale = coll.getLocale(ULocale.VALID_LOCALE);
        if (!locale.equals(new ULocale(validLocale))) {
            errln(String.format("[Coll %s]: Error in valid locale, expected %s, got %s", requestedLocale, validLocale, locale.getName()));
        }
        locale = coll.getLocale(ULocale.ACTUAL_LOCALE);
        if (!locale.equals(new ULocale(actualLocale))) {
            errln(String.format("[Coll %s]: Error in actual locale, expected %s, got %s", requestedLocale, actualLocale, locale.getName()));
        }
        // If we open a collator for the actual locale, we should get an equivalent one again.
        Collator coll2;
        try {
            coll2 = Collator.getInstance(locale);
        } catch (Exception e) {
            errln(String.format("Failed to open collator for actual locale \"%s\" with %s", locale.getName(), e));
            continue;
        }
        ULocale actual2 = coll2.getLocale(ULocale.ACTUAL_LOCALE);
        if (!actual2.equals(locale)) {
            errln(String.format("[Coll actual \"%s\"]: Error in actual locale, got different one: \"%s\"", locale.getName(), actual2.getName()));
        }
        if (!coll2.equals(coll)) {
            errln(String.format("[Coll actual \"%s\"]: Got different collator than before", locale.getName()));
        }
    }
    /* completely non-existent locale for collator should get a root collator */
    {
        try {
            coll = Collator.getInstance(new ULocale("blahaha"));
        } catch (Exception e) {
            errln("Failed to open collator with " + e);
            return;
        }
        ULocale valid = coll.getLocale(ULocale.VALID_LOCALE);
        String name = valid.getName();
        if (name.length() != 0 && !name.equals("root")) {
            errln("Valid locale for nonexisting locale collator is \"" + name + "\" not root");
        }
        ULocale actual = coll.getLocale(ULocale.ACTUAL_LOCALE);
        name = actual.getName();
        if (name.length() != 0 && !name.equals("root")) {
            errln("Actual locale for nonexisting locale collator is \"" + name + "\" not root");
        }
    }
    /* collator instantiated from rules should have all locales null */
    try {
        coll = new RuleBasedCollator(rules);
    } catch (Exception e) {
        errln("RuleBasedCollator(" + rules + ") failed: " + e);
        return;
    }
    locale = coll.getLocale(ULocale.VALID_LOCALE);
    if (locale != null) {
        errln(String.format("For collator instantiated from rules, valid locale %s is not bogus", locale.getName()));
    }
    locale = coll.getLocale(ULocale.ACTUAL_LOCALE);
    if (locale != null) {
        errln(String.format("For collator instantiated from rules, actual locale %s is not bogus", locale.getName()));
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) 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