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