2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Port From: ICU4C v2.1 : Collate/G7CollationTest
10 * Source File: $ICU4CRoot/source/test/intltest/g7coll.cpp
13 package com.ibm.icu.dev.test.collator;
15 import java.util.Locale;
17 import com.ibm.icu.dev.test.TestFmwk;
18 import com.ibm.icu.text.CollationKey;
19 import com.ibm.icu.text.Collator;
20 import com.ibm.icu.text.RuleBasedCollator;
22 public class G7CollationTest extends TestFmwk{
23 public static void main(String[] args) throws Exception{
24 new G7CollationTest().run(args);
25 // new G7CollationTest().TestDemo3();
28 private static String[] testCases = {
29 "blackbirds", "Pat", "p\u00E9ch\u00E9", "p\u00EAche", "p\u00E9cher",
30 "p\u00EAcher", "Tod", "T\u00F6ne", "Tofu", "blackbird", "Ton",
31 "PAT", "black-bird", "black-birds", "pat", // 14
33 "czar", "churo", "cat", "darn", "?", /* 19 */
34 "quick", "#", "&", "a-rdvark", "aardvark", /* 23 */
35 "abbot", "co-p", "cop", "coop", "zebra"
38 private static int[][] results = {
39 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */
40 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */
41 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */
42 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */
43 { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */
44 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */
45 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */
46 { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */
47 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
48 { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 },
49 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
50 { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },
51 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
52 { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },
53 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
54 { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }
57 //private static final int MAX_TOKEN_LEN = 16;
58 //private static final int TESTLOCALES = 12;
59 private static final int FIXEDTESTSET = 15;
60 private static final int TOTALTESTSET = 30;
62 // perform test with added rules " & Z < p, P"
63 public void TestDemo1() {
64 logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
66 Collator col = Collator.getInstance(Locale.ENGLISH);
69 String baseRules = ((RuleBasedCollator)col).getRules();
70 String newRules = " & Z < p, P";
71 newRules = baseRules + newRules;
72 RuleBasedCollator myCollation = null;
74 myCollation = new RuleBasedCollator(newRules);
75 } catch(Exception e) {
76 errln("Fail to create RuleBasedCollator with rules:" + newRules);
81 for (j = 0; j < FIXEDTESTSET; j++) {
82 for (n = j+1; n < FIXEDTESTSET; n++) {
83 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], -1);
89 // perorm test with added rules "& C < ch , cH, Ch, CH"
90 public void TestDemo2() {
91 logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
92 Collator col = Collator.getInstance(Locale.ENGLISH);
95 String baseRules = ((RuleBasedCollator)col).getRules();
96 String newRules = "& C < ch , cH, Ch, CH";
97 newRules = baseRules + newRules;
98 RuleBasedCollator myCollation = null;
100 myCollation = new RuleBasedCollator(newRules);
102 errln("Fail to create RuleBasedCollator with rules:" + newRules);
107 for (j = 0; j < TOTALTESTSET; j++) {
108 for (n = j+1; n < TOTALTESTSET; n++) {
109 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1);
115 // perform test with added rules
116 // "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"
117 public void TestDemo3() {
118 // logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
119 Collator col = Collator.getInstance(Locale.ENGLISH);
122 String baseRules = ((RuleBasedCollator)col).getRules();
123 String newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
124 newRules = baseRules + newRules;
125 RuleBasedCollator myCollation = null;
127 myCollation = new RuleBasedCollator(newRules);
129 errln("Fail to create RuleBasedCollator with rules:" + newRules);
134 for (j = 0; j < TOTALTESTSET; j++) {
135 for (n = j+1; n < TOTALTESTSET; n++) {
136 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], -1);
142 // perform test with added rules
143 // " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "
144 public void TestDemo4() {
145 logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
146 Collator col = Collator.getInstance(Locale.ENGLISH);
148 String baseRules = ((RuleBasedCollator)col).getRules();
149 String newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
150 newRules = baseRules + newRules;
151 RuleBasedCollator myCollation = null;
153 myCollation = new RuleBasedCollator(newRules);
155 errln("Fail to create RuleBasedCollator with rules:" + newRules);
160 for (j = 0; j < TOTALTESTSET; j++) {
161 for (n = j+1; n < TOTALTESTSET; n++) {
162 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], -1);
167 public void TestG7Data() {
173 Locale.CANADA_FRENCH,
179 for (i = 0; i < locales.length; i++) {
180 Collator myCollation= null;
181 RuleBasedCollator tblColl1 = null;
183 myCollation = Collator.getInstance(locales[i]);
184 tblColl1 = new RuleBasedCollator(((RuleBasedCollator)myCollation).getRules());
185 } catch (Exception foo) {
186 warnln("Exception: " + foo.getMessage() +
187 "; Locale : " + locales[i].getDisplayName() + " getRules failed");
190 for (j = 0; j < FIXEDTESTSET; j++) {
191 for (int n = j+1; n < FIXEDTESTSET; n++) {
192 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], -1);
200 // main test routine, tests comparisons for a set of strings against sets of expected results
201 private void doTest(Collator myCollation, String source, String target,
204 int compareResult = myCollation.compare(source, target);
205 CollationKey sortKey1, sortKey2;
206 sortKey1 = myCollation.getCollationKey(source);
207 sortKey2 = myCollation.getCollationKey(target);
208 int keyResult = sortKey1.compareTo(sortKey2);
209 reportCResult(source, target, sortKey1, sortKey2, compareResult,
210 keyResult, compareResult, result);
213 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
214 int compareResult, int keyResult, int incResult, int expectedResult ){
215 if (expectedResult < -1 || expectedResult > 1) {
216 errln("***** invalid call to reportCResult ****");
220 boolean ok1 = (compareResult == expectedResult);
221 boolean ok2 = (keyResult == expectedResult);
222 boolean ok3 = (incResult == expectedResult);
224 if (ok1 && ok2 && ok3 && !isVerbose()){
227 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
228 String msg2 = "\", \"";
229 String msg3 = "\") returned ";
230 String msg4 = "; expected ";
232 String sExpect = new String("");
233 String sResult = new String("");
234 sResult = appendCompareResult(compareResult, sResult);
235 sExpect = appendCompareResult(expectedResult, sExpect);
237 logln(msg1 + source + msg2 + target + msg3 + sResult);
239 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
242 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
243 msg2 = "\").compareTo(key(\"";
244 msg3 = "\")) returned ";
245 sResult = appendCompareResult(keyResult, sResult);
247 logln(msg1 + source + msg2 + target + msg3 + sResult);
249 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
252 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
255 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
257 msg3 = "\") returned ";
259 sResult = appendCompareResult(incResult, sResult);
262 logln(msg1 + source + msg2 + target + msg3 + sResult);
264 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
269 private String appendCompareResult(int result, String target){
272 } else if (result == 0) {
274 } else if (result == 1) {
278 target += huh + result;
283 String prettify(CollationKey sourceKey) {
285 byte[] bytes= sourceKey.toByteArray();
288 for (i = 0; i < bytes.length; i++) {
289 target += Integer.toHexString(bytes[i]);