2 *******************************************************************************
\r
3 * Copyright (C) 2002-2005, 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 com.ibm.icu.dev.test.*;
\r
17 import com.ibm.icu.text.*;
\r
19 import java.util.Locale;
\r
21 public class CollationDummyTest extends TestFmwk {
\r
22 public static void main(String[] args) throws Exception {
\r
23 new CollationDummyTest().run(args);
\r
24 // new CollationDummyTest().TestVariableTop();
\r
27 //testSourceCases[][] and testTargetCases[][], testCases[][] are ported from the file callcoll.c in icu4c
\r
28 private static char[][] testSourceCases = {
\r
29 {0x61, 0x62, 0x27, 0x63},
\r
30 {0x63, 0x6f, 0x2d, 0x6f, 0x70},
\r
32 {0x61, 0x6d, 0x70, 0x65, 0x72, 0x73, 0x61, 0x64},
\r
34 {0x66, 0x6f, 0x75, 0x72},
\r
35 {0x66, 0x69, 0x76, 0x65},
\r
41 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
\r
45 {0x61, 0x63, 0x48, 0x63}, // simple test
\r
46 {0x70, 0x00EA, 0x63, 0x68, 0x65},
\r
48 {0x61, 0x62, 0x63}, // 20
\r
52 {0x61, 0x00E6, 0x63},
\r
53 {0x61, 0x63, 0x48, 0x63}, // primary test
\r
54 {0x62, 0x6c, 0x61, 0x63, 0x6b},
\r
55 {0x66, 0x6f, 0x75, 0x72},
\r
56 {0x66, 0x69, 0x76, 0x65},
\r
58 {0x61, 0x62, 0x63}, // 30
\r
59 {0x61, 0x62, 0x63},
\r
60 {0x61, 0x62, 0x63, 0x48},
\r
62 {0x61, 0x63, 0x48, 0x63}, // 34
\r
63 {0x61, 0x63, 0x65, 0x30},
\r
65 {0x70, 0x00EA,0x30} // 37
\r
68 private static char[][] testTargetCases = {
\r
69 {0x61, 0x62, 0x63, 0x27},
\r
70 {0x43, 0x4f, 0x4f, 0x50},
\r
78 {0x70, 0x6e, 0x65}, // 10
\r
81 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
\r
82 {0x61, 0x3c, 0x3d, 0x62},
\r
84 {0x61, 0x43, 0x48, 0x63},
\r
85 {0x61, 0x43, 0x48, 0x63}, // simple test
\r
86 {0x70, 0x00E9, 0x63, 0x68, 0x00E9},
\r
88 {0x61, 0x42, 0x43}, // 20
\r
89 {0x61, 0x62, 0x63, 0x68},
\r
91 {0x00E4, 0x62, 0x63},
\r
92 {0x61, 0x00C6, 0x63},
\r
93 {0x61, 0x43, 0x48, 0x63}, // primary test
\r
94 {0x62, 0x6c, 0x61, 0x63, 0x6b, 0x2d, 0x62, 0x69, 0x72, 0x64},
\r
99 {0x61, 0x42, 0x63}, // 30
\r
100 {0x61, 0x62, 0x63, 0x68},
\r
101 {0x61, 0x62, 0x64},
\r
102 {0x61, 0x43, 0x48, 0x63}, // 34
\r
103 {0x61, 0x63, 0x65, 0x30},
\r
105 {0x70, 0x00EB,0x30} // 37
\r
108 private static char[][] testCases = {
\r
126 -1, //Collator::GREATER,
\r
142 // test primary > 17
\r
152 // test secondary > 26
\r
166 final int MAX_TOKEN_LEN = 16;
\r
168 public RuleBasedCollator myCollation;
\r
170 public CollationDummyTest() {
\r
172 protected void init() throws Exception{
\r
173 String ruleset = "& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
\r
174 // String ruleset = "& Four, 4";
\r
175 myCollation = null;
\r
176 myCollation = new RuleBasedCollator(ruleset);
\r
179 // perform test with strength tertiary
\r
180 public void TestTertiary() {
\r
182 myCollation.setStrength(Collator.TERTIARY);
\r
183 for (i = 0; i < 17 ; i++) {
\r
184 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
188 // perform test with strength PRIMARY
\r
189 public void TestPrimary() {
\r
190 // problem in strcollinc for unfinshed contractions
\r
191 myCollation.setStrength(Collator.PRIMARY);
\r
192 for (int i = 17; i < 26 ; i++) {
\r
193 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
197 //perform test with strength SECONDARY
\r
198 public void TestSecondary() {
\r
200 myCollation.setStrength(Collator.SECONDARY);
\r
201 for (i = 26; i < 34; i++) {
\r
202 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
206 // perform extra tests
\r
207 public void TestExtra() {
\r
209 myCollation.setStrength(Collator.TERTIARY);
\r
210 for (i = 0; i < testCases.length - 1; i++) {
\r
211 for (j = i + 1; j < testCases.length; j += 1) {
\r
212 doTest(myCollation, testCases[i], testCases[j], -1);
\r
217 public void TestIdentical() {
\r
219 myCollation.setStrength(Collator.IDENTICAL);
\r
220 for (i= 34; i<37; i++) {
\r
221 doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);
\r
225 public void TestJB581() {
\r
226 String source = "THISISATEST.";
\r
227 String target = "Thisisatest.";
\r
228 Collator coll = null;
\r
230 coll = Collator.getInstance(Locale.ENGLISH);
\r
231 } catch (Exception e) {
\r
232 errln("ERROR: Failed to create the collator for : en_US\n");
\r
236 int result = coll.compare(source, target);
\r
237 // result is 1, secondary differences only for ignorable space characters
\r
239 errln("Comparing two strings with only secondary differences in C failed.\n");
\r
243 // To compare them with just primary differences
\r
244 coll.setStrength(Collator.PRIMARY);
\r
245 result = coll.compare(source, target);
\r
248 errln("Comparing two strings with no differences in C failed.\n");
\r
252 // Now, do the same comparison with keys
\r
253 CollationKey sourceKeyOut, targetKeyOut;
\r
254 sourceKeyOut = coll.getCollationKey(source);
\r
255 targetKeyOut = coll.getCollationKey(target);
\r
256 result = sourceKeyOut.compareTo(targetKeyOut);
\r
258 errln("Comparing two strings with sort keys in C failed.\n");
\r
263 //TestSurrogates() is ported from cintltst/callcoll.c
\r
266 * Tests surrogate support.
\r
268 public void TestSurrogates()
\r
270 String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";
\r
271 String source[] = {"z",
\r
273 "\ud800\udc0a\u0308",
\r
277 String target[] = {"\uD800\uDC00",
\r
278 "\ud800\udc0a\u0308",
\r
283 // this test is to verify the supplementary sort key order in the english
\r
285 Collator enCollation;
\r
287 enCollation = Collator.getInstance(Locale.ENGLISH);
\r
288 } catch (Exception e) {
\r
289 errln("ERROR: Failed to create the collator for ENGLISH");
\r
293 myCollation.setStrength(Collator.TERTIARY);
\r
295 // logln("start of english collation supplementary characters test\n");
\r
296 while (count < 2) {
\r
297 doTest(enCollation, source[count], target[count], -1);
\r
300 doTest(enCollation, source[count], target[count], 1);
\r
302 // logln("start of tailored collation supplementary characters test\n");
\r
304 Collator newCollation;
\r
306 newCollation = new RuleBasedCollator(rules);
\r
307 } catch (Exception e) {
\r
308 errln("ERROR: Failed to create the collator for rules");
\r
312 // tests getting collation elements for surrogates for tailored rules
\r
313 while (count < 4) {
\r
314 doTest(newCollation, source[count], target[count], -1);
\r
318 // tests that \uD801\uDC01 still has the same value, not changed
\r
319 CollationKey enKey = enCollation.getCollationKey(source[3]);
\r
320 CollationKey newKey = newCollation.getCollationKey(source[3]);
\r
321 int keyResult = enKey.compareTo(newKey);
\r
322 if(keyResult != 0) {
\r
323 errln("Failed : non-tailored supplementary characters should have the same value\n");
\r
327 //TestVariableTop() is ported from cintltst/callcoll.c
\r
329 * Tests the [variable top] tag in rule syntax. Since the default [alternate]
\r
330 * tag has the value shifted, any codepoints before [variable top] should give
\r
331 * a primary ce of 0.
\r
333 public void TestVariableTop() {
\r
334 String rule = "&z = [variable top]";
\r
337 char[] source = new char[1];
\r
339 int expected[] = {0};
\r
342 enColl = Collator.getInstance(Locale.ENGLISH);
\r
343 } catch (Exception e) {
\r
344 errln("ERROR: Failed to create the collator for ENGLISH");
\r
349 myColl = new RuleBasedCollator(rule);
\r
350 } catch(Exception e){
\r
351 errln("Fail to create RuleBasedCollator with rules:" + rule);
\r
354 enColl.setStrength(Collator.PRIMARY);
\r
355 myColl.setStrength(Collator.PRIMARY);
\r
357 ((RuleBasedCollator)enColl).setAlternateHandlingShifted(true);
\r
358 ((RuleBasedCollator)myColl).setAlternateHandlingShifted(true);
\r
360 if(((RuleBasedCollator)enColl).isAlternateHandlingShifted() != true) {
\r
361 errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");
\r
364 // space is supposed to be a variable
\r
365 CollationKey key = enColl.getCollationKey(" ");
\r
366 byte[] result = key.toByteArray();
\r
368 for(int i = 0; i < result.length; i++) {
\r
369 if(result[i]!= expected[i]) {
\r
370 errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
\r
378 key = myColl.getCollationKey(new String(source));
\r
379 result = key.toByteArray();
\r
381 for(int i = 0; i < result.length; i++) {
\r
382 if(result[i]!= expected[i]) {
\r
383 errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
\r
391 public void TestJB1401() {
\r
392 Collator myCollator = null;
\r
393 char[] NFD_UnsafeStartChars = {
\r
394 0x0f73, // Tibetan Vowel Sign II
\r
395 0x0f75, // Tibetan Vowel Sign UU
\r
396 0x0f81, // Tibetan Vowel Sign Reversed II
\r
402 myCollator = Collator.getInstance(Locale.ENGLISH);
\r
403 } catch(Exception e) {
\r
404 errln("ERROR: Failed to create the collator for ENGLISH");
\r
407 myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
409 // Get the next funny character to be tested, and set up the
\r
410 // three test strings X, Y, Z, consisting of an A-grave + test char,
\r
411 // in original form, NFD, and then NFC form.
\r
412 char c = NFD_UnsafeStartChars[i];
\r
415 String x = "\u00C0" + c; // \u00C0 is A Grave
\r
420 y = Normalizer.decompose(x, false);
\r
421 z = Normalizer.decompose(y, true);
\r
422 } catch (Exception e) {
\r
423 errln("ERROR: Failed to normalize test of character" + c);
\r
427 // Collation test. All three strings should be equal.
\r
428 // doTest does both strcoll and sort keys, with params in both orders.
\r
429 doTest(myCollator, x, y, 0);
\r
430 doTest(myCollator, x, z, 0);
\r
431 doTest(myCollator, y, z, 0);
\r
433 // Run collation element iterators over the three strings. Results should be same for each.
\r
436 CollationElementIterator ceiX, ceiY, ceiZ;
\r
440 ceiX = ((RuleBasedCollator)myCollator).getCollationElementIterator(x);
\r
441 ceiY = ((RuleBasedCollator)myCollator).getCollationElementIterator(y);
\r
442 ceiZ = ((RuleBasedCollator)myCollator).getCollationElementIterator(z);
\r
443 } catch(Exception e) {
\r
444 errln("ERROR: getCollationElementIterator failed");
\r
453 } catch (Exception e) {
\r
454 errln("ERROR: CollationElementIterator.next failed for iteration " + j);
\r
458 if (ceX != ceY || ceY != ceZ) {
\r
459 errln("ERROR: ucol_next failed for iteration " + j);
\r
462 if (ceX == CollationElementIterator.NULLORDER) {
\r
470 // main test method called with different strengths,
\r
471 // tests comparison of custum collation with different strengths
\r
473 private void doTest(Collator collation, char[] source, char[] target, int result) {
\r
474 String s = new String(source);
\r
475 String t = new String(target);
\r
476 doTestVariant(collation, s, t, result);
\r
478 doTestVariant(collation, t, s, 1);
\r
479 } else if(result == 1) {
\r
480 doTestVariant(collation, t, s, -1);
\r
482 doTestVariant(collation, t, s, 0);
\r
486 // main test method called with different strengths,
\r
487 // tests comparison of custum collation with different strengths
\r
489 private void doTest(Collator collation,String s, String t, int result) {
\r
490 doTestVariant(collation, s, t, result);
\r
492 doTestVariant(collation, t, s, 1);
\r
493 } else if(result == 1) {
\r
494 doTestVariant(collation, t, s, -1);
\r
496 doTestVariant(collation, t, s, 0);
\r
500 private void doTestVariant(Collator collation, String source, String target, int result) {
\r
501 int compareResult = collation.compare(source, target);
\r
502 CollationKey srckey , tgtkey;
\r
503 srckey = collation.getCollationKey(source);
\r
504 tgtkey = collation.getCollationKey(target);
\r
505 int keyResult = srckey.compareTo(tgtkey);
\r
506 if (compareResult != result) {
\r
507 errln("String comparison failed in variant test\n");
\r
509 if (keyResult != result) {
\r
510 errln("Collation key comparison failed in variant test\n");
\r