Search in sources :

Example 1 with CollationRootElements

use of android.icu.impl.coll.CollationRootElements in project j2objc by google.

the class CollationTest method TestTailoredElements.

@Test
public void TestTailoredElements() {
    CollationData root = CollationRoot.getData();
    CollationRootElements rootElements = new CollationRootElements(root.rootElements);
    Set<String> prevLocales = new HashSet<String>();
    prevLocales.add("");
    prevLocales.add("root");
    prevLocales.add("root@collation=standard");
    long[] ces;
    ULocale[] locales = Collator.getAvailableULocales();
    String localeID = "root";
    int locIdx = 0;
    for (; locIdx < locales.length; localeID = locales[locIdx++].getName()) {
        ULocale locale = new ULocale(localeID);
        String[] types = Collator.getKeywordValuesForLocale("collation", locale, false);
        for (int typeIdx = 0; typeIdx < types.length; ++typeIdx) {
            // first: default type
            String type = types[typeIdx];
            if (type.startsWith("private-")) {
                errln("Collator.getKeywordValuesForLocale(" + localeID + ") returns private collation keyword: " + type);
            }
            ULocale localeWithType = locale.setKeywordValue("collation", type);
            Collator coll = Collator.getInstance(localeWithType);
            ULocale actual = coll.getLocale(ULocale.ACTUAL_LOCALE);
            if (prevLocales.contains(actual.getName())) {
                continue;
            }
            prevLocales.add(actual.getName());
            logln("TestTailoredElements(): requested " + localeWithType.getName() + " -> actual " + actual.getName());
            if (!(coll instanceof RuleBasedCollator)) {
                continue;
            }
            RuleBasedCollator rbc = (RuleBasedCollator) coll;
            // Note: It would be better to get tailored strings such that we can
            // identify the prefix, and only get the CEs for the prefix+string,
            // not also for the prefix.
            // There is currently no API for that.
            // It would help in an unusual case where a contraction starting in the prefix
            // extends past its end, and we do not see the intended mapping.
            // For example, for a mapping p|st, if there is also a contraction ps,
            // then we get CEs(ps)+CEs(t), rather than CEs(p|st).
            UnicodeSet tailored = coll.getTailoredSet();
            UnicodeSetIterator iter = new UnicodeSetIterator(tailored);
            while (iter.next()) {
                String s = iter.getString();
                ces = rbc.internalGetCEs(s);
                for (int i = 0; i < ces.length; ++i) {
                    long ce = ces[i];
                    if (!isValidCE(rootElements, root, ce)) {
                        logln(prettify(s));
                        errln("invalid tailored CE 0x" + Utility.hex(ce, 16) + " at CE index " + i + " from string:");
                    }
                }
            }
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationRootElements(android.icu.impl.coll.CollationRootElements) UnicodeSetIterator(android.icu.text.UnicodeSetIterator) CollationData(android.icu.impl.coll.CollationData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with CollationRootElements

use of android.icu.impl.coll.CollationRootElements in project j2objc by google.

the class CollationTest method TestRootElements.

@Test
public void TestRootElements() {
    CollationData root = CollationRoot.getData();
    CollationRootElements rootElements = new CollationRootElements(root.rootElements);
    RootElementsIterator iter = new RootElementsIterator(root);
    // We check each root CE for validity,
    // and we also verify that there is a tailoring gap between each two CEs.
    // compressible primary weights
    CollationWeights cw1c = new CollationWeights();
    // uncompressible primary weights
    CollationWeights cw1u = new CollationWeights();
    CollationWeights cw2 = new CollationWeights();
    CollationWeights cw3 = new CollationWeights();
    cw1c.initForPrimary(true);
    cw1u.initForPrimary(false);
    cw2.initForSecondary();
    cw3.initForTertiary();
    // Note: The root elements do not include Han-implicit or unassigned-implicit CEs,
    // nor the special merge-separator CE for U+FFFE.
    long prevPri = 0;
    long prevSec = 0;
    long prevTer = 0;
    while (iter.next()) {
        long pri = iter.getPrimary();
        long secTer = iter.getSecTer();
        // CollationRootElements CEs must have 0 case and quaternary bits.
        if ((secTer & Collation.CASE_AND_QUATERNARY_MASK) != 0) {
            errln("CollationRootElements CE has non-zero case and/or quaternary bits: " + "0x" + Utility.hex(pri, 8) + " 0x" + Utility.hex(secTer, 8));
        }
        long sec = secTer >>> 16;
        long ter = secTer & Collation.ONLY_TERTIARY_MASK;
        long ctq = ter;
        if (pri == 0 && sec == 0 && ter != 0) {
            // Tertiary CEs must have uppercase bits,
            // but they are not stored in the CollationRootElements.
            ctq |= 0x8000;
        }
        if (!isValidCE(rootElements, root, pri, sec, ctq)) {
            errln("invalid root CE 0x" + Utility.hex(pri, 8) + " 0x" + Utility.hex(secTer, 8));
        } else {
            if (pri != prevPri) {
                long newWeight = 0;
                if (prevPri == 0 || prevPri >= Collation.FFFD_PRIMARY) {
                // There is currently no tailoring gap after primary ignorables,
                // and we forbid tailoring after U+FFFD and U+FFFF.
                } else if (root.isCompressiblePrimary(prevPri)) {
                    if (!cw1c.allocWeights(prevPri, pri, 1)) {
                        errln("no primary/compressible tailoring gap between " + "0x" + Utility.hex(prevPri, 8) + " and 0x" + Utility.hex(pri, 8));
                    } else {
                        newWeight = cw1c.nextWeight();
                    }
                } else {
                    if (!cw1u.allocWeights(prevPri, pri, 1)) {
                        errln("no primary/uncompressible tailoring gap between " + "0x" + Utility.hex(prevPri, 8) + " and 0x" + Utility.hex(pri, 8));
                    } else {
                        newWeight = cw1u.nextWeight();
                    }
                }
                if (newWeight != 0 && !(prevPri < newWeight && newWeight < pri)) {
                    errln("mis-allocated primary weight, should get " + "0x" + Utility.hex(prevPri, 8) + " < 0x" + Utility.hex(newWeight, 8) + " < 0x" + Utility.hex(pri, 8));
                }
            } else if (sec != prevSec) {
                long lowerLimit = prevSec == 0 ? rootElements.getSecondaryBoundary() - 0x100 : prevSec;
                if (!cw2.allocWeights(lowerLimit, sec, 1)) {
                    errln("no secondary tailoring gap between " + "0x" + Utility.hex(lowerLimit) + " and 0x" + Utility.hex(sec));
                } else {
                    long newWeight = cw2.nextWeight();
                    if (!(prevSec < newWeight && newWeight < sec)) {
                        errln("mis-allocated secondary weight, should get " + "0x" + Utility.hex(lowerLimit) + " < 0x" + Utility.hex(newWeight) + " < 0x" + Utility.hex(sec));
                    }
                }
            } else if (ter != prevTer) {
                long lowerLimit = prevTer == 0 ? rootElements.getTertiaryBoundary() - 0x100 : prevTer;
                if (!cw3.allocWeights(lowerLimit, ter, 1)) {
                    errln("no tertiary tailoring gap between " + "0x" + Utility.hex(lowerLimit) + " and 0x" + Utility.hex(ter));
                } else {
                    long newWeight = cw3.nextWeight();
                    if (!(prevTer < newWeight && newWeight < ter)) {
                        errln("mis-allocated tertiary weight, should get " + "0x" + Utility.hex(lowerLimit) + " < 0x" + Utility.hex(newWeight) + " < 0x" + Utility.hex(ter));
                    }
                }
            } else {
                errln("duplicate root CE 0x" + Utility.hex(pri, 8) + " 0x" + Utility.hex(secTer, 8));
            }
        }
        prevPri = pri;
        prevSec = sec;
        prevTer = ter;
    }
}
Also used : CollationRootElements(android.icu.impl.coll.CollationRootElements) CollationData(android.icu.impl.coll.CollationData) CollationWeights(android.icu.impl.coll.CollationWeights) Test(org.junit.Test)

Aggregations

CollationData (android.icu.impl.coll.CollationData)2 CollationRootElements (android.icu.impl.coll.CollationRootElements)2 Test (org.junit.Test)2 CollationWeights (android.icu.impl.coll.CollationWeights)1 Collator (android.icu.text.Collator)1 RuleBasedCollator (android.icu.text.RuleBasedCollator)1 UnicodeSet (android.icu.text.UnicodeSet)1 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)1 ULocale (android.icu.util.ULocale)1 HashSet (java.util.HashSet)1