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