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