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