2 *******************************************************************************
\r
3 * Copyright (C) 2002-2007, 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 com.ibm.icu.dev.test.*;
\r
16 import com.ibm.icu.text.*;
\r
17 import java.util.Locale;
\r
19 public class G7CollationTest extends TestFmwk{
\r
20 public static void main(String[] args) throws Exception{
\r
21 new G7CollationTest().run(args);
\r
22 // new G7CollationTest().TestDemo3();
\r
25 private static String[] testCases = {
\r
26 "blackbirds", "Pat", "p\u00E9ch\u00E9", "p\u00EAche", "p\u00E9cher",
\r
27 "p\u00EAcher", "Tod", "T\u00F6ne", "Tofu", "blackbird", "Ton",
\r
28 "PAT", "black-bird", "black-birds", "pat", // 14
\r
30 "czar", "churo", "cat", "darn", "?", /* 19 */
\r
31 "quick", "#", "&", "a-rdvark", "aardvark", /* 23 */
\r
32 "abbot", "co-p", "cop", "coop", "zebra"
\r
35 private static int[][] results = {
\r
36 { 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
37 { 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
38 { 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
39 { 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
40 { 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
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 }, /* de_DE */
\r
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 }, /* it_IT */
\r
43 { 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
44 /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
\r
45 { 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
46 /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
\r
47 { 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
48 /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
\r
49 { 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
50 /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */ /* loop to TOTALTESTSET */
\r
51 { 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
54 //private static final int MAX_TOKEN_LEN = 16;
\r
55 //private static final int TESTLOCALES = 12;
\r
56 private static final int FIXEDTESTSET = 15;
\r
57 private static final int TOTALTESTSET = 30;
\r
59 // perform test with added rules " & Z < p, P"
\r
60 public void TestDemo1() {
\r
61 logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
\r
63 Collator col = Collator.getInstance(Locale.ENGLISH);
\r
66 String baseRules = ((RuleBasedCollator)col).getRules();
\r
67 String newRules = " & Z < p, P";
\r
68 newRules = baseRules + newRules;
\r
69 RuleBasedCollator myCollation = null;
\r
71 myCollation = new RuleBasedCollator(newRules);
\r
72 } catch(Exception e) {
\r
73 errln("Fail to create RuleBasedCollator with rules:" + newRules);
\r
78 for (j = 0; j < FIXEDTESTSET; j++) {
\r
79 for (n = j+1; n < FIXEDTESTSET; n++) {
\r
80 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], -1);
\r
86 // perorm test with added rules "& C < ch , cH, Ch, CH"
\r
87 public void TestDemo2() {
\r
88 logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
\r
89 Collator col = Collator.getInstance(Locale.ENGLISH);
\r
92 String baseRules = ((RuleBasedCollator)col).getRules();
\r
93 String newRules = "& C < ch , cH, Ch, CH";
\r
94 newRules = baseRules + newRules;
\r
95 RuleBasedCollator myCollation = null;
\r
97 myCollation = new RuleBasedCollator(newRules);
\r
98 }catch(Exception e){
\r
99 errln("Fail to create RuleBasedCollator with rules:" + newRules);
\r
104 for (j = 0; j < TOTALTESTSET; j++) {
\r
105 for (n = j+1; n < TOTALTESTSET; n++) {
\r
106 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1);
\r
112 // perform test with added rules
\r
113 // "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"
\r
114 public void TestDemo3() {
\r
115 // logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
\r
116 Collator col = Collator.getInstance(Locale.ENGLISH);
\r
119 String baseRules = ((RuleBasedCollator)col).getRules();
\r
120 String newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
\r
121 newRules = baseRules + newRules;
\r
122 RuleBasedCollator myCollation = null;
\r
124 myCollation = new RuleBasedCollator(newRules);
\r
125 }catch(Exception e){
\r
126 errln("Fail to create RuleBasedCollator with rules:" + newRules);
\r
131 for (j = 0; j < TOTALTESTSET; j++) {
\r
132 for (n = j+1; n < TOTALTESTSET; n++) {
\r
133 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], -1);
\r
139 // perform test with added rules
\r
140 // " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "
\r
141 public void TestDemo4() {
\r
142 logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
\r
143 Collator col = Collator.getInstance(Locale.ENGLISH);
\r
145 String baseRules = ((RuleBasedCollator)col).getRules();
\r
146 String newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
\r
147 newRules = baseRules + newRules;
\r
148 RuleBasedCollator myCollation = null;
\r
150 myCollation = new RuleBasedCollator(newRules);
\r
151 }catch(Exception e){
\r
152 errln("Fail to create RuleBasedCollator with rules:" + newRules);
\r
157 for (j = 0; j < TOTALTESTSET; j++) {
\r
158 for (n = j+1; n < TOTALTESTSET; n++) {
\r
159 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], -1);
\r
164 public void TestG7Data() {
\r
165 Locale locales[] = {
\r
170 Locale.CANADA_FRENCH,
\r
176 for (i = 0; i < locales.length; i++) {
\r
177 Collator myCollation= null;
\r
178 RuleBasedCollator tblColl1 = null;
\r
180 myCollation = Collator.getInstance(locales[i]);
\r
181 tblColl1 = new RuleBasedCollator(((RuleBasedCollator)myCollation).getRules());
\r
182 } catch (Exception foo) {
\r
183 warnln("Exception: " + foo.getMessage() +
\r
184 "; Locale : " + locales[i].getDisplayName() + " getRules failed");
\r
187 for (j = 0; j < FIXEDTESTSET; j++) {
\r
188 for (int n = j+1; n < FIXEDTESTSET; n++) {
\r
189 doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], -1);
\r
192 myCollation = null;
\r
197 // main test routine, tests comparisons for a set of strings against sets of expected results
\r
198 private void doTest(Collator myCollation, String source, String target,
\r
201 int compareResult = myCollation.compare(source, target);
\r
202 CollationKey sortKey1, sortKey2;
\r
203 sortKey1 = myCollation.getCollationKey(source);
\r
204 sortKey2 = myCollation.getCollationKey(target);
\r
205 int keyResult = sortKey1.compareTo(sortKey2);
\r
206 reportCResult(source, target, sortKey1, sortKey2, compareResult,
\r
207 keyResult, compareResult, result);
\r
210 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
\r
211 int compareResult, int keyResult, int incResult, int expectedResult ){
\r
212 if (expectedResult < -1 || expectedResult > 1) {
\r
213 errln("***** invalid call to reportCResult ****");
\r
217 boolean ok1 = (compareResult == expectedResult);
\r
218 boolean ok2 = (keyResult == expectedResult);
\r
219 boolean ok3 = (incResult == expectedResult);
\r
221 if (ok1 && ok2 && ok3 && !isVerbose()){
\r
224 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
\r
225 String msg2 = "\", \"";
\r
226 String msg3 = "\") returned ";
\r
227 String msg4 = "; expected ";
\r
229 String sExpect = new String("");
\r
230 String sResult = new String("");
\r
231 sResult = appendCompareResult(compareResult, sResult);
\r
232 sExpect = appendCompareResult(expectedResult, sExpect);
\r
234 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
236 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
239 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
240 msg2 = "\").compareTo(key(\"";
\r
241 msg3 = "\")) returned ";
\r
242 sResult = appendCompareResult(keyResult, sResult);
\r
244 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
246 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
249 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
252 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
254 msg3 = "\") returned ";
\r
256 sResult = appendCompareResult(incResult, sResult);
\r
259 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
261 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
266 private String appendCompareResult(int result, String target){
\r
267 if (result == -1) {
\r
269 } else if (result == 0) {
\r
271 } else if (result == 1) {
\r
272 target += "GREATER";
\r
275 target += huh + result;
\r
280 String prettify(CollationKey sourceKey) {
\r
282 byte[] bytes= sourceKey.toByteArray();
\r
283 String target = "[";
\r
285 for (i = 0; i < bytes.length; i++) {
\r
286 target += Integer.toHexString(bytes[i]);
\r