2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : collate/CollationMonkeyTest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/mnkytst.cpp
\r
13 package com.ibm.icu.dev.test.collator;
\r
15 import java.util.Locale;
\r
16 import java.util.Random;
\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
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
30 public class CollationMonkeyTest extends TestFmwk {
\r
32 private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
\r
34 public static void main(String[] args) throws Exception {
\r
35 new CollationMonkeyTest().run(args);
\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
43 Collator myCollator;
\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
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
59 CollationKey collationKey1, collationKey2;
\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
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
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
83 String addOne = subs + String.valueOf(0xE000);
\r
85 collationKey1 = myCollator.getCollationKey(subs);
\r
86 collationKey2 = myCollator.getCollationKey(addOne);
\r
87 result = collationKey1.compareTo(collationKey2);
\r
89 msg += "CollationKey(";
\r
91 msg += ") .LT. CollationKey(";
\r
98 result = collationKey2.compareTo(collationKey1);
\r
100 msg += "CollationKey(";
\r
102 msg += ") .GT. CollationKey(";
\r
104 msg += ") Failed.";
\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
116 Collator myCollator;
\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
124 /* Seed the random-number generator with current time so that
\r
125 * the numbers will be different every time we run.
\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
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
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
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
152 String addOne = subs + String.valueOf(0xE000);
\r
154 result = myCollator.compare(subs, addOne);
\r
155 if (result != -1) {
\r
165 result = myCollator.compare(addOne, subs);
\r
176 void report(String s, String t, int result, int revResult) {
\r
177 if (revResult != -result) {
\r
182 msg += " round trip comparison failed";
\r
183 msg += " (result " + result + ", reverse Result " + revResult + ")";
\r
188 public void TestRules() {
\r
189 String testSourceCases[] = {
\r
190 "\u0061\u0062\u007a",
\r
191 "\u0061\u0062\u007a",
\r
194 String testTargetCases[] = {
\r
195 "\u0061\u0062\u00e4",
\r
196 "\u0061\u0062\u0061\u0308",
\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
207 myCollation = new RuleBasedCollator(newRules);
\r
208 } catch (Exception e) {
\r
209 warnln( "Demo Test 1 Table Collation object creation failed.");
\r
213 for(i=0; i<2; i++){
\r
214 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
\r
216 logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
\r
218 newRules = baseRules + " & z < a" + String.valueOf(0x0308);
\r
220 myCollation = new RuleBasedCollator(newRules);
\r
221 } catch (Exception e) {
\r
222 errln( "Demo Test 1 Table Collation object creation failed.");
\r
225 for(i=0; i<2; i++){
\r
226 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
\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
235 sortKey1 = myCollation.getCollationKey(source);
\r
236 sortKey2 = myCollation.getCollationKey(target);
\r
237 } catch (Exception e) {
\r
238 errln("SortKey generation Failed.\n");
\r
241 int keyResult = sortKey1.compareTo(sortKey2);
\r
242 reportCResult( mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
\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
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
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
266 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
268 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
270 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
271 msg2 = "\").compareTo(key(\"";
\r
272 msg3 = "\")) returned ";
\r
273 sResult = appendCompareResult(keyResult, sResult);
\r
275 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
277 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
280 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
282 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
284 msg3 = "\") returned ";
\r
285 sResult = appendCompareResult(incResult, sResult);
\r
287 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
289 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
294 String appendCompareResult(int result, String target) {
\r
295 if (result == -1) { //LESS
\r
297 } else if (result == 0) { //EQUAL
\r
299 } else if (result == 1) { //GREATER
\r
300 target += "GREATER";
\r
303 target += huh + result;
\r
308 String prettify(CollationKey sourceKey) {
\r
310 byte[] bytes= sourceKey.toByteArray();
\r
311 String target = "[";
\r
313 for (i = 0; i < bytes.length; i++) {
\r
314 target += Integer.toHexString(bytes[i]);
\r