]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationMiscTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationMiscTest.java
1  /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 /** \r
9  * Port From:   ICU4C v2.1 : cintltest\r
10  * Source File: $ICU4CRoot/source/test/cintltest/cmsccoll.c\r
11  */\r
12  \r
13 package com.ibm.icu.dev.test.collator;\r
14 \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
19 \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
34 \r
35 public class CollationMiscTest extends TestFmwk {\r
36 \r
37     public static void main(String[] args) throws Exception {\r
38         new CollationMiscTest().run(args);\r
39         // new CollationMiscTest().TestLocaleRuleBasedCollators(); \r
40     }\r
41     \r
42     //private static final int NORM_BUFFER_TEST_LEN_ = 32;\r
43     private static final class Tester \r
44     {\r
45         int u;\r
46         String NFC;\r
47         String NFD;\r
48     }\r
49     \r
50     private static final boolean hasCollationElements(Locale locale)\r
51     {\r
52         ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,locale);\r
53         if (rb != null) {\r
54             try {\r
55                 String collkey = rb.getStringWithFallback("collations/default"); \r
56                 ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);\r
57                 if (elements != null) {\r
58                     return true;\r
59                 }\r
60             } catch (Exception e) {\r
61             }\r
62         }\r
63         return false;\r
64     }\r
65     \r
66     public void TestComposeDecompose() \r
67     {\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
72         try {\r
73             coll = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH);\r
74         } \r
75         catch (Exception e) {\r
76             warnln("Error opening collator\n");\r
77             return;\r
78         }\r
79     \r
80         int noCases = 0;\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
86     \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
91                 t[noCases].u = u;\r
92                 if (len != t[noCases].NFD.length() \r
93                     || (comp.compareTo(t[noCases].NFD) != 0)) {\r
94                     t[noCases].NFC = comp;\r
95                 }\r
96                 noCases ++;\r
97                 t[noCases] = new Tester();\r
98             } \r
99         }\r
100     \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
106             }\r
107         }\r
108     \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
116      \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
125                                              0);\r
126                     }\r
127                 }\r
128             }\r
129         }\r
130     }\r
131     \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
136         String[] rules = {\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
148             // ignorable \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
157         };\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
178         };\r
179         \r
180         for (int i = 0; i< rules.length; i++) {\r
181             genericRulesStarter(rules[i], data[i]);\r
182         }\r
183     }\r
184     \r
185     void genericRulesStarter(String rules, String[] s) {\r
186         genericRulesStarterWithResult(rules, s, -1);\r
187     }\r
188     \r
189     void genericRulesStarterWithResult(String rules, String[] s, int result) {\r
190         \r
191         RuleBasedCollator coll = null;\r
192         try {\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
198         }\r
199     }\r
200     \r
201     void genericRulesStarterWithOptionsAndResult(String rules, String[] s, String[] atts, Object[] attVals, int result) {\r
202         RuleBasedCollator coll = null;\r
203         try {\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
209         }\r
210     }\r
211     void genericOrderingTestWithResult(Collator coll, String[] s, int result) {\r
212         String t1 = "";\r
213         String t2 = "";\r
214         \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
221                                      result);\r
222             }\r
223         }\r
224     }\r
225 \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
230             return;\r
231         }\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
236             return;    \r
237         } else {\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
246             if (ok1) {\r
247                 // logln(msg1 + source + msg2 + target + msg3 + sResult);\r
248             } else {\r
249                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
250             }\r
251             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
252             msg2 = "\").compareTo(key(\"";\r
253             msg3 = "\")) returned ";\r
254             sResult = appendCompareResult(keyResult, sResult);\r
255             if (ok2) {\r
256                 // logln(msg1 + source + msg2 + target + msg3 + sResult);\r
257             } else {\r
258                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
259                 msg1 = "  ";\r
260                 msg2 = " vs. ";\r
261                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
262             }\r
263             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
264             msg2 = "\", \"";\r
265             msg3 = "\") returned ";\r
266             sResult = appendCompareResult(incResult, sResult);\r
267             if (ok3) {\r
268                 // logln(msg1 + source + msg2 + target + msg3 + sResult);\r
269             } else {\r
270                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
271             }                \r
272         }\r
273     }\r
274     \r
275     String appendCompareResult(int result, String target) {\r
276         if (result == -1) {  //LESS\r
277             target += "LESS";\r
278         } else if (result == 0) {  //EQUAL\r
279             target += "EQUAL";\r
280         } else if (result == 1) {  //GREATER\r
281             target += "GREATER";\r
282         } else {\r
283             String huh = "?";\r
284             target += huh + result;\r
285         }\r
286         return target;\r
287     }\r
288     \r
289     String prettify(CollationKey sourceKey) {\r
290         int i;\r
291         byte[] bytes= sourceKey.toByteArray();\r
292         String target = "[";\r
293     \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
298             }\r
299             else {\r
300                 target += numStr;\r
301             }\r
302             target += " ";\r
303         }\r
304         target += "]";\r
305         return target;\r
306     }\r
307     \r
308     public void TestBeforePrefixFailure() {\r
309         String[] rules = {\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
313         };\r
314         String[][] data = {\r
315             {"x", "\uff41"},\r
316             {"\u30a9", "\u30a7"},\r
317             {"\u30a9", "\u30a7"},\r
318         };\r
319         \r
320         for(int i = 0; i< rules.length; i++) {\r
321             genericRulesStarter(rules[i], data[i]);\r
322         }\r
323     }\r
324     \r
325     public void TestContractionClosure() {\r
326         String[] rules = {\r
327             "&b=\u00e4\u00e4",\r
328             "&b=\u00C5",\r
329         };\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
333         };\r
334         \r
335         for(int i = 0; i< rules.length; i++) {\r
336             genericRulesStarterWithResult(rules[i], data[i], 0);\r
337         }\r
338     }\r
339     \r
340     public void TestPrefixCompose() {\r
341         String rule1 = "&\u30a7<<<\u30ab|\u30fc=\u30ac|\u30fc";\r
342         \r
343         String string = rule1;\r
344         try {\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
349         }\r
350     }\r
351     \r
352     public void TestStrCollIdenticalPrefix() {\r
353         String rule = "&\ud9b0\udc70=\ud9b0\udc71";\r
354         String test[] = {\r
355             "ab\ud9b0\udc70",\r
356             "ab\ud9b0\udc71"\r
357         };\r
358         genericRulesStarterWithResult(rule, test, 0);\r
359     }\r
360     \r
361     public void TestPrefix() {\r
362         String[] rules = {\r
363             "&z <<< z|a",\r
364             "&z <<< z|   a", \r
365             "[strength I]&a=\ud900\udc25&z<<<\ud900\udc25|a", \r
366         };\r
367         String[][] data = {\r
368             {"zz", "za"},\r
369             {"zz", "za"},\r
370             {"aa", "az", "\ud900\udc25z", "\ud900\udc25a", "zz"},\r
371         };\r
372         \r
373         for(int i = 0; i<rules.length; i++) {\r
374             genericRulesStarter(rules[i], data[i]);\r
375         }\r
376     }\r
377     \r
378     public void TestNewJapanese() {\r
379         \r
380         String test1[] = {\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
406             "\u3072\u3086",\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
444             "\u30d5\u30fd",\r
445             "\u3075\u309e",\r
446             "\u3076\u309d",\r
447             "\u3076\u3075",\r
448             "\u3076\u30d5",\r
449             "\u30d6\u3075",\r
450             "\u30d6\u30d5",\r
451             "\u3076\u309e",\r
452             "\u3076\u3077",\r
453             "\u30d6\u3077",\r
454             "\u3077\u309d",\r
455             "\u30d7\u30fd",\r
456             "\u3077\u3075",\r
457         };\r
458         \r
459         String test2[] = {\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
492         };\r
493         \r
494         String[] att = { "strength", };\r
495         Object[] val = { new Integer(Collator.QUATERNARY), };\r
496         \r
497         String[] attShifted = { "strength", "AlternateHandling"};\r
498         Object valShifted[] = { new Integer(Collator.QUATERNARY), \r
499                                 Boolean.TRUE };\r
500        \r
501         genericLocaleStarterWithOptions(Locale.JAPANESE, test1, att, val);\r
502         genericLocaleStarterWithOptions(Locale.JAPANESE, test2, att, val);\r
503         \r
504         genericLocaleStarterWithOptions(Locale.JAPANESE, test1, attShifted,\r
505                                         valShifted);\r
506         genericLocaleStarterWithOptions(Locale.JAPANESE, test2, attShifted,\r
507                                         valShifted);\r
508     }\r
509     \r
510     void genericLocaleStarter(Locale locale, String s[]) {\r
511         RuleBasedCollator coll = null;\r
512         try {\r
513             coll = (RuleBasedCollator)Collator.getInstance(locale);\r
514             \r
515         } catch (Exception e) {\r
516             warnln("Unable to open collator for locale " + locale);\r
517             return;\r
518         }\r
519         // logln("Locale starter for " + locale);\r
520         genericOrderingTest(coll, s);\r
521     }\r
522     \r
523     void genericLocaleStarterWithOptions(Locale locale, String[] s, String[] attrs, Object[] values) {\r
524         genericLocaleStarterWithOptionsAndResult(locale, s, attrs, values, -1);\r
525     }\r
526     \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
531             } \r
532             else if (attrs[i].equals("decomp")) {\r
533                 coll.setDecomposition(((Integer)values[i]).intValue());\r
534             } \r
535             else if (attrs[i].equals("AlternateHandling")) {\r
536                 coll.setAlternateHandlingShifted(((Boolean)values[i]\r
537                                                   ).booleanValue());\r
538             }\r
539             else if (attrs[i].equals("NumericCollation")) {\r
540                 coll.setNumericCollation(((Boolean)values[i]).booleanValue());\r
541             }\r
542             else if (attrs[i].equals("UpperFirst")) {\r
543                 coll.setUpperCaseFirst(((Boolean)values[i]).booleanValue());\r
544             }\r
545             else if (attrs[i].equals("LowerFirst")) {\r
546                 coll.setLowerCaseFirst(((Boolean)values[i]).booleanValue());\r
547             }\r
548             else if (attrs[i].equals("CaseLevel")) {\r
549                 coll.setCaseLevel(((Boolean)values[i]).booleanValue());\r
550             }\r
551         }        \r
552     }\r
553     \r
554     void genericLocaleStarterWithOptionsAndResult(Locale locale, String[] s, String[] attrs, Object[] values, int result) {\r
555         RuleBasedCollator coll = null;\r
556         try {\r
557             coll = (RuleBasedCollator)Collator.getInstance(locale);\r
558         } catch (Exception e) {\r
559             warnln("Unable to open collator for locale " + locale);\r
560             return;\r
561         }\r
562         // logln("Locale starter for " +locale);\r
563         \r
564         // logln("Setting attributes");\r
565         genericOptionsSetter(coll, attrs, values);\r
566         \r
567         genericOrderingTestWithResult(coll, s, result);\r
568     }\r
569     \r
570     void genericOrderingTest(Collator coll, String[] s) {\r
571         genericOrderingTestWithResult(coll, s, -1);\r
572     }\r
573     \r
574     public void TestNonChars() {\r
575         String test[] = {\r
576             "\u0000",\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
594         };\r
595         Collator coll = null;\r
596         try {\r
597             coll = Collator.getInstance(new Locale("en", "US"));\r
598         } catch (Exception e) {\r
599             warnln("Unable to open collator");\r
600             return;\r
601         }\r
602         // logln("Test non characters");\r
603         \r
604         genericOrderingTestWithResult(coll, test, 0);\r
605     }\r
606     \r
607     public void TestExtremeCompression() {\r
608         String[] test = new String[4];\r
609         \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
613                 temp.append('a');\r
614             }\r
615             temp.append((char)('a' + i));\r
616             test[i] = temp.toString();\r
617         }\r
618         \r
619         genericLocaleStarter(new Locale("en", "US"), test);\r
620     }\r
621 \r
622     /**\r
623      * Tests surrogate support.\r
624      */\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
630 \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
634             + "< c < b";\r
635         genericRulesStarter(rule, test);\r
636     }\r
637     \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
644     }\r
645     \r
646     public void TestCyrillicTailoring() {\r
647         String test[] = {\r
648             "\u0410b",\r
649             "\u0410\u0306a",\r
650             "\u04d0A"\r
651         };\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
659     }\r
660 \r
661     public void TestSuppressContractions() {\r
662         String testNoCont2[] = {\r
663             "\u0410\u0302a",\r
664             "\u0410\u0306b",\r
665             "\u0410c"            \r
666         };\r
667         String testNoCont[] = {\r
668             "a\u0410",            \r
669             "A\u0410\u0306",\r
670             "\uFF21\u0410\u0302"\r
671         };\r
672             \r
673         genericRulesStarter("[suppressContractions [\u0400-\u047f]]", testNoCont);\r
674         genericRulesStarter("[suppressContractions [\u0400-\u047f]]", testNoCont2);\r
675     }\r
676         \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
681         };\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
687     \r
688         };\r
689         boolean[][] caseTestAttributes = {\r
690             { false, false},\r
691             { true, false},\r
692             { false, true},\r
693             { true, true}\r
694         };\r
695         \r
696         int i,j,k;\r
697         Collator  myCollation;\r
698         try {\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
702             return;\r
703         }\r
704         // logln("Testing different case settings");\r
705         myCollation.setStrength(Collator.TERTIARY);\r
706     \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
711             }\r
712             else {\r
713                 // upper case first\r
714                 ((RuleBasedCollator)myCollation).setLowerCaseFirst(true);   \r
715             }\r
716             ((RuleBasedCollator)myCollation).setCaseLevel(\r
717                                                           caseTestAttributes[k][1]);\r
718           \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
726                 }\r
727             }\r
728         }\r
729         try {\r
730             myCollation = new RuleBasedCollator(gRules);\r
731         } catch (Exception e) {\r
732             warnln("ERROR: in creation of rule based collator");\r
733             return;\r
734         }\r
735         // logln("Testing different case settings with custom rules");\r
736         myCollation.setStrength(Collator.TERTIARY);\r
737     \r
738         for(k = 0; k<4; k++) {\r
739             if (caseTestAttributes[k][0] == true) {\r
740                 ((RuleBasedCollator)myCollation).setUpperCaseFirst(true);\r
741             }\r
742             else {\r
743                 ((RuleBasedCollator)myCollation).setUpperCaseFirst(false);\r
744             }\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
753                 }\r
754             }\r
755         }\r
756 \r
757         {\r
758             String[] lowerFirst = {\r
759                 "h",\r
760                 "H",\r
761                 "ch",\r
762                 "Ch",\r
763                 "CH",\r
764                 "cha",\r
765                 "chA",\r
766                 "Cha",\r
767                 "ChA",\r
768                 "CHa",\r
769                 "CHA",\r
770                 "i",\r
771                 "I"\r
772             };\r
773     \r
774             String[] upperFirst = {\r
775                 "H",\r
776                 "h",\r
777                 "CH",\r
778                 "Ch",\r
779                 "ch",\r
780                 "CHA",\r
781                 "CHa",\r
782                 "ChA",\r
783                 "Cha",\r
784                 "chA",\r
785                 "cha",\r
786                 "I",\r
787                 "i"\r
788             };\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
798         }\r
799     }\r
800 \r
801     public void TestIncompleteCnt() {\r
802         String[] cnt1 = {\r
803             "AA",\r
804             "AC",\r
805             "AZ",\r
806             "AQ",\r
807             "AB",\r
808             "ABZ",\r
809             "ABQ",\r
810             "Z",\r
811             "ABC",\r
812             "Q",\r
813             "B"\r
814         };\r
815             \r
816         String[] cnt2 = {\r
817             "DA",\r
818             "DAD",\r
819             "DAZ",\r
820             "MAR",\r
821             "Z",\r
822             "DAVIS",\r
823             "MARK",\r
824             "DAV",\r
825             "DAVI"\r
826         };\r
827         RuleBasedCollator coll =  null;\r
828         String temp = " & Z < ABC < Q < B";\r
829         try {\r
830             coll = new RuleBasedCollator(temp);\r
831         } catch (Exception e) {\r
832             warnln("fail to create RuleBasedCollator");\r
833             return;\r
834         }\r
835         \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
842             }\r
843         }\r
844         \r
845         temp = " & Z < DAVIS < MARK <DAV";\r
846         try {\r
847             coll = new RuleBasedCollator(temp);\r
848         } catch (Exception e) {\r
849             warnln("fail to create RuleBasedCollator");\r
850             return;\r
851         }\r
852         \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
859             }\r
860         }\r
861     }\r
862         \r
863     public void TestBlackBird() {\r
864         String[] shifted = {\r
865             "black bird",\r
866             "black-bird",\r
867             "blackbird",\r
868             "black Bird",\r
869             "black-Bird",\r
870             "blackBird",\r
871             "black birds",\r
872             "black-birds",\r
873             "blackbirds"\r
874         };\r
875         int[] shiftedTert = {\r
876             0,\r
877             0,\r
878             0,\r
879             -1,\r
880             0,\r
881             0,\r
882             -1,\r
883             0,\r
884             0\r
885         };\r
886         String[] nonignorable = {\r
887             "black bird",\r
888             "black Bird",\r
889             "black birds",\r
890             "black-bird",\r
891             "black-Bird",\r
892             "black-birds",\r
893             "blackbird",\r
894             "blackBird",\r
895             "blackbirds"\r
896         };\r
897         int i = 0, j = 0;\r
898         int size = 0;\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
909             }\r
910         }\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
919             }\r
920         }\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
927                                  shiftedTert[i]);\r
928         }\r
929     }\r
930     \r
931     public void TestFunkyA() {\r
932         String[] testSourceCases = {\r
933             "\u0041\u0300\u0301",\r
934             "\u0041\u0300\u0316",\r
935             "\u0041\u0300",\r
936             "\u00C0\u0301",\r
937             // this would work with forced normalization \r
938             "\u00C0\u0316",\r
939         };\r
940         \r
941         String[] testTargetCases = {\r
942             "\u0041\u0301\u0300",\r
943             "\u0041\u0316\u0300",\r
944             "\u00C0",\r
945             "\u0041\u0301\u0300",\r
946             // this would work with forced normalization \r
947             "\u0041\u0316\u0300",\r
948         };\r
949         \r
950         int[] results = {\r
951             1,\r
952             0,\r
953             0,\r
954             1,\r
955             0\r
956         };\r
957 \r
958         Collator  myCollation;\r
959         try {\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
963             return;\r
964         }\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
969             {\r
970                 CollationTest.doTest(this, (RuleBasedCollator)myCollation, \r
971                                      testSourceCases[i], testTargetCases[i], \r
972                                      results[i]);\r
973             }\r
974     }\r
975     \r
976     public void TestChMove() {\r
977         String[] chTest = {\r
978             "c",\r
979             "C",\r
980             "ca", "cb", "cx", "cy", "CZ",\r
981             "c\u030C", "C\u030C",\r
982             "h",\r
983             "H",\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
987             "i", "I", "iarly",\r
988             "r", "R",\r
989             "r\u030C", "R\u030C",\r
990             "s",\r
991             "S",\r
992             "s\u030C", "S\u030C",\r
993             "z", "Z",\r
994             "z\u030C", "Z\u030C"\r
995         };\r
996         Collator coll = null;\r
997         try {\r
998             coll = Collator.getInstance(new Locale("cs", ""));\r
999         } catch (Exception e) {\r
1000             warnln("Cannot create Collator");\r
1001             return;\r
1002         }\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
1009             }\r
1010         }\r
1011     }\r
1012     \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
1018         };\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
1024         };\r
1025         \r
1026         int i = 0;\r
1027         \r
1028         for(i = 0; i < rules.length; i++) {\r
1029             genericRulesStarter(rules[i], cases[i]);\r
1030         }\r
1031         \r
1032     }\r
1033 \r
1034     public void TestFCDProblem() {\r
1035         String s1 = "\u0430\u0306\u0325";\r
1036         String s2 = "\u04D1\u0325";\r
1037         Collator coll = null;\r
1038         try {\r
1039             coll = Collator.getInstance();\r
1040         } catch (Exception e) {\r
1041             warnln("Can't create collator");\r
1042             return;\r
1043         }\r
1044         \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
1049     }\r
1050     \r
1051     public void TestEmptyRule() {\r
1052         String rulez = "";\r
1053         try {\r
1054             RuleBasedCollator coll = new RuleBasedCollator(rulez);\r
1055             logln("rule:" + coll.getRules());\r
1056         } catch (Exception e) {\r
1057             warnln(e.getMessage());\r
1058         }\r
1059     }\r
1060     \r
1061     /* superseded by TestBeforePinyin, since Chinese collation rules have changed */\r
1062     /*\r
1063     public void TestJ784() {\r
1064         String[] data = {\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
1071         };\r
1072         genericLocaleStarter(new Locale("zh", ""), data);\r
1073     }\r
1074     */\r
1075     \r
1076     public void TestJ815() {\r
1077         String data[] = {\r
1078             "aa",\r
1079             "Aa",\r
1080             "ab",\r
1081             "Ab",\r
1082             "ad",\r
1083             "Ad",\r
1084             "ae",\r
1085             "Ae",\r
1086             "\u00e6",\r
1087             "\u00c6",\r
1088             "af",\r
1089             "Af",\r
1090             "b",\r
1091             "B"\r
1092         };\r
1093         genericLocaleStarter(new Locale("fr", ""), data);\r
1094         genericRulesStarter("[backwards 2]&A<<\u00e6/e<<<\u00c6/E", data);\r
1095     }\r
1096     \r
1097     public void TestJ3087()\r
1098     {\r
1099         String rule[] = {"&h<H&CH=\u0427",\r
1100                          "&CH=\u0427&h<H",\r
1101                          "&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
1106             try {\r
1107                 rbc = new RuleBasedCollator(rule[i]); \r
1108             } catch (Exception e) {\r
1109                 warnln(e.getMessage());\r
1110                 return;\r
1111             }\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
1120                 if (ce1 != ce2) {\r
1121                     errln("Error generating RuleBasedCollator with the rule "\r
1122                           + rule[i]);\r
1123                     errln("CH != \\u0427");\r
1124                 }\r
1125             }\r
1126         }\r
1127     }\r
1128     \r
1129     public void DontTestJ831() { // Latvian does not use upper first\r
1130         String[] data = {\r
1131             "I",\r
1132             "i",\r
1133             "Y",\r
1134             "y"\r
1135         };\r
1136         genericLocaleStarter(new Locale("lv", ""), data);\r
1137     }\r
1138     \r
1139     public void TestBefore() {\r
1140         String data[] = {\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
1147         };\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
1155     }\r
1156 \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
1168             "&z<m<<<q<<<m",\r
1169             "&z<<<m<q<<<m",\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
1174         };\r
1175         \r
1176         String[] expectedRules = {\r
1177             //"&\u2089<<<x",\r
1178             "&\u0252<<<x",\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
1186             "&z<q<<<m",\r
1187             "&z<q<<<m",\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
1192         };\r
1193         \r
1194         String[][] testdata = {\r
1195             //            {"\u2089", "x"},\r
1196             {"\u0252", "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
1204             {"z", "q", "m"},\r
1205             {"z", "q", "m"},\r
1206             {"a", "b", "d"},\r
1207             {"r", "c"},\r
1208             {"a", "b", "c", "m", "d"},\r
1209             {"a", "m", "b", "c", "d"}\r
1210         };\r
1211         \r
1212         String rlz = "";\r
1213         for(int i = 0; i<rules.length; i++) {\r
1214             logln("testing rule " + rules[i] + ", expected to be" + expectedRules[i]);\r
1215             try {\r
1216                 rlz = rules[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
1224             }\r
1225             //testAgainstUCA(cresulting, credundant, "expected", TRUE, &status);\r
1226             // logln("testing using data\n");\r
1227             genericRulesStarter(rules[i], testdata[i]);\r
1228         }\r
1229     }\r
1230     \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
1236         };\r
1237         \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
1242         };\r
1243         \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
1248         };\r
1249         \r
1250         for(int i = 0; i<rules.length; i++) {\r
1251             // logln("testing rule " + rules[i] + ", expected to be " + expectedRules[i]);\r
1252             try {\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
1261             }\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
1265             \r
1266             // logln("testing using data");\r
1267             genericRulesStarter(rules[i], testdata[i]);\r
1268         }\r
1269     }\r
1270 \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
1279         };\r
1280         \r
1281         String rules =\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
1288         \r
1289         String rlz = rules;\r
1290         \r
1291         Collator coll = null;\r
1292         try {\r
1293             coll = new RuleBasedCollator(rlz);\r
1294         } catch (Exception e) {\r
1295             warnln("Unable to open collator with rules" + rules);\r
1296             return;\r
1297         }\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
1301         \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
1305         \r
1306         // no such locale in icu4j\r
1307         // logln("Using ko__LOTUS locale\n");\r
1308         // genericLocaleStarter(new Locale("ko__LOTUS", ""), koreanData);\r
1309     }\r
1310 \r
1311     public void TestIncrementalNormalize() {\r
1312         Collator        coll = null;\r
1313         // logln("Test 1 ....");\r
1314         {\r
1315             /* Test 1.  Run very long unnormalized strings, to force overflow of*/\r
1316             /*          most buffers along the way.*/\r
1317             \r
1318             try {\r
1319                 coll = Collator.getInstance(new Locale("en", "US"));\r
1320             } catch (Exception e) {\r
1321                 warnln("Cannot get default instance!");\r
1322                 return;\r
1323             }\r
1324             char baseA     =0x41;\r
1325             char ccMix[]   = {0x316, 0x321, 0x300};\r
1326             int          sLen;\r
1327             int          i;\r
1328             StringBuffer strA = new StringBuffer();\r
1329             StringBuffer strB = new StringBuffer();\r
1330             \r
1331             coll.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
1332             \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
1341                 }\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
1348             }\r
1349         }\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
1353         {\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
1358         }\r
1359         /*  Test 3:  Non-normal sequence is terminated by a surrogate pair.*/\r
1360         // logln("Test 3 ....");\r
1361         {\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
1366         }\r
1367         /*  Test 4:  Imbedded nulls do not terminate a string when length is specified.*/\r
1368         // logln("Test 4 ....");\r
1369         /*\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
1373             \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
1377          }\r
1378             \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
1382          }\r
1383             \r
1384          CollationKey sortKeyA = coll.getCollationKey(new String(strA));\r
1385          CollationKey sortKeyB = coll.getCollationKey(new String(strB));\r
1386             \r
1387          int r = sortKeyA.compareTo(sortKeyB);\r
1388          if (r <= 0) {\r
1389          errln("Error 4 in test 4\n");\r
1390          }\r
1391     \r
1392          coll.setStrength(Collator.IDENTICAL);\r
1393          sortKeyA = coll.getCollationKey(new String(strA));\r
1394          sortKeyB = coll.getCollationKey(new String(strB));\r
1395     \r
1396          r = sortKeyA.compareTo(sortKeyB);\r
1397          if (r <= 0) {\r
1398          errln("Error 7 in test 4\n");\r
1399          }\r
1400             \r
1401          coll.setStrength(Collator.TERTIARY);\r
1402          }\r
1403         */\r
1404         /*  Test 5:  Null characters in non-normal source strings.*/\r
1405         // logln("Test 5 ....");\r
1406         /*\r
1407          * not a valid test since string are null-terminated in java{\r
1408          {\r
1409          char strA[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x42,};\r
1410          char strB[] = {0x41, 0x41, 0x300, 0x316, 0x00, 0x00,};\r
1411            \r
1412     \r
1413          int result = coll.compare(new String(strA, 0, 6), new String(strB, 0, 6));\r
1414          if (result < 0) {\r
1415          errln("ERROR 1 in test 5\n");\r
1416          }\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
1420          }\r
1421     \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
1425          if (r <= 0) {\r
1426          errln("Error 4 in test 5\n");\r
1427          }\r
1428     \r
1429          coll.setStrength(Collator.IDENTICAL);\r
1430             \r
1431          sortKeyA = coll.getCollationKey(new String(strA));\r
1432          sortKeyB = coll.getCollationKey(new String(strB));\r
1433          r = sortKeyA.compareTo(sortKeyB);\r
1434          if (r <= 0) {\r
1435          errln("Error 7 in test 5\n");\r
1436          }\r
1437             \r
1438          coll.setStrength(Collator.TERTIARY);\r
1439          }\r
1440         */\r
1441         /*  Test 6:  Null character as base of a non-normal combining sequence.*/\r
1442         // logln("Test 6 ....");\r
1443         /*\r
1444          * not a valid test since string are null-terminated in java{\r
1445          {\r
1446          char strA[] = {0x41, 0x0, 0x300, 0x316, 0x41, 0x302,};\r
1447          char strB[] = {0x41, 0x0, 0x302, 0x316, 0x41, 0x300,};\r
1448     \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
1452          }\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
1456          }\r
1457          }\r
1458         */\r
1459     }\r
1460     \r
1461     public void TestContraction() {\r
1462         String[] testrules = {\r
1463             "&A = AB / B",\r
1464             "&A = A\\u0306/\\u0306",\r
1465             "&c = ch / h",\r
1466         };\r
1467         String[] testdata = {\r
1468             "AB", "AB", "A\u0306", "ch"\r
1469         };\r
1470         String[] testdata2 = {\r
1471             "\u0063\u0067",\r
1472             "\u0063\u0068",\r
1473             "\u0063\u006C",\r
1474         };\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
1484         };\r
1485     \r
1486         RuleBasedCollator  coll = null;\r
1487         for (int i = 0; i < testrules.length; i ++) {\r
1488             CollationElementIterator iter1 = null;\r
1489             int j = 0;\r
1490             // logln("Rule " + testrules[i] + " for testing\n");\r
1491             String rule = testrules[i];\r
1492             try {\r
1493                 coll = new RuleBasedCollator(rule);\r
1494             } catch (Exception e) {\r
1495                 warnln("Collator creation failed " + testrules[i]);\r
1496                 return;\r
1497             }\r
1498             try {\r
1499                 iter1 = coll.getCollationElementIterator(testdata[i]);\r
1500             } catch (Exception e) {\r
1501                 errln("Collation iterator creation failed\n");\r
1502                 return;\r
1503             }\r
1504             while (j < 2) {\r
1505                 CollationElementIterator iter2;\r
1506                 int ce;\r
1507                 try {\r
1508                     iter2 = coll.getCollationElementIterator(String.valueOf(testdata[i].charAt(j)));\r
1509                 \r
1510                 }catch (Exception e) {\r
1511                     errln("Collation iterator creation failed\n");\r
1512                     return;\r
1513                 }\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
1518                         return;\r
1519                     }\r
1520                     ce = iter2.next();\r
1521                 }\r
1522                 j ++;\r
1523             }\r
1524             if (iter1.next() != CollationElementIterator.NULLORDER) {\r
1525                 errln("Collation elements not exhausted\n");\r
1526                 return;\r
1527             }\r
1528         }\r
1529         String rule = "& a < b < c < ch < d & c = ch / h";\r
1530         try {\r
1531             coll = new RuleBasedCollator(rule);\r
1532         } catch (Exception e) {\r
1533             errln("cannot create rulebased collator");\r
1534             return;\r
1535         }\r
1536         \r
1537         if (coll.compare(testdata2[0], testdata2[1]) != -1) {\r
1538             errln("Expected " + testdata2[0] + " < " + testdata2[1]);\r
1539             return;\r
1540         }\r
1541         if (coll.compare(testdata2[1], testdata2[2]) != -1) {\r
1542             errln("Expected " + testdata2[1] + " < " + testdata2[2]);\r
1543             return;\r
1544         }\r
1545         for (int i = 0; i < testrules3.length; i += 2) {\r
1546             RuleBasedCollator          coll1, coll2;\r
1547             CollationElementIterator iter1, iter2;\r
1548             char               ch = 0x0042;\r
1549             int            ce;\r
1550             rule = testrules3[i];\r
1551             try {\r
1552                 coll1 = new RuleBasedCollator(rule);\r
1553             } catch (Exception e) {\r
1554                 errln("Fail: cannot create rulebased collator, rule:" + rule);\r
1555                 return;\r
1556             }\r
1557             rule = testrules3[i + 1];\r
1558             try {\r
1559                 coll2 = new RuleBasedCollator(rule);\r
1560             } catch (Exception e) {\r
1561                 errln("Collator creation failed " + testrules[i]);\r
1562                 return;\r
1563             }\r
1564             try {\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
1569                 return;\r
1570             }\r
1571             ce = iter1.next();\r
1572             \r
1573             while (ce != CollationElementIterator.NULLORDER) {\r
1574                 if (ce != iter2.next()) {\r
1575                     errln("CEs does not match\n");\r
1576                     return;\r
1577                 }\r
1578                 ce = iter1.next();\r
1579             }\r
1580             if (iter2.next() != CollationElementIterator.NULLORDER) {\r
1581                 errln("CEs not exhausted\n");\r
1582                 return;\r
1583             }\r
1584         }\r
1585     }\r
1586     \r
1587     public void TestExpansion() {\r
1588         String[] testrules = {\r
1589             "&J << K / B & K << M",\r
1590             "&J << K / B << M"\r
1591         };\r
1592         String[] testdata = {\r
1593             "JA", "MA", "KA", "KC", "JC", "MC",\r
1594         };\r
1595         \r
1596         Collator  coll;\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
1600             try {\r
1601                 coll = new RuleBasedCollator(rule);\r
1602             } catch (Exception e) {\r
1603                 warnln("Collator creation failed " + testrules[i]);\r
1604                 return;\r
1605             }\r
1606             \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
1610             }\r
1611         }\r
1612     }\r
1613     \r
1614     public void TestContractionEndCompare()\r
1615     {\r
1616         String rules = "&b=ch";\r
1617         String src = "bec";\r
1618         String tgt = "bech";\r
1619         Collator coll = null;\r
1620         try {\r
1621             coll = new RuleBasedCollator(rules);\r
1622         } catch (Exception e) {\r
1623             warnln("Collator creation failed " + rules);\r
1624             return;\r
1625         }\r
1626         CollationTest.doTest(this, (RuleBasedCollator)coll, src, tgt, 1);\r
1627     }\r
1628     \r
1629     public void TestLocaleRuleBasedCollators() {\r
1630         if (getInclusion() < 5) {\r
1631             // not serious enough to run this\r
1632             return;\r
1633         }\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
1638             try {\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
1643                     continue;\r
1644                 }\r
1645                 String rule = null;\r
1646                 /*\r
1647                   Object[][] colldata = (Object[][])elements;\r
1648                   // %%CollationBin\r
1649                   if (colldata[0][1] instanceof byte[]){\r
1650                   rule = (String)colldata[1][1];\r
1651                   }\r
1652                   else {\r
1653                   rule = (String)colldata[0][1];\r
1654                   }\r
1655                 */\r
1656                 rule = elements.getString("Sequence");   \r
1657 \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
1662                 }\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
1669                     }\r
1670                 }\r
1671                 prevrule = rule;\r
1672             } catch (Exception e) {\r
1673                 warnln("Error retrieving resource bundle for testing: " + e.toString());\r
1674             }\r
1675         }\r
1676     }\r
1677     \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
1683          */\r
1684         String rules[] = {\r
1685             "[optimize [\\uAC00-\\uD7FF]]"\r
1686         };\r
1687         String data[][] = {\r
1688             { "a", "b"}\r
1689         };\r
1690         int i = 0;\r
1691     \r
1692         for(i = 0; i<rules.length; i++) {\r
1693             genericRulesStarter(rules[i], data[i]);\r
1694         }\r
1695     }    \r
1696     \r
1697     public void TestIdenticalCompare() \r
1698     {    \r
1699         try {\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
1708         }\r
1709     }\r
1710     \r
1711     public void TestMergeSortKeys() \r
1712     {\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
1718         \r
1719         Collator coll = Collator.getInstance(Locale.ENGLISH);\r
1720         genericLocaleStarter(Locale.ENGLISH, cases);\r
1721         \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
1734                 }\r
1735                 if (i > 0) {\r
1736                     if (mergedPrefixKeys[i-1].compareTo(mergedPrefixKeys[i])\r
1737                         >= 0) {\r
1738                         errln("Error while comparing prefixed keys @ strength "\r
1739                               + strength);\r
1740                         errln(prettify(mergedPrefixKeys[i-1]));\r
1741                         errln(prettify(mergedPrefixKeys[i]));\r
1742                     }\r
1743                     if (mergedSuffixKeys[i-1].compareTo(mergedSuffixKeys[i]) \r
1744                         >= 0) {\r
1745                         errln("Error while comparing suffixed keys @ strength "\r
1746                               + strength);\r
1747                         errln(prettify(mergedSuffixKeys[i-1]));\r
1748                         errln(prettify(mergedSuffixKeys[i]));\r
1749                     }\r
1750                 }\r
1751             }\r
1752             if (strength == Collator.QUATERNARY) {\r
1753                 strength = Collator.IDENTICAL;\r
1754             } \r
1755             else {\r
1756                 strength ++;\r
1757             }\r
1758         }       \r
1759     }\r
1760     \r
1761     public void TestVariableTop() \r
1762     {\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
1765         try {\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
1771                 try {\r
1772                     int varTop = coll.setVariableTop(tokens[i]);\r
1773                     if (i > 4) {\r
1774                         errln("Token " + tokens[i] + " expected to fail");\r
1775                     }\r
1776                     if (varTop != coll.getVariableTop()) {\r
1777                         errln("Error setting and getting variable top");\r
1778                     }\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
1784                         }\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
1789                         }\r
1790                     }\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
1798                     }\r
1799                     if (coll.getVariableTop() != varTopOriginal) {\r
1800                         errln("When exception is thrown variable top should "\r
1801                               + "not be changed");\r
1802                     }\r
1803                 }\r
1804                 coll.setVariableTop(varTopOriginal);\r
1805                 if (varTopOriginal != coll.getVariableTop()) {\r
1806                     errln("Couldn't restore old variable top\n");\r
1807                 }\r
1808             }\r
1809             \r
1810             // Testing calling with error set\r
1811             try {\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
1816             }\r
1817             try {\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
1822             }\r
1823         } catch (Exception e) {\r
1824             warnln("Error creating RuleBasedCollator");\r
1825         }\r
1826     }\r
1827     \r
1828     public void TestUCARules() \r
1829     {\r
1830         try {\r
1831             // only root locale can have empty tailorings .. not English!\r
1832             RuleBasedCollator coll \r
1833                 = (RuleBasedCollator)Collator.getInstance(new Locale("","",""));\r
1834             String rule \r
1835                 = coll.getRules(false);\r
1836             if (!rule.equals("")) {\r
1837                 errln("Empty rule string should have empty rules " + rule);\r
1838             }\r
1839             rule = coll.getRules(true);\r
1840             if (rule.equals("")) {\r
1841                 errln("UCA rule string should not be empty");\r
1842             }\r
1843             coll = new RuleBasedCollator(rule);\r
1844         } catch (Exception e) {\r
1845             warnln(e.getMessage());\r
1846         }\r
1847     }\r
1848     \r
1849     /**\r
1850      * Jitterbug 2726\r
1851      */\r
1852     public void TestShifted()\r
1853     {\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
1859     }\r
1860     \r
1861     /**\r
1862      * Test for CollationElementIterator previous and next for the whole set of\r
1863      * unicode characters with normalization on.\r
1864      */\r
1865     public void TestNumericCollation()\r
1866     {\r
1867         String basicTestStrings[] = {"hello1", "hello2", "hello123456"};\r
1868         String preZeroTestStrings[] = {"avery1",\r
1869                                        "avery01",\r
1870                                        "avery001",\r
1871                                        "avery0001"};\r
1872         String thirtyTwoBitNumericStrings[] = {"avery42949672960",\r
1873                                                "avery42949672961",\r
1874                                                "avery42949672962",\r
1875                                                "avery429496729610"};\r
1876     \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
1887         };\r
1888     \r
1889         String foreignDigits[] = {"\u0661",\r
1890                                   "\u0662",\r
1891                                   "\u0663",\r
1892                                   "\u0661\u0660",\r
1893                                   "\u0661\u0662",\r
1894                                   "\u0661\u0663",\r
1895                                   "\u0662\u0660",\r
1896                                   "\u0662\u0662",\r
1897                                   "\u0662\u0663",\r
1898                                   "\u0663\u0660",\r
1899                                   "\u0663\u0662",\r
1900                                   "\u0663\u0663"\r
1901         };\r
1902     \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
1909                                         val);\r
1910         genericLocaleStarterWithOptions(Locale.ENGLISH, \r
1911                                         thirtyTwoBitNumericStrings, att, val);\r
1912         genericLocaleStarterWithOptions(Locale.ENGLISH, foreignDigits, att, \r
1913                                         val);\r
1914         genericLocaleStarterWithOptions(Locale.ENGLISH, supplementaryDigits, \r
1915                                         att, val);    \r
1916     \r
1917         // Setting up our collator to do digits.\r
1918         coll.setNumericCollation(true);\r
1919     \r
1920         // Testing that prepended zeroes still yield the correct collation \r
1921         // behavior. \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
1927             }\r
1928         }\r
1929 \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
1934     }\r
1935         \r
1936     public void Test3249()\r
1937     {\r
1938         String rule = "&x < a &z < a";\r
1939         try {\r
1940             RuleBasedCollator coll = new RuleBasedCollator(rule);\r
1941             if(coll!=null){\r
1942                 logln("Collator did not throw an exception");   \r
1943             }\r
1944         } catch (Exception e) {\r
1945             warnln("Error creating RuleBasedCollator with " + rule + " failed");\r
1946         }\r
1947     }\r
1948     \r
1949     public void TestTibetanConformance() \r
1950     {  \r
1951         String test[] = {"\u0FB2\u0591\u0F71\u0061", "\u0FB2\u0F71\u0061"};\r
1952         try {\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
1957             }\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
1962         }\r
1963     }\r
1964     \r
1965     public void TestJ3347()\r
1966     {\r
1967         try {\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
1972             }\r
1973         } catch (Exception e) {\r
1974             warnln("Error creating UCA collator");\r
1975         }\r
1976     }\r
1977     \r
1978     public void TestPinyinProblem()\r
1979     {\r
1980         String test[] = { "\u4E56\u4E56\u7761", "\u4E56\u5B69\u5B50" };\r
1981         genericLocaleStarter(new Locale("zh", "", "PINYIN"), test);     \r
1982     }\r
1983     \r
1984     static final long topByte = 0xFF000000L;\r
1985     static final long bottomByte = 0xFFL;\r
1986     static final long fourBytes = 0xFFFFFFFFL;\r
1987     \r
1988     static final int MAX_INPUT = 0x220001; // 2 * Unicode range + 2\r
1989     \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
1993         } \r
1994     }\r
1995 \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
1998     }\r
1999     \r
2000     private void throwError(String title, long ce) {\r
2001         errln(title + "\t" + Utility.hex(ce & fourBytes));\r
2002     }\r
2003     \r
2004     public void TestImplicitGeneration()\r
2005     {\r
2006         logln("Start");\r
2007         try {\r
2008             ImplicitCEGenerator foo = new ImplicitCEGenerator(0xE0, 0xE4);\r
2009                 \r
2010             //int x = foo.getRawImplicit(0xF810);\r
2011             foo.getRawFromImplicit(0xE20303E7);\r
2012 \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
2018             long last = 0;\r
2019             long current;\r
2020             for (int i = 0; i <= MAX_INPUT; ++i) {\r
2021                 current = foo.getImplicitFromRaw(i) & fourBytes;\r
2022                         \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
2027                 }\r
2028                 if (last != 0) {\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
2034                         }\r
2035                     }\r
2036                 }\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
2042                         \r
2043                 // do some consistency checks\r
2044                 /*\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
2058                   }\r
2059                 */\r
2060                 // print out some values for spot-checking\r
2061                 if (lastTop != currentTop || i == 0x10000 || i == 0x110000) {\r
2062                     show(i-3, foo);\r
2063                     show(i-2, foo);\r
2064                     show(i-1, foo);\r
2065                     if (i == 0) {\r
2066                         // do nothing\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
2071                     }\r
2072                     show(i, foo);\r
2073                     show(i+1, foo);\r
2074                     show(i+2, foo);\r
2075                     logln("...");\r
2076                 }\r
2077                 last = current;\r
2078                 if(foo.getCodePointFromRaw(foo.getRawFromCodePoint(i)) != i) {\r
2079                     errln("No raw <-> code point roundtrip for "+Utility.hex(i));\r
2080                 }                       \r
2081             }\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
2088         } finally {\r
2089             logln("End");\r
2090         }\r
2091     }\r
2092 \r
2093     /* supercedes TestJ784 */\r
2094     public void TestBeforePinyin() {\r
2095         String rules = \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
2102     \r
2103         String test[] = {\r
2104             "l\u0101",\r
2105             "la",\r
2106             "l\u0101n",\r
2107             "lan ",\r
2108             "l\u0113",\r
2109             "le",\r
2110             "l\u0113n",\r
2111             "len"\r
2112         };\r
2113     \r
2114         String test2[] = {\r
2115             "x\u0101",\r
2116             "x\u0100",\r
2117             "X\u0101",\r
2118             "X\u0100",\r
2119             "x\u00E1",\r
2120             "x\u00C1",\r
2121             "X\u00E1",\r
2122             "X\u00C1",\r
2123             "x\u01CE",\r
2124             "x\u01CD",\r
2125             "X\u01CE",\r
2126             "X\u01CD",\r
2127             "x\u00E0",\r
2128             "x\u00C0",\r
2129             "X\u00E0",\r
2130             "X\u00C0",\r
2131             "xa",\r
2132             "xA",\r
2133             "Xa",\r
2134             "XA",\r
2135             "x\u0101x",\r
2136             "x\u0100x",\r
2137             "x\u00E1x",\r
2138             "x\u00C1x",\r
2139             "x\u01CEx",\r
2140             "x\u01CDx",\r
2141             "x\u00E0x",\r
2142             "x\u00C0x",\r
2143             "xax",\r
2144             "xAx"\r
2145         };\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
2151     }\r
2152 \r
2153     public void TestUpperFirstQuaternary()\r
2154     {\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
2159     }\r
2160     \r
2161     public void TestJ4960()\r
2162     {\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
2174         // and case level\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
2178     }\r
2179     \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
2187         }\r
2188     \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
2193         }\r
2194         logln("");\r
2195         logln("Number of collator values returned : " + values.length);\r
2196         \r
2197         Set foundValues = new TreeSet(Arrays.asList(values));\r
2198         \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
2209             }\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
2215             }\r
2216           }        \r
2217         }\r
2218         logln(" ");\r
2219     }\r
2220 \r
2221     public void\r
2222     TestJ5367()\r
2223     {\r
2224         String[] test = { "a", "y" };\r
2225         String rules = "&Ny << Y &[first secondary ignorable] <<< a";\r
2226         genericRulesStarter(rules, test);        \r
2227     }\r
2228     \r
2229     public void\r
2230     TestVI5913()\r
2231     {\r
2232 \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
2238         };\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
2247             }\r
2248         };\r
2249         \r
2250         \r
2251         for(int i = 0; i < rules.length; i++) {\r
2252             \r
2253             RuleBasedCollator coll = null;\r
2254             try {\r
2255                 coll = new RuleBasedCollator(rules[i]);\r
2256             } catch (Exception e) {\r
2257                 warnln("Unable to open collator with rules " + rules[i]);\r
2258             }\r
2259 \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
2267                 }\r
2268                 logln("   Key:"+  prettify(key));\r
2269             }\r
2270         }   \r
2271         \r
2272         \r
2273         RuleBasedCollator vi_vi = null;\r
2274         try {\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
2287                 }\r
2288                 // logln("source:" + key.getSourceString());\r
2289                 logln("   Key:"+  prettify(key));\r
2290             }\r
2291         } catch (Exception e) {\r
2292             warnln("Error creating Vietnese collator");\r
2293             return;\r
2294         }\r
2295         \r
2296     }\r
2297     \r
2298     \r
2299     public void Test6179()\r
2300     {\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
2304         };\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
2310         \r
2311         int i=0; \r
2312         {\r
2313             \r
2314             RuleBasedCollator coll = null;\r
2315             try {\r
2316                 coll = new RuleBasedCollator(rules[i]);\r
2317             } catch (Exception e) {\r
2318                 warnln("Unable to open collator with rules " + rules[i]);\r
2319             }\r
2320 \r
2321             logln("Test rule["+i+"]"+rules[i]);\r
2322             \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
2330             }\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
2335             }\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
2343             }\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
2348             }\r
2349         }\r
2350         {\r
2351             i=1;   \r
2352             RuleBasedCollator coll = null;\r
2353             try {\r
2354                 coll = new RuleBasedCollator(rules[i]);\r
2355             } catch (Exception e) {\r
2356                 warnln("Unable to open collator with rules " + rules[i]);\r
2357             }\r
2358 \r
2359             logln("Test rule["+i+"]"+rules[i]);\r
2360             \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
2368                     }\r
2369                     break;\r
2370                 }\r
2371             }\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
2376             }\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
2384                     }\r
2385                     break;\r
2386                 }\r
2387             }\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
2392             }\r
2393         }   \r
2394     }\r
2395     \r
2396     public void TestUCAPrecontext()\r
2397     {\r
2398         String rules[] = {\r
2399                 "& \u00B7<a ",\r
2400                 "& L\u00B7 << a", // 'a' is an expansion. \r
2401         };\r
2402         String cases[] = {\r
2403             "\u00B7", \r
2404             "\u0387", \r
2405             "a",\r
2406             "l",\r
2407             "L\u0332",\r
2408             "l\u00B7",\r
2409             "l\u0387",\r
2410             "L\u0387",\r
2411             "la\u0387",\r
2412             "La\u00b7",\r
2413         };\r
2414 \r
2415         // Test en sort\r
2416         RuleBasedCollator en = null;\r
2417         \r
2418         logln("EN sort:");\r
2419         try {\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
2424                 if (j>0) {\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
2429                     }\r
2430                 }\r
2431                 /*\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
2435                 }\r
2436                 */\r
2437                 logln("String:"+cases[j]+"   Key:"+  prettify(key));\r
2438             }\r
2439         } catch (Exception e) {\r
2440             warnln("Error creating Vietnese collator");\r
2441             return;\r
2442         }\r
2443         \r
2444         // Test ja sort\r
2445         RuleBasedCollator ja = null;\r
2446         logln("JA sort:");\r
2447         try {\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
2452                 if (j>0) {\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
2457                     }\r
2458                 }\r
2459                 logln("String:"+cases[j]+"   Key:"+  prettify(key));\r
2460             }\r
2461         } catch (Exception e) {\r
2462             warnln("Error creating Vietnese collator");\r
2463             return;\r
2464         }\r
2465         for(int i = 0; i < rules.length; i++) {\r
2466             \r
2467             RuleBasedCollator coll = null;\r
2468             logln("Tailoring rule:"+rules[i]);\r
2469             try {\r
2470                 coll = new RuleBasedCollator(rules[i]);\r
2471             } catch (Exception e) {\r
2472                 warnln("Unable to open collator with rules " + rules[i]);\r
2473             }\r
2474 \r
2475             for (int j=0; j<cases.length; j++) {\r
2476                 CollationKey key = coll.getCollationKey(cases[j]);\r
2477                 if (j>0) {\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
2483                         }\r
2484                     }\r
2485                     else {\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
2489                         }\r
2490                     }\r
2491                 }\r
2492                 logln("String:"+cases[j]+"   Key:"+  prettify(key));\r
2493             }\r
2494         }   \r
2495     }\r
2496 \r
2497     public void TestSameStrengthList() {\r
2498         String[] testSourceCases = {\r
2499             "\u0061",\r
2500             "\u0061",\r
2501             "\u006c\u0061",\r
2502             "\u0061\u0061\u0061",\r
2503             "\u0062",\r
2504         };\r
2505         \r
2506         String[] testTargetCases = {\r
2507             "\u0031",\r
2508             "\u0066",\r
2509             "\u006b\u0062",\r
2510             "\u0031\u0032\u0033",\r
2511             "\u007a",\r
2512         };\r
2513         \r
2514         int[] results = {\r
2515             0,\r
2516             -1,\r
2517             -1,\r
2518             0,\r
2519             -1\r
2520         };\r
2521 \r
2522         Collator  myCollation;\r
2523         String rules = "&a<*bcd &b<<*klm &k<<<*xyz &a=*123";\r
2524         try {\r
2525             myCollation = new RuleBasedCollator(rules);\r
2526         } catch (Exception e) {\r
2527             warnln("ERROR: in creation of rule based collator");\r
2528             return;\r
2529         }\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
2534         {\r
2535             CollationTest.doTest(this, (RuleBasedCollator)myCollation, \r
2536                                  testSourceCases[i], testTargetCases[i], \r
2537                                  results[i]);\r
2538         }\r
2539     }\r
2540 \r
2541     /*\r
2542      * Tests the method public boolean equals(Object target) in CollationKey\r
2543      */\r
2544     public void TestCollationKeyEquals() {\r
2545         CollationKey ck = new CollationKey("", (byte[]) null);\r
2546 \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
2551         }\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
2555         }\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
2559         }\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
2563         }\r
2564 \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
2569         }\r
2570     }\r
2571 \r
2572     /*\r
2573      * Tests the method public int hashCode() in CollationKey\r
2574      */\r
2575     public void TestCollationKeyHashCode() {\r
2576         CollationKey ck = new CollationKey("", (byte[]) null);\r
2577 \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
2582         }\r
2583     }\r
2584 \r
2585     /*\r
2586      * Tests the method public CollationKey getBound(int boundType, int noOfLevels)\r
2587      */\r
2588     public void TestGetBound() {\r
2589         CollationKey ck = new CollationKey("", (byte[]) null);\r
2590 \r
2591         // Tests when "if (noOfLevels > Collator.PRIMARY)" is false\r
2592         // Tests when "default: " is true for "switch (boundType)"\r
2593         try {\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
2598         }\r
2599 \r
2600         // Tests when "if (noOfLevels > 0)"\r
2601         byte b[] = {};\r
2602         CollationKey ck1 = new CollationKey("", b);\r
2603         try {\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
2608         }\r
2609     }\r
2610 \r
2611     /*\r
2612      * Tests the method public CollationKey merge(CollationKey source)\r
2613      */\r
2614     public void TestMerge() {\r
2615         byte b[] = {};\r
2616         CollationKey ck = new CollationKey("", b);\r
2617 \r
2618         // Tests when "if (source == null || source.getLength() == 0)" is true\r
2619         try {\r
2620             ck.merge(null);\r
2621             errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");\r
2622         } catch (Exception e) {\r
2623         }\r
2624         try {\r
2625             ck.merge(ck);\r
2626             errln("Collationkey.merge(CollationKey) was suppose to return " + "an exception for a null parameter.");\r
2627         } catch (Exception e) {\r
2628         }\r
2629     }\r
2630     \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
2636         \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
2640         }\r
2641         \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
2645         }\r
2646     }\r
2647 }\r