2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : cintltest
\r
10 * Source File: $ICU4CRoot/source/test/cintltest/cmsccoll.c
\r
13 package com.ibm.icu.dev.test.collator;
\r
15 import java.util.Arrays;
\r
16 import java.util.Locale;
\r
17 import java.util.Set;
\r
18 import java.util.TreeSet;
\r
20 import com.ibm.icu.dev.test.TestFmwk;
\r
21 import com.ibm.icu.impl.ICUResourceBundle;
\r
22 import com.ibm.icu.impl.ImplicitCEGenerator;
\r
23 import com.ibm.icu.impl.Utility;
\r
24 import com.ibm.icu.text.CollationElementIterator;
\r
25 import com.ibm.icu.text.CollationKey;
\r
26 import com.ibm.icu.text.Collator;
\r
27 import com.ibm.icu.text.Normalizer;
\r
28 import com.ibm.icu.text.RawCollationKey;
\r
29 import com.ibm.icu.text.RuleBasedCollator;
\r
30 import com.ibm.icu.text.UTF16;
\r
31 import com.ibm.icu.text.CollationKey.BoundMode;
\r
32 import com.ibm.icu.util.ULocale;
\r
33 import com.ibm.icu.util.UResourceBundle;
\r
35 public class CollationMiscTest extends TestFmwk {
\r
37 public static void main(String[] args) throws Exception {
\r
38 new CollationMiscTest().run(args);
\r
39 // new CollationMiscTest().TestLocaleRuleBasedCollators();
\r
42 //private static final int NORM_BUFFER_TEST_LEN_ = 32;
\r
43 private static final class Tester
\r
50 private static final boolean hasCollationElements(Locale locale)
\r
52 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,locale);
\r
55 String collkey = rb.getStringWithFallback("collations/default");
\r
56 ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
\r
57 if (elements != null) {
\r
60 } catch (Exception e) {
\r
66 public void TestComposeDecompose()
\r
68 Tester t[] = new Tester[0x30000];
\r
69 t[0] = new Tester();
\r
70 logln("Testing UCA extensively\n");
\r
71 RuleBasedCollator coll;
\r
73 coll = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH);
\r
75 catch (Exception e) {
\r
76 warnln("Error opening collator\n");
\r
81 for (int u = 0; u < 0x30000; u ++) {
\r
82 String comp = UTF16.valueOf(u);
\r
83 int len = comp.length();
\r
84 t[noCases].NFC = Normalizer.normalize(u, Normalizer.NFC);
\r
85 t[noCases].NFD = Normalizer.normalize(u, Normalizer.NFD);
\r
87 if (t[noCases].NFC.length() != t[noCases].NFD.length()
\r
88 || (t[noCases].NFC.compareTo(t[noCases].NFD) != 0)
\r
89 || (len != t[noCases].NFD.length())
\r
90 || (comp.compareTo(t[noCases].NFD) != 0)) {
\r
92 if (len != t[noCases].NFD.length()
\r
93 || (comp.compareTo(t[noCases].NFD) != 0)) {
\r
94 t[noCases].NFC = comp;
\r
97 t[noCases] = new Tester();
\r
101 for (int u = 0; u < noCases; u ++) {
\r
102 if (!coll.equals(t[u].NFC, t[u].NFD)) {
\r
103 errln("Failure: codePoint \\u" + Integer.toHexString(t[u].u)
\r
104 + " fails TestComposeDecompose in the UCA");
\r
105 CollationTest.doTest(this, coll, t[u].NFC, t[u].NFD, 0);
\r
109 logln("Testing locales, number of cases = " + noCases);
\r
110 Locale loc[] = Collator.getAvailableLocales();
\r
111 for (int i = 0; i < loc.length; i ++) {
\r
112 if (hasCollationElements(loc[i])) {
\r
113 logln("Testing locale " + loc[i].getDisplayName());
\r
114 coll = (RuleBasedCollator)Collator.getInstance(loc[i]);
\r
115 coll.setStrength(Collator.IDENTICAL);
\r
117 for (int u = 0; u < noCases; u ++) {
\r
118 if (!coll.equals(t[u].NFC, t[u].NFD)) {
\r
119 errln("Failure: codePoint \\u"
\r
120 + Integer.toHexString(t[u].u)
\r
121 + " fails TestComposeDecompose for locale "
\r
122 + loc[i].getDisplayName());
\r
123 // this tests for the iterators too
\r
124 CollationTest.doTest(this, coll, t[u].NFC, t[u].NFD,
\r
132 public void TestRuleOptions() {
\r
133 // values here are hardcoded and are correct for the current UCA when
\r
134 // the UCA changes, one might be forced to change these values.
\r
135 // (\\u02d0, \\U00010FFFC etc...)
\r
137 // cannot test this anymore, as [last primary ignorable] doesn't
\r
138 // have a code point associated to it anymore
\r
139 // "&[before 3][last primary ignorable]<<<k",
\r
140 // - all befores here amount to zero
\r
141 "&[before 3][first tertiary ignorable]<<<a",
\r
142 "&[before 3][last tertiary ignorable]<<<a",
\r
143 "&[before 3][first secondary ignorable]<<<a",
\r
144 "&[before 3][last secondary ignorable]<<<a",
\r
145 // 'normal' befores
\r
146 "&[before 3][first primary ignorable]<<<c<<<b &[first primary ignorable]<a",
\r
147 // we don't have a code point that corresponds to the last primary
\r
149 "&[before 3][last primary ignorable]<<<c<<<b &[last primary ignorable]<a",
\r
150 "&[before 3][first variable]<<<c<<<b &[first variable]<a",
\r
151 "&[last variable]<a &[before 3][last variable]<<<c<<<b ",
\r
152 "&[first regular]<a &[before 1][first regular]<b",
\r
153 "&[before 1][last regular]<b &[last regular]<a",
\r
154 "&[before 1][first implicit]<b &[first implicit]<a",
\r
155 "&[before 1][last implicit]<b &[last implicit]<a",
\r
156 "&[last variable]<z&[last primary ignorable]<x&[last secondary ignorable]<<y&[last tertiary ignorable]<<<w&[top]<u",
\r
158 String[][] data = {
\r
159 // {"k", "\u20e3"},
\r
160 {"\\u0000", "a"}, // you cannot go before first tertiary ignorable
\r
161 {"\\u0000", "a"}, // you cannot go before last tertiary ignorable
\r
162 {"\\u0000", "a"}, // you cannot go before first secondary ignorable
\r
163 {"\\u0000", "a"}, // you cannot go before first secondary ignorable
\r
164 {"c", "b", "\\u0332", "a"},
\r
165 {"\\u0332", "\\u20e3", "c", "b", "a"},
\r
166 {"c", "b", "\\u0009", "a", "\\u000a"},
\r
167 {"c", "b", "\\uD834\\uDF71", "a", "\\u02d0"},
\r
168 {"b", "\\u02d0", "a", "\\u02d1"},
\r
169 // The character in the second ordering test string
\r
170 // has to match the character that has the [last regular] weight
\r
171 // which changes with each UCA version.
\r
172 // See the bottom of FractionalUCA.txt which says something like
\r
173 // [last regular [CE 27, 05, 05]] # U+1342E EGYPTIAN HIEROGLYPH AA032
\r
174 {"b", "\\U0001342E", "a", "\\u4e00"},
\r
175 {"b", "\\u4e00", "a", "\\u4e01"},
\r
176 {"b", "\\U0010FFFD", "a"},
\r
177 {"\ufffb", "w", "y", "\u20e3", "x", "\u137c", "z", "u"},
\r
180 for (int i = 0; i< rules.length; i++) {
\r
181 genericRulesStarter(rules[i], data[i]);
\r
185 void genericRulesStarter(String rules, String[] s) {
\r
186 genericRulesStarterWithResult(rules, s, -1);
\r
189 void genericRulesStarterWithResult(String rules, String[] s, int result) {
\r
191 RuleBasedCollator coll = null;
\r
193 coll = new RuleBasedCollator(rules);
\r
194 // logln("Rules starter for " + rules);
\r
195 genericOrderingTestWithResult(coll, s, result);
\r
196 } catch (Exception e) {
\r
197 warnln("Unable to open collator with rules " + rules);
\r
201 void genericRulesStarterWithOptionsAndResult(String rules, String[] s, String[] atts, Object[] attVals, int result) {
\r
202 RuleBasedCollator coll = null;
\r
204 coll = new RuleBasedCollator(rules);
\r
205 genericOptionsSetter(coll, atts, attVals);
\r
206 genericOrderingTestWithResult(coll, s, result);
\r
207 } catch (Exception e) {
\r
208 warnln("Unable to open collator with rules " + rules);
\r
211 void genericOrderingTestWithResult(Collator coll, String[] s, int result) {
\r
215 for(int i = 0; i < s.length - 1; i++) {
\r
216 for(int j = i+1; j < s.length; j++) {
\r
217 t1 = Utility.unescape(s[i]);
\r
218 t2 = Utility.unescape(s[j]);
\r
219 // System.out.println(i + " " + j);
\r
220 CollationTest.doTest(this, (RuleBasedCollator)coll, t1, t2,
\r
226 void reportCResult(String source, String target, CollationKey sourceKey, CollationKey targetKey,
\r
227 int compareResult, int keyResult, int incResult, int expectedResult ) {
\r
228 if (expectedResult < -1 || expectedResult > 1) {
\r
229 errln("***** invalid call to reportCResult ****");
\r
232 boolean ok1 = (compareResult == expectedResult);
\r
233 boolean ok2 = (keyResult == expectedResult);
\r
234 boolean ok3 = (incResult == expectedResult);
\r
235 if (ok1 && ok2 && ok3 /* synwee to undo && !isVerbose()*/) {
\r
238 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
\r
239 String msg2 = "\", \"";
\r
240 String msg3 = "\") returned ";
\r
241 String msg4 = "; expected ";
\r
242 String sExpect = new String("");
\r
243 String sResult = new String("");
\r
244 sResult = appendCompareResult(compareResult, sResult);
\r
245 sExpect = appendCompareResult(expectedResult, sExpect);
\r
247 // logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
249 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
251 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
252 msg2 = "\").compareTo(key(\"";
\r
253 msg3 = "\")) returned ";
\r
254 sResult = appendCompareResult(keyResult, sResult);
\r
256 // logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
258 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
261 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
263 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
265 msg3 = "\") returned ";
\r
266 sResult = appendCompareResult(incResult, sResult);
\r
268 // logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
270 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
275 String appendCompareResult(int result, String target) {
\r
276 if (result == -1) { //LESS
\r
278 } else if (result == 0) { //EQUAL
\r
280 } else if (result == 1) { //GREATER
\r
281 target += "GREATER";
\r
284 target += huh + result;
\r
289 String prettify(CollationKey sourceKey) {
\r
291 byte[] bytes= sourceKey.toByteArray();
\r
292 String target = "[";
\r
294 for (i = 0; i < bytes.length; i++) {
\r
295 String numStr = Integer.toHexString(bytes[i]);
\r
296 if (numStr.length()>2) {
\r
297 target += numStr.substring(numStr.length()-2);
\r
308 public void TestBeforePrefixFailure() {
\r
310 "&g <<< a&[before 3]\uff41 <<< x",
\r
311 "&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74&[before 3]\u30a7<<<\u30a9",
\r
312 "&[before 3]\u30a7<<<\u30a9&\u30A7=\u30A7=\u3047=\uff6a&\u30A8=\u30A8=\u3048=\uff74",
\r
314 String[][] data = {
\r
316 {"\u30a9", "\u30a7"},
\r
317 {"\u30a9", "\u30a7"},
\r
320 for(int i = 0; i< rules.length; i++) {
\r
321 genericRulesStarter(rules[i], data[i]);
\r
325 public void TestContractionClosure() {
\r
330 String[][] data = {
\r
331 { "b", "\u00e4\u00e4", "a\u0308a\u0308", "\u00e4a\u0308", "a\u0308\u00e4" },
\r
332 { "b", "\u00C5", "A\u030A", "\u212B" },
\r
335 for(int i = 0; i< rules.length; i++) {
\r
336 genericRulesStarterWithResult(rules[i], data[i], 0);
\r
340 public void TestPrefixCompose() {
\r
341 String rule1 = "&\u30a7<<<\u30ab|\u30fc=\u30ac|\u30fc";
\r
343 String string = rule1;
\r
345 RuleBasedCollator coll = new RuleBasedCollator(string);
\r
346 logln("rule:" + coll.getRules());
\r
347 } catch (Exception e) {
\r
348 warnln("Error open RuleBasedCollator rule = " + string);
\r
352 public void TestStrCollIdenticalPrefix() {
\r
353 String rule = "&\ud9b0\udc70=\ud9b0\udc71";
\r
358 genericRulesStarterWithResult(rule, test, 0);
\r
361 public void TestPrefix() {
\r
365 "[strength I]&a=\ud900\udc25&z<<<\ud900\udc25|a",
\r
367 String[][] data = {
\r
370 {"aa", "az", "\ud900\udc25z", "\ud900\udc25a", "zz"},
\r
373 for(int i = 0; i<rules.length; i++) {
\r
374 genericRulesStarter(rules[i], data[i]);
\r
378 public void TestNewJapanese() {
\r
381 "\u30b7\u30e3\u30fc\u30ec",
\r
382 "\u30b7\u30e3\u30a4",
\r
383 "\u30b7\u30e4\u30a3",
\r
384 "\u30b7\u30e3\u30ec",
\r
385 "\u3061\u3087\u3053",
\r
386 "\u3061\u3088\u3053",
\r
387 "\u30c1\u30e7\u30b3\u30ec\u30fc\u30c8",
\r
388 "\u3066\u30fc\u305f",
\r
389 "\u30c6\u30fc\u30bf",
\r
390 "\u30c6\u30a7\u30bf",
\r
391 "\u3066\u3048\u305f",
\r
392 "\u3067\u30fc\u305f",
\r
393 "\u30c7\u30fc\u30bf",
\r
394 "\u30c7\u30a7\u30bf",
\r
395 "\u3067\u3048\u305f",
\r
396 "\u3066\u30fc\u305f\u30fc",
\r
397 "\u30c6\u30fc\u30bf\u30a1",
\r
398 "\u30c6\u30a7\u30bf\u30fc",
\r
399 "\u3066\u3047\u305f\u3041",
\r
400 "\u3066\u3048\u305f\u30fc",
\r
401 "\u3067\u30fc\u305f\u30fc",
\r
402 "\u30c7\u30fc\u30bf\u30a1",
\r
403 "\u3067\u30a7\u305f\u30a1",
\r
404 "\u30c7\u3047\u30bf\u3041",
\r
405 "\u30c7\u30a8\u30bf\u30a2",
\r
407 "\u3073\u3085\u3042",
\r
408 "\u3074\u3085\u3042",
\r
409 "\u3073\u3085\u3042\u30fc",
\r
410 "\u30d3\u30e5\u30a2\u30fc",
\r
411 "\u3074\u3085\u3042\u30fc",
\r
412 "\u30d4\u30e5\u30a2\u30fc",
\r
413 "\u30d2\u30e5\u30a6",
\r
414 "\u30d2\u30e6\u30a6",
\r
415 "\u30d4\u30e5\u30a6\u30a2",
\r
416 "\u3073\u3085\u30fc\u3042\u30fc",
\r
417 "\u30d3\u30e5\u30fc\u30a2\u30fc",
\r
418 "\u30d3\u30e5\u30a6\u30a2\u30fc",
\r
419 "\u3072\u3085\u3093",
\r
420 "\u3074\u3085\u3093",
\r
421 "\u3075\u30fc\u308a",
\r
422 "\u30d5\u30fc\u30ea",
\r
423 "\u3075\u3045\u308a",
\r
424 "\u3075\u30a5\u308a",
\r
425 "\u3075\u30a5\u30ea",
\r
426 "\u30d5\u30a6\u30ea",
\r
427 "\u3076\u30fc\u308a",
\r
428 "\u30d6\u30fc\u30ea",
\r
429 "\u3076\u3045\u308a",
\r
430 "\u30d6\u30a5\u308a",
\r
431 "\u3077\u3046\u308a",
\r
432 "\u30d7\u30a6\u30ea",
\r
433 "\u3075\u30fc\u308a\u30fc",
\r
434 "\u30d5\u30a5\u30ea\u30fc",
\r
435 "\u3075\u30a5\u308a\u30a3",
\r
436 "\u30d5\u3045\u308a\u3043",
\r
437 "\u30d5\u30a6\u30ea\u30fc",
\r
438 "\u3075\u3046\u308a\u3043",
\r
439 "\u30d6\u30a6\u30ea\u30a4",
\r
440 "\u3077\u30fc\u308a\u30fc",
\r
441 "\u3077\u30a5\u308a\u30a4",
\r
442 "\u3077\u3046\u308a\u30fc",
\r
443 "\u30d7\u30a6\u30ea\u30a4",
\r
460 "\u306f\u309d", // H\u309d
\r
461 "\u30cf\u30fd", // K\u30fd
\r
462 "\u306f\u306f", // HH
\r
463 "\u306f\u30cf", // HK
\r
464 "\u30cf\u30cf", // KK
\r
465 "\u306f\u309e", // H\u309e
\r
466 "\u30cf\u30fe", // K\u30fe
\r
467 "\u306f\u3070", // HH\u309b
\r
468 "\u30cf\u30d0", // KK\u309b
\r
469 "\u306f\u3071", // HH\u309c
\r
470 "\u30cf\u3071", // KH\u309c
\r
471 "\u30cf\u30d1", // KK\u309c
\r
472 "\u3070\u309d", // H\u309b\u309d
\r
473 "\u30d0\u30fd", // K\u309b\u30fd
\r
474 "\u3070\u306f", // H\u309bH
\r
475 "\u30d0\u30cf", // K\u309bK
\r
476 "\u3070\u309e", // H\u309b\u309e
\r
477 "\u30d0\u30fe", // K\u309b\u30fe
\r
478 "\u3070\u3070", // H\u309bH\u309b
\r
479 "\u30d0\u3070", // K\u309bH\u309b
\r
480 "\u30d0\u30d0", // K\u309bK\u309b
\r
481 "\u3070\u3071", // H\u309bH\u309c
\r
482 "\u30d0\u30d1", // K\u309bK\u309c
\r
483 "\u3071\u309d", // H\u309c\u309d
\r
484 "\u30d1\u30fd", // K\u309c\u30fd
\r
485 "\u3071\u306f", // H\u309cH
\r
486 "\u30d1\u30cf", // K\u309cK
\r
487 "\u3071\u3070", // H\u309cH\u309b
\r
488 "\u3071\u30d0", // H\u309cK\u309b
\r
489 "\u30d1\u30d0", // K\u309cK\u309b
\r
490 "\u3071\u3071", // H\u309cH\u309c
\r
491 "\u30d1\u30d1", // K\u309cK\u309c
\r
494 String[] att = { "strength", };
\r
495 Object[] val = { new Integer(Collator.QUATERNARY), };
\r
497 String[] attShifted = { "strength", "AlternateHandling"};
\r
498 Object valShifted[] = { new Integer(Collator.QUATERNARY),
\r
501 genericLocaleStarterWithOptions(Locale.JAPANESE, test1, att, val);
\r
502 genericLocaleStarterWithOptions(Locale.JAPANESE, test2, att, val);
\r
504 genericLocaleStarterWithOptions(Locale.JAPANESE, test1, attShifted,
\r
506 genericLocaleStarterWithOptions(Locale.JAPANESE, test2, attShifted,
\r
510 void genericLocaleStarter(Locale locale, String s[]) {
\r
511 RuleBasedCollator coll = null;
\r
513 coll = (RuleBasedCollator)Collator.getInstance(locale);
\r
515 } catch (Exception e) {
\r
516 warnln("Unable to open collator for locale " + locale);
\r
519 // logln("Locale starter for " + locale);
\r
520 genericOrderingTest(coll, s);
\r
523 void genericLocaleStarterWithOptions(Locale locale, String[] s, String[] attrs, Object[] values) {
\r
524 genericLocaleStarterWithOptionsAndResult(locale, s, attrs, values, -1);
\r
527 private void genericOptionsSetter(RuleBasedCollator coll, String[] attrs, Object[] values) {
\r
528 for(int i = 0; i < attrs.length; i++) {
\r
529 if (attrs[i].equals("strength")) {
\r
530 coll.setStrength(((Integer)values[i]).intValue());
\r
532 else if (attrs[i].equals("decomp")) {
\r
533 coll.setDecomposition(((Integer)values[i]).intValue());
\r
535 else if (attrs[i].equals("AlternateHandling")) {
\r
536 coll.setAlternateHandlingShifted(((Boolean)values[i]
\r
539 else if (attrs[i].equals("NumericCollation")) {
\r
540 coll.setNumericCollation(((Boolean)values[i]).booleanValue());
\r
542 else if (attrs[i].equals("UpperFirst")) {
\r
543 coll.setUpperCaseFirst(((Boolean)values[i]).booleanValue());
\r
545 else if (attrs[i].equals("LowerFirst")) {
\r
546 coll.setLowerCaseFirst(((Boolean)values[i]).booleanValue());
\r
548 else if (attrs[i].equals("CaseLevel")) {
\r
549 coll.setCaseLevel(((Boolean)values[i]).booleanValue());
\r
554 void genericLocaleStarterWithOptionsAndResult(Locale locale, String[] s, String[] attrs, Object[] values, int result) {
\r
555 RuleBasedCollator coll = null;
\r
557 coll = (RuleBasedCollator)Collator.getInstance(locale);
\r
558 } catch (Exception e) {
\r
559 warnln("Unable to open collator for locale " + locale);
\r
562 // logln("Locale starter for " +locale);
\r
564 // logln("Setting attributes");
\r
565 genericOptionsSetter(coll, attrs, values);
\r
567 genericOrderingTestWithResult(coll, s, result);
\r
570 void genericOrderingTest(Collator coll, String[] s) {
\r
571 genericOrderingTestWithResult(coll, s, -1);
\r
574 public void TestNonChars() {
\r
577 "\uFFFE", "\uFFFF",
\r
578 "\\U0001FFFE", "\\U0001FFFF",
\r
579 "\\U0002FFFE", "\\U0002FFFF",
\r
580 "\\U0003FFFE", "\\U0003FFFF",
\r
581 "\\U0004FFFE", "\\U0004FFFF",
\r
582 "\\U0005FFFE", "\\U0005FFFF",
\r
583 "\\U0006FFFE", "\\U0006FFFF",
\r
584 "\\U0007FFFE", "\\U0007FFFF",
\r
585 "\\U0008FFFE", "\\U0008FFFF",
\r
586 "\\U0009FFFE", "\\U0009FFFF",
\r
587 "\\U000AFFFE", "\\U000AFFFF",
\r
588 "\\U000BFFFE", "\\U000BFFFF",
\r
589 "\\U000CFFFE", "\\U000CFFFF",
\r
590 "\\U000DFFFE", "\\U000DFFFF",
\r
591 "\\U000EFFFE", "\\U000EFFFF",
\r
592 "\\U000FFFFE", "\\U000FFFFF",
\r
593 "\\U0010FFFE", "\\U0010FFFF"
\r
595 Collator coll = null;
\r
597 coll = Collator.getInstance(new Locale("en", "US"));
\r
598 } catch (Exception e) {
\r
599 warnln("Unable to open collator");
\r
602 // logln("Test non characters");
\r
604 genericOrderingTestWithResult(coll, test, 0);
\r
607 public void TestExtremeCompression() {
\r
608 String[] test = new String[4];
\r
610 for(int i = 0; i<4; i++) {
\r
611 StringBuffer temp = new StringBuffer();
\r
612 for (int j = 0; j < 2047; j++) {
\r
615 temp.append((char)('a' + i));
\r
616 test[i] = temp.toString();
\r
619 genericLocaleStarter(new Locale("en", "US"), test);
\r
623 * Tests surrogate support.
\r
625 public void TestSurrogates() {
\r
626 String test[] = {"z","\ud900\udc25", "\ud805\udc50", "\ud800\udc00y",
\r
627 "\ud800\udc00r", "\ud800\udc00f", "\ud800\udc00",
\r
628 "\ud800\udc00c", "\ud800\udc00b", "\ud800\udc00fa",
\r
629 "\ud800\udc00fb", "\ud800\udc00a", "c", "b"};
\r
631 String rule = "&z < \ud900\udc25 < \ud805\udc50 < \ud800\udc00y "
\r
632 + "< \ud800\udc00r < \ud800\udc00f << \ud800\udc00 "
\r
633 + "< \ud800\udc00fa << \ud800\udc00fb < \ud800\udc00a "
\r
635 genericRulesStarter(rule, test);
\r
638 public void TestBocsuCoverage() {
\r
639 String test = "\u0041\u0441\u4441\\U00044441\u4441\u0441\u0041";
\r
640 Collator coll = Collator.getInstance();
\r
641 coll.setStrength(Collator.IDENTICAL);
\r
642 CollationKey key = coll.getCollationKey(test);
\r
643 logln("source:" + key.getSourceString());
\r
646 public void TestCyrillicTailoring() {
\r
652 genericLocaleStarter(new Locale("en", ""), test);
\r
653 genericRulesStarter("&\u0410 = \u0410", test);
\r
654 genericRulesStarter("&Z < \u0410", test);
\r
655 genericRulesStarter("&\u0410 = \u0410 < \u04d0", test);
\r
656 genericRulesStarter("&Z < \u0410 < \u04d0", test);
\r
657 genericRulesStarter("&\u0410 = \u0410 < \u0410\u0301", test);
\r
658 genericRulesStarter("&Z < \u0410 < \u0410\u0301", test);
\r
661 public void TestSuppressContractions() {
\r
662 String testNoCont2[] = {
\r
667 String testNoCont[] = {
\r
670 "\uFF21\u0410\u0302"
\r
673 genericRulesStarter("[suppressContractions [\u0400-\u047f]]", testNoCont);
\r
674 genericRulesStarter("[suppressContractions [\u0400-\u047f]]", testNoCont2);
\r
677 public void TestCase() {
\r
678 String gRules = "\u0026\u0030\u003C\u0031\u002C\u2460\u003C\u0061\u002C\u0041";
\r
679 String[] testCase = {
\r
680 "1a", "1A", "\u2460a", "\u2460A"
\r
682 int[][] caseTestResults = {
\r
683 { -1, -1, -1, 0, -1, -1, 0, 0, -1 },
\r
684 { 1, -1, -1, 0, -1, -1, 0, 0, 1 },
\r
685 { -1, -1, -1, 0, 1, -1, 0, 0, -1 },
\r
686 { 1, -1, 1, 0, -1, -1, 0, 0, 1 }
\r
689 boolean[][] caseTestAttributes = {
\r
697 Collator myCollation;
\r
699 myCollation = Collator.getInstance(new Locale("en", "US"));
\r
700 } catch (Exception e) {
\r
701 warnln("ERROR: in creation of rule based collator ");
\r
704 // logln("Testing different case settings");
\r
705 myCollation.setStrength(Collator.TERTIARY);
\r
707 for(k = 0; k <4; k++) {
\r
708 if (caseTestAttributes[k][0] == true) {
\r
709 // upper case first
\r
710 ((RuleBasedCollator)myCollation).setUpperCaseFirst(true);
\r
713 // upper case first
\r
714 ((RuleBasedCollator)myCollation).setLowerCaseFirst(true);
\r
716 ((RuleBasedCollator)myCollation).setCaseLevel(
\r
717 caseTestAttributes[k][1]);
\r
719 // logln("Case first = " + caseTestAttributes[k][0] + ", Case level = " + caseTestAttributes[k][1]);
\r
720 for (i = 0; i < 3 ; i++) {
\r
721 for(j = i+1; j<4; j++) {
\r
722 CollationTest.doTest(this,
\r
723 (RuleBasedCollator)myCollation,
\r
724 testCase[i], testCase[j],
\r
725 caseTestResults[k][3*i+j-1]);
\r
730 myCollation = new RuleBasedCollator(gRules);
\r
731 } catch (Exception e) {
\r
732 warnln("ERROR: in creation of rule based collator");
\r
735 // logln("Testing different case settings with custom rules");
\r
736 myCollation.setStrength(Collator.TERTIARY);
\r
738 for(k = 0; k<4; k++) {
\r
739 if (caseTestAttributes[k][0] == true) {
\r
740 ((RuleBasedCollator)myCollation).setUpperCaseFirst(true);
\r
743 ((RuleBasedCollator)myCollation).setUpperCaseFirst(false);
\r
745 ((RuleBasedCollator)myCollation).setCaseLevel(
\r
746 caseTestAttributes[k][1]);
\r
747 for (i = 0; i < 3 ; i++) {
\r
748 for(j = i+1; j<4; j++) {
\r
749 CollationTest.doTest(this,
\r
750 (RuleBasedCollator)myCollation,
\r
751 testCase[i], testCase[j],
\r
752 caseTestResults[k][3*i+j-1]);
\r
758 String[] lowerFirst = {
\r
774 String[] upperFirst = {
\r
789 // logln("mixed case test");
\r
790 // logln("lower first, case level off");
\r
791 genericRulesStarter("[casefirst lower]&H<ch<<<Ch<<<CH", lowerFirst);
\r
792 // logln("upper first, case level off");
\r
793 genericRulesStarter("[casefirst upper]&H<ch<<<Ch<<<CH", upperFirst);
\r
794 // logln("lower first, case level on");
\r
795 genericRulesStarter("[casefirst lower][caselevel on]&H<ch<<<Ch<<<CH", lowerFirst);
\r
796 // logln("upper first, case level on");
\r
797 genericRulesStarter("[casefirst upper][caselevel on]&H<ch<<<Ch<<<CH", upperFirst);
\r
801 public void TestIncompleteCnt() {
\r
827 RuleBasedCollator coll = null;
\r
828 String temp = " & Z < ABC < Q < B";
\r
830 coll = new RuleBasedCollator(temp);
\r
831 } catch (Exception e) {
\r
832 warnln("fail to create RuleBasedCollator");
\r
836 int size = cnt1.length;
\r
837 for(int i = 0; i < size-1; i++) {
\r
838 for(int j = i+1; j < size; j++) {
\r
839 String t1 = cnt1[i];
\r
840 String t2 = cnt1[j];
\r
841 CollationTest.doTest(this, coll, t1, t2, -1);
\r
845 temp = " & Z < DAVIS < MARK <DAV";
\r
847 coll = new RuleBasedCollator(temp);
\r
848 } catch (Exception e) {
\r
849 warnln("fail to create RuleBasedCollator");
\r
853 size = cnt2.length;
\r
854 for(int i = 0; i < size-1; i++) {
\r
855 for(int j = i+1; j < size; j++) {
\r
856 String t1 = cnt2[i];
\r
857 String t2 = cnt2[j];
\r
858 CollationTest.doTest(this, coll, t1, t2, -1);
\r
863 public void TestBlackBird() {
\r
864 String[] shifted = {
\r
875 int[] shiftedTert = {
\r
886 String[] nonignorable = {
\r
899 Collator coll = Collator.getInstance(new Locale("en", "US"));
\r
900 //ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_OFF, &status);
\r
901 //ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
\r
902 ((RuleBasedCollator)coll).setAlternateHandlingShifted(false);
\r
903 size = nonignorable.length;
\r
904 for(i = 0; i < size-1; i++) {
\r
905 for(j = i+1; j < size; j++) {
\r
906 String t1 = nonignorable[i];
\r
907 String t2 = nonignorable[j];
\r
908 CollationTest.doTest(this, (RuleBasedCollator)coll, t1, t2, -1);
\r
911 ((RuleBasedCollator)coll).setAlternateHandlingShifted(true);
\r
912 coll.setStrength(Collator.QUATERNARY);
\r
913 size = shifted.length;
\r
914 for(i = 0; i < size-1; i++) {
\r
915 for(j = i+1; j < size; j++) {
\r
916 String t1 = shifted[i];
\r
917 String t2 = shifted[j];
\r
918 CollationTest.doTest(this, (RuleBasedCollator)coll, t1, t2, -1);
\r
921 coll.setStrength(Collator.TERTIARY);
\r
922 size = shifted.length;
\r
923 for(i = 1; i < size; i++) {
\r
924 String t1 = shifted[i-1];
\r
925 String t2 = shifted[i];
\r
926 CollationTest.doTest(this, (RuleBasedCollator)coll, t1, t2,
\r
931 public void TestFunkyA() {
\r
932 String[] testSourceCases = {
\r
933 "\u0041\u0300\u0301",
\r
934 "\u0041\u0300\u0316",
\r
937 // this would work with forced normalization
\r
941 String[] testTargetCases = {
\r
942 "\u0041\u0301\u0300",
\r
943 "\u0041\u0316\u0300",
\r
945 "\u0041\u0301\u0300",
\r
946 // this would work with forced normalization
\r
947 "\u0041\u0316\u0300",
\r
958 Collator myCollation;
\r
960 myCollation = Collator.getInstance(new Locale("en", "US"));
\r
961 } catch (Exception e) {
\r
962 warnln("ERROR: in creation of rule based collator");
\r
965 // logln("Testing some A letters, for some reason");
\r
966 myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
967 myCollation.setStrength(Collator.TERTIARY);
\r
968 for (int i = 0; i < 4 ; i++)
\r
970 CollationTest.doTest(this, (RuleBasedCollator)myCollation,
\r
971 testSourceCases[i], testTargetCases[i],
\r
976 public void TestChMove() {
\r
977 String[] chTest = {
\r
980 "ca", "cb", "cx", "cy", "CZ",
\r
981 "c\u030C", "C\u030C",
\r
984 "ha", "Ha", "harly", "hb", "HB", "hx", "HX", "hy", "HY",
\r
985 "ch", "cH", "Ch", "CH",
\r
986 "cha", "charly", "che", "chh", "chch", "chr",
\r
989 "r\u030C", "R\u030C",
\r
992 "s\u030C", "S\u030C",
\r
994 "z\u030C", "Z\u030C"
\r
996 Collator coll = null;
\r
998 coll = Collator.getInstance(new Locale("cs", ""));
\r
999 } catch (Exception e) {
\r
1000 warnln("Cannot create Collator");
\r
1003 int size = chTest.length;
\r
1004 for(int i = 0; i < size-1; i++) {
\r
1005 for(int j = i+1; j < size; j++) {
\r
1006 String t1 = chTest[i];
\r
1007 String t2 = chTest[j];
\r
1008 CollationTest.doTest(this, (RuleBasedCollator)coll, t1, t2, -1);
\r
1013 public void TestImplicitTailoring() {
\r
1014 String rules[] = { "&[before 1]\u4e00 < b < c &[before 1]\u4e00 < d < e",
\r
1015 "&\u4e00 < a <<< A < b <<< B",
\r
1016 "&[before 1]\u4e00 < \u4e01 < \u4e02",
\r
1017 "&[before 1]\u4e01 < \u4e02 < \u4e03",
\r
1019 String cases[][] = {
\r
1020 { "d", "e", "b", "c", "\u4e00"},
\r
1021 { "\u4e00", "a", "A", "b", "B", "\u4e01"},
\r
1022 { "\u4e01", "\u4e02", "\u4e00"},
\r
1023 { "\u4e02", "\u4e03", "\u4e01"},
\r
1028 for(i = 0; i < rules.length; i++) {
\r
1029 genericRulesStarter(rules[i], cases[i]);
\r
1034 public void TestFCDProblem() {
\r
1035 String s1 = "\u0430\u0306\u0325";
\r
1036 String s2 = "\u04D1\u0325";
\r
1037 Collator coll = null;
\r
1039 coll = Collator.getInstance();
\r
1040 } catch (Exception e) {
\r
1041 warnln("Can't create collator");
\r
1045 coll.setDecomposition(Collator.NO_DECOMPOSITION);
\r
1046 CollationTest.doTest(this, (RuleBasedCollator)coll, s1, s2, 0);
\r
1047 coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
1048 CollationTest.doTest(this, (RuleBasedCollator)coll, s1, s2, 0);
\r
1051 public void TestEmptyRule() {
\r
1052 String rulez = "";
\r
1054 RuleBasedCollator coll = new RuleBasedCollator(rulez);
\r
1055 logln("rule:" + coll.getRules());
\r
1056 } catch (Exception e) {
\r
1057 warnln(e.getMessage());
\r
1061 /* superseded by TestBeforePinyin, since Chinese collation rules have changed */
\r
1063 public void TestJ784() {
\r
1065 "A", "\u0101", "\u00e1", "\u01ce", "\u00e0",
\r
1066 "E", "\u0113", "\u00e9", "\u011b", "\u00e8",
\r
1067 "I", "\u012b", "\u00ed", "\u01d0", "\u00ec",
\r
1068 "O", "\u014d", "\u00f3", "\u01d2", "\u00f2",
\r
1069 "U", "\u016b", "\u00fa", "\u01d4", "\u00f9",
\r
1070 "\u00fc", "\u01d6", "\u01d8", "\u01da", "\u01dc"
\r
1072 genericLocaleStarter(new Locale("zh", ""), data);
\r
1076 public void TestJ815() {
\r
1093 genericLocaleStarter(new Locale("fr", ""), data);
\r
1094 genericRulesStarter("[backwards 2]&A<<\u00e6/e<<<\u00c6/E", data);
\r
1097 public void TestJ3087()
\r
1099 String rule[] = {"&h<H&CH=\u0427",
\r
1102 RuleBasedCollator rbc = null;
\r
1103 CollationElementIterator iter1;
\r
1104 CollationElementIterator iter2;
\r
1105 for (int i = 0; i < rule.length; i ++) {
\r
1107 rbc = new RuleBasedCollator(rule[i]);
\r
1108 } catch (Exception e) {
\r
1109 warnln(e.getMessage());
\r
1112 iter1 = rbc.getCollationElementIterator("CH");
\r
1113 iter2 = rbc.getCollationElementIterator("\u0427");
\r
1114 int ce1 = CollationElementIterator.IGNORABLE;
\r
1115 int ce2 = CollationElementIterator.IGNORABLE;
\r
1116 while (ce1 != CollationElementIterator.NULLORDER
\r
1117 && ce2 != CollationElementIterator.NULLORDER) {
\r
1118 ce1 = iter1.next();
\r
1119 ce2 = iter2.next();
\r
1121 errln("Error generating RuleBasedCollator with the rule "
\r
1123 errln("CH != \\u0427");
\r
1129 public void DontTestJ831() { // Latvian does not use upper first
\r
1136 genericLocaleStarter(new Locale("lv", ""), data);
\r
1139 public void TestBefore() {
\r
1141 "\u0101", "\u00e1", "\u01ce", "\u00e0", "A",
\r
1142 "\u0113", "\u00e9", "\u011b", "\u00e8", "E",
\r
1143 "\u012b", "\u00ed", "\u01d0", "\u00ec", "I",
\r
1144 "\u014d", "\u00f3", "\u01d2", "\u00f2", "O",
\r
1145 "\u016b", "\u00fa", "\u01d4", "\u00f9", "U",
\r
1146 "\u01d6", "\u01d8", "\u01da", "\u01dc", "\u00fc"
\r
1148 genericRulesStarter(
\r
1149 "&[before 1]a<\u0101<\u00e1<\u01ce<\u00e0"
\r
1150 + "&[before 1]e<\u0113<\u00e9<\u011b<\u00e8"
\r
1151 + "&[before 1]i<\u012b<\u00ed<\u01d0<\u00ec"
\r
1152 + "&[before 1]o<\u014d<\u00f3<\u01d2<\u00f2"
\r
1153 + "&[before 1]u<\u016b<\u00fa<\u01d4<\u00f9"
\r
1154 + "&u<\u01d6<\u01d8<\u01da<\u01dc<\u00fc", data);
\r
1157 public void TestRedundantRules() {
\r
1158 String[] rules = {
\r
1159 //"& a <<< b <<< c << d <<< e& [before 1] e <<< x",
\r
1160 "& b <<< c <<< d << e <<< f& [before 3] f <<< x",
\r
1161 "& a < b <<< c << d <<< e& [before 1] e <<< x",
\r
1162 "& a < b < c < d& [before 1] c < m",
\r
1163 "& a < b <<< c << d <<< e& [before 3] e <<< x",
\r
1164 "& a < b <<< c << d <<< e& [before 2] e <<< x",
\r
1165 "& a < b <<< c << d <<< e <<< f < g& [before 1] g < x",
\r
1166 "& a <<< b << c < d& a < m",
\r
1167 "&a<b<<b\u0301 &z<b",
\r
1170 "& a < b < c < d& r < c",
\r
1171 "& a < b < c < d& r < c",
\r
1172 "& a < b < c < d& c < m",
\r
1173 "& a < b < c < d& a < m"
\r
1176 String[] expectedRules = {
\r
1179 "& a <<< x < b <<< c << d <<< e",
\r
1180 "& a < b < m < c < d",
\r
1181 "& a < b <<< c << d <<< x <<< e",
\r
1182 "& a < b <<< c <<< x << d <<< e",
\r
1183 "& a < b <<< c << d <<< e <<< f < x < g",
\r
1184 "& a <<< b << c < m < d",
\r
1185 "&a<b\u0301 &z<b",
\r
1188 "& a < b < d& r < c",
\r
1189 "& a < b < d& r < c",
\r
1190 "& a < b < c < m < d",
\r
1191 "& a < m < b < c < d"
\r
1194 String[][] testdata = {
\r
1195 // {"\u2089", "x"},
\r
1197 {"a", "x", "b", "c", "d", "e"},
\r
1198 {"a", "b", "m", "c", "d"},
\r
1199 {"a", "b", "c", "d", "x", "e"},
\r
1200 {"a", "b", "c", "x", "d", "e"},
\r
1201 {"a", "b", "c", "d", "e", "f", "x", "g"},
\r
1202 {"a", "b", "c", "m", "d"},
\r
1203 {"a", "b\u0301", "z", "b"},
\r
1208 {"a", "b", "c", "m", "d"},
\r
1209 {"a", "m", "b", "c", "d"}
\r
1213 for(int i = 0; i<rules.length; i++) {
\r
1214 logln("testing rule " + rules[i] + ", expected to be" + expectedRules[i]);
\r
1217 Collator credundant = new RuleBasedCollator(rlz);
\r
1218 rlz = expectedRules[i];
\r
1219 Collator cresulting = new RuleBasedCollator(rlz);
\r
1220 logln(" credundant Rule:" + ((RuleBasedCollator)credundant).getRules());
\r
1221 logln(" cresulting Rule:" + ((RuleBasedCollator)cresulting).getRules());
\r
1222 } catch (Exception e) {
\r
1223 warnln("Cannot create RuleBasedCollator");
\r
1225 //testAgainstUCA(cresulting, credundant, "expected", TRUE, &status);
\r
1226 // logln("testing using data\n");
\r
1227 genericRulesStarter(rules[i], testdata[i]);
\r
1231 public void TestExpansionSyntax() {
\r
1232 String[] rules = {
\r
1233 "&AE <<< a << b <<< c &d <<< f",
\r
1234 "&AE <<< a <<< b << c << d < e < f <<< g",
\r
1235 "&AE <<< B <<< C / D <<< F"
\r
1238 String[] expectedRules = {
\r
1239 "&A <<< a / E << b / E <<< c /E &d <<< f",
\r
1240 "&A <<< a / E <<< b / E << c / E << d / E < e < f <<< g",
\r
1241 "&A <<< B / E <<< C / ED <<< F / E"
\r
1244 String[][] testdata = {
\r
1245 {"AE", "a", "b", "c"},
\r
1246 {"AE", "a", "b", "c", "d", "e", "f", "g"},
\r
1247 {"AE", "B", "C"} // / ED <<< F / E"},
\r
1250 for(int i = 0; i<rules.length; i++) {
\r
1251 // logln("testing rule " + rules[i] + ", expected to be " + expectedRules[i]);
\r
1253 String rlz = rules[i];
\r
1254 Collator credundant = new RuleBasedCollator(rlz);
\r
1255 rlz = expectedRules[i];
\r
1256 Collator cresulting = new RuleBasedCollator(rlz);
\r
1257 logln(" credundant Rule:" + ((RuleBasedCollator)credundant).getRules());
\r
1258 logln(" cresulting Rule:" + ((RuleBasedCollator)cresulting).getRules());
\r
1259 } catch (Exception e) {
\r
1260 warnln(e.getMessage());
\r
1262 // testAgainstUCA still doesn't handle expansions correctly, so this is not run
\r
1263 // as a hard error test, but only in information mode
\r
1264 //testAgainstUCA(cresulting, credundant, "expected", FALSE, &status);
\r
1266 // logln("testing using data");
\r
1267 genericRulesStarter(rules[i], testdata[i]);
\r
1271 public void TestHangulTailoring() {
\r
1272 String[] koreanData = {
\r
1273 "\uac00", "\u4f3d", "\u4f73", "\u5047", "\u50f9", "\u52a0", "\u53ef", "\u5475",
\r
1274 "\u54e5", "\u5609", "\u5ac1", "\u5bb6", "\u6687", "\u67b6", "\u67b7", "\u67ef",
\r
1275 "\u6b4c", "\u73c2", "\u75c2", "\u7a3c", "\u82db", "\u8304", "\u8857", "\u8888",
\r
1276 "\u8a36", "\u8cc8", "\u8dcf", "\u8efb", "\u8fe6", "\u99d5",
\r
1277 "\u4EEE", "\u50A2", "\u5496", "\u54FF", "\u5777", "\u5B8A", "\u659D", "\u698E",
\r
1278 "\u6A9F", "\u73C8", "\u7B33", "\u801E", "\u8238", "\u846D", "\u8B0C"
\r
1282 "&\uac00 <<< \u4f3d <<< \u4f73 <<< \u5047 <<< \u50f9 <<< \u52a0 <<< \u53ef <<< \u5475 "
\r
1283 + "<<< \u54e5 <<< \u5609 <<< \u5ac1 <<< \u5bb6 <<< \u6687 <<< \u67b6 <<< \u67b7 <<< \u67ef "
\r
1284 + "<<< \u6b4c <<< \u73c2 <<< \u75c2 <<< \u7a3c <<< \u82db <<< \u8304 <<< \u8857 <<< \u8888 "
\r
1285 + "<<< \u8a36 <<< \u8cc8 <<< \u8dcf <<< \u8efb <<< \u8fe6 <<< \u99d5 "
\r
1286 + "<<< \u4EEE <<< \u50A2 <<< \u5496 <<< \u54FF <<< \u5777 <<< \u5B8A <<< \u659D <<< \u698E "
\r
1287 + "<<< \u6A9F <<< \u73C8 <<< \u7B33 <<< \u801E <<< \u8238 <<< \u846D <<< \u8B0C";
\r
1289 String rlz = rules;
\r
1291 Collator coll = null;
\r
1293 coll = new RuleBasedCollator(rlz);
\r
1294 } catch (Exception e) {
\r
1295 warnln("Unable to open collator with rules" + rules);
\r
1298 // logln("Using start of korean rules\n");
\r
1299 genericOrderingTest(coll, koreanData);
\r
1300 // logln("Setting jamoSpecial to TRUE and testing once more\n");
\r
1302 // can't set jamo in icu4j
\r
1303 // ((UCATableHeader *)coll->image)->jamoSpecial = TRUE; // don't try this at home
\r
1304 // genericOrderingTest(coll, koreanData);
\r
1306 // no such locale in icu4j
\r
1307 // logln("Using ko__LOTUS locale\n");
\r
1308 // genericLocaleStarter(new Locale("ko__LOTUS", ""), koreanData);
\r
1311 public void TestIncrementalNormalize() {
\r
1312 Collator coll = null;
\r
1313 // logln("Test 1 ....");
\r
1315 /* Test 1. Run very long unnormalized strings, to force overflow of*/
\r
1316 /* most buffers along the way.*/
\r
1319 coll = Collator.getInstance(new Locale("en", "US"));
\r
1320 } catch (Exception e) {
\r
1321 warnln("Cannot get default instance!");
\r
1325 char ccMix[] = {0x316, 0x321, 0x300};
\r
1328 StringBuffer strA = new StringBuffer();
\r
1329 StringBuffer strB = new StringBuffer();
\r
1331 coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
1333 for (sLen = 1000; sLen<1001; sLen++) {
\r
1334 strA.delete(0, strA.length());
\r
1335 strA.append(baseA);
\r
1336 strB.delete(0, strB.length());
\r
1337 strB.append(baseA);
\r
1338 for (i=1; i< sLen; i++) {
\r
1339 strA.append(ccMix[i % 3]);
\r
1340 strB.insert(1, ccMix[i % 3]);
\r
1342 coll.setStrength(Collator.TERTIARY); // Do test with default strength, which runs
\r
1343 CollationTest.doTest(this, (RuleBasedCollator)coll,
\r
1344 strA.toString(), strB.toString(), 0); // optimized functions in the impl
\r
1345 coll.setStrength(Collator.IDENTICAL); // Do again with the slow, general impl.
\r
1346 CollationTest.doTest(this, (RuleBasedCollator)coll,
\r
1347 strA.toString(), strB.toString(), 0);
\r
1350 /* Test 2: Non-normal sequence in a string that extends to the last character*/
\r
1351 /* of the string. Checks a couple of edge cases.*/
\r
1352 // logln("Test 2 ....");
\r
1354 String strA = "AA\u0300\u0316";
\r
1355 String strB = "A\u00c0\u0316";
\r
1356 coll.setStrength(Collator.TERTIARY);
\r
1357 CollationTest.doTest(this, (RuleBasedCollator)coll, strA, strB, 0);
\r
1359 /* Test 3: Non-normal sequence is terminated by a surrogate pair.*/
\r
1360 // logln("Test 3 ....");
\r
1362 String strA = "AA\u0300\u0316\uD800\uDC01";
\r
1363 String strB = "A\u00c0\u0316\uD800\uDC00";
\r
1364 coll.setStrength(Collator.TERTIARY);
\r
1365 CollationTest.doTest(this, (RuleBasedCollator)coll, strA, strB, 1);
\r
1367 /* Test 4: Imbedded nulls do not terminate a string when length is specified.*/
\r
1368 // logln("Test 4 ....");
\r
1370 * not a valid test since string are null-terminated in java{
\r
1371 char strA[] = {0x41, 0x00, 0x42};
\r
1372 char strB[] = {0x41, 0x00, 0x00};
\r
1374 int result = coll.compare(new String(strA), new String(strB));
\r
1375 if (result != 1) {
\r
1376 errln("ERROR 1 in test 4\n");
\r
1379 result = coll.compare(new String(strA, 0, 1), new String(strB, 0, 1));
\r
1380 if (result != 0) {
\r
1381 errln("ERROR 1 in test 4\n");
\r
1384 CollationKey sortKeyA = coll.getCollationKey(new String(strA));
\r
1385 CollationKey sortKeyB = coll.getCollationKey(new String(strB));
\r
1387 int r = sortKeyA.compareTo(sortKeyB);
\r
1389 errln("Error 4 in test 4\n");
\r
1392 coll.setStrength(Collator.IDENTICAL);
\r
1393 sortKeyA = coll.getCollationKey(new String(strA));
\r
1394 sortKeyB = coll.getCollationKey(new String(strB));
\r
1396 r = sortKeyA.compareTo(sortKeyB);
\r
1398 errln("Error 7 in test 4\n");
\r
1401 coll.setStrength(Collator.TERTIARY);
\r
1404 /* Test 5: Null characters in non-normal source strings.*/
\r
1405 // logln("Test 5 ....");
\r
1407 * not a valid test since string are null-terminated in java{
\r
1409 char strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42,};
\r
1410 char strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00,};
\r
1413 int result = coll.compare(new String(strA, 0, 6), new String(strB, 0, 6));
\r
1415 errln("ERROR 1 in test 5\n");
\r
1417 result = coll.compare(new String(strA, 0, 4), new String(strB, 0, 4));
\r
1418 if (result != 0) {
\r
1419 errln("ERROR 2 in test 5\n");
\r
1422 CollationKey sortKeyA = coll.getCollationKey(new String(strA));
\r
1423 CollationKey sortKeyB = coll.getCollationKey(new String(strB));
\r
1424 int r = sortKeyA.compareTo(sortKeyB);
\r
1426 errln("Error 4 in test 5\n");
\r
1429 coll.setStrength(Collator.IDENTICAL);
\r
1431 sortKeyA = coll.getCollationKey(new String(strA));
\r
1432 sortKeyB = coll.getCollationKey(new String(strB));
\r
1433 r = sortKeyA.compareTo(sortKeyB);
\r
1435 errln("Error 7 in test 5\n");
\r
1438 coll.setStrength(Collator.TERTIARY);
\r
1441 /* Test 6: Null character as base of a non-normal combining sequence.*/
\r
1442 // logln("Test 6 ....");
\r
1444 * not a valid test since string are null-terminated in java{
\r
1446 char strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302,};
\r
1447 char strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300,};
\r
1449 int result = coll.compare(new String(strA, 0, 5), new String(strB, 0, 5));
\r
1450 if (result != -1) {
\r
1451 errln("Error 1 in test 6\n");
\r
1453 result = coll.compare(new String(strA, 0, 1), new String(strB, 0, 1));
\r
1454 if (result != 0) {
\r
1455 errln("Error 2 in test 6\n");
\r
1461 public void TestContraction() {
\r
1462 String[] testrules = {
\r
1464 "&A = A\\u0306/\\u0306",
\r
1467 String[] testdata = {
\r
1468 "AB", "AB", "A\u0306", "ch"
\r
1470 String[] testdata2 = {
\r
1475 String[] testrules3 = {
\r
1476 "&z < xyz &xyzw << B",
\r
1477 "&z < xyz &xyz << B / w",
\r
1478 "&z < ch &achm << B",
\r
1479 "&z < ch &a << B / chm",
\r
1480 "&\ud800\udc00w << B",
\r
1481 "&\ud800\udc00 << B / w",
\r
1482 "&a\ud800\udc00m << B",
\r
1483 "&a << B / \ud800\udc00m",
\r
1486 RuleBasedCollator coll = null;
\r
1487 for (int i = 0; i < testrules.length; i ++) {
\r
1488 CollationElementIterator iter1 = null;
\r
1490 // logln("Rule " + testrules[i] + " for testing\n");
\r
1491 String rule = testrules[i];
\r
1493 coll = new RuleBasedCollator(rule);
\r
1494 } catch (Exception e) {
\r
1495 warnln("Collator creation failed " + testrules[i]);
\r
1499 iter1 = coll.getCollationElementIterator(testdata[i]);
\r
1500 } catch (Exception e) {
\r
1501 errln("Collation iterator creation failed\n");
\r
1505 CollationElementIterator iter2;
\r
1508 iter2 = coll.getCollationElementIterator(String.valueOf(testdata[i].charAt(j)));
\r
1510 }catch (Exception e) {
\r
1511 errln("Collation iterator creation failed\n");
\r
1514 ce = iter2.next();
\r
1515 while (ce != CollationElementIterator.NULLORDER) {
\r
1516 if (iter1.next() != ce) {
\r
1517 errln("Collation elements in contraction split does not match\n");
\r
1520 ce = iter2.next();
\r
1524 if (iter1.next() != CollationElementIterator.NULLORDER) {
\r
1525 errln("Collation elements not exhausted\n");
\r
1529 String rule = "& a < b < c < ch < d & c = ch / h";
\r
1531 coll = new RuleBasedCollator(rule);
\r
1532 } catch (Exception e) {
\r
1533 errln("cannot create rulebased collator");
\r
1537 if (coll.compare(testdata2[0], testdata2[1]) != -1) {
\r
1538 errln("Expected " + testdata2[0] + " < " + testdata2[1]);
\r
1541 if (coll.compare(testdata2[1], testdata2[2]) != -1) {
\r
1542 errln("Expected " + testdata2[1] + " < " + testdata2[2]);
\r
1545 for (int i = 0; i < testrules3.length; i += 2) {
\r
1546 RuleBasedCollator coll1, coll2;
\r
1547 CollationElementIterator iter1, iter2;
\r
1550 rule = testrules3[i];
\r
1552 coll1 = new RuleBasedCollator(rule);
\r
1553 } catch (Exception e) {
\r
1554 errln("Fail: cannot create rulebased collator, rule:" + rule);
\r
1557 rule = testrules3[i + 1];
\r
1559 coll2 = new RuleBasedCollator(rule);
\r
1560 } catch (Exception e) {
\r
1561 errln("Collator creation failed " + testrules[i]);
\r
1565 iter1 = coll1.getCollationElementIterator(String.valueOf(ch));
\r
1566 iter2 = coll2.getCollationElementIterator(String.valueOf(ch));
\r
1567 } catch (Exception e) {
\r
1568 errln("Collation iterator creation failed\n");
\r
1571 ce = iter1.next();
\r
1573 while (ce != CollationElementIterator.NULLORDER) {
\r
1574 if (ce != iter2.next()) {
\r
1575 errln("CEs does not match\n");
\r
1578 ce = iter1.next();
\r
1580 if (iter2.next() != CollationElementIterator.NULLORDER) {
\r
1581 errln("CEs not exhausted\n");
\r
1587 public void TestExpansion() {
\r
1588 String[] testrules = {
\r
1589 "&J << K / B & K << M",
\r
1590 "&J << K / B << M"
\r
1592 String[] testdata = {
\r
1593 "JA", "MA", "KA", "KC", "JC", "MC",
\r
1597 for (int i = 0; i < testrules.length; i++) {
\r
1598 // logln("Rule " + testrules[i] + " for testing\n");
\r
1599 String rule = testrules[i];
\r
1601 coll = new RuleBasedCollator(rule);
\r
1602 } catch (Exception e) {
\r
1603 warnln("Collator creation failed " + testrules[i]);
\r
1607 for (int j = 0; j < 5; j ++) {
\r
1608 CollationTest.doTest(this, (RuleBasedCollator)coll,
\r
1609 testdata[j], testdata[j + 1], -1);
\r
1614 public void TestContractionEndCompare()
\r
1616 String rules = "&b=ch";
\r
1617 String src = "bec";
\r
1618 String tgt = "bech";
\r
1619 Collator coll = null;
\r
1621 coll = new RuleBasedCollator(rules);
\r
1622 } catch (Exception e) {
\r
1623 warnln("Collator creation failed " + rules);
\r
1626 CollationTest.doTest(this, (RuleBasedCollator)coll, src, tgt, 1);
\r
1629 public void TestLocaleRuleBasedCollators() {
\r
1630 if (getInclusion() < 5) {
\r
1631 // not serious enough to run this
\r
1634 Locale locale[] = Collator.getAvailableLocales();
\r
1635 String prevrule = null;
\r
1636 for (int i = 0; i < locale.length; i ++) {
\r
1637 Locale l = locale[i];
\r
1639 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,l);
\r
1640 String collkey = rb.getStringWithFallback("collations/default");
\r
1641 ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
\r
1642 if (elements == null) {
\r
1645 String rule = null;
\r
1647 Object[][] colldata = (Object[][])elements;
\r
1649 if (colldata[0][1] instanceof byte[]){
\r
1650 rule = (String)colldata[1][1];
\r
1653 rule = (String)colldata[0][1];
\r
1656 rule = elements.getString("Sequence");
\r
1658 RuleBasedCollator col1 =
\r
1659 (RuleBasedCollator)Collator.getInstance(l);
\r
1660 if (!rule.equals(col1.getRules())) {
\r
1661 errln("Rules should be the same in the RuleBasedCollator and Locale");
\r
1663 if (rule != null && rule.length() > 0
\r
1664 && !rule.equals(prevrule)) {
\r
1665 RuleBasedCollator col2 = new RuleBasedCollator(rule);
\r
1666 if (!col1.equals(col2)) {
\r
1667 errln("Error creating RuleBasedCollator from " +
\r
1668 "locale rules for " + l.toString());
\r
1672 } catch (Exception e) {
\r
1673 warnln("Error retrieving resource bundle for testing: " + e.toString());
\r
1678 public void TestOptimize() {
\r
1679 /* this is not really a test - just trying out
\r
1680 * whether copying of UCA contents will fail
\r
1681 * Cannot really test, since the functionality
\r
1682 * remains the same.
\r
1684 String rules[] = {
\r
1685 "[optimize [\\uAC00-\\uD7FF]]"
\r
1687 String data[][] = {
\r
1692 for(i = 0; i<rules.length; i++) {
\r
1693 genericRulesStarter(rules[i], data[i]);
\r
1697 public void TestIdenticalCompare()
\r
1700 RuleBasedCollator coll
\r
1701 = new RuleBasedCollator("& \uD800\uDC00 = \uD800\uDC01");
\r
1702 String strA = "AA\u0300\u0316\uD800\uDC01";
\r
1703 String strB = "A\u00c0\u0316\uD800\uDC00";
\r
1704 coll.setStrength(Collator.IDENTICAL);
\r
1705 CollationTest.doTest(this, coll, strA, strB, 1);
\r
1706 } catch (Exception e) {
\r
1707 warnln(e.getMessage());
\r
1711 public void TestMergeSortKeys()
\r
1713 String cases[] = {"abc", "abcd", "abcde"};
\r
1714 String prefix = "foo";
\r
1715 String suffix = "egg";
\r
1716 CollationKey mergedPrefixKeys[] = new CollationKey[cases.length];
\r
1717 CollationKey mergedSuffixKeys[] = new CollationKey[cases.length];
\r
1719 Collator coll = Collator.getInstance(Locale.ENGLISH);
\r
1720 genericLocaleStarter(Locale.ENGLISH, cases);
\r
1722 int strength = Collator.PRIMARY;
\r
1723 while (strength <= Collator.IDENTICAL) {
\r
1724 coll.setStrength(strength);
\r
1725 CollationKey prefixKey = coll.getCollationKey(prefix);
\r
1726 CollationKey suffixKey = coll.getCollationKey(suffix);
\r
1727 for (int i = 0; i < cases.length; i ++) {
\r
1728 CollationKey key = coll.getCollationKey(cases[i]);
\r
1729 mergedPrefixKeys[i] = prefixKey.merge(key);
\r
1730 mergedSuffixKeys[i] = suffixKey.merge(key);
\r
1731 if (mergedPrefixKeys[i].getSourceString() != null
\r
1732 || mergedSuffixKeys[i].getSourceString() != null) {
\r
1733 errln("Merged source string error: expected null");
\r
1736 if (mergedPrefixKeys[i-1].compareTo(mergedPrefixKeys[i])
\r
1738 errln("Error while comparing prefixed keys @ strength "
\r
1740 errln(prettify(mergedPrefixKeys[i-1]));
\r
1741 errln(prettify(mergedPrefixKeys[i]));
\r
1743 if (mergedSuffixKeys[i-1].compareTo(mergedSuffixKeys[i])
\r
1745 errln("Error while comparing suffixed keys @ strength "
\r
1747 errln(prettify(mergedSuffixKeys[i-1]));
\r
1748 errln(prettify(mergedSuffixKeys[i]));
\r
1752 if (strength == Collator.QUATERNARY) {
\r
1753 strength = Collator.IDENTICAL;
\r
1761 public void TestVariableTop()
\r
1763 // parseNextToken is not released as public so i create my own rules
\r
1764 String rules = "& a < b < c < de < fg & hi = j";
\r
1766 RuleBasedCollator coll = new RuleBasedCollator(rules);
\r
1767 String tokens[] = {"a", "b", "c", "de", "fg", "hi", "j", "ab"};
\r
1768 coll.setAlternateHandlingShifted(true);
\r
1769 for (int i = 0; i < tokens.length; i ++) {
\r
1770 int varTopOriginal = coll.getVariableTop();
\r
1772 int varTop = coll.setVariableTop(tokens[i]);
\r
1774 errln("Token " + tokens[i] + " expected to fail");
\r
1776 if (varTop != coll.getVariableTop()) {
\r
1777 errln("Error setting and getting variable top");
\r
1779 CollationKey key1 = coll.getCollationKey(tokens[i]);
\r
1780 for (int j = 0; j < i; j ++) {
\r
1781 CollationKey key2 = coll.getCollationKey(tokens[j]);
\r
1782 if (key2.compareTo(key1) < 0) {
\r
1783 errln("Setting variable top shouldn't change the comparison sequence");
\r
1785 byte sortorder[] = key2.toByteArray();
\r
1786 if (sortorder.length > 0
\r
1787 && (key2.toByteArray())[0] > 1) {
\r
1788 errln("Primary sort order should be 0");
\r
1791 } catch (Exception e) {
\r
1792 CollationElementIterator iter
\r
1793 = coll.getCollationElementIterator(tokens[i]);
\r
1794 /*int ce =*/ iter.next();
\r
1795 int ce2 = iter.next();
\r
1796 if (ce2 == CollationElementIterator.NULLORDER) {
\r
1797 errln("Token " + tokens[i] + " not expected to fail");
\r
1799 if (coll.getVariableTop() != varTopOriginal) {
\r
1800 errln("When exception is thrown variable top should "
\r
1801 + "not be changed");
\r
1804 coll.setVariableTop(varTopOriginal);
\r
1805 if (varTopOriginal != coll.getVariableTop()) {
\r
1806 errln("Couldn't restore old variable top\n");
\r
1810 // Testing calling with error set
\r
1812 coll.setVariableTop("");
\r
1813 errln("Empty string should throw an IllegalArgumentException");
\r
1814 } catch (IllegalArgumentException e) {
\r
1815 logln("PASS: Empty string failed as expected");
\r
1818 coll.setVariableTop(null);
\r
1819 errln("Null string should throw an IllegalArgumentException");
\r
1820 } catch (IllegalArgumentException e) {
\r
1821 logln("PASS: null string failed as expected");
\r
1823 } catch (Exception e) {
\r
1824 warnln("Error creating RuleBasedCollator");
\r
1828 public void TestUCARules()
\r
1831 // only root locale can have empty tailorings .. not English!
\r
1832 RuleBasedCollator coll
\r
1833 = (RuleBasedCollator)Collator.getInstance(new Locale("","",""));
\r
1835 = coll.getRules(false);
\r
1836 if (!rule.equals("")) {
\r
1837 errln("Empty rule string should have empty rules " + rule);
\r
1839 rule = coll.getRules(true);
\r
1840 if (rule.equals("")) {
\r
1841 errln("UCA rule string should not be empty");
\r
1843 coll = new RuleBasedCollator(rule);
\r
1844 } catch (Exception e) {
\r
1845 warnln(e.getMessage());
\r
1852 public void TestShifted()
\r
1854 RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance();
\r
1855 collator.setStrength(Collator.PRIMARY);
\r
1856 collator.setAlternateHandlingShifted(true);
\r
1857 CollationTest.doTest(this, collator, " a", "a", 0); // works properly
\r
1858 CollationTest.doTest(this, collator, "a", "a ", 0); // inconsistent results
\r
1862 * Test for CollationElementIterator previous and next for the whole set of
\r
1863 * unicode characters with normalization on.
\r
1865 public void TestNumericCollation()
\r
1867 String basicTestStrings[] = {"hello1", "hello2", "hello123456"};
\r
1868 String preZeroTestStrings[] = {"avery1",
\r
1872 String thirtyTwoBitNumericStrings[] = {"avery42949672960",
\r
1873 "avery42949672961",
\r
1874 "avery42949672962",
\r
1875 "avery429496729610"};
\r
1877 String supplementaryDigits[] = {"\uD835\uDFCE", // 0
\r
1878 "\uD835\uDFCF", // 1
\r
1879 "\uD835\uDFD0", // 2
\r
1880 "\uD835\uDFD1", // 3
\r
1881 "\uD835\uDFCF\uD835\uDFCE", // 10
\r
1882 "\uD835\uDFCF\uD835\uDFCF", // 11
\r
1883 "\uD835\uDFCF\uD835\uDFD0", // 12
\r
1884 "\uD835\uDFD0\uD835\uDFCE", // 20
\r
1885 "\uD835\uDFD0\uD835\uDFCF", // 21
\r
1886 "\uD835\uDFD0\uD835\uDFD0" // 22
\r
1889 String foreignDigits[] = {"\u0661",
\r
1903 // Open our collator.
\r
1904 RuleBasedCollator coll
\r
1905 = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH);
\r
1906 String att[] = {"NumericCollation"};
\r
1907 Boolean val[] = {Boolean.TRUE};
\r
1908 genericLocaleStarterWithOptions(Locale.ENGLISH, basicTestStrings, att,
\r
1910 genericLocaleStarterWithOptions(Locale.ENGLISH,
\r
1911 thirtyTwoBitNumericStrings, att, val);
\r
1912 genericLocaleStarterWithOptions(Locale.ENGLISH, foreignDigits, att,
\r
1914 genericLocaleStarterWithOptions(Locale.ENGLISH, supplementaryDigits,
\r
1917 // Setting up our collator to do digits.
\r
1918 coll.setNumericCollation(true);
\r
1920 // Testing that prepended zeroes still yield the correct collation
\r
1922 // We expect that every element in our strings array will be equal.
\r
1923 for (int i = 0; i < preZeroTestStrings.length - 1; i ++) {
\r
1924 for (int j = i + 1; j < preZeroTestStrings.length; j ++) {
\r
1925 CollationTest.doTest(this, coll, preZeroTestStrings[i],
\r
1926 preZeroTestStrings[j],0);
\r
1930 //cover setNumericCollationDefault, getNumericCollation
\r
1931 assertTrue("The Numeric Collation setting is on", coll.getNumericCollation());
\r
1932 coll.setNumericCollationDefault();
\r
1933 logln("After set Numeric to default, the setting is: " + coll.getNumericCollation());
\r
1936 public void Test3249()
\r
1938 String rule = "&x < a &z < a";
\r
1940 RuleBasedCollator coll = new RuleBasedCollator(rule);
\r
1942 logln("Collator did not throw an exception");
\r
1944 } catch (Exception e) {
\r
1945 warnln("Error creating RuleBasedCollator with " + rule + " failed");
\r
1949 public void TestTibetanConformance()
\r
1951 String test[] = {"\u0FB2\u0591\u0F71\u0061", "\u0FB2\u0F71\u0061"};
\r
1953 Collator coll = Collator.getInstance();
\r
1954 coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
1955 if (coll.compare(test[0], test[1]) != 0) {
\r
1956 errln("Tibetan comparison error");
\r
1958 CollationTest.doTest(this, (RuleBasedCollator)coll,
\r
1959 test[0], test[1], 0);
\r
1960 } catch (Exception e) {
\r
1961 warnln("Error creating UCA collator");
\r
1965 public void TestJ3347()
\r
1968 Collator coll = Collator.getInstance(Locale.FRENCH);
\r
1969 ((RuleBasedCollator)coll).setAlternateHandlingShifted(true);
\r
1970 if (coll.compare("6", "!6") != 0) {
\r
1971 errln("Jitterbug 3347 failed");
\r
1973 } catch (Exception e) {
\r
1974 warnln("Error creating UCA collator");
\r
1978 public void TestPinyinProblem()
\r
1980 String test[] = { "\u4E56\u4E56\u7761", "\u4E56\u5B69\u5B50" };
\r
1981 genericLocaleStarter(new Locale("zh", "", "PINYIN"), test);
\r
1984 static final long topByte = 0xFF000000L;
\r
1985 static final long bottomByte = 0xFFL;
\r
1986 static final long fourBytes = 0xFFFFFFFFL;
\r
1988 static final int MAX_INPUT = 0x220001; // 2 * Unicode range + 2
\r
1990 private void show(int i, ImplicitCEGenerator imp) {
\r
1991 if (i >= 0 && i <= MAX_INPUT) {
\r
1992 logln(Utility.hex(i) + "\t" + Utility.hex(imp.getImplicitFromRaw(i) & fourBytes));
\r
1996 private void throwError(String title, int cp, ImplicitCEGenerator imp) {
\r
1997 throw new IllegalArgumentException(title + "\t" + Utility.hex(cp, 6) + "\t" + Utility.hex(imp.getImplicitFromRaw(cp) & fourBytes));
\r
2000 private void throwError(String title, long ce) {
\r
2001 errln(title + "\t" + Utility.hex(ce & fourBytes));
\r
2004 public void TestImplicitGeneration()
\r
2008 ImplicitCEGenerator foo = new ImplicitCEGenerator(0xE0, 0xE4);
\r
2010 //int x = foo.getRawImplicit(0xF810);
\r
2011 foo.getRawFromImplicit(0xE20303E7);
\r
2013 //int gap4 = foo.getGap4();
\r
2014 //logln("Gap4: " + gap4);
\r
2015 //int gap3 = foo.getGap3();
\r
2016 //int minTrail = foo.getMinTrail();
\r
2017 //int maxTrail = foo.getMaxTrail();
\r
2020 for (int i = 0; i <= MAX_INPUT; ++i) {
\r
2021 current = foo.getImplicitFromRaw(i) & fourBytes;
\r
2023 // check that it round-trips AND that all intervening ones are illegal
\r
2024 int roundtrip = foo.getRawFromImplicit((int)current);
\r
2025 if (roundtrip != i) {
\r
2026 throwError("No roundtrip", i, foo);
\r
2029 for (long j = last + 1; j < current; ++j) {
\r
2030 roundtrip = foo.getRawFromImplicit((int)j);
\r
2031 // raise an error if it *doesn't* find an error
\r
2032 if (roundtrip != -1) {
\r
2033 throwError("Fails to recognize illegal", j);
\r
2037 // now do other consistency checks
\r
2038 long lastBottom = last & bottomByte;
\r
2039 long currentBottom = current & bottomByte;
\r
2040 long lastTop = last & topByte;
\r
2041 long currentTop = current & topByte;
\r
2043 // do some consistency checks
\r
2045 long gap = current - last;
\r
2046 if (currentBottom != 0) { // if we are a 4-byte
\r
2047 // gap has to be at least gap4
\r
2048 // and gap from minTrail, maxTrail has to be at least gap4
\r
2049 if (gap <= gap4) foo.throwError("Failed gap4 between", i);
\r
2050 if (currentBottom < minTrail + gap4) foo.throwError("Failed gap4 before", i);
\r
2051 if (currentBottom > maxTrail - gap4) foo.throwError("Failed gap4 after", i);
\r
2052 } else { // we are a three-byte
\r
2053 gap = gap >> 8; // move gap down for comparison.
\r
2054 long current3Bottom = (current >> 8) & bottomByte;
\r
2055 if (gap <= gap3) foo.throwError("Failed gap3 between ", i);
\r
2056 if (current3Bottom < minTrail + gap3) foo.throwError("Failed gap3 before", i);
\r
2057 if (current3Bottom > maxTrail - gap3) foo.throwError("Failed gap3 after", i);
\r
2060 // print out some values for spot-checking
\r
2061 if (lastTop != currentTop || i == 0x10000 || i == 0x110000) {
\r
2067 } else if (lastBottom == 0 && currentBottom != 0) {
\r
2068 logln("+ primary boundary, 4-byte CE's below");
\r
2069 } else if (lastTop != currentTop) {
\r
2070 logln("+ primary boundary");
\r
2078 if(foo.getCodePointFromRaw(foo.getRawFromCodePoint(i)) != i) {
\r
2079 errln("No raw <-> code point roundtrip for "+Utility.hex(i));
\r
2082 show(MAX_INPUT-2, foo);
\r
2083 show(MAX_INPUT-1, foo);
\r
2084 show(MAX_INPUT, foo);
\r
2085 } catch (Exception e) {
\r
2086 e.printStackTrace();
\r
2087 warnln(e.getMessage());
\r
2093 /* supercedes TestJ784 */
\r
2094 public void TestBeforePinyin() {
\r
2096 "&[before 2]A << \u0101 <<< \u0100 << \u00E1 <<< \u00C1 << \u01CE <<< \u01CD << \u00E0 <<< \u00C0" +
\r
2097 "&[before 2]e << \u0113 <<< \u0112 << \u00E9 <<< \u00C9 << \u011B <<< \u011A << \u00E8 <<< \u00C8" +
\r
2098 "&[before 2] i << \u012B <<< \u012A << \u00ED <<< \u00CD << \u01D0 <<< \u01CF << \u00EC <<< \u00CC" +
\r
2099 "&[before 2] o << \u014D <<< \u014C << \u00F3 <<< \u00D3 << \u01D2 <<< \u01D1 << \u00F2 <<< \u00D2" +
\r
2100 "&[before 2]u << \u016B <<< \u016A << \u00FA <<< \u00DA << \u01D4 <<< \u01D3 << \u00F9 <<< \u00D9" +
\r
2101 "&U << \u01D6 <<< \u01D5 << \u01D8 <<< \u01D7 << \u01DA <<< \u01D9 << \u01DC <<< \u01DB << \u00FC";
\r
2114 String test2[] = {
\r
2146 /* TODO: port builder fixes to before */
\r
2147 genericRulesStarter(rules, test);
\r
2148 genericLocaleStarter(new Locale("zh","",""), test);
\r
2149 genericRulesStarter(rules, test2);
\r
2150 genericLocaleStarter(new Locale("zh","",""), test2);
\r
2153 public void TestUpperFirstQuaternary()
\r
2155 String tests[] = { "B", "b", "Bb", "bB" };
\r
2156 String[] att = { "strength", "UpperFirst" };
\r
2157 Object attVals[] = { new Integer(Collator.QUATERNARY), Boolean.TRUE };
\r
2158 genericLocaleStarterWithOptions(new Locale("root","",""), tests, att, attVals);
\r
2161 public void TestJ4960()
\r
2163 String tests[] = { "\\u00e2T", "aT" };
\r
2164 String att[] = { "strength", "CaseLevel" };
\r
2165 Object attVals[] = { new Integer(Collator.PRIMARY), Boolean.TRUE };
\r
2166 String tests2[] = { "a", "A" };
\r
2167 String rule = "&[first tertiary ignorable]=A=a";
\r
2168 String att2[] = { "CaseLevel" };
\r
2169 Object attVals2[] = { Boolean.TRUE };
\r
2170 // Test whether we correctly ignore primary ignorables on case level when
\r
2171 // we have only primary & case level
\r
2172 genericLocaleStarterWithOptionsAndResult(new Locale("root", ""), tests, att, attVals, 0);
\r
2173 // Test whether ICU4J will make case level for sortkeys that have primary strength
\r
2175 genericLocaleStarterWithOptions(new Locale("root", ""), tests2, att, attVals);
\r
2176 // Test whether completely ignorable letters have case level info (they shouldn't)
\r
2177 genericRulesStarterWithOptionsAndResult(rule, tests2, att2, attVals2, 0);
\r
2180 public void TestJB5298(){
\r
2181 ULocale[] locales = Collator.getAvailableULocales();
\r
2182 logln("Number of collator locales returned : " + locales.length);
\r
2183 // double-check keywords
\r
2184 String[] keywords = Collator.getKeywords();
\r
2185 if (keywords.length != 1 || !keywords[0].equals("collation")) {
\r
2186 throw new IllegalArgumentException("internal collation error");
\r
2189 String[] values = Collator.getKeywordValues("collation");
\r
2190 log("Collator.getKeywordValues returned: ");
\r
2191 for(int i=0; i<values.length;i++){
\r
2192 log(values[i]+", ");
\r
2195 logln("Number of collator values returned : " + values.length);
\r
2197 Set foundValues = new TreeSet(Arrays.asList(values));
\r
2199 for (int i = 0; i < locales.length; ++i) {
\r
2200 for (int j = 0; j < values.length; ++j) {
\r
2201 ULocale tryLocale = values[j].equals("standard")
\r
2202 ? locales[i] : new ULocale(locales[i] + "@collation=" + values[j]);
\r
2203 // only append if not standard
\r
2204 ULocale canon = Collator.getFunctionalEquivalent("collation",tryLocale);
\r
2205 if (!canon.equals(tryLocale)) {
\r
2206 continue; // has a different
\r
2207 }else {// functional equivalent, so skip
\r
2208 logln(tryLocale + " : "+canon+", ");
\r
2210 String can = canon.toString();
\r
2211 int idx = can.indexOf("@collation=");
\r
2212 String val = idx >= 0 ? can.substring(idx+11, can.length()) : "";
\r
2213 if(val.length()>0 && !foundValues.contains(val)){
\r
2214 errln("Unknown collation found "+ can);
\r
2224 String[] test = { "a", "y" };
\r
2225 String rules = "&Ny << Y &[first secondary ignorable] <<< a";
\r
2226 genericRulesStarter(rules, test);
\r
2233 String rules[] = {
\r
2234 "&a < \u00e2 <<< \u00c2",
\r
2235 "&a < \u1FF3 ", // OMEGA WITH YPOGEGRAMMENI
\r
2236 "&s < \u0161 ", // &s < s with caron
\r
2237 "&z < a\u00EA", // &z < a+e with circumflex
\r
2239 String cases[][] = {
\r
2240 { "\u1EAC", "A\u0323\u0302", "\u1EA0\u0302", "\u00C2\u0323", },
\r
2241 { "\u1FA2", "\u03C9\u0313\u0300\u0345", "\u1FF3\u0313\u0300",
\r
2242 "\u1F60\u0300\u0345", "\u1f62\u0345", "\u1FA0\u0300", },
\r
2243 { "\u1E63\u030C", "s\u0323\u030C", "s\u030C\u0323"},
\r
2244 { "a\u1EC7", // a+ e with dot below and circumflex
\r
2245 "a\u1EB9\u0302", // a + e with dot below + combining circumflex
\r
2246 "a\u00EA\u0323", // a + e with circumflex + combining dot below
\r
2251 for(int i = 0; i < rules.length; i++) {
\r
2253 RuleBasedCollator coll = null;
\r
2255 coll = new RuleBasedCollator(rules[i]);
\r
2256 } catch (Exception e) {
\r
2257 warnln("Unable to open collator with rules " + rules[i]);
\r
2260 logln("Test case["+i+"]:");
\r
2261 CollationKey expectingKey = coll.getCollationKey(cases[i][0]);
\r
2262 for (int j=1; j<cases[i].length; j++) {
\r
2263 CollationKey key = coll.getCollationKey(cases[i][j]);
\r
2264 if ( key.compareTo(expectingKey)!=0) {
\r
2265 errln("Error! Test case["+i+"]:"+"source:" + key.getSourceString());
\r
2266 errln("expecting:"+prettify(expectingKey)+ "got:"+ prettify(key));
\r
2268 logln(" Key:"+ prettify(key));
\r
2273 RuleBasedCollator vi_vi = null;
\r
2275 vi_vi = (RuleBasedCollator)Collator.getInstance(
\r
2276 new Locale("vi", ""));
\r
2277 logln("VI sort:");
\r
2278 CollationKey expectingKey = vi_vi.getCollationKey(cases[0][0]);
\r
2279 for (int j=1; j<cases[0].length; j++) {
\r
2280 CollationKey key = vi_vi.getCollationKey(cases[0][j]);
\r
2281 if ( key.compareTo(expectingKey)!=0) {
\r
2282 // TODO (claireho): change the logln to errln after vi.res is up-to-date.
\r
2283 // errln("source:" + key.getSourceString());
\r
2284 // errln("expecting:"+prettify(expectingKey)+ "got:"+ prettify(key));
\r
2285 logln("Error!! in Vietnese sort - source:" + key.getSourceString());
\r
2286 logln("expecting:"+prettify(expectingKey)+ "got:"+ prettify(key));
\r
2288 // logln("source:" + key.getSourceString());
\r
2289 logln(" Key:"+ prettify(key));
\r
2291 } catch (Exception e) {
\r
2292 warnln("Error creating Vietnese collator");
\r
2299 public void Test6179()
\r
2301 String rules[] = {
\r
2302 "&[last primary ignorable]<< a &[first primary ignorable]<<b ",
\r
2303 "&[last secondary ignorable]<<< a &[first secondary ignorable]<<<b",
\r
2305 // defined in UCA5.1
\r
2306 String firstPrimIgn = "\u0332";
\r
2307 String lastPrimIgn = "\uD800\uDDFD";
\r
2308 String firstVariable = "\u0009";
\r
2309 byte[] secIgnKey = {1,1,4,0};
\r
2314 RuleBasedCollator coll = null;
\r
2316 coll = new RuleBasedCollator(rules[i]);
\r
2317 } catch (Exception e) {
\r
2318 warnln("Unable to open collator with rules " + rules[i]);
\r
2321 logln("Test rule["+i+"]"+rules[i]);
\r
2323 CollationKey keyA = coll.getCollationKey("a");
\r
2324 logln("Key for \"a\":"+ prettify(keyA));
\r
2325 if (keyA.compareTo(coll.getCollationKey(lastPrimIgn))<=0) {
\r
2326 CollationKey key = coll.getCollationKey(lastPrimIgn);
\r
2327 logln("Collation key for 0xD800 0xDDFD: "+prettify(key));
\r
2328 errln("Error! String \"a\" must be greater than \uD800\uDDFD -"+
\r
2329 "[Last Primary Ignorable]");
\r
2331 if (keyA.compareTo(coll.getCollationKey(firstVariable))>=0) {
\r
2332 CollationKey key = coll.getCollationKey(firstVariable);
\r
2333 logln("Collation key for 0x0009: "+prettify(key));
\r
2334 errln("Error! String \"a\" must be less than 0x0009 - [First Variable]");
\r
2336 CollationKey keyB = coll.getCollationKey("b");
\r
2337 logln("Key for \"b\":"+ prettify(keyB));
\r
2338 if (keyB.compareTo(coll.getCollationKey(firstPrimIgn))<=0) {
\r
2339 CollationKey key = coll.getCollationKey(firstPrimIgn);
\r
2340 logln("Collation key for 0x0332: "+prettify(key));
\r
2341 errln("Error! String \"b\" must be greater than 0x0332 -"+
\r
2342 "[First Primary Ignorable]");
\r
2344 if (keyB.compareTo(coll.getCollationKey(firstVariable))>=0) {
\r
2345 CollationKey key = coll.getCollationKey(firstVariable);
\r
2346 logln("Collation key for 0x0009: "+prettify(key));
\r
2347 errln("Error! String \"b\" must be less than 0x0009 - [First Variable]");
\r
2352 RuleBasedCollator coll = null;
\r
2354 coll = new RuleBasedCollator(rules[i]);
\r
2355 } catch (Exception e) {
\r
2356 warnln("Unable to open collator with rules " + rules[i]);
\r
2359 logln("Test rule["+i+"]"+rules[i]);
\r
2361 CollationKey keyA = coll.getCollationKey("a");
\r
2362 logln("Key for \"a\":"+ prettify(keyA));
\r
2363 byte[] keyAInBytes = keyA.toByteArray();
\r
2364 for (int j=0; j<keyAInBytes.length && j<secIgnKey.length; j++) {
\r
2365 if (keyAInBytes[j]!=secIgnKey[j]) {
\r
2366 if ((char)keyAInBytes[j]<=(char)secIgnKey[j]) {
\r
2367 logln("Error! String \"a\" must be greater than [Last Secondary Ignorable]");
\r
2372 if (keyA.compareTo(coll.getCollationKey(firstVariable))>=0) {
\r
2373 errln("Error! String \"a\" must be less than 0x0009 - [First Variable]");
\r
2374 CollationKey key = coll.getCollationKey(firstVariable);
\r
2375 logln("Collation key for 0x0009: "+prettify(key));
\r
2377 CollationKey keyB = coll.getCollationKey("b");
\r
2378 logln("Key for \"b\":"+ prettify(keyB));
\r
2379 byte[] keyBInBytes = keyB.toByteArray();
\r
2380 for (int j=0; j<keyBInBytes.length && j<secIgnKey.length; j++) {
\r
2381 if (keyBInBytes[j]!=secIgnKey[j]) {
\r
2382 if ((char)keyBInBytes[j]<=(char)secIgnKey[j]) {
\r
2383 errln("Error! String \"b\" must be greater than [Last Secondary Ignorable]");
\r
2388 if (keyB.compareTo(coll.getCollationKey(firstVariable))>=0) {
\r
2389 CollationKey key = coll.getCollationKey(firstVariable);
\r
2390 logln("Collation key for 0x0009: "+prettify(key));
\r
2391 errln("Error! String \"b\" must be less than 0x0009 - [First Variable]");
\r
2396 public void TestUCAPrecontext()
\r
2398 String rules[] = {
\r
2400 "& L\u00B7 << a", // 'a' is an expansion.
\r
2402 String cases[] = {
\r
2416 RuleBasedCollator en = null;
\r
2418 logln("EN sort:");
\r
2420 en = (RuleBasedCollator)Collator.getInstance(
\r
2421 new Locale("en", ""));
\r
2422 for (int j=0; j<cases.length; j++) {
\r
2423 CollationKey key = en.getCollationKey(cases[j]);
\r
2425 CollationKey prevKey = en.getCollationKey(cases[j-1]);
\r
2426 if (key.compareTo(prevKey)<0) {
\r
2427 errln("Error! EN test["+j+"]:"+"source:" + cases[j]+
\r
2428 "is not greater than previous test.");
\r
2432 if ( key.compareTo(expectingKey)!=0) {
\r
2433 errln("Error! Test case["+i+"]:"+"source:" + key.getSourceString());
\r
2434 errln("expecting:"+prettify(expectingKey)+ "got:"+ prettify(key));
\r
2437 logln("String:"+cases[j]+" Key:"+ prettify(key));
\r
2439 } catch (Exception e) {
\r
2440 warnln("Error creating Vietnese collator");
\r
2445 RuleBasedCollator ja = null;
\r
2446 logln("JA sort:");
\r
2448 ja = (RuleBasedCollator)Collator.getInstance(
\r
2449 new Locale("ja", ""));
\r
2450 for (int j=0; j<cases.length; j++) {
\r
2451 CollationKey key = ja.getCollationKey(cases[j]);
\r
2453 CollationKey prevKey = ja.getCollationKey(cases[j-1]);
\r
2454 if (key.compareTo(prevKey)<0) {
\r
2455 errln("Error! JA test["+j+"]:"+"source:" + cases[j]+
\r
2456 "is not greater than previous test.");
\r
2459 logln("String:"+cases[j]+" Key:"+ prettify(key));
\r
2461 } catch (Exception e) {
\r
2462 warnln("Error creating Vietnese collator");
\r
2465 for(int i = 0; i < rules.length; i++) {
\r
2467 RuleBasedCollator coll = null;
\r
2468 logln("Tailoring rule:"+rules[i]);
\r
2470 coll = new RuleBasedCollator(rules[i]);
\r
2471 } catch (Exception e) {
\r
2472 warnln("Unable to open collator with rules " + rules[i]);
\r
2475 for (int j=0; j<cases.length; j++) {
\r
2476 CollationKey key = coll.getCollationKey(cases[j]);
\r
2478 CollationKey prevKey = coll.getCollationKey(cases[j-1]);
\r
2479 if (i==1 && j==3) {
\r
2480 if (key.compareTo(prevKey)>0) {
\r
2481 errln("Error! Rule:"+rules[i]+" test["+j+"]:"+"source:"+
\r
2482 cases[j]+"is not greater than previous test.");
\r
2486 if (key.compareTo(prevKey)<0) {
\r
2487 errln("Error! Rule:"+rules[i]+" test["+j+"]:"+"source:"+
\r
2488 cases[j]+"is not greater than previous test.");
\r
2492 logln("String:"+cases[j]+" Key:"+ prettify(key));
\r
2497 public void TestSameStrengthList() {
\r
2498 String[] testSourceCases = {
\r
2502 "\u0061\u0061\u0061",
\r
2506 String[] testTargetCases = {
\r
2510 "\u0031\u0032\u0033",
\r
2522 Collator myCollation;
\r
2523 String rules = "&a<*bcd &b<<*klm &k<<<*xyz &a=*123";
\r
2525 myCollation = new RuleBasedCollator(rules);
\r
2526 } catch (Exception e) {
\r
2527 warnln("ERROR: in creation of rule based collator");
\r
2530 // logln("Testing some A letters, for some reason");
\r
2531 myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
2532 myCollation.setStrength(Collator.TERTIARY);
\r
2533 for (int i = 0; i < 5 ; i++)
\r
2535 CollationTest.doTest(this, (RuleBasedCollator)myCollation,
\r
2536 testSourceCases[i], testTargetCases[i],
\r
2542 * Tests the method public boolean equals(Object target) in CollationKey
\r
2544 public void TestCollationKeyEquals() {
\r
2545 CollationKey ck = new CollationKey("", (byte[]) null);
\r
2547 // Tests when "if (!(target instanceof CollationKey))" is true
\r
2548 if (ck.equals(new Object())) {
\r
2549 errln("CollationKey.equals() was not suppose to return false "
\r
2550 + "since it is comparing to a non Collation Key object.");
\r
2552 if (ck.equals("")) {
\r
2553 errln("CollationKey.equals() was not suppose to return false "
\r
2554 + "since it is comparing to a non Collation Key object.");
\r
2556 if (ck.equals(0)) {
\r
2557 errln("CollationKey.equals() was not suppose to return false "
\r
2558 + "since it is comparing to a non Collation Key object.");
\r
2560 if (ck.equals(0.0)) {
\r
2561 errln("CollationKey.equals() was not suppose to return false "
\r
2562 + "since it is comparing to a non Collation Key object.");
\r
2565 // Tests when "if (target == null)" is true
\r
2566 if (ck.equals((CollationKey) null)) {
\r
2567 errln("CollationKey.equals() was not suppose to return false "
\r
2568 + "since it is comparing to a null Collation Key object.");
\r
2573 * Tests the method public int hashCode() in CollationKey
\r
2575 public void TestCollationKeyHashCode() {
\r
2576 CollationKey ck = new CollationKey("", (byte[]) null);
\r
2578 // Tests when "if (m_key_ == null)" is true
\r
2579 if (ck.hashCode() != 1) {
\r
2580 errln("CollationKey.hashCode() was suppose to return 1 "
\r
2581 + "when m_key is null due a null parameter in the " + "constructor.");
\r
2586 * Tests the method public CollationKey getBound(int boundType, int noOfLevels)
\r
2588 public void TestGetBound() {
\r
2589 CollationKey ck = new CollationKey("", (byte[]) null);
\r
2591 // Tests when "if (noOfLevels > Collator.PRIMARY)" is false
\r
2592 // Tests when "default: " is true for "switch (boundType)"
\r
2594 ck.getBound(BoundMode.COUNT, -1);
\r
2595 errln("CollationKey.getBound(int,int) was suppose to return an "
\r
2596 + "exception for an invalid boundType value.");
\r
2597 } catch (Exception e) {
\r
2600 // Tests when "if (noOfLevels > 0)"
\r
2602 CollationKey ck1 = new CollationKey("", b);
\r
2604 ck1.getBound(0, 1);
\r
2605 errln("CollationKey.getBound(int,int) was suppose to return an "
\r
2606 + "exception a value of noOfLevels that exceeds expected.");
\r
2607 } catch (Exception e) {
\r
2612 * Tests the method public CollationKey merge(CollationKey source)
\r
2614 public void TestMerge() {
\r
2616 CollationKey ck = new CollationKey("", b);
\r
2618 // Tests when "if (source == null || source.getLength() == 0)" is true
\r
2621 errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");
\r
2622 } catch (Exception e) {
\r
2626 errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");
\r
2627 } catch (Exception e) {
\r
2631 /* Test the method public int compareTo(RawCollationKey rhs) */
\r
2632 public void TestRawCollationKeyCompareTo(){
\r
2633 RawCollationKey rck = new RawCollationKey();
\r
2634 byte[] b = {(byte) 10, (byte) 20};
\r
2635 RawCollationKey rck100 = new RawCollationKey(b, 2);
\r
2637 if(rck.compareTo(rck) != 0){
\r
2638 errln("RawCollatonKey.compareTo(RawCollationKey) was suppose to return 0 " +
\r
2639 "for two idential RawCollationKey objects.");
\r
2642 if(rck.compareTo(rck100) == 0){
\r
2643 errln("RawCollatonKey.compareTo(RawCollationKey) was not suppose to return 0 " +
\r
2644 "for two different RawCollationKey objects.");
\r