2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : Collate/CollationDummyTest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/allcoll.cpp
\r
11 * $ICU4CRoot/source/test/cintltst/callcoll.c
\r
14 package com.ibm.icu.dev.test.collator;
\r
16 import java.util.Locale;
\r
18 import com.ibm.icu.dev.test.TestFmwk;
\r
19 import com.ibm.icu.text.CollationElementIterator;
\r
20 import com.ibm.icu.text.CollationKey;
\r
21 import com.ibm.icu.text.Collator;
\r
22 import com.ibm.icu.text.Normalizer;
\r
23 import com.ibm.icu.text.RuleBasedCollator;
\r
25 public class CollationDummyTest extends TestFmwk {
\r
26 public static void main(String[] args) throws Exception {
\r
27 new CollationDummyTest().run(args);
\r
28 // new CollationDummyTest().TestVariableTop();
\r
31 //testSourceCases[][] and testTargetCases[][], testCases[][] are ported from the file callcoll.c in icu4c
\r
32 private static char[][] testSourceCases = {
\r
33 {0x61, 0x62, 0x27, 0x63},
\r
34 {0x63, 0x6f, 0x2d, 0x6f, 0x70},
\r
36 {0x61, 0x6d, 0x70, 0x65, 0x72, 0x73, 0x61, 0x64},
\r
38 {0x66, 0x6f, 0x75, 0x72},
\r
39 {0x66, 0x69, 0x76, 0x65},
\r
45 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
\r
49 {0x61, 0x63, 0x48, 0x63}, // simple test
\r
50 {0x70, 0x00EA, 0x63, 0x68, 0x65},
\r
52 {0x61, 0x62, 0x63}, // 20
\r
56 {0x61, 0x00E6, 0x63},
\r
57 {0x61, 0x63, 0x48, 0x63}, // primary test
\r
58 {0x62, 0x6c, 0x61, 0x63, 0x6b},
\r
59 {0x66, 0x6f, 0x75, 0x72},
\r
60 {0x66, 0x69, 0x76, 0x65},
\r
62 {0x61, 0x62, 0x63}, // 30
\r
63 {0x61, 0x62, 0x63},
\r
64 {0x61, 0x62, 0x63, 0x48},
\r
66 {0x61, 0x63, 0x48, 0x63}, // 34
\r
67 {0x61, 0x63, 0x65, 0x30},
\r
69 {0x70, 0x00EA,0x30} // 37
\r
72 private static char[][] testTargetCases = {
\r
73 {0x61, 0x62, 0x63, 0x27},
\r
74 {0x43, 0x4f, 0x4f, 0x50},
\r
82 {0x70, 0x6e, 0x65}, // 10
\r
85 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
\r
86 {0x61, 0x3c, 0x3d, 0x62},
\r
88 {0x61, 0x43, 0x48, 0x63},
\r
89 {0x61, 0x43, 0x48, 0x63}, // simple test
\r
90 {0x70, 0x00E9, 0x63, 0x68, 0x00E9},
\r
92 {0x61, 0x42, 0x43}, // 20
\r
93 {0x61, 0x62, 0x63, 0x68},
\r
95 {0x00E4, 0x62, 0x63},
\r
96 {0x61, 0x00C6, 0x63},
\r
97 {0x61, 0x43, 0x48, 0x63}, // primary test
\r
98 {0x62, 0x6c, 0x61, 0x63, 0x6b, 0x2d, 0x62, 0x69, 0x72, 0x64},
\r
101 {0x6f, 0x6e, 0x65},
\r
102 {0x61, 0x62, 0x63},
\r
103 {0x61, 0x42, 0x63}, // 30
\r
104 {0x61, 0x62, 0x63, 0x68},
\r
105 {0x61, 0x62, 0x64},
\r
106 {0x61, 0x43, 0x48, 0x63}, // 34
\r
107 {0x61, 0x63, 0x65, 0x30},
\r
109 {0x70, 0x00EB,0x30} // 37
\r
112 private static char[][] testCases = {
\r
130 -1, //Collator::GREATER,
\r
146 // test primary > 17
\r
156 // test secondary > 26
\r
170 final int MAX_TOKEN_LEN = 16;
\r
172 public RuleBasedCollator myCollation;
\r
174 public CollationDummyTest() {
\r
176 protected void init() throws Exception{
\r
177 String ruleset = "& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
\r
178 // String ruleset = "& Four, 4";
\r
179 myCollation = null;
\r
180 myCollation = new RuleBasedCollator(ruleset);
\r
183 // perform test with strength tertiary
\r
184 public void TestTertiary() {
\r
186 myCollation.setStrength(Collator.TERTIARY);
\r
187 for (i = 0; i < 17 ; i++) {
\r
188 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
192 // perform test with strength PRIMARY
\r
193 public void TestPrimary() {
\r
194 // problem in strcollinc for unfinshed contractions
\r
195 myCollation.setStrength(Collator.PRIMARY);
\r
196 for (int i = 17; i < 26 ; i++) {
\r
197 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
201 //perform test with strength SECONDARY
\r
202 public void TestSecondary() {
\r
204 myCollation.setStrength(Collator.SECONDARY);
\r
205 for (i = 26; i < 34; i++) {
\r
206 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
210 // perform extra tests
\r
211 public void TestExtra() {
\r
213 myCollation.setStrength(Collator.TERTIARY);
\r
214 for (i = 0; i < testCases.length - 1; i++) {
\r
215 for (j = i + 1; j < testCases.length; j += 1) {
\r
216 doTest(myCollation, testCases[i], testCases[j], -1);
\r
221 public void TestIdentical() {
\r
223 myCollation.setStrength(Collator.IDENTICAL);
\r
224 for (i= 34; i<37; i++) {
\r
225 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
229 public void TestJB581() {
\r
230 String source = "THISISATEST.";
\r
231 String target = "Thisisatest.";
\r
232 Collator coll = null;
\r
234 coll = Collator.getInstance(Locale.ENGLISH);
\r
235 } catch (Exception e) {
\r
236 errln("ERROR: Failed to create the collator for : en_US\n");
\r
240 int result = coll.compare(source, target);
\r
241 // result is 1, secondary differences only for ignorable space characters
\r
243 errln("Comparing two strings with only secondary differences in C failed.\n");
\r
247 // To compare them with just primary differences
\r
248 coll.setStrength(Collator.PRIMARY);
\r
249 result = coll.compare(source, target);
\r
252 errln("Comparing two strings with no differences in C failed.\n");
\r
256 // Now, do the same comparison with keys
\r
257 CollationKey sourceKeyOut, targetKeyOut;
\r
258 sourceKeyOut = coll.getCollationKey(source);
\r
259 targetKeyOut = coll.getCollationKey(target);
\r
260 result = sourceKeyOut.compareTo(targetKeyOut);
\r
262 errln("Comparing two strings with sort keys in C failed.\n");
\r
267 //TestSurrogates() is ported from cintltst/callcoll.c
\r
270 * Tests surrogate support.
\r
272 public void TestSurrogates()
\r
274 String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";
\r
275 String source[] = {"z",
\r
277 "\ud800\udc0a\u0308",
\r
281 String target[] = {"\uD800\uDC00",
\r
282 "\ud800\udc0a\u0308",
\r
287 // this test is to verify the supplementary sort key order in the english
\r
289 Collator enCollation;
\r
291 enCollation = Collator.getInstance(Locale.ENGLISH);
\r
292 } catch (Exception e) {
\r
293 errln("ERROR: Failed to create the collator for ENGLISH");
\r
297 myCollation.setStrength(Collator.TERTIARY);
\r
299 // logln("start of english collation supplementary characters test\n");
\r
300 while (count < 2) {
\r
301 doTest(enCollation, source[count], target[count], -1);
\r
304 doTest(enCollation, source[count], target[count], 1);
\r
306 // logln("start of tailored collation supplementary characters test\n");
\r
308 Collator newCollation;
\r
310 newCollation = new RuleBasedCollator(rules);
\r
311 } catch (Exception e) {
\r
312 errln("ERROR: Failed to create the collator for rules");
\r
316 // tests getting collation elements for surrogates for tailored rules
\r
317 while (count < 4) {
\r
318 doTest(newCollation, source[count], target[count], -1);
\r
322 // tests that \uD801\uDC01 still has the same value, not changed
\r
323 CollationKey enKey = enCollation.getCollationKey(source[3]);
\r
324 CollationKey newKey = newCollation.getCollationKey(source[3]);
\r
325 int keyResult = enKey.compareTo(newKey);
\r
326 if(keyResult != 0) {
\r
327 errln("Failed : non-tailored supplementary characters should have the same value\n");
\r
331 //TestVariableTop() is ported from cintltst/callcoll.c
\r
333 * Tests the [variable top] tag in rule syntax. Since the default [alternate]
\r
334 * tag has the value shifted, any codepoints before [variable top] should give
\r
335 * a primary ce of 0.
\r
337 public void TestVariableTop() {
\r
338 String rule = "&z = [variable top]";
\r
341 char[] source = new char[1];
\r
343 int expected[] = {0};
\r
346 enColl = Collator.getInstance(Locale.ENGLISH);
\r
347 } catch (Exception e) {
\r
348 errln("ERROR: Failed to create the collator for ENGLISH");
\r
353 myColl = new RuleBasedCollator(rule);
\r
354 } catch(Exception e){
\r
355 errln("Fail to create RuleBasedCollator with rules:" + rule);
\r
358 enColl.setStrength(Collator.PRIMARY);
\r
359 myColl.setStrength(Collator.PRIMARY);
\r
361 ((RuleBasedCollator)enColl).setAlternateHandlingShifted(true);
\r
362 ((RuleBasedCollator)myColl).setAlternateHandlingShifted(true);
\r
364 if(((RuleBasedCollator)enColl).isAlternateHandlingShifted() != true) {
\r
365 errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");
\r
368 // space is supposed to be a variable
\r
369 CollationKey key = enColl.getCollationKey(" ");
\r
370 byte[] result = key.toByteArray();
\r
372 for(int i = 0; i < result.length; i++) {
\r
373 if(result[i]!= expected[i]) {
\r
374 errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
\r
382 key = myColl.getCollationKey(new String(source));
\r
383 result = key.toByteArray();
\r
385 for(int i = 0; i < result.length; i++) {
\r
386 if(result[i]!= expected[i]) {
\r
387 errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
\r
395 public void TestJB1401() {
\r
396 Collator myCollator = null;
\r
397 char[] NFD_UnsafeStartChars = {
\r
398 0x0f73, // Tibetan Vowel Sign II
\r
399 0x0f75, // Tibetan Vowel Sign UU
\r
400 0x0f81, // Tibetan Vowel Sign Reversed II
\r
406 myCollator = Collator.getInstance(Locale.ENGLISH);
\r
407 } catch(Exception e) {
\r
408 errln("ERROR: Failed to create the collator for ENGLISH");
\r
411 myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
413 // Get the next funny character to be tested, and set up the
\r
414 // three test strings X, Y, Z, consisting of an A-grave + test char,
\r
415 // in original form, NFD, and then NFC form.
\r
416 char c = NFD_UnsafeStartChars[i];
\r
419 String x = "\u00C0" + c; // \u00C0 is A Grave
\r
424 y = Normalizer.decompose(x, false);
\r
425 z = Normalizer.decompose(y, true);
\r
426 } catch (Exception e) {
\r
427 errln("ERROR: Failed to normalize test of character" + c);
\r
431 // Collation test. All three strings should be equal.
\r
432 // doTest does both strcoll and sort keys, with params in both orders.
\r
433 doTest(myCollator, x, y, 0);
\r
434 doTest(myCollator, x, z, 0);
\r
435 doTest(myCollator, y, z, 0);
\r
437 // Run collation element iterators over the three strings. Results should be same for each.
\r
440 CollationElementIterator ceiX, ceiY, ceiZ;
\r
444 ceiX = ((RuleBasedCollator)myCollator).getCollationElementIterator(x);
\r
445 ceiY = ((RuleBasedCollator)myCollator).getCollationElementIterator(y);
\r
446 ceiZ = ((RuleBasedCollator)myCollator).getCollationElementIterator(z);
\r
447 } catch(Exception e) {
\r
448 errln("ERROR: getCollationElementIterator failed");
\r
457 } catch (Exception e) {
\r
458 errln("ERROR: CollationElementIterator.next failed for iteration " + j);
\r
462 if (ceX != ceY || ceY != ceZ) {
\r
463 errln("ERROR: ucol_next failed for iteration " + j);
\r
466 if (ceX == CollationElementIterator.NULLORDER) {
\r
474 // main test method called with different strengths,
\r
475 // tests comparison of custum collation with different strengths
\r
477 private void doTest(Collator collation, char[] source, char[] target, int result) {
\r
478 String s = new String(source);
\r
479 String t = new String(target);
\r
480 doTestVariant(collation, s, t, result);
\r
482 doTestVariant(collation, t, s, 1);
\r
483 } else if(result == 1) {
\r
484 doTestVariant(collation, t, s, -1);
\r
486 doTestVariant(collation, t, s, 0);
\r
490 // main test method called with different strengths,
\r
491 // tests comparison of custum collation with different strengths
\r
493 private void doTest(Collator collation,String s, String t, int result) {
\r
494 doTestVariant(collation, s, t, result);
\r
496 doTestVariant(collation, t, s, 1);
\r
497 } else if(result == 1) {
\r
498 doTestVariant(collation, t, s, -1);
\r
500 doTestVariant(collation, t, s, 0);
\r
504 private void doTestVariant(Collator collation, String source, String target, int result) {
\r
505 int compareResult = collation.compare(source, target);
\r
506 CollationKey srckey , tgtkey;
\r
507 srckey = collation.getCollationKey(source);
\r
508 tgtkey = collation.getCollationKey(target);
\r
509 int keyResult = srckey.compareTo(tgtkey);
\r
510 if (compareResult != result) {
\r
511 errln("String comparison failed in variant test\n");
\r
513 if (keyResult != result) {
\r
514 errln("Collation key comparison failed in variant test\n");
\r