Search in sources :

Example 11 with UnicodeSet

use of android.icu.text.UnicodeSet 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 12 with UnicodeSet

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

the class CollationAPITest method TestGetContractions.

// capitst.c/TestGetContractionsAndUnsafes()
@Test
public void TestGetContractions() throws Exception {
    /*        static struct {
         const char* locale;
         const char* inConts;
         const char* outConts;
         const char* inExp;
         const char* outExp;
         const char* unsafeCodeUnits;
         const char* safeCodeUnits;
         }
         */
    String[][] tests = { { "ru", "[{\u0418\u0306}{\u0438\u0306}]", "[\u0439\u0457]", "[\u00e6]", "[ae]", "[\u0418\u0438]", "[aAbBxv]" }, { "uk", "[{\u0406\u0308}{\u0456\u0308}{\u0418\u0306}{\u0438\u0306}]", "[\u0407\u0419\u0439\u0457]", "[\u00e6]", "[ae]", "[\u0406\u0456\u0418\u0438]", "[aAbBxv]" }, { "sh", "[{C\u0301}{C\u030C}{C\u0341}{DZ\u030C}{Dz\u030C}{D\u017D}{D\u017E}{lj}{nj}]", "[{\u309d\u3099}{\u30fd\u3099}]", "[\u00e6]", "[a]", "[nlcdzNLCDZ]", "[jabv]" }, { "ja", /*
                     * The "collv2" builder omits mappings if the collator maps their
                     * character sequences to the same CEs.
                     * For example, it omits Japanese contractions for NFD forms
                     * of the voiced iteration mark (U+309E = U+309D + U+3099), such as
                     * {\u3053\u3099\u309D\u3099}{\u3053\u309D\u3099}
                     * {\u30B3\u3099\u30FD\u3099}{\u30B3\u30FD\u3099}.
                     * It does add mappings for the precomposed forms.
                     */
    "[{\u3053\u3099\u309D}{\u3053\u3099\u309E}{\u3053\u3099\u30FC}" + "{\u3053\u309D}{\u3053\u309E}{\u3053\u30FC}" + "{\u30B3\u3099\u30FC}{\u30B3\u3099\u30FD}{\u30B3\u3099\u30FE}" + "{\u30B3\u30FC}{\u30B3\u30FD}{\u30B3\u30FE}]", "[{\u30FD\u3099}{\u309D\u3099}{\u3053\u3099}{\u30B3\u3099}{lj}{nj}]", "[\u30FE\u00e6]", "[a]", "[\u3099]", "[]" } };
    RuleBasedCollator coll = null;
    int i = 0;
    UnicodeSet conts = new UnicodeSet();
    UnicodeSet exp = new UnicodeSet();
    UnicodeSet set = new UnicodeSet();
    for (i = 0; i < tests.length; i++) {
        logln("Testing locale: " + tests[i][0]);
        coll = (RuleBasedCollator) Collator.getInstance(new ULocale(tests[i][0]));
        coll.getContractionsAndExpansions(conts, exp, true);
        boolean ok = true;
        logln("Contractions " + conts.size() + ":\n" + conts.toPattern(true));
        ok &= doSetsTest(conts, set, tests[i][1], tests[i][2]);
        logln("Expansions " + exp.size() + ":\n" + exp.toPattern(true));
        ok &= doSetsTest(exp, set, tests[i][3], tests[i][4]);
        if (!ok) {
            // In case of failure, log the rule string for better diagnostics.
            String rules = coll.getRules(false);
            logln("Collation rules (getLocale()=" + coll.getLocale(ULocale.ACTUAL_LOCALE).toString() + "): " + Utility.escape(rules));
        }
    // No unsafe set in ICU4J
    // noConts = ucol_getUnsafeSet(coll, conts, &status);
    // doSetsTest(conts, set, tests[i][5], tests[i][6]);
    // log_verbose("Unsafes "+conts.size()+":\n"+conts.toPattern(true)+"\n");
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) ULocale(android.icu.util.ULocale) UnicodeSet(android.icu.text.UnicodeSet) Test(org.junit.Test)

Example 13 with UnicodeSet

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

the class CollationAPITest method TestGetTailoredSet.

@Test
public void TestGetTailoredSet() {
    logln("testing getTailoredSet...");
    String[] rules = { "&a < \u212b", "& S < \u0161 <<< \u0160" };
    String[][] data = { { "\u212b", "A\u030a", "\u00c5" }, { "\u0161", "s\u030C", "\u0160", "S\u030C" } };
    int i = 0, j = 0;
    RuleBasedCollator coll;
    UnicodeSet set;
    for (i = 0; i < rules.length; i++) {
        try {
            logln("Instantiating a collator from " + rules[i]);
            coll = new RuleBasedCollator(rules[i]);
            set = coll.getTailoredSet();
            logln("Got set: " + set.toPattern(true));
            if (set.size() < data[i].length) {
                errln("Tailored set size smaller (" + set.size() + ") than expected (" + data[i].length + ")");
            }
            for (j = 0; j < data[i].length; j++) {
                logln("Checking to see whether " + data[i][j] + " is in set");
                if (!set.contains(data[i][j])) {
                    errln("Tailored set doesn't contain " + data[i][j] + "... It should");
                }
            }
        } catch (Exception e) {
            warnln("Couldn't open collator with rules " + rules[i]);
        }
    }
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) UnicodeSet(android.icu.text.UnicodeSet) MissingResourceException(java.util.MissingResourceException) Test(org.junit.Test)

Example 14 with UnicodeSet

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

the class UnicodeMapTest method TestUnicodeMapGeneralCategory.

public void TestUnicodeMapGeneralCategory() {
    logln("Setting General Category");
    UnicodeMap<String> map1 = new UnicodeMap();
    Map<Integer, String> map2 = new HashMap<Integer, String>();
    // Map<Integer, String> map3 = new TreeMap<Integer, String>();
    map1 = new UnicodeMap<String>();
    map2 = new TreeMap<Integer, String>();
    for (int cp = 0; cp <= SET_LIMIT; ++cp) {
        int enumValue = UCharacter.getIntPropertyValue(cp, propEnum);
        // if (enumValue <= 0) continue; // for smaller set
        String value = UCharacter.getPropertyValueName(propEnum, enumValue, UProperty.NameChoice.LONG);
        map1.put(cp, value);
        map2.put(cp, value);
    }
    checkNext(map1, map2, Integer.MAX_VALUE);
    logln("Comparing General Category");
    check(map1, map2, -1);
    logln("Comparing Values");
    Set<String> values1 = map1.getAvailableValues(new TreeSet<String>());
    Set<String> values2 = new TreeSet<String>(map2.values());
    if (!TestBoilerplate.verifySetsIdentical(this, values1, values2)) {
        throw new IllegalArgumentException("Halting");
    }
    logln("Comparing Sets");
    for (Iterator<String> it = values1.iterator(); it.hasNext(); ) {
        String value = it.next();
        logln(value == null ? "null" : value);
        UnicodeSet set1 = map1.keySet(value);
        UnicodeSet set2 = TestBoilerplate.getSet(map2, value);
        if (!TestBoilerplate.verifySetsIdentical(this, set1, set2)) {
            throw new IllegalArgumentException("Halting");
        }
    }
}
Also used : HashMap(java.util.HashMap) UnicodeSet(android.icu.text.UnicodeSet) TreeSet(java.util.TreeSet) UnicodeMap(android.icu.dev.util.UnicodeMap)

Example 15 with UnicodeSet

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

the class WriteCharts method printSet.

public static void printSet(String source) {
    UnicodeSet s = new UnicodeSet(source);
    System.out.println("Printout for '" + source + "'");
    int count = s.getRangeCount();
    for (int i = 0; i < count; ++i) {
        int start = s.getRangeStart(i);
        int end = s.getRangeEnd(i);
        System.out.println(Integer.toString(start, 16) + ".." + Integer.toString(end, 16));
    }
}
Also used : UnicodeSet(android.icu.text.UnicodeSet)

Aggregations

UnicodeSet (android.icu.text.UnicodeSet)158 Test (org.junit.Test)112 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)25 Transliterator (android.icu.text.Transliterator)19 ReplaceableString (android.icu.text.ReplaceableString)14 ULocale (android.icu.util.ULocale)13 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)9 Normalizer2 (android.icu.text.Normalizer2)7 RuleBasedCollator (android.icu.text.RuleBasedCollator)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 FilteredNormalizer2 (android.icu.text.FilteredNormalizer2)4 SpoofChecker (android.icu.text.SpoofChecker)4 TreeSet (java.util.TreeSet)4 UnicodeMap (android.icu.dev.util.UnicodeMap)3 AlphabeticIndex (android.icu.text.AlphabeticIndex)3 CollationKey (android.icu.text.CollationKey)3 RawCollationKey (android.icu.text.RawCollationKey)3 CheckResult (android.icu.text.SpoofChecker.CheckResult)3 SpanCondition (android.icu.text.UnicodeSet.SpanCondition)3