]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationMonkeyTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationMonkeyTest.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/CollationMonkeyTest
10  * Source File: $ICU4CRoot/source/test/intltest/mnkytst.cpp
11  **/
12
13 package com.ibm.icu.dev.test.collator;
14
15 import java.util.Locale;
16 import java.util.Random;
17
18 import com.ibm.icu.dev.test.TestFmwk;
19 import com.ibm.icu.text.CollationKey;
20 import com.ibm.icu.text.Collator;
21 import com.ibm.icu.text.RuleBasedCollator;
22
23 /**
24  * CollationMonkeyTest is a third level test class.  This tests the random 
25  * substrings of the default test strings to verify if the compare and 
26  * sort key algorithm works correctly.  For example, any string is always
27  * less than the string itself appended with any character.
28  */
29
30 public class CollationMonkeyTest extends TestFmwk {
31     
32     private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
33     
34     public static void main(String[] args) throws Exception {
35         new CollationMonkeyTest().run(args);
36     }
37     
38     public void TestCollationKey() {
39         if(source.length() == 0) {
40             errln("CollationMonkeyTest.TestCollationKey(): source is empty - ICU_DATA not set or data missing?");
41             return;
42         }
43         Collator myCollator;
44         try {
45              myCollator = Collator.getInstance(new Locale("en", "US"));
46         } catch (Exception e) {
47             warnln("ERROR: in creation of collator of ENGLISH locale");
48             return;
49         }
50         
51         Random rand = createRandom(); // use test framework's random seed
52         int s = rand.nextInt(0x7fff) % source.length();
53         int t = rand.nextInt(0x7fff) % source.length();
54         int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
55         int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
56         String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
57         String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
58     
59         CollationKey collationKey1, collationKey2;
60     
61         myCollator.setStrength(Collator.TERTIARY);
62         collationKey1 = myCollator.getCollationKey(subs);
63         collationKey2 = myCollator.getCollationKey(subt);
64         int result = collationKey1.compareTo(collationKey2);  // Tertiary
65         int revResult = collationKey2.compareTo(collationKey1);  // Tertiary
66         report( subs, subt, result, revResult);
67     
68         myCollator.setStrength(Collator.SECONDARY);
69         collationKey1 = myCollator.getCollationKey(subs);
70         collationKey2 = myCollator.getCollationKey(subt);
71         result = collationKey1.compareTo(collationKey2);  // Secondary
72         revResult = collationKey2.compareTo(collationKey1);   // Secondary
73         report( subs, subt, result, revResult);
74     
75         myCollator.setStrength(Collator.PRIMARY);
76         collationKey1 = myCollator.getCollationKey(subs);
77         collationKey2 = myCollator.getCollationKey(subt);
78         result = collationKey1.compareTo(collationKey2);  // Primary
79         revResult = collationKey2.compareTo(collationKey1);   // Primary
80         report(subs, subt, result, revResult);
81     
82         String msg = "";
83         String addOne = subs + String.valueOf(0xE000);
84     
85         collationKey1 = myCollator.getCollationKey(subs);
86         collationKey2 = myCollator.getCollationKey(addOne);
87         result = collationKey1.compareTo(collationKey2);
88         if (result != -1) {
89             msg += "CollationKey(";
90             msg += subs;
91             msg += ") .LT. CollationKey(";
92             msg += addOne;
93             msg += ") Failed.";
94             errln(msg);
95         }
96     
97         msg = "";
98         result = collationKey2.compareTo(collationKey1);
99         if (result != 1) {
100             msg += "CollationKey(";
101             msg += addOne;
102             msg += ") .GT. CollationKey(";
103             msg += subs;
104             msg += ") Failed.";
105             errln(msg);
106         }
107     }
108     
109     // perform monkey tests using Collator.compare
110     public void TestCompare() {
111         if(source.length() == 0) {
112             errln("CollationMonkeyTest.TestCompare(): source is empty - ICU_DATA not set or data missing?");
113             return;
114         }
115         
116         Collator myCollator;
117         try {
118              myCollator = Collator.getInstance(new Locale("en", "US"));
119         } catch (Exception e) {
120             warnln("ERROR: in creation of collator of ENGLISH locale");
121             return;
122         }
123         
124         /* Seed the random-number generator with current time so that
125          * the numbers will be different every time we run.
126          */
127         
128         Random rand = createRandom(); // use test framework's random seed
129         int s = rand.nextInt(0x7fff) % source.length();
130         int t = rand.nextInt(0x7fff) % source.length();
131         int slen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
132         int tlen = Math.abs(rand.nextInt(0x7fff) % source.length() - source.length()) % source.length();
133         String subs = source.substring(Math.min(s, slen), Math.min(s + slen, source.length()));
134         String subt = source.substring(Math.min(t, tlen), Math.min(t + tlen, source.length()));
135     
136         myCollator.setStrength(Collator.TERTIARY);
137         int result = myCollator.compare(subs, subt);  // Tertiary
138         int revResult = myCollator.compare(subt, subs);  // Tertiary
139         report(subs, subt, result, revResult);
140     
141         myCollator.setStrength(Collator.SECONDARY);
142         result = myCollator.compare(subs, subt);  // Secondary
143         revResult = myCollator.compare(subt, subs);  // Secondary
144         report(subs, subt, result, revResult);
145     
146         myCollator.setStrength(Collator.PRIMARY);
147         result = myCollator.compare(subs, subt);  // Primary
148         revResult = myCollator.compare(subt, subs);  // Primary
149         report(subs, subt, result, revResult);
150     
151         String msg = "";
152         String addOne = subs + String.valueOf(0xE000);
153     
154         result = myCollator.compare(subs, addOne);
155         if (result != -1) {
156             msg += "Test : ";
157             msg += subs;
158             msg += " .LT. ";
159             msg += addOne;
160             msg += " Failed.";
161             errln(msg);
162         }
163     
164         msg = "";
165         result = myCollator.compare(addOne, subs);
166         if (result != 1) {
167             msg += "Test : ";
168             msg += addOne;
169             msg += " .GT. ";
170             msg += subs;
171             msg += " Failed.";
172             errln(msg);
173         }
174     }
175     
176     void report(String s, String t, int result, int revResult) {
177         if (revResult != -result) {
178             String msg = "";
179             msg += s; 
180             msg += " and ";
181             msg += t;
182             msg += " round trip comparison failed";
183             msg += " (result " + result + ", reverse Result " + revResult + ")"; 
184             errln(msg);
185         }
186     }
187     
188     public void TestRules() {
189         String testSourceCases[] = {
190             "\u0061\u0062\u007a", 
191             "\u0061\u0062\u007a", 
192         };
193     
194         String testTargetCases[] = {
195             "\u0061\u0062\u00e4",
196             "\u0061\u0062\u0061\u0308",
197         };
198         
199         int i=0;
200         logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
201         Collator col = Collator.getInstance(new Locale("en", "US"));
202         String baseRules = ((RuleBasedCollator)col).getRules();
203         String newRules = " & z < ";
204         newRules = baseRules + newRules + String.valueOf(0x00e4);
205         RuleBasedCollator myCollation = null;
206         try {
207             myCollation = new RuleBasedCollator(newRules);
208         } catch (Exception e) {
209             warnln( "Demo Test 1 Table Collation object creation failed.");
210             return;
211         }
212         
213         for(i=0; i<2; i++){
214             doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
215         }
216         logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
217         newRules = "";
218         newRules = baseRules + " & z < a" + String.valueOf(0x0308);
219         try {
220             myCollation = new RuleBasedCollator(newRules);
221         } catch (Exception e) {
222             errln( "Demo Test 1 Table Collation object creation failed.");
223             return;
224         }
225         for(i=0; i<2; i++){
226             doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
227         }
228     }
229     
230     void doTest(RuleBasedCollator myCollation, String mysource, String target, int result) {
231         int compareResult = myCollation.compare(source, target);
232         CollationKey sortKey1, sortKey2;
233         
234         try {
235             sortKey1 = myCollation.getCollationKey(source);
236             sortKey2 = myCollation.getCollationKey(target);
237         } catch (Exception e) {
238             errln("SortKey generation Failed.\n");
239             return;
240         }
241         int keyResult = sortKey1.compareTo(sortKey2);
242         reportCResult( mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
243     }
244     
245     public void reportCResult(String src, String target, CollationKey sourceKey, CollationKey targetKey,
246                               int compareResult, int keyResult, int incResult, int expectedResult ) {
247         if (expectedResult < -1 || expectedResult > 1) {
248             errln("***** invalid call to reportCResult ****");
249             return;
250         }
251         boolean ok1 = (compareResult == expectedResult);
252         boolean ok2 = (keyResult == expectedResult);
253         boolean ok3 = (incResult == expectedResult);
254         if (ok1 && ok2 && ok3 && !isVerbose()) {
255             return;    
256         } else {
257             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
258             String msg2 = "\", \"";
259             String msg3 = "\") returned ";
260             String msg4 = "; expected ";
261             String sExpect = new String("");
262             String sResult = new String("");
263             sResult = appendCompareResult(compareResult, sResult);
264             sExpect = appendCompareResult(expectedResult, sExpect);
265             if (ok1) {
266                 logln(msg1 + src + msg2 + target + msg3 + sResult);
267             } else {
268                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
269             }
270             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
271             msg2 = "\").compareTo(key(\"";
272             msg3 = "\")) returned ";
273             sResult = appendCompareResult(keyResult, sResult);
274             if (ok2) {
275                 logln(msg1 + src + msg2 + target + msg3 + sResult);
276             } else {
277                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
278                 msg1 = "  ";
279                 msg2 = " vs. ";
280                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
281             }
282             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
283             msg2 = "\", \"";
284             msg3 = "\") returned ";
285             sResult = appendCompareResult(incResult, sResult);
286             if (ok3) {
287                 logln(msg1 + src + msg2 + target + msg3 + sResult);
288             } else {
289                 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
290             }                
291         }
292     }
293     
294     String appendCompareResult(int result, String target) {
295         if (result == -1) {  //LESS
296             target += "LESS";
297         } else if (result == 0) {  //EQUAL
298             target += "EQUAL";
299         } else if (result == 1) {  //GREATER
300             target += "GREATER";
301         } else {
302             String huh = "?";
303             target += huh + result;
304         }
305         return target;
306     }
307     
308     String prettify(CollationKey sourceKey) {
309         int i;
310         byte[] bytes= sourceKey.toByteArray();
311         String target = "[";
312     
313         for (i = 0; i < bytes.length; i++) {
314             target += Integer.toHexString(bytes[i]);
315             target += " ";
316         }
317         target += "]";
318         return target;
319     }
320 }