]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationAPITest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationAPITest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2008, 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 : collate/CollationAPITest\r
10  * Source File: $ICU4CRoot/source/test/intltest/apicoll.cpp\r
11  **/\r
12  \r
13 package com.ibm.icu.dev.test.collator;\r
14 \r
15 import java.util.Locale;\r
16 import java.util.Arrays;\r
17 import java.text.CharacterIterator;\r
18 import java.text.StringCharacterIterator;\r
19 import com.ibm.icu.dev.test.*;\r
20 import com.ibm.icu.text.*;\r
21 import com.ibm.icu.util.ULocale;\r
22 import com.ibm.icu.util.VersionInfo;\r
23 import java.util.MissingResourceException;\r
24 \r
25 public class CollationAPITest extends TestFmwk {\r
26     public static void main(String[] args) throws Exception {\r
27         new CollationAPITest().run(args);\r
28         //new CollationAPITest().TestGetTailoredSet();\r
29     }\r
30     \r
31     /**\r
32      * This tests the collation key related APIs.\r
33      * - constructor/destructor\r
34      * - Collator.getCollationKey\r
35      * - == and != operators\r
36      * - comparison between collation keys\r
37      * - creating collation key with a byte array and vice versa\r
38      */\r
39     public void TestCollationKey() {\r
40         logln("testing CollationKey begins...");\r
41         Collator col = Collator.getInstance();\r
42         col.setStrength(Collator.TERTIARY);\r
43     \r
44         String test1 = "Abcda";\r
45         String test2 = "abcda";\r
46     \r
47         logln("Testing weird arguments");\r
48         CollationKey sortk1 = col.getCollationKey("");\r
49         // key gets reset here\r
50         byte[] bytes = sortk1.toByteArray();\r
51         doAssert(bytes.length == 3 && bytes[0] == 1 && bytes[1] == 1 \r
52                  && bytes[2] == 0, \r
53                  "Empty string should return an empty collation key");\r
54         // bogus key returned here\r
55         sortk1 = col.getCollationKey(null);\r
56         doAssert(sortk1 == null, "Error code should return bogus collation key");\r
57 \r
58         logln("Use tertiary comparison level testing ....");\r
59         sortk1 = col.getCollationKey(test1);\r
60         CollationKey sortk2 = col.getCollationKey(test2);\r
61         Object sortk3 = sortk2;\r
62         doAssert((sortk1.compareTo(sortk2)) > 0 \r
63                  && (sortk1.compareTo(sortk3)) > 0, "Result should be \"Abcda\" >>> \"abcda\"");\r
64     \r
65         CollationKey sortkNew;\r
66         sortkNew = sortk1;\r
67         doAssert(!(sortk1.equals(sortk2)) && !(sortk1.equals(sortk3)), \r
68                  "The sort keys should be different");\r
69         doAssert((sortk1.hashCode() != sortk2.hashCode()), "sort key hashCode() failed");\r
70         doAssert((sortk1.equals(sortkNew)), "The sort keys assignment failed");\r
71         doAssert((sortk1.hashCode() == sortkNew.hashCode()), "sort key hashCode() failed");\r
72 \r
73         // check invaliad comparisons\r
74         Object fake = "fake";\r
75         try {\r
76             sortk1.compareTo(fake);\r
77             errln("Non-CollationKey comparison");\r
78         }\r
79         catch (Exception e) {\r
80                 logln("PASS: Non-CollationKey comparison failed as expected");\r
81         }\r
82         if (sortk1.equals(fake)) {\r
83             errln("Non-CollationKey comparison");\r
84         }\r
85         \r
86         // port from apicoll\r
87         try {\r
88             col = Collator.getInstance();\r
89         } catch (Exception e) {\r
90             errln("Collator.getInstance() failed");\r
91         }\r
92         if (col.getStrength() != Collator.TERTIARY){\r
93             errln("Default collation did not have tertiary strength");\r
94         }\r
95 \r
96         // Need to use identical strength\r
97         col.setStrength(Collator.IDENTICAL);\r
98 \r
99         byte key2compat[] = {\r
100             // 3.9 key, UCA 5.1\r
101             (byte) 0x2c, (byte) 0x2e, (byte) 0x30,\r
102             (byte) 0x32, (byte) 0x2c, (byte) 0x01,\r
103             (byte) 0x09, (byte) 0x01, (byte) 0x09,\r
104             (byte) 0x01, (byte) 0x2b, (byte) 0x01,\r
105             (byte) 0x92, (byte) 0x93, (byte) 0x94,\r
106             (byte) 0x95, (byte) 0x92, (byte) 0x00\r
107 \r
108             // 3.6 key, UCA 5.0\r
109             /*\r
110             (byte) 0x29, (byte) 0x2b, (byte) 0x2d,\r
111             (byte) 0x2f, (byte) 0x29, (byte) 0x01,\r
112             (byte) 0x09, (byte) 0x01, (byte) 0x09,\r
113             (byte) 0x01, (byte) 0x28, (byte) 0x01,\r
114             (byte) 0x92, (byte) 0x93, (byte) 0x94,\r
115             (byte) 0x95, (byte) 0x92, (byte) 0x00\r
116             */\r
117 \r
118             // 3.4 key UCA 4.1\r
119             /*\r
120             (byte) 0x28, (byte) 0x2a, (byte) 0x2c, \r
121             (byte) 0x2e, (byte) 0x28, (byte) 0x01, \r
122             (byte) 0x09, (byte) 0x01, (byte) 0x09, \r
123             (byte) 0x01, (byte) 0x27, (byte) 0x01, \r
124             (byte) 0x92, (byte) 0x93, (byte) 0x94, \r
125             (byte) 0x95, (byte) 0x92, (byte) 0x00        \r
126             */ \r
127             \r
128             //          2.6.1 key\r
129             /*\r
130             0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01, \r
131             0x09, 0x01, 0x09, 0x01, 0x25, 0x01, \r
132             0x92, 0x93, 0x94, 0x95, 0x92, 0x00 \r
133             */\r
134             \r
135             // 2.2 key\r
136             /*\r
137             0x1D, 0x1F, 0x21, 0x23, 0x1D, 0x01,\r
138             0x09, 0x01, 0x09, 0x01, 0x1C, 0x01,\r
139             0x92, 0x93, 0x94, 0x95, 0x92, 0x00\r
140             */\r
141             \r
142             // 2.0 key\r
143             /*\r
144             0x19, 0x1B, 0x1D, 0x1F, 0x19,\r
145             0x01, 0x09, 0x01, 0x09, 0x01,\r
146             0x18, 0x01,\r
147             0x92, 0x93, 0x94, 0x95, 0x92,\r
148             0x00\r
149             */\r
150             \r
151             // 1.8.1 key.\r
152             /*\r
153             0x19, 0x1B, 0x1D, 0x1F, 0x19,\r
154             0x01, 0x0A, 0x01, 0x0A, 0x01,\r
155             0x92, 0x93, 0x94, 0x95, 0x92,\r
156             0x00 \r
157             */\r
158         };\r
159 \r
160         CollationKey key1 = col.getCollationKey(test1);\r
161         CollationKey key2 = col.getCollationKey(test2);\r
162         CollationKey key3 = col.getCollationKey(test2);\r
163     \r
164         doAssert(key1.compareTo(key2) > 0, \r
165                  "Result should be \"Abcda\" > \"abcda\"");\r
166         doAssert(key2.compareTo(key1) < 0,\r
167                 "Result should be \"abcda\" < \"Abcda\"");\r
168         doAssert(key2.compareTo(key3) == 0,\r
169                 "Result should be \"abcda\" ==  \"abcda\"");\r
170      \r
171         doAssert(Arrays.equals(key2.toByteArray(), key2compat),\r
172                  "Binary format for 'abcda' sortkey different for identical strength!");\r
173     \r
174         logln("Use secondary comparision level testing ...");\r
175         col.setStrength(Collator.SECONDARY);\r
176     \r
177         key1 = col.getCollationKey(test1);\r
178         key2 = col.getCollationKey(test2);\r
179         key3 = col.getCollationKey(test2);\r
180     \r
181         doAssert(key1.compareTo(key2) == 0, \r
182                 "Result should be \"Abcda\" == \"abcda\"");\r
183         doAssert(key2.compareTo(key3) == 0,\r
184                 "Result should be \"abcda\" ==  \"abcda\"");\r
185     \r
186         byte tempkey[] = key2.toByteArray();\r
187         byte subkey2compat[] = new byte[tempkey.length];\r
188         System.arraycopy(key2compat, 0, subkey2compat, 0, tempkey.length);\r
189         subkey2compat[subkey2compat.length - 1] = 0;\r
190         doAssert(Arrays.equals(tempkey, subkey2compat),\r
191                  "Binary format for 'abcda' sortkey different for secondary strength!");\r
192     \r
193         logln("testing sortkey ends...");\r
194     }\r
195     \r
196     public void TestRawCollationKey()\r
197     {\r
198         // testing constructors\r
199         RawCollationKey key = new RawCollationKey();\r
200         if (key.bytes != null || key.size != 0) {\r
201             errln("Empty default constructor expected to leave the bytes null "\r
202                   + "and size 0"); \r
203         }\r
204         byte array[] = new byte[128];\r
205         key = new RawCollationKey(array);\r
206         if (key.bytes != array || key.size != 0) {\r
207             errln("Constructor taking an array expected to adopt it and "\r
208                   + "retaining its size 0"); \r
209         }\r
210         try {\r
211             key = new RawCollationKey(array, 129);\r
212             errln("Constructor taking an array and a size > array.length "\r
213                   + "expected to throw an exception"); \r
214         } catch (IndexOutOfBoundsException e) {\r
215                 logln("PASS: Constructor failed as expected");\r
216         }\r
217         try {\r
218             key = new RawCollationKey(array, -1);\r
219             errln("Constructor taking an array and a size < 0 "\r
220                   + "expected to throw an exception"); \r
221         } catch (IndexOutOfBoundsException e) {\r
222                 logln("PASS: Constructor failed as expected");\r
223         }\r
224         key = new RawCollationKey(array, array.length >> 1);\r
225         if (key.bytes != array || key.size != (array.length >> 1)) {\r
226             errln("Constructor taking an array and a size, "\r
227                   + "expected to adopt it and take the size specified"); \r
228         }\r
229         key = new RawCollationKey(10);\r
230         if (key.bytes == null || key.bytes.length != 10 || key.size != 0) {\r
231             errln("Constructor taking a specified capacity expected to "\r
232                   + "create a new internal byte array with length 10 and "\r
233                   + "retain size 0"); \r
234         }\r
235     }\r
236     \r
237     void doAssert(boolean conditions, String message) {\r
238         if (!conditions) {\r
239             errln(message);\r
240         }\r
241     }\r
242     \r
243     /**\r
244      * This tests the comparison convenience methods of a collator object.\r
245      * - greater than\r
246      * - greater than or equal to\r
247      * - equal to\r
248      */\r
249     public void TestCompare() {\r
250         logln("The compare tests begin : ");\r
251         Collator col = Collator.getInstance(Locale.ENGLISH);\r
252         \r
253         String test1 = "Abcda";\r
254         String test2 = "abcda";\r
255         logln("Use tertiary comparison level testing ....");\r
256         \r
257         Object obj1 = test1;\r
258         Object obj2 = test2;\r
259         doAssert((col.compare(obj1, obj2) > 0), "Result should be \"Abcda\" != \"abcda\"");\r
260         doAssert((!col.equals(test1, test2) ), "Result should be \"Abcda\" != \"abcda\"");\r
261         doAssert((col.compare(test1, test2) > 0 ), "Result should be \"Abcda\" >>> \"abcda\"");\r
262     \r
263         col.setStrength(Collator.SECONDARY);\r
264         logln("Use secondary comparison level testing ....");\r
265                     \r
266         doAssert((col.compare(obj1, obj2) == 0), "Result should be \"Abcda\" == \"abcda\"");\r
267         doAssert((col.equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");\r
268         doAssert((col.compare(test1, test2) == 0), "Result should be \"Abcda\" == \"abcda\"");\r
269     \r
270         col.setStrength(Collator.PRIMARY);\r
271         logln("Use primary comparison level testing ....");\r
272         \r
273         doAssert((col.compare(obj1, obj2) == 0 ), "Result should be \"Abcda\" == \"abcda\"");\r
274         doAssert((col.equals(test1, test2) ), "Result should be \"Abcda\" == \"abcda\"");\r
275         doAssert((col.compare(test1, test2) == 0 ), "Result should be \"Abcda\" == \"abcda\"");\r
276         logln("The compare tests end.");\r
277         \r
278         Integer die = new Integer(1);\r
279         try {\r
280             col.compare(die, test1);\r
281             errln("Non-Strings should fail col.compare(Object, Object)");\r
282         } catch (Exception e) {\r
283                 logln("PASS: Non-Strings comparison failed as expected");\r
284         }\r
285     }\r
286     \r
287     /**\r
288     * Tests decomposition setting\r
289     */\r
290     public void TestDecomposition() {\r
291         Collator en_US = null, el_GR = null, vi_VN = null;\r
292 \r
293         en_US = Collator.getInstance(new Locale("en", "US"));\r
294         el_GR = Collator.getInstance(new Locale("el", "GR"));\r
295         vi_VN = Collator.getInstance(new Locale("vi", "VN"));\r
296 \r
297         \r
298         // there is no reason to have canonical decomposition in en_US OR default locale */\r
299         if (vi_VN.getDecomposition() != Collator.CANONICAL_DECOMPOSITION)\r
300         {\r
301             errln("vi_VN collation did not have cannonical decomposition for normalization!");\r
302         }\r
303 \r
304         if (el_GR.getDecomposition() != Collator.CANONICAL_DECOMPOSITION)\r
305         {\r
306             errln("el_GR collation did not have cannonical decomposition for normalization!");\r
307         }\r
308 \r
309         if (en_US.getDecomposition() != Collator.NO_DECOMPOSITION)\r
310         {\r
311             errln("en_US collation had cannonical decomposition for normalization!");\r
312         }\r
313     }\r
314     \r
315     /**\r
316      * This tests the duplication of a collator object.\r
317      */\r
318     public void TestDuplicate() {\r
319         //Clone does not be implemented \r
320         Collator col1 = Collator.getInstance(Locale.ENGLISH);\r
321         \r
322         // Collator col2 = (Collator)col1.clone();\r
323         // doAssert(col1.equals(col2), "Cloned object is not equal to the orginal");\r
324         String ruleset = "< a, A < b, B < c, C < d, D, e, E";\r
325         RuleBasedCollator col3 = null;\r
326         try {\r
327             col3 = new RuleBasedCollator(ruleset);\r
328         } catch (Exception e) {\r
329             errln("Failure creating RuleBasedCollator with rule:" + ruleset);\r
330             return;\r
331         }\r
332         doAssert(!col1.equals(col3), "Cloned object is equal to some dummy");\r
333         col3 = (RuleBasedCollator)col1;\r
334         doAssert(col1.equals(col3), "Copied object is not equal to the orginal");\r
335         \r
336     }\r
337     \r
338     /**\r
339      * This tests the CollationElementIterator related APIs.\r
340      * - creation of a CollationElementIterator object\r
341      * - == and != operators\r
342      * - iterating forward\r
343      * - reseting the iterator index\r
344      * - requesting the order properties(primary, secondary or tertiary)\r
345      */\r
346     public void TestElemIter() {\r
347         // logln("testing sortkey begins...");\r
348         Collator col = Collator.getInstance(Locale.ENGLISH);\r
349 \r
350            \r
351         String testString1 = "XFILE What subset of all possible test cases has the highest probability of detecting the most errors?";\r
352         String testString2 = "Xf_ile What subset of all possible test cases has the lowest probability of detecting the least errors?";\r
353         // logln("Constructors and comparison testing....");\r
354         CollationElementIterator iterator1 = ((RuleBasedCollator)col).getCollationElementIterator(testString1);\r
355         \r
356         CharacterIterator chariter=new StringCharacterIterator(testString1);\r
357         // copy ctor\r
358         CollationElementIterator iterator2 = ((RuleBasedCollator)col).getCollationElementIterator(chariter);\r
359         UCharacterIterator uchariter=UCharacterIterator.getInstance(testString2);\r
360         CollationElementIterator iterator3 = ((RuleBasedCollator)col).getCollationElementIterator(uchariter);\r
361     \r
362         int offset = 0;\r
363         offset = iterator1.getOffset();\r
364         if (offset != 0) {\r
365             errln("Error in getOffset for collation element iterator");\r
366             return;\r
367         }\r
368         iterator1.setOffset(6);\r
369         iterator1.setOffset(0);\r
370         int order1, order2, order3;\r
371         \r
372         order1 = iterator1.next();\r
373         doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");\r
374         order2 = iterator2.next();\r
375         \r
376         doAssert((iterator1.equals(iterator2)), "The second iterator advance failed"); \r
377         doAssert((order1 == order2), "The order result should be the same");\r
378         order3 = iterator3.next();\r
379         \r
380         doAssert((CollationElementIterator.primaryOrder(order1) == \r
381             CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");\r
382         doAssert((CollationElementIterator.secondaryOrder(order1) == \r
383             CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");\r
384         doAssert((CollationElementIterator.tertiaryOrder(order1) == \r
385             CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");\r
386     \r
387         order1 = iterator1.next(); \r
388         order3 = iterator3.next();\r
389         \r
390         doAssert((CollationElementIterator.primaryOrder(order1) == \r
391             CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");\r
392         doAssert((CollationElementIterator.tertiaryOrder(order1) != \r
393             CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");\r
394     \r
395         order1 = iterator1.next(); \r
396         order3 = iterator3.next();\r
397         // invalid test wrong in UCA\r
398         // doAssert((CollationElementIterator.secondaryOrder(order1) != \r
399         //    CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");\r
400             \r
401         doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");\r
402     \r
403         iterator1.reset(); \r
404         iterator2.reset(); \r
405         iterator3.reset();\r
406         order1 = iterator1.next();\r
407         \r
408         doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");\r
409         \r
410         order2 = iterator2.next();\r
411         \r
412         doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");\r
413         doAssert((order1 == order2), "The order result should be the same");\r
414     \r
415         order3 = iterator3.next();\r
416         \r
417         doAssert((CollationElementIterator.primaryOrder(order1) == \r
418             CollationElementIterator.primaryOrder(order3)), "The primary orders should be the same");\r
419         doAssert((CollationElementIterator.secondaryOrder(order1) == \r
420             CollationElementIterator.secondaryOrder(order3)), "The secondary orders should be the same");\r
421         doAssert((CollationElementIterator.tertiaryOrder(order1) == \r
422             CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be the same");\r
423     \r
424         order1 = iterator1.next(); \r
425         order2 = iterator2.next(); \r
426         order3 = iterator3.next();\r
427         \r
428         doAssert((CollationElementIterator.primaryOrder(order1) == \r
429             CollationElementIterator.primaryOrder(order3)), "The primary orders should be identical");\r
430         doAssert((CollationElementIterator.tertiaryOrder(order1) != \r
431             CollationElementIterator.tertiaryOrder(order3)), "The tertiary orders should be different");\r
432     \r
433         order1 = iterator1.next(); \r
434         order3 = iterator3.next();\r
435         \r
436         // obsolete invalid test, removed\r
437         // doAssert((CollationElementIterator.secondaryOrder(order1) != \r
438         //    CollationElementIterator.secondaryOrder(order3)), "The secondary orders should not be the same");\r
439         doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");\r
440         doAssert(!(iterator2.equals(iterator3)), "The iterators should be different");\r
441         logln("testing CollationElementIterator ends...");\r
442     }\r
443     \r
444     /**\r
445      * This tests the hashCode method of a collator object.\r
446      */\r
447     public void TestHashCode() {\r
448         logln("hashCode tests begin.");\r
449         Collator col1 = Collator.getInstance(Locale.ENGLISH);\r
450     \r
451         Collator col2 = null;\r
452         Locale dk = new Locale("da", "DK", "");\r
453         try {\r
454             col2 = Collator.getInstance(dk);\r
455         } catch (Exception e) {\r
456             errln("Danish collation creation failed.");\r
457             return;\r
458         }\r
459     \r
460         Collator col3 = null;\r
461         try {\r
462             col3 = Collator.getInstance(Locale.ENGLISH);\r
463         } catch (Exception e) {\r
464             errln("2nd default collation creation failed.");\r
465             return;\r
466         }\r
467     \r
468         logln("Collator.hashCode() testing ...");\r
469         \r
470         doAssert(col1.hashCode() != col2.hashCode(), "Hash test1 result incorrect" );                 \r
471         doAssert(!(col1.hashCode() == col2.hashCode()), "Hash test2 result incorrect" );              \r
472         doAssert(col1.hashCode() == col3.hashCode(), "Hash result not equal" );               \r
473     \r
474         logln("hashCode tests end.");\r
475         \r
476         String test1 = "Abcda";\r
477         String test2 = "abcda";\r
478         \r
479         CollationKey sortk1, sortk2, sortk3;\r
480                     \r
481         sortk1 = col3.getCollationKey(test1);\r
482         sortk2 = col3.getCollationKey(test2); \r
483         sortk3 = col3.getCollationKey(test2); \r
484         \r
485         doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");               \r
486         doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );\r
487     }\r
488     \r
489     /**\r
490      * This tests the properties of a collator object.\r
491      * - constructor\r
492      * - factory method getInstance\r
493      * - compare and getCollationKey\r
494      * - get/set decomposition mode and comparison level\r
495      */\r
496     public void TestProperty() {\r
497         /* \r
498           All the collations have the same version in an ICU\r
499           version.\r
500           ICU 2.0 currVersionArray = {0x18, 0xC0, 0x02, 0x02};\r
501           ICU 2.1 currVersionArray = {0x19, 0x00, 0x03, 0x03};\r
502           ICU 2.8 currVersionArray = {0x29, 0x80, 0x00, 0x04};          \r
503         */    \r
504         logln("The property tests begin : ");\r
505         logln("Test ctors : ");\r
506         Collator col = Collator.getInstance(Locale.ENGLISH);\r
507 \r
508     \r
509         logln("Test getVersion");\r
510         VersionInfo expectedVersion = VersionInfo.getInstance(0x31, 0xC0, 0x00, 0x05);\r
511         doAssert(col.getVersion().equals(expectedVersion), "Expected version "+expectedVersion.toString()+" got "+col.getVersion().toString());\r
512         \r
513         logln("Test getUCAVersion");\r
514         VersionInfo expectedUCAVersion = VersionInfo.getInstance(0x05, 1, 0, 0);\r
515         doAssert(col.getUCAVersion().equals(expectedUCAVersion), "Expected UCA version "+expectedUCAVersion.toString()+" got "+col.getUCAVersion().toString());\r
516         \r
517         doAssert((col.compare("ab", "abc") < 0), "ab < abc comparison failed");\r
518         doAssert((col.compare("ab", "AB") < 0), "ab < AB comparison failed");\r
519         doAssert((col.compare("blackbird", "black-bird") > 0), "black-bird > blackbird comparison failed");\r
520         doAssert((col.compare("black bird", "black-bird") < 0), "black bird > black-bird comparison failed");\r
521         doAssert((col.compare("Hello", "hello") > 0), "Hello > hello comparison failed");\r
522     \r
523         logln("Test ctors ends.");\r
524         \r
525         logln("testing Collator.getStrength() method ...");\r
526         doAssert((col.getStrength() == Collator.TERTIARY), "collation object has the wrong strength");\r
527         doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");\r
528             \r
529         logln("testing Collator.setStrength() method ...");\r
530         col.setStrength(Collator.SECONDARY);\r
531         doAssert((col.getStrength() != Collator.TERTIARY), "collation object's strength is secondary difference");\r
532         doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");\r
533         doAssert((col.getStrength() == Collator.SECONDARY), "collation object has the wrong strength");\r
534     \r
535         logln("testing Collator.setDecomposition() method ...");\r
536         col.setDecomposition(Collator.NO_DECOMPOSITION);\r
537         doAssert((col.getDecomposition() != Collator.CANONICAL_DECOMPOSITION), "Decomposition mode != Collator.CANONICAL_DECOMPOSITION");\r
538         doAssert((col.getDecomposition() == Collator.NO_DECOMPOSITION), "Decomposition mode = Collator.NO_DECOMPOSITION");\r
539         \r
540         \r
541         RuleBasedCollator rcol = (RuleBasedCollator)Collator.getInstance(new Locale("da", "DK"));\r
542         doAssert(rcol.getRules().length() != 0, "da_DK rules does not have length 0");\r
543         \r
544         try {\r
545             col = Collator.getInstance(Locale.FRENCH);\r
546         } catch (Exception e) {\r
547             errln("Creating French collation failed.");\r
548             return;\r
549         }\r
550     \r
551         col.setStrength(Collator.PRIMARY);\r
552         logln("testing Collator.getStrength() method again ...");\r
553         doAssert((col.getStrength() != Collator.TERTIARY), "collation object has the wrong strength");\r
554         doAssert((col.getStrength() == Collator.PRIMARY), "collation object's strength is not primary difference");\r
555             \r
556         logln("testing French Collator.setStrength() method ...");\r
557         col.setStrength(Collator.TERTIARY);\r
558         doAssert((col.getStrength() == Collator.TERTIARY), "collation object's strength is not tertiary difference");\r
559         doAssert((col.getStrength() != Collator.PRIMARY), "collation object's strength is primary difference");\r
560         doAssert((col.getStrength() != Collator.SECONDARY), "collation object's strength is secondary difference");\r
561     \r
562     }\r
563     public void TestJunkCollator(){\r
564         logln("Create junk collation: ");\r
565         Locale abcd = new Locale("ab", "CD", "");\r
566         \r
567         Collator junk = Collator.getInstance(abcd);\r
568         Collator col = Collator.getInstance();\r
569 \r
570     \r
571         String colrules = ((RuleBasedCollator)col).getRules();\r
572         String junkrules = ((RuleBasedCollator)junk).getRules();\r
573         doAssert(colrules == junkrules || colrules.equals(junkrules), \r
574                    "The default collation should be returned.");\r
575         Collator frCol = null;\r
576         try {\r
577             frCol = Collator.getInstance(Locale.FRANCE);\r
578         } catch (Exception e) {\r
579             errln("Creating French collator failed.");\r
580             return;\r
581         }\r
582     \r
583         doAssert(!(frCol.equals(junk)), "The junk is the same as the French collator.");\r
584         logln("Collator property test ended.");\r
585     \r
586     }\r
587     /**\r
588     * This tests the RuleBasedCollator\r
589     * - constructor/destructor\r
590     * - getRules\r
591     */\r
592     public void TestRuleBasedColl() {\r
593         RuleBasedCollator col1 = null, col2 = null, col3 = null, col4 = null;\r
594     \r
595         String ruleset1 = "&9 < a, A < b, B < c, C; ch, cH, Ch, CH < d, D, e, E"; \r
596         String ruleset2 = "&9 < a, A < b, B < c, C < d, D, e, E";\r
597         String ruleset3 = "&";\r
598         \r
599         try {\r
600             col1 = new RuleBasedCollator(ruleset1);\r
601         } catch (Exception e) {\r
602             // only first error needs to be a warning since we exit function \r
603             warnln("RuleBased Collator creation failed.");\r
604             return;\r
605         }\r
606     \r
607         try {\r
608             col2 = new RuleBasedCollator(ruleset2);\r
609         } catch (Exception e) {\r
610             errln("RuleBased Collator creation failed.");\r
611             return;\r
612         }\r
613     \r
614         try {\r
615             // empty rules fail\r
616             col3 = new RuleBasedCollator(ruleset3);\r
617             errln("Failure: Empty rules for the collator should fail");\r
618             return;\r
619         } catch (MissingResourceException e) {\r
620             warnln(e.getMessage());\r
621         } catch (Exception e) {\r
622             logln("PASS: Empty rules for the collator failed as expected");\r
623         }\r
624         \r
625         Locale locale = new Locale("aa", "AA");\r
626         try {\r
627             col3 = (RuleBasedCollator)Collator.getInstance(locale);\r
628         } catch (Exception e) {\r
629             errln("Fallback Collator creation failed.: %s");\r
630             return;\r
631         }\r
632     \r
633         try {\r
634             col3 = (RuleBasedCollator)Collator.getInstance();\r
635         } catch (Exception e) {\r
636             errln("Default Collator creation failed.: %s");\r
637             return;\r
638         }\r
639     \r
640         String rule1 = col1.getRules(); \r
641         String rule2 = col2.getRules();\r
642         String rule3 = col3.getRules();\r
643     \r
644         doAssert(!rule1.equals(rule2), "Default collator getRules failed");\r
645         doAssert(!rule2.equals(rule3), "Default collator getRules failed");\r
646         doAssert(!rule1.equals(rule3), "Default collator getRules failed");\r
647         \r
648         try {\r
649             col4 = new RuleBasedCollator(rule2);\r
650         } catch (Exception e) {\r
651             errln("RuleBased Collator creation failed.");\r
652             return;\r
653         }\r
654     \r
655         String rule4 = col4.getRules();\r
656         doAssert(rule2.equals(rule4), "Default collator getRules failed");\r
657         // tests that modifier ! is always ignored\r
658         String exclamationrules = "!&a<b";\r
659         // java does not allow ! to be the start of the rule\r
660         String thaistr = "\u0e40\u0e01\u0e2d";\r
661         try {\r
662             RuleBasedCollator col5 = new RuleBasedCollator(exclamationrules);\r
663             RuleBasedCollator encol = (RuleBasedCollator)\r
664                                         Collator.getInstance(Locale.ENGLISH);\r
665             CollationElementIterator col5iter \r
666                                    = col5.getCollationElementIterator(thaistr);\r
667             CollationElementIterator encoliter \r
668                                    = encol.getCollationElementIterator(\r
669                                                                       thaistr);\r
670             while (true) {\r
671                 // testing with en since thai has its own tailoring\r
672                 int ce = col5iter.next();\r
673                 int ce2 = encoliter.next();\r
674                 if (ce2 != ce) {\r
675                     errln("! modifier test failed");\r
676                 }\r
677                 if (ce == CollationElementIterator.NULLORDER) {\r
678                     break;\r
679                 }\r
680             }\r
681         } catch (Exception e) {\r
682             errln("RuleBased Collator creation failed for ! modifier.");\r
683             return;\r
684         }\r
685     }\r
686     \r
687     /**\r
688     * This tests the RuleBasedCollator\r
689     * - getRules\r
690     */\r
691     public void TestRules() {\r
692         RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(new Locale("","","")); //root\r
693             // logln("PASS: RuleBased Collator creation passed");\r
694 \r
695     \r
696         String rules = coll.getRules();\r
697         if (rules != null && rules.length() != 0) {\r
698             errln("Root tailored rules failed");\r
699         }\r
700     }\r
701     \r
702     public void TestSafeClone() {\r
703         String test1 = "abCda";\r
704         String test2 = "abcda";\r
705         \r
706         // one default collator & two complex ones \r
707         RuleBasedCollator someCollators[] = {\r
708             (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH),\r
709             (RuleBasedCollator)Collator.getInstance(Locale.KOREA),\r
710             (RuleBasedCollator)Collator.getInstance(Locale.JAPAN)\r
711         };\r
712         RuleBasedCollator someClonedCollators[] = new RuleBasedCollator[3];\r
713         \r
714         // change orig & clone & make sure they are independent \r
715     \r
716         for (int index = 0; index < someCollators.length; index ++)\r
717         {\r
718             try {\r
719                 someClonedCollators[index] \r
720                             = (RuleBasedCollator)someCollators[index].clone();\r
721             } catch (CloneNotSupportedException e) {\r
722                 errln("Error cloning collator");\r
723             }\r
724     \r
725             someClonedCollators[index].setStrength(Collator.TERTIARY);\r
726             someCollators[index].setStrength(Collator.PRIMARY);\r
727             someClonedCollators[index].setCaseLevel(false);\r
728             someCollators[index].setCaseLevel(false);\r
729             \r
730             doAssert(someClonedCollators[index].compare(test1, test2) > 0, \r
731                      "Result should be \"abCda\" >>> \"abcda\" ");\r
732             doAssert(someCollators[index].compare(test1, test2) == 0, \r
733                      "Result should be \"abCda\" == \"abcda\" ");\r
734         }\r
735     }\r
736 \r
737     public void TestGetTailoredSet() \r
738     {\r
739         logln("testing getTailoredSet...");\r
740         String rules[] = {\r
741             "&a < \u212b",             \r
742             "& S < \u0161 <<< \u0160",\r
743         };\r
744         String data[][] = {\r
745             { "\u212b", "A\u030a", "\u00c5" },        \r
746             { "\u0161", "s\u030C", "\u0160", "S\u030C" }            \r
747         };\r
748     \r
749         int i = 0, j = 0;\r
750         \r
751         RuleBasedCollator coll;\r
752         UnicodeSet set;\r
753         \r
754         for(i = 0; i < rules.length; i++) {\r
755             try {\r
756                 logln("Instantiating a collator from "+rules[i]);\r
757                 coll = new RuleBasedCollator(rules[i]);\r
758                 set = coll.getTailoredSet();\r
759                 logln("Got set: "+set.toPattern(true));\r
760                 if(set.size() != data[i].length) {\r
761                     errln("Tailored set size different ("+set.size()+") than expected ("+data[i].length+")");\r
762                 }\r
763                 for(j = 0; j < data[i].length; j++) {\r
764                     logln("Checking to see whether "+data[i][j]+" is in set");\r
765                     if(!set.contains(data[i][j])) {\r
766                         errln("Tailored set doesn't contain "+data[i][j]+"... It should");\r
767                     }\r
768                 }\r
769             } catch (Exception e) {\r
770                 warnln("Couldn't open collator with rules "+ rules[i]);\r
771             }\r
772         }\r
773     }\r
774 \r
775     /** \r
776      * Simple test to see if Collator is subclassable\r
777      */\r
778     public void TestSubClass() \r
779     {\r
780         class TestCollator extends Collator\r
781         {\r
782             public boolean equals(Object that) {\r
783                 return this == that;\r
784             }\r
785     \r
786             public int hashCode() {\r
787                 return 0;\r
788             }\r
789             \r
790             public int compare(String source, String target) {\r
791                 return source.compareTo(target);\r
792             }\r
793             \r
794             public CollationKey getCollationKey(String source)\r
795             {   return new CollationKey(source, \r
796                           getRawCollationKey(source, new RawCollationKey()));\r
797             }\r
798             \r
799             public RawCollationKey getRawCollationKey(String source, \r
800                                                       RawCollationKey key)\r
801             {\r
802                 byte temp1[] = source.getBytes();\r
803                 byte temp2[] = new byte[temp1.length + 1];\r
804                 System.arraycopy(temp1, 0, temp2, 0, temp1.length);\r
805                 temp2[temp1.length] = 0;\r
806                 if (key == null) {\r
807                     key = new RawCollationKey();\r
808                 }\r
809                 key.bytes = temp2; \r
810                 key.size = temp2.length;\r
811                 return key;\r
812             }\r
813             \r
814             public void setVariableTop(int ce)\r
815             {\r
816             }\r
817             \r
818             public int setVariableTop(String str) \r
819             {\r
820                 return 0;\r
821             }\r
822             \r
823             public int getVariableTop()\r
824             {\r
825                 return 0;\r
826             }\r
827             public VersionInfo getVersion()\r
828             {\r
829                 return VersionInfo.getInstance(0);\r
830             }\r
831             public VersionInfo getUCAVersion()\r
832             {\r
833                 return VersionInfo.getInstance(0);\r
834             }\r
835         }\r
836  \r
837         Collator col1 = new TestCollator();\r
838         Collator col2 = new TestCollator();\r
839         if (col1.equals(col2)) {\r
840             errln("2 different instance of TestCollator should fail");\r
841         }\r
842         if (col1.hashCode() != col2.hashCode()) {\r
843             errln("Every TestCollator has the same hashcode");\r
844         }\r
845         String abc = "abc";\r
846         String bcd = "bcd";\r
847         if (col1.compare(abc, bcd) != abc.compareTo(bcd)) {\r
848             errln("TestCollator compare should be the same as the default " +\r
849                   "string comparison");\r
850         }\r
851         CollationKey key = col1.getCollationKey(abc);\r
852         byte temp1[] = abc.getBytes();\r
853         byte temp2[] = new byte[temp1.length + 1];\r
854         System.arraycopy(temp1, 0, temp2, 0, temp1.length);\r
855         temp2[temp1.length] = 0;\r
856         if (!java.util.Arrays.equals(key.toByteArray(), temp2) \r
857             || !key.getSourceString().equals(abc)) {\r
858             errln("TestCollator collationkey API is returning wrong values");\r
859         }\r
860         UnicodeSet set = col1.getTailoredSet();\r
861         if (!set.equals(new UnicodeSet(0, 0x10FFFF))) {\r
862             errln("Error getting default tailored set");\r
863         }\r
864     }\r
865     \r
866      /** \r
867      * Simple test the collator setter and getters\r
868      */\r
869     public void TestSetGet() \r
870     {\r
871         RuleBasedCollator collator = (RuleBasedCollator)Collator.getInstance();\r
872         int decomp = collator.getDecomposition();\r
873         int strength = collator.getStrength();\r
874         boolean alt = collator.isAlternateHandlingShifted();\r
875         boolean caselevel = collator.isCaseLevel();\r
876         boolean french = collator.isFrenchCollation();\r
877         boolean hquart = collator.isHiraganaQuaternary();\r
878         boolean lowercase = collator.isLowerCaseFirst();\r
879         boolean uppercase = collator.isUpperCaseFirst();\r
880         \r
881         collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
882         if (collator.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {\r
883             errln("Setting decomposition failed");\r
884         }\r
885         collator.setStrength(Collator.QUATERNARY);\r
886         if (collator.getStrength() != Collator.QUATERNARY) {\r
887             errln("Setting strength failed");\r
888         }\r
889         collator.setAlternateHandlingShifted(!alt);\r
890         if (collator.isAlternateHandlingShifted() == alt) {\r
891             errln("Setting alternate handling failed");\r
892         }\r
893         collator.setCaseLevel(!caselevel);\r
894         if (collator.isCaseLevel() == caselevel) {\r
895             errln("Setting case level failed");\r
896         }\r
897         collator.setFrenchCollation(!french);\r
898         if (collator.isFrenchCollation() == french) {\r
899             errln("Setting french collation failed");\r
900         }\r
901         collator.setHiraganaQuaternary(!hquart);\r
902         if (collator.isHiraganaQuaternary() == hquart) {\r
903             errln("Setting hiragana quartenary failed");\r
904         }\r
905         collator.setLowerCaseFirst(!lowercase);\r
906         if (collator.isLowerCaseFirst() == lowercase) {\r
907             errln("Setting lower case first failed");\r
908         }\r
909         collator.setUpperCaseFirst(!uppercase);\r
910         if (collator.isUpperCaseFirst() == uppercase) {\r
911             errln("Setting upper case first failed");\r
912         }   \r
913         collator.setDecompositionDefault();\r
914         if (collator.getDecomposition() != decomp) {\r
915             errln("Setting decomposition default failed");\r
916         }\r
917         collator.setStrengthDefault();\r
918         if (collator.getStrength() != strength) {\r
919             errln("Setting strength default failed");\r
920         }\r
921         collator.setAlternateHandlingDefault();\r
922         if (collator.isAlternateHandlingShifted() != alt) {\r
923             errln("Setting alternate handling default failed");\r
924         }\r
925         collator.setCaseLevelDefault();\r
926         if (collator.isCaseLevel() != caselevel) {\r
927             errln("Setting case level default failed");\r
928         }\r
929         collator.setFrenchCollationDefault();\r
930         if (collator.isFrenchCollation() != french) {\r
931             errln("Setting french handling default failed");\r
932         }\r
933         collator.setHiraganaQuaternaryDefault();\r
934         if (collator.isHiraganaQuaternary() != hquart) {\r
935             errln("Setting Hiragana Quartenary default failed");\r
936         }\r
937         collator.setCaseFirstDefault();\r
938         if (collator.isLowerCaseFirst() != lowercase \r
939             || collator.isUpperCaseFirst() != uppercase) {\r
940             errln("Setting case first handling default failed");\r
941         }\r
942     }\r
943     \r
944     public void TestBounds() \r
945     {\r
946         Collator coll = Collator.getInstance(new Locale("sh", ""));\r
947           \r
948         String test[] = { "John Smith", "JOHN SMITH", \r
949                           "john SMITH", "j\u00F6hn sm\u00EFth",\r
950                           "J\u00F6hn Sm\u00EFth", "J\u00D6HN SM\u00CFTH",\r
951                           "john smithsonian", "John Smithsonian",\r
952         };\r
953       \r
954         String testStr[] = {\r
955                           "\u010CAKI MIHALJ", \r
956                           "\u010CAKI MIHALJ",\r
957                           "\u010CAKI PIRO\u0160KA", \r
958                           "\u010CABAI ANDRIJA",\r
959                           "\u010CABAI LAJO\u0160", \r
960                           "\u010CABAI MARIJA",\r
961                           "\u010CABAI STEVAN", \r
962                           "\u010CABAI STEVAN",\r
963                           "\u010CABARKAPA BRANKO", \r
964                           "\u010CABARKAPA MILENKO",\r
965                           "\u010CABARKAPA MIROSLAV", \r
966                           "\u010CABARKAPA SIMO",\r
967                           "\u010CABARKAPA STANKO", \r
968                           "\u010CABARKAPA TAMARA",\r
969                           "\u010CABARKAPA TOMA\u0160", \r
970                           "\u010CABDARI\u0106 NIKOLA",\r
971                           "\u010CABDARI\u0106 ZORICA",\r
972                           "\u010CABI NANDOR",\r
973                           "\u010CABOVI\u0106 MILAN",\r
974                           "\u010CABRADI AGNEZIJA",\r
975                           "\u010CABRADI IVAN",\r
976                           "\u010CABRADI JELENA",\r
977                           "\u010CABRADI LJUBICA",\r
978                           "\u010CABRADI STEVAN",\r
979                           "\u010CABRDA MARTIN",\r
980                           "\u010CABRILO BOGDAN",\r
981                           "\u010CABRILO BRANISLAV",\r
982                           "\u010CABRILO LAZAR",\r
983                           "\u010CABRILO LJUBICA",\r
984                           "\u010CABRILO SPASOJA",\r
985                           "\u010CADE\u0160 ZDENKA",\r
986                           "\u010CADESKI BLAGOJE",\r
987                           "\u010CADOVSKI VLADIMIR",\r
988                           "\u010CAGLJEVI\u0106 TOMA",\r
989                           "\u010CAGOROVI\u0106 VLADIMIR",\r
990                           "\u010CAJA VANKA",\r
991                           "\u010CAJI\u0106 BOGOLJUB",\r
992                           "\u010CAJI\u0106 BORISLAV",\r
993                           "\u010CAJI\u0106 RADOSLAV",\r
994                           "\u010CAK\u0160IRAN MILADIN",\r
995                           "\u010CAKAN EUGEN",\r
996                           "\u010CAKAN EVGENIJE",\r
997                           "\u010CAKAN IVAN",\r
998                           "\u010CAKAN JULIJAN",\r
999                           "\u010CAKAN MIHAJLO",\r
1000                           "\u010CAKAN STEVAN",\r
1001                           "\u010CAKAN VLADIMIR",\r
1002                           "\u010CAKAN VLADIMIR",\r
1003                           "\u010CAKAN VLADIMIR",\r
1004                           "\u010CAKARA ANA",\r
1005                           "\u010CAKAREVI\u0106 MOMIR",\r
1006                           "\u010CAKAREVI\u0106 NEDELJKO",\r
1007                           "\u010CAKI \u0160ANDOR",\r
1008                           "\u010CAKI AMALIJA",\r
1009                           "\u010CAKI ANDRA\u0160",\r
1010                           "\u010CAKI LADISLAV",\r
1011                           "\u010CAKI LAJO\u0160",\r
1012                           "\u010CAKI LASLO" }; \r
1013         \r
1014         CollationKey testKey[] = new CollationKey[testStr.length];\r
1015         for (int i = 0; i < testStr.length; i ++) {\r
1016             testKey[i] = coll.getCollationKey(testStr[i]);\r
1017         }\r
1018         \r
1019         Arrays.sort(testKey);\r
1020         for(int i = 0; i < testKey.length - 1; i ++) {\r
1021             CollationKey lower \r
1022                            = testKey[i].getBound(CollationKey.BoundMode.LOWER,\r
1023                                                  Collator.SECONDARY);\r
1024             for (int j = i + 1; j < testKey.length; j ++) {\r
1025                 CollationKey upper \r
1026                            = testKey[j].getBound(CollationKey.BoundMode.UPPER,\r
1027                                                  Collator.SECONDARY);\r
1028                 for (int k = i; k <= j; k ++) {\r
1029                     if (lower.compareTo(testKey[k]) > 0) {\r
1030                         errln("Problem with lower bound at i = " + i + " j = "\r
1031                               + j + " k = " + k);\r
1032                     }\r
1033                     if (upper.compareTo(testKey[k]) <= 0) {\r
1034                         errln("Problem with upper bound at i = " + i + " j = "\r
1035                               + j + " k = " + k);\r
1036                     }\r
1037                 }\r
1038             }\r
1039         }\r
1040         \r
1041         for (int i = 0; i < test.length; i ++) \r
1042         {\r
1043             CollationKey key = coll.getCollationKey(test[i]);\r
1044             CollationKey lower = key.getBound(CollationKey.BoundMode.LOWER,\r
1045                                               Collator.SECONDARY);\r
1046             CollationKey upper = key.getBound(CollationKey.BoundMode.UPPER_LONG,\r
1047                                               Collator.SECONDARY);\r
1048             for (int j = i + 1; j < test.length; j ++) {\r
1049                 key = coll.getCollationKey(test[j]);\r
1050                 if (lower.compareTo(key) > 0) {\r
1051                     errln("Problem with lower bound i = " + i + " j = " + j);\r
1052                 }\r
1053                 if (upper.compareTo(key) <= 0) {\r
1054                     errln("Problem with upper bound i = " + i + " j = " + j);\r
1055                 }\r
1056             }\r
1057         }\r
1058     }\r
1059     \r
1060     public final void TestGetAll() {\r
1061         Locale[] list = Collator.getAvailableLocales();\r
1062         int errorCount = 0;\r
1063         for (int i = 0; i < list.length; ++i) {\r
1064             log("Locale name: ");\r
1065             log(list[i].toString());\r
1066             log(" , the display name is : ");\r
1067             logln(list[i].getDisplayName());\r
1068             try{\r
1069                 logln("     ...... Or display as: " + Collator.getDisplayName(list[i]));\r
1070                 logln("     ...... and display in Chinese: " + \r
1071                       Collator.getDisplayName(list[i],Locale.CHINA)); \r
1072             }catch(MissingResourceException ex){\r
1073                 errorCount++;\r
1074                 logln("could not get displayName for " + list[i]);\r
1075             }           \r
1076         }\r
1077         if(errorCount>0){\r
1078           warnln("Could not load the locale data.");\r
1079         }\r
1080     }    \r
1081 \r
1082     private void\r
1083     doSetsTest(UnicodeSet ref, UnicodeSet set, String inSet, String outSet) {\r
1084         \r
1085         set.clear();\r
1086         set.applyPattern(inSet);\r
1087         \r
1088         if(!ref.containsAll(set)) {\r
1089             err("Some stuff from "+inSet+" is not present in the set\n");            \r
1090         }\r
1091         \r
1092         set.clear();\r
1093         set.applyPattern(outSet);\r
1094         if(!ref.containsNone(set)) {\r
1095             err("Some stuff from "+outSet+" is present in the set\n");\r
1096         }\r
1097     }\r
1098     \r
1099     public void TestGetContractions()throws Exception {\r
1100         /*        static struct {\r
1101          const char* locale;\r
1102          const char* inConts;\r
1103          const char* outConts;\r
1104          const char* inExp;\r
1105          const char* outExp;\r
1106          const char* unsafeCodeUnits;\r
1107          const char* safeCodeUnits;\r
1108          }\r
1109          */\r
1110         String tests[][] = {\r
1111                 { "ru", \r
1112                     "[{\u0474\u030F}{\u0475\u030F}{\u04D8\u0308}{\u04D9\u0308}{\u04E8\u0308}{\u04E9\u0308}]", \r
1113                     "[{\u0430\u0306}{\u0410\u0306}{\u0430\u0308}{\u0410\u0306}{\u0433\u0301}{\u0413\u0301}]",\r
1114                     "[\u00e6]",\r
1115                     "[a]",\r
1116                     "[\u0474\u0475\u04d8\u04d9\u04e8\u04e9]",\r
1117                     "[aAbB\u0430\u0410\u0433\u0413]"\r
1118                 },\r
1119                 { "uk",\r
1120                     "[{\u0474\u030F}{\u0475\u030F}{\u04D8\u0308}{\u04D9\u0308}{\u04E8\u0308}{\u04E9\u0308}"+ \r
1121                     "{\u0430\u0306}{\u0410\u0306}{\u0430\u0308}{\u0410\u0306}{\u0433\u0301}{\u0413\u0301}]",\r
1122                     "[]",\r
1123                     "[\u00e6]",\r
1124                     "[a]",\r
1125                     "[\u0474\u0475\u04D8\u04D9\u04E8\u04E9\u0430\u0410\u0433\u0413]",\r
1126                     "[aAbBxv]",\r
1127                 },\r
1128                 { "sh",\r
1129                     "[{C\u0301}{C\u030C}{C\u0341}{DZ\u030C}{Dz\u030C}{D\u017D}{D\u017E}{lj}{nj}]",\r
1130                     "[{\u309d\u3099}{\u30fd\u3099}]",\r
1131                     "[\u00e6]",\r
1132                     "[a]",\r
1133                     "[nlcdzNLCDZ]",\r
1134                     "[jabv]"\r
1135                 },\r
1136                 { "ja",\r
1137                     "[{\u3053\u3099\u309D}{\u3053\u3099\u309D\u3099}{\u3053\u3099\u309E}{\u3053\u3099\u30FC}{\u3053\u309D}{\u3053\u309D\u3099}{\u3053\u309E}{\u3053\u30FC}{\u30B3\u3099\u30FC}{\u30B3\u3099\u30FD}{\u30B3\u3099\u30FD\u3099}{\u30B3\u3099\u30FE}{\u30B3\u30FC}{\u30B3\u30FD}{\u30B3\u30FD\u3099}{\u30B3\u30FE}]",\r
1138                     "[{\u30FD\u3099}{\u309D\u3099}{\u3053\u3099}{\u30B3\u3099}{lj}{nj}]",\r
1139                     "[\u30FE\u00e6]",\r
1140                     "[a]",\r
1141                     "[\u3099]",\r
1142                     "[]"\r
1143                 }\r
1144         };\r
1145         \r
1146         \r
1147         \r
1148         \r
1149         RuleBasedCollator coll = null;\r
1150         int i = 0;\r
1151         UnicodeSet conts = new UnicodeSet();\r
1152         UnicodeSet exp = new UnicodeSet();\r
1153         UnicodeSet set = new UnicodeSet();\r
1154         \r
1155         for(i = 0; i < tests.length; i++) {\r
1156             logln("Testing locale: "+ tests[i][0]);\r
1157             coll = (RuleBasedCollator)Collator.getInstance(new ULocale(tests[i][0]));\r
1158             coll.getContractionsAndExpansions(conts, exp, true);\r
1159             logln("Contractions "+conts.size()+":\n"+conts.toPattern(true));\r
1160             doSetsTest(conts, set, tests[i][1], tests[i][2]);\r
1161             logln("Expansions "+exp.size()+":\n"+exp.toPattern(true));\r
1162             doSetsTest(exp, set, tests[i][3], tests[i][4]);\r
1163             \r
1164             // No unsafe set in ICU4J\r
1165             //noConts = ucol_getUnsafeSet(coll, conts, &status);\r
1166             //doSetsTest(conts, set, tests[i][5], tests[i][6]);\r
1167             //log_verbose("Unsafes "+conts.size()+":\n"+conts.toPattern(true)+"\n");\r
1168         }\r
1169     }\r
1170     private static final String bigone = "One";\r
1171     private static final String littleone = "one";\r
1172     \r
1173     public void TestClone() {\r
1174         logln("\ninit c0");\r
1175         RuleBasedCollator c0 = (RuleBasedCollator)Collator.getInstance();\r
1176         c0.setStrength(Collator.TERTIARY);\r
1177         dump("c0", c0);\r
1178 \r
1179         logln("\ninit c1");\r
1180         RuleBasedCollator c1 = (RuleBasedCollator)Collator.getInstance();\r
1181         c1.setStrength(Collator.TERTIARY);\r
1182         c1.setUpperCaseFirst(!c1.isUpperCaseFirst());\r
1183         dump("c0", c0);\r
1184         dump("c1", c1);\r
1185         try{\r
1186             logln("\ninit c2");\r
1187             RuleBasedCollator c2 = (RuleBasedCollator)c1.clone();\r
1188             c2.setUpperCaseFirst(!c2.isUpperCaseFirst());\r
1189             dump("c0", c0);\r
1190             dump("c1", c1);\r
1191             dump("c2", c2);\r
1192             if(c1.equals(c2)){\r
1193                 errln("The cloned objects refer to same data");\r
1194             }\r
1195         }catch(CloneNotSupportedException ex){\r
1196             errln("Could not clone the collator");\r
1197         }\r
1198     }\r
1199 \r
1200     private void dump(String msg, RuleBasedCollator c) {\r
1201         logln(msg + " " + c.compare(bigone, littleone) +\r
1202                            " s: " + c.getStrength() +\r
1203                            " u: " + c.isUpperCaseFirst());\r
1204     }\r
1205 }\r