Search in sources :

Example 16 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestFirstCharacters.

@Test
public void TestFirstCharacters() {
    AlphabeticIndex alphabeticIndex = new AlphabeticIndex(Locale.ENGLISH);
    RuleBasedCollator collator = alphabeticIndex.getCollator();
    collator.setStrength(Collator.IDENTICAL);
    Collection<String> firsts = alphabeticIndex.getFirstCharactersInScripts();
    // Verify that each script is represented exactly once.
    // Exclude pseudo-scripts like Common (no letters).
    // Exclude scripts like Braille and Sutton SignWriting
    // because they only have symbols, not letters.
    UnicodeSet missingScripts = new UnicodeSet("[^[:inherited:][:unknown:][:common:][:Braille:][:SignWriting:]]");
    String last = "";
    for (String index : firsts) {
        if (collator.compare(last, index) >= 0) {
            errln("Characters not in order: " + last + " !< " + index);
        }
        int script = getFirstRealScript(index);
        if (script == UScript.UNKNOWN) {
            continue;
        }
        UnicodeSet s = new UnicodeSet().applyIntPropertyValue(UProperty.SCRIPT, script);
        if (missingScripts.containsNone(s)) {
            errln("2nd character in script: " + index + "\t" + new UnicodeSet(missingScripts).retainAll(s).toPattern(false));
        }
        missingScripts.removeAll(s);
    }
    if (missingScripts.size() != 0) {
        String missingScriptNames = "";
        UnicodeSet missingChars = new UnicodeSet(missingScripts);
        for (; ; ) {
            int c = missingChars.charAt(0);
            if (c < 0) {
                break;
            }
            int script = UScript.getScript(c);
            missingScriptNames += " " + UCharacter.getPropertyValueName(UProperty.SCRIPT, script, UProperty.NameChoice.SHORT);
            missingChars.removeAll(new UnicodeSet().applyIntPropertyValue(UProperty.SCRIPT, script));
        }
        errln("Missing character from:" + missingScriptNames + " -- " + missingScripts);
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) AlphabeticIndex(android.icu.text.AlphabeticIndex) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 17 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestA.

// public void TestAAKeyword() {
// ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(
// ICUResourceBundle.ICU_COLLATION_BASE_NAME, "zh");
// showBundle(rb, 0);
// String[] keywords = Collator.getKeywords();
// System.out.println(Arrays.asList(keywords));
// String locale = "zh";
// ULocale ulocale = new ULocale(locale);
// for (String keyword : keywords) {
// List<String> values = Arrays.asList(Collator.getKeywordValuesForLocale(keyword, ulocale, false));
// List<String> allValues = Arrays.asList(Collator.getKeywordValues(keyword));
// for (String value : allValues) {
// System.out.println(keyword + "=" + value);
// checkKeyword(locale, value, values.contains(value));
// }
// }
// }
// 
// private void checkKeyword(String locale, String collationValue, boolean shouldExist) {
// final ULocale base = new ULocale(locale);
// final ULocale desired = new ULocale(locale + "@collation=" + collationValue);
// Collator foo = Collator.getInstance(desired);
// ULocale actual = foo.getLocale(ULocale.ACTUAL_LOCALE);
// if (shouldExist) {
// assertEquals("actual should match desired", desired, actual);
// } else {
// assertEquals("actual should match base", base, actual);
// }
// int comp = foo.compare("a", "ā");
// assertEquals("should fall back to default for zh", -1, comp);
// }
// 
// /**
// * @param rb
// * @param i
// */
// private static void showBundle(UResourceBundle rb, int i) {
// for (String key : rb.keySet()) {
// System.out.print("\n" + Utility.repeat("  ", i) + key);
// UResourceBundle rb2 = rb.get(key);
// showBundle(rb2, i+1);
// }
// }
@Test
public void TestA() {
    String[][] tests = { { "zh_Hant", "渡辺", "12劃" }, { "zh", "渡辺", "D" } /*, "zh@collation=unihan", "ja@collation=unihan", "ko@collation=unihan"*/
    };
    for (String[] test : tests) {
        AlphabeticIndex<Integer> alphabeticIndex = new AlphabeticIndex<Integer>(new ULocale(test[0]));
        final String probe = test[1];
        final String expectedLabel = test[2];
        alphabeticIndex.addRecord(probe, 1);
        List labels = alphabeticIndex.getBucketLabels();
        logln(labels.toString());
        Bucket<Integer> bucket = find(alphabeticIndex, probe);
        assertEquals("locale " + test[0] + " name=" + probe + " in bucket", expectedLabel, bucket.getLabel());
    }
}
Also used : ULocale(android.icu.util.ULocale) ArrayList(java.util.ArrayList) List(java.util.List) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 18 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestPinyinFirst.

/**
 * Test AlphabeticIndex vs. Pinyin with script reordering.
 */
@Test
public void TestPinyinFirst() {
    RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.CHINESE);
    coll.setReorderCodes(UScript.HAN);
    AlphabeticIndex index = new AlphabeticIndex(coll);
    // ... A-Z ...
    assertEquals("getBucketCount()", 28, index.getBucketCount());
    index.addLabels(Locale.CHINESE);
    // ... A-Z ...
    assertEquals("getBucketCount()", 28, index.getBucketCount());
    int bucketIndex = index.getBucketIndex("\u897f");
    assertEquals("getBucketIndex(U+897F)", 'X' - 'A' + 1, 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)

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