]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationFrenchTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationFrenchTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 /** \r
9  * Port From:   ICU4C v2.1 : Collate/CollationFrenchTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/frcoll.cpp\r
11  **/\r
12  \r
13 package com.ibm.icu.dev.test.collator;\r
14  \r
15 import java.util.Locale;\r
16 \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
21  \r
22 public class CollationFrenchTest extends TestFmwk{\r
23     public static void main(String[] args) throws Exception {\r
24         new CollationFrenchTest().run(args);\r
25     }\r
26     \r
27     private static char[][] testSourceCases = {\r
28         {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/},\r
29         {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/},\r
30         {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/},\r
31         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},\r
32         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},\r
33         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},\r
34         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},\r
35         {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/},\r
36         {0x01f1},\r
37         {0xfb00},\r
38         {0x01fa},\r
39         {0x0101}\r
40     };\r
41 \r
42     private static char[][] testTargetCases = {\r
43         {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/},\r
44         {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/},\r
45         {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/},\r
46         {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},\r
47         {0x0070/*'p'*/,  0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},\r
48         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},\r
49         {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},\r
50         {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/},\r
51         {0x01ee},\r
52         {0x25ca},\r
53         {0x00e0},\r
54         {0x01df}\r
55     };\r
56 \r
57     private static int[] results = {\r
58         -1,\r
59         -1,\r
60         -1, /*Collator::GREATER,*/\r
61         -1,\r
62         1,\r
63         1,\r
64         -1,\r
65         1,\r
66        -1, /*Collator::GREATER,*/\r
67         1,\r
68         -1,\r
69         -1\r
70     };\r
71 \r
72     // 0x0300 is grave, 0x0301 is acute\r
73     // the order of elements in this array must be different than the order in CollationEnglishTest\r
74     private static char[][] testAcute = {\r
75     /*00*/    {0x0065/*'e'*/, 0x0065/*'e'*/},\r
76     /*01*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/},\r
77     /*02*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/},\r
78     /*03*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/},\r
79     /*04*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/},\r
80     /*05*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301}, \r
81     /*06*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301},\r
82     /*07*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301},\r
83     /*08*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301},\r
84     /*09*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301},\r
85     /*0a*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301},\r
86     /*0b*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},\r
87     /*0c*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},\r
88     /*0d*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},\r
89     /*0e*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},\r
90     /*0f*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300},\r
91     /*10*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300},\r
92     /*11*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300},\r
93     /*12*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300},\r
94     /*13*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300},\r
95     /*14*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300},\r
96     /*15*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},\r
97     /*16*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},\r
98     /*17*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300},\r
99     /*18*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300}\r
100     };\r
101 \r
102     private static char[][] testBugs = {\r
103         {0x0061/*'a'*/},\r
104         {0x0041/*'A'*/},\r
105         {0x0065/*'e'*/},\r
106         {0x0045/*'E'*/},\r
107         {0x00e9},\r
108         {0x00e8},\r
109         {0x00ea},\r
110         {0x00eb},\r
111         {0x0065/*'e'*/, 0x0061/*'a'*/},\r
112         {0x0078/*'x'*/}\r
113     };\r
114     \r
115     \r
116     private Collator myCollation = null;\r
117     \r
118     public CollationFrenchTest() {\r
119 \r
120     }\r
121     protected void init()throws Exception{\r
122         myCollation = Collator.getInstance(Locale.FRENCH);\r
123     }\r
124      \r
125     // perform tests with strength TERTIARY\r
126     public void TestTertiary() {\r
127         int i = 0;\r
128         myCollation.setStrength(Collator.TERTIARY);\r
129         \r
130         for (i = 0; i < 12 ; i++) {\r
131             doTest(testSourceCases[i], testTargetCases[i], results[i]);\r
132         }\r
133     }\r
134     \r
135     // perform tests with strength SECONDARY\r
136     public void TestSecondary() {\r
137         //test acute and grave ordering\r
138         int i = 0;\r
139         int j;\r
140         int expected;\r
141 \r
142         myCollation.setStrength(Collator.SECONDARY);\r
143         \r
144         for (i = 0; i < testAcute.length; i++) {\r
145             for (j = 0; j < testAcute.length; j++) {\r
146                 if (i <  j) {\r
147                     expected = -1;\r
148                 } else if (i == j) {\r
149                     expected = 0;\r
150                 } else {\r
151                     expected = 1;\r
152                 }\r
153                 doTest(testAcute[i], testAcute[j], expected );\r
154             }\r
155         }\r
156     }\r
157 \r
158     // perform extra tests\r
159     public void TestExtra() {\r
160         int i, j;\r
161         myCollation.setStrength(Collator.TERTIARY);\r
162         for (i = 0; i < 9 ; i++) {\r
163             for (j = i + 1; j < 10; j += 1) {\r
164                 doTest(testBugs[i], testBugs[j], -1);\r
165             }\r
166         }\r
167     }\r
168     \r
169     public void TestContinuationReordering()\r
170     {\r
171         String rule = "&0x2f00 << 0x2f01";\r
172         try {\r
173             RuleBasedCollator collator = new RuleBasedCollator(rule);\r
174             collator.setFrenchCollation(true);\r
175             CollationKey key1 \r
176                         = collator.getCollationKey("a\u0325\u2f00\u2f01b\u0325");\r
177             CollationKey key2\r
178                         = collator.getCollationKey("a\u0325\u2f01\u2f01b\u0325");\r
179             if (key1.compareTo(key2) >= 0) {\r
180                 errln("Error comparing continuation strings");\r
181             }\r
182         } catch (Exception e) {\r
183             errln(e.toString());\r
184         }\r
185     }\r
186      \r
187     // main test routine, test rules specific to the french locale\r
188     private void doTest(char[] source, char[] target, int result) {\r
189         String s = new String(source);\r
190         String t = new String(target);\r
191         int compareResult = myCollation.compare(s, t);\r
192         CollationKey sortKey1, sortKey2;\r
193         sortKey1 = myCollation.getCollationKey(s);\r
194         sortKey2 = myCollation.getCollationKey(t);\r
195         int keyResult = sortKey1.compareTo(sortKey2);\r
196         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);  \r
197     }\r
198     \r
199     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
200                                 int compareResult, int keyResult, int incResult, int expectedResult ) {\r
201         if (expectedResult < -1 || expectedResult > 1) {\r
202             errln("***** invalid call to reportCResult ****");\r
203             return;\r
204         }\r
205 \r
206         boolean ok1 = (compareResult == expectedResult);\r
207         boolean ok2 = (keyResult == expectedResult);\r
208         boolean ok3 = (incResult == expectedResult);\r
209 \r
210         if (ok1 && ok2 && ok3 && !isVerbose()) {\r
211             return;    \r
212         } else {\r
213             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
214             String msg2 = "\", \"";\r
215             String msg3 = "\") returned ";\r
216             String msg4 = "; expected ";\r
217             \r
218             String sExpect = new String("");\r
219             String sResult = new String("");\r
220             sResult = appendCompareResult(compareResult, sResult);\r
221             sExpect = appendCompareResult(expectedResult, sExpect);\r
222             if (ok1) {\r
223                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
224             } else {\r
225                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
226             }\r
227             \r
228             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
229             msg2 = "\").compareTo(key(\"";\r
230             msg3 = "\")) returned ";\r
231             sResult = appendCompareResult(keyResult, sResult);\r
232             if (ok2) {\r
233                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
234             } else {\r
235                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
236                 msg1 = "  ";\r
237                 msg2 = " vs. ";\r
238                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
239             }\r
240             \r
241             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
242             msg2 = "\", \"";\r
243             msg3 = "\") returned ";\r
244 \r
245             sResult = appendCompareResult(incResult, sResult);\r
246 \r
247             if (ok3) {\r
248                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
249             } else {\r
250                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
251             }               \r
252         }\r
253     }\r
254     \r
255     private String appendCompareResult(int result, String target) {\r
256         if (result == -1) {\r
257             target += "LESS";\r
258         } else if (result == 0) {\r
259             target += "EQUAL";\r
260         } else if (result == 1) {\r
261             target += "GREATER";\r
262         } else {\r
263             String huh = "?";\r
264             target += huh + result;\r
265         }\r
266         return target;\r
267     }\r
268     \r
269     String prettify(CollationKey sourceKey) {\r
270         int i;\r
271         byte[] bytes= sourceKey.toByteArray();\r
272         String target = "[";\r
273     \r
274         for (i = 0; i < bytes.length; i++) {\r
275             target += Integer.toHexString(bytes[i]);\r
276             target += " ";\r
277         }\r
278         target += "]";\r
279         return target;\r
280     }\r
281