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