2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.dev.test.collator;
10 import java.util.Locale;
11 import java.util.Random;
13 import com.ibm.icu.dev.test.TestFmwk;
14 import com.ibm.icu.text.CollationKey;
15 import com.ibm.icu.text.Collator;
16 import com.ibm.icu.text.RuleBasedCollator;
21 * CollationCreationMethodTest checks to ensure that the collators act the same whether they are created by choosing a
22 * locale and loading the data from file, or by using rules.
24 * @author Brian Rower - IBM - August 2008
27 public class CollationCreationMethodTest extends TestFmwk
30 public static void main(String[] args) throws Exception
32 new CollationCreationMethodTest().run(args);
35 public void TestRuleVsLocaleCreationMonkey()
37 //create a RBC from a collator reader by reading in a locale collation file
38 //also create one simply from a rules string (which should be
39 //pulled from the locale collation file)
40 //and then do crazy monkey testing on it to make sure they are the same.
42 Random r = createRandom();
48 Locale[] locales = Collator.getAvailableLocales();
50 RuleBasedCollator localeCollator;
51 RuleBasedCollator ruleCollator;
53 for(z = 0; z < 60; z++)
55 x = r.nextInt(locales.length);
56 Locale locale = locales[x];
60 //this is making the assumption that the only type of collator that will be made is RBC
61 localeCollator = (RuleBasedCollator)Collator.getInstance(locale);
62 logln("Rules for " + locale + " are: " + localeCollator.getRules());
63 ruleCollator = new RuleBasedCollator(localeCollator.getRules());
67 warnln("ERROR: in creation of collator of locale " + locale.getDisplayName() + ": " + e);
71 //do it several times for each collator
73 for(y = 0; y < n; y++)
76 randString1 = generateNewString(r);
78 key1 = localeCollator.getCollationKey(randString1);
79 key2 = ruleCollator.getCollationKey(randString1);
81 report(locale.getDisplayName(), randString1, key1, key2);
86 private String generateNewString(Random r)
88 int maxCodePoints = 40;
89 byte[] c = new byte[r.nextInt(maxCodePoints)*2]; //two bytes for each code point
94 for(x = 0; x < c.length/2; x = x + 2) //once around for each UTF-16 character
96 z = r.nextInt(0x7fff); //the code point...
99 c[x] = (byte)(z >>> 4);
103 s = new String(c, "UTF-16BE");
107 warnln("Error creating random strings");
112 private void report(String localeName, String string1, CollationKey k1, CollationKey k2)
116 StringBuilder msg = new StringBuilder();
117 msg.append("With ").append(localeName).append(" collator\n and input string: ").append(string1).append('\n');
118 msg.append(" failed to produce identical keys on both collators\n");
119 msg.append(" localeCollator key: ").append(CollationMiscTest.prettify(k1)).append('\n');
120 msg.append(" ruleCollator key: ").append(CollationMiscTest.prettify(k2)).append('\n');
121 errln(msg.toString());