Search in sources :

Example 1 with AlphabeticIndex

use of android.icu.text.AlphabeticIndex 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 2 with AlphabeticIndex

use of android.icu.text.AlphabeticIndex 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 3 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestEmpty.

@Test
public void TestEmpty() {
    // just verify that it doesn't blow up.
    Set<ULocale> locales = new LinkedHashSet<ULocale>();
    locales.add(ULocale.ROOT);
    locales.addAll(Arrays.asList(ULocale.getAvailableLocales()));
    for (ULocale locale : locales) {
        try {
            AlphabeticIndex<String> alphabeticIndex = new AlphabeticIndex(locale);
            alphabeticIndex.addRecord("hi", "HI");
            for (Bucket<String> bucket : alphabeticIndex) {
                @SuppressWarnings("unused") LabelType labelType = bucket.getLabelType();
            }
        } catch (Exception e) {
            errln("Exception when creating AlphabeticIndex for:\t" + locale.toLanguageTag());
            errln(e.toString());
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ULocale(android.icu.util.ULocale) LabelType(android.icu.text.AlphabeticIndex.Bucket.LabelType) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 4 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestJapaneseKanji.

@Test
public void TestJapaneseKanji() {
    AlphabeticIndex index = new AlphabeticIndex(ULocale.JAPANESE);
    AlphabeticIndex.ImmutableIndex immIndex = index.buildImmutableIndex();
    // There are no index characters for Kanji in the Japanese standard collator.
    // They should all go into the overflow bucket.
    final int[] kanji = { 0x4E9C, 0x95C7, 0x4E00, 0x58F1 };
    int overflowIndex = immIndex.getBucketCount() - 1;
    for (int i = 0; i < kanji.length; ++i) {
        String msg = String.format("kanji[%d]=U+%04X in overflow bucket", i, kanji[i]);
        assertEquals(msg, overflowIndex, immIndex.getBucketIndex(UTF16.valueOf(kanji[i])));
    }
}
Also used : ImmutableIndex(android.icu.text.AlphabeticIndex.ImmutableIndex) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 5 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestFirstScriptCharacters.

@Test
public void TestFirstScriptCharacters() {
    Collection<String> firstCharacters = new AlphabeticIndex(ULocale.ENGLISH).getFirstCharactersInScripts();
    Collection<String> expectedFirstCharacters = firstStringsInScript((RuleBasedCollator) Collator.getInstance(ULocale.ROOT));
    Collection<String> diff = new TreeSet<String>(firstCharacters);
    diff.removeAll(expectedFirstCharacters);
    assertTrue("First Characters contains unexpected ones: " + diff, diff.isEmpty());
    diff.clear();
    diff.addAll(expectedFirstCharacters);
    diff.removeAll(firstCharacters);
    assertTrue("First Characters missing expected ones: " + diff, diff.isEmpty());
}
Also used : TreeSet(java.util.TreeSet) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Aggregations

AlphabeticIndex (android.icu.text.AlphabeticIndex)18 Test (org.junit.Test)17 ULocale (android.icu.util.ULocale)10 RuleBasedCollator (android.icu.text.RuleBasedCollator)6 ImmutableIndex (android.icu.text.AlphabeticIndex.ImmutableIndex)5 UnicodeSet (android.icu.text.UnicodeSet)4 ArrayList (java.util.ArrayList)3 LabelType (android.icu.text.AlphabeticIndex.Bucket.LabelType)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 R4 (android.icu.impl.Row.R4)1 Bucket (android.icu.text.AlphabeticIndex.Bucket)1 RawCollationKey (android.icu.text.RawCollationKey)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 Set (java.util.Set)1