Search in sources :

Example 36 with CollationKey

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

the class CollationFrozenMonkeyTest method TestCollationKey.

@Test
public void TestCollationKey() {
    if (source.length() == 0) {
        errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
        return;
    }
    Collator myPrimaryCollator;
    Collator mySecondaryCollator;
    Collator myTertiaryCollator;
    try {
        Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
        myCollator.freeze();
        myPrimaryCollator = myCollator.cloneAsThawed();
        myPrimaryCollator.setStrength(Collator.PRIMARY);
        myPrimaryCollator.freeze();
        mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
        mySecondaryCollator.setStrength(Collator.SECONDARY);
        mySecondaryCollator.freeze();
        myTertiaryCollator = mySecondaryCollator.cloneAsThawed();
        myTertiaryCollator.setStrength(Collator.TERTIARY);
        myTertiaryCollator.freeze();
    } catch (Exception e) {
        warnln("ERROR: in creation of collator of ENGLISH locale");
        return;
    }
    // use test framework's random seed
    Random rand = createRandom();
    int s = rand.nextInt(0x7fff) % source.length();
    int t = rand.nextInt(0x7fff) % source.length();
    int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
    String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
    String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
    CollationKey collationKey1, collationKey2;
    collationKey1 = myTertiaryCollator.getCollationKey(subs);
    collationKey2 = myTertiaryCollator.getCollationKey(subt);
    // Tertiary
    int result = collationKey1.compareTo(collationKey2);
    // Tertiary
    int revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    collationKey1 = mySecondaryCollator.getCollationKey(subs);
    collationKey2 = mySecondaryCollator.getCollationKey(subt);
    // Secondary
    result = collationKey1.compareTo(collationKey2);
    // Secondary
    revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    collationKey1 = myPrimaryCollator.getCollationKey(subs);
    collationKey2 = myPrimaryCollator.getCollationKey(subt);
    // Primary
    result = collationKey1.compareTo(collationKey2);
    // Primary
    revResult = collationKey2.compareTo(collationKey1);
    report(subs, subt, result, revResult);
    String msg = "";
    String addOne = subs + String.valueOf(0xE000);
    collationKey1 = myPrimaryCollator.getCollationKey(subs);
    collationKey2 = myPrimaryCollator.getCollationKey(addOne);
    result = collationKey1.compareTo(collationKey2);
    if (result != -1) {
        msg += "CollationKey(";
        msg += subs;
        msg += ") .LT. CollationKey(";
        msg += addOne;
        msg += ") Failed.";
        errln(msg);
    }
    msg = "";
    result = collationKey2.compareTo(collationKey1);
    if (result != 1) {
        msg += "CollationKey(";
        msg += addOne;
        msg += ") .GT. CollationKey(";
        msg += subs;
        msg += ") Failed.";
        errln(msg);
    }
}
Also used : Locale(java.util.Locale) Random(java.util.Random) CollationKey(android.icu.text.CollationKey) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 37 with CollationKey

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

the class CollationFrozenMonkeyTest method doTest.

