use of android.icu.text.CollationKey in project j2objc by google.
the class CollationFrozenMonkeyTest method TestCollationKey.
@Test
public void TestCollationKey() {
if (source.length() == 0) {
errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
return;
}
Collator myPrimaryCollator;
Collator mySecondaryCollator;
Collator myTertiaryCollator;
try {
Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
myCollator.freeze();
myPrimaryCollator = myCollator.cloneAsThawed();
myPrimaryCollator.setStrength(Collator.PRIMARY);
myPrimaryCollator.freeze();
mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
mySecondaryCollator.setStrength(Collator.SECONDARY);
mySecondaryCollator.freeze();
myTertiaryCollator = mySecondaryCollator.cloneAsThawed();
myTertiaryCollator.setStrength(Collator.TERTIARY);
myTertiaryCollator.freeze();
} catch (Exception e) {
warnln("ERROR: in creation of collator of ENGLISH locale");
return;
}
// use test framework's random seed
Random rand = createRandom();
int s = rand.nextInt(0x7fff) % source.length();
int t = rand.nextInt(0x7fff) % source.length();
int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
CollationKey collationKey1, collationKey2;
collationKey1 = myTertiaryCollator.getCollationKey(subs);
collationKey2 = myTertiaryCollator.getCollationKey(subt);
// Tertiary
int result = collationKey1.compareTo(collationKey2);
// Tertiary
int revResult = collationKey2.compareTo(collationKey1);
report(subs, subt, result, revResult);
collationKey1 = mySecondaryCollator.getCollationKey(subs);
collationKey2 = mySecondaryCollator.getCollationKey(subt);
// Secondary
result = collationKey1.compareTo(collationKey2);
// Secondary
revResult = collationKey2.compareTo(collationKey1);
report(subs, subt, result, revResult);
collationKey1 = myPrimaryCollator.getCollationKey(subs);
collationKey2 = myPrimaryCollator.getCollationKey(subt);
// Primary
result = collationKey1.compareTo(collationKey2);
// Primary
revResult = collationKey2.compareTo(collationKey1);
report(subs, subt, result, revResult);
String msg = "";
String addOne = subs + String.valueOf(0xE000);
collationKey1 = myPrimaryCollator.getCollationKey(subs);
collationKey2 = myPrimaryCollator.getCollationKey(addOne);
result = collationKey1.compareTo(collationKey2);
if (result != -1) {
msg += "CollationKey(";
msg += subs;
msg += ") .LT. CollationKey(";
msg += addOne;
msg += ") Failed.";
errln(msg);
}
msg = "";
result = collationKey2.compareTo(collationKey1);
if (result != 1) {
msg += "CollationKey(";
msg += addOne;
msg += ") .GT. CollationKey(";
msg += subs;
msg += ") Failed.";
errln(msg);
}
}
use of android.icu.text.CollationKey in project j2objc by google.
the class CollationFrozenMonkeyTest method doTest.
void doTest(RuleBasedCollator myCollation, String mysource, String target, int result) {
int compareResult = myCollation.compare(source, target);
CollationKey sortKey1, sortKey2;
try {
sortKey1 = myCollation.getCollationKey(source);
sortKey2 = myCollation.getCollationKey(target);
} catch (Exception e) {
errln("SortKey generation Failed.\n");
return;
}
int keyResult = sortKey1.compareTo(sortKey2);
reportCResult(mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
}
use of android.icu.text.CollationKey in project j2objc by google.
the class CollationMiscTest method TestImport.
@Test
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) {
// Android patch: Add --omitCollationRules to genrb.
logln("ERROR: in creation of rule based collator");
// Android patch end.
}
}
use of android.icu.text.CollationKey in project j2objc by google.
the class CollationMiscTest method TestHungarianTailoring.
/* Track7223: CollationElementIterator does not return correct order for Hungarian */
@Test
public void TestHungarianTailoring() {
String rules = new String("&DZ<dzs<<<Dzs<<<DZS" + "&G<gy<<<Gy<<<GY" + "&L<ly<<<Ly<<<LY" + "&N<ny<<<Ny<<<NY" + "&S<sz<<<Sz<<<SZ" + "&T<ty<<<Ty<<<TY" + "&Z<zs<<<Zs<<<ZS" + "&O<\u00f6<<<\u00d6<<\u0151<<<\u0150" + "&U<\u00fc<<<\u00dc<<\u0171<<<\u0171" + "&cs<<<ccs/cs" + "&Cs<<<Ccs/cs" + "&CS<<<CCS/CS" + "&dz<<<ddz/dz" + "&Dz<<<Ddz/dz" + "&DZ<<<DDZ/DZ" + "&dzs<<<ddzs/dzs" + "&Dzs<<<Ddzs/dzs" + "&DZS<<<DDZS/DZS" + "&gy<<<ggy/gy" + "&Gy<<<Ggy/gy" + "&GY<<<GGY/GY");
RuleBasedCollator coll;
try {
String str1 = "ggy";
String str2 = "GGY";
coll = new RuleBasedCollator(rules);
if (coll.compare("ggy", "GGY") >= 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();
}
}
use of android.icu.text.CollationKey in project j2objc by google.
the class CollationMiscTest method TestBeforeRuleWithScriptReordering.
/*
* 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.
*/
@Test
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]);
}
}
Aggregations