2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Port From: ICU4C v2.1 : Collate/CollationEnglishTest
10 * Source File: $ICU4CRoot/source/test/intltest/encoll.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;
21 public class CollationEnglishTest extends TestFmwk{
22 public static void main(String[] args) throws Exception{
23 new CollationEnglishTest().run(args);
26 private static char[][] testSourceCases = {
27 {0x0061 /* 'a' */, 0x0062 /* 'b' */},
28 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x002D /* '-' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
29 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0020 /* ' ' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
30 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x002D /* '-' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
31 {0x0048 /* 'H' */, 0x0065 /* 'e' */, 0x006C /* 'l' */, 0x006C /* 'l' */, 0x006F /* 'o' */},
32 {0x0041 /* 'A' */, 0x0042 /* 'B' */, 0x0043 /* 'C' */},
33 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
34 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
35 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x002D /* '-' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
36 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x002D /* '-' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
37 {0x0070 /* 'p' */, 0x00EA, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */},
38 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x00E9},
39 {0x00C4, 0x0042 /* 'B' */, 0x0308, 0x0043 /* 'C' */, 0x0308},
40 {0x0061 /* 'a' */, 0x0308, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
41 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */, 0x0072 /* 'r' */},
42 {0x0072 /* 'r' */, 0x006F /* 'o' */, 0x006C /* 'l' */, 0x0065 /* 'e' */, 0x0073 /* 's' */},
43 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
46 {0x0061 /* 'a' */, 0x0062 /* 'b' */},
47 {0x0074 /* 't' */, 0x0063 /* 'c' */, 0x006F /* 'o' */, 0x006D /* 'm' */, 0x0070 /* 'p' */, 0x0061 /* 'a' */, 0x0072 /* 'r' */, 0x0065 /* 'e' */, 0x0070 /* 'p' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0069 /* 'i' */, 0x006E /* 'n' */},
48 {0x0061 /* 'a' */, 0x0062 /* 'b' */},
49 {0x0061 /* 'a' */, 0x0023 /* '#' */, 0x0062 /* 'b' */},
50 {0x0061 /* 'a' */, 0x0023 /* '#' */, 0x0062 /* 'b' */},
51 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
52 {0x0041 /* 'A' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
53 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
54 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
55 {0x00E6, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
56 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
57 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
58 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
59 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
60 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
61 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
62 {0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x0048 /* 'H' */, 0x0063 /* 'c' */},
63 {0x0061 /* 'a' */, 0x0308, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
64 {0x0074 /* 't' */, 0x0068 /* 'h' */, 0x0069 /* 'i' */, 0x0302, 0x0073 /* 's' */},
65 {0x0070 /* 'p' */, 0x00EA, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */},
66 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
67 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
68 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
69 {0x0061 /* 'a' */, 0x00E6, 0x0063 /* 'c' */},
70 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
71 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
72 {0x0061 /* 'a' */, 0x00E6, 0x0063 /* 'c' */},
73 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
74 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
75 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x00E9} // 49
78 private static char[][] testTargetCases = {
79 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
80 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
81 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x002D /* '-' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
82 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */},
83 {0x0068 /* 'h' */, 0x0065 /* 'e' */, 0x006C /* 'l' */, 0x006C /* 'l' */, 0x006F /* 'o' */},
84 {0x0041 /* 'A' */, 0x0042 /* 'B' */, 0x0043 /* 'C' */},
85 {0x0041 /* 'A' */, 0x0042 /* 'B' */, 0x0043 /* 'C' */},
86 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */, 0x0073 /* 's' */},
87 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */, 0x0073 /* 's' */},
88 {0x0062 /* 'b' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0063 /* 'c' */, 0x006B /* 'k' */, 0x0062 /* 'b' */, 0x0069 /* 'i' */, 0x0072 /* 'r' */, 0x0064 /* 'd' */},
89 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x00E9},
90 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */, 0x0072 /* 'r' */},
91 {0x00C4, 0x0042 /* 'B' */, 0x0308, 0x0043 /* 'C' */, 0x0308},
92 {0x0041 /* 'A' */, 0x0308, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
93 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */},
94 {0x0072 /* 'r' */, 0x006F /* 'o' */, 0x0302, 0x006C /* 'l' */, 0x0065 /* 'e' */},
95 {0x0041 /* 'A' */, 0x00E1, 0x0063 /* 'c' */, 0x0064 /* 'd' */},
96 {0x0041 /* 'A' */, 0x00E1, 0x0063 /* 'c' */, 0x0064 /* 'd' */},
97 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
98 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
99 {0x0054 /* 'T' */, 0x0043 /* 'C' */, 0x006F /* 'o' */, 0x006D /* 'm' */, 0x0070 /* 'p' */, 0x0061 /* 'a' */, 0x0072 /* 'r' */, 0x0065 /* 'e' */, 0x0050 /* 'P' */, 0x006C /* 'l' */, 0x0061 /* 'a' */, 0x0069 /* 'i' */, 0x006E /* 'n' */},
100 {0x0061 /* 'a' */, 0x0042 /* 'B' */, 0x0063 /* 'c' */},
101 {0x0061 /* 'a' */, 0x0023 /* '#' */, 0x0042 /* 'B' */},
102 {0x0061 /* 'a' */, 0x0026 /* '&' */, 0x0062 /* 'b' */},
103 {0x0061 /* 'a' */, 0x0023 /* '#' */, 0x0063 /* 'c' */},
104 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
105 {0x00C4, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
106 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
107 {0x00C4, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
108 {0x00C4, 0x0062 /* 'b' */, 0x0063 /* 'c' */, 0x0064 /* 'd' */, 0x0061 /* 'a' */},
109 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0023 /* '#' */, 0x0063 /* 'c' */},
110 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
111 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x003D /* '=' */, 0x0063 /* 'c' */},
112 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0064 /* 'd' */},
113 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
114 {0x0061 /* 'a' */, 0x0043 /* 'C' */, 0x0048 /* 'H' */, 0x0063 /* 'c' */},
115 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
116 {0x0074 /* 't' */, 0x0068 /* 'h' */, 0x00EE, 0x0073 /* 's' */},
117 {0x0070 /* 'p' */, 0x00E9, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x00E9},
118 {0x0061 /* 'a' */, 0x0042 /* 'B' */, 0x0043 /* 'C' */},
119 {0x0061 /* 'a' */, 0x0062 /* 'b' */, 0x0064 /* 'd' */},
120 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
121 {0x0061 /* 'a' */, 0x00C6, 0x0063 /* 'c' */},
122 {0x0061 /* 'a' */, 0x0042 /* 'B' */, 0x0064 /* 'd' */},
123 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
124 {0x0061 /* 'a' */, 0x00C6, 0x0063 /* 'c' */},
125 {0x0061 /* 'a' */, 0x0042 /* 'B' */, 0x0064 /* 'd' */},
126 {0x00E4, 0x0062 /* 'b' */, 0x0063 /* 'c' */},
127 {0x0070 /* 'p' */, 0x00EA, 0x0063 /* 'c' */, 0x0068 /* 'h' */, 0x0065 /* 'e' */}
130 private static int[] results = {
131 //-1:LESS; 0:EQUAL; 1:GREATER
133 -1, /*Collator::GREATER,*/
141 -1, /*Collator::GREATER,*/ /* 10 */
158 /* Test Tertiary > 26 */
169 /* test identical > 36 */
172 /* test primary > 38 */
178 /* test secondary > 43 */
187 private static char [][] testBugs = {
200 // 0x0300 is grave, 0x0301 is acute
201 // the order of elements in this array must be different than the order in CollationFrenchTest
202 private static char[][] testAcute = {
204 {0x65, 0x65, 0x0301},
205 {0x65, 0x65, 0x0301, 0x0300},
206 {0x65, 0x65, 0x0300},
207 {0x65, 0x65, 0x0300, 0x0301},
208 {0x65, 0x0301, 0x65},
209 {0x65, 0x0301, 0x65, 0x0301},
210 {0x65, 0x0301, 0x65, 0x0301, 0x0300},
211 {0x65, 0x0301, 0x65, 0x0300},
212 {0x65, 0x0301, 0x65, 0x0300, 0x0301},
213 {0x65, 0x0301, 0x0300, 0x65},
214 {0x65, 0x0301, 0x0300, 0x65, 0x0301},
215 {0x65, 0x0301, 0x0300, 0x65, 0x0301, 0x0300},
216 {0x65, 0x0301, 0x0300, 0x65, 0x0300},
217 {0x65, 0x0301, 0x0300, 0x65, 0x0300, 0x0301},
218 {0x65, 0x0300, 0x65},
219 {0x65, 0x0300, 0x65, 0x0301},
220 {0x65, 0x0300, 0x65, 0x0301, 0x0300},
221 {0x65, 0x0300, 0x65, 0x0300},
222 {0x65, 0x0300, 0x65, 0x0300, 0x0301},
223 {0x65, 0x0300, 0x0301, 0x65},
224 {0x65, 0x0300, 0x0301, 0x65, 0x0301},
225 {0x65, 0x0300, 0x0301, 0x65, 0x0301, 0x0300},
226 {0x65, 0x0300, 0x0301, 0x65, 0x0300},
227 {0x65, 0x0300, 0x0301, 0x65, 0x0300, 0x0301}
230 private static char[][] testMore = {
231 {0x0061 /* 'a' */, 0x0065 /* 'e' */},
234 {0x0061 /* 'a' */, 0x0066 /* 'f' */},
235 {0x006F /* 'o' */, 0x0065 /* 'e' */},
238 {0x006F /* 'o' */, 0x0066 /* 'f' */},
241 private Collator myCollation = null;
243 public CollationEnglishTest() {
245 protected void init()throws Exception{
246 myCollation = Collator.getInstance(Locale.ENGLISH);
249 //performs test with strength PRIMARY
250 public void TestPrimary() {
252 myCollation.setStrength(Collator.PRIMARY);
253 for (i = 38; i < 43 ; i++) {
254 doTest(testSourceCases[i], testTargetCases[i], results[i]);
258 //perform test with strength SECONDARY
259 public void TestSecondary() {
261 myCollation.setStrength(Collator.SECONDARY);
262 for (i = 43; i < 49 ; i++) {
263 doTest(testSourceCases[i], testTargetCases[i], results[i]);
266 //test acute and grave ordering (compare to french collation)
269 for (i = 0; i < testAcute.length; i++) {
270 for (j = 0; j < testAcute.length; j++) {
271 logln("i = " + i + "; j = " + j);
278 doTest(testAcute[i], testAcute[j], expected );
283 //perform test with strength TERTIARY
284 public void TestTertiary() {
286 myCollation.setStrength(Collator.TERTIARY);
287 //for (i = 0; i < 38 ; i++) //attention: there is something wrong with 36, 37.
288 for (i = 0; i < 38 ; i++)
290 doTest(testSourceCases[i], testTargetCases[i], results[i]);
294 for (i = 0; i < 10; i++)
296 for (j = i+1; j < 10; j++)
298 doTest(testBugs[i], testBugs[j], -1);
302 //test more interesting cases
304 for (i = 0; i < testMore.length; i++)
306 for (j = 0; j < testMore.length; j++)
314 doTest(testMore[i], testMore[j], expected );
319 // main test routine, tests rules defined by the "en" locale
320 private void doTest(char[] source, char[] target, int result){
322 String s = new String(source);
323 String t = new String(target);
324 int compareResult = myCollation.compare(s, t);
325 CollationKey sortKey1, sortKey2;
326 sortKey1 = myCollation.getCollationKey(s);
327 sortKey2 = myCollation.getCollationKey(t);
328 int keyResult = sortKey1.compareTo(sortKey2);
329 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
333 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
334 int compareResult, int keyResult, int incResult, int expectedResult ){
335 if (expectedResult < -1 || expectedResult > 1)
337 errln("***** invalid call to reportCResult ****");
341 boolean ok1 = (compareResult == expectedResult);
342 boolean ok2 = (keyResult == expectedResult);
343 boolean ok3 = (incResult == expectedResult);
345 if (ok1 && ok2 && ok3 && !isVerbose()){
348 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
349 String msg2 = "\", \"";
350 String msg3 = "\") returned ";
351 String msg4 = "; expected ";
353 String sExpect = new String("");
354 String sResult = new String("");
355 sResult = appendCompareResult(compareResult, sResult);
356 sExpect = appendCompareResult(expectedResult, sExpect);
358 logln(msg1 + source + msg2 + target + msg3 + sResult);
360 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
363 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
364 msg2 = "\").compareTo(key(\"";
365 msg3 = "\")) returned ";
366 sResult = appendCompareResult(keyResult, sResult);
368 logln(msg1 + source + msg2 + target + msg3 + sResult);
370 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
373 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
376 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
378 msg3 = "\") returned ";
380 sResult = appendCompareResult(incResult, sResult);
383 logln(msg1 + source + msg2 + target + msg3 + sResult);
385 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
390 private String appendCompareResult(int result, String target){
391 if (result == -1) //LESS
395 else if (result == 0) //EQUAL
399 else if (result == 1) //GREATER
407 target += huh + result;
412 String prettify(CollationKey sourceKey) {
414 byte[] bytes= sourceKey.toByteArray();
417 for (i = 0; i < bytes.length; i++) {
418 target += Integer.toHexString(bytes[i]);