]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationCreationMethodTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationCreationMethodTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2009, International Business Machines Corporation and         *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7  \r
8 package com.ibm.icu.dev.test.collator;\r
9 \r
10 import java.util.Locale;\r
11 import java.util.Random;\r
12 \r
13 import com.ibm.icu.dev.test.*;\r
14 import com.ibm.icu.text.*;\r
15 \r
16 \r
17 /**\r
18  * \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
21  * \r
22  * @author Brian Rower - IBM - August 2008\r
23  *\r
24  */\r
25 public class CollationCreationMethodTest extends TestFmwk \r
26 {\r
27     \r
28     public static void main(String[] args) throws Exception \r
29     {\r
30         new CollationCreationMethodTest().run(args);\r
31     }\r
32 \r
33     public void TestRuleVsLocaleCreationMonkey()\r
34     {\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
39         int x,y,z;\r
40         Random r = createRandom();\r
41         String randString1;\r
42         CollationKey key1;\r
43         CollationKey key2;\r
44 \r
45 \r
46         Locale[] locales = Collator.getAvailableLocales();\r
47 \r
48         RuleBasedCollator localeCollator;\r
49         RuleBasedCollator ruleCollator;\r
50 \r
51         for(z = 0; z < 60; z++)\r
52         {\r
53             x = r.nextInt(locales.length);\r
54 \r
55             try\r
56             {\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
61             } \r
62             catch (Exception e) \r
63             {\r
64                 warnln("ERROR: in creation of collator of " + locales[x].getDisplayName() + " locale");\r
65                 return;\r
66             }\r
67 \r
68             //do it several times for each collator\r
69             int n = 3;\r
70             for(y = 0; y < n; y++)\r
71             {\r
72 \r
73                 randString1 = generateNewString(r);\r
74 \r
75                 key1 = localeCollator.getCollationKey(randString1);\r
76                 key2 = ruleCollator.getCollationKey(randString1);\r
77                \r
78                 report(locales[x].getDisplayName(), randString1, key1, key2);\r
79             }\r
80         }\r
81     }\r
82 \r
83     private String generateNewString(Random r)\r
84     {\r
85         int maxCodePoints = 40;\r
86         byte[] c = new byte[r.nextInt(maxCodePoints)*2]; //two bytes for each code point\r
87         int x;\r
88         int z;\r
89         String s = "";\r
90 \r
91         for(x = 0; x < c.length/2; x = x + 2) //once around for each UTF-16 character\r
92         {\r
93             z = r.nextInt(0x7fff); //the code point...\r
94 \r
95             c[x + 1] = (byte)z;\r
96             c[x] = (byte)(z >>> 4);\r
97         }\r
98         try\r
99         {\r
100             s = new String(c, "UTF-16BE");\r
101         }\r
102         catch(Exception e)\r
103         {\r
104             warnln("Error creating random strings");\r
105         }\r
106         return s;\r
107     }\r
108 \r
109     private void report(String localeName, String string1, CollationKey k1, CollationKey k2) \r
110     {\r
111         if (!k1.equals(k2)) \r
112         {\r
113             String msg = "";\r
114             msg += "With " + localeName + "Collator: ";\r
115             msg += string1; \r
116             msg += " failed to produce identical keys on both collators";\r
117             errln(msg);\r
118         }\r
119     }\r
120 }\r