• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.draft;
2 
3 import org.unicode.cldr.util.Timer;
4 
5 import com.ibm.icu.dev.test.TestFmwk;
6 import com.ibm.icu.text.StringTransform;
7 import com.ibm.icu.text.Transliterator;
8 import com.ibm.icu.text.UnicodeFilter;
9 
10 public class TransformTest extends TestFmwk {
11 
12     private static final boolean SHOW = false;
13     private static final int TIMING_ITERATIONS = 10000;
14 
main(String[] args)15     public static void main(String[] args) {
16         new TransformTest().run(args);
17     }
18 
TestFix()19     public void TestFix() {
20         String[][] tests = {
21             { "!=", "([:m:])*" },
22             { "==", "(\\[:m:])*" },
23             { "==", "\\Q([:m:])*\\E" },
24             { "a(?:gh|[b])", "a[b{gh}]" },
25         };
26         for (String[] test : tests) {
27             if (test[0].equals("!=")) {
28                 assertNotEquals("Should be different", test[1],
29                     PatternFixer.fixJava(test[1]));
30             } else {
31                 assertEquals("Should be equal", test[0].equals("==") ? test[1] : test[0],
32                     PatternFixer.fixJava(test[1]));
33             }
34         }
35     }
36 
TestSomeBasic()37     public void TestSomeBasic() {
38         String[] tests = {
39             "RULES",
40             "ab > AB; ::NULL; BA > CD;",
41             "ABAB",
42             "RULES",
43             "ab > AB; BA > CD;",
44             "ABAB",
45             "RULES",
46             "D { a > A;" +
47                 "c {(d)} e > X$1;" +
48                 "d > D",
49             "dabcdefgd",
50             "ad",
51             "Da",
52             "RULES",
53             "::[a-z];" +
54                 "[:L:] { b } > B;" +
55                 "B > M;" +
56                 "z > Z;" +
57                 "w > W;" +
58                 "x > X;" +
59                 "q > Q;" +
60                 "C > Y;",
61             "zB",
62             "abXbCabXbCabXbCabXbCabXbCabXbCabXbCabXbCabXbCabXbCabX"
63         };
64         boolean setRules = true;
65         String rules;
66         StringTransform transform = null;
67         Transliterator oldTransform = null;
68         for (String testCase : tests) {
69             if (testCase.equalsIgnoreCase("rules")) {
70                 setRules = true;
71                 continue;
72             }
73             if (setRules) {
74                 rules = testCase;
75                 transform = RegexTransformBuilder.createFromRules(rules);
76                 if (SHOW) logln("New:\n" + transform.toString());
77                 oldTransform = Transliterator.createFromRules("foo", rules, Transliterator.FORWARD);
78                 if (SHOW) show(oldTransform);
79                 setRules = false;
80                 continue;
81             }
82             check(TIMING_ITERATIONS, testCase, transform, transform);
83         }
84     }
85 
TestCyrillic()86     public void TestCyrillic() {
87         checkAgainstCurrent("Latin-Cyrillic", "abc", "Def", "ango");
88     }
89 
TestGreek()90     public void TestGreek() {
91         checkAgainstCurrent("Latin-Greek", "abk", "Delpho", "ango", "ago");
92     }
93 
checkAgainstCurrent(String translitId, String... tests)94     public void checkAgainstCurrent(String translitId, String... tests) {
95         Transliterator oldGreek = Transliterator.getInstance(translitId);
96         String rules = oldGreek.toRules(false);
97         if (SHOW) logln(rules);
98         StringTransform newGreek = RegexTransformBuilder.createFromRules(rules);
99         if (SHOW) logln(newGreek.toString());
100         for (String test : tests) {
101             check(TIMING_ITERATIONS, test, newGreek, oldGreek);
102         }
103     }
104 
check(int iterations, String test, StringTransform newTransform, StringTransform oldTransform)105     private void check(int iterations, String test, StringTransform newTransform, StringTransform oldTransform) {
106 
107         Timer t = new Timer();
108         String result = null;
109         String oldResult = null;
110         t.start();
111         for (int i = 0; i < iterations; ++i) {
112             result = newTransform.transform(test);
113         }
114         t.stop();
115         long newDuration = t.getDuration();
116 
117         t.start();
118         for (int i = 0; i < iterations; ++i) {
119             oldResult = oldTransform.transform(test);
120         }
121         t.stop();
122         final long oldDuration = t.getDuration();
123 
124         if (!result.equals(oldResult)) {
125             errln("BAD:\t" + test + "\t=>\tnew:\t" + result + "\t!=\told:\t" + oldResult);
126         } else {
127             logln("OK:\t" + test + "\t=>\t" + result);
128         }
129         final String percent = oldDuration == 0 ? "INF" : String.valueOf(newDuration * 100 / oldDuration - 100);
130         logln("new time: " + newDuration / 1.0 / iterations + "\told time: " + oldDuration / 1.0 / iterations
131             + "\tnew%: " + percent + "%");
132     }
133 
show(Transliterator oldTransform)134     private void show(Transliterator oldTransform) {
135         UnicodeFilter filter = oldTransform.getFilter();
136         logln("Old:\n" + (filter == null ? "" : filter.toString() + ";\n") + oldTransform.toRules(true));
137     }
138 
139 }
140