use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class CollationMonkeyTest method TestRules.
@Test
public void TestRules() {
String[] testSourceCases = { "\u0061\u0062\u007a", "\u0061\u0062\u007a" };
String[] testTargetCases = { "\u0061\u0062\u00e4", "\u0061\u0062\u0061\u0308" };
int i = 0;
logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
Collator col = Collator.getInstance(new Locale("en", "US"));
String baseRules = ((RuleBasedCollator) col).getRules();
String newRules = " & z < ";
newRules = baseRules + newRules + String.valueOf(0x00e4);
RuleBasedCollator myCollation = null;
try {
myCollation = new RuleBasedCollator(newRules);
} catch (Exception e) {
warnln("Demo Test 1 Table Collation object creation failed.");
return;
}
for (i = 0; i < 2; i++) {
doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
}
logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
newRules = "";
newRules = baseRules + " & z < a" + String.valueOf(0x0308);
try {
myCollation = new RuleBasedCollator(newRules);
} catch (Exception e) {
errln("Demo Test 1 Table Collation object creation failed.");
return;
}
for (i = 0; i < 2; i++) {
doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
}
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class CollationTest method TestTailoredElements.
@Test
public void TestTailoredElements() {
CollationData root = CollationRoot.getData();
CollationRootElements rootElements = new CollationRootElements(root.rootElements);
Set<String> prevLocales = new HashSet<String>();
prevLocales.add("");
prevLocales.add("root");
prevLocales.add("root@collation=standard");
long[] ces;
ULocale[] locales = Collator.getAvailableULocales();
String localeID = "root";
int locIdx = 0;
for (; locIdx < locales.length; localeID = locales[locIdx++].getName()) {
ULocale locale = new ULocale(localeID);
String[] types = Collator.getKeywordValuesForLocale("collation", locale, false);
for (int typeIdx = 0; typeIdx < types.length; ++typeIdx) {
// first: default type
String type = types[typeIdx];
if (type.startsWith("private-")) {
errln("Collator.getKeywordValuesForLocale(" + localeID + ") returns private collation keyword: " + type);
}
ULocale localeWithType = locale.setKeywordValue("collation", type);
Collator coll = Collator.getInstance(localeWithType);
ULocale actual = coll.getLocale(ULocale.ACTUAL_LOCALE);
if (prevLocales.contains(actual.getName())) {
continue;
}
prevLocales.add(actual.getName());
logln("TestTailoredElements(): requested " + localeWithType.getName() + " -> actual " + actual.getName());
if (!(coll instanceof RuleBasedCollator)) {
continue;
}
RuleBasedCollator rbc = (RuleBasedCollator) coll;
// Note: It would be better to get tailored strings such that we can
// identify the prefix, and only get the CEs for the prefix+string,
// not also for the prefix.
// There is currently no API for that.
// It would help in an unusual case where a contraction starting in the prefix
// extends past its end, and we do not see the intended mapping.
// For example, for a mapping p|st, if there is also a contraction ps,
// then we get CEs(ps)+CEs(t), rather than CEs(p|st).
UnicodeSet tailored = coll.getTailoredSet();
UnicodeSetIterator iter = new UnicodeSetIterator(tailored);
while (iter.next()) {
String s = iter.getString();
ces = rbc.internalGetCEs(s);
for (int i = 0; i < ces.length; ++i) {
long ce = ces[i];
if (!isValidCE(rootElements, root, ce)) {
logln(prettify(s));
errln("invalid tailored CE 0x" + Utility.hex(ce, 16) + " at CE index " + i + " from string:");
}
}
}
}
}
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class SearchTest method TestOverlapCanonical.
@Test
public void TestOverlapCanonical() {
int count;
for (count = 0; count < OVERLAPCANONICAL.length; count++) {
if (!assertEqualWithAttribute(OVERLAPCANONICAL[count], true, true)) {
errln("Error at overlap test number %d" + count);
}
}
for (count = 0; count < NONOVERLAP.length; count++) {
if (!assertCanonicalEqual(NONOVERLAPCANONICAL[count])) {
errln("Error at non overlap test number %d" + count);
}
}
for (count = 0; count < OVERLAPCANONICAL.length && count < NONOVERLAPCANONICAL.length; count++) {
SearchData search = OVERLAPCANONICAL[count];
RuleBasedCollator collator = getCollator(search.collator);
StringSearch strsrch = new StringSearch(search.pattern, new StringCharacterIterator(search.text), collator, null);
strsrch.setCanonical(true);
strsrch.setOverlapping(true);
if (strsrch.isOverlapping() != true) {
errln("Error setting overlap option");
}
if (!assertEqualWithStringSearch(strsrch, search)) {
strsrch = null;
return;
}
search = NONOVERLAPCANONICAL[count];
strsrch.setOverlapping(false);
if (strsrch.isOverlapping() != false) {
errln("Error setting overlap option");
}
strsrch.reset();
if (!assertEqualWithStringSearch(strsrch, search)) {
strsrch = null;
errln("Error at test number %d" + count);
}
}
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class SearchTest method init.
@Before
public void init() throws Exception {
m_en_us_ = (RuleBasedCollator) Collator.getInstance(Locale.US);
m_fr_fr_ = (RuleBasedCollator) Collator.getInstance(Locale.FRANCE);
m_de_ = (RuleBasedCollator) Collator.getInstance(new Locale("de", "DE"));
m_es_ = (RuleBasedCollator) Collator.getInstance(new Locale("es", "ES"));
m_en_wordbreaker_ = BreakIterator.getWordInstance();
m_en_characterbreaker_ = BreakIterator.getCharacterInstance();
String rules = m_de_.getRules() + EXTRACOLLATIONRULE;
m_de_ = new RuleBasedCollator(rules);
rules = m_es_.getRules() + EXTRACOLLATIONRULE;
m_es_ = new RuleBasedCollator(rules);
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class SearchTest method assertEqualWithAttribute.
boolean assertEqualWithAttribute(SearchData search, boolean canonical, boolean overlap) {
Collator collator = getCollator(search.collator);
BreakIterator breaker = getBreakIterator(search.breaker);
StringSearch strsrch;
String text = search.text;
String pattern = search.pattern;
if (breaker != null) {
breaker.setText(text);
}
collator.setStrength(search.strength);
try {
strsrch = new StringSearch(pattern, new StringCharacterIterator(text), (RuleBasedCollator) collator, breaker);
strsrch.setCanonical(canonical);
strsrch.setOverlapping(overlap);
strsrch.setElementComparisonType(search.cmpType);
} catch (Exception e) {
errln("Error opening string search " + e.getMessage());
return false;
}
if (!assertEqualWithStringSearch(strsrch, search)) {
collator.setStrength(TERTIARY);
return false;
}
collator.setStrength(TERTIARY);
return true;
}
Aggregations