]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/cpdtrtst.cpp
ICU-6.2.6.tar.gz
[apple/icu.git] / icuSources / test / intltest / cpdtrtst.cpp
1 /***************************************************************************
2 *
3 * Copyright (C) 2000-2003, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *
6 ************************************************************************
7 * Date Name Description
8 * 03/09/2000 Madhu Creation.
9 ************************************************************************/
10
11 #include "unicode/utypes.h"
12
13 #if !UCONFIG_NO_TRANSLITERATION
14
15 #include "cpdtrtst.h"
16 #include "unicode/utypes.h"
17 #include "unicode/translit.h"
18 #include "unicode/uniset.h"
19 #include "cpdtrans.h"
20 #include "cmemory.h"
21
22 //---------------------------------------------
23 // runIndexedTest
24 //---------------------------------------------
25
26 void
27 CompoundTransliteratorTest::runIndexedTest(int32_t index, UBool exec,
28 const char* &name, char* /*par*/) {
29 switch (index) {
30 TESTCASE(0,TestConstruction);
31 TESTCASE(1,TestCloneEqual);
32 TESTCASE(2,TestGetCount);
33 TESTCASE(3,TestGetSetAdoptTransliterator);
34 TESTCASE(4,TestTransliterate);
35 default: name = ""; break;
36 }
37 }
38
39 void CompoundTransliteratorTest::TestConstruction(){
40 logln("Testing the construction of the compound Transliterator");
41 UnicodeString names[]={"Greek-Latin", "Latin-Devanagari", "Devanagari-Latin", "Latin-Greek"};
42 UParseError parseError;
43 UErrorCode status=U_ZERO_ERROR;
44 Transliterator* t1=Transliterator::createInstance(names[0], UTRANS_FORWARD, parseError, status);
45 Transliterator* t2=Transliterator::createInstance(names[1], UTRANS_FORWARD, parseError, status);
46 Transliterator* t3=Transliterator::createInstance(names[2], UTRANS_FORWARD, parseError, status);
47 Transliterator* t4=Transliterator::createInstance(names[3], UTRANS_FORWARD, parseError, status);
48 if(U_FAILURE(status)){
49 errln("Transliterator construction failed");
50 return;
51 }
52
53
54 Transliterator* transarray1[]={t1};
55 Transliterator* transarray2[]={t1, t4};
56 Transliterator* transarray3[]={t4, t1, t2};
57 Transliterator* transarray4[]={t1, t2, t3, t4};
58
59 Transliterator** transarray[4];
60 transarray[0] = transarray1;
61 transarray[1] = transarray2;
62 transarray[2] = transarray3;
63 transarray[3] = transarray4;
64
65 const UnicodeString IDs[]={
66 names[0],
67 names[0]+";"+names[3],
68 names[3]+";"+names[1]+";"+names[2],
69 names[0]+";"+names[1]+";"+names[2]+";"+names[3]
70 };
71
72 uint16_t i=0;
73 for(i=0; i<4; i++){
74 status = U_ZERO_ERROR;
75 CompoundTransliterator *cpdtrans=new CompoundTransliterator(IDs[i],parseError, status);
76 if (U_FAILURE(status)) {
77 errln("Construction using CompoundTransliterator(UnicodeString&, Direction, UnicodeFilter*) failed");
78 }
79 delete cpdtrans;
80
81 CompoundTransliterator *cpdtrans2=new CompoundTransliterator(transarray[i], i+1);
82 if(cpdtrans2 == 0){
83 errln("Construction using CompoundTransliterator(Transliterator* const transliterators[], "
84 "int32_t count, UnicodeFilter* adoptedFilter = 0) failed");
85 continue;
86 }
87 CompoundTransliterator *copycpd=new CompoundTransliterator(*cpdtrans2);
88 if(copycpd->getCount() != cpdtrans2->getCount() || copycpd->getID() != cpdtrans2->getID()) {
89 errln("Copy construction failed");
90 continue;
91 }
92
93
94 delete copycpd;
95 delete cpdtrans2;
96
97 }
98 {
99 /*Test Jitterbug 914 */
100 UErrorCode err = U_ZERO_ERROR;
101 CompoundTransliterator cpdTrans(UnicodeString("Latin-Hangul"),UTRANS_REVERSE,NULL,parseError,err);
102 UnicodeString newID =cpdTrans.getID();
103 if(newID!=UnicodeString("Hangul-Latin")){
104 errln(UnicodeString("Test for Jitterbug 914 for cpdTrans(UnicodeString(\"Latin-Hangul\"),UTRANS_REVERSE,NULL,err) failed"));
105 }
106 }
107 delete t1;
108 delete t2;
109 delete t3;
110 delete t4;
111
112 }
113
114 void CompoundTransliteratorTest::TestCloneEqual(){
115 logln("Testing the clone() and equality operator functions of Compound Transliterator");
116 UErrorCode status = U_ZERO_ERROR;
117 UParseError parseError;
118 CompoundTransliterator *ct1=new CompoundTransliterator("Greek-Latin;Latin-Devanagari",parseError,status);
119 if(U_FAILURE(status)){
120 errln("construction failed");
121 delete ct1;
122 return;
123 }
124 CompoundTransliterator *ct2=new CompoundTransliterator("Greek-Latin", parseError, status);
125 if(U_FAILURE(status)){
126 errln("construction failed");
127 delete ct1;
128 delete ct2;
129 return;
130 }
131 CompoundTransliterator *copyct1=new CompoundTransliterator(*ct1);
132 if(copyct1 == 0){
133 errln("copy construction failed");
134 return;
135 }
136 CompoundTransliterator *copyct2=new CompoundTransliterator(*ct2);
137 if(copyct2 == 0){
138 errln("copy construction failed");
139 return;
140 }
141 CompoundTransliterator equalct1=*copyct1;
142 CompoundTransliterator equalct2=*copyct2;
143
144 if(copyct1->getID() != ct1->getID() || copyct2->getID() != ct2->getID() ||
145 copyct1->getCount() != ct1->getCount() || copyct2->getCount() != ct2->getCount() ||
146 copyct2->getID() == ct1->getID() || copyct1->getID() == ct2->getID() ||
147 copyct2->getCount() == ct1->getCount() || copyct1->getCount() == ct2->getCount() ){
148 errln("Error: copy constructors failed");
149 }
150
151 if(equalct1.getID() != ct1->getID() || equalct2.getID() != ct2->getID() ||
152 equalct1.getID() != copyct1->getID() || equalct2.getID() != copyct2->getID() ||
153 equalct1.getCount() != ct1->getCount() || equalct2.getCount() != ct2->getCount() ||
154 copyct2->getID() == ct1->getID() || copyct1->getID() == ct2->getID() ||
155 equalct1.getCount() != copyct1->getCount() || equalct2.getCount() != copyct2->getCount() ||
156 equalct2.getCount() == ct1->getCount() || equalct1.getCount() == ct2->getCount() ) {
157 errln("Error: =operator or copy constructor failed");
158 }
159
160 CompoundTransliterator *clonect1a=(CompoundTransliterator*)ct1->clone();
161 CompoundTransliterator *clonect1b=(CompoundTransliterator*)equalct1.clone();
162 CompoundTransliterator *clonect2a=(CompoundTransliterator*)ct2->clone();
163 CompoundTransliterator *clonect2b=(CompoundTransliterator*)copyct2->clone();
164
165
166 if(clonect1a->getID() != ct1->getID() || clonect1a->getCount() != ct1->getCount() ||
167 clonect1a->getID() != clonect1b->getID() || clonect1a->getCount() != clonect1b->getCount() ||
168 clonect1a->getID() != equalct1.getID() || clonect1a->getCount() != equalct1.getCount() ||
169 clonect1a->getID() != copyct1->getID() || clonect1a->getCount() != copyct1->getCount() ||
170
171 clonect2b->getID() != ct2->getID() || clonect2a->getCount() != ct2->getCount() ||
172 clonect2a->getID() != clonect2b->getID() || clonect2a->getCount() != clonect2b->getCount() ||
173 clonect2a->getID() != equalct2.getID() || clonect2a->getCount() != equalct2.getCount() ||
174 clonect2b->getID() != copyct2->getID() || clonect2b->getCount() != copyct2->getCount() ) {
175 errln("Error: clone() failed");
176 }
177
178 delete ct1;
179 delete ct2;
180 delete copyct1;
181 delete copyct2;
182 delete clonect1a;
183 delete clonect1b;
184 delete clonect2a;
185 delete clonect2b;
186
187 }
188
189 void CompoundTransliteratorTest::TestGetCount(){
190 logln("Testing the getCount() API of CompoundTransliterator");
191 UErrorCode status = U_ZERO_ERROR;
192 UParseError parseError;
193 CompoundTransliterator *ct1=new CompoundTransliterator("Halfwidth-Fullwidth;Fullwidth-Halfwidth", parseError, status);
194 CompoundTransliterator *ct2=new CompoundTransliterator("Any-Hex;Hex-Any;Cyrillic-Latin;Latin-Cyrillic", parseError, status);
195 CompoundTransliterator *ct3=(CompoundTransliterator*)ct1;
196 CompoundTransliterator *ct4=new CompoundTransliterator("Latin-Devanagari", parseError, status);
197 CompoundTransliterator *ct5=new CompoundTransliterator(*ct4);
198
199 if (U_FAILURE(status)) {
200 errln("FAILED: CompoundTransliterator constructor failed");
201 } else
202 if(ct1->getCount() == ct2->getCount() || ct1->getCount() != ct3->getCount() ||
203 ct2->getCount() == ct3->getCount() ||
204 ct4->getCount() != ct5->getCount() || ct4->getCount() == ct1->getCount() ||
205 ct4->getCount() == ct2->getCount() || ct4->getCount() == ct3->getCount() ||
206 ct5->getCount() == ct2->getCount() || ct5->getCount() == ct3->getCount() ) {
207 errln("Error: getCount() failed");
208 }
209
210 /* Quick test getTargetSet(), only test that it doesn't die. TODO: a better test. */
211 UnicodeSet ts;
212 UnicodeSet *retUS = NULL;
213 retUS = &ct1->getTargetSet(ts);
214 if (retUS != &ts || ts.size() == 0) {
215 errln("CompoundTransliterator::getTargetSet() failed.\n");
216 }
217
218 /* Quick test getSourceSet(), only test that it doesn't die. TODO: a better test. */
219 UnicodeSet ss;
220 retUS = NULL;
221 retUS = &ct1->getSourceSet(ss);
222 if (retUS != &ss || ss.size() == 0) {
223 errln("CompoundTransliterator::getSourceSet() failed.\n");
224 }
225
226 delete ct1;
227 delete ct2;
228 delete ct4;
229 delete ct5;
230 }
231
232 void CompoundTransliteratorTest::TestGetSetAdoptTransliterator(){
233 logln("Testing the getTransliterator() API of CompoundTransliterator");
234 UnicodeString ID("Latin-Greek;Greek-Latin;Latin-Devanagari;Devanagari-Latin;Latin-Cyrillic;Cyrillic-Latin;Any-Hex;Hex-Any");
235 UErrorCode status = U_ZERO_ERROR;
236 UParseError parseError;
237 CompoundTransliterator *ct1=new CompoundTransliterator(ID, parseError, status);
238 if(U_FAILURE(status)){
239 errln("CompoundTransliterator construction failed");
240 return;
241 }
242 int32_t count=ct1->getCount();
243 UnicodeString *array=split(ID, 0x003b, count);
244 int i;
245 for(i=0; i < count; i++){
246 UnicodeString child= ct1->getTransliterator(i).getID();
247 if(child != *(array+i)){
248 errln("Error getTransliterator() failed: Expected->" + *(array+i) + " Got->" + child);
249 }else {
250 logln("OK: getTransliterator() passed: Expected->" + *(array+i) + " Got->" + child);
251 }
252 }
253 delete []array;
254
255 logln("Testing setTransliterator() API of CompoundTransliterator");
256 UnicodeString ID2("Hex-Any;Any-Hex;Latin-Cyrillic;Cyrillic-Latin;Halfwidth-Fullwidth;Fullwidth-Halfwidth");
257 array=split(ID2, 0x003b, count);
258 Transliterator** transarray=new Transliterator*[count];
259 for(i=0;i<count;i++){
260 transarray[i]=Transliterator::createInstance(*(array+i), UTRANS_FORWARD, parseError, status);
261 if(U_FAILURE(status)){
262 errln("Error could not create Transliterator with ID :"+*(array+i));
263 }else{
264 logln("The ID for the transltierator created is " + transarray[i]->getID());
265 }
266 status = U_ZERO_ERROR;
267 }
268
269 /*setTransliterator and adoptTransliterator */
270
271 ct1->setTransliterators(transarray, count);
272 if(ct1->getCount() != count || ct1->getID() != ID2){
273 errln((UnicodeString)"Error: setTransliterators() failed.\n\t Count:- expected->" + count + (UnicodeString)". got->" + ct1->getCount() +
274 (UnicodeString)"\n\tID :- expected->" + ID2 + (UnicodeString)". got->" + ct1->getID());
275 }
276 else{
277 logln("OK: setTransliterators() passed");
278 }
279 /*UnicodeString temp;
280 for(i=0;i<count-1;i++){
281 temp.append(ct1->getTransliterator(i).getID());
282 temp.append(";");
283 }
284 temp.append(ct1->getTransliterator(i).getID());
285 if(temp != ID2){
286 errln("Error: setTransliterator() failed. Expected->" + ID2 + "\nGot->" + temp);
287 }
288 else{
289 logln("OK: setTransliterator() passed");
290 }*/
291 logln("Testing adoptTransliterator() API of CompoundTransliterator");
292 UnicodeString ID3("Latin-Katakana");
293 Transliterator **transarray2=(Transliterator **)uprv_malloc(sizeof(Transliterator*)*1);
294 transarray2[0] = Transliterator::createInstance(ID3,UTRANS_FORWARD,parseError,status);
295 if (transarray2[0] != 0) {
296 ct1->adoptTransliterators(transarray2, 1);
297 }
298 if(ct1->getCount() != 1 || ct1->getID() != ID3){
299 errln((UnicodeString)"Error: adoptTransliterators() failed.\n\t Count:- expected->1" + (UnicodeString)". got->" + ct1->getCount() +
300 (UnicodeString)"\n\tID :- expected->" + ID3 + (UnicodeString)". got->" + ct1->getID());
301 }
302 else{
303 logln("OK: adoptTranslterator() passed");
304 }
305 delete ct1;
306 for(i=0;i<count;i++){
307 delete transarray[i];
308 }
309 delete []transarray;
310 delete []array;
311 }
312
313 /**
314 * Splits a UnicodeString
315 */
316 UnicodeString* CompoundTransliteratorTest::split(const UnicodeString& str, UChar seperator, int32_t& count) {
317
318 //get the count
319 int32_t i;
320 count =1;
321 for(i=0; i<str.length(); i++){
322 if(str.charAt(i) == seperator)
323 count++;
324 }
325 // make an array
326 UnicodeString* result = new UnicodeString[count];
327 int32_t last = 0;
328 int32_t current = 0;
329 for (i = 0; i < str.length(); ++i) {
330 if (str.charAt(i) == seperator) {
331 str.extractBetween(last, i, result[current]);
332 last = i+1;
333 current++;
334 }
335 }
336 str.extractBetween(last, i, result[current]);
337 return result;
338 }
339 void CompoundTransliteratorTest::TestTransliterate(){
340 logln("Testing the handleTransliterate() API of CompoundTransliterator");
341 UErrorCode status = U_ZERO_ERROR;
342 UParseError parseError;
343 CompoundTransliterator *ct1=new CompoundTransliterator("Any-Hex;Hex-Any",parseError, status);
344 if(U_FAILURE(status)){
345 errln("CompoundTransliterator construction failed");
346 }else {
347 #if 0
348 // handleTransliterate is a protected method that was erroneously made
349 // public. It is not public API that needs to be tested.
350 UnicodeString s("abcabc");
351 expect(*ct1, s, s);
352 UTransPosition index = { 0, 0, 0, 0 };
353 UnicodeString rsource2(s);
354 UnicodeString expectedResult=s;
355 ct1->handleTransliterate(rsource2, index, FALSE);
356 expectAux(ct1->getID() + ":String, index(0,0,0), incremental=FALSE", rsource2 + "->" + rsource2, rsource2==expectedResult, expectedResult);
357 UTransPosition _index = {1,3,2,3};
358 uprv_memcpy(&index, &_index, sizeof(index));
359 UnicodeString rsource3(s);
360 ct1->handleTransliterate(rsource3, index, TRUE);
361 expectAux(ct1->getID() + ":String, index(1,2,3), incremental=TRUE", rsource3 + "->" + rsource3, rsource3==expectedResult, expectedResult);
362 #endif
363 }
364 delete ct1;
365 UnicodeString Data[]={
366 //ID, input string, transliterated string
367 "Any-Hex;Hex-Any;Any-Hex", "hello", UnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F", ""),
368 "Any-Hex;Hex-Any", "hello! How are you?", "hello! How are you?",
369 //"Devanagari-Latin;Latin-Devanagari", CharsToUnicodeString("\\u092D\\u0948'\\u0930'\\u0935"), CharsToUnicodeString("\\u092D\\u0948\\u0930\\u0935"), // quotes lost
370 "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", //"abkdefghijShchshchzhh",
371 "Latin-Greek;Greek-Latin", "ABGabgAKLMN", "ABGabgAKLMN",
372 //"Latin-Arabic;Arabic-Latin", "Ad'r'a'b'i'k'dh'dd'gh", "Adrabikdhddgh",
373 "Hiragana-Katakana", CharsToUnicodeString("\\u3041\\u308f\\u3099\\u306e\\u304b\\u3092\\u3099"),
374 CharsToUnicodeString("\\u30A1\\u30f7\\u30ce\\u30ab\\u30fa"),
375 "Hiragana-Katakana;Katakana-Hiragana", CharsToUnicodeString("\\u3041\\u308f\\u3099\\u306e\\u304b\\u3051"),
376 CharsToUnicodeString("\\u3041\\u308f\\u3099\\u306e\\u304b\\u3051"),
377 "Katakana-Hiragana;Hiragana-Katakana", CharsToUnicodeString("\\u30A1\\u30f7\\u30ce\\u30f5\\u30f6"),
378 CharsToUnicodeString("\\u30A1\\u30f7\\u30ce\\u30ab\\u30b1"),
379 "Latin-Katakana;Katakana-Latin", CharsToUnicodeString("vavivuvevohuzizuzonyinyunyasesuzezu"),
380 CharsToUnicodeString("vavivuvevohuzizuzonyinyunyasesuzezu"),
381 };
382 uint32_t i;
383 for(i=0; i<sizeof(Data)/sizeof(Data[0]); i=i+3){
384 UErrorCode status = U_ZERO_ERROR;
385
386 CompoundTransliterator *ct2=new CompoundTransliterator(Data[i+0], parseError, status);
387 if(U_FAILURE(status)){
388 errln("CompoundTransliterator construction failed for " + Data[i+0]);
389 } else {
390 expect(*ct2, Data[i+1], Data[i+2]);
391 }
392 delete ct2;
393 }
394
395 }
396
397
398
399 //======================================================================
400 // Support methods
401 //======================================================================
402 void CompoundTransliteratorTest::expect(const CompoundTransliterator& t,
403 const UnicodeString& source,
404 const UnicodeString& expectedResult) {
405
406 UnicodeString rsource(source);
407 t.transliterate(rsource);
408 expectAux(t.getID() + ":Replaceable", source + "->" + rsource, rsource==expectedResult, expectedResult);
409
410 // Test transliterate (incremental) transliteration --
411 rsource.remove();
412 rsource.append(source);
413 UTransPosition index;
414 index.contextStart =0;
415 index.contextLimit = source.length();
416 index.start = 0;
417 index.limit = source.length();
418 UErrorCode ec = U_ZERO_ERROR;
419 t.transliterate(rsource, index, ec);
420 t.finishTransliteration(rsource,index);
421 expectAux(t.getID() + ":handleTransliterate ", source + "->" + rsource, rsource==expectedResult, expectedResult);
422
423 }
424
425 void CompoundTransliteratorTest::expectAux(const UnicodeString& tag,
426 const UnicodeString& summary, UBool pass,
427 const UnicodeString& expectedResult) {
428 if (pass) {
429 logln(UnicodeString("(")+tag+") " + prettify(summary));
430 } else {
431 errln(UnicodeString("FAIL: (")+tag+") "
432 + prettify(summary)
433 + ", expected " + prettify(expectedResult));
434 }
435 }
436
437 #endif /* #if !UCONFIG_NO_TRANSLITERATION */