2 *******************************************************************************
3 * Copyright (C) 2011, 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 * 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.
30 public class CollationFrozenMonkeyTest extends TestFmwk {
32 private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
34 public static void main(String[] args) throws Exception {
35 new CollationFrozenMonkeyTest().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?");
43 Collator myPrimaryCollator;
44 Collator mySecondaryCollator;
45 Collator myTertiaryCollator;
47 Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
50 myPrimaryCollator = myCollator.cloneAsThawed();
51 myPrimaryCollator.setStrength(Collator.PRIMARY);
52 myPrimaryCollator.freeze();
54 mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
55 mySecondaryCollator.setStrength(Collator.SECONDARY);
56 mySecondaryCollator.freeze();
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");
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()));
74 CollationKey collationKey1, collationKey2;
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);
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);
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);
95 String addOne = subs + String.valueOf(0xE000);
97 collationKey1 = myPrimaryCollator.getCollationKey(subs);
98 collationKey2 = myPrimaryCollator.getCollationKey(addOne);
99 result = collationKey1.compareTo(collationKey2);
101 msg += "CollationKey(";
103 msg += ") .LT. CollationKey(";
110 result = collationKey2.compareTo(collationKey1);
112 msg += "CollationKey(";
114 msg += ") .GT. CollationKey(";
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?");
128 Collator myPrimaryCollator;
129 Collator mySecondaryCollator;
130 Collator myTertiaryCollator;
132 Collator myCollator = Collator.getInstance(new Locale("en", "CA"));
135 myPrimaryCollator = myCollator.cloneAsThawed();
136 myPrimaryCollator.setStrength(Collator.PRIMARY);
137 myPrimaryCollator.freeze();
139 mySecondaryCollator = myPrimaryCollator.cloneAsThawed();
140 mySecondaryCollator.setStrength(Collator.SECONDARY);
141 mySecondaryCollator.freeze();
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");
152 /* Seed the random-number generator with current time so that
153 * the numbers will be different every time we run.
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()));
164 int result = myTertiaryCollator.compare(subs, subt); // Tertiary
165 int revResult = myTertiaryCollator.compare(subt, subs); // Tertiary
166 report(subs, subt, result, revResult);
168 result = mySecondaryCollator.compare(subs, subt); // Secondary
169 revResult = mySecondaryCollator.compare(subt, subs); // Secondary
170 report(subs, subt, result, revResult);
172 result = myPrimaryCollator.compare(subs, subt); // Primary
173 revResult = myPrimaryCollator.compare(subt, subs); // Primary
174 report(subs, subt, result, revResult);
177 String addOne = subs + String.valueOf(0xE000);
179 result = myPrimaryCollator.compare(subs, addOne);
190 result = myPrimaryCollator.compare(addOne, subs);
201 void report(String s, String t, int result, int revResult) {
202 if (revResult != -result) {
207 msg += " round trip comparison failed";
208 msg += " (result " + result + ", reverse Result " + revResult + ")";
213 public void TestRules() {
214 String testSourceCases[] = {
215 "\u0061\u0062\u007a",
216 "\u0061\u0062\u007a",
219 String testTargetCases[] = {
220 "\u0061\u0062\u00e4",
221 "\u0061\u0062\u0061\u0308",
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;
232 myCollation = new RuleBasedCollator(newRules);
233 } catch (Exception e) {
234 warnln( "Demo Test 1 Table Collation object creation failed.");
239 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
241 logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
243 newRules = baseRules + " & z < a" + String.valueOf(0x0308);
245 myCollation = new RuleBasedCollator(newRules);
246 } catch (Exception e) {
247 errln( "Demo Test 1 Table Collation object creation failed.");
251 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
255 void doTest(RuleBasedCollator myCollation, String mysource, String target, int result) {
256 int compareResult = myCollation.compare(source, target);
257 CollationKey sortKey1, sortKey2;
260 sortKey1 = myCollation.getCollationKey(source);
261 sortKey2 = myCollation.getCollationKey(target);
262 } catch (Exception e) {
263 errln("SortKey generation Failed.\n");
266 int keyResult = sortKey1.compareTo(sortKey2);
267 reportCResult( mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
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 ****");
276 boolean ok1 = (compareResult == expectedResult);
277 boolean ok2 = (keyResult == expectedResult);
278 boolean ok3 = (incResult == expectedResult);
279 if (ok1 && ok2 && ok3 && !isVerbose()) {
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);
291 logln(msg1 + src + msg2 + target + msg3 + sResult);
293 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
295 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
296 msg2 = "\").compareTo(key(\"";
297 msg3 = "\")) returned ";
298 sResult = appendCompareResult(keyResult, sResult);
300 logln(msg1 + src + msg2 + target + msg3 + sResult);
302 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
305 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
307 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
309 msg3 = "\") returned ";
310 sResult = appendCompareResult(incResult, sResult);
312 logln(msg1 + src + msg2 + target + msg3 + sResult);
314 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
319 String appendCompareResult(int result, String target) {
320 if (result == -1) { //LESS
322 } else if (result == 0) { //EQUAL
324 } else if (result == 1) { //GREATER
328 target += huh + result;
333 String prettify(CollationKey sourceKey) {
335 byte[] bytes= sourceKey.toByteArray();
338 for (i = 0; i < bytes.length; i++) {
339 target += Integer.toHexString(bytes[i]);