Search in sources :

Example 16 with RuleBasedCollator

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

the class SearchTest method assertEqual.

boolean assertEqual(SearchData search) {
    Collator collator = getCollator(search.collator);
    BreakIterator breaker = getBreakIterator(search.breaker);
    StringSearch strsrch;
    String text = search.text;
    String pattern = search.pattern;
    if (breaker != null) {
        breaker.setText(text);
    }
    collator.setStrength(search.strength);
    try {
        strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
        strsrch.setElementComparisonType(search.cmpType);
    } catch (Exception e) {
        errln("Error opening string search " + e.getMessage());
        return false;
    }
    if (!assertEqualWithStringSearch(strsrch, search)) {
        collator.setStrength(TERTIARY);
        return false;
    }
    collator.setStrength(TERTIARY);
    return true;
}
Also used : StringCharacterIterator(java.text.StringCharacterIterator) RuleBasedCollator(android.icu.text.RuleBasedCollator) StringSearch(android.icu.text.StringSearch) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) BreakIterator(android.icu.text.BreakIterator)

Example 17 with RuleBasedCollator

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

the class AlphabeticIndexTest method TestHaniFirst.

/**
 * Test AlphabeticIndex vs. root with script reordering.
 */
@Test
public void TestHaniFirst() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
    coll.setReorderCodes(UScript.HAN);
    AlphabeticIndex index = new AlphabeticIndex(coll);
    // ... (underflow only)
    assertEquals("getBucketCount()", 1, index.getBucketCount());
    index.addLabels(Locale.ENGLISH);
    // ... A-Z ...
    assertEquals("getBucketCount()", 28, index.getBucketCount());
    int bucketIndex = index.getBucketIndex("\u897f");
    // underflow bucket
    assertEquals("getBucketIndex(U+897F)", 0, bucketIndex);
    bucketIndex = index.getBucketIndex("i");
    assertEquals("getBucketIndex(i)", 9, bucketIndex);
    bucketIndex = index.getBucketIndex("\u03B1");
    assertEquals("getBucketIndex(Greek alpha)", 27, bucketIndex);
    // U+50005 is an unassigned code point which sorts at the end, independent of the Hani group.
    bucketIndex = index.getBucketIndex(UTF16.valueOf(0x50005));
    assertEquals("getBucketIndex(U+50005)", 27, bucketIndex);
    bucketIndex = index.getBucketIndex("\uFFFF");
    assertEquals("getBucketIndex(U+FFFF)", 27, bucketIndex);
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 18 with RuleBasedCollator

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

the class AlphabeticIndexTest method TestNoLabels.

/**
 * With no real labels, there should be only the underflow label.
 */
@Test
public void TestNoLabels() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
    AlphabeticIndex<Integer> index = new AlphabeticIndex<Integer>(coll);
    index.addRecord("\u897f", 0);
    index.addRecord("i", 0);
    index.addRecord("\u03B1", 0);
    // code coverage
    assertEquals("getRecordCount()", 3, index.getRecordCount());
    // ...
    assertEquals("getBucketCount()", 1, index.getBucketCount());
    Bucket<Integer> bucket = index.iterator().next();
    assertEquals("underflow label type", LabelType.UNDERFLOW, bucket.getLabelType());
    assertEquals("all records in the underflow bucket", 3, bucket.size());
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 19 with RuleBasedCollator

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

the class AlphabeticIndexTest method TestFrozenCollator.

