Search in sources :

Example 11 with CollationKey

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

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

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

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

the class CollationDummyTest method TestVariableTop.

// TestVariableTop() is ported from cintltst/callcoll.c
/**
 * Tests the [variable top] tag in rule syntax. Since the default [alternate]
 * tag has the value shifted, any codepoints before [variable top] should give
 * a primary ce of 0.
 */
@Test
public void TestVariableTop() {
    /*
         * Starting with ICU 53, setting the variable top via a pseudo relation string
         * is not supported any more.
         * It was replaced by the [maxVariable symbol] setting.
         * See ICU tickets #9958 and #8032.
         */
    if (!SUPPORT_VARIABLE_TOP_RELATION) {
        return;
    }
    String rule = "&z = [variable top]";
    Collator myColl;
    Collator enColl;
    char[] source = new char[1];
    char ch;
    int[] expected = { 0 };
    try {
        enColl = Collator.getInstance(Locale.ENGLISH);
    } catch (Exception e) {
        errln("ERROR: Failed to create the collator for ENGLISH");
        return;
    }
    try {
        myColl = new RuleBasedCollator(rule);
    } catch (Exception e) {
        errln("Fail to create RuleBasedCollator with rules:" + rule);
        return;
    }
    enColl.setStrength(Collator.PRIMARY);
    myColl.setStrength(Collator.PRIMARY);
    ((RuleBasedCollator) enColl).setAlternateHandlingShifted(true);
    ((RuleBasedCollator) myColl).setAlternateHandlingShifted(true);
    if (((RuleBasedCollator) enColl).isAlternateHandlingShifted() != true) {
        errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");
    }
    // space is supposed to be a variable
    CollationKey key = enColl.getCollationKey(" ");
    byte[] result = key.toByteArray();
    for (int i = 0; i < result.length; i++) {
        if (result[i] != expected[i]) {
            errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
            break;
        }
    }
    ch = 'a';
    while (ch < 'z') {
        source[0] = ch;
        key = myColl.getCollationKey(new String(source));
        result = key.toByteArray();
        for (int i = 0; i < result.length; i++) {
            if (result[i] != expected[i]) {
                errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
                break;
            }
        }
        ch++;
    }
}
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 15 with CollationKey

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

the class CollationDummyTest method doTestVariant.

private void doTestVariant(Collator collation, String source, String target, int result) {
    int compareResult = collation.compare(source, target);
    CollationKey srckey, tgtkey;
    srckey = collation.getCollationKey(source);
    tgtkey = collation.getCollationKey(target);
    int keyResult = srckey.compareTo(tgtkey);
    if (compareResult != result) {
        errln("String comparison failed in variant test\n");
    }
    if (keyResult != result) {
        errln("Collation key comparison failed in variant test\n");
    }
}
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