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