Search in sources :

Example 1 with StringComparator

use of android.icu.text.UTF16.StringComparator in project j2objc by google.

the class BasicTest method TestCompare.

@Test
public void TestCompare() {
    // at least as many items as in strings[] !
    String[] s = new String[100];
    int i, j, k, count = strings.length;
    int result, refResult;
    // create the UnicodeStrings
    for (i = 0; i < count; ++i) {
        s[i] = Utility.unescape(strings[i]);
    }
    UTF16.StringComparator comp = new UTF16.StringComparator();
    // test them each with each other
    for (i = 0; i < count; ++i) {
        for (j = i; j < count; ++j) {
            for (k = 0; k < opt.length; ++k) {
                // test Normalizer::compare
                result = norm_compare(s[i], s[j], opt[k].options);
                refResult = ref_norm_compare(s[i], s[j], opt[k].options);
                if (sign(result) != sign(refResult)) {
                    errln("Normalizer::compare( " + i + ", " + j + ", " + k + "( " + opt[k].name + "))=" + result + " should be same sign as " + refResult);
                }
                // test UnicodeString::caseCompare - same internal implementation function
                if (0 != (opt[k].options & Normalizer.COMPARE_IGNORE_CASE)) {
                    // result=s[i]. (s[j], opt[k].options);
                    if ((opt[k].options & Normalizer.FOLD_CASE_EXCLUDE_SPECIAL_I) == 0) {
                        comp.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
                    } else {
                        comp.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
                    }
                    comp.setCodePointCompare((opt[k].options & Normalizer.COMPARE_CODE_POINT_ORDER) != 0);
                    // result=comp.caseCompare(s[i],s[j], opt[k].options);
                    result = comp.compare(s[i], s[j]);
                    refResult = ref_case_compare(s[i], s[j], opt[k].options);
                    if (sign(result) != sign(refResult)) {
                        errln("Normalizer::compare( " + i + ", " + j + ", " + k + "( " + opt[k].name + "))=" + result + " should be same sign as " + refResult);
                    }
                }
            }
        }
    }
    // test cases with i and I to make sure Turkic works
    char[] iI = new char[] { 0x49, 0x69, 0x130, 0x131 };
    UnicodeSet set = new UnicodeSet(), iSet = new UnicodeSet();
    Normalizer2Impl nfcImpl = Norm2AllModes.getNFCInstance().impl;
    nfcImpl.ensureCanonIterData();
    String s1, s2;
    // collect all sets into one for contiguous output
    for (i = 0; i < iI.length; ++i) {
        if (nfcImpl.getCanonStartSet(iI[i], iSet)) {
            set.addAll(iSet);
        }
    }
    // test all of these precomposed characters
    Normalizer2 nfcNorm2 = Normalizer2.getNFCInstance();
    UnicodeSetIterator it = new UnicodeSetIterator(set);
    int c;
    while (it.next() && (c = it.codepoint) != UnicodeSetIterator.IS_STRING) {
        s1 = UTF16.valueOf(c);
        s2 = nfcNorm2.getDecomposition(c);
        for (k = 0; k < opt.length; ++k) {
            // test Normalizer::compare
            result = norm_compare(s1, s2, opt[k].options);
            refResult = ref_norm_compare(s1, s2, opt[k].options);
            if (sign(result) != sign(refResult)) {
                errln("Normalizer.compare(U+" + hex(c) + " with its NFD, " + opt[k].name + ")" + signString(result) + " should be " + signString(refResult));
            }
            // test UnicodeString::caseCompare - same internal implementation function
            if ((opt[k].options & Normalizer.COMPARE_IGNORE_CASE) > 0) {
                if ((opt[k].options & Normalizer.FOLD_CASE_EXCLUDE_SPECIAL_I) == 0) {
                    comp.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
                } else {
                    comp.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
                }
                comp.setCodePointCompare((opt[k].options & Normalizer.COMPARE_CODE_POINT_ORDER) != 0);
                result = comp.compare(s1, s2);
                refResult = ref_case_compare(s1, s2, opt[k].options);
                if (sign(result) != sign(refResult)) {
                    errln("UTF16.compare(U+" + hex(c) + " with its NFD, " + opt[k].name + ")" + signString(result) + " should be " + signString(refResult));
                }
            }
        }
    }
    // test getDecomposition() for some characters that do not decompose
    if (nfcNorm2.getDecomposition(0x20) != null || nfcNorm2.getDecomposition(0x4e00) != null || nfcNorm2.getDecomposition(0x20002) != null) {
        errln("NFC.getDecomposition() returns TRUE for characters which do not have decompositions");
    }
    // test getRawDecomposition() for some characters that do not decompose
    if (nfcNorm2.getRawDecomposition(0x20) != null || nfcNorm2.getRawDecomposition(0x4e00) != null || nfcNorm2.getRawDecomposition(0x20002) != null) {
        errln("getRawDecomposition() returns TRUE for characters which do not have decompositions");
    }
    // test composePair() for some pairs of characters that do not compose
    if (nfcNorm2.composePair(0x20, 0x301) >= 0 || nfcNorm2.composePair(0x61, 0x305) >= 0 || nfcNorm2.composePair(0x1100, 0x1160) >= 0 || nfcNorm2.composePair(0xac00, 0x11a7) >= 0) {
        errln("NFC.composePair() incorrectly composes some pairs of characters");
    }
    // test FilteredNormalizer2.getDecomposition()
    UnicodeSet filter = new UnicodeSet("[^\u00a0-\u00ff]");
    FilteredNormalizer2 fn2 = new FilteredNormalizer2(nfcNorm2, filter);
    if (fn2.getDecomposition(0xe4) != null || !"A\u0304".equals(fn2.getDecomposition(0x100))) {
        errln("FilteredNormalizer2(NFC, ^A0-FF).getDecomposition() failed");
    }
    // test FilteredNormalizer2.getRawDecomposition()
    if (fn2.getRawDecomposition(0xe4) != null || !"A\u0304".equals(fn2.getRawDecomposition(0x100))) {
        errln("FilteredNormalizer2(NFC, ^A0-FF).getRawDecomposition() failed");
    }
    // test FilteredNormalizer2::composePair()
    if (0x100 != fn2.composePair(0x41, 0x304) || // unfiltered result: U+1E08
    fn2.composePair(0xc7, 0x301) >= 0) {
        errln("FilteredNormalizer2(NFC, ^A0-FF).composePair() failed");
    }
}
Also used : UnicodeSetIterator(android.icu.text.UnicodeSetIterator) FilteredNormalizer2(android.icu.text.FilteredNormalizer2) Normalizer2(android.icu.text.Normalizer2) FilteredNormalizer2(android.icu.text.FilteredNormalizer2) UTF16(android.icu.text.UTF16) Normalizer2Impl(android.icu.impl.Normalizer2Impl) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 2 with StringComparator

