Search in sources :

Example 1 with RawCollationKey

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

the class CollationTest method doTestVariant.

private static void doTestVariant(TestFmwk test, RuleBasedCollator myCollation, String source, String target, int result) {
    int compareResult = myCollation.compare(source, target);
    if (compareResult != result) {
        // !!! if not mod build, error, else nothing.
        // warnln if not build, error, else always print warning.
        // do we need a 'quiet warning?' (err or log).  Hmmm,
        // would it work to have the 'verbose' flag let you
        // suppress warnings?  Are there ever some warnings you
        // want to suppress, and others you don't?
        TestFmwk.errln("Comparing \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
    CollationKey ssk = myCollation.getCollationKey(source);
    CollationKey tsk = myCollation.getCollationKey(target);
    compareResult = ssk.compareTo(tsk);
    if (compareResult != result) {
        TestFmwk.errln("Comparing CollationKeys of \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
    RawCollationKey srsk = new RawCollationKey();
    myCollation.getRawCollationKey(source, srsk);
    RawCollationKey trsk = new RawCollationKey();
    myCollation.getRawCollationKey(target, trsk);
    compareResult = ssk.compareTo(tsk);
    if (compareResult != result) {
        TestFmwk.errln("Comparing RawCollationKeys of \"" + Utility.hex(source) + "\" with \"" + Utility.hex(target) + "\" expected " + result + " but got " + compareResult);
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey)

Example 2 with RawCollationKey

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

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

the class CollationAPITest method TestSubClass.

/**
 * Simple test to see if Collator is subclassable.
 * Also test coverage of base class methods that are overridden by RuleBasedCollator.
 */
@Test
public void TestSubClass() {
    class TestCollator extends Collator {

        @Override
        public boolean equals(Object that) {
            return this == that;
        }

        @Override
        public int hashCode() {
            return 0;
        }

        @Override
        public int compare(String source, String target) {
            return source.compareTo(target);
        }

        @Override
        public CollationKey getCollationKey(String source) {
            return new CollationKey(source, getRawCollationKey(source, new RawCollationKey()));
        }

        @Override
        public RawCollationKey getRawCollationKey(String source, RawCollationKey key) {
            byte[] temp1 = source.getBytes();
            byte[] temp2 = new byte[temp1.length + 1];
            System.arraycopy(temp1, 0, temp2, 0, temp1.length);
            temp2[temp1.length] = 0;
            if (key == null) {
                key = new RawCollationKey();
            }
            key.bytes = temp2;
            key.size = temp2.length;
            return key;
        }

        @Override
        public void setVariableTop(int ce) {
            if (isFrozen()) {
                throw new UnsupportedOperationException("Attempt to modify frozen object");
            }
        }

        @Override
        public int setVariableTop(String str) {
            if (isFrozen()) {
                throw new UnsupportedOperationException("Attempt to modify frozen object");
            }
            return 0;
        }

        @Override
        public int getVariableTop() {
            return 0;
        }

        @Override
        public VersionInfo getVersion() {
            return VersionInfo.getInstance(0);
        }

        @Override
        public VersionInfo getUCAVersion() {
            return VersionInfo.getInstance(0);
        }
    }
    Collator col1 = new TestCollator();
    Collator col2 = new TestCollator();
    if (col1.equals(col2)) {
        errln("2 different instance of TestCollator should fail");
    }
    if (col1.hashCode() != col2.hashCode()) {
        errln("Every TestCollator has the same hashcode");
    }
    String abc = "abc";
    String bcd = "bcd";
    if (col1.compare(abc, bcd) != abc.compareTo(bcd)) {
        errln("TestCollator compare should be the same as the default " + "string comparison");
    }
    CollationKey key = col1.getCollationKey(abc);
    byte[] temp1 = abc.getBytes();
    byte[] temp2 = new byte[temp1.length + 1];
    System.arraycopy(temp1, 0, temp2, 0, temp1.length);
    temp2[temp1.length] = 0;
    if (!java.util.Arrays.equals(key.toByteArray(), temp2) || !key.getSourceString().equals(abc)) {
        errln("TestCollator collationkey API is returning wrong values");
    }
    UnicodeSet set = col1.getTailoredSet();
    if (!set.equals(new UnicodeSet(0, 0x10FFFF))) {
        errln("Error getting default tailored set");
    }
    // Base class code coverage.
    // Most of these methods are dummies;
    // they are overridden by any subclass that supports their features.
    assertEquals("compare(strings as Object)", 0, col1.compare(new StringBuilder("abc"), new StringBuffer("abc")));
    col1.setStrength(Collator.SECONDARY);
    assertNotEquals("getStrength()", Collator.PRIMARY, col1.getStrength());
    // setStrength2() is @internal and returns this.
    // The base class getStrength() always returns the same value,
    // since the base class does not have a field to store the strength.
    assertNotEquals("setStrength2().getStrength()", Collator.PRIMARY, col1.setStrength2(Collator.IDENTICAL).getStrength());
    // (base class).setDecomposition() may or may not be implemented.
    try {
        col1.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    } catch (UnsupportedOperationException expected) {
    }
    // don't care about the value
    assertNotEquals("getDecomposition()", -1, col1.getDecomposition());
    // (base class).setMaxVariable() may or may not be implemented.
    try {
        col1.setMaxVariable(Collator.ReorderCodes.CURRENCY);
    } catch (UnsupportedOperationException expected) {
    }
    // don't care about the value
    assertNotEquals("getMaxVariable()", -1, col1.getMaxVariable());
    // (base class).setReorderCodes() may or may not be implemented.
    try {
        col1.setReorderCodes(0, 1, 2);
    } catch (UnsupportedOperationException expected) {
    }
    try {
        col1.getReorderCodes();
    } catch (UnsupportedOperationException expected) {
    }
    assertFalse("getDisplayName()", Collator.getDisplayName(Locale.GERMAN).isEmpty());
    assertFalse("getDisplayName()", Collator.getDisplayName(Locale.GERMAN, Locale.ITALIAN).isEmpty());
    assertNotEquals("getLocale()", ULocale.GERMAN, col1.getLocale(ULocale.ACTUAL_LOCALE));
    // Cover Collator.setLocale() which is only package-visible.
    Object token = Collator.registerInstance(new TestCollator(), new ULocale("de-Japn-419"));
    Collator.unregister(token);
    // Freezable default implementations. freeze() may or may not be implemented.
    assertFalse("not yet frozen", col2.isFrozen());
    try {
        col2.freeze();
        assertTrue("now frozen", col2.isFrozen());
    } catch (UnsupportedOperationException expected) {
    }
    try {
        col2.setStrength(Collator.PRIMARY);
        if (col2.isFrozen()) {
            fail("(frozen Collator).setStrength() should throw an exception");
        }
    } catch (UnsupportedOperationException expected) {
    }
    try {
        Collator col3 = col2.cloneAsThawed();
        assertFalse("!cloneAsThawed().isFrozen()", col3.isFrozen());
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) ULocale(android.icu.util.ULocale) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) UnicodeSet(android.icu.text.UnicodeSet) Collator(android.icu.text.Collator) RuleBasedCollator(android.icu.text.RuleBasedCollator) Test(org.junit.Test)

Example 4 with RawCollationKey

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

the class CollationAPITest method TestRawCollationKey.

@Test
public void TestRawCollationKey() {
    // testing constructors
    RawCollationKey key = new RawCollationKey();
    if (key.bytes != null || key.size != 0) {
        errln("Empty default constructor expected to leave the bytes null " + "and size 0");
    }
    byte[] array = new byte[128];
    key = new RawCollationKey(array);
    if (key.bytes != array || key.size != 0) {
        errln("Constructor taking an array expected to adopt it and " + "retaining its size 0");
    }
    try {
        key = new RawCollationKey(array, 129);
        errln("Constructor taking an array and a size > array.length " + "expected to throw an exception");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: Constructor failed as expected");
    }
    try {
        key = new RawCollationKey(array, -1);
        errln("Constructor taking an array and a size < 0 " + "expected to throw an exception");
    } catch (IndexOutOfBoundsException e) {
        logln("PASS: Constructor failed as expected");
    }
    key = new RawCollationKey(array, array.length >> 1);
    if (key.bytes != array || key.size != (array.length >> 1)) {
        errln("Constructor taking an array and a size, " + "expected to adopt it and take the size specified");
    }
    key = new RawCollationKey(10);
    if (key.bytes == null || key.bytes.length != 10 || key.size != 0) {
        errln("Constructor taking a specified capacity expected to " + "create a new internal byte array with length 10 and " + "retain size 0");
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) Test(org.junit.Test)

Example 5 with RawCollationKey

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

the class CollationMiscTest method TestRawCollationKeyCompareTo.

/* Test the method public int compareTo(RawCollationKey rhs) */
@Test
public void TestRawCollationKeyCompareTo() {
    RawCollationKey rck = new RawCollationKey();
    byte[] b = { (byte) 10, (byte) 20 };
    RawCollationKey rck100 = new RawCollationKey(b, 2);
    if (rck.compareTo(rck) != 0) {
        errln("RawCollatonKey.compareTo(RawCollationKey) was suppose to return 0 " + "for two idential RawCollationKey objects.");
    }
    if (rck.compareTo(rck100) == 0) {
        errln("RawCollatonKey.compareTo(RawCollationKey) was not suppose to return 0 " + "for two different RawCollationKey objects.");
    }
}
Also used : RawCollationKey(android.icu.text.RawCollationKey) Test(org.junit.Test)

Aggregations

RawCollationKey (android.icu.text.RawCollationKey)6 Test (org.junit.Test)4 CollationKey (android.icu.text.CollationKey)2 RuleBasedCollator (android.icu.text.RuleBasedCollator)2 UnicodeSet (android.icu.text.UnicodeSet)2 ULocale (android.icu.util.ULocale)2 R4 (android.icu.impl.Row.R4)1 AlphabeticIndex (android.icu.text.AlphabeticIndex)1 Collator (android.icu.text.Collator)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1