void doTest(RuleBasedCollator myCollation, String mysource, String target, int result) {
    int compareResult = myCollation.compare(source, target);
    CollationKey sortKey1, sortKey2;
    try {
        sortKey1 = myCollation.getCollationKey(source);
        sortKey2 = myCollation.getCollationKey(target);
    } catch (Exception e) {
        errln("SortKey generation Failed.\n");
        return;
    }
    int keyResult = sortKey1.compareTo(sortKey2);
    reportCResult(mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
}
Also used : CollationKey(android.icu.text.CollationKey)

Example 38 with CollationKey

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

the class CollationMiscTest method TestImport.

@Test
public void TestImport() {
    try {
        RuleBasedCollator vicoll = (RuleBasedCollator) Collator.getInstance(new ULocale("vi"));
        RuleBasedCollator escoll = (RuleBasedCollator) Collator.getInstance(new ULocale("es"));
        RuleBasedCollator viescoll = new RuleBasedCollator(vicoll.getRules() + escoll.getRules());
        RuleBasedCollator importviescoll = new RuleBasedCollator("[import vi][import es]");
        UnicodeSet tailoredSet = viescoll.getTailoredSet();
        UnicodeSet importTailoredSet = importviescoll.getTailoredSet();
        if (!tailoredSet.equals(importTailoredSet)) {
            warnln("Tailored set not equal");
        }
        for (UnicodeSetIterator it = new UnicodeSetIterator(tailoredSet); it.next(); ) {
            String t = it.getString();
            CollationKey sk1 = viescoll.getCollationKey(t);
            CollationKey sk2 = importviescoll.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 39 with CollationKey

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

the class CollationMiscTest method TestHungarianTailoring.

/* Track7223: CollationElementIterator does not return correct order for Hungarian */
@Test
public void TestHungarianTailoring() {
    String rules = new String("&DZ<dzs<<<Dzs<<<DZS" + "&G<gy<<<Gy<<<GY" + "&L<ly<<<Ly<<<LY" + "&N<ny<<<Ny<<<NY" + "&S<sz<<<Sz<<<SZ" + "&T<ty<<<Ty<<<TY" + "&Z<zs<<<Zs<<<ZS" + "&O<\u00f6<<<\u00d6<<\u0151<<<\u0150" + "&U<\u00fc<<<\u00dc<<\u0171<<<\u0171" + "&cs<<<ccs/cs" + "&Cs<<<Ccs/cs" + "&CS<<<CCS/CS" + "&dz<<<ddz/dz" + "&Dz<<<Ddz/dz" + "&DZ<<<DDZ/DZ" + "&dzs<<<ddzs/dzs" + "&Dzs<<<Ddzs/dzs" + "&DZS<<<DDZS/DZS" + "&gy<<<ggy/gy" + "&Gy<<<Ggy/gy" + "&GY<<<GGY/GY");
    RuleBasedCollator coll;
    try {
        String str1 = "ggy";
        String str2 = "GGY";
        coll = new RuleBasedCollator(rules);
        if (coll.compare("ggy", "GGY") >= 0) {
            errln("TestHungarianTailoring.compare(" + str1 + "," + str2 + ") was suppose to return -1 ");
        }
        CollationKey sortKey1 = coll.getCollationKey(str1);
        CollationKey sortKey2 = coll.getCollationKey(str2);
        if (sortKey1.compareTo(sortKey2) >= 0) {
            errln("TestHungarianTailoring getCollationKey(\"" + str1 + "\") was suppose " + "less than getCollationKey(\"" + str2 + "\").");
            errln("  getCollationKey(\"ggy\"):" + CollationTest.prettify(sortKey1) + "  getCollationKey(\"GGY\"):" + CollationTest.prettify(sortKey2));
        }
        CollationElementIterator iter1 = coll.getCollationElementIterator(str1);
        CollationElementIterator iter2 = coll.getCollationElementIterator(str2);
        int ce1, ce2;
        while ((ce1 = iter1.next()) != CollationElementIterator.NULLORDER && (ce2 = iter2.next()) != CollationElementIterator.NULLORDER) {
            if (ce1 > ce2) {
                errln("TestHungarianTailoring.CollationElementIterator(" + str1 + "," + str2 + ") was suppose to return -1 ");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
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 40 with CollationKey

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

the class CollationMiscTest method TestBeforeRuleWithScriptReordering.

/*
     * This test ensures that characters placed before a character in a different script have the same lead byte
     * in their collation key before and after script reordering.
     */
@Test
public void TestBeforeRuleWithScriptReordering() throws Exception {
    /* build collator */
    String rules = "&[before 1]\u03b1 < \u0e01";
    int[] reorderCodes = { UScript.GREEK };
    int result;
    Collator myCollation = new RuleBasedCollator(rules);
    myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    myCollation.setStrength(Collator.TERTIARY);
    String base = "\u03b1";
    /* base */
    String before = "\u0e01";
    /* ko kai */
    /* check collation results - before rule applied but not script reordering */
    result = myCollation.compare(base, before);
    if (!(result > 0)) {
        errln("Collation result not correct before script reordering.");
    }
    /* check the lead byte of the collation keys before script reordering */
    CollationKey baseKey = myCollation.getCollationKey(base);
    CollationKey beforeKey = myCollation.getCollationKey(before);
    byte[] baseKeyBytes = baseKey.toByteArray();
    byte[] beforeKeyBytes = beforeKey.toByteArray();
    if (baseKeyBytes[0] != beforeKeyBytes[0]) {
        errln("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = " + baseKeyBytes[0] + ", before character lead byte = " + beforeKeyBytes[0]);
    }
    /* reorder the scripts */
    myCollation.setReorderCodes(reorderCodes);
    /* check collation results - before rule applied and after script reordering */
    result = myCollation.compare(base, before);
    if (!(result > 0)) {
        errln("Collation result not correct after script reordering.");
    }
    /* check the lead byte of the collation keys after script reordering */
    baseKey = myCollation.getCollationKey(base);
    beforeKey = myCollation.getCollationKey(before);
    baseKeyBytes = baseKey.toByteArray();
    beforeKeyBytes = beforeKey.toByteArray();
    if (baseKeyBytes[0] != beforeKeyBytes[0]) {
        errln("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = " + baseKeyBytes[0] + ", before character lead byte = " + beforeKeyBytes[0]);
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) 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