Search in sources :

Example 1 with CollationKey

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

the class CollationTest method doTestVariant.

private static void doTestVariant(TestFmwk test, RuleBasedCollator myCollation, String source, String target, int result) {
    int compareResult = myCollation.compare(source, target);
    if (compareResult != result) {
        // !!! if not mod build, error, else nothing.
        // warnln if not build, error, else always print warning.
        // do we need a 'quiet warning?' (err or log).  Hmmm,
        // would it work to have the 'verbose' flag let you
        // suppress warnings?  Are there ever some warnings you
        // want to suppress, and others you don't?
        TestFmwk.errln("Comparing \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
    CollationKey ssk = myCollation.getCollationKey(source);
    CollationKey tsk = myCollation.getCollationKey(target);
    compareResult = ssk.compareTo(tsk);
    if (compareResult != result) {
        TestFmwk.errln("Comparing CollationKeys of \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
    RawCollationKey srsk = new RawCollationKey();
    myCollation.getRawCollationKey(source, srsk);
    RawCollationKey trsk = new RawCollationKey();
    myCollation.getRawCollationKey(target, trsk);
    compareResult = ssk.compareTo(tsk);
    if (compareResult != result) {
        TestFmwk.errln("Comparing RawCollationKeys of \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey)

Example 2 with CollationKey

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

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

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

the class CollationThaiTest method TestDictionary.

/**
 * Read the external dictionary file, which is already in proper
 * sorted order, and confirm that the collator compares each line as
 * preceding the following line.
 */
@Test
public void TestDictionary() {
    RuleBasedCollator coll = null;
    try {
        coll = getThaiCollator();
    } catch (Exception e) {
        warnln("could not construct Thai collator");
        return;
    }
    // Read in a dictionary of Thai words
    int line = 0;
    int failed = 0;
    int wordCount = 0;
    BufferedReader in = null;
    try {
        String fileName = "riwords.txt";
        in = TestUtil.getDataReader(fileName, "UTF-8");
        // 
        // Loop through each word in the dictionary and compare it to the previous
        // word. They should be in sorted order.
        // 
        String lastWord = "";
        String word = in.readLine();
        while (word != null) {
            line++;
            // Skip comments and blank lines
            if (word.length() == 0 || word.charAt(0) == 0x23) {
                word = in.readLine();
                continue;
            }
            // Show the first 8 words being compared, so we can see what's happening
            ++wordCount;
            if (wordCount <= 8) {
                logln("Word " + wordCount + ": " + word);
            }
            if (lastWord.length() > 0) {
                // CollationTest.doTest isn't really set up to handle situations where
                // the result can be equal or greater than the previous, so have to skip for now.
                // Not a big deal, since we're still testing to make sure everything sorts out
                // right, just not looking at the colation keys in detail...
                // CollationTest.doTest(this, coll, lastWord, word, -1);
                int result = coll.compare(lastWord, word);
                if (result > 0) {
                    failed++;
                    if (MAX_FAILURES_TO_SHOW < 0 || failed <= MAX_FAILURES_TO_SHOW) {
                        String msg = "--------------------------------------------\n" + line + " compare(" + lastWord + ", " + word + ") returned " + result + ", expected -1\n";
                        CollationKey k1, k2;
                        k1 = coll.getCollationKey(lastWord);
                        k2 = coll.getCollationKey(word);
                        msg += "key1: " + CollationTest.prettify(k1) + "\n" + "key2: " + CollationTest.prettify(k2);
                        errln(msg);
                    }
                }
            }
            lastWord = word;
            word = in.readLine();
        }
    } catch (IOException e) {
        errln("IOException " + e.getMessage());
    } finally {
        if (in == null) {
            errln("Error: could not open test file. Aborting test.");
            return;
        } else {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
    if (failed != 0) {
        if (failed > MAX_FAILURES_TO_SHOW) {
            errln("Too many failures; only the first " + MAX_FAILURES_TO_SHOW + " failures were shown");
        }
        errln("Summary: " + failed + " of " + (line - 1) + " comparisons failed");
    }
    logln("Words checked: " + wordCount);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with CollationKey

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

the class CollationTurkishTest method doTest.

// main test routine, tests rules specific to turkish locale
private void doTest(char[] source, char[] target, int result) {
    String s = new String(source);
    String t = new String(target);
    int compareResult = myCollation.compare(s, t);
    CollationKey sortKey1, sortKey2;
    sortKey1 = myCollation.getCollationKey(s);
    sortKey2 = myCollation.getCollationKey(t);
    int keyResult = sortKey1.compareTo(sortKey2);
    reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
}
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