Search in sources :

Example 1 with RuleBasedCollator

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

the class CollationTest method TestMinMax.

@Test
public void TestMinMax() {
    setRootCollator();
    RuleBasedCollator rbc = (RuleBasedCollator) coll;
    final String s = "\uFFFE\uFFFF";
    long[] ces;
    ces = rbc.internalGetCEs(s);
    if (ces.length != 2) {
        errln("expected 2 CEs for <FFFE, FFFF>, got " + ces.length);
        return;
    }
    long ce = ces[0];
    long expected = Collation.makeCE(Collation.MERGE_SEPARATOR_PRIMARY);
    if (ce != expected) {
        errln("CE(U+fffe)=0x" + Utility.hex(ce) + " != 02..");
    }
    ce = ces[1];
    expected = Collation.makeCE(Collation.MAX_PRIMARY);
    if (ce != expected) {
        errln("CE(U+ffff)=0x" + Utility.hex(ce) + " != max..");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 2 with RuleBasedCollator

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

the class CollationTest method checkCompareTwo.

private boolean checkCompareTwo(String norm, String prevFileLine, String prevString, String s, int expectedOrder, int expectedLevel) {
    // Get the sort keys first, for error debug output.
    Output<CollationKey> prevKeyOut = new Output<CollationKey>();
    CollationKey prevKey;
    if (!getCollationKey(norm, fileLine, prevString, prevKeyOut)) {
        return false;
    }
    prevKey = prevKeyOut.value;
    Output<CollationKey> keyOut = new Output<CollationKey>();
    CollationKey key;
    if (!getCollationKey(norm, fileLine, s, keyOut)) {
        return false;
    }
    key = keyOut.value;
    int order = coll.compare(prevString, s);
    if (order != expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").compare(previous, current) wrong order: " + order + " != " + expectedOrder);
        return false;
    }
    order = coll.compare(s, prevString);
    if (order != -expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").compare(current, previous) wrong order: " + order + " != " + -expectedOrder);
        return false;
    }
    order = prevKey.compareTo(key);
    if (order != expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey(previous, current).compareTo() wrong order: " + order + " != " + expectedOrder);
        return false;
    }
    boolean collHasCaseLevel = ((RuleBasedCollator) coll).isCaseLevel();
    int level = getDifferenceLevel(prevKey, key, order, collHasCaseLevel);
    if (order != Collation.EQUAL && expectedLevel != Collation.NO_LEVEL) {
        if (level != expectedLevel) {
            logln(fileTestName);
            logln(prevFileLine);
            logln(fileLine);
            logln(printCollationKey(prevKey));
            logln(printCollationKey(key));
            errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey(previous, current).compareTo()=" + order + " wrong level: " + level + " != " + expectedLevel);
            return false;
        }
    }
    // If either string contains U+FFFE, then their sort keys must compare the same as
    // the merged sort keys of each string's between-FFFE segments.
    // 
    // It is not required that
    // sortkey(str1 + "\uFFFE" + str2) == mergeSortkeys(sortkey(str1), sortkey(str2))
    // only that those two methods yield the same order.
    // 
    // Use bit-wise OR so that getMergedCollationKey() is always called for both strings.
    Output<CollationKey> outPrevKey = new Output<CollationKey>(prevKey);
    Output<CollationKey> outKey = new Output<CollationKey>(key);
    if (getMergedCollationKey(prevString, outPrevKey) | getMergedCollationKey(s, outKey)) {
        prevKey = outPrevKey.value;
        key = outKey.value;
        order = prevKey.compareTo(key);
        if (order != expectedOrder) {
            logln(fileTestName);
            errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey" + "(previous, current segments between U+FFFE)).merge().compareTo() wrong order: " + order + " != " + expectedOrder);
            logln(prevFileLine);
            logln(fileLine);
            logln(printCollationKey(prevKey));
            logln(printCollationKey(key));
            return false;
        }
        int mergedLevel = getDifferenceLevel(prevKey, key, order, collHasCaseLevel);
        if (order != Collation.EQUAL && expectedLevel != Collation.NO_LEVEL) {
            if (mergedLevel != level) {
                logln(fileTestName);
                errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey" + "(previous, current segments between U+FFFE)).merge().compareTo()=" + order + " wrong level: " + mergedLevel + " != " + level);
                logln(prevFileLine);
                logln(fileLine);
                logln(printCollationKey(prevKey));
                logln(printCollationKey(key));
                return false;
            }
        }
    }
    return true;
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Output(android.icu.util.Output)

Example 3 with RuleBasedCollator

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

the class CollationTest method getCollationKey.

private boolean getCollationKey(String norm, String line, String s, Output<CollationKey> keyOut) {
    CollationKey key = coll.getCollationKey(s);
    keyOut.value = key;
    byte[] keyBytes = key.toByteArray();
    if (keyBytes.length == 0 || keyBytes[keyBytes.length - 1] != 0) {
        logln(fileTestName);
        logln(line);
        logln(printCollationKey(key));
        errln("Collator(" + norm + ").getCollationKey() wrote an empty or unterminated key");
        return false;
    }
    int numLevels = coll.getStrength();
    if (numLevels < Collator.IDENTICAL) {
        ++numLevels;
    } else {
        numLevels = 5;
    }
    if (((RuleBasedCollator) coll).isCaseLevel()) {
        ++numLevels;
    }
    int numLevelSeparators = 0;
    for (int i = 0; i < (keyBytes.length - 1); ++i) {
        byte b = keyBytes[i];
        if (b == 0) {
            logln(fileTestName);
            logln(line);
            logln(printCollationKey(key));
            errln("Collator(" + norm + ").getCollationKey() contains a 00 byte");
            return false;
        }
        if (b == 1) {
            ++numLevelSeparators;
        }
    }
    if (numLevelSeparators != (numLevels - 1)) {
        logln(fileTestName);
        logln(line);
        logln(printCollationKey(key));
        errln("Collator(" + norm + ").getCollationKey() has " + numLevelSeparators + " level separators for " + numLevels + " levels");
        return false;
    }
    return true;
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey)

