1 /***************************************************************************
3 * Copyright (C) 2000-2003, International Business Machines
4 * Corporation and others. All Rights Reserved.
6 ************************************************************************
7 * Date Name Description
8 * 03/09/2000 Madhu Creation.
9 ************************************************************************/
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
16 #include "unicode/utypes.h"
17 #include "unicode/translit.h"
18 #include "unicode/uniset.h"
22 //---------------------------------------------
24 //---------------------------------------------
27 CompoundTransliteratorTest::runIndexedTest(int32_t index
, UBool exec
,
28 const char* &name
, char* /*par*/) {
30 TESTCASE(0,TestConstruction
);
31 TESTCASE(1,TestCloneEqual
);
32 TESTCASE(2,TestGetCount
);
33 TESTCASE(3,TestGetSetAdoptTransliterator
);
34 TESTCASE(4,TestTransliterate
);
35 default: name
= ""; break;
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");
54 Transliterator
* transarray1
[]={t1
};
55 Transliterator
* transarray2
[]={t1
, t4
};
56 Transliterator
* transarray3
[]={t4
, t1
, t2
};
57 Transliterator
* transarray4
[]={t1
, t2
, t3
, t4
};
59 Transliterator
** transarray
[4];
60 transarray
[0] = transarray1
;
61 transarray
[1] = transarray2
;
62 transarray
[2] = transarray3
;
63 transarray
[3] = transarray4
;
65 const UnicodeString IDs
[]={
67 names
[0]+";"+names
[3],
68 names
[3]+";"+names
[1]+";"+names
[2],
69 names
[0]+";"+names
[1]+";"+names
[2]+";"+names
[3]
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");
81 CompoundTransliterator
*cpdtrans2
=new CompoundTransliterator(transarray
[i
], i
+1);
83 errln("Construction using CompoundTransliterator(Transliterator* const transliterators[], "
84 "int32_t count, UnicodeFilter* adoptedFilter = 0) failed");
87 CompoundTransliterator
*copycpd
=new CompoundTransliterator(*cpdtrans2
);
88 if(copycpd
->getCount() != cpdtrans2
->getCount() || copycpd
->getID() != cpdtrans2
->getID()) {
89 errln("Copy construction failed");
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"));
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");
124 CompoundTransliterator
*ct2
=new CompoundTransliterator("Greek-Latin", parseError
, status
);
125 if(U_FAILURE(status
)){
126 errln("construction failed");
131 CompoundTransliterator
*copyct1
=new CompoundTransliterator(*ct1
);
133 errln("copy construction failed");
136 CompoundTransliterator
*copyct2
=new CompoundTransliterator(*ct2
);
138 errln("copy construction failed");
141 CompoundTransliterator equalct1
=*copyct1
;
142 CompoundTransliterator equalct2
=*copyct2
;
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");
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");
160 CompoundTransliterator
*clonect1a
=(CompoundTransliterator
*)ct1
->clone();
161 CompoundTransliterator
*clonect1b
=(CompoundTransliterator
*)equalct1
.clone();
162 CompoundTransliterator
*clonect2a
=(CompoundTransliterator
*)ct2
->clone();
163 CompoundTransliterator
*clonect2b
=(CompoundTransliterator
*)copyct2
->clone();
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() ||
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");
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
);
199 if (U_FAILURE(status
)) {
200 errln("FAILED: CompoundTransliterator constructor failed");
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");
210 /* Quick test getTargetSet(), only test that it doesn't die. TODO: a better test. */
212 UnicodeSet
*retUS
= NULL
;
213 retUS
= &ct1
->getTargetSet(ts
);
214 if (retUS
!= &ts
|| ts
.size() == 0) {
215 errln("CompoundTransliterator::getTargetSet() failed.\n");
218 /* Quick test getSourceSet(), only test that it doesn't die. TODO: a better test. */
221 retUS
= &ct1
->getSourceSet(ss
);
222 if (retUS
!= &ss
|| ss
.size() == 0) {
223 errln("CompoundTransliterator::getSourceSet() failed.\n");
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");
242 int32_t count
=ct1
->getCount();
243 UnicodeString
*array
=split(ID
, 0x003b, count
);
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
);
250 logln("OK: getTransliterator() passed: Expected->" + *(array
+i
) + " Got->" + child
);
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
));
264 logln("The ID for the transltierator created is " + transarray
[i
]->getID());
266 status
= U_ZERO_ERROR
;
269 /*setTransliterator and adoptTransliterator */
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());
277 logln("OK: setTransliterators() passed");
279 /*UnicodeString temp;
280 for(i=0;i<count-1;i++){
281 temp.append(ct1->getTransliterator(i).getID());
284 temp.append(ct1->getTransliterator(i).getID());
286 errln("Error: setTransliterator() failed. Expected->" + ID2 + "\nGot->" + temp);
289 logln("OK: setTransliterator() passed");
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);
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());
303 logln("OK: adoptTranslterator() passed");
306 for(i
=0;i
<count
;i
++){
307 delete transarray
[i
];
314 * Splits a UnicodeString
316 UnicodeString
* CompoundTransliteratorTest::split(const UnicodeString
& str
, UChar seperator
, int32_t& count
) {
321 for(i
=0; i
<str
.length(); i
++){
322 if(str
.charAt(i
) == seperator
)
326 UnicodeString
* result
= new UnicodeString
[count
];
329 for (i
= 0; i
< str
.length(); ++i
) {
330 if (str
.charAt(i
) == seperator
) {
331 str
.extractBetween(last
, i
, result
[current
]);
336 str
.extractBetween(last
, i
, result
[current
]);
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");
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");
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
);
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"),
383 for(i
=0; i
<sizeof(Data
)/sizeof(Data
[0]); i
=i
+3){
384 UErrorCode status
= U_ZERO_ERROR
;
386 CompoundTransliterator
*ct2
=new CompoundTransliterator(Data
[i
+0], parseError
, status
);
387 if(U_FAILURE(status
)){
388 errln("CompoundTransliterator construction failed for " + Data
[i
+0]);
390 expect(*ct2
, Data
[i
+1], Data
[i
+2]);
399 //======================================================================
401 //======================================================================
402 void CompoundTransliteratorTest::expect(const CompoundTransliterator
& t
,
403 const UnicodeString
& source
,
404 const UnicodeString
& expectedResult
) {
406 UnicodeString
rsource(source
);
407 t
.transliterate(rsource
);
408 expectAux(t
.getID() + ":Replaceable", source
+ "->" + rsource
, rsource
==expectedResult
, expectedResult
);
410 // Test transliterate (incremental) transliteration --
412 rsource
.append(source
);
413 UTransPosition index
;
414 index
.contextStart
=0;
415 index
.contextLimit
= source
.length();
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
);
425 void CompoundTransliteratorTest::expectAux(const UnicodeString
& tag
,
426 const UnicodeString
& summary
, UBool pass
,
427 const UnicodeString
& expectedResult
) {
429 logln(UnicodeString("(")+tag
+") " + prettify(summary
));
431 errln(UnicodeString("FAIL: (")+tag
+") "
433 + ", expected " + prettify(expectedResult
));
437 #endif /* #if !UCONFIG_NO_TRANSLITERATION */