2 *******************************************************************************
3 * Copyright (C) 2003-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.util;
9 import java.util.Arrays;
10 import java.util.HashSet;
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;
23 * To change the template for this generated type comment go to
24 * Window>Preferences>Java>Code Generation>Code and Comments
26 public class LocaleDataTest extends TestFmwk{
28 public static void main(String[] args) throws Exception{
29 new LocaleDataTest().run(args);
32 private ULocale[] availableLocales = null;
34 public LocaleDataTest(){
36 protected void init(){
37 availableLocales = ICUResourceBundle.getAvailableULocales();
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")){
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()
62 logln("PaperSize returned the expected values for locale " + locale);
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()
71 logln("PaperSize returned the expected values for locale " + locale);
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")){
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);
90 errln("Did not get the expected measurement system for locale: "+ locale);
93 if(ms == LocaleData.MeasurementSystem.SI){
94 logln("Got the expected measurement system for locale: " + locale);
96 errln("Did not get the expected measurement system for locale: "+ locale);
102 // Simple test case for checking exemplar character type coverage
103 public void TestEnglishExemplarCharacters() {
104 final char[] testChars = {
108 0, // filler for deprecated currency exemplar
111 LocaleData ld = LocaleData.getInstance(ULocale.ENGLISH);
112 for (int type = 0; type < LocaleData.ES_COUNT; type++) {
113 UnicodeSet exSet = ld.getExemplarSet(0, type);
115 if (testChars[type] > 0 && !exSet.contains(testChars[type])) {
116 errln("Character '" + testChars[type] + "' is not included in exemplar type " + type);
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 {
128 private UnicodeSet set;
130 ExemplarGroup(UnicodeSet s, int[] scriptCodes) {
134 public int hashCode() {
136 for (int i=0; i<scs.length && i<4; i++) {
137 hash = (hash<<8)+scs[i];
141 public boolean equals(Object other) {
142 ExemplarGroup o = (ExemplarGroup)other;
143 boolean r = Arrays.equals(scs, o.scs) &&
149 public void TestExemplarSet(){
150 HashSet testedExemplars = new HashSet();
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);
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]) + ":]");
178 boolean existsInScript = false;
179 UnicodeSetIterator iter = new UnicodeSetIterator(exemplarSet);
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;
190 for(int j=0; j<sets.length; j++){
191 if(sets[j].contains(iter.string)){
192 existsInScript = true;
198 if(existsInScript == false){
199 errln("ExemplarSet containment failed for locale : "+ locale);
203 // This is expensive, so only do it if it will be visible
205 logln(locale.toString() + " exemplar " + exemplarSets[0]);
206 logln(locale.toString() + " exemplar(case-folded) " + exemplarSets[1]);
208 assertTrue(locale.toString() + " case-folded is a superset",
209 exemplarSets[1].containsAll(exemplarSets[0]));
210 if (exemplarSets[1].equals(exemplarSets[0])) {
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);
219 public void TestExemplarSet2(){
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);
232 UnicodeSet exemplarSets[] = new UnicodeSet[4];
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;
239 UnicodeSet exemplarSet = ld.getExemplarSet(option, type);
240 exemplarSets[k*2+h] = exemplarSet;
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]) + ":]");
250 boolean existsInScript = false;
251 UnicodeSetIterator iter = new UnicodeSetIterator(exemplarSet);
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;
262 for(int j=0; j<sets.length; j++){
263 if(sets[j].contains(iter.string)){
264 existsInScript = true;
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);
277 // This is expensive, so only do it if it will be visible
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]);
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])) {
291 if (exemplarSets[3].equals(exemplarSets[1])) {
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);
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
308 "foo", // Bogus locale
311 final int[] testTypes = {
312 LocaleData.ES_STANDARD,
313 LocaleData.ES_AUXILIARY,
315 LocaleData.ES_CURRENCY,
316 LocaleData.ES_PUNCTUATION,
319 final String[] testTypeNames = {
327 for (String locstr : testLocales) {
328 ULocale loc = new ULocale(locstr);
329 for (int i = 0; i < testTypes.length; i++) {
331 UnicodeSet set = LocaleData.getExemplarSet(loc, 0, testTypes[i]);
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");
339 } catch (Exception e) {
340 errln(loc + "(" + testTypeNames[i] + ") Exception:" + e.getMessage());
346 public void TestCoverage(){
347 LocaleData ld = LocaleData.getInstance();
348 boolean t = ld.getNoSubstitute();
349 ld.setNoSubstitute(t);
350 assertEquals("LocaleData get/set NoSubstitute",
352 ld.getNoSubstitute());
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));
360 public void TestFallback(){
361 LocaleData fr_FR = LocaleData.getInstance(ULocale.FRANCE);
362 LocaleData fr_CA = LocaleData.getInstance(ULocale.CANADA_FRENCH);
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));
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);
380 logln("LocaleSeparator for locale " + locale + ": " + separator);
381 if (!separator.equals(", ")) {
382 errln("Unexpected LocaleSeparator for locale: "+ locale);
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);
393 logln("LocaleSeparator for locale " + locale + ": " + separator);
394 if (!separator.equals("\u3001")) {
395 errln("Unexpected LocaleSeparator for locale: "+ locale);
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());