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