/* GENERATED SOURCE. DO NOT MODIFY. */ /* ******************************************************************************* * Copyright (C) 2002-2015, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* */ /** * Port From: ICU4C v2.1 : cintltest * Source File: $ICU4CRoot/source/test/cintltest/cmsccoll.c */ package android.icu.dev.test.collator; import java.util.Arrays; import java.util.Locale; import java.util.Set; import java.util.TreeSet; import android.icu.dev.test.TestFmwk; import android.icu.impl.ICUResourceBundle; import android.icu.impl.Utility; import android.icu.lang.UScript; import android.icu.text.CollationElementIterator; import android.icu.text.CollationKey; import android.icu.text.CollationKey.BoundMode; import android.icu.text.Collator; import android.icu.text.Collator.ReorderCodes; import android.icu.text.Normalizer; import android.icu.text.RawCollationKey; import android.icu.text.RuleBasedCollator; import android.icu.text.UTF16; import android.icu.text.UnicodeSet; import android.icu.text.UnicodeSetIterator; import android.icu.util.ULocale; import android.icu.util.UResourceBundle; import org.junit.runner.RunWith; import android.icu.junit.IcuTestFmwkRunner; @RunWith(IcuTestFmwkRunner.class) public class CollationMiscTest extends TestFmwk { public static void main(String[] args) throws Exception { new CollationMiscTest().run(args); // new CollationMiscTest().TestLocaleRuleBasedCollators(); } //private static final int NORM_BUFFER_TEST_LEN_ = 32; private static final class Tester { int u; String NFC; String NFD; } private static final boolean hasCollationElements(Locale locale) { ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,locale); if (rb != null) { try { String collkey = rb.getStringWithFallback("collations/default"); ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey); if (elements != null) { return true; } } catch (Exception e) { } } return false; } public void TestComposeDecompose() { Tester t[] = new Tester[0x30000]; t[0] = new Tester(); logln("Testing UCA extensively\n"); RuleBasedCollator coll; try { coll = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH); } catch (Exception e) { warnln("Error opening collator\n"); return; } int noCases = 0; for (int u = 0; u < 0x30000; u ++) { String comp = UTF16.valueOf(u); int len = comp.length(); t[noCases].NFC = Normalizer.normalize(u, Normalizer.NFC); t[noCases].NFD = Normalizer.normalize(u, Normalizer.NFD); if (t[noCases].NFC.length() != t[noCases].NFD.length() || (t[noCases].NFC.compareTo(t[noCases].NFD) != 0) || (len != t[noCases].NFD.length()) || (comp.compareTo(t[noCases].NFD) != 0)) { t[noCases].u = u; if (len != t[noCases].NFD.length() || (comp.compareTo(t[noCases].NFD) != 0)) { t[noCases].NFC = comp; } noCases ++; t[noCases] = new Tester(); } } for (int u = 0; u < noCases; u ++) { if (!coll.equals(t[u].NFC, t[u].NFD)) { errln("Failure: codePoint \\u" + Integer.toHexString(t[u].u) + " fails TestComposeDecompose in the UCA"); CollationTest.doTest(this, coll, t[u].NFC, t[u].NFD, 0); } } logln("Testing locales, number of cases = " + noCases); Locale loc[] = Collator.getAvailableLocales(); for (int i = 0; i < loc.length; i ++) { if (hasCollationElements(loc[i])) { logln("Testing locale " + loc[i].getDisplayName()); coll = (RuleBasedCollator)Collator.getInstance(loc[i]); coll.setStrength(Collator.IDENTICAL); for (int u = 0; u < noCases; u ++) { if (!coll.equals(t[u].NFC, t[u].NFD)) { errln("Failure: codePoint \\u" + Integer.toHexString(t[u].u) + " fails TestComposeDecompose for locale " + loc[i].getDisplayName()); // this tests for the iterators too CollationTest.doTest(this, coll, t[u].NFC, t[u].NFD, 0); } } } } } public void TestRuleOptions() { // values here are hardcoded and are correct for the current UCA when // the UCA changes, one might be forced to change these values. /* * These strings contain the last character before [variable top] * and the first and second characters (by primary weights) after it. * See FractionalUCA.txt. For example: [last variable [0C FE, 05, 05]] # U+10A7F OLD SOUTH ARABIAN NUMERIC INDICATOR [variable top = 0C FE] [first regular [0D 0A, 05, 05]] # U+0060 GRAVE ACCENT and 00B4; [0D 0C, 05, 05] * * Note: Starting with UCA 6.0, the [variable top] collation element * is not the weight of any character or string, * which means that LAST_VARIABLE_CHAR_STRING sorts before [last variable]. */ String LAST_VARIABLE_CHAR_STRING = "\\U00010A7F"; String FIRST_REGULAR_CHAR_STRING = "\\u0060"; String SECOND_REGULAR_CHAR_STRING = "\\u00B4"; /* * This string has to match the character that has the [last regular] weight * which changes with each UCA version. * See the bottom of FractionalUCA.txt which says something like [last regular [7A FE, 05, 05]] # U+1342E EGYPTIAN HIEROGLYPH AA032 * * Note: Starting with UCA 6.0, the [last regular] collation element * is not the weight of any character or string, * which means that LAST_REGULAR_CHAR_STRING sorts before [last regular]. */ String LAST_REGULAR_CHAR_STRING = "\\U0001342E"; String[] rules = { // cannot test this anymore, as [last primary ignorable] doesn't // have a code point associated to it anymore // "&[before 3][last primary ignorable]<< 1) { errln("***** invalid call to reportCResult ****"); return; } boolean ok1 = (compareResult == expectedResult); boolean ok2 = (keyResult == expectedResult); boolean ok3 = (incResult == expectedResult); if (ok1 && ok2 && ok3 /* synwee to undo && !isVerbose()*/) { return; } else { String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\""; String msg2 = "\", \""; String msg3 = "\") returned "; String msg4 = "; expected "; String sExpect = new String(""); String sResult = new String(""); sResult = CollationTest.appendCompareResult(compareResult, sResult); sExpect = CollationTest.appendCompareResult(expectedResult, sExpect); if (ok1) { // logln(msg1 + source + msg2 + target + msg3 + sResult); } else { errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); } msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\""; msg2 = "\").compareTo(key(\""; msg3 = "\")) returned "; sResult = CollationTest.appendCompareResult(keyResult, sResult); if (ok2) { // logln(msg1 + source + msg2 + target + msg3 + sResult); } else { errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); msg1 = " "; msg2 = " vs. "; errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey)); } msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\""; msg2 = "\", \""; msg3 = "\") returned "; sResult = CollationTest.appendCompareResult(incResult, sResult); if (ok3) { // logln(msg1 + source + msg2 + target + msg3 + sResult); } else { errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); } } } public void TestBeforePrefixFailure() { String[] rules = { "&g <<< a&[before 3]\uff41 <<< x", "&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74&[before 3]\u30a7<<<\u30a9", "&[before 3]\u30a7<<<\u30a9&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74", }; String[][] data = { {"x", "\uff41"}, {"\u30a9", "\u30a7"}, {"\u30a9", "\u30a7"}, }; for(int i = 0; i< rules.length; i++) { genericRulesStarter(rules[i], data[i]); } } public void TestContractionClosure() { // Note: This was also ported to the data-driven test, see collationtest.txt. String[] rules = { "&b=\u00e4\u00e4", "&b=\u00C5", }; String[][] data = { { "b", "\u00e4\u00e4", "a\u0308a\u0308", "\u00e4a\u0308", "a\u0308\u00e4" }, { "b", "\u00C5", "A\u030A", "\u212B" }, }; for(int i = 0; i< rules.length; i++) { genericRulesStarterWithResult(rules[i], data[i], 0); } } public void TestPrefixCompose() { String rule1 = "&\u30a7<<<\u30ab|\u30fc=\u30ac|\u30fc"; String string = rule1; try { RuleBasedCollator coll = new RuleBasedCollator(string); logln("rule:" + coll.getRules()); } catch (Exception e) { warnln("Error open RuleBasedCollator rule = " + string); } } public void TestStrCollIdenticalPrefix() { String rule = "&\ud9b0\udc70=\ud9b0\udc71"; String test[] = { "ab\ud9b0\udc70", "ab\ud9b0\udc71" }; genericRulesStarterWithResult(rule, test, 0); } public void TestPrefix() { String[] rules = { "&z <<< z|a", "&z <<< z| a", "[strength I]&a=\ud900\udc25&z<<<\ud900\udc25|a", }; String[][] data = { {"zz", "za"}, {"zz", "za"}, {"aa", "az", "\ud900\udc25z", "\ud900\udc25a", "zz"}, }; for(int i = 0; i 0 && !rule.equals(prevrule)) { RuleBasedCollator col2 = new RuleBasedCollator(rule); if (!col1.equals(col2)) { errln("Error creating RuleBasedCollator from " + "locale rules for " + l.toString()); } } prevrule = rule; } catch (Exception e) { warnln("Error retrieving resource bundle for testing: " + e.toString()); } } } public void TestOptimize() { /* this is not really a test - just trying out * whether copying of UCA contents will fail * Cannot really test, since the functionality * remains the same. */ String rules[] = { "[optimize [\\uAC00-\\uD7FF]]" }; String data[][] = { { "a", "b"} }; int i = 0; for(i = 0; i 0) { if (mergedPrefixKeys[i-1].compareTo(mergedPrefixKeys[i]) >= 0) { errln("Error while comparing prefixed keys @ strength " + strength); errln(CollationTest.prettify(mergedPrefixKeys[i-1])); errln(CollationTest.prettify(mergedPrefixKeys[i])); } if (mergedSuffixKeys[i-1].compareTo(mergedSuffixKeys[i]) >= 0) { errln("Error while comparing suffixed keys @ strength " + strength); errln(CollationTest.prettify(mergedSuffixKeys[i-1])); errln(CollationTest.prettify(mergedSuffixKeys[i])); } } } if (strength == Collator.QUATERNARY) { strength = Collator.IDENTICAL; } else { strength ++; } } } public void TestVariableTop() { // ICU 53+: The character must be in a supported reordering group, // and the variable top is pinned to the end of that group. // parseNextToken is not released as public so i create my own rules String rules = "& ' ' < b < c < de < fg & hi = j"; try { RuleBasedCollator coll = new RuleBasedCollator(rules); String tokens[] = {" ", "b", "c", "de", "fg", "hi", "j", "ab"}; coll.setAlternateHandlingShifted(true); for (int i = 0; i < tokens.length; i ++) { int varTopOriginal = coll.getVariableTop(); try { int varTop = coll.setVariableTop(tokens[i]); if (i > 4) { errln("Token " + tokens[i] + " expected to fail"); } if (varTop != coll.getVariableTop()) { errln("Error setting and getting variable top"); } CollationKey key1 = coll.getCollationKey(tokens[i]); for (int j = 0; j < i; j ++) { CollationKey key2 = coll.getCollationKey(tokens[j]); if (key2.compareTo(key1) < 0) { errln("Setting variable top shouldn't change the comparison sequence"); } byte sortorder[] = key2.toByteArray(); if (sortorder.length > 0 && (key2.toByteArray())[0] > 1) { errln("Primary sort order should be 0"); } } } catch (Exception e) { CollationElementIterator iter = coll.getCollationElementIterator(tokens[i]); /*int ce =*/ iter.next(); int ce2 = iter.next(); if (ce2 == CollationElementIterator.NULLORDER) { errln("Token " + tokens[i] + " not expected to fail"); } if (coll.getVariableTop() != varTopOriginal) { errln("When exception is thrown variable top should " + "not be changed"); } } coll.setVariableTop(varTopOriginal); if (varTopOriginal != coll.getVariableTop()) { errln("Couldn't restore old variable top\n"); } } // Testing calling with error set try { coll.setVariableTop(""); errln("Empty string should throw an IllegalArgumentException"); } catch (IllegalArgumentException e) { logln("PASS: Empty string failed as expected"); } try { coll.setVariableTop(null); errln("Null string should throw an IllegalArgumentException"); } catch (IllegalArgumentException e) { logln("PASS: null string failed as expected"); } } catch (Exception e) { warnln("Error creating RuleBasedCollator"); } } // ported from cmsccoll.c public void TestVariableTopSetting() { int varTopOriginal = 0, varTop1, varTop2; Collator coll = Collator.getInstance(ULocale.ROOT); String empty = ""; String space = " "; String dot = "."; /* punctuation */ String degree = "\u00b0"; /* symbol */ String dollar = "$"; /* currency symbol */ String zero = "0"; /* digit */ varTopOriginal = coll.getVariableTop(); logln(String.format("coll.getVariableTop(root) -> %08x", varTopOriginal)); ((RuleBasedCollator)coll).setAlternateHandlingShifted(true); varTop1 = coll.setVariableTop(space); varTop2 = coll.getVariableTop(); logln(String.format("coll.setVariableTop(space) -> %08x", varTop1)); if(varTop1 != varTop2 || !coll.equals(empty, space) || coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(space, dot) >= 0) { errln("coll.setVariableTop(space) did not work"); } varTop1 = coll.setVariableTop(dot); varTop2 = coll.getVariableTop(); logln(String.format("coll.setVariableTop(dot) -> %08x", varTop1)); if(varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dot, degree) >= 0) { errln("coll.setVariableTop(dot) did not work"); } varTop1 = coll.setVariableTop(degree); varTop2 = coll.getVariableTop(); logln(String.format("coll.setVariableTop(degree) -> %08x", varTop1)); if(varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(degree, dollar) >= 0) { errln("coll.setVariableTop(degree) did not work"); } varTop1 = coll.setVariableTop(dollar); varTop2 = coll.getVariableTop(); logln(String.format("coll.setVariableTop(dollar) -> %08x", varTop1)); if(varTop1 != varTop2 || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || !coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dollar, zero) >= 0) { errln("coll.setVariableTop(dollar) did not work"); } logln("Testing setting variable top to contractions"); try { coll.setVariableTop("@P"); errln("Invalid contraction succeded in setting variable top!"); } catch(Exception expected) { } logln("Test restoring variable top"); coll.setVariableTop(varTopOriginal); if(varTopOriginal != coll.getVariableTop()) { errln("Couldn't restore old variable top"); } } // ported from cmsccoll.c public void TestMaxVariable() { int oldMax, max; String empty = ""; String space = " "; String dot = "."; /* punctuation */ String degree = "\u00b0"; /* symbol */ String dollar = "$"; /* currency symbol */ String zero = "0"; /* digit */ Collator coll = Collator.getInstance(ULocale.ROOT); oldMax = coll.getMaxVariable(); logln(String.format("coll.getMaxVariable(root) -> %04x", oldMax)); ((RuleBasedCollator)coll).setAlternateHandlingShifted(true); coll.setMaxVariable(Collator.ReorderCodes.SPACE); max = coll.getMaxVariable(); logln(String.format("coll.setMaxVariable(space) -> %04x", max)); if(max != Collator.ReorderCodes.SPACE || !coll.equals(empty, space) || coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(space, dot) >= 0) { errln("coll.setMaxVariable(space) did not work"); } coll.setMaxVariable(Collator.ReorderCodes.PUNCTUATION); max = coll.getMaxVariable(); logln(String.format("coll.setMaxVariable(punctuation) -> %04x", max)); if(max != Collator.ReorderCodes.PUNCTUATION || !coll.equals(empty, space) || !coll.equals(empty, dot) || coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dot, degree) >= 0) { errln("coll.setMaxVariable(punctuation) did not work"); } coll.setMaxVariable(Collator.ReorderCodes.SYMBOL); max = coll.getMaxVariable(); logln(String.format("coll.setMaxVariable(symbol) -> %04x", max)); if(max != Collator.ReorderCodes.SYMBOL || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(degree, dollar) >= 0) { errln("coll.setMaxVariable(symbol) did not work"); } coll.setMaxVariable(Collator.ReorderCodes.CURRENCY); max = coll.getMaxVariable(); logln(String.format("coll.setMaxVariable(currency) -> %04x", max)); if(max != Collator.ReorderCodes.CURRENCY || !coll.equals(empty, space) || !coll.equals(empty, dot) || !coll.equals(empty, degree) || !coll.equals(empty, dollar) || coll.equals(empty, zero) || coll.compare(dollar, zero) >= 0) { errln("coll.setMaxVariable(currency) did not work"); } logln("Test restoring maxVariable"); coll.setMaxVariable(oldMax); if(oldMax != coll.getMaxVariable()) { errln("Couldn't restore old maxVariable"); } } public void TestUCARules() { try { // only root locale can have empty tailorings .. not English! RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(new Locale("","","")); String rule = coll.getRules(false); if (!rule.equals("")) { errln("Empty rule string should have empty rules " + rule); } rule = coll.getRules(true); if (rule.equals("")) { errln("UCA rule string should not be empty"); } coll = new RuleBasedCollator(rule); } catch (Exception e) { warnln(e.getMessage()); } } /** * Jitterbug 2726 */ public void TestShifted() { RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(); collator.setStrength(Collator.PRIMARY); collator.setAlternateHandlingShifted(true); CollationTest.doTest(this, collator, " a", "a", 0); // works properly CollationTest.doTest(this, collator, "a", "a ", 0); // inconsistent results } /** * Test for CollationElementIterator previous and next for the whole set of * unicode characters with normalization on. */ public void TestNumericCollation() { String basicTestStrings[] = {"hello1", "hello2", "hello123456"}; String preZeroTestStrings[] = {"avery1", "avery01", "avery001", "avery0001"}; String thirtyTwoBitNumericStrings[] = {"avery42949672960", "avery42949672961", "avery42949672962", "avery429496729610"}; String supplementaryDigits[] = {"\uD835\uDFCE", // 0 "\uD835\uDFCF", // 1 "\uD835\uDFD0", // 2 "\uD835\uDFD1", // 3 "\uD835\uDFCF\uD835\uDFCE", // 10 "\uD835\uDFCF\uD835\uDFCF", // 11 "\uD835\uDFCF\uD835\uDFD0", // 12 "\uD835\uDFD0\uD835\uDFCE", // 20 "\uD835\uDFD0\uD835\uDFCF", // 21 "\uD835\uDFD0\uD835\uDFD0" // 22 }; String foreignDigits[] = {"\u0661", "\u0662", "\u0663", "\u0661\u0660", "\u0661\u0662", "\u0661\u0663", "\u0662\u0660", "\u0662\u0662", "\u0662\u0663", "\u0663\u0660", "\u0663\u0662", "\u0663\u0663" }; //Additional tests to cover bug reported in #9476 String lastDigitDifferent[]={"2004","2005", "110005", "110006", "11005", "11006", "100000000005","100000000006"}; // Open our collator. RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH); String att[] = {"NumericCollation"}; Boolean val[] = {Boolean.TRUE}; genericLocaleStarterWithOptions(Locale.ENGLISH, basicTestStrings, att, val); genericLocaleStarterWithOptions(Locale.ENGLISH, thirtyTwoBitNumericStrings, att, val); genericLocaleStarterWithOptions(Locale.ENGLISH, foreignDigits, att, val); genericLocaleStarterWithOptions(Locale.ENGLISH, supplementaryDigits, att, val); // Setting up our collator to do digits. coll.setNumericCollation(true); // Testing that prepended zeroes still yield the correct collation // behavior. // We expect that every element in our strings array will be equal. for (int i = 0; i < preZeroTestStrings.length - 1; i ++) { for (int j = i + 1; j < preZeroTestStrings.length; j ++) { CollationTest.doTest(this, coll, preZeroTestStrings[i], preZeroTestStrings[j],0); } } //Testing that the behavior reported in #9476 is fixed //We expect comparisons between adjacent pairs will result in -1 for (int i=0; i < lastDigitDifferent.length -1; i=i+2 ) { CollationTest.doTest(this, coll, lastDigitDifferent[i], lastDigitDifferent[i+1], -1); } //cover setNumericCollationDefault, getNumericCollation assertTrue("The Numeric Collation setting is on", coll.getNumericCollation()); coll.setNumericCollationDefault(); logln("After set Numeric to default, the setting is: " + coll.getNumericCollation()); } public void Test3249() { String rule = "&x < a &z < a"; try { RuleBasedCollator coll = new RuleBasedCollator(rule); if(coll!=null){ logln("Collator did not throw an exception"); } } catch (Exception e) { warnln("Error creating RuleBasedCollator with " + rule + " failed"); } } public void TestTibetanConformance() { String test[] = {"\u0FB2\u0591\u0F71\u0061", "\u0FB2\u0F71\u0061"}; try { Collator coll = Collator.getInstance(); coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION); if (coll.compare(test[0], test[1]) != 0) { errln("Tibetan comparison error"); } CollationTest.doTest(this, (RuleBasedCollator)coll, test[0], test[1], 0); } catch (Exception e) { warnln("Error creating UCA collator"); } } public void TestJ3347() { try { Collator coll = Collator.getInstance(Locale.FRENCH); ((RuleBasedCollator)coll).setAlternateHandlingShifted(true); if (coll.compare("6", "!6") != 0) { errln("Jitterbug 3347 failed"); } } catch (Exception e) { warnln("Error creating UCA collator"); } } public void TestPinyinProblem() { String test[] = { "\u4E56\u4E56\u7761", "\u4E56\u5B69\u5B50" }; genericLocaleStarter(new Locale("zh", "", "PINYIN"), test); } /* supercedes TestJ784 */ public void TestBeforePinyin() { String rules = "&[before 2]A << \u0101 <<< \u0100 << \u00E1 <<< \u00C1 << \u01CE <<< \u01CD << \u00E0 <<< \u00C0" + "&[before 2]e << \u0113 <<< \u0112 << \u00E9 <<< \u00C9 << \u011B <<< \u011A << \u00E8 <<< \u00C8" + "&[before 2] i << \u012B <<< \u012A << \u00ED <<< \u00CD << \u01D0 <<< \u01CF << \u00EC <<< \u00CC" + "&[before 2] o << \u014D <<< \u014C << \u00F3 <<< \u00D3 << \u01D2 <<< \u01D1 << \u00F2 <<< \u00D2" + "&[before 2]u << \u016B <<< \u016A << \u00FA <<< \u00DA << \u01D4 <<< \u01D3 << \u00F9 <<< \u00D9" + "&U << \u01D6 <<< \u01D5 << \u01D8 <<< \u01D7 << \u01DA <<< \u01D9 << \u01DC <<< \u01DB << \u00FC"; String test[] = { "l\u0101", "la", "l\u0101n", "lan ", "l\u0113", "le", "l\u0113n", "len" }; String test2[] = { "x\u0101", "x\u0100", "X\u0101", "X\u0100", "x\u00E1", "x\u00C1", "X\u00E1", "X\u00C1", "x\u01CE", "x\u01CD", "X\u01CE", "X\u01CD", "x\u00E0", "x\u00C0", "X\u00E0", "X\u00C0", "xa", "xA", "Xa", "XA", "x\u0101x", "x\u0100x", "x\u00E1x", "x\u00C1x", "x\u01CEx", "x\u01CDx", "x\u00E0x", "x\u00C0x", "xax", "xAx" }; /* TODO: port builder fixes to before */ genericRulesStarter(rules, test); genericLocaleStarter(new Locale("zh","",""), test); genericRulesStarter(rules, test2); genericLocaleStarter(new Locale("zh","",""), test2); } public void TestUpperFirstQuaternary() { String tests[] = { "B", "b", "Bb", "bB" }; String[] att = { "strength", "UpperFirst" }; Object attVals[] = { new Integer(Collator.QUATERNARY), Boolean.TRUE }; genericLocaleStarterWithOptions(new Locale("root","",""), tests, att, attVals); } public void TestJ4960() { String tests[] = { "\\u00e2T", "aT" }; String att[] = { "strength", "CaseLevel" }; Object attVals[] = { new Integer(Collator.PRIMARY), Boolean.TRUE }; String tests2[] = { "a", "A" }; String rule = "&[first tertiary ignorable]=A=a"; String att2[] = { "CaseLevel" }; Object attVals2[] = { Boolean.TRUE }; // Test whether we correctly ignore primary ignorables on case level when // we have only primary & case level genericLocaleStarterWithOptionsAndResult(new Locale("root", ""), tests, att, attVals, 0); // Test whether ICU4J will make case level for sortkeys that have primary strength // and case level genericLocaleStarterWithOptions(new Locale("root", ""), tests2, att, attVals); // Test whether completely ignorable letters have case level info (they shouldn't) genericRulesStarterWithOptionsAndResult(rule, tests2, att2, attVals2, 0); } public void TestJB5298(){ ULocale[] locales = Collator.getAvailableULocales(); logln("Number of collator locales returned : " + locales.length); // double-check keywords String[] keywords = Collator.getKeywords(); if (keywords.length != 1 || !keywords[0].equals("collation")) { throw new IllegalArgumentException("internal collation error"); } String[] values = Collator.getKeywordValues("collation"); log("Collator.getKeywordValues returned: "); for(int i=0; i= 0 ? can.substring(idx+11, can.length()) : ""; if(val.length()>0 && !foundValues.contains(val)){ errln("Unknown collation found "+ can); } } } logln(" "); } public void TestJ5367() { String[] test = { "a", "y" }; String rules = "&Ny << Y &[first secondary ignorable] <<< a"; genericRulesStarter(rules, test); } public void TestVI5913() { String rules[] = { "&a < \u00e2 <<< \u00c2", "&a < \u1FF3 ", // OMEGA WITH YPOGEGRAMMENI "&s < \u0161 ", // &s < s with caron /* * Note: Just tailoring &z=0) { CollationKey key = coll.getCollationKey(firstVariable); logln("Collation key for 0x0009: "+CollationTest.prettify(key)); errln("Error! String \"a\" must be less than 0x0009 - [First Variable]"); } CollationKey keyB = coll.getCollationKey("b"); logln("Key for \"b\":"+ CollationTest.prettify(keyB)); if (keyB.compareTo(coll.getCollationKey(firstPrimIgn))<=0) { CollationKey key = coll.getCollationKey(firstPrimIgn); logln("Collation key for 0x0332: "+CollationTest.prettify(key)); errln("Error! String \"b\" must be greater than 0x0332 -"+ "[First Primary Ignorable]"); } if (keyB.compareTo(coll.getCollationKey(firstVariable))>=0) { CollationKey key = coll.getCollationKey(firstVariable); logln("Collation key for 0x0009: "+CollationTest.prettify(key)); errln("Error! String \"b\" must be less than 0x0009 - [First Variable]"); } } { i=1; RuleBasedCollator coll = null; try { coll = new RuleBasedCollator(rules[i]); } catch (Exception e) { warnln("Unable to open collator with rules " + rules[i]); } logln("Test rule["+i+"]"+rules[i]); CollationKey keyA = coll.getCollationKey("a"); logln("Key for \"a\":"+ CollationTest.prettify(keyA)); byte[] keyAInBytes = keyA.toByteArray(); for (int j=0; j=0) { errln("Error! String \"a\" must be less than 0x0009 - [First Variable]"); CollationKey key = coll.getCollationKey(firstVariable); logln("Collation key for 0x0009: "+CollationTest.prettify(key)); } CollationKey keyB = coll.getCollationKey("b"); logln("Key for \"b\":"+ CollationTest.prettify(keyB)); byte[] keyBInBytes = keyB.toByteArray(); for (int j=0; j=0) { CollationKey key = coll.getCollationKey(firstVariable); logln("Collation key for 0x0009: "+CollationTest.prettify(key)); errln("Error! String \"b\" must be less than 0x0009 - [First Variable]"); } } } public void TestUCAPrecontext() { String rules[] = { "& \u00B70) { CollationKey prevKey = en.getCollationKey(cases[j-1]); if (key.compareTo(prevKey)<0) { errln("Error! EN test["+j+"]:source:" + cases[j]+ " is not >= previous test string."); } } /* if ( key.compareTo(expectingKey)!=0) { errln("Error! Test case["+i+"]:"+"source:" + key.getSourceString()); errln("expecting:"+prettify(expectingKey)+ "got:"+ prettify(key)); } */ logln("String:"+cases[j]+" Key:"+ CollationTest.prettify(key)); } } catch (Exception e) { warnln("Error creating English collator"); return; } // Test ja sort RuleBasedCollator ja = null; logln("JA sort:"); try { ja = (RuleBasedCollator)Collator.getInstance( new Locale("ja", "")); for (int j=0; j0) { CollationKey prevKey = ja.getCollationKey(cases[j-1]); if (key.compareTo(prevKey)<0) { errln("Error! JA test["+j+"]:source:" + cases[j]+ " is not >= previous test string."); } } logln("String:"+cases[j]+" Key:"+ CollationTest.prettify(key)); } } catch (Exception e) { warnln("Error creating Japanese collator"); return; } for(int i = 0; i < rules.length; i++) { RuleBasedCollator coll = null; logln("Tailoring rule:"+rules[i]); try { coll = new RuleBasedCollator(rules[i]); } catch (Exception e) { warnln("Unable to open collator with rules " + rules[i]); continue; } for (int j=0; j0) { CollationKey prevKey = coll.getCollationKey(cases[j-1]); if (i==1 && j==3) { if (key.compareTo(prevKey)>0) { errln("Error! Rule:"+rules[i]+" test["+j+"]:source:"+ cases[j]+" is not <= previous test string."); } } else { if (key.compareTo(prevKey)<0) { errln("Error! Rule:"+rules[i]+" test["+j+"]:source:"+ cases[j]+" is not >= previous test string."); } } } logln("String:"+cases[j]+" Key:"+ CollationTest.prettify(key)); } } } /** * Stores a test case for collation testing. */ private class OneTestCase { /** The first value to compare. **/ public String m_source_; /** The second value to compare. **/ public String m_target_; /** * 0 if the two values sort equal, * -1 if the first value sorts before the second * 1 if the first value sorts after the first */ public int m_result_; public OneTestCase(String source, String target, int result) { m_source_ = source; m_target_ = target; m_result_ = result; } } /** * Convenient function to test collation rules. * @param testCases * @param rules Collation rules in ICU format. All the strings in this * array represent the same rule, expressed in different forms. */ private void doTestCollation( OneTestCase[] testCases, String[] rules) { Collator myCollation; for (String rule : rules) { try { myCollation = new RuleBasedCollator(rule); } catch (Exception e) { warnln("ERROR: in creation of rule based collator: " + e); return; } myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION); myCollation.setStrength(Collator.TERTIARY); for (OneTestCase testCase : testCases) { CollationTest.doTest(this, (RuleBasedCollator)myCollation, testCase.m_source_, testCase.m_target_, testCase.m_result_); } } } // Test cases to check whether the rules equivalent to // "&a }; doTestCollation(testCases, rules); } /* * Tests the method public boolean equals(Object target) in CollationKey */ public void TestCollationKeyEquals() { CollationKey ck = new CollationKey("", (byte[]) null); // Tests when "if (!(target instanceof CollationKey))" is true if (ck.equals(new Object())) { errln("CollationKey.equals() was not suppose to return false " + "since it is comparing to a non Collation Key object."); } if (ck.equals("")) { errln("CollationKey.equals() was not suppose to return false " + "since it is comparing to a non Collation Key object."); } if (ck.equals(0)) { errln("CollationKey.equals() was not suppose to return false " + "since it is comparing to a non Collation Key object."); } if (ck.equals(0.0)) { errln("CollationKey.equals() was not suppose to return false " + "since it is comparing to a non Collation Key object."); } // Tests when "if (target == null)" is true if (ck.equals((CollationKey) null)) { errln("CollationKey.equals() was not suppose to return false " + "since it is comparing to a null Collation Key object."); } } /* * Tests the method public int hashCode() in CollationKey */ public void TestCollationKeyHashCode() { CollationKey ck = new CollationKey("", (byte[]) null); // Tests when "if (m_key_ == null)" is true if (ck.hashCode() != 1) { errln("CollationKey.hashCode() was suppose to return 1 " + "when m_key is null due a null parameter in the " + "constructor."); } } /* * Tests the method public CollationKey getBound(int boundType, int noOfLevels) */ public void TestGetBound() { CollationKey ck = new CollationKey("", (byte[]) null); // Tests when "if (noOfLevels > Collator.PRIMARY)" is false // Tests when "default: " is true for "switch (boundType)" try { ck.getBound(BoundMode.COUNT, -1); errln("CollationKey.getBound(int,int) was suppose to return an " + "exception for an invalid boundType value."); } catch (Exception e) { } // Tests when "if (noOfLevels > 0)" byte b[] = {}; CollationKey ck1 = new CollationKey("", b); try { ck1.getBound(0, 1); errln("CollationKey.getBound(int,int) was suppose to return an " + "exception a value of noOfLevels that exceeds expected."); } catch (Exception e) { } } /* * Tests the method public CollationKey merge(CollationKey source) */ public void TestMerge() { byte b[] = {}; CollationKey ck = new CollationKey("", b); // Tests when "if (source == null || source.getLength() == 0)" is true try { ck.merge(null); errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter."); } catch (Exception e) { } try { ck.merge(ck); errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter."); } catch (Exception e) { } } /* Test the method public int compareTo(RawCollationKey rhs) */ 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."); } } /* Track7223: CollationElementIterator does not return correct order for Hungarian */ public void TestHungarianTailoring(){ String rules = new String("&DZ= 0) { errln("TestHungarianTailoring.compare(" + str1 + ","+ str2 + ") was suppose to return -1 "); } CollationKey sortKey1 = coll.getCollationKey(str1); CollationKey sortKey2 = coll.getCollationKey(str2); if (sortKey1.compareTo(sortKey2) >= 0) { errln("TestHungarianTailoring getCollationKey(\"" + str1 +"\") was suppose "+ "less than getCollationKey(\""+ str2 + "\")."); errln(" getCollationKey(\"ggy\"):" + CollationTest.prettify(sortKey1) + " getCollationKey(\"GGY\"):" + CollationTest.prettify(sortKey2)); } CollationElementIterator iter1 = coll.getCollationElementIterator(str1); CollationElementIterator iter2 = coll.getCollationElementIterator(str2); int ce1, ce2; while((ce1 = iter1.next()) != CollationElementIterator.NULLORDER && (ce2 = iter2.next()) != CollationElementIterator.NULLORDER) { if (ce1 > ce2) { errln("TestHungarianTailoring.CollationElementIterator(" + str1 + ","+ str2 + ") was suppose to return -1 "); } } } catch (Exception e) { e.printStackTrace(); } } public void TestImport(){ try{ RuleBasedCollator vicoll = (RuleBasedCollator)Collator.getInstance(new ULocale("vi")); RuleBasedCollator escoll = (RuleBasedCollator)Collator.getInstance(new ULocale("es")); RuleBasedCollator viescoll = new RuleBasedCollator(vicoll.getRules() + escoll.getRules()); RuleBasedCollator importviescoll = new RuleBasedCollator("[import vi][import es]"); UnicodeSet tailoredSet = viescoll.getTailoredSet(); UnicodeSet importTailoredSet = importviescoll.getTailoredSet(); if(!tailoredSet.equals(importTailoredSet)){ warnln("Tailored set not equal"); } for (UnicodeSetIterator it = new UnicodeSetIterator(tailoredSet); it.next();) { String t = it.getString(); CollationKey sk1 = viescoll.getCollationKey(t); CollationKey sk2 = importviescoll.getCollationKey(t); if(!sk1.equals(sk2)){ warnln("Collation key's not equal for " + t); } } }catch(Exception e){ warnln("ERROR: in creation of rule based collator"); } } public void TestImportWithType(){ try{ RuleBasedCollator vicoll = (RuleBasedCollator)Collator.getInstance(new ULocale("vi")); RuleBasedCollator decoll = (RuleBasedCollator)Collator.getInstance(ULocale.forLanguageTag("de-u-co-phonebk")); RuleBasedCollator videcoll = new RuleBasedCollator(vicoll.getRules() + decoll.getRules()); RuleBasedCollator importvidecoll = new RuleBasedCollator("[import vi][import de-u-co-phonebk]"); UnicodeSet tailoredSet = videcoll.getTailoredSet(); UnicodeSet importTailoredSet = importvidecoll.getTailoredSet(); if(!tailoredSet.equals(importTailoredSet)){ warnln("Tailored set not equal"); } for (UnicodeSetIterator it = new UnicodeSetIterator(tailoredSet); it.next();) { String t = it.getString(); CollationKey sk1 = videcoll.getCollationKey(t); CollationKey sk2 = importvidecoll.getCollationKey(t); if(!sk1.equals(sk2)){ warnln("Collation key's not equal for " + t); } } }catch(Exception e){ warnln("ERROR: in creation of rule based collator"); } } /* * This test ensures that characters placed before a character in a different script have the same lead byte * in their collation key before and after script reordering. */ public void TestBeforeRuleWithScriptReordering() throws Exception { /* build collator */ String rules = "&[before 1]\u03b1 < \u0e01"; int[] reorderCodes = {UScript.GREEK}; int result; Collator myCollation = new RuleBasedCollator(rules); myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION); myCollation.setStrength(Collator.TERTIARY); String base = "\u03b1"; /* base */ String before = "\u0e01"; /* ko kai */ /* check collation results - before rule applied but not script reordering */ result = myCollation.compare(base, before); if (!(result > 0)) { errln("Collation result not correct before script reordering."); } /* check the lead byte of the collation keys before script reordering */ CollationKey baseKey = myCollation.getCollationKey(base); CollationKey beforeKey = myCollation.getCollationKey(before); byte[] baseKeyBytes = baseKey.toByteArray(); byte[] beforeKeyBytes = beforeKey.toByteArray(); if (baseKeyBytes[0] != beforeKeyBytes[0]) { errln("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = " + baseKeyBytes[0] + ", before character lead byte = " + beforeKeyBytes[0]); } /* reorder the scripts */ myCollation.setReorderCodes(reorderCodes); /* check collation results - before rule applied and after script reordering */ result = myCollation.compare(base, before); if (!(result > 0)) { errln("Collation result not correct after script reordering."); } /* check the lead byte of the collation keys after script reordering */ baseKey = myCollation.getCollationKey(base); beforeKey = myCollation.getCollationKey(before); baseKeyBytes = baseKey.toByteArray(); beforeKeyBytes = beforeKey.toByteArray(); if (baseKeyBytes[0] != beforeKeyBytes[0]) { errln("Different lead byte for sort keys using before rule and before script reordering. base character lead byte = " + baseKeyBytes[0] + ", before character lead byte = " + beforeKeyBytes[0]); } } /* * Test that in a primary-compressed sort key all bytes except the first one are unchanged under script reordering. */ public void TestNonLeadBytesDuringCollationReordering() throws Exception { Collator myCollation; byte[] baseKey; byte[] reorderKey; int[] reorderCodes = {UScript.GREEK}; String testString = "\u03b1\u03b2\u03b3"; /* build collator tertiary */ myCollation = new RuleBasedCollator(""); myCollation.setStrength(Collator.TERTIARY); baseKey = myCollation.getCollationKey(testString).toByteArray(); myCollation.setReorderCodes(reorderCodes); reorderKey = myCollation.getCollationKey(testString).toByteArray(); if (baseKey.length != reorderKey.length) { errln("Key lengths not the same during reordering.\n"); } for (int i = 1; i < baseKey.length; i++) { if (baseKey[i] != reorderKey[i]) { errln("Collation key bytes not the same at position " + i); } } /* build collator tertiary */ myCollation = new RuleBasedCollator(""); myCollation.setStrength(Collator.QUATERNARY); baseKey = myCollation.getCollationKey(testString).toByteArray(); myCollation.setReorderCodes(reorderCodes); reorderKey = myCollation.getCollationKey(testString).toByteArray(); if (baseKey.length != reorderKey.length) { errln("Key lengths not the same during reordering.\n"); } for (int i = 1; i < baseKey.length; i++) { if (baseKey[i] != reorderKey[i]) { errln("Collation key bytes not the same at position " + i); } } } /* * Test reordering API. */ public void TestReorderingAPI() throws Exception { Collator myCollation; int[] reorderCodes = {UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION}; int[] duplicateReorderCodes = {UScript.HIRAGANA, UScript.GREEK, ReorderCodes.CURRENCY, UScript.KATAKANA}; int[] reorderCodesStartingWithDefault = {ReorderCodes.DEFAULT, UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION}; int[] retrievedReorderCodes; String greekString = "\u03b1"; String punctuationString = "\u203e"; /* build collator tertiary */ myCollation = new RuleBasedCollator(""); myCollation.setStrength(Collator.TERTIARY); /* set the reorderding */ myCollation.setReorderCodes(reorderCodes); retrievedReorderCodes = myCollation.getReorderCodes(); if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) { errln("ERROR: retrieved reorder codes do not match set reorder codes."); } if (!(myCollation.compare(greekString, punctuationString) < 0)) { errln("ERROR: collation result should have been less."); } /* clear the reordering */ myCollation.setReorderCodes(null); retrievedReorderCodes = myCollation.getReorderCodes(); if (retrievedReorderCodes.length != 0) { errln("ERROR: retrieved reorder codes was not null."); } if (!(myCollation.compare(greekString, punctuationString) > 0)) { errln("ERROR: collation result should have been greater."); } // do it again with an empty but non-null array /* set the reorderding */ myCollation.setReorderCodes(reorderCodes); retrievedReorderCodes = myCollation.getReorderCodes(); if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) { errln("ERROR: retrieved reorder codes do not match set reorder codes."); } if (!(myCollation.compare(greekString, punctuationString) < 0)) { errln("ERROR: collation result should have been less."); } /* clear the reordering */ myCollation.setReorderCodes(new int[]{}); retrievedReorderCodes = myCollation.getReorderCodes(); if (retrievedReorderCodes.length != 0) { errln("ERROR: retrieved reorder codes was not null."); } if (!(myCollation.compare(greekString, punctuationString) > 0)) { errln("ERROR: collation result should have been greater."); } /* clear the reordering using [NONE] */ myCollation.setReorderCodes(new int[]{ ReorderCodes.NONE }); retrievedReorderCodes = myCollation.getReorderCodes(); if (retrievedReorderCodes.length != 0) { errln("ERROR: [NONE] retrieved reorder codes was not null."); } boolean gotException = false; /* set duplicates in the reorder codes */ try { myCollation.setReorderCodes(duplicateReorderCodes); } catch (IllegalArgumentException e) { // expect exception on illegal arguments gotException = true; } if (!gotException) { errln("ERROR: exception was not thrown for illegal reorder codes argument."); } /* set duplicate reorder codes */ gotException = false; try { myCollation.setReorderCodes(reorderCodesStartingWithDefault); } catch (IllegalArgumentException e) { gotException = true; } if (!gotException) { errln("ERROR: reorder codes following a 'default' code should have thrown an exception but did not."); } } /* * Test reordering API. */ public void TestReorderingAPIWithRuleCreatedCollator() throws Exception { Collator myCollation; String rules = "[reorder Hani Grek]"; int[] rulesReorderCodes = {UScript.HAN, UScript.GREEK}; int[] reorderCodes = {UScript.GREEK, UScript.HAN, ReorderCodes.PUNCTUATION}; int[] retrievedReorderCodes; /* build collator tertiary */ myCollation = new RuleBasedCollator(rules); myCollation.setStrength(Collator.TERTIARY); retrievedReorderCodes = myCollation.getReorderCodes(); if (!Arrays.equals(rulesReorderCodes, retrievedReorderCodes)) { errln("ERROR: retrieved reorder codes do not match set reorder codes."); } /* clear the reordering */ myCollation.setReorderCodes(null); retrievedReorderCodes = myCollation.getReorderCodes(); if (retrievedReorderCodes.length != 0) { errln("ERROR: retrieved reorder codes was not null."); } /* set the reorderding */ myCollation.setReorderCodes(reorderCodes); retrievedReorderCodes = myCollation.getReorderCodes(); if (!Arrays.equals(reorderCodes, retrievedReorderCodes)) { errln("ERROR: retrieved reorder codes do not match set reorder codes."); } /* reset the reordering */ myCollation.setReorderCodes(ReorderCodes.DEFAULT); retrievedReorderCodes = myCollation.getReorderCodes(); if (!Arrays.equals(rulesReorderCodes, retrievedReorderCodes)) { errln("ERROR: retrieved reorder codes do not match set reorder codes."); } } static boolean containsExpectedScript(int[] scripts, int expectedScript) { for (int i = 0; i < scripts.length; ++i) { if (expectedScript == scripts[i]) { return true; } } return false; } public void TestEquivalentReorderingScripts() { // Beginning with ICU 55, collation reordering moves single scripts // rather than groups of scripts, // except where scripts share a range and sort primary-equal. final int[] expectedScripts = { UScript.HIRAGANA, UScript.KATAKANA, UScript.KATAKANA_OR_HIRAGANA }; int[] equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.GOTHIC); if (equivalentScripts.length != 1 || equivalentScripts[0] != UScript.GOTHIC) { errln(String.format("ERROR/Gothic: retrieved equivalent scripts wrong: " + "length expected 1, was = %d; expected [%d] was [%d]", equivalentScripts.length, UScript.GOTHIC, equivalentScripts[0])); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.HIRAGANA); if (equivalentScripts.length != expectedScripts.length) { errln(String.format("ERROR/Hiragana: retrieved equivalent script length wrong: " + "expected %d, was = %d", expectedScripts.length, equivalentScripts.length)); } int prevScript = -1; for (int i = 0; i < equivalentScripts.length; ++i) { int script = equivalentScripts[i]; if (script <= prevScript) { errln("ERROR/Hiragana: equivalent scripts out of order at index " + i); } prevScript = script; } for (int code : expectedScripts) { if (!containsExpectedScript(equivalentScripts, code)) { errln("ERROR/Hiragana: equivalent scripts do not contain " + code); } } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.KATAKANA); if (equivalentScripts.length != expectedScripts.length) { errln(String.format("ERROR/Katakana: retrieved equivalent script length wrong: " + "expected %d, was = %d", expectedScripts.length, equivalentScripts.length)); } for (int code : expectedScripts) { if (!containsExpectedScript(equivalentScripts, code)) { errln("ERROR/Katakana: equivalent scripts do not contain " + code); } } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.KATAKANA_OR_HIRAGANA); if (equivalentScripts.length != expectedScripts.length) { errln(String.format("ERROR/Hrkt: retrieved equivalent script length wrong: " + "expected %d, was = %d", expectedScripts.length, equivalentScripts.length)); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.HAN); if (equivalentScripts.length != 3) { errln("ERROR/Hani: retrieved equivalent script length wrong: " + "expected 3, was = " + equivalentScripts.length); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.SIMPLIFIED_HAN); if (equivalentScripts.length != 3) { errln("ERROR/Hans: retrieved equivalent script length wrong: " + "expected 3, was = " + equivalentScripts.length); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.TRADITIONAL_HAN); if (equivalentScripts.length != 3) { errln("ERROR/Hant: retrieved equivalent script length wrong: " + "expected 3, was = " + equivalentScripts.length); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.MEROITIC_CURSIVE); if (equivalentScripts.length != 2) { errln("ERROR/Merc: retrieved equivalent script length wrong: " + "expected 2, was = " + equivalentScripts.length); } equivalentScripts = RuleBasedCollator.getEquivalentReorderCodes(UScript.MEROITIC_HIEROGLYPHS); if (equivalentScripts.length != 2) { errln("ERROR/Mero: retrieved equivalent script length wrong: " + "expected 2, was = " + equivalentScripts.length); } } public void TestGreekFirstReorderCloning() { String[] testSourceCases = { "\u0041", "\u03b1\u0041", "\u0061", "\u0041\u0061", "\u0391", }; String[] testTargetCases = { "\u03b1", "\u0041\u03b1", "\u0391", "\u0391\u03b1", "\u0391", }; int[] results = { 1, -1, 1, 1, 0 }; Collator originalCollation; Collator myCollation; String rules = "[reorder Grek]"; try { originalCollation = new RuleBasedCollator(rules); } catch (Exception e) { warnln("ERROR: in creation of rule based collator"); return; } try { myCollation = (Collator) originalCollation.clone(); } catch (Exception e) { warnln("ERROR: in creation of rule based collator"); return; } myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION); myCollation.setStrength(Collator.TERTIARY); for (int i = 0; i < testSourceCases.length ; i++) { CollationTest.doTest(this, (RuleBasedCollator)myCollation, testSourceCases[i], testTargetCases[i], results[i]); } } /* * Utility function to test one collation reordering test case. * @param testcases Array of test cases. * @param n_testcases Size of the array testcases. * @param str_rules Array of rules. These rules should be specifying the same rule in different formats. * @param n_rules Size of the array str_rules. */ private void doTestOneReorderingAPITestCase(OneTestCase testCases[], int reorderTokens[]) { Collator myCollation = Collator.getInstance(ULocale.ENGLISH); myCollation.setReorderCodes(reorderTokens); for (OneTestCase testCase : testCases) { CollationTest.doTest(this, (RuleBasedCollator)myCollation, testCase.m_source_, testCase.m_target_, testCase.m_result_); } } public void TestGreekFirstReorder() { String[] strRules = { "[reorder Grek]" }; int[] apiRules = { UScript.GREEK }; OneTestCase[] privateUseCharacterStrings = { new OneTestCase("\u0391", "\u0391", 0), new OneTestCase("\u0041", "\u0391", 1), new OneTestCase("\u03B1\u0041", "\u03B1\u0391", 1), new OneTestCase("\u0060", "\u0391", -1), new OneTestCase("\u0391", "\ue2dc", -1), new OneTestCase("\u0391", "\u0060", 1), }; /* Test rules creation */ doTestCollation(privateUseCharacterStrings, strRules); /* Test collation reordering API */ doTestOneReorderingAPITestCase(privateUseCharacterStrings, apiRules); } public void TestGreekLastReorder() { String[] strRules = { "[reorder Zzzz Grek]" }; int[] apiRules = { UScript.UNKNOWN, UScript.GREEK }; OneTestCase[] privateUseCharacterStrings = { new OneTestCase("\u0391", "\u0391", 0), new OneTestCase("\u0041", "\u0391", -1), new OneTestCase("\u03B1\u0041", "\u03B1\u0391", -1), new OneTestCase("\u0060", "\u0391", -1), new OneTestCase("\u0391", "\ue2dc", 1), }; /* Test rules creation */ doTestCollation(privateUseCharacterStrings, strRules); /* Test collation reordering API */ doTestOneReorderingAPITestCase(privateUseCharacterStrings, apiRules); } public void TestNonScriptReorder() { String[] strRules = { "[reorder Grek Symbol DIGIT Latn Punct space Zzzz cURRENCy]" }; int[] apiRules = { UScript.GREEK, ReorderCodes.SYMBOL, ReorderCodes.DIGIT, UScript.LATIN, ReorderCodes.PUNCTUATION, ReorderCodes.SPACE, UScript.UNKNOWN, ReorderCodes.CURRENCY }; OneTestCase[] privateUseCharacterStrings = { new OneTestCase("\u0391", "\u0041", -1), new OneTestCase("\u0041", "\u0391", 1), new OneTestCase("\u0060", "\u0041", -1), new OneTestCase("\u0060", "\u0391", 1), new OneTestCase("\u0024", "\u0041", 1), }; /* Test rules creation */ doTestCollation(privateUseCharacterStrings, strRules); /* Test collation reordering API */ doTestOneReorderingAPITestCase(privateUseCharacterStrings, apiRules); } public void TestHaniReorder() { String[] strRules = { "[reorder Hani]" }; int[] apiRules = { UScript.HAN }; OneTestCase[] privateUseCharacterStrings = { new OneTestCase("\u4e00", "\u0041", -1), new OneTestCase("\u4e00", "\u0060", 1), new OneTestCase("\uD86D\uDF40", "\u0041", -1), new OneTestCase("\uD86D\uDF40", "\u0060", 1), new OneTestCase("\u4e00", "\uD86D\uDF40", -1), new OneTestCase("\ufa27", "\u0041", -1), new OneTestCase("\uD869\uDF00", "\u0041", -1), }; /* Test rules creation */ doTestCollation(privateUseCharacterStrings, strRules); /* Test collation reordering API */ doTestOneReorderingAPITestCase(privateUseCharacterStrings, apiRules); } public void TestHaniReorderWithOtherRules() { String[] strRules = { "[reorder Hani] &b