]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/collate/src/com/ibm/icu/dev/test/collator/G7CollationTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / collate / src / com / ibm / icu / dev / test / collator / G7CollationTest.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/G7CollationTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/g7coll.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 G7CollationTest extends TestFmwk{\r
23     public static void main(String[] args) throws Exception{\r
24         new G7CollationTest().run(args);\r
25         // new G7CollationTest().TestDemo3();\r
26     }\r
27     \r
28     private static String[] testCases = {\r
29         "blackbirds", "Pat", "p\u00E9ch\u00E9", "p\u00EAche", "p\u00E9cher",            \r
30         "p\u00EAcher", "Tod", "T\u00F6ne", "Tofu", "blackbird", "Ton", \r
31         "PAT", "black-bird", "black-birds", "pat", // 14\r
32         // Additional tests\r
33         "czar", "churo", "cat", "darn", "?",                                                                                /* 19 */\r
34         "quick", "#", "&", "a-rdvark", "aardvark",                                                        /* 23 */\r
35         "abbot", "co-p", "cop", "coop", "zebra"\r
36     };\r
37 \r
38     private static int[][] results = {\r
39         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_US */\r
40         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_GB */\r
41         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* en_CA */\r
42         { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_FR */\r
43         { 12, 13, 9, 0, 14, 1, 11, 3, 2, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* fr_CA */\r
44         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* de_DE */\r
45         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* it_IT */\r
46         { 12, 13, 9, 0, 14, 1, 11, 2, 3, 4, 5, 6, 8, 10, 7, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, /* ja_JP */\r
47         /* new table collation with rules "& Z < p, P"  loop to FIXEDTESTSET */\r
48         { 12, 13, 9, 0, 6, 8, 10, 7, 14, 1, 11, 2, 3, 4, 5, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, \r
49         /* new table collation with rules "& C < ch , cH, Ch, CH " loop to TOTALTESTSET */\r
50         { 19, 22, 21, 23, 24, 25, 12, 13, 9, 0, 17, 26, 28, 27, 15, 16, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 },\r
51         /* new table collation with rules "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&'  " loop to TOTALTESTSET */\r
52         { 23, 24, 25, 22, 12, 13, 9, 0, 17, 16, 26, 28, 27, 15, 18, 21, 14, 1, 11, 2, 3, 4, 5, 19, 20, 6, 8, 10, 7, 29 },\r
53         /* analogous to Japanese rules " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- " */  /* loop to TOTALTESTSET */\r
54         { 19, 22, 21, 24, 23, 25, 12, 13, 9, 0, 17, 16, 28, 26, 27, 15, 18, 14, 1, 11, 2, 3, 4, 5, 20, 6, 8, 10, 7, 29 }\r
55     };\r
56     \r
57     //private static final int MAX_TOKEN_LEN = 16;\r
58     //private static final int TESTLOCALES = 12;\r
59     private static final int FIXEDTESTSET = 15;\r
60     private static final int TOTALTESTSET = 30;\r
61     \r
62     // perform test with added rules " & Z < p, P"\r
63     public void TestDemo1() {\r
64         logln("Demo Test 1 : Create a new table collation with rules \"& Z < p, P\"");\r
65         \r
66         Collator col = Collator.getInstance(Locale.ENGLISH);    \r
67 \r
68         \r
69         String baseRules = ((RuleBasedCollator)col).getRules();\r
70         String newRules = " & Z < p, P";\r
71         newRules = baseRules + newRules; \r
72         RuleBasedCollator myCollation = null; \r
73         try {\r
74             myCollation = new RuleBasedCollator(newRules);\r
75         } catch(Exception e) {\r
76             errln("Fail to create RuleBasedCollator with rules:" + newRules);\r
77             return;\r
78         }\r
79         \r
80         int j, n;\r
81         for (j = 0; j < FIXEDTESTSET; j++) {\r
82             for (n = j+1; n < FIXEDTESTSET; n++) {\r
83                 doTest(myCollation, testCases[results[8][j]], testCases[results[8][n]], -1);\r
84             }\r
85         }\r
86     }\r
87     \r
88 \r
89     // perorm test with added rules "& C < ch , cH, Ch, CH"\r
90     public void TestDemo2() {\r
91         logln("Demo Test 2 : Create a new table collation with rules \"& C < ch , cH, Ch, CH\"");\r
92         Collator col = Collator.getInstance(Locale.ENGLISH);    \r
93 \r
94 \r
95         String baseRules = ((RuleBasedCollator)col).getRules();\r
96         String newRules = "& C < ch , cH, Ch, CH";\r
97         newRules = baseRules + newRules; \r
98         RuleBasedCollator myCollation = null; \r
99         try {\r
100             myCollation = new RuleBasedCollator(newRules);\r
101         }catch(Exception e){\r
102             errln("Fail to create RuleBasedCollator with rules:" + newRules);\r
103             return;\r
104         }  \r
105 \r
106         int j, n;\r
107         for (j = 0; j < TOTALTESTSET; j++) {\r
108             for (n = j+1; n < TOTALTESTSET; n++) {\r
109                 doTest(myCollation, testCases[results[9][j]], testCases[results[9][n]], -1);\r
110             }\r
111         }\r
112     }\r
113     \r
114 \r
115     // perform test with added rules \r
116     // "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"\r
117     public void TestDemo3() {\r
118         // logln("Demo Test 3 : Create a new table collation with rules \"& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'\"");\r
119         Collator col = Collator.getInstance(Locale.ENGLISH);    \r
120 \r
121         \r
122         String baseRules = ((RuleBasedCollator)col).getRules();\r
123         String newRules = "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'";\r
124         newRules = baseRules + newRules;\r
125         RuleBasedCollator myCollation = null; \r
126         try {\r
127             myCollation = new RuleBasedCollator(newRules);\r
128         }catch(Exception e){\r
129             errln("Fail to create RuleBasedCollator with rules:" + newRules);\r
130             return;\r
131         }  \r
132 \r
133         int j, n;\r
134         for (j = 0; j < TOTALTESTSET; j++) {\r
135             for (n = j+1; n < TOTALTESTSET; n++) {\r
136                 doTest(myCollation, testCases[results[10][j]], testCases[results[10][n]], -1);\r
137             }\r
138         }\r
139     }\r
140     \r
141 \r
142     // perform test with added rules \r
143     // " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "\r
144     public void TestDemo4() {\r
145         logln("Demo Test 4 : Create a new table collation with rules \" & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' \"");\r
146         Collator col = Collator.getInstance(Locale.ENGLISH);    \r
147 \r
148         String baseRules = ((RuleBasedCollator)col).getRules();\r
149         String newRules = " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";\r
150         newRules = baseRules + newRules;\r
151         RuleBasedCollator myCollation = null; \r
152         try {\r
153             myCollation = new RuleBasedCollator(newRules);\r
154         }catch(Exception e){\r
155             errln("Fail to create RuleBasedCollator with rules:" + newRules);\r
156             return;\r
157         }  \r
158 \r
159         int j, n;\r
160         for (j = 0; j < TOTALTESTSET; j++) {\r
161             for (n = j+1; n < TOTALTESTSET; n++) {\r
162                 doTest(myCollation, testCases[results[11][j]], testCases[results[11][n]], -1);\r
163             }\r
164         }\r
165     }\r
166     \r
167     public void TestG7Data() {\r
168         Locale locales[] = {\r
169                 Locale.US,\r
170                 Locale.UK,\r
171                 Locale.CANADA,\r
172                 Locale.FRANCE,\r
173                 Locale.CANADA_FRENCH,\r
174                 Locale.GERMANY,\r
175                 Locale.JAPAN,\r
176                 Locale.ITALY\r
177             };\r
178         int i = 0, j = 0;\r
179         for (i = 0; i < locales.length; i++) {\r
180             Collator myCollation= null;\r
181             RuleBasedCollator tblColl1 = null;\r
182             try {\r
183                 myCollation = Collator.getInstance(locales[i]);\r
184                 tblColl1 = new RuleBasedCollator(((RuleBasedCollator)myCollation).getRules());\r
185             } catch (Exception foo) {\r
186                 warnln("Exception: " + foo.getMessage() +\r
187                       "; Locale : " + locales[i].getDisplayName() + " getRules failed");\r
188                 continue;\r
189             }\r
190             for (j = 0; j < FIXEDTESTSET; j++) {\r
191                 for (int n = j+1; n < FIXEDTESTSET; n++) {\r
192                     doTest(tblColl1, testCases[results[i][j]], testCases[results[i][n]], -1);\r
193                 }\r
194             }\r
195             myCollation = null;\r
196         }\r
197     }\r
198     \r
199     \r
200     // main test routine, tests comparisons for a set of strings against sets of expected results\r
201     private void doTest(Collator myCollation, String source, String target, \r
202                         int result){\r
203         \r
204         int compareResult = myCollation.compare(source, target);\r
205         CollationKey sortKey1, sortKey2;\r
206         sortKey1 = myCollation.getCollationKey(source);\r
207         sortKey2 = myCollation.getCollationKey(target);\r
208         int keyResult = sortKey1.compareTo(sortKey2);\r
209         reportCResult(source, target, sortKey1, sortKey2, compareResult, \r
210                       keyResult, compareResult, result);\r
211     }\r
212     \r
213     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
214                                 int compareResult, int keyResult, int incResult, int expectedResult ){\r
215         if (expectedResult < -1 || expectedResult > 1) {\r
216             errln("***** invalid call to reportCResult ****");\r
217             return;\r
218         }\r
219 \r
220         boolean ok1 = (compareResult == expectedResult);\r
221         boolean ok2 = (keyResult == expectedResult);\r
222         boolean ok3 = (incResult == expectedResult);\r
223 \r
224         if (ok1 && ok2 && ok3 && !isVerbose()){\r
225             return;    \r
226         } else {\r
227             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
228             String msg2 = "\", \"";\r
229             String msg3 = "\") returned ";\r
230             String msg4 = "; expected ";\r
231             \r
232             String sExpect = new String("");\r
233             String sResult = new String("");\r
234             sResult = appendCompareResult(compareResult, sResult);\r
235             sExpect = appendCompareResult(expectedResult, sExpect);\r
236             if (ok1) {\r
237                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
238             } else {\r
239                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
240             }\r
241             \r
242             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
243             msg2 = "\").compareTo(key(\"";\r
244             msg3 = "\")) returned ";\r
245             sResult = appendCompareResult(keyResult, sResult);\r
246             if (ok2) {\r
247                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
248             } else {\r
249                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
250                 msg1 = "  ";\r
251                 msg2 = " vs. ";\r
252                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
253             }\r
254             \r
255             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
256             msg2 = "\", \"";\r
257             msg3 = "\") returned ";\r
258 \r
259             sResult = appendCompareResult(incResult, sResult);\r
260 \r
261             if (ok3) {\r
262                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
263             } else {\r
264                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
265             }                \r
266         }\r
267     }\r
268     \r
269     private String appendCompareResult(int result, String target){\r
270         if (result == -1) {\r
271             target += "LESS";\r
272         } else if (result == 0) {\r
273             target += "EQUAL";\r
274         } else if (result == 1) {\r
275             target += "GREATER";\r
276         } else {\r
277             String huh = "?";\r
278             target += huh + result;\r
279         }\r
280         return target;\r
281     }\r
282     \r
283    String prettify(CollationKey sourceKey) {\r
284         int i;\r
285         byte[] bytes= sourceKey.toByteArray();\r
286         String target = "[";\r
287     \r
288         for (i = 0; i < bytes.length; i++) {\r
289             target += Integer.toHexString(bytes[i]);\r
290             target += " ";\r
291         }\r
292         target += "]";\r
293         return target;\r
294     }\r
295 }