1 /************************************************************************
3 * Copyright (c) 2000-2016, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ************************************************************************/
6 /************************************************************************
7 * Date Name Description
8 * 1/03/2000 Madhu Creation.
9 ************************************************************************/
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/utypes.h"
18 #include "unicode/translit.h"
19 #include "unicode/unifilt.h"
25 #include "unicode/rep.h"
26 #include "unicode/locid.h"
27 #include "unicode/uniset.h"
29 int32_t getInt(UnicodeString str
)
36 str
.extract(0, len
, buffer
, "");
41 //---------------------------------------------
43 //---------------------------------------------
46 TransliteratorAPITest::runIndexedTest(int32_t index
, UBool exec
,
47 const char* &name
, char* /*par*/) {
49 TESTCASE(0,TestgetInverse
);
50 TESTCASE(1,TestgetID
);
51 TESTCASE(2,TestGetDisplayName
);
52 TESTCASE(3,TestTransliterate1
);
53 TESTCASE(4,TestTransliterate2
);
54 TESTCASE(5,TestTransliterate3
);
55 TESTCASE(6,TestSimpleKeyboardTransliterator
);
56 TESTCASE(7,TestKeyboardTransliterator1
);
57 TESTCASE(8,TestKeyboardTransliterator2
);
58 TESTCASE(9,TestKeyboardTransliterator3
);
59 TESTCASE(10,TestGetAdoptFilter
);
60 TESTCASE(11,TestClone
);
61 TESTCASE(12,TestNullTransliterator
);
62 TESTCASE(13,TestRegisterUnregister
);
63 TESTCASE(14,TestUnicodeFunctor
);
64 default: name
= ""; break;
69 void TransliteratorAPITest::TestgetID() {
70 UnicodeString trans
="Latin-Greek";
72 UParseError parseError
;
73 UErrorCode status
= U_ZERO_ERROR
;
74 Transliterator
* t
= Transliterator::createInstance(trans
, UTRANS_FORWARD
, parseError
, status
);
75 if(t
==0 || U_FAILURE(status
)){
76 dataerrln("FAIL: construction of Latin-Greek - %s",u_errorName(status
));
81 errln("FAIL: getID returned " + ID
+ " instead of Latin-Greek");
85 for (i
=0; i
<Transliterator::countAvailableIDs(); i
++){
86 status
= U_ZERO_ERROR
;
87 ID
= (UnicodeString
) Transliterator::getAvailableID(i
);
88 if(ID
.indexOf("Thai")>-1){
91 t
= Transliterator::createInstance(ID
, UTRANS_FORWARD
, parseError
, status
);
97 errln("FAIL: getID() returned " + t
->getID() + " instead of " + ID
);
100 ID
=(UnicodeString
)Transliterator::getAvailableID(i
);
101 if(ID
!= (UnicodeString
)Transliterator::getAvailableID(0)){
102 errln("FAIL: calling getAvailableID(index > coundAvailableIDs) should make index=0\n");
105 Transliterator
* t1
=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD
, parseError
, status
);
106 Transliterator
* t2
=Transliterator::createInstance("Latin-Greek", UTRANS_FORWARD
, parseError
, status
);
107 if(t1
==0 || t2
== 0){
108 errln("FAIL: construction");
111 Transliterator
* t3
=t1
->clone();
112 Transliterator
* t4
=t2
->clone();
114 if(t1
->getID() != t3
->getID() || t2
->getID() != t4
->getID() ||
115 t1
->getID() == t4
->getID() || t2
->getID() == t3
->getID() ||
116 t1
->getID()== t4
->getID() )
117 errln("FAIL: getID or clone failed");
120 Transliterator
* t5
=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD
, parseError
, status
);
122 errln("FAIL: construction");
123 else if(t1
->getID() != t5
->getID() || t5
->getID() != t3
->getID() || t1
->getID() != t3
->getID())
124 errln("FAIL: getID or clone failed");
134 void TransliteratorAPITest::TestgetInverse() {
135 UErrorCode status
= U_ZERO_ERROR
;
136 UParseError parseError
;
137 Transliterator
* t1
= Transliterator::createInstance("Katakana-Latin", UTRANS_FORWARD
, parseError
, status
);
138 Transliterator
* invt1
= Transliterator::createInstance("Latin-Katakana", UTRANS_FORWARD
, parseError
, status
);
139 Transliterator
* t2
= Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD
, parseError
, status
);
140 Transliterator
* invt2
= Transliterator::createInstance("Devanagari-Latin", UTRANS_FORWARD
, parseError
, status
);
141 if(t1
== 0 || invt1
== 0 || t2
== 0 || invt2
== 0) {
142 dataerrln("FAIL: in instantiation - %s", u_errorName(status
));
146 Transliterator
* inverse1
=t1
->createInverse(status
);
147 Transliterator
* inverse2
=t2
->createInverse(status
);
148 if(inverse1
->getID() != invt1
->getID() || inverse2
->getID() != invt2
->getID()
149 || inverse1
->getID() == invt2
->getID() || inverse2
->getID() == invt1
->getID() )
150 errln("FAIL: getInverse() ");
152 UnicodeString TransID
[]={
153 "Halfwidth-Fullwidth",
154 "Fullwidth-Halfwidth",
157 //"Arabic-Latin", // removed in 2.0
161 //"Hebrew-Latin", // removed in 2.0
170 for(uint32_t i
=0; i
<UPRV_LENGTHOF(TransID
); i
=i
+2){
171 Transliterator
*trans1
=Transliterator::createInstance(TransID
[i
], UTRANS_FORWARD
, parseError
, status
);
173 errln("FAIL: in instantiation for" + TransID
[i
]);
176 Transliterator
*inver1
=trans1
->createInverse(status
);
177 if(inver1
->getID() != TransID
[i
+1] )
178 errln("FAIL :getInverse() for " + TransID
[i
] + " returned " + inver1
->getID() + " instead of " + TransID
[i
+1]);
191 void TransliteratorAPITest::TestClone(){
192 Transliterator
*t1
, *t2
, *t3
, *t4
;
193 UErrorCode status
= U_ZERO_ERROR
;
194 UParseError parseError
;
195 t1
=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD
, parseError
, status
);
196 t2
=Transliterator::createInstance("Latin-Greek", UTRANS_FORWARD
, parseError
, status
);
197 if(t1
== 0 || t2
== 0){
198 dataerrln("FAIL: construction - %s", u_errorName(status
));
204 if(t1
->getID() != t3
->getID() || t2
->getID() != t4
->getID() )
205 errln("FAIL: clone or getID failed");
206 if(t1
->getID()==t4
->getID() || t2
->getID()==t3
->getID() || t1
->getID()==t4
->getID())
207 errln("FAIL: clone or getID failed");
216 void TransliteratorAPITest::TestGetDisplayName() {
217 UnicodeString dispNames
[]= {
219 //"CurlyQuotes-StraightQuotes" ,"CurlyQuotes to StraightQuotes",
220 "Any-Hex" ,"Any to Hex Escape",
221 "Halfwidth-Fullwidth" ,"Halfwidth to Fullwidth" ,
222 //"Latin-Arabic" ,"Latin to Arabic" ,
223 "Latin-Devanagari" ,"Latin to Devanagari" ,
224 "Greek-Latin" ,"Greek to Latin" ,
225 //"Arabic-Latin" ,"Arabic to Latin" ,
226 "Hex-Any" ,"Hex Escape to Any",
227 "Cyrillic-Latin" ,"Cyrillic to Latin" ,
228 "Latin-Greek" ,"Latin to Greek" ,
229 "Latin-Katakana" ,"Latin to Katakana" ,
230 //"Latin-Hebrew" ,"Latin to Hebrew" ,
231 "Katakana-Latin" ,"Katakana to Latin"
233 UnicodeString name
="";
235 UnicodeString message
;
236 UErrorCode status
= U_ZERO_ERROR
;
237 UParseError parseError
;
239 #if UCONFIG_NO_FORMATTING
240 logln("Skipping, UCONFIG_NO_FORMATTING is set\n");
244 for (uint32_t i
=0; i
<UPRV_LENGTHOF(dispNames
); i
=i
+2 ) {
245 t
= Transliterator::createInstance(dispNames
[i
+0], UTRANS_FORWARD
, parseError
, status
);
247 dataerrln("FAIL: construction: " + dispNames
[i
+0] + " - " + u_errorName(status
));
248 status
= U_ZERO_ERROR
;
251 t
->getDisplayName(t
->getID(), name
);
252 message
="Display name for ID:" + t
->getID();
253 // doTest(message, name, dispNames[i+1]); //!!! This will obviously fail for any locale other than english and its children!!!
255 t
->getDisplayName(t
->getID(), Locale::getUS(), name
);
257 message
.append("Display name for on english locale ID:");
258 message
.append(t
->getID());
259 // message="Display name for on english locale ID:" + t->getID();
260 doTest(message
, name
, dispNames
[i
+1]);
269 void TransliteratorAPITest::TestTransliterate1(){
271 UnicodeString Data
[]={
272 //ID, input string, transliterated string
273 "Any-Hex", "hello", UnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F", "") ,
274 "Hex-Any", UnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F", ""), "hello" ,
275 "Latin-Devanagari",CharsToUnicodeString("bha\\u0304rata"), CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924") ,
276 "Latin-Devanagari",UnicodeString("kra ksha khra gra cra dya dhya",""), CharsToUnicodeString("\\u0915\\u094D\\u0930 \\u0915\\u094D\\u0936 \\u0916\\u094D\\u0930 \\u0917\\u094D\\u0930 \\u091a\\u094D\\u0930 \\u0926\\u094D\\u092F \\u0927\\u094D\\u092F") ,
278 "Devanagari-Latin", CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), CharsToUnicodeString("bh\\u0101rata"),
279 // "Contracted-Expanded", CharsToUnicodeString("\\u00C0\\u00C1\\u0042"), CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042") ,
280 // "Expanded-Contracted", CharsToUnicodeString("\\u0041\\u0300\\u0041\\u0301\\u0042"), CharsToUnicodeString("\\u00C0\\u00C1\\u0042") ,
281 //"Latin-Arabic", "aap", CharsToUnicodeString("\\u0627\\u06A4") ,
282 //"Arabic-Latin", CharsToUnicodeString("\\u0627\\u06A4"), "aap"
285 UnicodeString gotResult
;
287 UnicodeString message
;
289 logln("Testing transliterate");
290 UErrorCode status
= U_ZERO_ERROR
;
291 UParseError parseError
;
293 for(uint32_t i
=0;i
<UPRV_LENGTHOF(Data
); i
=i
+3){
294 t
=Transliterator::createInstance(Data
[i
+0], UTRANS_FORWARD
, parseError
, status
);
296 dataerrln("FAIL: construction: " + Data
[i
+0] + " Error: " + u_errorName(status
));
297 dataerrln("PreContext: " + prettify(parseError
.preContext
) + " PostContext: " + prettify( parseError
.postContext
) );
298 status
= U_ZERO_ERROR
;
301 gotResult
= Data
[i
+1];
302 t
->transliterate(gotResult
);
303 message
=t
->getID() + "->transliterate(UnicodeString, UnicodeString) for\n\t Source:" + prettify(Data
[i
+1]);
304 doTest(message
, gotResult
, Data
[i
+2]);
308 t
->transliterate(temp
);
310 message
.append(t
->getID());
311 message
.append("->transliterate(Replaceable) for \n\tSource:");
312 message
.append(Data
[i
][1]);
313 doTest(message
, temp
, Data
[i
+2]);
315 callEverything(t
, __LINE__
);
320 void TransliteratorAPITest::TestTransliterate2(){
321 //testing tranliterate(String text, int start, int limit, StringBuffer result)
322 UnicodeString Data2
[]={
323 //ID, input string, start, limit, transliterated string
324 "Any-Hex", "hello! How are you?", "0", "5", UnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F", ""), UnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F! How are you?", "") ,
325 "Any-Hex", "hello! How are you?", "7", "12", UnicodeString("\\u0048\\u006F\\u0077\\u0020\\u0061", ""), UnicodeString("hello! \\u0048\\u006F\\u0077\\u0020\\u0061re you?", ""),
326 "Hex-Any", CharsToUnicodeString("\\u0068\\u0065\\u006C\\u006C\\u006F\\u0021\\u0020"), "0", "5", "hello", "hello! " ,
327 // "Contracted-Expanded", CharsToUnicodeString("\\u00C0\\u00C1\\u0042"), "1", "2", CharsToUnicodeString("\\u0041\\u0301"), CharsToUnicodeString("\\u00C0\\u0041\\u0301\\u0042") ,
328 "Devanagari-Latin", CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), "0", "1", "bha", CharsToUnicodeString("bha\\u093E\\u0930\\u0924") ,
329 "Devanagari-Latin", CharsToUnicodeString("\\u092D\\u093E\\u0930\\u0924"), "1", "2", CharsToUnicodeString("\\u0314\\u0101"), CharsToUnicodeString("\\u092D\\u0314\\u0101\\u0930\\u0924")
332 logln("\n Testing transliterate(String, int, int, StringBuffer)");
334 UnicodeString gotResBuf
;
336 int32_t start
, limit
;
337 UErrorCode status
= U_ZERO_ERROR
;
338 UParseError parseError
;
340 for(uint32_t i
=0; i
<UPRV_LENGTHOF(Data2
); i
=i
+6){
341 t
=Transliterator::createInstance(Data2
[i
+0], UTRANS_FORWARD
, parseError
, status
);
343 dataerrln("FAIL: construction: " + prettify(Data2
[i
+0]) + " - " + u_errorName(status
));
346 start
=getInt(Data2
[i
+2]);
347 limit
=getInt(Data2
[i
+3]);
348 Data2
[i
+1].extractBetween(start
, limit
, gotResBuf
);
349 t
->transliterate(gotResBuf
);
350 // errln("FAIL: calling transliterate on " + t->getID());
351 doTest(t
->getID() + ".transliterate(UnicodeString, int32_t, int32_t, UnicodeString):(" + start
+ "," + limit
+ ") for \n\t source: " + prettify(Data2
[i
+1]), gotResBuf
, Data2
[i
+4]);
354 t
->transliterate(temp
, start
, limit
);
355 doTest(t
->getID() + ".transliterate(Replaceable, int32_t, int32_t, ):(" + start
+ "," + limit
+ ") for \n\t source: " + prettify(Data2
[i
+1]), temp
, Data2
[i
+5]);
356 status
= U_ZERO_ERROR
;
357 callEverything(t
, __LINE__
);
362 status
= U_ZERO_ERROR
;
363 logln("\n Try calling transliterate with illegal start and limit values");
364 t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
365 if(U_FAILURE(status
)) {
366 errln("Error creating transliterator %s", u_errorName(status
));
370 gotResBuf
= temp
= "try start greater than limit";
371 t
->transliterate(gotResBuf
, 10, 5);
372 if(gotResBuf
== temp
) {
373 logln("OK: start greater than limit value handled correctly");
375 errln("FAIL: start greater than limit value returned" + gotResBuf
);
378 callEverything(t
, __LINE__
);
382 void TransliteratorAPITest::TestTransliterate3(){
383 UnicodeString rs
="This is the replaceable String";
384 UnicodeString Data
[] = {
385 "0", "0", "This is the replaceable String",
386 "2", "3", UnicodeString("Th\\u0069s is the replaceable String", ""),
387 "21", "23", UnicodeString("Th\\u0069s is the repl\\u0061\\u0063eable String", ""),
388 "14", "17", UnicodeString("Th\\u0069s is t\\u0068\\u0065\\u0020repl\\u0061\\u0063eable String", ""),
391 UnicodeString message
;
392 UParseError parseError
;
393 UErrorCode status
= U_ZERO_ERROR
;
394 Transliterator
*t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
395 if(U_FAILURE(status
)) {
396 errln("Error creating transliterator %s", u_errorName(status
));
402 errln("FAIL : construction");
403 for(uint32_t i
=0; i
<UPRV_LENGTHOF(Data
); i
=i
+3){
404 start
=getInt(Data
[i
+0]);
405 limit
=getInt(Data
[i
+1]);
406 t
->transliterate(rs
, start
, limit
);
407 message
=t
->getID() + ".transliterate(ReplaceableString, start, limit):("+start
+","+limit
+"):";
408 doTest(message
, rs
, Data
[i
+2]);
413 void TransliteratorAPITest::TestSimpleKeyboardTransliterator(){
414 logln("simple call to transliterate");
415 UErrorCode status
=U_ZERO_ERROR
;
416 UParseError parseError
;
417 Transliterator
* t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
419 UnicodeString context
;
421 if (parseError
.preContext
[0]) {
422 context
+= (UnicodeString
)" at " + parseError
.preContext
;
424 if (parseError
.postContext
[0]) {
425 context
+= (UnicodeString
)" | " + parseError
.postContext
;
427 errln((UnicodeString
)"FAIL: can't create Any-Hex, " +
428 (UnicodeString
)u_errorName(status
) + context
);
431 UTransPosition index
={19,20,20,20};
432 UnicodeString rs
= "Transliterate this-''";
433 UnicodeString insertion
="abc";
434 UnicodeString expected
=UnicodeString("Transliterate this-'\\u0061\\u0062\\u0063'", "");
435 t
->transliterate(rs
, index
, insertion
, status
);
436 if(U_FAILURE(status
))
437 errln("FAIL: " + t
->getID()+ ".translitere(Replaceable, int[], UnicodeString, UErrorCode)-->" + (UnicodeString
)u_errorName(status
));
438 t
->finishTransliteration(rs
, index
);
439 UnicodeString message
="transliterate";
440 doTest(message
, rs
, expected
);
442 logln("try calling transliterate with invalid index values");
443 UTransPosition index1
[]={
444 //START, LIMIT, CURSOR
445 {10, 10, 12, 10}, //invalid since CURSOR>LIMIT valid:-START <= CURSOR <= LIMIT
446 {17, 16, 17, 17}, //invalid since START>LIMIT valid:-0<=START<=LIMIT
447 {-1, 16, 14, 16}, //invalid since START<0
448 {3, 50, 2, 50} //invalid since LIMIT>text.length()
450 for(uint32_t i
=0; i
<UPRV_LENGTHOF(index1
); i
++){
452 t
->transliterate(rs
, index1
[i
], insertion
, status
);
453 if(status
== U_ILLEGAL_ARGUMENT_ERROR
)
454 logln("OK: invalid index values handled correctly");
456 errln("FAIL: invalid index values didn't throw U_ILLEGAL_ARGUMENT_ERROR throw" + (UnicodeString
)u_errorName(status
));
461 void TransliteratorAPITest::TestKeyboardTransliterator1(){
462 UnicodeString Data
[]={
464 "a", UnicodeString("\\u0061", "") ,
465 "bbb", UnicodeString("\\u0061\\u0062\\u0062\\u0062", "") ,
466 "ca", UnicodeString("\\u0061\\u0062\\u0062\\u0062\\u0063\\u0061", "") ,
467 " ", UnicodeString("\\u0061\\u0062\\u0062\\u0062\\u0063\\u0061\\u0020", "") ,
468 "", UnicodeString("\\u0061\\u0062\\u0062\\u0062\\u0063\\u0061\\u0020", "") ,
470 "a", UnicodeString("\\u0061", "") ,
471 "b", UnicodeString("\\u0061\\u0062", "") ,
472 "z", UnicodeString("\\u0061\\u0062\\u007A", "") ,
473 "", UnicodeString("\\u0061\\u0062\\u007A", "")
476 UParseError parseError
;
477 UErrorCode status
= U_ZERO_ERROR
;
478 Transliterator
* t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
479 if(U_FAILURE(status
)) {
480 errln("Error creating transliterator %s", u_errorName(status
));
484 //keyboardAux(t, Data);
485 UTransPosition index
={0, 0, 0, 0};
488 logln("Testing transliterate(Replaceable, int32_t, UnicodeString, UErrorCode)");
489 for (i
=0; i
<10; i
=i
+2) {
491 if (Data
[i
+0] != "") {
492 log
= s
+ " + " + Data
[i
+0] + " -> ";
493 t
->transliterate(s
, index
, Data
[i
+0], status
);
494 if(U_FAILURE(status
)){
495 errln("FAIL: " + t
->getID()+ ".transliterate(Replaceable, int32_t[], UnicodeString, UErrorCode)-->" + (UnicodeString
)u_errorName(status
));
500 t
->finishTransliteration(s
, index
);
502 // Show the start index '{' and the cursor '|'
503 displayOutput(s
, Data
[i
+1], log
, index
);
509 index
.contextStart
= index
.contextLimit
= index
.start
= index
.limit
= 0;
510 logln("Testing transliterate(Replaceable, int32_t, UChar, UErrorCode)");
511 for(i
=10; i
<UPRV_LENGTHOF(Data
); i
=i
+2){
513 if (Data
[i
+0] != "") {
514 log
= s
+ " + " + Data
[i
+0] + " -> ";
515 UChar c
=Data
[i
+0].charAt(0);
516 t
->transliterate(s
, index
, c
, status
);
517 if(U_FAILURE(status
))
518 errln("FAIL: " + t
->getID()+ ".transliterate(Replaceable, int32_t[], UChar, UErrorCode)-->" + (UnicodeString
)u_errorName(status
));
522 t
->finishTransliteration(s
, index
);
524 // Show the start index '{' and the cursor '|'
525 displayOutput(s
, Data
[i
+1], log
, index
);
531 void TransliteratorAPITest::TestKeyboardTransliterator2(){
532 UnicodeString Data
[]={
533 //insertion, buffer, index[START], index[LIMIT], index[CURSOR]
535 "abc", UnicodeString("Initial String: add-\\u0061\\u0062\\u0063-", ""), "19", "20", "20",
536 "a", UnicodeString("In\\u0069\\u0061tial String: add-\\u0061\\u0062\\u0063-", ""), "2", "3", "2" ,
537 "b", UnicodeString("\\u0062In\\u0069\\u0061tial String: add-\\u0061\\u0062\\u0063-", ""), "0", "0", "0" ,
538 "", UnicodeString("\\u0062In\\u0069\\u0061tial String: add-\\u0061\\u0062\\u0063-", ""), "0", "0", "0" ,
539 //data for Latin-Devanagiri
540 CharsToUnicodeString("a\\u0304"), CharsToUnicodeString("Hindi -\\u0906-"), "6", "7", "6",
541 CharsToUnicodeString("ma\\u0304"), CharsToUnicodeString("Hindi -\\u0906\\u092E\\u093E-"), "7", "8", "7",
542 CharsToUnicodeString("ra\\u0304"), CharsToUnicodeString("Hi\\u0930\\u093Endi -\\u0906\\u092E\\u093E-"),"1", "2", "2",
543 CharsToUnicodeString(""), CharsToUnicodeString("Hi\\u0930\\u093Endi -\\u0906\\u092E\\u093E-"),"1", "2", "2"
544 //data for contracted-Expanded
545 // CharsToUnicodeString("\\u00C1"), CharsToUnicodeString("Ad\\u0041\\u0301d here:"), "1", "2", "1" ,
546 // CharsToUnicodeString("\\u00C0"), CharsToUnicodeString("Ad\\u0041\\u0301d here:\\u0041\\u0300"), "11", "11", "11",
547 // "", CharsToUnicodeString("Ad\\u0041\\u0301d here:\\u0041\\u0300"), "11", "11", "11",
551 UnicodeString dataStr
;
552 logln("Testing transliterate(Replaceable, int32_t, UnicodeString, UErrorCode)");
553 UErrorCode status
= U_ZERO_ERROR
;
554 UParseError parseError
;
555 rs
="Initial String: add--";
556 t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
558 dataerrln("FAIL : construction - %s", u_errorName(status
));
560 keyboardAux(t
, Data
, rs
, 0, 20);
565 t
=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD
, parseError
, status
);
567 dataerrln("FAIL : construction - %s", u_errorName(status
));
569 keyboardAux(t
, Data
, rs
, 20, 40);
573 // t=Transliterator::createInstance("Contracted-Expanded");
574 // keyboardAux(t, Data, rs, 35, 55);
580 void TransliteratorAPITest::TestKeyboardTransliterator3(){
581 UnicodeString s
="This is the main string";
582 UnicodeString Data
[] = {
583 "0", "0", "0", "This is the main string",
584 "1", "3", "2", UnicodeString("Th\\u0069s is the main string", ""),
585 "20", "21", "20", UnicodeString("Th\\u0069s is the mai\\u006E string", "")
588 UErrorCode status
=U_ZERO_ERROR
;
589 UParseError parseError
;
590 UTransPosition index
={0, 0, 0, 0};
591 logln("Testing transliterate(Replaceable, int32_t, UErrorCode)");
592 Transliterator
*t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
593 if(t
== 0 || U_FAILURE(status
)) {
594 errln("Error creating transliterator %s", u_errorName(status
));
598 for(uint32_t i
=0; i
<UPRV_LENGTHOF(Data
); i
=i
+4){
600 index
.contextStart
=getInt(Data
[i
+0]);
601 index
.contextLimit
=index
.limit
=getInt(Data
[i
+1]);
602 index
.start
=getInt(Data
[i
+2]);
603 t
->transliterate(s
, index
, status
);
604 if(U_FAILURE(status
)){
605 errln("FAIL: " + t
->getID()+ ".transliterate(Replaceable, int32_t[], UErrorCode)-->" + (UnicodeString
)u_errorName(status
));
608 t
->finishTransliteration(s
, index
);
610 // Show the start index '{' and the cursor '|'
611 displayOutput(s
, Data
[i
+3], log
, index
);
616 void TransliteratorAPITest::TestNullTransliterator(){
617 UErrorCode status
=U_ZERO_ERROR
;
618 UnicodeString
s("Transliterate using null transliterator");
619 Transliterator
*nullTrans
=Transliterator::createInstance("Any-Null", UTRANS_FORWARD
, status
);
622 int32_t limit
=s
.length();
623 UnicodeString replaceable
=s
;
624 transLimit
=nullTrans
->transliterate(replaceable
, start
, limit
);
625 if(transLimit
!= limit
){
626 errln("ERROR: NullTransliterator->transliterate() failed");
628 doTest((UnicodeString
)"nulTrans->transliterate", replaceable
, s
);
629 replaceable
.remove();
630 replaceable
.append(s
);
631 UTransPosition index
;
632 index
.contextStart
=start
;
633 index
.contextLimit
= limit
;
636 nullTrans
->finishTransliteration(replaceable
, index
);
637 if(index
.start
!= limit
){
638 errln("ERROR: NullTransliterator->handleTransliterate() failed");
640 doTest((UnicodeString
)"NullTransliterator->handleTransliterate", replaceable
, s
);
641 callEverything(nullTrans
, __LINE__
);
646 void TransliteratorAPITest::TestRegisterUnregister(){
648 UErrorCode status
=U_ZERO_ERROR
;
649 /* Make sure it doesn't exist */
650 if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD
, status
) != NULL
) {
651 errln("FAIL: TestA-TestB already registered\n");
655 if (Transliterator::createInstance("TestA-TestB",
656 (UTransDirection)UTRANS_REVERSE) != NULL) {
657 errln("FAIL: TestA-TestB inverse already registered\n");
661 status
=U_ZERO_ERROR
;
664 UParseError parseError
;
665 Transliterator
*t
= Transliterator::createFromRules("TestA-TestB",
667 UTRANS_FORWARD
, parseError
,
670 Transliterator::registerInstance(t
);
672 /* Now check again -- should exist now*/
673 Transliterator
*s
= Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD
, status
);
675 errln("FAIL: TestA-TestB not registered\n");
678 callEverything(s
, __LINE__
);
679 callEverything(t
, __LINE__
);
683 s = Transliterator::createInstance("TestA-TestB",
684 (UTransDirection)UTRANS_REVERSE);
686 errln("FAIL: TestA-TestB inverse not registered\n");
692 /*unregister the instance*/
693 Transliterator::unregister("TestA-TestB");
694 /* now Make sure it doesn't exist */
695 if (Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD
, status
) != NULL
) {
696 errln("FAIL: TestA-TestB isn't unregistered\n");
703 int gTestFilter1ClassID
= 0;
704 int gTestFilter2ClassID
= 0;
705 int gTestFilter3ClassID
= 0;
708 * Used by TestFiltering().
710 class TestFilter1
: public UnicodeFilter
{
711 UClassID
getDynamicClassID()const { return &gTestFilter1ClassID
; }
712 virtual UnicodeFunctor
* clone() const {
713 return new TestFilter1(*this);
715 virtual UBool
contains(UChar32 c
) const {
716 if(c
==0x63 || c
==0x61 || c
==0x43 || c
==0x41)
722 virtual UnicodeString
& toPattern(UnicodeString
& result
,
723 UBool
/*escapeUnprintable*/) const {
726 virtual UBool
matchesIndexValue(uint8_t /*v*/) const {
729 virtual void addMatchSetTo(UnicodeSet
& /*toUnionTo*/) const {}
731 class TestFilter2
: public UnicodeFilter
{
732 UClassID
getDynamicClassID()const { return &gTestFilter2ClassID
; }
733 virtual UnicodeFunctor
* clone() const {
734 return new TestFilter2(*this);
736 virtual UBool
contains(UChar32 c
) const {
737 if(c
==0x65 || c
==0x6c)
743 virtual UnicodeString
& toPattern(UnicodeString
& result
,
744 UBool
/*escapeUnprintable*/) const {
747 virtual UBool
matchesIndexValue(uint8_t /*v*/) const {
750 virtual void addMatchSetTo(UnicodeSet
& /*toUnionTo*/) const {}
752 class TestFilter3
: public UnicodeFilter
{
753 UClassID
getDynamicClassID()const { return &gTestFilter3ClassID
; }
754 virtual UnicodeFunctor
* clone() const {
755 return new TestFilter3(*this);
757 virtual UBool
contains(UChar32 c
) const {
758 if(c
==0x6f || c
==0x77)
764 virtual UnicodeString
& toPattern(UnicodeString
& result
,
765 UBool
/*escapeUnprintable*/) const {
768 virtual UBool
matchesIndexValue(uint8_t /*v*/) const {
771 virtual void addMatchSetTo(UnicodeSet
& /*toUnionTo*/) const {}
775 void TransliteratorAPITest::TestGetAdoptFilter(){
776 UErrorCode status
= U_ZERO_ERROR
;
777 UParseError parseError
;
778 Transliterator
*t
=Transliterator::createInstance("Any-Hex", UTRANS_FORWARD
, parseError
, status
);
779 if(t
== 0 || U_FAILURE(status
)) {
780 errln("Error creating transliterator %s", u_errorName(status
));
784 const UnicodeFilter
*u
=t
->getFilter();
786 errln("FAIL: getFilter failed. Didn't return null when the transliterator used no filtering");
791 UnicodeString got
, temp
, message
;
792 UnicodeString data
="ABCabcbbCBa";
794 t
->transliterate(temp
);
795 t
->adoptFilter(new TestFilter1
);
798 t
->transliterate(got
);
799 UnicodeString exp
=UnicodeString("A\\u0042Ca\\u0062c\\u0062\\u0062C\\u0042a", "");
800 message
="transliteration after adoptFilter(a,A,c,C) ";
801 doTest(message
, got
, exp
);
803 logln("Testing round trip");
804 t
->adoptFilter((UnicodeFilter
*)u
);
805 if(t
->getFilter() == NULL
)
806 logln("OK: adoptFilter and getFilter round trip worked");
808 errln("FAIL: adoptFilter or getFilter round trip failed");
811 t
->transliterate(got
);
812 exp
=UnicodeString("\\u0041\\u0042\\u0043\\u0061\\u0062\\u0063\\u0062\\u0062\\u0043\\u0042\\u0061", "");
813 message
="transliteration after adopting null filter";
814 doTest(message
, got
, exp
);
815 message
="adoptFilter round trip";
816 doTest("adoptFilter round trip", got
, temp
);
818 t
->adoptFilter(new TestFilter2
);
819 callEverything(t
, __LINE__
);
821 exp
=UnicodeString("\\u0068eell\\u006Fe", "");
823 t
->transliterate(got
);
824 message
="transliteration using (e,l) filter";
825 doTest("transliteration using (e,l) filter", got
, exp
);
828 exp
=UnicodeString("\\u0061\\u0072e\\u0020\\u0077ell", "");
830 t
->transliterate(got
);
831 doTest(message
, got
, exp
);
833 t
->adoptFilter(new TestFilter3
);
835 exp
=UnicodeString("\\u0068o\\u002C\\u0020wow\\u0021", "");
837 t
->transliterate(got
);
838 message
="transliteration using (o,w) filter";
839 doTest("transliteration using (o,w) filter", got
, exp
);
842 exp
=UnicodeString("ow\\u006C", "");
844 t
->transliterate(got
);
845 doTest("transliteration using (o,w) filter", got
, exp
);
853 void TransliteratorAPITest::keyboardAux(Transliterator
*t
, UnicodeString DATA
[], UnicodeString
& s
, int32_t begin
, int32_t end
) {
854 UTransPosition index
={0, 0, 0, 0};
855 UErrorCode status
=U_ZERO_ERROR
;
856 for (int32_t i
=begin
; i
<end
; i
=i
+5) {
858 if (DATA
[i
+0] != "") {
859 log
= s
+ " + " + DATA
[i
] + " -> ";
860 index
.contextStart
=getInt(DATA
[i
+2]);
861 index
.contextLimit
=index
.limit
=getInt(DATA
[i
+3]);
862 index
.start
=getInt(DATA
[i
+4]);
863 t
->transliterate(s
, index
, DATA
[i
+0], status
);
864 if(U_FAILURE(status
)){
865 errln("FAIL: " + t
->getID()+ ".transliterate(Replaceable, int32_t[], UnicodeString, UErrorCode)-->" + (UnicodeString
)u_errorName(status
));
869 t
->finishTransliteration(s
, index
);
871 // Show the start index '{' and the cursor '|'
872 displayOutput(s
, DATA
[i
+1], log
, index
);
877 void TransliteratorAPITest::displayOutput(const UnicodeString
& got
, const UnicodeString
& expected
, UnicodeString
& log
, UTransPosition
& index
){
878 // Show the start index '{' and the cursor '|'
879 UnicodeString a
, b
, c
, d
, e
;
880 got
.extractBetween(0, index
.contextStart
, a
);
881 got
.extractBetween(index
.contextStart
, index
.start
, b
);
882 got
.extractBetween(index
.start
, index
.limit
, c
);
883 got
.extractBetween(index
.limit
, index
.contextLimit
, d
);
884 got
.extractBetween(index
.contextLimit
, got
.length(), e
);
886 append((UChar
)0x7b/*{*/).
888 append((UChar
)0x7c/*|*/).
892 append((UChar
)0x7d/*}*/).
895 logln("OK:" + prettify(log
));
897 errln("FAIL: " + prettify(log
) + ", expected " + prettify(expected
));
901 /*Internal Functions used*/
902 void TransliteratorAPITest::doTest(const UnicodeString
& message
, const UnicodeString
& result
, const UnicodeString
& expected
){
903 if (prettify(result
) == prettify(expected
))
904 logln((UnicodeString
)"Ok: " + prettify(message
) + " passed \"" + prettify(expected
) + "\"");
906 dataerrln((UnicodeString
)"FAIL:" + message
+ " failed Got-->" + prettify(result
)+ ", Expected--> " + prettify(expected
) );
911 // callEverything call all of the const (non-destructive) methods on a
912 // transliterator, just to verify that they don't fail in some
915 #define CEASSERT(a) {if (!(a)) { \
916 errln("FAIL at line %d from line %d: %s", __LINE__, line, #a); return; }}
918 void TransliteratorAPITest::callEverything(const Transliterator
*tr
, int line
) {
919 Transliterator
*clonedTR
= tr
->clone();
920 CEASSERT(clonedTR
!= NULL
);
922 int32_t maxcl
= tr
->getMaximumContextLength();
923 CEASSERT(clonedTR
->getMaximumContextLength() == maxcl
);
926 UnicodeString clonedId
;
928 clonedId
= clonedTR
->getID();
929 CEASSERT(id
== clonedId
);
931 const UnicodeFilter
*filter
= tr
->getFilter();
932 const UnicodeFilter
*clonedFilter
= clonedTR
->getFilter();
933 if (filter
== NULL
|| clonedFilter
== NULL
) {
934 // If one filter is NULL they better both be NULL.
935 CEASSERT(filter
== clonedFilter
);
937 CEASSERT(filter
!= clonedFilter
);
941 UnicodeString clonedRules
;
942 rules
= tr
->toRules(rules
, FALSE
);
943 clonedRules
= clonedTR
->toRules(clonedRules
, FALSE
);
944 CEASSERT(rules
== clonedRules
);
946 UnicodeSet sourceSet
;
947 UnicodeSet clonedSourceSet
;
948 tr
->getSourceSet(sourceSet
);
949 clonedTR
->getSourceSet(clonedSourceSet
);
950 CEASSERT(clonedSourceSet
== sourceSet
);
952 UnicodeSet targetSet
;
953 UnicodeSet clonedTargetSet
;
954 tr
->getTargetSet(targetSet
);
955 clonedTR
->getTargetSet(clonedTargetSet
);
956 CEASSERT(targetSet
== clonedTargetSet
);
958 UClassID classID
= tr
->getDynamicClassID();
959 CEASSERT(classID
== clonedTR
->getDynamicClassID());
960 CEASSERT(classID
!= 0);
965 static const int MyUnicodeFunctorTestClassID
= 0;
966 class MyUnicodeFunctorTestClass
: public UnicodeFunctor
{
968 virtual UnicodeFunctor
* clone() const {return NULL
;}
969 static UClassID
getStaticClassID(void) {return (UClassID
)&MyUnicodeFunctorTestClassID
;}
970 virtual UClassID
getDynamicClassID(void) const {return getStaticClassID();};
971 virtual void setData(const TransliterationRuleData
*) {}
974 void TransliteratorAPITest::TestUnicodeFunctor() {
975 MyUnicodeFunctorTestClass myClass
;
976 if (myClass
.toMatcher() != NULL
) {
977 errln("FAIL: UnicodeFunctor::toMatcher did not return NULL");
979 if (myClass
.toReplacer() != NULL
) {
980 errln("FAIL: UnicodeFunctor::toReplacer did not return NULL");
985 #endif /* #if !UCONFIG_NO_TRANSLITERATION */