use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class SearchTest method assertEqual.
boolean assertEqual(SearchData search) {
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.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;
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class AlphabeticIndexTest method TestHaniFirst.
/**
* Test AlphabeticIndex vs. root with script reordering.
*/
@Test
public void TestHaniFirst() {
RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
coll.setReorderCodes(UScript.HAN);
AlphabeticIndex index = new AlphabeticIndex(coll);
// ... (underflow only)
assertEquals("getBucketCount()", 1, index.getBucketCount());
index.addLabels(Locale.ENGLISH);
// ... A-Z ...
assertEquals("getBucketCount()", 28, index.getBucketCount());
int bucketIndex = index.getBucketIndex("\u897f");
// underflow bucket
assertEquals("getBucketIndex(U+897F)", 0, bucketIndex);
bucketIndex = index.getBucketIndex("i");
assertEquals("getBucketIndex(i)", 9, bucketIndex);
bucketIndex = index.getBucketIndex("\u03B1");
assertEquals("getBucketIndex(Greek alpha)", 27, bucketIndex);
// U+50005 is an unassigned code point which sorts at the end, independent of the Hani group.
bucketIndex = index.getBucketIndex(UTF16.valueOf(0x50005));
assertEquals("getBucketIndex(U+50005)", 27, bucketIndex);
bucketIndex = index.getBucketIndex("\uFFFF");
assertEquals("getBucketIndex(U+FFFF)", 27, bucketIndex);
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class AlphabeticIndexTest method TestNoLabels.
/**
* With no real labels, there should be only the underflow label.
*/
@Test
public void TestNoLabels() {
RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
AlphabeticIndex<Integer> index = new AlphabeticIndex<Integer>(coll);
index.addRecord("\u897f", 0);
index.addRecord("i", 0);
index.addRecord("\u03B1", 0);
// code coverage
assertEquals("getRecordCount()", 3, index.getRecordCount());
// ...
assertEquals("getBucketCount()", 1, index.getBucketCount());
Bucket<Integer> bucket = index.iterator().next();
assertEquals("underflow label type", LabelType.UNDERFLOW, bucket.getLabelType());
assertEquals("all records in the underflow bucket", 3, bucket.size());
}
use of android.icu.text.RuleBasedCollator in project j2objc by google.
the class AlphabeticIndexTest method TestFrozenCollator.
@Test
public void TestFrozenCollator() {
// Ticket #9472
RuleBasedCollator coll = (RuleBasedCollator) Collator.getInstance(new ULocale("da"));
coll.setStrength(Collator.IDENTICAL);
coll.freeze();
// The AlphabeticIndex constructor used to throw an exception
// because it cloned the collator (which preserves frozenness)
// and set the clone's strength to PRIMARY.
AlphabeticIndex index = new AlphabeticIndex(coll);
assertEquals("same strength as input Collator", Collator.IDENTICAL, index.getCollator().getStrength());
}
use of android.icu.text.RuleBasedCollator 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);
}
}
}
}
Aggregations