Search in sources :

Example 41 with UnicodeSet

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

the class UnicodeSetTest method TestFrozen.

/**
 * Test that frozen classes disallow changes. For 4217
 */
@Test
public void TestFrozen() {
    UnicodeSet test = new UnicodeSet("[[:whitespace:]A]");
    test.freeze();
    checkModification(test, true);
    checkModification(test, false);
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 42 with UnicodeSet

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

the class UnicodeSetTest method TestRangeConstructor.

@Test
public void TestRangeConstructor() {
    UnicodeSet w = new UnicodeSet().addAll(3, 5);
    UnicodeSet s = new UnicodeSet(3, 5);
    assertEquals("new constructor", w, s);
    w = new UnicodeSet().addAll(3, 5).addAll(7, 7);
    UnicodeSet t = new UnicodeSet(3, 5, 7, 7);
    assertEquals("new constructor", w, t);
    // check to make sure right exceptions are thrown
    Class expected = IllegalArgumentException.class;
    Class actual;
    try {
        actual = null;
        @SuppressWarnings("unused") UnicodeSet u = new UnicodeSet(5);
    } catch (IllegalArgumentException e) {
        actual = e.getClass();
    }
    assertEquals("exception if odd", expected, actual);
    try {
        actual = null;
        @SuppressWarnings("unused") UnicodeSet u = new UnicodeSet(3, 2, 7, 9);
    } catch (IllegalArgumentException e) {
        actual = e.getClass();
    }
    assertEquals("exception for start/end problem", expected, actual);
    try {
        actual = null;
        @SuppressWarnings("unused") UnicodeSet u = new UnicodeSet(3, 5, 6, 9);
    } catch (IllegalArgumentException e) {
        actual = e.getClass();
    }
    assertEquals("exception for end/start problem", expected, actual);
    CheckRangeSpeed(10000, new UnicodeSet("[:whitespace:]"));
    CheckRangeSpeed(1000, new UnicodeSet("[:letter:]"));
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 43 with UnicodeSet

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

the class UnicodeSetTest method TestPatterns.

@Test
public void TestPatterns() {
    UnicodeSet set = new UnicodeSet();
    expectPattern(set, "[[a-m]&[d-z]&[k-y]]", "km");
    expectPattern(set, "[[a-z]-[m-y]-[d-r]]", "aczz");
    expectPattern(set, "[a\\-z]", "--aazz");
    expectPattern(set, "[-az]", "--aazz");
    expectPattern(set, "[az-]", "--aazz");
    expectPattern(set, "[[[a-z]-[aeiou]i]]", "bdfnptvz");
    // Throw in a test of complement
    set.complement();
    String exp = '\u0000' + "aeeoouu" + (char) ('z' + 1) + '\uFFFF';
    expectPairs(set, exp);
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 44 with UnicodeSet

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

the class UnicodeSetTest method TestEscapePattern.

@Test
public void TestEscapePattern() {
    // The following pattern must contain at least one range "c-d"
    // where c or d is a Pattern_White_Space.
    String pattern = "[\\uFEFF \\u200E-\\u20FF \\uFFF9-\\uFFFC \\U0001D173-\\U0001D17A \\U000F0000-\\U000FFFFD ]";
    String exp = "[\\u200E-\\u20FF\\uFEFF\\uFFF9-\\uFFFC\\U0001D173-\\U0001D17A\\U000F0000-\\U000FFFFD]";
    // this fails -- which is what we expect.
    for (int pass = 1; pass <= 2; ++pass) {
        String pat = pattern;
        if (pass == 2) {
            pat = Utility.unescape(pat);
        }
        // Pattern is only good for pass 1
        boolean isPatternValid = (pass == 1);
        UnicodeSet set = null;
        try {
            set = new UnicodeSet(pat);
        } catch (IllegalArgumentException e) {
            set = null;
        }
        if ((set != null) != isPatternValid) {
            errln("FAIL: applyPattern(" + Utility.escape(pat) + ") => " + set);
            continue;
        }
        if (set == null) {
            continue;
        }
        if (set.contains((char) 0x0644)) {
            errln("FAIL: " + Utility.escape(pat) + " contains(U+0664)");
        }
        String newpat = set.toPattern(true);
        if (newpat.equals(exp)) {
            logln(Utility.escape(pat) + " => " + newpat);
        } else {
            errln("FAIL: " + Utility.escape(pat) + " => " + newpat);
        }
        for (int i = 0; i < set.getRangeCount(); ++i) {
            StringBuffer str = new StringBuffer("Range ");
            str.append((char) (0x30 + i)).append(": ");
            UTF16.append(str, set.getRangeStart(i));
            str.append(" - ");
            UTF16.append(str, set.getRangeEnd(i));
            String s = Utility.escape(str.toString() + " (" + set.getRangeStart(i) + " - " + set.getRangeEnd(i) + ")");
            if (set.getRangeStart(i) < 0) {
                errln("FAIL: " + s);
            } else {
                logln(s);
            }
        }
    }
}
Also used : UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 45 with UnicodeSet

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

the class UnicodeSetTest method TestCharSequenceArgs.

@Test
public void TestCharSequenceArgs() {
    // statics
    assertEquals("CharSequence from", new UnicodeSet("[{abc}]"), UnicodeSet.from(new StringBuilder("abc")));
    assertEquals("CharSequence fromAll", new UnicodeSet("[a-c]"), UnicodeSet.fromAll(new StringBuilder("abc")));
    assertEquals("CharSequence compare", 1.0f, Math.signum(UnicodeSet.compare(new StringBuilder("abc"), 0x61)));
    assertEquals("CharSequence compare", -1.0f, Math.signum(UnicodeSet.compare(0x61, new StringBuilder("abc"))));
    assertEquals("CharSequence compare", 0.0f, Math.signum(UnicodeSet.compare(new StringBuilder("a"), 0x61)));
    assertEquals("CharSequence compare", 0.0f, Math.signum(UnicodeSet.compare(0x61, new StringBuilder("a"))));
    assertEquals("CharSequence getSingleCodePoint", 0x1F466, UnicodeSet.getSingleCodePoint(new StringBuilder("👦")));
    // iterables/arrays
    Iterable<StringBuilder> iterable = Arrays.asList(new StringBuilder("A"), new StringBuilder("B"));
    assertEquals("CharSequence containsAll", true, new UnicodeSet("[AB]").containsAll(iterable));
    assertEquals("CharSequence containsAll", false, new UnicodeSet("[a-cA]").containsAll(iterable));
    assertEquals("CharSequence containsNone", true, new UnicodeSet("[a-c]").containsNone(iterable));
    assertEquals("CharSequence containsNone", false, new UnicodeSet("[a-cA]").containsNone(iterable));
    assertEquals("CharSequence containsSome", true, new UnicodeSet("[a-cA]").containsSome(iterable));
    assertEquals("CharSequence containsSome", false, new UnicodeSet("[a-c]").containsSome(iterable));
    assertEquals("CharSequence addAll", new UnicodeSet("[a-cAB]"), new UnicodeSet("[a-cA]").addAll(new StringBuilder("A"), new StringBuilder("B")));
    assertEquals("CharSequence removeAll", new UnicodeSet("[a-c]"), new UnicodeSet("[a-cA]").removeAll(iterable));
    assertEquals("CharSequence retainAll", new UnicodeSet("[A]"), new UnicodeSet("[a-cA]").retainAll(iterable));
    // UnicodeSet results
    assertEquals("CharSequence add", new UnicodeSet("[Aa-c{abc}{qr}]"), new UnicodeSet("[a-cA{qr}]").add(new StringBuilder("abc")));
    assertEquals("CharSequence retain", new UnicodeSet("[{abc}]"), new UnicodeSet("[a-cA{abc}{qr}]").retain(new StringBuilder("abc")));
    assertEquals("CharSequence remove", new UnicodeSet("[Aa-c{qr}]"), new UnicodeSet("[a-cA{abc}{qr}]").remove(new StringBuilder("abc")));
    assertEquals("CharSequence complement", new UnicodeSet("[Aa-c{qr}]"), new UnicodeSet("[a-cA{abc}{qr}]").complement(new StringBuilder("abc")));
    assertEquals("CharSequence complement", new UnicodeSet("[Aa-c{abc}{qr}]"), new UnicodeSet("[a-cA{qr}]").complement(new StringBuilder("abc")));
    assertEquals("CharSequence addAll", new UnicodeSet("[a-cABC]"), new UnicodeSet("[a-cA]").addAll(new StringBuilder("ABC")));
    assertEquals("CharSequence retainAll", new UnicodeSet("[a-c]"), new UnicodeSet("[a-cA]").retainAll(new StringBuilder("abcB")));
    assertEquals("CharSequence removeAll", new UnicodeSet("[Aab]"), new UnicodeSet("[a-cA]").removeAll(new StringBuilder("cC")));
    assertEquals("CharSequence complementAll", new UnicodeSet("[ABbc]"), new UnicodeSet("[a-cA]").complementAll(new StringBuilder("aB")));
    // containment
    assertEquals("CharSequence contains", true, new UnicodeSet("[a-cA{ab}]").contains(new StringBuilder("ab")));
    assertEquals("CharSequence containsNone", false, new UnicodeSet("[a-cA]").containsNone(new StringBuilder("ab")));
    assertEquals("CharSequence containsSome", true, new UnicodeSet("[a-cA{ab}]").containsSome(new StringBuilder("ab")));
    // spanning
    assertEquals("CharSequence span", 3, new UnicodeSet("[a-cA]").span(new StringBuilder("abc"), SpanCondition.SIMPLE));
    assertEquals("CharSequence span", 3, new UnicodeSet("[a-cA]").span(new StringBuilder("abc"), 1, SpanCondition.SIMPLE));
    assertEquals("CharSequence spanBack", 0, new UnicodeSet("[a-cA]").spanBack(new StringBuilder("abc"), SpanCondition.SIMPLE));
    assertEquals("CharSequence spanBack", 0, new UnicodeSet("[a-cA]").spanBack(new StringBuilder("abc"), 1, SpanCondition.SIMPLE));
    // internal
    OutputInt outCount = new OutputInt();
    assertEquals("CharSequence matchesAt", 2, new UnicodeSet("[a-cA]").matchesAt(new StringBuilder("abc"), 1));
    assertEquals("CharSequence spanAndCount", 3, new UnicodeSet("[a-cA]").spanAndCount(new StringBuilder("abc"), 1, SpanCondition.SIMPLE, outCount));
    assertEquals("CharSequence findIn", 3, new UnicodeSet("[a-cA]").findIn(new StringBuilder("abc"), 1, true));
    assertEquals("CharSequence findLastIn", -1, new UnicodeSet("[a-cA]").findLastIn(new StringBuilder("abc"), 1, true));
    assertEquals("CharSequence add", "c", new UnicodeSet("[abA]").stripFrom(new StringBuilder("abc"), true));
}
Also used : OutputInt(android.icu.util.OutputInt) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Aggregations

UnicodeSet (android.icu.text.UnicodeSet)158 Test (org.junit.Test)112 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)25 Transliterator (android.icu.text.Transliterator)19 ReplaceableString (android.icu.text.ReplaceableString)14 ULocale (android.icu.util.ULocale)13 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)9 Normalizer2 (android.icu.text.Normalizer2)7 RuleBasedCollator (android.icu.text.RuleBasedCollator)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 FilteredNormalizer2 (android.icu.text.FilteredNormalizer2)4 SpoofChecker (android.icu.text.SpoofChecker)4 TreeSet (java.util.TreeSet)4 UnicodeMap (android.icu.dev.util.UnicodeMap)3 AlphabeticIndex (android.icu.text.AlphabeticIndex)3 CollationKey (android.icu.text.CollationKey)3 RawCollationKey (android.icu.text.RawCollationKey)3 CheckResult (android.icu.text.SpoofChecker.CheckResult)3 SpanCondition (android.icu.text.UnicodeSet.SpanCondition)3