xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestCLDRUtils.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 /*
2  **********************************************************************
3  * Copyright (c) 2010, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: Steven R. Loomis
7  **********************************************************************
8  */
9 package org.unicode.cldr.unittest;
10 
11 import com.ibm.icu.dev.test.TestFmwk;
12 import com.ibm.icu.text.Transform;
13 import com.ibm.icu.util.ULocale;
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 import org.unicode.cldr.util.CLDRConfig;
17 import org.unicode.cldr.util.CLDRFile;
18 import org.unicode.cldr.util.CLDRLocale;
19 import org.unicode.cldr.util.CLDRLocale.CLDRFormatter;
20 import org.unicode.cldr.util.CLDRLocale.FormatBehavior;
21 import org.unicode.cldr.util.SimpleFactory;
22 
23 /**
24  * @author srl
25  */
26 public class TestCLDRUtils extends TestFmwk {
27 
28     static Transform<String, String> SHORT_ALT_PICKER =
29             new Transform<String, String>() {
30                 @Override
31                 public String transform(@SuppressWarnings("unused") String source) {
32                     return "short";
33                 }
34             };
35 
TestVariantName()36     public void TestVariantName() {
37         CLDRFile english = CLDRConfig.getInstance().getEnglish();
38 
39         checkNames(
40                 english,
41                 "en_US_POSIX",
42                 "American English (Computer)",
43                 "US English (Computer)",
44                 "English (United States, Computer)",
45                 "English (US, Computer)");
46 
47         checkNames(
48                 english,
49                 new ULocale("en_US_POSIX").toLanguageTag(),
50                 "American English (POSIX Compliant Locale)",
51                 "US English (POSIX Compliant Locale)",
52                 "English (United States, POSIX Compliant Locale)",
53                 "English (US, POSIX Compliant Locale)");
54 
55         checkNames(
56                 english,
57                 "en_HK",
58                 "English (Hong Kong SAR China)",
59                 "English (Hong Kong)",
60                 "English (Hong Kong SAR China)",
61                 "English (Hong Kong)");
62 
63         checkNames(
64                 english,
65                 "en_GB",
66                 "British English",
67                 "UK English",
68                 "English (United Kingdom)",
69                 "English (UK)");
70 
71         checkNames(english, "eo_001", "Esperanto (world)");
72 
73         checkNames(english, "el_POLYTON", "Greek (Polytonic)");
74 
75         checkNames(english, new ULocale("el__POLYTON").toLanguageTag(), "Greek (Polytonic)");
76 
77         CLDRFile french = CLDRConfig.getInstance().getCldrFactory().make("fr", true);
78 
79         checkNames(
80                 french,
81                 "en_US_POSIX",
82                 "anglais américain (informatique)",
83                 "anglais américain (informatique)", // or "anglais (É.-U., informatique)" if short
84                 // has priority over dialect
85                 "anglais (États-Unis, informatique)",
86                 "anglais (É.-U., informatique)");
87     }
88 
89     /**
90      * @param french
91      * @param locale
92      * @param combinedLong
93      * @param otherNames : combinedShort, uncombinedLong, uncombinedShort
94      */
checkNames( CLDRFile french, String locale, String combinedLong, String... otherNames)95     private void checkNames(
96             CLDRFile french, String locale, String combinedLong, String... otherNames) {
97         assertEquals(
98                 "Test variant formatting combinedLong " + locale,
99                 combinedLong,
100                 french.getName(locale));
101         String combinedShort = otherNames.length > 0 ? otherNames[0] : combinedLong;
102         String uncombinedLong = otherNames.length > 1 ? otherNames[1] : combinedLong;
103         String uncombinedShort = otherNames.length > 2 ? otherNames[2] : uncombinedLong;
104 
105         assertEquals(
106                 "Test variant formatting combinedShort " + locale,
107                 combinedShort,
108                 french.getName(locale, false, SHORT_ALT_PICKER));
109         assertEquals(
110                 "Test variant formatting uncombinedLong " + locale,
111                 uncombinedLong,
112                 french.getName(locale, true));
113         assertEquals(
114                 "Test variant formatting uncombinedShort " + locale,
115                 uncombinedShort,
116                 french.getName(locale, true, SHORT_ALT_PICKER));
117     }
118 
TestEmptyCLDRFile()119     public void TestEmptyCLDRFile() {
120         CLDRLocale aloc = CLDRLocale.getInstance("und_AQ_NONEXISTENT");
121         logln("Testing CLDRFile.make(" + aloc.toString() + ").write()");
122         CLDRFile emptyFile = SimpleFactory.makeFile(aloc.getBaseName());
123         StringWriter outStream = new StringWriter();
124         try {
125             emptyFile.write(new PrintWriter(outStream));
126         } finally {
127             logln(
128                     aloc.getBaseName()
129                             + ".xml: "
130                             + outStream
131                                     .toString()
132                                     .replaceAll("\n", "\\\\n")
133                                     .replaceAll("\t", "\\\\t"));
134         }
135         if (outStream.toString().length() == 0) {
136             errln("Error: empty CLDRFile of " + aloc + " turned into a 0-length string.");
137         }
138     }
139 
TestCLDRLocaleFormatNoFile()140     public void TestCLDRLocaleFormatNoFile() {
141         logln("Tests for CLDRLocale:");
142         CLDRLocale.setDefaultFormatter(new CLDRFormatter(FormatBehavior.replace));
143         String tests_str[] = {
144             "",
145             "root",
146             "el__POLYTON",
147             "el_POLYTON",
148             "__UND",
149             "en",
150             "en_GB",
151             "en_Shav",
152             "en_Shav_GB",
153             "en_Latn_GB_POLYTON",
154             "nnh",
155             "sq_XK"
156         };
157         for (String s : tests_str) {
158             CLDRLocale loc = CLDRLocale.getInstance(s);
159             String str = loc.toString();
160             ULocale uloc = loc.toULocale();
161             String fromloc = new ULocale(s).toString();
162             String format = loc.getDisplayName();
163             CLDRLocale parent = loc.getParent();
164             logln(
165                     s
166                             + ":  tostring:"
167                             + str
168                             + ", uloc:"
169                             + uloc
170                             + ", fromloc:"
171                             + fromloc
172                             + ", format: "
173                             + format
174                             + ", parent:"
175                             + parent);
176         }
177 
178         CLDRLocale.setDefaultFormatter(CLDRLocale.getSimpleFormatterFor(ULocale.getDefault()));
179     }
180 
TestCLDRLocaleInheritance()181     public void TestCLDRLocaleInheritance() {
182         CLDRLocale ml = CLDRLocale.getInstance("ml");
183         CLDRLocale ml_IN = CLDRLocale.getInstance("ml_IN");
184         CLDRLocale ml_Mlym = CLDRLocale.getInstance("ml_Mlym");
185         CLDRLocale ml_Mlym_IN = CLDRLocale.getInstance("ml_Mlym_IN");
186 
187         logln("Testing descendants of " + ml + " " + ml.getDisplayName());
188         CLDRLocale areSub[][] = { // isChild returns true for i!=0
189             {ml, ml_IN, ml_Mlym, ml_Mlym_IN}, {ml_Mlym, ml_Mlym_IN},
190         };
191         for (CLDRLocale[] row : areSub) {
192             CLDRLocale parent = row[0];
193             for (CLDRLocale child : row) {
194                 // TODO move the first checkChild here if the meaning changes.
195                 if (child == parent) {
196                     continue;
197                 }
198                 checkChild(child, parent, false);
199                 checkChild(parent, child, true);
200             }
201         }
202         CLDRLocale notSub[] = { // isChild returns false
203             CLDRLocale.getInstance("mli"), CLDRLocale.getInstance("root"), CLDRLocale.ROOT
204         };
205         for (CLDRLocale child : notSub) {
206             checkChild(ml, child, false);
207         }
208     }
209 
TestCLDRLocaleEquivalence()210     public void TestCLDRLocaleEquivalence() {
211         assertSame(
212                 "root is caseless", CLDRLocale.getInstance("root"), CLDRLocale.getInstance("RoOt"));
213         assertSame("root = empty", CLDRLocale.getInstance("root"), CLDRLocale.getInstance(""));
214         assertEquals("ROOT.basename = root", "root", CLDRLocale.ROOT.getBaseName());
215         assertSame("instance of root = ROOT", CLDRLocale.getInstance("root"), CLDRLocale.ROOT);
216         String test = "zh-TW-u-co-pinyin";
217         assertEquals(test, test, CLDRLocale.getInstance(test).toLanguageTag());
218     }
219 
checkChild(CLDRLocale parent, CLDRLocale child, boolean expected)220     private boolean checkChild(CLDRLocale parent, CLDRLocale child, boolean expected) {
221         boolean got = child.childOf(parent);
222         String message =
223                 child
224                         + ".childOf("
225                         + parent
226                         + ") "
227                         + "["
228                         + child.getDisplayName()
229                         + ", "
230                         + parent.getDisplayName()
231                         + "] "
232                         + "= "
233                         + got;
234         if (got == expected) {
235             logln(message);
236         } else {
237             errln(message + ", but expected " + expected);
238         }
239         return got;
240     }
241 
242     /**
243      * @param args
244      */
main(String[] args)245     public static void main(String[] args) {
246         double deltaTime = System.currentTimeMillis();
247         new TestCLDRUtils().run(args);
248         deltaTime = System.currentTimeMillis() - deltaTime;
249         System.out.println("Seconds: " + deltaTime / 1000);
250     }
251 }
252