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