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