Search in sources :

Example 6 with RawCollationKey

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

the class UCAConformanceTest method conformanceTest.

private void conformanceTest(RuleBasedCollator coll) {
    if (in == null || coll == null) {
        return;
    }
    int skipFlags = 0;
    if (coll.isAlternateHandlingShifted()) {
        skipFlags |= IS_SHIFTED;
    }
    if (coll == rbUCA) {
        skipFlags |= FROM_RULES;
    }
    logln("-prop:ucaconfnosortkeys=1 turns off getSortKey() in UCAConformanceTest");
    boolean withSortKeys = getProperty("ucaconfnosortkeys") == null;
    int lineNo = 0;
    String line = null, oldLine = null, buffer = null, oldB = null;
    RawCollationKey sk1 = new RawCollationKey(), sk2 = new RawCollationKey();
    RawCollationKey oldSk = null, newSk = sk1;
    try {
        while ((line = in.readLine()) != null) {
            lineNo++;
            if (line.length() == 0 || line.charAt(0) == '#') {
                continue;
            }
            buffer = parseString(line);
            if (skipLineBecauseOfBug(buffer, skipFlags)) {
                logln("Skipping line " + lineNo + " because of a known bug");
                continue;
            }
            if (withSortKeys) {
                coll.getRawCollationKey(buffer, newSk);
            }
            if (oldSk != null) {
                boolean ok = true;
                int skres = withSortKeys ? oldSk.compareTo(newSk) : 0;
                int cmpres = coll.compare(oldB, buffer);
                int cmpres2 = coll.compare(buffer, oldB);
                if (cmpres != -cmpres2) {
                    errln(String.format("Compare result not symmetrical on line %d: " + "previous vs. current (%d) / current vs. previous (%d)", lineNo, cmpres, cmpres2));
                    ok = false;
                }
                if (withSortKeys && cmpres != normalizeResult(skres)) {
                    errln("Difference between coll.compare (" + cmpres + ") and sortkey compare (" + skres + ") on line " + lineNo);
                    ok = false;
                }
                int res = cmpres;
                if (res == 0 && !isAtLeastUCA62) {
                    // Up to UCA 6.1, the collation test files use a custom tie-breaker,
                    // comparing the raw input strings.
                    res = comparer.compare(oldB, buffer);
                // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
                // comparing the NFD versions of the input strings,
                // which we do via setting strength=identical.
                }
                if (res > 0) {
                    errln("Line " + lineNo + " is not greater or equal than previous line");
                    ok = false;
                }
                if (!ok) {
                    errln("  Previous data line " + oldLine);
                    errln("  Current data line  " + line);
                    if (withSortKeys) {
                        errln("  Previous key: " + CollationTest.prettify(oldSk));
                        errln("  Current key:  " + CollationTest.prettify(newSk));
                    }
                }
            }
            oldSk = newSk;
            oldB = buffer;
            oldLine = line;
            if (oldSk == sk1) {
                newSk = sk2;
            } else {
                newSk = sk1;
            }
        }
    } catch (Exception e) {
        errln("Unexpected exception " + e);
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
        in = null;
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

RawCollationKey (android.icu.text.RawCollationKey)6 Test (org.junit.Test)4 CollationKey (android.icu.text.CollationKey)2 RuleBasedCollator (android.icu.text.RuleBasedCollator)2 UnicodeSet (android.icu.text.UnicodeSet)2 ULocale (android.icu.util.ULocale)2 R4 (android.icu.impl.Row.R4)1 AlphabeticIndex (android.icu.text.AlphabeticIndex)1 Collator (android.icu.text.Collator)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1