Search in sources :

Example 31 with RuleBasedCollator

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

Example 32 with RuleBasedCollator

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

the class CollationMiscTest method TestVI5913.

public void TestVI5913() {
    String[] rules = { "&a < \u00e2 <<< \u00c2", // OMEGA WITH YPOGEGRAMMENI
    "&a < \u1FF3 ", // &s < s with caron
    "&s < \u0161 ", // &x < ae &z < a+e with circumflex
    "&x < ae &z < a\u00EA" };
    String[][] cases = { { "\u1EAC", "A\u0323\u0302", "\u1EA0\u0302", "\u00C2\u0323" }, { "\u1FA2", "\u03C9\u0313\u0300\u0345", "\u1FF3\u0313\u0300", "\u1F60\u0300\u0345", "\u1f62\u0345", "\u1FA0\u0300" }, { "\u1E63\u030C", "s\u0323\u030C", "s\u030C\u0323" }, { // a+ e with dot below and circumflex
    "a\u1EC7", // a + e with dot below + combining circumflex
    "a\u1EB9\u0302", // a + e with circumflex + combining dot below
    "a\u00EA\u0323" } };
    for (int i = 0; i < rules.length; i++) {
        RuleBasedCollator coll = null;
        try {
            coll = new RuleBasedCollator(rules[i]);
        } catch (Exception e) {
            warnln("Unable to open collator with rules " + rules[i]);
        }
        logln("Test case[" + i + "]:");
        CollationKey expectingKey = coll.getCollationKey(cases[i][0]);
        for (int j = 1; j < cases[i].length; j++) {
            CollationKey key = coll.getCollationKey(cases[i][j]);
            if (key.compareTo(expectingKey) != 0) {
                errln("Error! Test case[" + i + "]:" + "source:" + key.getSourceString());
                errln("expecting:" + CollationTest.prettify(expectingKey) + "got:" + CollationTest.prettify(key));
            }
            logln("   Key:" + CollationTest.prettify(key));
        }
    }
    RuleBasedCollator vi_vi = null;
    try {
        vi_vi = (RuleBasedCollator) Collator.getInstance(new Locale("vi", ""));
        logln("VI sort:");
        CollationKey expectingKey = vi_vi.getCollationKey(cases[0][0]);
        for (int j = 1; j < cases[0].length; j++) {
            CollationKey key = vi_vi.getCollationKey(cases[0][j]);
            if (key.compareTo(expectingKey) != 0) {
                // TODO (claireho): change the logln to errln after vi.res is up-to-date.
                // errln("source:" + key.getSourceString());
                // errln("expecting:"+prettify(expectingKey)+ "got:"+  prettify(key));
                logln("Error!! in Vietnese sort - source:" + key.getSourceString());
                logln("expecting:" + CollationTest.prettify(expectingKey) + "got:" + CollationTest.prettify(key));
            }
            // logln("source:" + key.getSourceString());
            logln("   Key:" + CollationTest.prettify(key));
        }
    } catch (Exception e) {
        warnln("Error creating Vietnese collator");
        return;
    }
}
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)

Example 33 with RuleBasedCollator

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

the class CollationMiscTest method TestImportWithType.

@Test
public void TestImportWithType() {
    try {
        RuleBasedCollator vicoll = (RuleBasedCollator) Collator.getInstance(new ULocale("vi"));
        RuleBasedCollator decoll = (RuleBasedCollator) Collator.getInstance(ULocale.forLanguageTag("de-u-co-phonebk"));
        RuleBasedCollator videcoll = new RuleBasedCollator(vicoll.getRules() + decoll.getRules());
        RuleBasedCollator importvidecoll = new RuleBasedCollator("[import vi][import de-u-co-phonebk]");
        UnicodeSet tailoredSet = videcoll.getTailoredSet();
        UnicodeSet importTailoredSet = importvidecoll.getTailoredSet();
        if (!tailoredSet.equals(importTailoredSet)) {
            warnln("Tailored set not equal");
        }
        for (UnicodeSetIterator it = new UnicodeSetIterator(tailoredSet); it.next(); ) {
            String t = it.getString();
            CollationKey sk1 = videcoll.getCollationKey(t);
            CollationKey sk2 = importvidecoll.getCollationKey(t);
            if (!sk1.equals(sk2)) {
                warnln("Collation key's not equal for " + t);
            }
        }
    } catch (Exception e) {
        // Android patch: Add --omitCollationRules to genrb.
        logln("ERROR: in creation of rule based collator");
    // Android patch end.
    }
}
Also used : UnicodeSetIterator(android.icu.text.UnicodeSetIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 34 with RuleBasedCollator

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

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

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