001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v2.1 : Collate/G7CollationTest
010: * Source File: $ICU4CRoot/source/test/intltest/g7coll.cpp
011: **/package com.ibm.icu.dev.test.collator;
012:
013: import com.ibm.icu.dev.test.*;
014: import com.ibm.icu.text.*;
015: import java.util.Locale;
016:
017: public class G7CollationTest extends TestFmwk {
018: public static void main(String[] args) throws Exception {
019: new G7CollationTest().run(args);
020: // new G7CollationTest().TestDemo3();
021: }
022:
023: private static String[] testCases = { "blackbirds", "Pat",
024: "p\u00E9ch\u00E9", "p\u00EAche", "p\u00E9cher",
025: "p\u00EAcher", "Tod", "T\u00F6ne", "Tofu", "blackbird",
026: "Ton", "PAT", "black-bird", "black-birds", "pat", // 14
027: // Additional tests
028: "czar", "churo", "cat", "darn", "?", /* 19 */
029: "quick", "#", "&", "a-rdvark", "aardvark", /* 23 */
030: "abbot", "co-p", "cop", "coop", "zebra" };
031:
032: private static int[][] results = {
033: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
034: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */
035: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
036: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */
037: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
038: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */
039: { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31,
040: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */
041: { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31,
042: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */
043: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
044: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */
045: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
046: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */
047: { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31,
048: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */
049: /* new table collation with rules "& Z < p, P" loop to FIXEDTESTSET */
050: { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31,
051: 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 },
052: /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */
053: { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15,
054: 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },
055: /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&' " loop to TOTALTESTSET */
056: { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18,
057: 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },
058: /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " *//* loop to TOTALTESTSET */
059: { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27,
060: 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 } };
061:
062: //private static final int MAX_TOKEN_LEN = 16;
063: //private static final int TESTLOCALES = 12;
064: private static final int FIXEDTESTSET = 15;
065: private static final int TOTALTESTSET = 30;
066:
067: // perform test with added rules " & Z < p, P"
068: public void TestDemo1() {
069: logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");
070:
071: Collator col = Collator.getInstance(Locale.ENGLISH);
072:
073: String baseRules = ((RuleBasedCollator) col).getRules();
074: String newRules = " & Z < p, P";
075: newRules = baseRules + newRules;
076: RuleBasedCollator myCollation = null;
077: try {
078: myCollation = new RuleBasedCollator(newRules);
079: } catch (Exception e) {
080: errln("Fail to create RuleBasedCollator with rules:"
081: + newRules);
082: return;
083: }
084:
085: if (myCollation == null) {
086: errln("Demo Test 2 Table Collation object creation failed.");
087: return;
088: }
089:
090: int j, n;
091: for (j = 0; j < FIXEDTESTSET; j++) {
092: for (n = j + 1; n < FIXEDTESTSET; n++) {
093: doTest(myCollation, testCases[results[8][j]],
094: testCases[results[8][n]], -1);
095: }
096: }
097: }
098:
099: // perorm test with added rules "& C < ch , cH, Ch, CH"
100: public void TestDemo2() {
101: logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");
102: Collator col = Collator.getInstance(Locale.ENGLISH);
103:
104: String baseRules = ((RuleBasedCollator) col).getRules();
105: String newRules = "& C < ch , cH, Ch, CH";
106: newRules = baseRules + newRules;
107: RuleBasedCollator myCollation = null;
108: try {
109: myCollation = new RuleBasedCollator(newRules);
110: } catch (Exception e) {
111: errln("Fail to create RuleBasedCollator with rules:"
112: + newRules);
113: return;
114: }
115:
116: int j, n;
117: for (j = 0; j < TOTALTESTSET; j++) {
118: for (n = j + 1; n < TOTALTESTSET; n++) {
119: doTest(myCollation, testCases[results[9][j]],
120: testCases[results[9][n]], -1);
121: }
122: }
123: }
124:
125: // perform test with added rules
126: // "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"
127: public void TestDemo3() {
128: // logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");
129: Collator col = Collator.getInstance(Locale.ENGLISH);
130:
131: String baseRules = ((RuleBasedCollator) col).getRules();
132: String newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";
133: newRules = baseRules + newRules;
134: RuleBasedCollator myCollation = null;
135: try {
136: myCollation = new RuleBasedCollator(newRules);
137: } catch (Exception e) {
138: errln("Fail to create RuleBasedCollator with rules:"
139: + newRules);
140: return;
141: }
142:
143: int j, n;
144: for (j = 0; j < TOTALTESTSET; j++) {
145: for (n = j + 1; n < TOTALTESTSET; n++) {
146: doTest(myCollation, testCases[results[10][j]],
147: testCases[results[10][n]], -1);
148: }
149: }
150: }
151:
152: // perform test with added rules
153: // " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "
154: public void TestDemo4() {
155: logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");
156: Collator col = Collator.getInstance(Locale.ENGLISH);
157:
158: String baseRules = ((RuleBasedCollator) col).getRules();
159: String newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
160: newRules = baseRules + newRules;
161: RuleBasedCollator myCollation = null;
162: try {
163: myCollation = new RuleBasedCollator(newRules);
164: } catch (Exception e) {
165: errln("Fail to create RuleBasedCollator with rules:"
166: + newRules);
167: return;
168: }
169:
170: int j, n;
171: for (j = 0; j < TOTALTESTSET; j++) {
172: for (n = j + 1; n < TOTALTESTSET; n++) {
173: doTest(myCollation, testCases[results[11][j]],
174: testCases[results[11][n]], -1);
175: }
176: }
177: }
178:
179: public void TestG7Data() {
180: Locale locales[] = { Locale.US, Locale.UK, Locale.CANADA,
181: Locale.FRANCE, Locale.CANADA_FRENCH, Locale.GERMANY,
182: Locale.JAPAN, Locale.ITALY };
183: int i = 0, j = 0;
184: for (i = 0; i < locales.length; i++) {
185: Collator myCollation = null;
186: RuleBasedCollator tblColl1 = null;
187: try {
188: myCollation = Collator.getInstance(locales[i]);
189: tblColl1 = new RuleBasedCollator(
190: ((RuleBasedCollator) myCollation).getRules());
191: } catch (Exception foo) {
192: warnln("Exception: " + foo.getMessage() + "; Locale : "
193: + locales[i].getDisplayName()
194: + " getRules failed");
195: continue;
196: }
197: for (j = 0; j < FIXEDTESTSET; j++) {
198: for (int n = j + 1; n < FIXEDTESTSET; n++) {
199: doTest(tblColl1, testCases[results[i][j]],
200: testCases[results[i][n]], -1);
201: }
202: }
203: myCollation = null;
204: }
205: }
206:
207: // main test routine, tests comparisons for a set of strings against sets of expected results
208: private void doTest(Collator myCollation, String source,
209: String target, int result) {
210:
211: int compareResult = myCollation.compare(source, target);
212: CollationKey sortKey1, sortKey2;
213: sortKey1 = myCollation.getCollationKey(source);
214: sortKey2 = myCollation.getCollationKey(target);
215: int keyResult = sortKey1.compareTo(sortKey2);
216: reportCResult(source, target, sortKey1, sortKey2,
217: compareResult, keyResult, compareResult, result);
218: }
219:
220: private void reportCResult(String source, String target,
221: CollationKey sourceKey, CollationKey targetKey,
222: int compareResult, int keyResult, int incResult,
223: int expectedResult) {
224: if (expectedResult < -1 || expectedResult > 1) {
225: errln("***** invalid call to reportCResult ****");
226: return;
227: }
228:
229: boolean ok1 = (compareResult == expectedResult);
230: boolean ok2 = (keyResult == expectedResult);
231: boolean ok3 = (incResult == expectedResult);
232:
233: if (ok1 && ok2 && ok3 && !isVerbose()) {
234: return;
235: } else {
236: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
237: String msg2 = "\", \"";
238: String msg3 = "\") returned ";
239: String msg4 = "; expected ";
240:
241: String sExpect = new String("");
242: String sResult = new String("");
243: sResult = appendCompareResult(compareResult, sResult);
244: sExpect = appendCompareResult(expectedResult, sExpect);
245: if (ok1) {
246: logln(msg1 + source + msg2 + target + msg3 + sResult);
247: } else {
248: errln(msg1 + source + msg2 + target + msg3 + sResult
249: + msg4 + sExpect);
250: }
251:
252: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
253: msg2 = "\").compareTo(key(\"";
254: msg3 = "\")) returned ";
255: sResult = appendCompareResult(keyResult, sResult);
256: if (ok2) {
257: logln(msg1 + source + msg2 + target + msg3 + sResult);
258: } else {
259: errln(msg1 + source + msg2 + target + msg3 + sResult
260: + msg4 + sExpect);
261: msg1 = " ";
262: msg2 = " vs. ";
263: errln(msg1 + prettify(sourceKey) + msg2
264: + prettify(targetKey));
265: }
266:
267: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
268: msg2 = "\", \"";
269: msg3 = "\") returned ";
270:
271: sResult = appendCompareResult(incResult, sResult);
272:
273: if (ok3) {
274: logln(msg1 + source + msg2 + target + msg3 + sResult);
275: } else {
276: errln(msg1 + source + msg2 + target + msg3 + sResult
277: + msg4 + sExpect);
278: }
279: }
280: }
281:
282: private String appendCompareResult(int result, String target) {
283: if (result == -1) {
284: target += "LESS";
285: } else if (result == 0) {
286: target += "EQUAL";
287: } else if (result == 1) {
288: target += "GREATER";
289: } else {
290: String huh = "?";
291: target += huh + result;
292: }
293: return target;
294: }
295:
296: String prettify(CollationKey sourceKey) {
297: int i;
298: byte[] bytes = sourceKey.toByteArray();
299: String target = "[";
300:
301: for (i = 0; i < bytes.length; i++) {
302: target += Integer.toHexString(bytes[i]);
303: target += " ";
304: }
305: target += "]";
306: return target;
307: }
308: }
|