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