001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.translit;
008:
009: import com.ibm.icu.text.*;
010: import com.ibm.icu.dev.test.*;
011: import com.ibm.icu.impl.Utility;
012:
013: /**
014: * @test
015: * @summary General test of CompoundTransliterator
016: */
017: public class CompoundTransliteratorTest extends TestFmwk {
018:
019: public static void main(String[] args) throws Exception {
020: new CompoundTransliteratorTest().run(args);
021: }
022:
023: public void TestConstruction() {
024: logln("Testing the construction of the compound Transliterator");
025: String names[] = { "Greek-Latin", "Latin-Devanagari",
026: "Devanagari-Latin", "Latin-Greek" };
027: Transliterator t1 = null;
028: Transliterator t2 = null;
029: Transliterator t3 = null;
030: Transliterator t4 = null;
031:
032: try {
033: t1 = Transliterator.getInstance(names[0]);
034: t2 = Transliterator.getInstance(names[1]);
035: t3 = Transliterator.getInstance(names[2]);
036: t4 = Transliterator.getInstance(names[3]);
037: } catch (IllegalArgumentException ex) {
038: errln("FAIL: Transliterator construction failed"
039: + ex.getMessage());
040: throw ex;
041: }
042:
043: Transliterator[] transarray1 = { t1 };
044: Transliterator[] transarray2 = { t1, t4 };
045: Transliterator[] transarray3 = { t4, t1, t2 };
046: Transliterator[] transarray4 = { t1, t2, t3, t4 };
047:
048: Transliterator[][] transarray = { transarray1, transarray2,
049: transarray3, transarray4 };
050: final String IDs[] = {
051: names[0],
052: names[0] + ";" + names[3],
053: names[3] + ";" + names[1] + ";" + names[2],
054: names[0] + ";" + names[1] + ";" + names[2] + ";"
055: + names[3] };
056:
057: for (int i = 0; i < 4; i++) {
058: try {
059: Transliterator cpdtrans = Transliterator
060: .getInstance(IDs[i]);
061: } catch (IllegalArgumentException ex1) {
062: errln("FAIL: construction using CompoundTransliterator(String ID) failed for "
063: + IDs[i]);
064: throw ex1;
065: }
066:
067: try {
068: Transliterator cpdtrans = Transliterator.getInstance(
069: IDs[i], Transliterator.FORWARD);
070: cpdtrans = null;
071: } catch (IllegalArgumentException ex2) {
072: errln("FAIL: construction using CompoundTransliterator(String ID, int direction=FORWARD) failed for "
073: + IDs[i]);
074: throw ex2;
075: }
076:
077: try {
078: Transliterator cpdtrans = Transliterator.getInstance(
079: IDs[i], Transliterator.REVERSE);
080: cpdtrans = null;
081: } catch (IllegalArgumentException ex3) {
082: errln("FAIL: construction using CompoundTransliterator(String ID, int direction=REVERSE) failed for "
083: + IDs[i]);
084: throw ex3;
085: }
086:
087: // try{
088: // CompoundTransliterator cpdtrans=new CompoundTransliterator(IDs[i], Transliterator.FORWARD, null);
089: // cpdtrans = null;
090: // }catch(IllegalArgumentException ex4) {
091: // errln("FAIL: construction using CompoundTransliterator(String ID, int direction=FORWARD," +
092: // "UnicodeFilter adoptedFilter=0) failed for " + IDs[i]);
093: // throw ex4;
094: // }
095: //
096: //
097: // try{
098: // CompoundTransliterator cpdtrans2=new CompoundTransliterator(transarray[i], null);
099: // cpdtrans2 = null;
100: // }catch(IllegalArgumentException ex5) {
101: // errln("FAIL: Construction using CompoundTransliterator(Transliterator transliterators[]," +
102: // "UnicodeFilter adoptedFilter = 0) failed");
103: // throw ex5;
104: // }
105:
106: }
107:
108: }
109:
110: public void TestGetTransliterator() {
111: logln("Testing the getTransliterator() API of CompoundTransliterator");
112: String ID = "Latin-Greek;Greek-Latin;Latin-Devanagari;Devanagari-Latin;Latin-Cyrillic;Cyrillic-Latin;Any-Hex;Hex-Any";
113: Transliterator ct1 = null;
114: try {
115: //ct1=new CompoundTransliterator(ID);
116: ct1 = Transliterator.getInstance(ID);
117: } catch (IllegalArgumentException iae) {
118: errln("CompoundTransliterator construction failed for ID="
119: + ID);
120: throw iae;
121: }
122: //int count=ct1.getCount();
123: Transliterator elems[] = ct1.getElements();
124: int count = elems.length;
125: String array[] = split(ID, ';');
126: if (count != array.length) {
127: errln("Error: getCount() failed. Expected:" + array.length
128: + " got:" + count);
129: }
130: for (int i = 0; i < count; i++) {
131: //String child= ct1.getTransliterator(i).getID();
132: String child = elems[i].getID();
133: if (!child.equals(array[i])) {
134: errln("Error getTransliterator() failed: Expected->"
135: + array[i] + " Got->" + child);
136: } else {
137: logln("OK: getTransliterator() passed: Expected->"
138: + array[i] + " Got->" + child);
139: }
140: }
141:
142: }
143:
144: public void TestTransliterate() {
145: logln("Testing the handleTransliterate() API of CompoundTransliterator");
146: Transliterator ct1 = null;
147: try {
148: ct1 = Transliterator.getInstance("Any-Hex;Hex-Any");
149: } catch (IllegalArgumentException iae) {
150: errln("FAIL: construction using CompoundTransliterator(String ID) failed for "
151: + "Any-Hex;Hex-Any");
152: throw iae;
153: }
154:
155: String s = "abcabc";
156: expect(ct1, s, s);
157: Transliterator.Position index = new Transliterator.Position();
158: ReplaceableString rsource2 = new ReplaceableString(s);
159: String expectedResult = s;
160: ct1.transliterate(rsource2, index);
161: ct1.finishTransliteration(rsource2, index);
162: String result = rsource2.toString();
163: expectAux(ct1.getID() + ":ReplaceableString, index(0,0,0,0)", s
164: + "->" + rsource2, result.equals(expectedResult),
165: expectedResult);
166:
167: Transliterator.Position index2 = new Transliterator.Position(1,
168: 3, 2, 3);
169: ReplaceableString rsource3 = new ReplaceableString(s);
170: ct1.transliterate(rsource3, index2);
171: ct1.finishTransliteration(rsource3, index2);
172: result = rsource3.toString();
173: expectAux(ct1.getID() + ":String, index2(1,2,2,3)", s + "->"
174: + rsource3, result.equals(expectedResult),
175: expectedResult);
176:
177: String Data[] = {
178: //ID, input string, transliterated string
179: "Any-Hex;Hex-Any;Any-Hex",
180: "hello",
181: "\\u0068\\u0065\\u006C\\u006C\\u006F",
182: "Any-Hex;Hex-Any",
183: "hello! How are you?",
184: "hello! How are you?",
185: "Devanagari-Latin;Latin-Devanagari",
186: "\u092D\u0948'\u0930'\u0935",
187: "\u092D\u0948\u0930\u0935", // quotes lost
188: "Latin-Cyrillic;Cyrillic-Latin",
189: "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h",
190: "a'b'k'd'e'f'g'h'i'j'Shch'shch'zh'h",
191: "Latin-Greek;Greek-Latin",
192: "ABGabgAKLMN",
193: "ABGabgAKLMN",
194: //"Latin-Arabic;Arabic-Latin", "Ad'r'a'b'i'k'dh'dd'gh", "Adrabikdhddgh",
195: "Hiragana-Katakana",
196: "\u3041\u308f\u3099\u306e\u304b\u3092\u3099",
197: "\u30A1\u30f7\u30ce\u30ab\u30fa",
198: "Hiragana-Katakana;Katakana-Hiragana",
199: "\u3041\u308f\u3099\u306e\u304b\u3051",
200: "\u3041\u308f\u3099\u306e\u304b\u3051",
201: "Katakana-Hiragana;Hiragana-Katakana",
202: "\u30A1\u30f7\u30ce\u30f5\u30f6",
203: "\u30A1\u30f7\u30ce\u30ab\u30b1",
204: "Latin-Katakana;Katakana-Latin",
205: "vavivuvevohuzizuzoninunasesuzezu",
206: "vavivuvevohuzizuzoninunasesuzezu", };
207: Transliterator ct2 = null;
208: for (int i = 0; i < Data.length; i += 3) {
209: try {
210: ct2 = Transliterator.getInstance(Data[i + 0]);
211: } catch (IllegalArgumentException iae2) {
212: errln("FAIL: CompoundTransliterator construction failed for "
213: + Data[i + 0]);
214: throw iae2;
215: }
216: expect(ct2, Data[i + 1], Data[i + 2]);
217: }
218:
219: }
220:
221: //======================================================================
222: // Support methods
223: //======================================================================
224:
225: /**
226: * Splits a string,
227: */
228: private static String[] split(String s, char divider) {
229:
230: // see how many there are
231: int count = 1;
232: for (int i = 0; i < s.length(); ++i) {
233: if (s.charAt(i) == divider)
234: ++count;
235: }
236:
237: // make an array with them
238: String[] result = new String[count];
239: int last = 0;
240: int current = 0;
241: int i;
242: for (i = 0; i < s.length(); ++i) {
243: if (s.charAt(i) == divider) {
244: result[current++] = s.substring(last, i);
245: last = i + 1;
246: }
247: }
248: result[current++] = s.substring(last, i);
249: return result;
250: }
251:
252: private void expect(Transliterator t, String source,
253: String expectedResult) {
254: String result = t.transliterate(source);
255: expectAux(t.getID() + ":String", source, result, expectedResult);
256:
257: ReplaceableString rsource = new ReplaceableString(source);
258: t.transliterate(rsource);
259: result = rsource.toString();
260: expectAux(t.getID() + ":Replaceable", source, result,
261: expectedResult);
262:
263: // Test keyboard (incremental) transliteration -- this result
264: // must be the same after we finalize (see below).
265: rsource.replace(0, rsource.length(), "");
266: Transliterator.Position index = new Transliterator.Position();
267: StringBuffer log = new StringBuffer();
268:
269: for (int i = 0; i < source.length(); ++i) {
270: if (i != 0) {
271: log.append(" + ");
272: }
273: log.append(source.charAt(i)).append(" -> ");
274: t.transliterate(rsource, index, String.valueOf(source
275: .charAt(i)));
276: // Append the string buffer with a vertical bar '|' where
277: // the committed index is.
278: String s = rsource.toString();
279: log.append(s.substring(0, index.start)).append('|').append(
280: s.substring(index.start));
281: }
282:
283: // As a final step in keyboard transliteration, we must call
284: // transliterate to finish off any pending partial matches that
285: // were waiting for more input.
286: t.finishTransliteration(rsource, index);
287: result = rsource.toString();
288: log.append(" => ").append(rsource.toString());
289: expectAux(t.getID() + ":Keyboard", log.toString(), result
290: .equals(expectedResult), expectedResult);
291:
292: }
293:
294: private void expectAux(String tag, String source, String result,
295: String expectedResult) {
296: expectAux(tag, source + " -> " + result, result
297: .equals(expectedResult), expectedResult);
298: }
299:
300: private void expectAux(String tag, String summary, boolean pass,
301: String expectedResult) {
302: if (pass) {
303: logln("(" + tag + ") " + Utility.escape(summary));
304: } else {
305: errln("FAIL: (" + tag + ") " + Utility.escape(summary)
306: + ", expected " + Utility.escape(expectedResult));
307: }
308: }
309: }
|