2 *******************************************************************************
\r
3 * Copyright (C) 2002-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : collate/CollationAPITest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/apicoll.cpp
\r
13 package com.ibm.icu.dev.test.collator;
\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
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
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
39 public void TestCollationKey() {
\r
40 logln("testing CollationKey begins...");
\r
41 Collator col = Collator.getInstance();
\r
42 col.setStrength(Collator.TERTIARY);
\r
44 String test1 = "Abcda";
\r
45 String test2 = "abcda";
\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
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
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
65 CollationKey sortkNew;
\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
73 // check invaliad comparisons
\r
74 Object fake = "fake";
\r
76 sortk1.compareTo(fake);
\r
77 errln("Non-CollationKey comparison");
\r
79 catch (Exception e) {
\r
80 logln("PASS: Non-CollationKey comparison failed as expected");
\r
82 if (sortk1.equals(fake)) {
\r
83 errln("Non-CollationKey comparison");
\r
86 // port from apicoll
\r
88 col = Collator.getInstance();
\r
89 } catch (Exception e) {
\r
90 errln("Collator.getInstance() failed");
\r
92 if (col.getStrength() != Collator.TERTIARY){
\r
93 errln("Default collation did not have tertiary strength");
\r
96 // Need to use identical strength
\r
97 col.setStrength(Collator.IDENTICAL);
\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
108 // 3.6 key, UCA 5.0
\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
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
130 0x26, 0x28, 0x2A, 0x2C, 0x26, 0x01,
\r
131 0x09, 0x01, 0x09, 0x01, 0x25, 0x01,
\r
132 0x92, 0x93, 0x94, 0x95, 0x92, 0x00
\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
144 0x19, 0x1B, 0x1D, 0x1F, 0x19,
\r
145 0x01, 0x09, 0x01, 0x09, 0x01,
\r
147 0x92, 0x93, 0x94, 0x95, 0x92,
\r
153 0x19, 0x1B, 0x1D, 0x1F, 0x19,
\r
154 0x01, 0x0A, 0x01, 0x0A, 0x01,
\r
155 0x92, 0x93, 0x94, 0x95, 0x92,
\r
160 CollationKey key1 = col.getCollationKey(test1);
\r
161 CollationKey key2 = col.getCollationKey(test2);
\r
162 CollationKey key3 = col.getCollationKey(test2);
\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
171 doAssert(Arrays.equals(key2.toByteArray(), key2compat),
\r
172 "Binary format for 'abcda' sortkey different for identical strength!");
\r
174 logln("Use secondary comparision level testing ...");
\r
175 col.setStrength(Collator.SECONDARY);
\r
177 key1 = col.getCollationKey(test1);
\r
178 key2 = col.getCollationKey(test2);
\r
179 key3 = col.getCollationKey(test2);
\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
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
193 logln("testing sortkey ends...");
\r
196 public void TestRawCollationKey()
\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
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
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
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
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
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
237 void doAssert(boolean conditions, String message) {
\r
244 * This tests the comparison convenience methods of a collator object.
\r
246 * - greater than or equal to
\r
249 public void TestCompare() {
\r
250 logln("The compare tests begin : ");
\r
251 Collator col = Collator.getInstance(Locale.ENGLISH);
\r
253 String test1 = "Abcda";
\r
254 String test2 = "abcda";
\r
255 logln("Use tertiary comparison level testing ....");
\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
263 col.setStrength(Collator.SECONDARY);
\r
264 logln("Use secondary comparison level testing ....");
\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
270 col.setStrength(Collator.PRIMARY);
\r
271 logln("Use primary comparison level testing ....");
\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
278 Integer die = new Integer(1);
\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
288 * Tests decomposition setting
\r
290 public void TestDecomposition() {
\r
291 Collator en_US = null, el_GR = null, vi_VN = null;
\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
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
301 errln("vi_VN collation did not have cannonical decomposition for normalization!");
\r
304 if (el_GR.getDecomposition() != Collator.CANONICAL_DECOMPOSITION)
\r
306 errln("el_GR collation did not have cannonical decomposition for normalization!");
\r
309 if (en_US.getDecomposition() != Collator.NO_DECOMPOSITION)
\r
311 errln("en_US collation had cannonical decomposition for normalization!");
\r
316 * This tests the duplication of a collator object.
\r
318 public void TestDuplicate() {
\r
319 //Clone does not be implemented
\r
320 Collator col1 = Collator.getInstance(Locale.ENGLISH);
\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
327 col3 = new RuleBasedCollator(ruleset);
\r
328 } catch (Exception e) {
\r
329 errln("Failure creating RuleBasedCollator with rule:" + ruleset);
\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
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
346 public void TestElemIter() {
\r
347 // logln("testing sortkey begins...");
\r
348 Collator col = Collator.getInstance(Locale.ENGLISH);
\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
356 CharacterIterator chariter=new StringCharacterIterator(testString1);
\r
358 CollationElementIterator iterator2 = ((RuleBasedCollator)col).getCollationElementIterator(chariter);
\r
359 UCharacterIterator uchariter=UCharacterIterator.getInstance(testString2);
\r
360 CollationElementIterator iterator3 = ((RuleBasedCollator)col).getCollationElementIterator(uchariter);
\r
363 offset = iterator1.getOffset();
\r
365 errln("Error in getOffset for collation element iterator");
\r
368 iterator1.setOffset(6);
\r
369 iterator1.setOffset(0);
\r
370 int order1, order2, order3;
\r
372 order1 = iterator1.next();
\r
373 doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
\r
374 order2 = iterator2.next();
\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
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
387 order1 = iterator1.next();
\r
388 order3 = iterator3.next();
\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
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
401 doAssert((order1 != CollationElementIterator.NULLORDER), "Unexpected end of iterator reached");
\r
403 iterator1.reset();
\r
404 iterator2.reset();
\r
406 order1 = iterator1.next();
\r
408 doAssert(!(iterator1.equals(iterator2)), "The first iterator advance failed");
\r
410 order2 = iterator2.next();
\r
412 doAssert((iterator1.equals(iterator2)), "The second iterator advance failed");
\r
413 doAssert((order1 == order2), "The order result should be the same");
\r
415 order3 = iterator3.next();
\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
424 order1 = iterator1.next();
\r
425 order2 = iterator2.next();
\r
426 order3 = iterator3.next();
\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
433 order1 = iterator1.next();
\r
434 order3 = iterator3.next();
\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
445 * This tests the hashCode method of a collator object.
\r
447 public void TestHashCode() {
\r
448 logln("hashCode tests begin.");
\r
449 Collator col1 = Collator.getInstance(Locale.ENGLISH);
\r
451 Collator col2 = null;
\r
452 Locale dk = new Locale("da", "DK", "");
\r
454 col2 = Collator.getInstance(dk);
\r
455 } catch (Exception e) {
\r
456 errln("Danish collation creation failed.");
\r
460 Collator col3 = null;
\r
462 col3 = Collator.getInstance(Locale.ENGLISH);
\r
463 } catch (Exception e) {
\r
464 errln("2nd default collation creation failed.");
\r
468 logln("Collator.hashCode() testing ...");
\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
474 logln("hashCode tests end.");
\r
476 String test1 = "Abcda";
\r
477 String test2 = "abcda";
\r
479 CollationKey sortk1, sortk2, sortk3;
\r
481 sortk1 = col3.getCollationKey(test1);
\r
482 sortk2 = col3.getCollationKey(test2);
\r
483 sortk3 = col3.getCollationKey(test2);
\r
485 doAssert(sortk1.hashCode() != sortk2.hashCode(), "Hash test1 result incorrect");
\r
486 doAssert(sortk2.hashCode() == sortk3.hashCode(), "Hash result not equal" );
\r
490 * This tests the properties of a collator object.
\r
492 * - factory method getInstance
\r
493 * - compare and getCollationKey
\r
494 * - get/set decomposition mode and comparison level
\r
496 public void TestProperty() {
\r
498 All the collations have the same version in an ICU
\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
504 logln("The property tests begin : ");
\r
505 logln("Test ctors : ");
\r
506 Collator col = Collator.getInstance(Locale.ENGLISH);
\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
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
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
523 logln("Test ctors ends.");
\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
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
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
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
545 col = Collator.getInstance(Locale.FRENCH);
\r
546 } catch (Exception e) {
\r
547 errln("Creating French collation failed.");
\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
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
563 public void TestJunkCollator(){
\r
564 logln("Create junk collation: ");
\r
565 Locale abcd = new Locale("ab", "CD", "");
\r
567 Collator junk = Collator.getInstance(abcd);
\r
568 Collator col = Collator.getInstance();
\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
577 frCol = Collator.getInstance(Locale.FRANCE);
\r
578 } catch (Exception e) {
\r
579 errln("Creating French collator failed.");
\r
583 doAssert(!(frCol.equals(junk)), "The junk is the same as the French collator.");
\r
584 logln("Collator property test ended.");
\r
588 * This tests the RuleBasedCollator
\r
589 * - constructor/destructor
\r
592 public void TestRuleBasedColl() {
\r
593 RuleBasedCollator col1 = null, col2 = null, col3 = null, col4 = null;
\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
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
608 col2 = new RuleBasedCollator(ruleset2);
\r
609 } catch (Exception e) {
\r
610 errln("RuleBased Collator creation failed.");
\r
615 // empty rules fail
\r
616 col3 = new RuleBasedCollator(ruleset3);
\r
617 errln("Failure: Empty rules for the collator should fail");
\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
625 Locale locale = new Locale("aa", "AA");
\r
627 col3 = (RuleBasedCollator)Collator.getInstance(locale);
\r
628 } catch (Exception e) {
\r
629 errln("Fallback Collator creation failed.: %s");
\r
634 col3 = (RuleBasedCollator)Collator.getInstance();
\r
635 } catch (Exception e) {
\r
636 errln("Default Collator creation failed.: %s");
\r
640 String rule1 = col1.getRules();
\r
641 String rule2 = col2.getRules();
\r
642 String rule3 = col3.getRules();
\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
649 col4 = new RuleBasedCollator(rule2);
\r
650 } catch (Exception e) {
\r
651 errln("RuleBased Collator creation failed.");
\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
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
671 // testing with en since thai has its own tailoring
\r
672 int ce = col5iter.next();
\r
673 int ce2 = encoliter.next();
\r
675 errln("! modifier test failed");
\r
677 if (ce == CollationElementIterator.NULLORDER) {
\r
681 } catch (Exception e) {
\r
682 errln("RuleBased Collator creation failed for ! modifier.");
\r
688 * This tests the RuleBasedCollator
\r
691 public void TestRules() {
\r
692 RuleBasedCollator coll = (RuleBasedCollator)Collator.getInstance(new Locale("","","")); //root
\r
693 // logln("PASS: RuleBased Collator creation passed");
\r
696 String rules = coll.getRules();
\r
697 if (rules != null && rules.length() != 0) {
\r
698 errln("Root tailored rules failed");
\r
702 public void TestSafeClone() {
\r
703 String test1 = "abCda";
\r
704 String test2 = "abcda";
\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
712 RuleBasedCollator someClonedCollators[] = new RuleBasedCollator[3];
\r
714 // change orig & clone & make sure they are independent
\r
716 for (int index = 0; index < someCollators.length; index ++)
\r
719 someClonedCollators[index]
\r
720 = (RuleBasedCollator)someCollators[index].clone();
\r
721 } catch (CloneNotSupportedException e) {
\r
722 errln("Error cloning collator");
\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
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
737 public void TestGetTailoredSet()
\r
739 logln("testing getTailoredSet...");
\r
742 "& S < \u0161 <<< \u0160",
\r
744 String data[][] = {
\r
745 { "\u212b", "A\u030a", "\u00c5" },
\r
746 { "\u0161", "s\u030C", "\u0160", "S\u030C" }
\r
751 RuleBasedCollator coll;
\r
754 for(i = 0; i < rules.length; i++) {
\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
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
769 } catch (Exception e) {
\r
770 warnln("Couldn't open collator with rules "+ rules[i]);
\r
776 * Simple test to see if Collator is subclassable
\r
778 public void TestSubClass()
\r
780 class TestCollator extends Collator
\r
782 public boolean equals(Object that) {
\r
783 return this == that;
\r
786 public int hashCode() {
\r
790 public int compare(String source, String target) {
\r
791 return source.compareTo(target);
\r
794 public CollationKey getCollationKey(String source)
\r
795 { return new CollationKey(source,
\r
796 getRawCollationKey(source, new RawCollationKey()));
\r
799 public RawCollationKey getRawCollationKey(String source,
\r
800 RawCollationKey key)
\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
807 key = new RawCollationKey();
\r
809 key.bytes = temp2;
\r
810 key.size = temp2.length;
\r
814 public void setVariableTop(int ce)
\r
818 public int setVariableTop(String str)
\r
823 public int getVariableTop()
\r
827 public VersionInfo getVersion()
\r
829 return VersionInfo.getInstance(0);
\r
831 public VersionInfo getUCAVersion()
\r
833 return VersionInfo.getInstance(0);
\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
842 if (col1.hashCode() != col2.hashCode()) {
\r
843 errln("Every TestCollator has the same hashcode");
\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
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
860 UnicodeSet set = col1.getTailoredSet();
\r
861 if (!set.equals(new UnicodeSet(0, 0x10FFFF))) {
\r
862 errln("Error getting default tailored set");
\r
867 * Simple test the collator setter and getters
\r
869 public void TestSetGet()
\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
881 collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
882 if (collator.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
\r
883 errln("Setting decomposition failed");
\r
885 collator.setStrength(Collator.QUATERNARY);
\r
886 if (collator.getStrength() != Collator.QUATERNARY) {
\r
887 errln("Setting strength failed");
\r
889 collator.setAlternateHandlingShifted(!alt);
\r
890 if (collator.isAlternateHandlingShifted() == alt) {
\r
891 errln("Setting alternate handling failed");
\r
893 collator.setCaseLevel(!caselevel);
\r
894 if (collator.isCaseLevel() == caselevel) {
\r
895 errln("Setting case level failed");
\r
897 collator.setFrenchCollation(!french);
\r
898 if (collator.isFrenchCollation() == french) {
\r
899 errln("Setting french collation failed");
\r
901 collator.setHiraganaQuaternary(!hquart);
\r
902 if (collator.isHiraganaQuaternary() == hquart) {
\r
903 errln("Setting hiragana quartenary failed");
\r
905 collator.setLowerCaseFirst(!lowercase);
\r
906 if (collator.isLowerCaseFirst() == lowercase) {
\r
907 errln("Setting lower case first failed");
\r
909 collator.setUpperCaseFirst(!uppercase);
\r
910 if (collator.isUpperCaseFirst() == uppercase) {
\r
911 errln("Setting upper case first failed");
\r
913 collator.setDecompositionDefault();
\r
914 if (collator.getDecomposition() != decomp) {
\r
915 errln("Setting decomposition default failed");
\r
917 collator.setStrengthDefault();
\r
918 if (collator.getStrength() != strength) {
\r
919 errln("Setting strength default failed");
\r
921 collator.setAlternateHandlingDefault();
\r
922 if (collator.isAlternateHandlingShifted() != alt) {
\r
923 errln("Setting alternate handling default failed");
\r
925 collator.setCaseLevelDefault();
\r
926 if (collator.isCaseLevel() != caselevel) {
\r
927 errln("Setting case level default failed");
\r
929 collator.setFrenchCollationDefault();
\r
930 if (collator.isFrenchCollation() != french) {
\r
931 errln("Setting french handling default failed");
\r
933 collator.setHiraganaQuaternaryDefault();
\r
934 if (collator.isHiraganaQuaternary() != hquart) {
\r
935 errln("Setting Hiragana Quartenary default failed");
\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
944 public void TestBounds()
\r
946 Collator coll = Collator.getInstance(new Locale("sh", ""));
\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
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
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
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
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
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
1033 if (upper.compareTo(testKey[k]) <= 0) {
\r
1034 errln("Problem with upper bound at i = " + i + " j = "
\r
1035 + j + " k = " + k);
\r
1041 for (int i = 0; i < test.length; i ++)
\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
1053 if (upper.compareTo(key) <= 0) {
\r
1054 errln("Problem with upper bound i = " + i + " j = " + j);
\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
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
1074 logln("could not get displayName for " + list[i]);
\r
1078 warnln("Could not load the locale data.");
\r
1083 doSetsTest(UnicodeSet ref, UnicodeSet set, String inSet, String outSet) {
\r
1086 set.applyPattern(inSet);
\r
1088 if(!ref.containsAll(set)) {
\r
1089 err("Some stuff from "+inSet+" is not present in the set\n");
\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
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
1110 String tests[][] = {
\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
1116 "[\u0474\u0475\u04d8\u04d9\u04e8\u04e9]",
\r
1117 "[aAbB\u0430\u0410\u0433\u0413]"
\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
1125 "[\u0474\u0475\u04D8\u04D9\u04E8\u04E9\u0430\u0410\u0433\u0413]",
\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
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
1149 RuleBasedCollator coll = null;
\r
1151 UnicodeSet conts = new UnicodeSet();
\r
1152 UnicodeSet exp = new UnicodeSet();
\r
1153 UnicodeSet set = new UnicodeSet();
\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
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
1170 private static final String bigone = "One";
\r
1171 private static final String littleone = "one";
\r
1173 public void TestClone() {
\r
1174 logln("\ninit c0");
\r
1175 RuleBasedCollator c0 = (RuleBasedCollator)Collator.getInstance();
\r
1176 c0.setStrength(Collator.TERTIARY);
\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
1186 logln("\ninit c2");
\r
1187 RuleBasedCollator c2 = (RuleBasedCollator)c1.clone();
\r
1188 c2.setUpperCaseFirst(!c2.isUpperCaseFirst());
\r
1192 if(c1.equals(c2)){
\r
1193 errln("The cloned objects refer to same data");
\r
1195 }catch(CloneNotSupportedException ex){
\r
1196 errln("Could not clone the collator");
\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