Search in sources :

Example 11 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestInflow.

@Test
public void TestInflow() {
    Object[][] tests = { { 0, ULocale.ENGLISH }, { 0, ULocale.ENGLISH, new ULocale("el") }, { 1, ULocale.ENGLISH, new ULocale("ru") }, { 0, ULocale.ENGLISH, new ULocale("el"), new UnicodeSet("[\u2C80]"), new ULocale("ru") }, { 0, ULocale.ENGLISH }, { 2, ULocale.ENGLISH, new ULocale("ru"), ULocale.JAPANESE } };
    for (Object[] test : tests) {
        int expected = (Integer) test[0];
        AlphabeticIndex<Double> alphabeticIndex = new AlphabeticIndex((ULocale) test[1]);
        for (int i = 2; i < test.length; ++i) {
            if (test[i] instanceof ULocale) {
                alphabeticIndex.addLabels((ULocale) test[i]);
            } else {
                alphabeticIndex.addLabels((UnicodeSet) test[i]);
            }
        }
        Counter<AlphabeticIndex.Bucket.LabelType> counter = new Counter();
        for (Bucket<Double> bucket : alphabeticIndex) {
            LabelType labelType = bucket.getLabelType();
            counter.add(labelType, 1);
        }
        String printList = Arrays.asList(test).toString();
        assertEquals(LabelType.UNDERFLOW + "\t" + printList, 1, counter.get(LabelType.UNDERFLOW));
        assertEquals(LabelType.INFLOW + "\t" + printList, expected, counter.get(LabelType.INFLOW));
        if (expected != counter.get(LabelType.INFLOW)) {
            // for debugging
            AlphabeticIndex<Double> indexCharacters2 = new AlphabeticIndex((ULocale) test[1]);
            for (int i = 2; i < test.length; ++i) {
                if (test[i] instanceof ULocale) {
                    indexCharacters2.addLabels((ULocale) test[i]);
                } else {
                    indexCharacters2.addLabels((UnicodeSet) test[i]);
                }
            }
            List<Bucket<Double>> buckets = CollectionUtilities.addAll(alphabeticIndex.iterator(), new ArrayList<Bucket<Double>>());
            logln(buckets.toString());
        }
        assertEquals(LabelType.OVERFLOW + "\t" + printList, 1, counter.get(LabelType.OVERFLOW));
    }
}
Also used : ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) AlphabeticIndex(android.icu.text.AlphabeticIndex) Bucket(android.icu.text.AlphabeticIndex.Bucket) LabelType(android.icu.text.AlphabeticIndex.Bucket.LabelType) Test(org.junit.Test)

Example 12 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestSetGetSpecialLabels.

@Test
public void TestSetGetSpecialLabels() {
    AlphabeticIndex index = new AlphabeticIndex(Locale.GERMAN).addLabels(new Locale("ru"));
    index.setUnderflowLabel("__");
    index.setInflowLabel("--");
    index.setOverflowLabel("^^");
    assertEquals("underflow label", "__", index.getUnderflowLabel());
    assertEquals("inflow label", "--", index.getInflowLabel());
    assertEquals("overflow label", "^^", index.getOverflowLabel());
    ImmutableIndex ii = index.buildImmutableIndex();
    assertEquals("0 -> underflow", "__", ii.getBucket(ii.getBucketIndex("0")).getLabel());
    assertEquals("Ω -> inflow", "--", ii.getBucket(ii.getBucketIndex("Ω")).getLabel());
    assertEquals("字 -> overflow", "^^", ii.getBucket(ii.getBucketIndex("字")).getLabel());
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ImmutableIndex(android.icu.text.AlphabeticIndex.ImmutableIndex) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 13 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestChineseZhuyin.

/**
 * Test with the Bopomofo-phonetic tailoring.
 */
@Test
public void TestChineseZhuyin() {
    AlphabeticIndex index = new AlphabeticIndex(ULocale.forLanguageTag("zh-u-co-zhuyin"));
    ImmutableIndex immIndex = index.buildImmutableIndex();
    // ... ㄅ ㄆ ㄇ ㄈ ㄉ -- ㄩ ...
    assertEquals("getBucketCount()", 38, immIndex.getBucketCount());
    assertEquals("label 1", "ㄅ", immIndex.getBucket(1).getLabel());
    assertEquals("label 2", "ㄆ", immIndex.getBucket(2).getLabel());
    assertEquals("label 3", "ㄇ", immIndex.getBucket(3).getLabel());
    assertEquals("label 4", "ㄈ", immIndex.getBucket(4).getLabel());
    assertEquals("label 5", "ㄉ", immIndex.getBucket(5).getLabel());
}
Also used : ImmutableIndex(android.icu.text.AlphabeticIndex.ImmutableIndex) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 14 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestChineseUnihan.

@Test
public void TestChineseUnihan() {
    AlphabeticIndex index = new AlphabeticIndex(new ULocale("zh-u-co-unihan"));
    // ICU 54 default is 99.
    index.setMaxLabelCount(500);
    // code coverage
    assertEquals("getMaxLabelCount()", 500, index.getMaxLabelCount());
    AlphabeticIndex.ImmutableIndex immIndex = index.buildImmutableIndex();
    int bucketCount = immIndex.getBucketCount();
    if (bucketCount < 216) {
        // There should be at least an underflow and overflow label,
        // and one for each of 214 radicals,
        // and maybe additional labels for simplified radicals.
        // (ICU4C: dataerrln(), prints only a warning if the data is missing)
        errln("too few buckets/labels for Chinese/unihan: " + bucketCount + " (is zh/unihan data available?)");
        return;
    } else {
        logln("Chinese/unihan has " + bucketCount + " buckets/labels");
    }
    // bucketIndex = radical number, adjusted for simplified radicals in lower buckets.
    int bucketIndex = index.getBucketIndex("\u4e5d");
    assertEquals("getBucketIndex(U+4E5D)", 5, bucketIndex);
    // radical 100, and there is a 90' since Unicode 8
    bucketIndex = index.getBucketIndex("\u7527");
    assertEquals("getBucketIndex(U+7527)", 101, bucketIndex);
}
Also used : ULocale(android.icu.util.ULocale) ImmutableIndex(android.icu.text.AlphabeticIndex.ImmutableIndex) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 15 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestIndexCharactersList.

@Test
public void TestIndexCharactersList() {
    for (String[] localeAndIndexCharacters : localeAndIndexCharactersLists) {
        ULocale locale = new ULocale(localeAndIndexCharacters[0]);
        String expectedIndexCharacters = "\u2026:" + localeAndIndexCharacters[1] + ":\u2026";
        Collection<String> alphabeticIndex = new AlphabeticIndex(locale).getBucketLabels();
        // Join the elements of the list to a string with delimiter ":"
        StringBuilder sb = new StringBuilder();
        Iterator<String> iter = alphabeticIndex.iterator();
        while (iter.hasNext()) {
            sb.append(iter.next());
            if (!iter.hasNext()) {
                break;
            }
            sb.append(":");
        }
        String actualIndexCharacters = sb.toString();
        if (!expectedIndexCharacters.equals(actualIndexCharacters)) {
            errln("Test failed for locale " + localeAndIndexCharacters[0] + "\n  Expected = |" + expectedIndexCharacters + "|\n  actual   = |" + actualIndexCharacters + "|");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) 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