]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationDummyTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationDummyTest.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/CollationDummyTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/allcoll.cpp\r
11  *              $ICU4CRoot/source/test/cintltst/callcoll.c\r
12  **/\r
13  \r
14 package com.ibm.icu.dev.test.collator;\r
15  \r
16 import com.ibm.icu.dev.test.*;\r
17 import com.ibm.icu.text.*;\r
18 \r
19 import java.util.Locale;\r
20  \r
21 public class CollationDummyTest extends TestFmwk {\r
22     public static void main(String[] args) throws Exception {\r
23         new CollationDummyTest().run(args);\r
24         // new CollationDummyTest().TestVariableTop();\r
25     }\r
26     \r
27     //testSourceCases[][] and testTargetCases[][], testCases[][] are ported from the file callcoll.c in icu4c\r
28     private static char[][] testSourceCases = {\r
29         {0x61, 0x62, 0x27, 0x63},\r
30         {0x63, 0x6f, 0x2d, 0x6f, 0x70},\r
31         {0x61, 0x62},\r
32         {0x61, 0x6d, 0x70, 0x65, 0x72, 0x73, 0x61, 0x64},\r
33         {0x61, 0x6c, 0x6c},\r
34         {0x66, 0x6f, 0x75, 0x72},\r
35         {0x66, 0x69, 0x76, 0x65},\r
36         {0x31},\r
37         {0x31},\r
38         {0x31},                                            //  10 \r
39         {0x32},\r
40         {0x32},\r
41         {0x48, 0x65, 0x6c, 0x6c, 0x6f},\r
42         {0x61, 0x3c, 0x62},\r
43         {0x61, 0x3c, 0x62},\r
44         {0x61, 0x63, 0x63},\r
45         {0x61, 0x63, 0x48, 0x63},  //  simple test \r
46         {0x70, 0x00EA, 0x63, 0x68, 0x65},\r
47         {0x61, 0x62, 0x63},\r
48         {0x61, 0x62, 0x63},                                  //  20 \r
49         {0x61, 0x62, 0x63},\r
50         {0x61, 0x62, 0x63},\r
51         {0x61, 0x62, 0x63},\r
52         {0x61, 0x00E6, 0x63},\r
53         {0x61, 0x63, 0x48, 0x63},  //  primary test \r
54         {0x62, 0x6c, 0x61, 0x63, 0x6b},\r
55         {0x66, 0x6f, 0x75, 0x72},\r
56         {0x66, 0x69, 0x76, 0x65},\r
57         {0x31},\r
58         {0x61, 0x62, 0x63},                                        //  30 \r
59         {0x61, 0x62, 0x63},                                  \r
60         {0x61, 0x62, 0x63, 0x48},\r
61         {0x61, 0x62, 0x63},\r
62         {0x61, 0x63, 0x48, 0x63},                              //  34 \r
63         {0x61, 0x63, 0x65, 0x30},\r
64         {0x31, 0x30},\r
65         {0x70, 0x00EA,0x30}                                    // 37     \r
66     };\r
67 \r
68     private static char[][] testTargetCases = {\r
69         {0x61, 0x62, 0x63, 0x27},\r
70         {0x43, 0x4f, 0x4f, 0x50},\r
71         {0x61, 0x62, 0x63},\r
72         {0x26},\r
73         {0x26},\r
74         {0x34},\r
75         {0x35},\r
76         {0x6f, 0x6e, 0x65},\r
77         {0x6e, 0x6e, 0x65},\r
78         {0x70, 0x6e, 0x65},                                  //  10 \r
79         {0x74, 0x77, 0x6f},\r
80         {0x75, 0x77, 0x6f},\r
81         {0x68, 0x65, 0x6c, 0x6c, 0x4f},\r
82         {0x61, 0x3c, 0x3d, 0x62},\r
83         {0x61, 0x62, 0x63},\r
84         {0x61, 0x43, 0x48, 0x63},\r
85         {0x61, 0x43, 0x48, 0x63},  //  simple test \r
86         {0x70, 0x00E9, 0x63, 0x68, 0x00E9},\r
87         {0x61, 0x62, 0x63},\r
88         {0x61, 0x42, 0x43},                                  //  20 \r
89         {0x61, 0x62, 0x63, 0x68},\r
90         {0x61, 0x62, 0x64},\r
91         {0x00E4, 0x62, 0x63},\r
92         {0x61, 0x00C6, 0x63},\r
93         {0x61, 0x43, 0x48, 0x63},  //  primary test \r
94         {0x62, 0x6c, 0x61, 0x63, 0x6b, 0x2d, 0x62, 0x69, 0x72, 0x64},\r
95         {0x34},\r
96         {0x35},\r
97         {0x6f, 0x6e, 0x65},\r
98         {0x61, 0x62, 0x63},\r
99         {0x61, 0x42, 0x63},                                  //  30 \r
100         {0x61, 0x62, 0x63, 0x68},\r
101         {0x61, 0x62, 0x64},\r
102         {0x61, 0x43, 0x48, 0x63},                                //  34 \r
103         {0x61, 0x63, 0x65, 0x30},\r
104         {0x31, 0x30},\r
105         {0x70, 0x00EB,0x30}                                    // 37 \r
106     };\r
107     \r
108     private static char[][] testCases = {\r
109         {0x61},\r
110         {0x41},\r
111         {0x00e4},\r
112         {0x00c4},\r
113         {0x61, 0x65},\r
114         {0x61, 0x45},\r
115         {0x41, 0x65},\r
116         {0x41, 0x45},\r
117         {0x00e6},\r
118         {0x00c6},\r
119         {0x62},\r
120         {0x63},\r
121         {0x7a}\r
122     };\r
123     \r
124     int[] results = {\r
125         -1,\r
126         -1, //Collator::GREATER,\r
127         -1,\r
128         -1,\r
129         -1,\r
130         -1,\r
131         -1,\r
132         1,\r
133         1,\r
134         -1,                                     //  10 \r
135         1,\r
136         -1,\r
137         1,\r
138         1,\r
139         -1,\r
140         -1,\r
141         -1,\r
142     //  test primary > 17 \r
143         0,\r
144         0,\r
145         0,                                    //  20 \r
146         -1,\r
147         -1,\r
148         0,\r
149         0,\r
150         0,\r
151         -1,\r
152     //  test secondary > 26 \r
153         0,\r
154         0,\r
155         0,\r
156         0,\r
157         0,                                    //  30 \r
158         0,\r
159         -1,\r
160         0,                                     //  34 \r
161         0,\r
162         0,\r
163         -1 \r
164     };\r
165     \r
166     final int MAX_TOKEN_LEN = 16;\r
167     \r
168     public RuleBasedCollator myCollation;\r
169     \r
170     public CollationDummyTest() {\r
171     }\r
172     protected void init() throws Exception{\r
173         String ruleset = "& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";\r
174         // String ruleset = "& Four, 4";\r
175         myCollation = null;\r
176         myCollation = new RuleBasedCollator(ruleset);\r
177     }\r
178     \r
179     // perform test with strength tertiary\r
180     public void TestTertiary() {\r
181         int i = 0;\r
182         myCollation.setStrength(Collator.TERTIARY);\r
183         for (i = 0; i < 17 ; i++) {\r
184             doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);\r
185         }\r
186     }\r
187 \r
188     // perform test with strength PRIMARY\r
189     public void TestPrimary() {\r
190        // problem in strcollinc for unfinshed contractions \r
191        myCollation.setStrength(Collator.PRIMARY);\r
192         for (int i = 17; i < 26 ; i++) {\r
193             doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);\r
194         }\r
195     }\r
196 \r
197     //perform test with strength SECONDARY\r
198     public void TestSecondary() {\r
199         int i;\r
200         myCollation.setStrength(Collator.SECONDARY);\r
201         for (i = 26; i < 34; i++) {\r
202             doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);\r
203         }\r
204     }\r
205 \r
206     // perform extra tests\r
207     public void TestExtra() {\r
208         int i, j;\r
209         myCollation.setStrength(Collator.TERTIARY);\r
210         for (i = 0; i < testCases.length - 1; i++) {\r
211             for (j = i + 1; j < testCases.length; j += 1) {\r
212                 doTest(myCollation, testCases[i], testCases[j], -1);\r
213             }\r
214         }\r
215     }\r
216 \r
217     public void TestIdentical() {\r
218         int i;\r
219         myCollation.setStrength(Collator.IDENTICAL);\r
220         for (i= 34; i<37; i++) {\r
221             doTest(myCollation, testSourceCases[i], testTargetCases[i], results[i]);\r
222         }\r
223     }\r
224 \r
225     public void TestJB581() {\r
226         String source = "THISISATEST.";\r
227         String target = "Thisisatest.";\r
228         Collator coll = null;\r
229         try {\r
230             coll = Collator.getInstance(Locale.ENGLISH);\r
231         } catch (Exception e) {\r
232             errln("ERROR: Failed to create the collator for : en_US\n");\r
233             return;\r
234         }\r
235 \r
236         int result = coll.compare(source, target);\r
237         // result is 1, secondary differences only for ignorable space characters\r
238         if (result != 1) {\r
239             errln("Comparing two strings with only secondary differences in C failed.\n");\r
240             return;\r
241         }\r
242         \r
243         // To compare them with just primary differences \r
244         coll.setStrength(Collator.PRIMARY);\r
245         result = coll.compare(source, target);\r
246         // result is 0 \r
247         if (result != 0) {\r
248             errln("Comparing two strings with no differences in C failed.\n");\r
249             return;\r
250         } \r
251           \r
252         // Now, do the same comparison with keys \r
253         CollationKey sourceKeyOut, targetKeyOut;\r
254         sourceKeyOut = coll.getCollationKey(source);\r
255         targetKeyOut = coll.getCollationKey(target);\r
256         result = sourceKeyOut.compareTo(targetKeyOut);\r
257         if (result != 0) {\r
258             errln("Comparing two strings with sort keys in C failed.\n");\r
259             return;\r
260         }\r
261     }\r
262     \r
263     //TestSurrogates() is ported from cintltst/callcoll.c\r
264     \r
265     /**\r
266     * Tests surrogate support.\r
267     */\r
268     public void TestSurrogates() \r
269     {\r
270         String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";\r
271         String source[] = {"z",\r
272                            "\uD800\uDC00",\r
273                            "\ud800\udc0a\u0308",\r
274                            "\ud800\udc02"    \r
275         };\r
276         \r
277         String target[] = {"\uD800\uDC00",\r
278                            "\ud800\udc0a\u0308",\r
279                            "A",\r
280                            "\ud800\udc03"    \r
281         };\r
282     \r
283         // this test is to verify the supplementary sort key order in the english \r
284         // collator\r
285         Collator enCollation;\r
286         try {\r
287             enCollation = Collator.getInstance(Locale.ENGLISH);\r
288         } catch (Exception e) {\r
289             errln("ERROR: Failed to create the collator for ENGLISH");\r
290             return;       \r
291         }\r
292         \r
293         myCollation.setStrength(Collator.TERTIARY);\r
294         int count = 0;\r
295         // logln("start of english collation supplementary characters test\n");\r
296         while (count < 2) {\r
297             doTest(enCollation, source[count], target[count], -1);\r
298             count ++;\r
299         }\r
300         doTest(enCollation, source[count], target[count], 1);\r
301             \r
302         // logln("start of tailored collation supplementary characters test\n");\r
303         count = 0;\r
304         Collator newCollation;\r
305         try {\r
306             newCollation = new RuleBasedCollator(rules);\r
307         } catch (Exception e) {\r
308             errln("ERROR: Failed to create the collator for rules");\r
309             return;       \r
310         }\r
311         \r
312         // tests getting collation elements for surrogates for tailored rules \r
313         while (count < 4) {\r
314             doTest(newCollation, source[count], target[count], -1);\r
315             count ++;\r
316         }\r
317     \r
318         // tests that \uD801\uDC01 still has the same value, not changed \r
319         CollationKey enKey = enCollation.getCollationKey(source[3]);\r
320         CollationKey newKey = newCollation.getCollationKey(source[3]);\r
321         int keyResult = enKey.compareTo(newKey);\r
322         if(keyResult != 0) {\r
323             errln("Failed : non-tailored supplementary characters should have the same value\n");\r
324         }\r
325     }\r
326     \r
327     //TestVariableTop() is ported from cintltst/callcoll.c\r
328     /**\r
329     * Tests the [variable top] tag in rule syntax. Since the default [alternate]\r
330     * tag has the value shifted, any codepoints before [variable top] should give\r
331     * a primary ce of 0.\r
332     */\r
333     public void TestVariableTop() {\r
334         String rule = "&z = [variable top]";\r
335         Collator  myColl;\r
336         Collator  enColl;\r
337         char[] source = new char[1];\r
338         char ch;\r
339         int expected[] = {0};\r
340     \r
341         try {\r
342             enColl = Collator.getInstance(Locale.ENGLISH);\r
343         } catch (Exception e) {\r
344             errln("ERROR: Failed to create the collator for ENGLISH");\r
345             return;\r
346         }\r
347         \r
348         try{\r
349             myColl = new RuleBasedCollator(rule);\r
350         } catch(Exception e){\r
351             errln("Fail to create RuleBasedCollator with rules:" + rule);\r
352             return;\r
353         }  \r
354         enColl.setStrength(Collator.PRIMARY);\r
355         myColl.setStrength(Collator.PRIMARY);\r
356         \r
357         ((RuleBasedCollator)enColl).setAlternateHandlingShifted(true);\r
358         ((RuleBasedCollator)myColl).setAlternateHandlingShifted(true);\r
359         \r
360         if(((RuleBasedCollator)enColl).isAlternateHandlingShifted() != true) {\r
361             errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");\r
362         }\r
363         \r
364         // space is supposed to be a variable \r
365         CollationKey key = enColl.getCollationKey(" ");   \r
366         byte[] result = key.toByteArray(); \r
367         \r
368         for(int i = 0; i < result.length; i++) {\r
369             if(result[i]!= expected[i]) {\r
370                 errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");  \r
371                 break;  \r
372             }\r
373         }\r
374         \r
375         ch = 'a';\r
376         while (ch < 'z') {\r
377             source[0] = ch;\r
378             key = myColl.getCollationKey(new String(source));\r
379             result = key.toByteArray();\r
380             \r
381             for(int i = 0; i < result.length; i++) {\r
382                 if(result[i]!= expected[i]) {\r
383                     errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");  \r
384                     break;  \r
385                 }\r
386             }\r
387             ch ++;\r
388         }\r
389     }\r
390     \r
391     public void TestJB1401() {\r
392         Collator     myCollator = null;\r
393         char[] NFD_UnsafeStartChars = {\r
394             0x0f73,          // Tibetan Vowel Sign II \r
395             0x0f75,          // Tibetan Vowel Sign UU \r
396             0x0f81,          // Tibetan Vowel Sign Reversed II \r
397             0\r
398         };\r
399         int i;\r
400     \r
401         try{\r
402             myCollator = Collator.getInstance(Locale.ENGLISH);\r
403         } catch(Exception e) {\r
404             errln("ERROR: Failed to create the collator for ENGLISH");\r
405             return;\r
406         }\r
407         myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
408         for (i=0; ; i++) {\r
409             // Get the next funny character to be tested, and set up the\r
410             // three test strings X, Y, Z, consisting of an A-grave + test char,\r
411             // in original form, NFD, and then NFC form.\r
412             char c = NFD_UnsafeStartChars[i];\r
413             if (c==0) {break;}\r
414             \r
415             String x = "\u00C0" + c;       // \u00C0 is A Grave\r
416             String y;\r
417             String z;\r
418     \r
419             try{\r
420                 y = Normalizer.decompose(x, false);\r
421                 z = Normalizer.decompose(y, true);\r
422             } catch (Exception e) {\r
423                 errln("ERROR: Failed to normalize test of character" + c);\r
424                 return;\r
425             }\r
426        \r
427             // Collation test.  All three strings should be equal.\r
428             // doTest does both strcoll and sort keys, with params in both orders.\r
429             doTest(myCollator, x, y, 0);\r
430             doTest(myCollator, x, z, 0);\r
431             doTest(myCollator, y, z, 0); \r
432     \r
433             // Run collation element iterators over the three strings.  Results should be same for each.\r
434              \r
435             {\r
436                 CollationElementIterator ceiX, ceiY, ceiZ;\r
437                 int ceX, ceY, ceZ;\r
438                 int j;\r
439                 try {\r
440                     ceiX = ((RuleBasedCollator)myCollator).getCollationElementIterator(x);\r
441                     ceiY = ((RuleBasedCollator)myCollator).getCollationElementIterator(y);\r
442                     ceiZ = ((RuleBasedCollator)myCollator).getCollationElementIterator(z);\r
443                 } catch(Exception e) {\r
444                     errln("ERROR: getCollationElementIterator failed");\r
445                     return;\r
446                 }\r
447     \r
448                 for (j=0;; j++) {\r
449                     try{\r
450                         ceX = ceiX.next();\r
451                         ceY = ceiY.next();\r
452                         ceZ = ceiZ.next();\r
453                     } catch (Exception e) {\r
454                         errln("ERROR: CollationElementIterator.next failed for iteration " + j);\r
455                         break;\r
456                     }\r
457                   \r
458                     if (ceX != ceY || ceY != ceZ) {\r
459                         errln("ERROR: ucol_next failed for iteration " + j);\r
460                         break;\r
461                     }\r
462                     if (ceX == CollationElementIterator.NULLORDER) {\r
463                         break;\r
464                     }\r
465                 }\r
466             }\r
467         }\r
468     }\r
469     \r
470     // main test method called with different strengths,\r
471     // tests comparison of custum collation with different strengths\r
472     \r
473     private void doTest(Collator collation, char[] source, char[] target, int result) {\r
474         String s = new String(source);\r
475         String t = new String(target);\r
476         doTestVariant(collation, s, t, result);\r
477         if(result == -1) {\r
478             doTestVariant(collation, t, s, 1);\r
479         } else if(result == 1) {\r
480             doTestVariant(collation, t, s, -1);\r
481         } else {\r
482             doTestVariant(collation, t, s, 0);\r
483         }\r
484     }\r
485     \r
486     // main test method called with different strengths,\r
487     // tests comparison of custum collation with different strengths\r
488     \r
489     private void doTest(Collator collation,String s, String t, int result) {\r
490         doTestVariant(collation, s, t, result);\r
491         if(result == -1) {\r
492             doTestVariant(collation, t, s, 1);\r
493         } else if(result == 1) {\r
494             doTestVariant(collation, t, s, -1);\r
495         } else {\r
496             doTestVariant(collation, t, s, 0);\r
497         }\r
498     }\r
499     \r
500     private void doTestVariant(Collator collation, String source, String target, int result) {\r
501         int compareResult = collation.compare(source, target);\r
502         CollationKey srckey , tgtkey;\r
503         srckey = collation.getCollationKey(source);\r
504         tgtkey = collation.getCollationKey(target);\r
505         int keyResult = srckey.compareTo(tgtkey);\r
506         if (compareResult != result) {\r
507             errln("String comparison failed in variant test\n");\r
508         }\r
509         if (keyResult != result) {\r
510             errln("Collation key comparison failed in variant test\n");\r
511         }\r
512     }\r
513 }