Example 4 with RuleBasedCollator

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

the class CollationTest method parseAndSetAttribute.

private void parseAndSetAttribute() throws ParseException {
    // Parse attributes even if the Collator could not be created,
    // in order to report syntax errors.
    int start = skipSpaces(1);
    int equalPos = fileLine.indexOf('=');
    if (equalPos < 0) {
        if (fileLine.regionMatches(start, "reorder", 0, 7)) {
            parseAndSetReorderCodes(start + 7);
            return;
        }
        logln(fileLine);
        throw new ParseException("missing '=' on line " + fileLineNumber, fileLineNumber);
    }
    String attrString = fileLine.substring(start, equalPos);
    String valueString = fileLine.substring(equalPos + 1);
    if (attrString.equals("maxVariable")) {
        int max;
        if (valueString.equals("space")) {
            max = ReorderCodes.SPACE;
        } else if (valueString.equals("punct")) {
            max = ReorderCodes.PUNCTUATION;
        } else if (valueString.equals("symbol")) {
            max = ReorderCodes.SYMBOL;
        } else if (valueString.equals("currency")) {
            max = ReorderCodes.CURRENCY;
        } else {
            logln(fileLine);
            throw new ParseException("invalid attribute value name on line " + fileLineNumber, fileLineNumber);
        }
        if (coll != null) {
            coll.setMaxVariable(max);
        }
        fileLine = null;
        return;
    }
    boolean parsed = true;
    RuleBasedCollator rbc = (RuleBasedCollator) coll;
    if (attrString.equals("backwards")) {
        if (valueString.equals("on")) {
            if (rbc != null)
                rbc.setFrenchCollation(true);
        } else if (valueString.equals("off")) {
            if (rbc != null)
                rbc.setFrenchCollation(false);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setFrenchCollationDefault();
        } else {
            parsed = false;
        }
    } else if (attrString.equals("alternate")) {
        if (valueString.equals("non-ignorable")) {
            if (rbc != null)
                rbc.setAlternateHandlingShifted(false);
        } else if (valueString.equals("shifted")) {
            if (rbc != null)
                rbc.setAlternateHandlingShifted(true);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setAlternateHandlingDefault();
        } else {
            parsed = false;
        }
    } else if (attrString.equals("caseFirst")) {
        if (valueString.equals("upper")) {
            if (rbc != null)
                rbc.setUpperCaseFirst(true);
        } else if (valueString.equals("lower")) {
            if (rbc != null)
                rbc.setLowerCaseFirst(true);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setCaseFirstDefault();
        } else {
            parsed = false;
        }
    } else if (attrString.equals("caseLevel")) {
        if (valueString.equals("on")) {
            if (rbc != null)
                rbc.setCaseLevel(true);
        } else if (valueString.equals("off")) {
            if (rbc != null)
                rbc.setCaseLevel(false);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setCaseLevelDefault();
        } else {
            parsed = false;
        }
    } else if (attrString.equals("strength")) {
        if (valueString.equals("primary")) {
            if (rbc != null)
                rbc.setStrength(Collator.PRIMARY);
        } else if (valueString.equals("secondary")) {
            if (rbc != null)
                rbc.setStrength(Collator.SECONDARY);
        } else if (valueString.equals("tertiary")) {
            if (rbc != null)
                rbc.setStrength(Collator.TERTIARY);
        } else if (valueString.equals("quaternary")) {
            if (rbc != null)
                rbc.setStrength(Collator.QUATERNARY);
        } else if (valueString.equals("identical")) {
            if (rbc != null)
                rbc.setStrength(Collator.IDENTICAL);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setStrengthDefault();
        } else {
            parsed = false;
        }
    } else if (attrString.equals("numeric")) {
        if (valueString.equals("on")) {
            if (rbc != null)
                rbc.setNumericCollation(true);
        } else if (valueString.equals("off")) {
            if (rbc != null)
                rbc.setNumericCollation(false);
        } else if (valueString.equals("default")) {
            if (rbc != null)
                rbc.setNumericCollationDefault();
        } else {
            parsed = false;
        }
    } else {
        logln(fileLine);
        throw new ParseException("invalid attribute name on line " + fileLineNumber, fileLineNumber);
    }
    if (!parsed) {
        logln(fileLine);
        throw new ParseException("invalid attribute value name or attribute=value combination on line " + fileLineNumber, fileLineNumber);
    }
    fileLine = null;
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ParseException(java.text.ParseException)

Example 5 with RuleBasedCollator

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

the class G7CollationTest method TestDemo2.

// perorm test with added rules "& C < ch , cH, Ch, CH"
@Test
public void TestDemo2() {
    logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
    Collator col = Collator.getInstance(Locale.ENGLISH);
    String baseRules = ((RuleBasedCollator) col).getRules();
    String newRules = "& C < ch , cH, Ch, CH";
    newRules = baseRules + newRules;
    RuleBasedCollator myCollation = null;
    try {
        myCollation = new RuleBasedCollator(newRules);
    } catch (Exception e) {
        errln("Fail to create RuleBasedCollator with rules:" + newRules);
        return;
    }
    int j, n;
    for (j = 0; j < TOTALTESTSET; j++) {
        for (n = j + 1; n < TOTALTESTSET; n++) {
            doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Collator(android.icu.text.Collator) 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