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/CollationFrenchTest
010: * Source File: $ICU4CRoot/source/test/intltest/frcoll.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 CollationFrenchTest extends TestFmwk {
018: public static void main(String[] args) throws Exception {
019: new CollationFrenchTest().run(args);
020: }
021:
022: private static char[][] testSourceCases = {
023: { 0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063 /*'c'*/},
024: { 0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045 /*'E'*/},
025: { 0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/,
026: 0x006f/*'o'*/, 0x0070 /*'p'*/},
027: { 0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/,
028: 0x0065 /*'e'*/},
029: { 0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/,
030: 0x0065/*'e'*/, 0x0072 /*'r'*/},
031: { 0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/,
032: 0x0065/*'e'*/, 0x0072 /*'r'*/},
033: { 0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/,
034: 0x0065/*'e'*/, 0x0072 /*'r'*/},
035: { 0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/,
036: 0x006c/*'l'*/, 0x006f /*'o'*/}, { 0x01f1 },
037: { 0xfb00 }, { 0x01fa }, { 0x0101 } };
038:
039: private static char[][] testTargetCases = {
040: { 0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043 /*'C'*/},
041: { 0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065 /*'e'*/},
042: { 0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050 /*'P'*/},
043: { 0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/,
044: 0x00E9 },
045: { 0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/,
046: 0x00E9 },
047: { 0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/,
048: 0x0065 /*'e'*/},
049: { 0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/,
050: 0x0065/*'e'*/, 0x0072 /*'r'*/},
051: { 0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/,
052: 0x006c/*'l'*/, 0x004f /*'O'*/}, { 0x01ee },
053: { 0x25ca }, { 0x00e0 }, { 0x01df } };
054:
055: private static int[] results = { -1, -1, -1, /*Collator::GREATER,*/
056: -1, 1, 1, -1, 1, -1, /*Collator::GREATER,*/
057: 1, -1, -1 };
058:
059: // 0x0300 is grave, 0x0301 is acute
060: // the order of elements in this array must be different than the order in CollationEnglishTest
061: private static char[][] testAcute = {
062: /*00*/{ 0x0065/*'e'*/, 0x0065 /*'e'*/},
063: /*01*/{ 0x0065/*'e'*/, 0x0301, 0x0065 /*'e'*/},
064: /*02*/{ 0x0065/*'e'*/, 0x0300, 0x0301, 0x0065 /*'e'*/},
065: /*03*/{ 0x0065/*'e'*/, 0x0300, 0x0065 /*'e'*/},
066: /*04*/{ 0x0065/*'e'*/, 0x0301, 0x0300, 0x0065 /*'e'*/},
067: /*05*/{ 0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301 },
068: /*06*/{ 0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301 },
069: /*07*/{ 0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,
070: 0x0301 },
071: /*08*/{ 0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301 },
072: /*09*/{ 0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,
073: 0x0301 },
074: /*0a*/{ 0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301 },
075: /*0b*/{ 0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300,
076: 0x0301 },
077: /*0c*/{ 0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,
078: 0x0300, 0x0301 },
079: /*0d*/{ 0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300,
080: 0x0301 },
081: /*0e*/{ 0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,
082: 0x0300, 0x0301 },
083: /*0f*/{ 0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300 },
084: /*10*/{ 0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300 },
085: /*11*/{ 0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,
086: 0x0300 },
087: /*12*/{ 0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300 },
088: /*13*/{ 0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,
089: 0x0300 },
090: /*14*/{ 0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300 },
091: /*15*/{ 0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301,
092: 0x0300 },
093: /*16*/{ 0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/,
094: 0x0301, 0x0300 },
095: /*17*/{ 0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301,
096: 0x0300 },
097: /*18*/{ 0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/,
098: 0x0301, 0x0300 } };
099:
100: private static char[][] testBugs = { { 0x0061 /*'a'*/},
101: { 0x0041 /*'A'*/}, { 0x0065 /*'e'*/}, { 0x0045 /*'E'*/},
102: { 0x00e9 }, { 0x00e8 }, { 0x00ea }, { 0x00eb },
103: { 0x0065/*'e'*/, 0x0061 /*'a'*/}, { 0x0078 /*'x'*/} };
104:
105: private Collator myCollation = null;
106:
107: public CollationFrenchTest() {
108:
109: }
110:
111: protected void init() throws Exception {
112: myCollation = Collator.getInstance(Locale.FRENCH);
113: }
114:
115: // perform tests with strength TERTIARY
116: public void TestTertiary() {
117: int i = 0;
118: myCollation.setStrength(Collator.TERTIARY);
119:
120: for (i = 0; i < 12; i++) {
121: doTest(testSourceCases[i], testTargetCases[i], results[i]);
122: }
123: }
124:
125: // perform tests with strength SECONDARY
126: public void TestSecondary() {
127: //test acute and grave ordering
128: int i = 0;
129: int j;
130: int expected;
131:
132: myCollation.setStrength(Collator.SECONDARY);
133:
134: for (i = 0; i < testAcute.length; i++) {
135: for (j = 0; j < testAcute.length; j++) {
136: if (i < j) {
137: expected = -1;
138: } else if (i == j) {
139: expected = 0;
140: } else {
141: expected = 1;
142: }
143: doTest(testAcute[i], testAcute[j], expected);
144: }
145: }
146: }
147:
148: // perform extra tests
149: public void TestExtra() {
150: int i, j;
151: myCollation.setStrength(Collator.TERTIARY);
152: for (i = 0; i < 9; i++) {
153: for (j = i + 1; j < 10; j += 1) {
154: doTest(testBugs[i], testBugs[j], -1);
155: }
156: }
157: }
158:
159: public void TestContinuationReordering() {
160: String rule = "&0x2f00 << 0x2f01";
161: try {
162: RuleBasedCollator collator = new RuleBasedCollator(rule);
163: collator.setFrenchCollation(true);
164: CollationKey key1 = collator
165: .getCollationKey("a\u0325\u2f00\u2f01b\u0325");
166: CollationKey key2 = collator
167: .getCollationKey("a\u0325\u2f01\u2f01b\u0325");
168: if (key1.compareTo(key2) >= 0) {
169: errln("Error comparing continuation strings");
170: }
171: } catch (Exception e) {
172: errln(e.toString());
173: }
174: }
175:
176: // main test routine, test rules specific to the french locale
177: private void doTest(char[] source, char[] target, int result) {
178: String s = new String(source);
179: String t = new String(target);
180: int compareResult = myCollation.compare(s, t);
181: CollationKey sortKey1, sortKey2;
182: sortKey1 = myCollation.getCollationKey(s);
183: sortKey2 = myCollation.getCollationKey(t);
184: int keyResult = sortKey1.compareTo(sortKey2);
185: reportCResult(s, t, sortKey1, sortKey2, compareResult,
186: keyResult, compareResult, result);
187: }
188:
189: private void reportCResult(String source, String target,
190: CollationKey sourceKey, CollationKey targetKey,
191: int compareResult, int keyResult, int incResult,
192: int expectedResult) {
193: if (expectedResult < -1 || expectedResult > 1) {
194: errln("***** invalid call to reportCResult ****");
195: return;
196: }
197:
198: boolean ok1 = (compareResult == expectedResult);
199: boolean ok2 = (keyResult == expectedResult);
200: boolean ok3 = (incResult == expectedResult);
201:
202: if (ok1 && ok2 && ok3 && !isVerbose()) {
203: return;
204: } else {
205: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
206: String msg2 = "\", \"";
207: String msg3 = "\") returned ";
208: String msg4 = "; expected ";
209:
210: String sExpect = new String("");
211: String sResult = new String("");
212: sResult = appendCompareResult(compareResult, sResult);
213: sExpect = appendCompareResult(expectedResult, sExpect);
214: if (ok1) {
215: logln(msg1 + source + msg2 + target + msg3 + sResult);
216: } else {
217: errln(msg1 + source + msg2 + target + msg3 + sResult
218: + msg4 + sExpect);
219: }
220:
221: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
222: msg2 = "\").compareTo(key(\"";
223: msg3 = "\")) returned ";
224: sResult = appendCompareResult(keyResult, sResult);
225: if (ok2) {
226: logln(msg1 + source + msg2 + target + msg3 + sResult);
227: } else {
228: errln(msg1 + source + msg2 + target + msg3 + sResult
229: + msg4 + sExpect);
230: msg1 = " ";
231: msg2 = " vs. ";
232: errln(msg1 + prettify(sourceKey) + msg2
233: + prettify(targetKey));
234: }
235:
236: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
237: msg2 = "\", \"";
238: msg3 = "\") returned ";
239:
240: sResult = appendCompareResult(incResult, sResult);
241:
242: if (ok3) {
243: logln(msg1 + source + msg2 + target + msg3 + sResult);
244: } else {
245: errln(msg1 + source + msg2 + target + msg3 + sResult
246: + msg4 + sExpect);
247: }
248: }
249: }
250:
251: private String appendCompareResult(int result, String target) {
252: if (result == -1) {
253: target += "LESS";
254: } else if (result == 0) {
255: target += "EQUAL";
256: } else if (result == 1) {
257: target += "GREATER";
258: } else {
259: String huh = "?";
260: target += huh + result;
261: }
262: return target;
263: }
264:
265: String prettify(CollationKey sourceKey) {
266: int i;
267: byte[] bytes = sourceKey.toByteArray();
268: String target = "[";
269:
270: for (i = 0; i < bytes.length; i++) {
271: target += Integer.toHexString(bytes[i]);
272: target += " ";
273: }
274: target += "]";
275: return target;
276: }
277: }
|