xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestBagFormatter.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2016, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  *******************************************************************************
6  */
7 package org.unicode.cldr.unittest;
8 
9 // TODO integrate this into the test framework
10 
11 import com.ibm.icu.dev.util.UnicodeMap;
12 import com.ibm.icu.lang.UProperty;
13 import com.ibm.icu.lang.UScript;
14 import com.ibm.icu.text.Transliterator;
15 import com.ibm.icu.text.UnicodeSet;
16 import java.io.IOException;
17 import java.io.PrintWriter;
18 import java.text.Collator;
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.Locale;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.unicode.cldr.draft.FileUtilities;
25 import org.unicode.cldr.util.props.BagFormatter;
26 import org.unicode.cldr.util.props.ICUPropertyFactory;
27 import org.unicode.cldr.util.props.UnicodeProperty;
28 
29 // TODO change to use test framework
30 public class TestBagFormatter {
31 
generatePropertyAliases(boolean showValues)32     static final void generatePropertyAliases(boolean showValues) {
33         generatePropertyAliases(showValues, ICUPropertyFactory.make());
34     }
35 
generatePropertyAliases(boolean showValues, UnicodeProperty.Factory ups)36     static final void generatePropertyAliases(boolean showValues, UnicodeProperty.Factory ups) {
37         Collator order = Collator.getInstance(Locale.ENGLISH);
38         TreeSet props = new TreeSet(order);
39         TreeSet values = new TreeSet(order);
40         BagFormatter bf = new BagFormatter();
41         props.addAll(ups.getAvailableNames());
42         for (int i = UnicodeProperty.BINARY; i < UnicodeProperty.LIMIT_TYPE; ++i) {
43             System.out.println(UnicodeProperty.getTypeName(i));
44             Iterator it = props.iterator();
45             while (it.hasNext()) {
46                 String propAlias = (String) it.next();
47                 UnicodeProperty up = ups.getProperty(propAlias);
48                 int type = up.getType();
49                 if (type != i) continue;
50                 System.out.println();
51                 System.out.println(propAlias + "\t" + bf.join(up.getNameAliases()));
52                 if (!showValues) continue;
53                 values.clear();
54                 if (type == UnicodeProperty.NUMERIC || type == UnicodeProperty.EXTENDED_NUMERIC) {
55                     UnicodeMap um = new UnicodeMap();
56                     um.putAll(up.getUnicodeMap());
57                     System.out.println(um.toString(new NumberComparator()));
58                     continue;
59                 }
60                 values.clear();
61                 values.addAll(up.getAvailableValues());
62                 Iterator it2 = values.iterator();
63                 while (it2.hasNext()) {
64                     String valueAlias = (String) it2.next();
65                     System.out.println(
66                             "\t" + bf.join(valueAlias + "\t" + up.getValueAliases(valueAlias)));
67                 }
68             }
69         }
70     }
71 
72     static class NumberComparator implements Comparator {
73         @Override
compare(Object o1, Object o2)74         public int compare(Object o1, Object o2) {
75             if (o1 == o2) return 0;
76             if (o1 == null) return 1;
77             if (o2 == null) return -1;
78             double n1 = Double.parseDouble((String) o1);
79             double n2 = Double.parseDouble((String) o2);
80             return n1 < n2 ? -1 : n1 > n2 ? 1 : 0;
81         }
82     }
83 
main(String[] args)84     public static void main(String[] args) throws Exception {
85         System.out.println("Start");
86         try {
87             // readCharacters();
88             UnicodeProperty prop = ICUPropertyFactory.make().getProperty("Canonicalcombiningclass");
89             prop.getAvailableValues();
90 
91             generatePropertyAliases(true);
92 
93             BagFormatter bf = new BagFormatter();
94 
95             UnicodeSet us = new UnicodeSet("[:gc=nd:]");
96             FileUtilities.CONSOLE.println("[:gc=nd:]");
97             bf.showSetNames(FileUtilities.CONSOLE, us);
98 
99             us = new UnicodeSet("[:numeric_value=2:]");
100             FileUtilities.CONSOLE.println("[:numeric_value=2:]");
101             bf.showSetNames(FileUtilities.CONSOLE, us);
102 
103             us = new UnicodeSet("[:numeric_type=numeric:]");
104             FileUtilities.CONSOLE.println("[:numeric_type=numeric:]");
105             bf.showSetNames(FileUtilities.CONSOLE, us);
106 
107             UnicodeProperty.Factory ups = ICUPropertyFactory.make();
108             us = ups.getSet("gc=mn", null, null);
109             FileUtilities.CONSOLE.println("gc=mn");
110             bf.showSetNames(FileUtilities.CONSOLE, us);
111 
112             if (true) return;
113             // showNames("Name", ".*MARK.*");
114             // showNames("NFD", "a.+");
115             // showNames("NFD", false);
116             // showNames("Lowercase_Mapping", false);
117             // TestUnicodePropertySource.test(true);
118             // showNames(".*\\ \\-.*");
119 
120             // checkHTML();
121             // testIsRTL();
122 
123             // TestTokenizer.test();
124             // RandomCollator.generate("collationTest.txt", null);
125 
126             // TestPick.test();
127             // printRandoms();
128             // if (true) return;
129             // testLocales();
130             // if (true) return;
131             /*
132             TestCollator tc = new TestCollator();
133             tc.test(RuleBasedCollator.getInstance(),1000);
134             */
135             /*
136             StringBuffer sb = new StringBuffer();
137             for (int i = 0; i < 100; ++i) {
138                 sb.setLength(0);
139                 rc.nextRule(sb);
140                 System.out.println(sb);
141             }
142             */
143         } finally {
144             System.out.println("End");
145         }
146     }
147 
testLocales()148     static void testLocales() throws IOException {
149         Locale[] locales = Collator.getAvailableLocales();
150         Set s = new TreeSet(Collator.getInstance());
151         for (int i = 0; i < locales.length; ++i) {
152             String lang = locales[i].getLanguage();
153             String dlang = locales[i].getDisplayLanguage();
154             String country = locales[i].getCountry();
155             String dcountry = locales[i].getDisplayCountry();
156             if (country.equals("")) continue;
157             s.add("" + "\t" + dcountry + "\t" + country + "\t" + dlang + "\t" + lang);
158         }
159         // CollectionFormatter cf = new CollectionFormatter();
160         PrintWriter pw = FileUtilities.openUTF8Writer("", "countries.txt");
161         Iterator it = s.iterator();
162         while (it.hasNext()) {
163             pw.println(it.next());
164         }
165         pw.close();
166     }
167 
168     /*
169      * Use the number of significant digits to round get a rounding value.
170      */
171     /*    static final double LOG10 = Math.log(10);
172     public static void useSignificantDigits(double value, int digits) {
173         double log10 = Math.log(value)/LOG10; // log[e]
174 
175     }*/
176 
177     static final UnicodeSet RTL = new UnicodeSet("[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");
178 
isRTL(Locale loc)179     static boolean isRTL(Locale loc) {
180         // in 2.8 we can use the exemplar characters, but for 2.6 we have to work around it
181         int[] scripts = UScript.getCode(loc);
182         return new UnicodeSet()
183                         .applyIntPropertyValue(
184                                 UProperty.SCRIPT, scripts == null ? UScript.LATIN : scripts[0])
185                         .retainAll(RTL)
186                         .size()
187                 != 0;
188     }
189 
testIsRTL()190     static void testIsRTL() {
191         Locale[] locales = Locale.getAvailableLocales();
192         Set s = new TreeSet();
193         for (int i = 0; i < locales.length; ++i) {
194             s.add((isRTL(locales[i]) ? "R " : "L ") + locales[i].getDisplayName());
195         }
196         Iterator it = s.iterator();
197         while (it.hasNext()) {
198             System.out.println(it.next());
199         }
200     }
201 
202     static final Transliterator toHTML =
203             Transliterator.createFromRules(
204                     "any-html",
205                     "'<' > '&lt;' ;" + "'&' > '&amp;' ;" + "'>' > '&gt;' ;" + "'\"' > '&quot;' ; ",
206                     Transliterator.FORWARD);
207     static final Transliterator fromHTML =
208             Transliterator.createFromRules(
209                     "html-any",
210                     "'<' < '&'[lL][Tt]';' ;"
211                             + "'&' < '&'[aA][mM][pP]';' ;"
212                             + "'>' < '&'[gG][tT]';' ;"
213                             + "'\"' < '&'[qQ][uU][oO][tT]';' ; ",
214                     Transliterator.REVERSE);
215 
checkHTML()216     static void checkHTML() {
217         String foo = "& n < b < \"ab\"";
218         String fii = toHTML.transliterate(foo);
219         System.out.println("in: " + foo);
220         System.out.println("out: " + fii);
221         System.out.println("in*: " + fromHTML.transliterate(fii));
222         System.out.println("IN*: " + fromHTML.transliterate(fii.toUpperCase()));
223     }
224     /*
225     static void showNames(String propAlias, boolean matches) {
226         BagFormatter bf = new BagFormatter();
227         UnicodeSet stuff;
228         stuff = new UnicodePropertySource.ICU()
229             .setPropertyAlias(propAlias)
230             .getPropertySet(matches, null);
231         System.out.println(bf.showSetNames(propAlias + " with " + matches, stuff));
232     }
233 
234     static void showNames(String propAlias, String pattern) {
235         BagFormatter bf = new BagFormatter();
236         UnicodeSet stuff;
237         stuff = new UnicodePropertySource.ICU()
238             .setPropertyAlias(propAlias)
239             .getPropertySet(Pattern.compile(pattern).matcher(""), null);
240         System.out.println(bf.showSetNames(propAlias + "with " + pattern, stuff));
241     }
242     */
243 }
244