2 *******************************************************************************
3 * Copyright (C) 2002-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.util;
9 // TODO integrate this into the test framework
11 import java.io.IOException;
12 import java.io.PrintWriter;
13 import java.text.Collator;
14 import java.util.Comparator;
15 import java.util.Iterator;
16 import java.util.Locale;
18 import java.util.TreeSet;
20 import com.ibm.icu.dev.util.BagFormatter;
21 import com.ibm.icu.dev.util.ICUPropertyFactory;
22 import com.ibm.icu.dev.util.UnicodeMap;
23 import com.ibm.icu.dev.util.UnicodeProperty;
24 import com.ibm.icu.lang.UProperty;
25 import com.ibm.icu.lang.UScript;
26 import com.ibm.icu.text.Transliterator;
27 import com.ibm.icu.text.UnicodeSet;
29 // TODO change to use test framework
30 public class TestBagFormatter {
32 static final void generatePropertyAliases(boolean showValues) {
33 generatePropertyAliases(showValues, ICUPropertyFactory.make());
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;
51 System.out.println(propAlias + "\t" + bf.join(up.getNameAliases()));
52 if (!showValues) continue;
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()));
61 values.addAll(up.getAvailableValues());
62 Iterator it2 = values.iterator();
63 while (it2.hasNext()) {
64 String valueAlias = (String)it2.next();
65 System.out.println("\t" + bf.join(valueAlias + "\t" + up.getValueAliases(valueAlias)));
71 static class NumberComparator implements Comparator {
72 public int compare(Object o1, Object o2) {
73 if (o1 == o2) return 0;
74 if (o1 == null) return 1;
75 if (o2 == null) return -1;
76 double n1 = Double.parseDouble((String)o1);
77 double n2 = Double.parseDouble((String)o2);
78 return n1 < n2 ? -1 : n1 > n2 ? 1 : 0;
82 public static void main(String[] args) throws Exception {
83 System.out.println("Start");
86 UnicodeProperty prop = ICUPropertyFactory.make().getProperty("Canonicalcombiningclass");
87 prop.getAvailableValues();
89 generatePropertyAliases(true);
91 BagFormatter bf = new BagFormatter();
93 UnicodeSet us = new UnicodeSet("[:gc=nd:]");
94 BagFormatter.CONSOLE.println("[:gc=nd:]");
95 bf.showSetNames(BagFormatter.CONSOLE,us);
97 us = new UnicodeSet("[:numeric_value=2:]");
98 BagFormatter.CONSOLE.println("[:numeric_value=2:]");
99 bf.showSetNames(BagFormatter.CONSOLE,us);
101 us = new UnicodeSet("[:numeric_type=numeric:]");
102 BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");
103 bf.showSetNames(BagFormatter.CONSOLE,us);
105 UnicodeProperty.Factory ups = ICUPropertyFactory.make();
106 us = ups.getSet("gc=mn", null, null);
107 BagFormatter.CONSOLE.println("gc=mn");
108 bf.showSetNames(BagFormatter.CONSOLE, us);
111 //showNames("Name", ".*MARK.*");
112 //showNames("NFD", "a.+");
113 //showNames("NFD", false);
114 //showNames("Lowercase_Mapping", false);
115 //TestUnicodePropertySource.test(true);
116 //showNames(".*\\ \\-.*");
122 //TestTokenizer.test();
123 //RandomCollator.generate("collationTest.txt", null);
131 TestCollator tc = new TestCollator();
132 tc.test(RuleBasedCollator.getInstance(),1000);
135 StringBuffer sb = new StringBuffer();
136 for (int i = 0; i < 100; ++i) {
139 System.out.println(sb);
143 System.out.println("End");
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;
164 //CollectionFormatter cf = new CollectionFormatter();
165 PrintWriter pw = BagFormatter.openUTF8Writer("", "countries.txt");
166 Iterator it = s.iterator();
167 while (it.hasNext()) {
168 pw.println(it.next());
175 * Use the number of significant digits to round get a rounding value.
177 /* static final double LOG10 = Math.log(10);
178 public static void useSignificantDigits(double value, int digits) {
179 double log10 = Math.log(value)/LOG10; // log[e]
183 static final UnicodeSet RTL = new UnicodeSet("[[:L:]&[[:bidi class=R:][:bidi class=AL:]]]");
185 static boolean isRTL(Locale loc) {
186 // in 2.8 we can use the exemplar characters, but for 2.6 we have to work around it
187 int[] scripts = UScript.getCode(loc);
188 return new UnicodeSet()
189 .applyIntPropertyValue(UProperty.SCRIPT, scripts == null ? UScript.LATIN : scripts[0])
190 .retainAll(RTL).size() != 0;
193 static void testIsRTL() {
194 Locale[] locales = Locale.getAvailableLocales();
195 Set s = new TreeSet();
196 for (int i = 0; i < locales.length; ++i) {
197 s.add((isRTL(locales[i]) ? "R " : "L ") + locales[i].getDisplayName());
199 Iterator it = s.iterator();
200 while (it.hasNext()) {
201 System.out.println(it.next());
205 static final Transliterator toHTML = Transliterator.createFromRules(
210 "'\"' > '"' ; ",
211 Transliterator.FORWARD);
212 static final Transliterator fromHTML = Transliterator.createFromRules(
214 "'<' < '&'[lL][Tt]';' ;" +
215 "'&' < '&'[aA][mM][pP]';' ;" +
216 "'>' < '&'[gG][tT]';' ;" +
217 "'\"' < '&'[qQ][uU][oO][tT]';' ; ",
218 Transliterator.REVERSE);
220 static void checkHTML() {
221 String foo = "& n < b < \"ab\"";
222 String fii = toHTML.transliterate(foo);
223 System.out.println("in: " + foo);
224 System.out.println("out: " + fii);
225 System.out.println("in*: " + fromHTML.transliterate(fii));
226 System.out.println("IN*: " + fromHTML.transliterate(fii.toUpperCase()));
229 static void showNames(String propAlias, boolean matches) {
230 BagFormatter bf = new BagFormatter();
232 stuff = new UnicodePropertySource.ICU()
233 .setPropertyAlias(propAlias)
234 .getPropertySet(matches, null);
235 System.out.println(bf.showSetNames(propAlias + " with " + matches, stuff));
238 static void showNames(String propAlias, String pattern) {
239 BagFormatter bf = new BagFormatter();
241 stuff = new UnicodePropertySource.ICU()
242 .setPropertyAlias(propAlias)
243 .getPropertySet(Pattern.compile(pattern).matcher(""), null);
244 System.out.println(bf.showSetNames(propAlias + "with " + pattern, stuff));