Search in sources :

Example 61 with Collator

use of android.icu.text.Collator 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 62 with Collator

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

the class CollationFrozenMonkeyTest method TestRules.

@Test
public void TestRules() {
    String[] testSourceCases = { "\u0061\u0062\u007a", "\u0061\u0062\u007a" };
    String[] testTargetCases = { "\u0061\u0062\u00e4", "\u0061\u0062\u0061\u0308" };
    int i = 0;
    logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
    Collator col = Collator.getInstance(new Locale("en", "US"));
    String baseRules = ((RuleBasedCollator) col).getRules();
    String newRules = " & z < ";
    newRules = baseRules + newRules + String.valueOf(0x00e4);
    RuleBasedCollator myCollation = null;
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        warnln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
    logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
    newRules = "";
    newRules = baseRules + " & z < a" + String.valueOf(0x0308);
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        errln("Demo Test 1 Table Collation object creation failed.");
        return;
    }
    for (i = 0; i < 2; i++) {
        doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
    }
}
Also used : Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) Test(org.junit.Test)

Example 63 with Collator

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

the class CollationMiscTest method TestGreekFirstReorderCloning.

@Test
public void TestGreekFirstReorderCloning() {
    String[] testSourceCases = { "\u0041", "\u03b1\u0041", "\u0061", "\u0041\u0061", "\u0391" };
    String[] testTargetCases = { "\u03b1", "\u0041\u03b1", "\u0391", "\u0391\u03b1", "\u0391" };
    int[] results = { 1, -1, 1, 1, 0 };
    Collator originalCollation;
    Collator myCollation;
    String rules = "[reorder Grek]";
    try {
        originalCollation = new RuleBasedCollator(rules);
    } catch (Exception e) {
        warnln("ERROR: in creation of rule based collator");
        return;
    }
    try {
        myCollation = (Collator) originalCollation.clone();
    } catch (Exception e) {
        warnln("ERROR: in creation of rule based collator");
        return;
    }
    myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    myCollation.setStrength(Collator.TERTIARY);
    for (int i = 0; i < testSourceCases.length; i++) {
        CollationTest.doTest(this, (RuleBasedCollator) myCollation, testSourceCases[i], testTargetCases[i], results[i]);
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 64 with Collator

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

the class CollationMiscTest method TestVariableTopSetting.

// ported from cmsccoll.c
@Test
public void TestVariableTopSetting() {
    int varTopOriginal = 0, varTop1, varTop2;
    Collator coll = Collator.getInstance(ULocale.ROOT);
    String empty = "";
    String space = " ";
    String dot = ".";
    /* punctuation */
    String degree = "\u00b0";
    /* symbol */
    String dollar = "$";
    /* currency symbol */
    String zero = "0";
    /* digit */
    varTopOriginal = coll.getVariableTop();
    logln(String.format("coll.getVariableTop(root) -> %08x", varTopOriginal));
    ((RuleBasedCollator) coll).setAlternateHandlingShifted(true);
    varTop1 = coll.setVariableTop(space);
    varTop2 = coll.getVariableTop();
    logln(String.format("coll.setVariableTop(space) -> %08x", varTop1));
    if (varTop1 != varTop2 || !coll.equals(empty, space) || coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(space, dot) >= 0) {
        errln("coll.setVariableTop(space) did not work");
    }
    varTop1 = coll.setVariableTop(dot);
    varTop2 = coll.getVariableTop();
    logln(String.format("coll.setVariableTop(dot) -> %08x", varTop1));
    if (varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dot, degree) >= 0) {
        errln("coll.setVariableTop(dot) did not work");
    }
    varTop1 = coll.setVariableTop(degree);
    varTop2 = coll.getVariableTop();
    logln(String.format("coll.setVariableTop(degree) -> %08x", varTop1));
    if (varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(degree, dollar) >= 0) {
        errln("coll.setVariableTop(degree) did not work");
    }
    varTop1 = coll.setVariableTop(dollar);
    varTop2 = coll.getVariableTop();
    logln(String.format("coll.setVariableTop(dollar) -> %08x", varTop1));
    if (varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || !coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dollar, zero) >= 0) {
        errln("coll.setVariableTop(dollar) did not work");
    }
    logln("Testing setting variable top to contractions");
    try {
        coll.setVariableTop("@P");
        errln("Invalid contraction succeded in setting variable top!");
    } catch (Exception expected) {
    }
    logln("Test restoring variable top");
    coll.setVariableTop(varTopOriginal);
    if (varTopOriginal != coll.getVariableTop()) {
        errln("Couldn't restore old variable top");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 65 with Collator

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

the class CollationMiscTest method TestChMove.

@Test
public void TestChMove() {
    String[] chTest = { "c", "C", "ca", "cb", "cx", "cy", "CZ", "c\u030C", "C\u030C", "h", "H", "ha", "Ha", "harly", "hb", "HB", "hx", "HX", "hy", "HY", "ch", "cH", "Ch", "CH", "cha", "charly", "che", "chh", "chch", "chr", "i", "I", "iarly", "r", "R", "r\u030C", "R\u030C", "s", "S", "s\u030C", "S\u030C", "z", "Z", "z\u030C", "Z\u030C" };
    Collator coll = null;
    try {
        coll = Collator.getInstance(new Locale("cs", ""));
    } catch (Exception e) {
        warnln("Cannot create Collator");
        return;
    }
    int size = chTest.length;
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            String t1 = chTest[i];
            String t2 = chTest[j];
            CollationTest.doTest(this, (RuleBasedCollator) coll, t1, t2, -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)

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