Search in sources :

Example 21 with CollationElementIterator

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

the class CollationMiscTest method TestContraction.

@Test
public void TestContraction() {
    String[] testrules = { "&A = AB / B", "&A = A\\u0306/\\u0306", "&c = ch / h" };
    String[] testdata = { "AB", "AB", "A\u0306", "ch" };
    String[] testdata2 = { "\u0063\u0067", "\u0063\u0068", "\u0063\u006C" };
    /*
         * These pairs of rule strings are not guaranteed to yield the very same mappings.
         * In fact, LDML 24 recommends an improved way of creating mappings
         * which always yields different mappings for such pairs. See
         * http://www.unicode.org/reports/tr35/tr35-33/tr35-collation.html#Orderings
        String[] testrules3 = {
            "&z < xyz &xyzw << B",
            "&z < xyz &xyz << B / w",
            "&z < ch &achm << B",
            "&z < ch &a << B / chm",
            "&\ud800\udc00w << B",
            "&\ud800\udc00 << B / w",
            "&a\ud800\udc00m << B",
            "&a << B / \ud800\udc00m",
        }; */
    RuleBasedCollator coll = null;
    for (int i = 0; i < testrules.length; i++) {
        CollationElementIterator iter1 = null;
        int j = 0;
        // logln("Rule " + testrules[i] + " for testing\n");
        String rule = testrules[i];
        try {
            coll = new RuleBasedCollator(rule);
        } catch (Exception e) {
            warnln("Collator creation failed " + testrules[i]);
            return;
        }
        try {
            iter1 = coll.getCollationElementIterator(testdata[i]);
        } catch (Exception e) {
            errln("Collation iterator creation failed\n");
            return;
        }
        while (j < 2) {
            CollationElementIterator iter2;
            int ce;
            try {
                iter2 = coll.getCollationElementIterator(String.valueOf(testdata[i].charAt(j)));
            } catch (Exception e) {
                errln("Collation iterator creation failed\n");
                return;
            }
            ce = iter2.next();
            while (ce != CollationElementIterator.NULLORDER) {
                if (iter1.next() != ce) {
                    errln("Collation elements in contraction split does not match\n");
                    return;
                }
                ce = iter2.next();
            }
            j++;
        }
        if (iter1.next() != CollationElementIterator.NULLORDER) {
            errln("Collation elements not exhausted\n");
            return;
        }
    }
    String rule = "& a < b < c < ch < d & c = ch / h";
    try {
        coll = new RuleBasedCollator(rule);
    } catch (Exception e) {
        errln("cannot create rulebased collator");
        return;
    }
    if (coll.compare(testdata2[0], testdata2[1]) != -1) {
        errln("Expected " + testdata2[0] + " < " + testdata2[1]);
        return;
    }
    if (coll.compare(testdata2[1], testdata2[2]) != -1) {
        errln("Expected " + testdata2[1] + " < " + testdata2[2]);
        return;
    }
/* see above -- for (int i = 0; i < testrules3.length; i += 2) {
            RuleBasedCollator          coll1, coll2;
            CollationElementIterator iter1, iter2;
            char               ch = 0x0042;
            int            ce;
            rule = testrules3[i];
            try {
                coll1 = new RuleBasedCollator(rule);
            } catch (Exception e) {
                errln("Fail: cannot create rulebased collator, rule:" + rule);
                return;
            }
            rule = testrules3[i + 1];
            try {
                coll2 = new RuleBasedCollator(rule);
            } catch (Exception e) {
                errln("Collator creation failed " + testrules[i]);
                return;
            }
            try {
                iter1 = coll1.getCollationElementIterator(String.valueOf(ch));
                iter2 = coll2.getCollationElementIterator(String.valueOf(ch));
            } catch (Exception e) {
                errln("Collation iterator creation failed\n");
                return;
            }
            ce = iter1.next();

            while (ce != CollationElementIterator.NULLORDER) {
                if (ce != iter2.next()) {
                    errln("CEs does not match\n");
                    return;
                }
                ce = iter1.next();
            }
            if (iter2.next() != CollationElementIterator.NULLORDER) {
                errln("CEs not exhausted\n");
                return;
            }
        } */
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 22 with CollationElementIterator

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

the class CollationRegressionTest method Test4179686.

// CollationElementIterator.previous broken for expanding char sequences
// 
@Test
public void Test4179686() throws Exception {
    RuleBasedCollator en_us = (RuleBasedCollator) Collator.getInstance(Locale.US);
    // Create a collator with a few expanding character sequences in it....
    RuleBasedCollator coll = new RuleBasedCollator(en_us.getRules() + " & ae ; \u00e4 & AE ; \u00c4" + " & oe ; \u00f6 & OE ; \u00d6" + " & ue ; \u00fc & UE ; \u00dc");
    // o-umlaut
    String text = "T\u00f6ne";
    CollationElementIterator iter = coll.getCollationElementIterator(text);
    List elements = new ArrayList();
    int elem;
    // Iterate forward and collect all of the elements into a Vector
    while ((elem = iter.next()) != CollationElementIterator.NULLORDER) {
        elements.add(new Integer(elem));
    }
    // Now iterate backward and make sure they're the same
    iter.reset();
    int index = elements.size() - 1;
    while ((elem = iter.previous()) != CollationElementIterator.NULLORDER) {
        int expect = ((Integer) elements.get(index)).intValue();
        if (elem != expect) {
            errln("Mismatch at index " + index + ": got " + Integer.toString(elem, 16) + ", expected " + Integer.toString(expect, 16));
        }
        index--;
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ArrayList(java.util.ArrayList) CollationElementIterator(android.icu.text.CollationElementIterator) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 23 with CollationElementIterator

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

the class CollationRegressionTest method Test4179216.

@Test
public void Test4179216() throws Exception {
    // you can position a CollationElementIterator in the middle of
    // a contracting character sequence, yielding a bogus collation
    // element
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(Locale.US);
    coll = new RuleBasedCollator(coll.getRules() + " & C < ch , cH , Ch , CH < cat < crunchy");
    String testText = "church church catcatcher runcrunchynchy";
    CollationElementIterator iter = coll.getCollationElementIterator(testText);
    // test that the "ch" combination works properly
    iter.setOffset(4);
    int elt4 = CollationElementIterator.primaryOrder(iter.next());
    iter.reset();
    int elt0 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(5);
    int elt5 = CollationElementIterator.primaryOrder(iter.next());
    // Compares and prints only 16-bit primary weights.
    if (elt4 != elt0 || elt5 != elt0) {
        errln(String.format("The collation elements at positions 0 (0x%04x), " + "4 (0x%04x), and 5 (0x%04x) don't match.", elt0, elt4, elt5));
    }
    // test that the "cat" combination works properly
    iter.setOffset(14);
    int elt14 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(15);
    int elt15 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(16);
    int elt16 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(17);
    int elt17 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(18);
    int elt18 = CollationElementIterator.primaryOrder(iter.next());
    iter.setOffset(19);
    int elt19 = CollationElementIterator.primaryOrder(iter.next());
    // Compares and prints only 16-bit primary weights.
    if (elt14 != elt15 || elt14 != elt16 || elt14 != elt17 || elt14 != elt18 || elt14 != elt19) {
        errln(String.format("\"cat\" elements don't match: elt14 = 0x%04x, " + "elt15 = 0x%04x, elt16 = 0x%04x, elt17 = 0x%04x, " + "elt18 = 0x%04x, elt19 = 0x%04x", elt14, elt15, elt16, elt17, elt18, elt19));
    }
    // now generate a complete list of the collation elements,
    // first using next() and then using setOffset(), and
    // make sure both interfaces return the same set of elements
    iter.reset();
    int elt = iter.next();
    int count = 0;
    while (elt != CollationElementIterator.NULLORDER) {
        ++count;
        elt = iter.next();
    }
    String[] nextElements = new String[count];
    String[] setOffsetElements = new String[count];
    int lastPos = 0;
    iter.reset();
    elt = iter.next();
    count = 0;
    while (elt != CollationElementIterator.NULLORDER) {
        nextElements[count++] = testText.substring(lastPos, iter.getOffset());
        lastPos = iter.getOffset();
        elt = iter.next();
    }
    count = 0;
    for (int i = 0; i < testText.length(); ) {
        iter.setOffset(i);
        lastPos = iter.getOffset();
        elt = iter.next();
        setOffsetElements[count++] = testText.substring(lastPos, iter.getOffset());
        i = iter.getOffset();
    }
    for (int i = 0; i < nextElements.length; i++) {
        if (nextElements[i].equals(setOffsetElements[i])) {
            logln(nextElements[i]);
        } else {
            errln("Error: next() yielded " + nextElements[i] + ", but setOffset() yielded " + setOffsetElements[i]);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 24 with CollationElementIterator

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

the class CollationRegressionTest method Test4048446.

// @bug 4048446
// 
// CollationElementIterator.reset() doesn't work
// 
@Test
public void Test4048446() {
    final String test1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";
    // final String test2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";
    RuleBasedCollator en_us = (RuleBasedCollator) Collator.getInstance(Locale.US);
    CollationElementIterator i1 = en_us.getCollationElementIterator(test1);
    CollationElementIterator i2 = en_us.getCollationElementIterator(test1);
    if (i1 == null || i2 == null) {
        errln("Could not create CollationElementIterator's");
        return;
    }
    while (i1.next() != CollationElementIterator.NULLORDER) {
    // 
    }
    i1.reset();
    assertEqual(i1, i2);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) Test(org.junit.Test)

Example 25 with CollationElementIterator

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

the class CollationRegressionTest method Test4101940.

// @bug 4101940
// 
@Test
public void Test4101940() /* char* par */
{
    RuleBasedCollator c = null;
    String rules = "&9 < a < b";
    String nothing = "";
    try {
        c = new RuleBasedCollator(rules);
    } catch (Exception e) {
        errln("Failed to create RuleBasedCollator");
        return;
    }
    CollationElementIterator i = c.getCollationElementIterator(nothing);
    i.reset();
    if (i.next() != CollationElementIterator.NULLORDER) {
        errln("next did not return NULLORDER");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationElementIterator(android.icu.text.CollationElementIterator) ParseException(java.text.ParseException) Test(org.junit.Test)

Aggregations

CollationElementIterator (android.icu.text.CollationElementIterator)26 RuleBasedCollator (android.icu.text.RuleBasedCollator)25 Test (org.junit.Test)25 ULocale (android.icu.util.ULocale)4 Collator (android.icu.text.Collator)3 Locale (java.util.Locale)3 CollationKey (android.icu.text.CollationKey)2 RawCollationKey (android.icu.text.RawCollationKey)2 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 StringCharacterIterator (java.text.StringCharacterIterator)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1