]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/LocaleDataTest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / LocaleDataTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2003-2013, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6 */
7 package com.ibm.icu.dev.test.util;
8
9 import java.util.Arrays;
10 import java.util.HashSet;
11
12 import com.ibm.icu.dev.test.TestFmwk;
13 import com.ibm.icu.impl.ICUResourceBundle;
14 import com.ibm.icu.lang.UScript;
15 import com.ibm.icu.text.UnicodeSet;
16 import com.ibm.icu.text.UnicodeSetIterator;
17 import com.ibm.icu.util.LocaleData;
18 import com.ibm.icu.util.ULocale;
19
20 /**
21  * @author ram
22  *
23  * To change the template for this generated type comment go to
24  * Window>Preferences>Java>Code Generation>Code and Comments
25  */
26 public class LocaleDataTest extends TestFmwk{
27
28     public static void main(String[] args) throws Exception{
29         new LocaleDataTest().run(args);
30     }
31     
32     private ULocale[] availableLocales = null;
33     
34     public LocaleDataTest(){
35     }
36     protected void init(){
37         availableLocales = ICUResourceBundle.getAvailableULocales();
38     }
39     public void TestPaperSize(){
40         for(int i = 0; i < availableLocales.length; i++){
41             ULocale locale = availableLocales[i];
42             LocaleData.PaperSize paperSize = LocaleData.getPaperSize(locale);
43             // skip testing of "in" .. deprecated code for Indonesian
44             String lang = locale.getLanguage();
45             if(lang.equals("in")){
46                 continue;
47             }
48             ULocale fullLoc = ULocale.addLikelySubtags(locale);
49             if(fullLoc.toString().indexOf("_BZ") >= 0 || fullLoc.toString().indexOf("_CA") >= 0 ||
50                fullLoc.toString().indexOf("_CL") >= 0 || fullLoc.toString().indexOf("_CO") >= 0 ||
51                fullLoc.toString().indexOf("_CR") >= 0 || fullLoc.toString().indexOf("_GT") >= 0 ||
52                fullLoc.toString().indexOf("_MX") >= 0 || fullLoc.toString().indexOf("_NI") >= 0 ||
53                fullLoc.toString().indexOf("_PA") >= 0 || fullLoc.toString().indexOf("_PH") >= 0 ||
54                fullLoc.toString().indexOf("_PR") >= 0 || fullLoc.toString().indexOf("_SV") >= 0 ||
55                fullLoc.toString().indexOf("_US") >= 0 || fullLoc.toString().indexOf("_VE") >= 0 ){
56                 if(paperSize.getHeight()!= 279 || paperSize.getWidth() != 216 ){
57                     errln("PaperSize did not return the expected value for locale "+ locale+
58                           " Expected height: 279 width: 216."+
59                           " Got height: "+paperSize.getHeight()+" width: "+paperSize.getWidth()
60                            );
61                 }else{
62                     logln("PaperSize returned the expected values for locale " + locale);
63                 }
64             }else{
65                 if(paperSize.getHeight()!= 297 || paperSize.getWidth() != 210 ){
66                     errln("PaperSize did not return the expected value for locale "+ locale +
67                           " Expected height: 297 width: 210."+
68                           " Got height: "+paperSize.getHeight() +" width: "+paperSize.getWidth() 
69                            );
70                 }else{
71                     logln("PaperSize returned the expected values for locale " + locale);
72                 }
73             }
74         }
75     }
76     public void TestMeasurementSystem(){
77         for(int i=0; i<availableLocales.length; i++){
78             ULocale locale = availableLocales[i];
79             LocaleData.MeasurementSystem ms = LocaleData.getMeasurementSystem(locale);
80             // skip testing of "in" .. deprecated code for Indonesian
81             String lang = locale.getLanguage();
82             if(lang.equals("in")){
83                 continue;
84             }           
85             ULocale fullLoc = ULocale.addLikelySubtags(locale);
86             if(fullLoc.toString().indexOf("_US") >= 0 || fullLoc.toString().indexOf("_MM") >= 0 || fullLoc.toString().indexOf("_LR") >= 0){
87                 if(ms == LocaleData.MeasurementSystem.US){
88                     logln("Got the expected measurement system for locale: " + locale);
89                 }else{
90                     errln("Did not get the expected measurement system for locale: "+ locale);
91                 }
92             }else{
93                 if(ms == LocaleData.MeasurementSystem.SI){
94                     logln("Got the expected measurement system for locale: " + locale);
95                 }else{
96                     errln("Did not get the expected measurement system for locale: "+ locale);
97                 } 
98             }
99         }
100     }
101
102     // Simple test case for checking exemplar character type coverage
103     public void TestEnglishExemplarCharacters() {
104         final char[] testChars = {
105             0x61,   // standard
106             0xE1,   // auxiliary
107             0x41,   // index
108             0,      // filler for deprecated currency exemplar
109             0x2D,   // punctuation
110         };
111         LocaleData ld = LocaleData.getInstance(ULocale.ENGLISH);
112         for (int type = 0; type < LocaleData.ES_COUNT; type++) {
113             UnicodeSet exSet = ld.getExemplarSet(0, type);
114             if (exSet != null) {
115                 if (testChars[type] > 0 && !exSet.contains(testChars[type])) {
116                     errln("Character '" + testChars[type] + "' is not included in exemplar type " + type);
117                 }
118             }
119         }
120     }
121
122     // Bundle together a UnicodeSet (of expemplars) and ScriptCode combination.
123     //   We keep a set of combinations that have already been tested, to
124     //   avoid repeated (time consuming) retesting of the same data.
125     //   Instances of this class must be well behaved as members of a set.
126     static class ExemplarGroup {
127         private int[] scs;
128         private UnicodeSet set;
129         
130         ExemplarGroup(UnicodeSet s, int[] scriptCodes) {
131             set = s;
132             scs = scriptCodes;
133         }
134         public int hashCode() {
135             int hash = 0;
136             for (int i=0; i<scs.length && i<4; i++) {
137                 hash = (hash<<8)+scs[i];
138             }
139             return hash;
140         }        
141         public boolean equals(Object other) {
142             ExemplarGroup o = (ExemplarGroup)other;
143             boolean r = Arrays.equals(scs, o.scs) &&
144                          set.equals(o.set);
145             return r;
146         }
147     }
148     
149     public void TestExemplarSet(){
150         HashSet  testedExemplars = new HashSet();
151         int equalCount = 0;
152         for(int i=0; i<availableLocales.length; i++){
153             ULocale locale = availableLocales[i];
154             int[] scriptCodes = UScript.getCode(locale);
155             if (scriptCodes==null) {
156                 // I hate the JDK's solution for deprecated language codes.
157                 // Why does the Locale constructor change the string I passed to it ?
158                 // such a broken hack !!!!!
159                 // so in effect I can never test the script code for Indonesian :(
160                 if(locale.toString().indexOf(("in"))<0){
161                     errln("UScript.getCode returned null for locale: " + locale); 
162                 }
163                 continue;
164             }
165             UnicodeSet exemplarSets[] = new UnicodeSet[2];
166             for (int k=0; k<2; ++k) {   // for casing option in (normal, caseInsensitive)
167                 int option = (k==0) ? 0 : UnicodeSet.CASE;
168                 UnicodeSet exemplarSet = LocaleData.getExemplarSet(locale, option);
169                 exemplarSets[k] = exemplarSet;
170                 ExemplarGroup exGrp = new ExemplarGroup(exemplarSet, scriptCodes);
171                 if (!testedExemplars.contains(exGrp)) {
172                     testedExemplars.add(exGrp);
173                     UnicodeSet[] sets = new UnicodeSet[scriptCodes.length];
174                     // create the UnicodeSets for the script
175                     for(int j=0; j < scriptCodes.length; j++){
176                         sets[j] = new UnicodeSet("[:" + UScript.getShortName(scriptCodes[j]) + ":]");
177                     }
178                     boolean existsInScript = false;
179                     UnicodeSetIterator iter = new UnicodeSetIterator(exemplarSet);
180                     // iterate over the 
181                     while (!existsInScript && iter.nextRange()) {
182                         if (iter.codepoint != UnicodeSetIterator.IS_STRING) {
183                             for(int j=0; j<sets.length; j++){
184                                 if(sets[j].contains(iter.codepoint, iter.codepointEnd)){
185                                     existsInScript = true;
186                                     break;
187                                 }
188                             }
189                         } else {
190                             for(int j=0; j<sets.length; j++){
191                                 if(sets[j].contains(iter.string)){
192                                     existsInScript = true;
193                                     break;
194                                 }
195                             }
196                         }
197                     }
198                     if(existsInScript == false){
199                         errln("ExemplarSet containment failed for locale : "+ locale);
200                     }
201                 }
202             }
203             // This is expensive, so only do it if it will be visible
204             if (isVerbose()) {
205                 logln(locale.toString() + " exemplar " + exemplarSets[0]);
206                 logln(locale.toString() + " exemplar(case-folded) " + exemplarSets[1]);
207             }
208             assertTrue(locale.toString() + " case-folded is a superset",
209                     exemplarSets[1].containsAll(exemplarSets[0]));
210             if (exemplarSets[1].equals(exemplarSets[0])) {
211                 ++equalCount;
212             }
213         }
214         // Note: The case-folded set should sometimes be a strict superset
215         // and sometimes be equal.
216         assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
217                    equalCount > 0 && equalCount < availableLocales.length);
218     }
219     public void TestExemplarSet2(){
220         int equalCount = 0;
221         HashSet  testedExemplars = new HashSet();
222         for(int i=0; i<availableLocales.length; i++){
223             ULocale locale = availableLocales[i];
224             LocaleData ld = LocaleData.getInstance(locale);
225             int[] scriptCodes = UScript.getCode(locale);
226             if (scriptCodes==null) {
227                 if(locale.toString().indexOf(("in"))<0){
228                     errln("UScript.getCode returned null for locale: "+ locale); 
229                 }
230                 continue;
231             }
232             UnicodeSet exemplarSets[] = new UnicodeSet[4];
233
234             for (int k=0; k<2; ++k) {  // for casing option in (normal, uncased)
235                 int option = (k==0) ? 0 : UnicodeSet.CASE;
236                 for(int h=0; h<2; ++h){  
237                     int type = (h==0) ? LocaleData.ES_STANDARD : LocaleData.ES_AUXILIARY;
238
239                     UnicodeSet exemplarSet = ld.getExemplarSet(option, type);
240                     exemplarSets[k*2+h] = exemplarSet;
241
242                     ExemplarGroup exGrp = new ExemplarGroup(exemplarSet, scriptCodes);
243                     if (!testedExemplars.contains(exGrp)) {
244                         testedExemplars.add(exGrp);
245                         UnicodeSet[] sets = new UnicodeSet[scriptCodes.length];
246                         // create the UnicodeSets for the script
247                         for(int j=0; j < scriptCodes.length; j++){
248                             sets[j] = new UnicodeSet("[:" + UScript.getShortName(scriptCodes[j]) + ":]");
249                         }
250                         boolean existsInScript = false;
251                         UnicodeSetIterator iter = new UnicodeSetIterator(exemplarSet);
252                         // iterate over the 
253                         while (!existsInScript && iter.nextRange()) {
254                             if (iter.codepoint != UnicodeSetIterator.IS_STRING) {
255                                 for(int j=0; j<sets.length; j++){
256                                     if(sets[j].contains(iter.codepoint, iter.codepointEnd)){
257                                         existsInScript = true;
258                                         break;
259                                     }
260                                 }
261                             } else {
262                                 for(int j=0; j<sets.length; j++){
263                                     if(sets[j].contains(iter.string)){
264                                         existsInScript = true;
265                                         break;
266                                     }
267                                 }
268                             }
269                         }
270                         // TODO: How to verify LocaleData.ES_AUXILIARY ???
271                         if(existsInScript == false && h == 0){
272                             errln("ExemplarSet containment failed for locale,option,type : "+ locale + ", " + option + ", " + type);
273                         }
274                     }
275                 }
276             }
277             // This is expensive, so only do it if it will be visible
278             if (isVerbose()) {
279                 logln(locale.toString() + " exemplar(ES_STANDARD)" + exemplarSets[0]);
280                 logln(locale.toString() + " exemplar(ES_AUXILIARY) " + exemplarSets[1]);
281                 logln(locale.toString() + " exemplar(case-folded,ES_STANDARD) " + exemplarSets[2]);
282                 logln(locale.toString() + " exemplar(case-folded,ES_AUXILIARY) " + exemplarSets[3]);
283             }
284             assertTrue(locale.toString() + " case-folded is a superset",
285                     exemplarSets[2].containsAll(exemplarSets[0]));
286             assertTrue(locale.toString() + " case-folded is a superset",
287                     exemplarSets[3].containsAll(exemplarSets[1]));
288             if (exemplarSets[2].equals(exemplarSets[0])) {
289                 ++equalCount;
290             }
291             if (exemplarSets[3].equals(exemplarSets[1])) {
292                 ++equalCount;
293             }
294         }
295         // Note: The case-folded set should sometimes be a strict superset
296         // and sometimes be equal.
297         assertTrue("case-folded is sometimes a strict superset, and sometimes equal",
298                 equalCount > 0 && equalCount < availableLocales.length * 2);
299     }
300
301     // Test case created for checking type coverage of static getExemplarSet method.
302     // See #9785, #9794 and #9795
303     public void TestExemplarSetTypes() {
304         final String[] testLocales = {
305             "am",   // No auxiliary / index exemplars as of ICU 50
306             "en",
307             "th",   // #9785
308             "foo",  // Bogus locale
309         };
310
311         final int[] testTypes = {
312             LocaleData.ES_STANDARD,
313             LocaleData.ES_AUXILIARY,
314             LocaleData.ES_INDEX,
315             LocaleData.ES_CURRENCY,
316             LocaleData.ES_PUNCTUATION,
317         };
318
319         final String[] testTypeNames = {
320                 "ES_STANDARD",
321                 "ES_AUXILIARY",
322                 "ES_INDEX",
323                 "ES_CURRENCY",
324                 "ES_PUNCTUATION",
325             };
326
327         for (String locstr : testLocales) {
328             ULocale loc = new ULocale(locstr);
329             for (int i = 0; i < testTypes.length; i++) {
330                 try {
331                     UnicodeSet set = LocaleData.getExemplarSet(loc, 0, testTypes[i]);
332                     if (set == null) {
333                         // Not sure null is really OK (#9795)
334                         logln(loc + "(" + testTypeNames[i] + ") returned null");
335                     } else if (set.isEmpty()) {
336                         // This is probably reasonable when data is absent
337                         logln(loc + "(" + testTypeNames[i] + ") returned an empty set");
338                     }
339                 } catch (Exception e) {
340                     errln(loc + "(" + testTypeNames[i] + ") Exception:" + e.getMessage());
341                 }
342             }
343         }
344     }
345
346     public void TestCoverage(){
347         LocaleData ld = LocaleData.getInstance();
348         boolean t = ld.getNoSubstitute();
349         ld.setNoSubstitute(t);
350         assertEquals("LocaleData get/set NoSubstitute",
351                 t,
352                 ld.getNoSubstitute());
353     
354         logln(ld.getDelimiter(LocaleData.QUOTATION_START));
355         logln(ld.getDelimiter(LocaleData.QUOTATION_END));
356         logln(ld.getDelimiter(LocaleData.ALT_QUOTATION_START));
357         logln(ld.getDelimiter(LocaleData.ALT_QUOTATION_END));
358     }
359
360     public void TestFallback(){
361         LocaleData fr_FR = LocaleData.getInstance(ULocale.FRANCE);
362         LocaleData fr_CA = LocaleData.getInstance(ULocale.CANADA_FRENCH);
363
364         // This better not crash when only some values are overridden
365         assertEquals("Start quotes are not equal", fr_FR.getDelimiter(LocaleData.QUOTATION_START), fr_CA.getDelimiter(LocaleData.QUOTATION_START));
366         assertEquals("End quotes are not equals", fr_FR.getDelimiter(LocaleData.QUOTATION_END), fr_CA.getDelimiter(LocaleData.QUOTATION_END));
367         assertNotEquals("Alt start quotes are equal", fr_FR.getDelimiter(LocaleData.ALT_QUOTATION_START), fr_CA.getDelimiter(LocaleData.ALT_QUOTATION_START));
368         assertNotEquals("Alt end quotes are equals", fr_FR.getDelimiter(LocaleData.ALT_QUOTATION_END), fr_CA.getDelimiter(LocaleData.ALT_QUOTATION_END));
369     }
370
371     public void TestLocaleDisplayPattern(){
372         ULocale locale = ULocale.ENGLISH;
373         LocaleData ld = LocaleData.getInstance(locale);
374         String pattern = ld.getLocaleDisplayPattern();
375         String separator = ld.getLocaleSeparator();
376         logln("LocaleDisplayPattern for locale " + locale + ": " + pattern);
377         if (!pattern.equals("{0} ({1})")) {
378           errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
379         }
380         logln("LocaleSeparator for locale " + locale + ": " + separator);
381         if (!separator.equals(", ")) {
382           errln("Unexpected LocaleSeparator for locale: "+ locale);
383         }
384
385         locale = ULocale.CHINESE;
386         ld = LocaleData.getInstance(locale);
387         pattern = ld.getLocaleDisplayPattern();
388         separator = ld.getLocaleSeparator();
389         logln("LocaleDisplayPattern for locale " + locale + ": " + pattern);
390         if (!pattern.equals("{0}\uFF08{1}\uFF09")) {
391           errln("Unexpected LocaleDisplayPattern for locale: "+ locale);
392         }
393         logln("LocaleSeparator for locale " + locale + ": " + separator);
394         if (!separator.equals("\u3001")) {
395           errln("Unexpected LocaleSeparator for locale: "+ locale);
396         }
397
398         for(int i = 0; i < availableLocales.length; i++){
399           locale = availableLocales[i];
400           ld = LocaleData.getInstance(locale);
401           logln(locale.toString() + " LocaleDisplayPattern:" + ld.getLocaleDisplayPattern());
402           logln(locale.toString() + " LocaleSeparator:" + ld.getLocaleSeparator());
403         }
404     }
405 }