2 *******************************************************************************
\r
3 * Copyright (C) 2002-2008, 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 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
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
27 public class CollationMonkeyTest extends TestFmwk {
\r
29 private String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
\r
31 public static void main(String[] args) throws Exception {
\r
32 new CollationMonkeyTest().run(args);
\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
40 Collator myCollator;
\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
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
56 CollationKey collationKey1, collationKey2;
\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
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
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
80 String addOne = subs + String.valueOf(0xE000);
\r
82 collationKey1 = myCollator.getCollationKey(subs);
\r
83 collationKey2 = myCollator.getCollationKey(addOne);
\r
84 result = collationKey1.compareTo(collationKey2);
\r
86 msg += "CollationKey(";
\r
88 msg += ") .LT. CollationKey(";
\r
95 result = collationKey2.compareTo(collationKey1);
\r
97 msg += "CollationKey(";
\r
99 msg += ") .GT. CollationKey(";
\r
101 msg += ") Failed.";
\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
113 Collator myCollator;
\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
121 /* Seed the random-number generator with current time so that
\r
122 * the numbers will be different every time we run.
\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
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
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
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
149 String addOne = subs + String.valueOf(0xE000);
\r
151 result = myCollator.compare(subs, addOne);
\r
152 if (result != -1) {
\r
162 result = myCollator.compare(addOne, subs);
\r
173 void report(String s, String t, int result, int revResult) {
\r
174 if (revResult != -result) {
\r
179 msg += " round trip comparison failed";
\r
180 msg += " (result " + result + ", reverse Result " + revResult + ")";
\r
185 public void TestRules() {
\r
186 String testSourceCases[] = {
\r
187 "\u0061\u0062\u007a",
\r
188 "\u0061\u0062\u007a",
\r
191 String testTargetCases[] = {
\r
192 "\u0061\u0062\u00e4",
\r
193 "\u0061\u0062\u0061\u0308",
\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
204 myCollation = new RuleBasedCollator(newRules);
\r
205 } catch (Exception e) {
\r
206 warnln( "Demo Test 1 Table Collation object creation failed.");
\r
210 for(i=0; i<2; i++){
\r
211 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
\r
213 logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
\r
215 newRules = baseRules + " & z < a" + String.valueOf(0x0308);
\r
217 myCollation = new RuleBasedCollator(newRules);
\r
218 } catch (Exception e) {
\r
219 errln( "Demo Test 1 Table Collation object creation failed.");
\r
222 for(i=0; i<2; i++){
\r
223 doTest(myCollation, testSourceCases[i], testTargetCases[i], -1);
\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
232 sortKey1 = myCollation.getCollationKey(source);
\r
233 sortKey2 = myCollation.getCollationKey(target);
\r
234 } catch (Exception e) {
\r
235 errln("SortKey generation Failed.\n");
\r
238 int keyResult = sortKey1.compareTo(sortKey2);
\r
239 reportCResult( mysource, target, sortKey1, sortKey2, compareResult, keyResult, compareResult, result );
\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
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
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
263 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
265 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
267 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
268 msg2 = "\").compareTo(key(\"";
\r
269 msg3 = "\")) returned ";
\r
270 sResult = appendCompareResult(keyResult, sResult);
\r
272 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
274 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
277 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
279 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
281 msg3 = "\") returned ";
\r
282 sResult = appendCompareResult(incResult, sResult);
\r
284 logln(msg1 + src + msg2 + target + msg3 + sResult);
\r
286 errln(msg1 + src + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
291 String appendCompareResult(int result, String target) {
\r
292 if (result == -1) { //LESS
\r
294 } else if (result == 0) { //EQUAL
\r
296 } else if (result == 1) { //GREATER
\r
297 target += "GREATER";
\r
300 target += huh + result;
\r
305 String prettify(CollationKey sourceKey) {
\r
307 byte[] bytes= sourceKey.toByteArray();
\r
308 String target = "[";
\r
310 for (i = 0; i < bytes.length; i++) {
\r
311 target += Integer.toHexString(bytes[i]);
\r