Search in sources :

Example 1 with UnicodeSet

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

the class UnicodeSetTest method TestAPI.

@Test
public void TestAPI() {
    // default ct
    UnicodeSet set = new UnicodeSet();
    if (!set.isEmpty() || set.getRangeCount() != 0) {
        errln("FAIL, set should be empty but isn't: " + set);
    }
    // clear(), isEmpty()
    set.add('a');
    if (set.isEmpty()) {
        errln("FAIL, set shouldn't be empty but is: " + set);
    }
    set.clear();
    if (!set.isEmpty()) {
        errln("FAIL, set should be empty but isn't: " + set);
    }
    // size()
    set.clear();
    if (set.size() != 0) {
        errln("FAIL, size should be 0, but is " + set.size() + ": " + set);
    }
    set.add('a');
    if (set.size() != 1) {
        errln("FAIL, size should be 1, but is " + set.size() + ": " + set);
    }
    set.add('1', '9');
    if (set.size() != 10) {
        errln("FAIL, size should be 10, but is " + set.size() + ": " + set);
    }
    set.clear();
    set.complement();
    if (set.size() != 0x110000) {
        errln("FAIL, size should be 0x110000, but is" + set.size());
    }
    // contains(first, last)
    set.clear();
    set.applyPattern("[A-Y 1-8 b-d l-y]");
    for (int i = 0; i < set.getRangeCount(); ++i) {
        int a = set.getRangeStart(i);
        int b = set.getRangeEnd(i);
        if (!set.contains(a, b)) {
            errln("FAIL, should contain " + (char) a + '-' + (char) b + " but doesn't: " + set);
        }
        if (set.contains((char) (a - 1), b)) {
            errln("FAIL, shouldn't contain " + (char) (a - 1) + '-' + (char) b + " but does: " + set);
        }
        if (set.contains(a, (char) (b + 1))) {
            errln("FAIL, shouldn't contain " + (char) a + '-' + (char) (b + 1) + " but does: " + set);
        }
    }
    // Ported InversionList test.
    UnicodeSet a = new UnicodeSet((char) 3, (char) 10);
    UnicodeSet b = new UnicodeSet((char) 7, (char) 15);
    UnicodeSet c = new UnicodeSet();
    logln("a [3-10]: " + a);
    logln("b [7-15]: " + b);
    c.set(a);
    c.addAll(b);
    UnicodeSet exp = new UnicodeSet((char) 3, (char) 15);
    if (c.equals(exp)) {
        logln("c.set(a).add(b): " + c);
    } else {
        errln("FAIL: c.set(a).add(b) = " + c + ", expect " + exp);
    }
    c.complement();
    exp.set((char) 0, (char) 2);
    exp.add((char) 16, UnicodeSet.MAX_VALUE);
    if (c.equals(exp)) {
        logln("c.complement(): " + c);
    } else {
        errln(Utility.escape("FAIL: c.complement() = " + c + ", expect " + exp));
    }
    c.complement();
    exp.set((char) 3, (char) 15);
    if (c.equals(exp)) {
        logln("c.complement(): " + c);
    } else {
        errln("FAIL: c.complement() = " + c + ", expect " + exp);
    }
    c.set(a);
    c.complementAll(b);
    exp.set((char) 3, (char) 6);
    exp.add((char) 11, (char) 15);
    if (c.equals(exp)) {
        logln("c.set(a).complement(b): " + c);
    } else {
        errln("FAIL: c.set(a).complement(b) = " + c + ", expect " + exp);
    }
    exp.set(c);
    c = bitsToSet(setToBits(c));
    if (c.equals(exp)) {
        logln("bitsToSet(setToBits(c)): " + c);
    } else {
        errln("FAIL: bitsToSet(setToBits(c)) = " + c + ", expect " + exp);
    }
    // Additional tests for coverage JB#2118
    // UnicodeSet::complement(class UnicodeString const &)
    // UnicodeSet::complementAll(class UnicodeString const &)
    // UnicodeSet::containsNone(class UnicodeSet const &)
    // UnicodeSet::containsNone(long,long)
    // UnicodeSet::containsSome(class UnicodeSet const &)
    // UnicodeSet::containsSome(long,long)
    // UnicodeSet::removeAll(class UnicodeString const &)
    // UnicodeSet::retain(long)
    // UnicodeSet::retainAll(class UnicodeString const &)
    // UnicodeSet::serialize(unsigned short *,long,enum UErrorCode &)
    // UnicodeSetIterator::getString(void)
    set.clear();
    set.complement("ab");
    exp.applyPattern("[{ab}]");
    if (!set.equals(exp)) {
        errln("FAIL: complement(\"ab\")");
        return;
    }
    UnicodeSetIterator iset = new UnicodeSetIterator(set);
    if (!iset.next() || iset.codepoint != UnicodeSetIterator.IS_STRING) {
        errln("FAIL: UnicodeSetIterator.next/IS_STRING");
    } else if (!iset.string.equals("ab")) {
        errln("FAIL: UnicodeSetIterator.string");
    }
    set.add((char) 0x61, (char) 0x7A);
    set.complementAll("alan");
    exp.applyPattern("[{ab}b-kmo-z]");
    if (!set.equals(exp)) {
        errln("FAIL: complementAll(\"alan\")");
        return;
    }
    exp.applyPattern("[a-z]");
    if (set.containsNone(exp)) {
        errln("FAIL: containsNone(UnicodeSet)");
    }
    if (!set.containsSome(exp)) {
        errln("FAIL: containsSome(UnicodeSet)");
    }
    exp.applyPattern("[aln]");
    if (!set.containsNone(exp)) {
        errln("FAIL: containsNone(UnicodeSet)");
    }
    if (set.containsSome(exp)) {
        errln("FAIL: containsSome(UnicodeSet)");
    }
    if (set.containsNone((char) 0x61, (char) 0x7A)) {
        errln("FAIL: containsNone(char, char)");
    }
    if (!set.containsSome((char) 0x61, (char) 0x7A)) {
        errln("FAIL: containsSome(char, char)");
    }
    if (!set.containsNone((char) 0x41, (char) 0x5A)) {
        errln("FAIL: containsNone(char, char)");
    }
    if (set.containsSome((char) 0x41, (char) 0x5A)) {
        errln("FAIL: containsSome(char, char)");
    }
    set.removeAll("liu");
    exp.applyPattern("[{ab}b-hj-kmo-tv-z]");
    if (!set.equals(exp)) {
        errln("FAIL: removeAll(\"liu\")");
        return;
    }
    set.retainAll("star");
    exp.applyPattern("[rst]");
    if (!set.equals(exp)) {
        errln("FAIL: retainAll(\"star\")");
        return;
    }
    set.retain((char) 0x73);
    exp.applyPattern("[s]");
    if (!set.equals(exp)) {
        errln("FAIL: retain('s')");
        return;
    }
    // ICU 2.6 coverage tests
    // public final UnicodeSet retain(String s);
    // public final UnicodeSet remove(int c);
    // public final UnicodeSet remove(String s);
    // public int hashCode();
    set.applyPattern("[a-z{ab}{cd}]");
    set.retain("cd");
    exp.applyPattern("[{cd}]");
    if (!set.equals(exp)) {
        errln("FAIL: retain(\"cd\")");
        return;
    }
    set.applyPattern("[a-z{ab}{cd}]");
    set.remove((char) 0x63);
    exp.applyPattern("[abd-z{ab}{cd}]");
    if (!set.equals(exp)) {
        errln("FAIL: remove('c')");
        return;
    }
    set.remove("cd");
    exp.applyPattern("[abd-z{ab}]");
    if (!set.equals(exp)) {
        errln("FAIL: remove(\"cd\")");
        return;
    }
    if (set.hashCode() != exp.hashCode()) {
        errln("FAIL: hashCode() unequal");
    }
    exp.clear();
    if (set.hashCode() == exp.hashCode()) {
        errln("FAIL: hashCode() equal");
    }
    {
        // Cover addAll(Collection) and addAllTo(Collection)
        // Seems that there is a bug in addAll(Collection) operation
        // Ram also add a similar test to UtilityTest.java
        logln("Testing addAll(Collection) ... ");
        String[] array = { "a", "b", "c", "de" };
        List list = Arrays.asList(array);
        Set aset = new HashSet(list);
        logln(" *** The source set's size is: " + aset.size());
        set.clear();
        set.addAll(aset);
        if (set.size() != aset.size()) {
            errln("FAIL: After addAll, the UnicodeSet size expected " + aset.size() + ", " + set.size() + " seen instead!");
        } else {
            logln("OK: After addAll, the UnicodeSet size got " + set.size());
        }
        List list2 = new ArrayList();
        set.addAllTo(list2);
        // verify the result
        log(" *** The elements are: ");
        String s = set.toPattern(true);
        logln(s);
        Iterator myiter = list2.iterator();
        while (myiter.hasNext()) {
            log(myiter.next().toString() + "  ");
        }
        // a new line
        logln("");
    }
}
Also used : UnicodeSetIterator(android.icu.text.UnicodeSetIterator) UnicodeSet(android.icu.text.UnicodeSet) SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) UnicodeSetIterator(android.icu.text.UnicodeSetIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) UnicodeSet(android.icu.text.UnicodeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Example 2 with UnicodeSet

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

the class UnicodeSetTest method toPatternAux.

public boolean toPatternAux(int start, int end) {
    // use Integer.toString because Utility.hex doesn't handle ints
    String source = "0x" + Integer.toString(start, 16).toUpperCase();
    if (start != end)
        source += "..0x" + Integer.toString(end, 16).toUpperCase();
    UnicodeSet testSet = new UnicodeSet();
    testSet.add(start, end);
    return checkPat(source, testSet);
}
Also used : UnicodeSet(android.icu.text.UnicodeSet)

Example 3 with UnicodeSet

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

the class UnicodeSetTest method checkRoundTrip.

/**
 * Basic consistency check for a few items.
 * That the iterator works, and that we can create a pattern and
 * get the same thing back
 */
void checkRoundTrip(UnicodeSet s) {
    String pat = s.toPattern(false);
    UnicodeSet t = copyWithIterator(s, false);
    checkEqual(s, t, "iterator roundtrip");
    // try range
    t = copyWithIterator(s, true);
    checkEqual(s, t, "iterator roundtrip");
    t = new UnicodeSet(pat);
    checkEqual(s, t, "toPattern(false)");
    pat = s.toPattern(true);
    t = new UnicodeSet(pat);
    checkEqual(s, t, "toPattern(true)");
}
Also used : UnicodeSet(android.icu.text.UnicodeSet)

Example 4 with UnicodeSet

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

the class UnicodeSetTest method checkCodePoints.

private void checkCodePoints(String a, String b, CountMethod quantifier, SpanCondition spanCondition, String expectedReplaced, int expectedCount) {
    final String ab = a + b;
    UnicodeSetSpanner m = new UnicodeSetSpanner(new UnicodeSet("[{" + a + "}]"));
    assertEquals("new UnicodeSetSpanner(\"[{" + a + "}]\").countIn(\"" + ab + "\")", expectedCount, callCountIn(m, ab, quantifier, spanCondition));
    if (expectedReplaced == null) {
        expectedReplaced = "-" + b;
    }
    assertEquals("new UnicodeSetSpanner(\"[{" + a + "}]\").replaceFrom(\"" + ab + "\", \"-\")", expectedReplaced, m.replaceFrom(ab, "-", quantifier));
}
Also used : UnicodeSetSpanner(android.icu.text.UnicodeSetSpanner) UnicodeSet(android.icu.text.UnicodeSet)

Example 5 with UnicodeSet

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

the class UnicodeSetTest method expectEqual.

void expectEqual(String name, String pat1, String pat2) {
    UnicodeSet set1, set2;
    try {
        set1 = new UnicodeSet(pat1);
        set2 = new UnicodeSet(pat2);
    } catch (IllegalArgumentException e) {
        errln("FAIL: Couldn't create UnicodeSet from pattern for \"" + name + "\": " + e.getMessage());
        return;
    }
    if (!set1.equals(set2)) {
        errln("FAIL: Sets built from patterns differ for \"" + name + "\"");
    }
}
Also used : UnicodeSet(android.icu.text.UnicodeSet)

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