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