Search in sources :

Example 1 with R4

use of android.icu.impl.Row.R4 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

R4 (android.icu.impl.Row.R4)1 AlphabeticIndex (android.icu.text.AlphabeticIndex)1 RawCollationKey (android.icu.text.RawCollationKey)1 RuleBasedCollator (android.icu.text.RuleBasedCollator)1 UnicodeSet (android.icu.text.UnicodeSet)1 ULocale (android.icu.util.ULocale)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Test (org.junit.Test)1