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