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