@Test
public void TestFrozenCollator() {
    // Ticket #9472
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(new ULocale("da"));
    coll.setStrength(Collator.IDENTICAL);
    coll.freeze();
    // The AlphabeticIndex constructor used to throw an exception
    // because it cloned the collator (which preserves frozenness)
    // and set the clone's strength to PRIMARY.
    AlphabeticIndex index = new AlphabeticIndex(coll);
    assertEquals("same strength as input Collator", Collator.IDENTICAL, index.getCollator().getStrength());
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 20 with RuleBasedCollator

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

the class AlphabeticIndexTest method TestClientSupport.

@Test
public void TestClientSupport() {
    for (String localeString : new String[] { "zh" }) {
        // KEY_LOCALES, new String[] {"zh"}
        ULocale ulocale = new ULocale(localeString);
        AlphabeticIndex<Double> alphabeticIndex = new AlphabeticIndex<Double>(ulocale).addLabels(Locale.ENGLISH);
        RuleBasedCollator collator = alphabeticIndex.getCollator();
        String[][] tests;
        if (!localeString.equals("zh")) {
            tests = new String[][] { SimpleTests };
        } else {
            tests = new String[][] { SimpleTests, hackPinyin, simplifiedNames };
        }
        for (String[] shortTest : tests) {
            double testValue = 100;
            alphabeticIndex.clearRecords();
            for (String name : shortTest) {
                alphabeticIndex.addRecord(name, testValue++);
            }
            if (DEBUG)
                showIndex(alphabeticIndex, false);
            // make my own copy
            testValue = 100;
            List<String> myBucketLabels = alphabeticIndex.getBucketLabels();
            ArrayList<Set<R4<RawCollationKey, String, Integer, Double>>> myBucketContents = new ArrayList<Set<R4<RawCollationKey, String, Integer, Double>>>(myBucketLabels.size());
            for (int i = 0; i < myBucketLabels.size(); ++i) {
                myBucketContents.add(new TreeSet<R4<RawCollationKey, String, Integer, Double>>());
            }
            for (String name : shortTest) {
                int bucketIndex = alphabeticIndex.getBucketIndex(name);
                if (bucketIndex > myBucketContents.size()) {
                    // call again for debugging
                    alphabeticIndex.getBucketIndex(name);
                }
                Set<R4<RawCollationKey, String, Integer, Double>> myBucket = myBucketContents.get(bucketIndex);
                RawCollationKey rawCollationKey = collator.getRawCollationKey(name, null);
                R4<RawCollationKey, String, Integer, Double> row = Row.of(rawCollationKey, name, name.length(), testValue++);
                myBucket.add(row);
            }
            if (DEBUG)
                showIndex(myBucketLabels, myBucketContents, false);
            // now compare
            int index = 0;
            boolean gotError = false;
            for (AlphabeticIndex.Bucket<Double> bucket : alphabeticIndex) {
                String bucketLabel = bucket.getLabel();
                String myLabel = myBucketLabels.get(index);
                if (!bucketLabel.equals(myLabel)) {
                    gotError |= !assertEquals(ulocale + "\tBucket Labels (" + index + ")", bucketLabel, myLabel);
                }
                Set<R4<RawCollationKey, String, Integer, Double>> myBucket = myBucketContents.get(index);
                Iterator<R4<RawCollationKey, String, Integer, Double>> myBucketIterator = myBucket.iterator();
                int recordIndex = 0;
                for (Record<Double> record : bucket) {
                    String myName = null;
                    if (myBucketIterator.hasNext()) {
                        R4<RawCollationKey, String, Integer, Double> myRecord = myBucketIterator.next();
                        myName = myRecord.get1();
                    }
                    if (!record.getName().equals(myName)) {
                        gotError |= !assertEquals(ulocale + "\t" + bucketLabel + "\t" + "Record Names (" + index + "." + recordIndex++ + ")", record.getName(), myName);
                    }
                }
                while (myBucketIterator.hasNext()) {
                    R4<RawCollationKey, String, Integer, Double> myRecord = myBucketIterator.next();
                    String myName = myRecord.get1();
                    gotError |= !assertEquals(ulocale + "\t" + bucketLabel + "\t" + "Record Names (" + index + "." + recordIndex++ + ")", null, myName);
                }
                index++;
            }
            if (gotError) {
                showIndex(myBucketLabels, myBucketContents, false);
                showIndex(alphabeticIndex, false);
            }
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) RawCollationKey(android.icu.text.RawCollationKey) R4(android.icu.impl.Row.R4) ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Aggregations

RuleBasedCollator (android.icu.text.RuleBasedCollator)140 Test (org.junit.Test)124 Collator (android.icu.text.Collator)42 ULocale (android.icu.util.ULocale)26 CollationElementIterator (android.icu.text.CollationElementIterator)25 Locale (java.util.Locale)22 CollationKey (android.icu.text.CollationKey)17 StringCharacterIterator (java.text.StringCharacterIterator)16 StringSearch (android.icu.text.StringSearch)14 RawCollationKey (android.icu.text.RawCollationKey)11 ParseException (java.text.ParseException)10 UnicodeSet (android.icu.text.UnicodeSet)8 AlphabeticIndex (android.icu.text.AlphabeticIndex)6 BreakIterator (android.icu.text.BreakIterator)6 MissingResourceException (java.util.MissingResourceException)5 IOException (java.io.IOException)4 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)3 UCharacterIterator (android.icu.text.UCharacterIterator)2 CharacterIterator (java.text.CharacterIterator)2 ArrayList (java.util.ArrayList)2