Search in sources :

Example 6 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)

Example 7 with AlphabeticIndex

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

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

Example 9 with AlphabeticIndex

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

the class AlphabeticIndexTest method TestBasics.

@Test
public void TestBasics() {
    ULocale[] list = ULocale.getAvailableLocales();
    // get keywords combinations
    // don't bother with multiple combinations at this point
    List keywords = new ArrayList();
    keywords.add("");
    String[] collationValues = Collator.getKeywordValues("collation");
    for (int j = 0; j < collationValues.length; ++j) {
        keywords.add("@collation=" + collationValues[j]);
    }
    for (int i = 0; i < list.length; ++i) {
        for (Iterator it = keywords.iterator(); it.hasNext(); ) {
            String collationValue = (String) it.next();
            String localeString = list[i].toString();
            // TODO change in exhaustive
            if (!KEY_LOCALES.contains(localeString))
                continue;
            ULocale locale = new ULocale(localeString + collationValue);
            if (collationValue.length() > 0 && !Collator.getFunctionalEquivalent("collation", locale).equals(locale)) {
                // logln("Skipping " + locale);
                continue;
            }
            if (locale.getCountry().length() != 0) {
                continue;
            }
            boolean isUnihan = collationValue.contains("unihan");
            AlphabeticIndex alphabeticIndex = new AlphabeticIndex(locale);
            if (isUnihan) {
                // Unihan tailorings have a label per radical, and there are at least 214,
                // if not more when simplified radicals are distinguished.
                alphabeticIndex.setMaxLabelCount(500);
            }
            final Collection mainChars = alphabeticIndex.getBucketLabels();
            String mainCharString = mainChars.toString();
            if (mainCharString.length() > 500) {
                mainCharString = mainCharString.substring(0, 500) + "...";
            }
            logln(mainChars.size() + "\t" + locale + "\t" + locale.getDisplayName(ULocale.ENGLISH));
            logln("Index:\t" + mainCharString);
            if (!isUnihan && mainChars.size() > 100) {
                errln("Index character set too large: " + locale + " [" + mainChars.size() + "]:\n    " + mainChars);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) AlphabeticIndex(android.icu.text.AlphabeticIndex) Test(org.junit.Test)

Example 10 with AlphabeticIndex

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

the class AlphabeticIndexTest method checkBuckets.

private void checkBuckets(String localeString, String[] test, ULocale additionalLocale, String testBucket, String... items) {
    StringBuilder UI = new StringBuilder();
    ULocale desiredLocale = new ULocale(localeString);
    // Create a simple index where the values for the strings are Integers, and add the strings
    AlphabeticIndex<Integer> index = new AlphabeticIndex<Integer>(desiredLocale).addLabels(additionalLocale);
    int counter = 0;
    Counter<String> itemCount = new Counter();
    for (String item : test) {
        index.addRecord(item, counter++);
        itemCount.add(item, 1);
    }
    // code coverage
    assertEquals("getRecordCount()", (int) itemCount.getTotal(), index.getRecordCount());
    List<String> labels = index.getBucketLabels();
    ImmutableIndex<Integer> immIndex = index.buildImmutableIndex();
    logln(desiredLocale + "\t" + desiredLocale.getDisplayName(ULocale.ENGLISH) + " - " + desiredLocale.getDisplayName(desiredLocale) + "\t" + index.getCollator().getLocale(ULocale.ACTUAL_LOCALE));
    UI.setLength(0);
    UI.append(desiredLocale + "\t");
    boolean showAll = true;
    // Show index at top. We could skip or gray out empty buckets
    for (AlphabeticIndex.Bucket<Integer> bucket : index) {
        if (showAll || bucket.size() != 0) {
            showLabelAtTop(UI, bucket.getLabel());
        }
    }
    logln(UI.toString());
    // Show the buckets with their contents, skipping empty buckets
    int bucketIndex = 0;
    for (Bucket<Integer> bucket : index) {
        assertEquals("bucket label vs. iterator", labels.get(bucketIndex), bucket.getLabel());
        assertEquals("bucket label vs. immutable", labels.get(bucketIndex), immIndex.getBucket(bucketIndex).getLabel());
        assertEquals("bucket label type vs. immutable", bucket.getLabelType(), immIndex.getBucket(bucketIndex).getLabelType());
        for (Record<Integer> r : bucket) {
            CharSequence name = r.getName();
            assertEquals("getBucketIndex(" + name + ")", bucketIndex, index.getBucketIndex(name));
            assertEquals("immutable getBucketIndex(" + name + ")", bucketIndex, immIndex.getBucketIndex(name));
        }
        if (bucket.getLabel().equals(testBucket)) {
            Counter<String> keys = getKeys(bucket);
            for (String item : items) {
                long globalCount = itemCount.get(item);
                long localeCount = keys.get(item);
                if (globalCount != localeCount) {
                    errln("Error: in " + "'" + testBucket + "', '" + item + "' should have count " + globalCount + " but has count " + localeCount);
                }
            }
        }
        if (bucket.size() != 0) {
            showLabelInList(UI, bucket.getLabel());
            for (AlphabeticIndex.Record<Integer> item : bucket) {
                showIndexedItem(UI, item.getName(), item.getData());
            }
            logln(UI.toString());
        }
        ++bucketIndex;
    }
    assertEquals("getBucketCount()", bucketIndex, index.getBucketCount());
    assertEquals("immutable getBucketCount()", bucketIndex, immIndex.getBucketCount());
    assertNull("immutable getBucket(-1)", immIndex.getBucket(-1));
    assertNull("immutable getBucket(count)", immIndex.getBucket(bucketIndex));
    for (Bucket<Integer> bucket : immIndex) {
        assertEquals("immutable bucket size", 0, bucket.size());
        assertFalse("immutable bucket iterator.hasNext()", bucket.iterator().hasNext());
    }
}
Also used : ULocale(android.icu.util.ULocale) AlphabeticIndex(android.icu.text.AlphabeticIndex)

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