]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/translit/src/com/ibm/icu/dev/test/translit/CompoundTransliteratorTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / translit / src / com / ibm / icu / dev / test / translit / CompoundTransliteratorTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.translit;
8 import com.ibm.icu.dev.test.TestFmwk;
9 import com.ibm.icu.impl.Utility;
10 import com.ibm.icu.text.ReplaceableString;
11 import com.ibm.icu.text.Transliterator;
12
13
14
15 /**
16  * @test
17  * @summary General test of CompoundTransliterator
18  */
19 public class CompoundTransliteratorTest extends TestFmwk {
20
21     public static void main(String[] args) throws Exception {
22         new CompoundTransliteratorTest().run(args);
23     }
24
25     public void TestConstruction(){
26         logln("Testing the construction of the compound Transliterator");
27         String names[]={"Greek-Latin", "Latin-Devanagari", "Devanagari-Latin", "Latin-Greek"};
28
29         try {
30             Transliterator.getInstance(names[0]);
31             Transliterator.getInstance(names[1]);
32             Transliterator.getInstance(names[2]);
33             Transliterator.getInstance(names[3]);
34         }catch(IllegalArgumentException ex) {
35             errln("FAIL: Transliterator construction failed" + ex.getMessage());
36             throw ex;
37         }
38   
39         final String IDs[]={
40             names[0], 
41             names[0]+";"+names[3], 
42             names[3]+";"+names[1]+";"+names[2], 
43             names[0]+";"+names[1]+";"+names[2]+";"+names[3] 
44         };
45
46    
47         for(int i=0; i<4; i++){
48             try{
49                 Transliterator.getInstance(IDs[i]);
50             }catch(IllegalArgumentException ex1) {
51                 errln("FAIL: construction using CompoundTransliterator(String ID) failed for " + IDs[i]);
52                 throw ex1;
53             }
54
55             try{
56                 Transliterator.getInstance(IDs[i], Transliterator.FORWARD);
57             }catch(IllegalArgumentException ex2) {
58                 errln("FAIL: construction using CompoundTransliterator(String ID, int direction=FORWARD) failed for " + IDs[i]);
59                 throw ex2;
60             }
61
62             try{
63                 Transliterator.getInstance(IDs[i], Transliterator.REVERSE);
64             }catch(IllegalArgumentException ex3) {
65                 errln("FAIL: construction using CompoundTransliterator(String ID, int direction=REVERSE) failed for " + IDs[i]);
66                 throw ex3;
67             }
68
69 //            try{
70 //                CompoundTransliterator cpdtrans=new CompoundTransliterator(IDs[i], Transliterator.FORWARD, null);
71 //                cpdtrans = null;
72 //            }catch(IllegalArgumentException ex4) {
73 //                errln("FAIL: construction using CompoundTransliterator(String ID, int direction=FORWARD," +
74 //                        "UnicodeFilter adoptedFilter=0) failed for " + IDs[i]);
75 //                throw ex4;
76 //            }
77 //  
78 //       
79 //            try{
80 //                CompoundTransliterator cpdtrans2=new CompoundTransliterator(transarray[i], null);
81 //                cpdtrans2 = null;
82 //            }catch(IllegalArgumentException ex5) {
83 //                errln("FAIL: Construction using CompoundTransliterator(Transliterator transliterators[]," +
84 //                       "UnicodeFilter adoptedFilter = 0)  failed");
85 //                throw ex5;
86 //            }
87
88             
89         }
90    
91     }
92  
93     public void TestGetTransliterator(){
94         logln("Testing the getTransliterator() API of CompoundTransliterator");
95         String ID="Latin-Greek;Greek-Latin;Latin-Devanagari;Devanagari-Latin;Latin-Cyrillic;Cyrillic-Latin;Any-Hex;Hex-Any";
96         Transliterator ct1=null;
97         try{
98             //ct1=new CompoundTransliterator(ID);
99             ct1 = Transliterator.getInstance(ID);
100         }catch(IllegalArgumentException iae) {
101             errln("CompoundTransliterator construction failed for ID=" + ID);
102             throw iae;
103         }
104         //int count=ct1.getCount();
105         Transliterator elems[] = ct1.getElements();
106         int count = elems.length;
107         String array[]=split(ID, ';');
108         if (count != array.length) {
109             errln("Error: getCount() failed. Expected:" + array.length + " got:" + count);
110         }
111         for(int i=0; i < count; i++){
112             //String child= ct1.getTransliterator(i).getID();
113             String child = elems[i].getID();
114             if(!child.equals(array[i])){
115                 errln("Error getTransliterator() failed: Expected->" + array[i] + " Got->" + child);
116             }else {
117                 logln("OK: getTransliterator() passed: Expected->" + array[i] + " Got->" + child);
118             }
119         }
120
121         
122     }
123  
124        
125     public void TestTransliterate(){
126         logln("Testing the handleTransliterate() API of CompoundTransliterator");
127         Transliterator ct1=null;
128         try{
129             ct1=Transliterator.getInstance("Any-Hex;Hex-Any");
130         }catch(IllegalArgumentException iae){
131             errln("FAIL: construction using CompoundTransliterator(String ID) failed for " + "Any-Hex;Hex-Any");
132             throw iae;
133         }
134     
135         String s="abcabc";
136         expect(ct1, s, s);
137         Transliterator.Position index = new Transliterator.Position();
138         ReplaceableString rsource2=new ReplaceableString(s);
139         String expectedResult=s;
140         ct1.transliterate(rsource2, index);
141         ct1.finishTransliteration(rsource2, index);
142         String result=rsource2.toString();
143         expectAux(ct1.getID() + ":ReplaceableString, index(0,0,0,0)", s + "->" + rsource2, result.equals(expectedResult), expectedResult);
144      
145         Transliterator.Position index2 = new Transliterator.Position(1,3,2,3);
146         ReplaceableString rsource3=new ReplaceableString(s);
147         ct1.transliterate(rsource3, index2); 
148         ct1.finishTransliteration(rsource3, index2);
149         result=rsource3.toString();
150         expectAux(ct1.getID() + ":String, index2(1,2,2,3)", s + "->" + rsource3, result.equals(expectedResult), expectedResult);
151
152        
153         String Data[]={
154              //ID, input string, transliterated string
155              "Any-Hex;Hex-Any;Any-Hex",     "hello",  "\\u0068\\u0065\\u006C\\u006C\\u006F", 
156              "Any-Hex;Hex-Any",                 "hello! How are you?",  "hello! How are you?",
157              "Devanagari-Latin;Latin-Devanagari",       "\u092D\u0948'\u0930'\u0935",  "\u092D\u0948\u0930\u0935", // quotes lost
158              "Latin-Cyrillic;Cyrillic-Latin",           "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h", "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h",
159              "Latin-Greek;Greek-Latin",                 "ABGabgAKLMN", "ABGabgAKLMN",
160              //"Latin-Arabic;Arabic-Latin",               "Ad'r'a'b'i'k'dh'dd'gh", "Adrabikdhddgh",
161              "Hiragana-Katakana",                       "\u3041\u308f\u3099\u306e\u304b\u3092\u3099", 
162                                                                  "\u30A1\u30f7\u30ce\u30ab\u30fa",  
163              "Hiragana-Katakana;Katakana-Hiragana",     "\u3041\u308f\u3099\u306e\u304b\u3051", 
164                                                                  "\u3041\u308f\u3099\u306e\u304b\u3051",
165              "Katakana-Hiragana;Hiragana-Katakana",     "\u30A1\u30f7\u30ce\u30f5\u30f6", 
166                                                                  "\u30A1\u30f7\u30ce\u30ab\u30b1",  
167              "Latin-Katakana;Katakana-Latin",                   "vavivuvevohuzizuzoninunasesuzezu", 
168                                                                  "vavivuvevohuzizuzoninunasesuzezu",  
169         };
170         Transliterator ct2=null;
171         for(int i=0; i<Data.length; i+=3){
172             try{
173                 ct2=Transliterator.getInstance(Data[i+0]);
174             }catch(IllegalArgumentException iae2){
175                 errln("FAIL: CompoundTransliterator construction failed for " + Data[i+0]);
176                 throw iae2;
177             }
178         expect(ct2, Data[i+1], Data[i+2]);
179         }
180    
181     }
182  
183
184     //======================================================================
185     // Support methods
186     //======================================================================
187
188      /**
189      * Splits a string,
190     */
191     private static String[] split(String s, char divider) {
192       
193     // see how many there are
194         int count = 1;
195     for (int i = 0; i < s.length(); ++i) {
196        if (s.charAt(i) == divider) ++count;
197     }
198         
199     // make an array with them
200     String[] result = new String[count];
201     int last = 0;
202     int current = 0;
203     int i;
204     for (i = 0; i < s.length(); ++i) {
205         if (s.charAt(i) == divider) {
206             result[current++] = s.substring(last,i);
207             last = i+1;
208         }
209     }
210     result[current++] = s.substring(last,i);
211     return result;
212     }
213
214     private void expect(Transliterator t, String source, String expectedResult) {
215         String result = t.transliterate(source);
216         expectAux(t.getID() + ":String", source, result, expectedResult);
217
218         ReplaceableString rsource = new ReplaceableString(source);
219         t.transliterate(rsource);
220         result = rsource.toString();
221         expectAux(t.getID() + ":Replaceable", source, result, expectedResult);
222
223         // Test keyboard (incremental) transliteration -- this result
224         // must be the same after we finalize (see below).
225         rsource.replace(0, rsource.length(), "");
226         Transliterator.Position index = new Transliterator.Position();
227         StringBuffer log = new StringBuffer();
228
229         for (int i=0; i<source.length(); ++i) {
230             if (i != 0) {
231                 log.append(" + ");
232             }
233             log.append(source.charAt(i)).append(" -> ");
234             t.transliterate(rsource, index,
235                             String.valueOf(source.charAt(i)));
236             // Append the string buffer with a vertical bar '|' where
237             // the committed index is.
238             String s = rsource.toString();
239             log.append(s.substring(0, index.start)).
240                 append('|').
241                 append(s.substring(index.start));
242         }
243         
244         // As a final step in keyboard transliteration, we must call
245         // transliterate to finish off any pending partial matches that
246         // were waiting for more input.
247         t.finishTransliteration(rsource, index);
248         result = rsource.toString();
249         log.append(" => ").append(rsource.toString());
250         expectAux(t.getID() + ":Keyboard", log.toString(),
251                  result.equals(expectedResult),
252                  expectedResult);
253
254     }
255     private void expectAux(String tag, String source,
256                   String result, String expectedResult) {
257         expectAux(tag, source + " -> " + result,
258                  result.equals(expectedResult),
259                  expectedResult);
260     }
261
262     private void expectAux(String tag, String summary, boolean pass, String expectedResult) {
263         if (pass) {
264             logln("(" + tag + ") " + Utility.escape(summary));
265         } else {
266             errln("FAIL: (" + tag+ ") "
267                 + Utility.escape(summary)
268                 + ", expected " + Utility.escape(expectedResult));
269         }
270     }  
271 }
272