use of android.icu.text.UTF16.StringComparator in project j2objc by google.

the class UTF16Test method TestStringComparator.

/**
 * Testing the setter and getter apis for StringComparator
 */
@Test
public void TestStringComparator() {
    UTF16.StringComparator compare = new UTF16.StringComparator();
    if (compare.getCodePointCompare() != false) {
        errln("Default string comparator should be code unit compare");
    }
    if (compare.getIgnoreCase() != false) {
        errln("Default string comparator should be case sensitive compare");
    }
    if (compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
        errln("Default string comparator should have fold case default compare");
    }
    compare.setCodePointCompare(true);
    if (compare.getCodePointCompare() != true) {
        errln("Error setting code point compare");
    }
    compare.setCodePointCompare(false);
    if (compare.getCodePointCompare() != false) {
        errln("Error setting code point compare");
    }
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    if (compare.getIgnoreCase() != true || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
        errln("Error setting ignore case and options");
    }
    compare.setIgnoreCase(false, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
    if (compare.getIgnoreCase() != false || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) {
        errln("Error setting ignore case and options");
    }
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
    if (compare.getIgnoreCase() != true || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) {
        errln("Error setting ignore case and options");
    }
    compare.setIgnoreCase(false, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    if (compare.getIgnoreCase() != false || compare.getIgnoreCaseOption() != UTF16.StringComparator.FOLD_CASE_DEFAULT) {
        errln("Error setting ignore case and options");
    }
}
Also used : StringComparator(android.icu.text.UTF16.StringComparator) UTF16(android.icu.text.UTF16) StringComparator(android.icu.text.UTF16.StringComparator) Test(org.junit.Test)

Example 3 with StringComparator

use of android.icu.text.UTF16.StringComparator in project j2objc by google.

the class UTF16Test method TestUtilities.

@Test
public void TestUtilities() {
    String[] tests = { "a", "\uFFFF", "😀", "\uD800", "\uDC00", "\uDBFF\uDfff", "", "\u0000", "\uDC00\uD800", "ab", "😀a", null };
    StringComparator sc = new UTF16.StringComparator(true, false, 0);
    for (String item1 : tests) {
        String nonNull1 = item1 == null ? "" : item1;
        int count = UTF16.countCodePoint(nonNull1);
        int expected = count == 0 || count > 1 ? -1 : nonNull1.codePointAt(0);
        assertEquals("codepoint test " + Utility.hex(nonNull1), expected, UTF16.getSingleCodePoint(item1));
        if (expected == -1) {
            continue;
        }
        for (String item2 : tests) {
            String nonNull2 = item2 == null ? "" : item2;
            int scValue = Integer.signum(sc.compare(nonNull1, nonNull2));
            int fValue = Integer.signum(UTF16.compareCodePoint(expected, item2));
            assertEquals("comparison " + Utility.hex(nonNull1) + ", " + Utility.hex(nonNull2), scValue, fValue);
        }
    }
}
Also used : ReplaceableString(android.icu.text.ReplaceableString) StringComparator(android.icu.text.UTF16.StringComparator) Test(org.junit.Test)

Example 4 with StringComparator

use of android.icu.text.UTF16.StringComparator in project j2objc by google.

the class UTF16Test method TestCaseCompare.

@Test
public void TestCaseCompare() {
    String mixed = "\u0061\u0042\u0131\u03a3\u00df\ufb03\ud93f\udfff";
    String otherDefault = "\u0041\u0062\u0131\u03c3\u0073\u0053\u0046\u0066\u0049\ud93f\udfff";
    String otherExcludeSpecialI = "\u0041\u0062\u0131\u03c3\u0053\u0073\u0066\u0046\u0069\ud93f\udfff";
    String different = "\u0041\u0062\u0131\u03c3\u0073\u0053\u0046\u0066\u0049\ud93f\udffd";
    UTF16.StringComparator compare = new UTF16.StringComparator();
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    // test u_strcasecmp()
    int result = compare.compare(mixed, otherDefault);
    if (result != 0) {
        errln("error: default compare(mixed, other) = " + result + " instead of 0");
    }
    // test u_strcasecmp() - exclude special i
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I);
    result = compare.compare(mixed, otherExcludeSpecialI);
    if (result != 0) {
        errln("error: exclude_i compare(mixed, other) = " + result + " instead of 0");
    }
    // test u_strcasecmp()
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    result = compare.compare(mixed, different);
    if (result <= 0) {
        errln("error: default compare(mixed, different) = " + result + " instead of positive");
    }
    // test substrings - stop before the sharp s (U+00df)
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    result = compare.compare(mixed.substring(0, 4), different.substring(0, 4));
    if (result != 0) {
        errln("error: default compare(mixed substring, different substring) = " + result + " instead of 0");
    }
    // test substrings - stop in the middle of the sharp s (U+00df)
    compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    result = compare.compare(mixed.substring(0, 5), different.substring(0, 5));
    if (result <= 0) {
        errln("error: default compare(mixed substring, different substring) = " + result + " instead of positive");
    }
}
Also used : StringComparator(android.icu.text.UTF16.StringComparator) UTF16(android.icu.text.UTF16) ReplaceableString(android.icu.text.ReplaceableString) StringComparator(android.icu.text.UTF16.StringComparator) Test(org.junit.Test)

Example 5 with StringComparator

use of android.icu.text.UTF16.StringComparator in project j2objc by google.

the class UTF16Test method TestCodePointCompare.

@Test
public void TestCodePointCompare() {
    // these strings are in ascending order
    String[] str = { "\u0061", "\u20ac\ud801", "\u20ac\ud800\udc00", "\ud800", "\ud800\uff61", "\udfff", "\uff61\udfff", "\uff61\ud800\udc02", "\ud800\udc02", "\ud84d\udc56" };
    UTF16.StringComparator cpcompare = new UTF16.StringComparator(true, false, UTF16.StringComparator.FOLD_CASE_DEFAULT);
    UTF16.StringComparator cucompare = new UTF16.StringComparator();
    for (int i = 0; i < str.length - 1; ++i) {
        if (cpcompare.compare(str[i], str[i + 1]) >= 0) {
            errln("error: compare() in code point order fails for string " + Utility.hex(str[i]) + " and " + Utility.hex(str[i + 1]));
        }
        // test code unit compare
        if (cucompare.compare(str[i], str[i + 1]) != str[i].compareTo(str[i + 1])) {
            errln("error: compare() in code unit order fails for string " + Utility.hex(str[i]) + " and " + Utility.hex(str[i + 1]));
        }
    }
}
Also used : StringComparator(android.icu.text.UTF16.StringComparator) UTF16(android.icu.text.UTF16) ReplaceableString(android.icu.text.ReplaceableString) StringComparator(android.icu.text.UTF16.StringComparator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 UTF16 (android.icu.text.UTF16)4 StringComparator (android.icu.text.UTF16.StringComparator)4 ReplaceableString (android.icu.text.ReplaceableString)3 Normalizer2Impl (android.icu.impl.Normalizer2Impl)1 FilteredNormalizer2 (android.icu.text.FilteredNormalizer2)1 Normalizer2 (android.icu.text.Normalizer2)1 UnicodeSet (android.icu.text.UnicodeSet)1 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)1