2 *******************************************************************************
\r
3 * Copyright (C) 2002-2009, 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.*;
\r
14 import com.ibm.icu.text.*;
\r
19 * CollationCreationMethodTest checks to ensure that the collators act the same whether they are created by choosing a
\r
20 * locale and loading the data from file, or by using rules.
\r
22 * @author Brian Rower - IBM - August 2008
\r
25 public class CollationCreationMethodTest extends TestFmwk
\r
28 public static void main(String[] args) throws Exception
\r
30 new CollationCreationMethodTest().run(args);
\r
33 public void TestRuleVsLocaleCreationMonkey()
\r
35 //create a RBC from a collator reader by reading in a locale collation file
\r
36 //also create one simply from a rules string (which should be
\r
37 //pulled from the locale collation file)
\r
38 //and then do crazy monkey testing on it to make sure they are the same.
\r
40 Random r = createRandom();
\r
46 Locale[] locales = Collator.getAvailableLocales();
\r
48 RuleBasedCollator localeCollator;
\r
49 RuleBasedCollator ruleCollator;
\r
51 for(z = 0; z < 60; z++)
\r
53 x = r.nextInt(locales.length);
\r
57 //this is making the assumption that the only type of collator that will be made is RBC
\r
58 localeCollator = (RuleBasedCollator)Collator.getInstance(locales[x]);
\r
59 ruleCollator = new RuleBasedCollator(localeCollator.getRules());
\r
60 logln("Rules are: " + localeCollator.getRules());
\r
62 catch (Exception e)
\r
64 warnln("ERROR: in creation of collator of " + locales[x].getDisplayName() + " locale");
\r
68 //do it several times for each collator
\r
70 for(y = 0; y < n; y++)
\r
73 randString1 = generateNewString(r);
\r
75 key1 = localeCollator.getCollationKey(randString1);
\r
76 key2 = ruleCollator.getCollationKey(randString1);
\r
78 report(locales[x].getDisplayName(), randString1, key1, key2);
\r
83 private String generateNewString(Random r)
\r
85 int maxCodePoints = 40;
\r
86 byte[] c = new byte[r.nextInt(maxCodePoints)*2]; //two bytes for each code point
\r
91 for(x = 0; x < c.length/2; x = x + 2) //once around for each UTF-16 character
\r
93 z = r.nextInt(0x7fff); //the code point...
\r
96 c[x] = (byte)(z >>> 4);
\r
100 s = new String(c, "UTF-16BE");
\r
104 warnln("Error creating random strings");
\r
109 private void report(String localeName, String string1, CollationKey k1, CollationKey k2)
\r
111 if (!k1.equals(k2))
\r
114 msg += "With " + localeName + "Collator: ";
\r
116 msg += " failed to produce identical keys on both collators";
\r