]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/transapi.cpp
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / transapi.cpp
1 /************************************************************************
2 * COPYRIGHT:
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 ************************************************************************/
10
11 #include "unicode/utypes.h"
12
13 #if !UCONFIG_NO_TRANSLITERATION
14
15 #include "ittrans.h"
16 #include "transapi.h"
17 #include "unicode/utypes.h"
18 #include "unicode/translit.h"
19 #include "unicode/unifilt.h"
20 #include "cpdtrans.h"
21 #include "cmemory.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "unicode/rep.h"
26 #include "unicode/locid.h"
27 #include "unicode/uniset.h"
28
29 int32_t getInt(UnicodeString str)
30 {
31 char buffer[20];
32 int len=str.length();
33 if(len>=20) {
34 len=19;
35 }
36 str.extract(0, len, buffer, "");
37 buffer[len]=0;
38 return atoi(buffer);
39 }
40
41 //---------------------------------------------
42 // runIndexedTest
43 //---------------------------------------------
44
45 void
46 TransliteratorAPITest::runIndexedTest(int32_t index, UBool exec,
47 const char* &name, char* /*par*/) {
48 switch (index) {
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;
65 }
66 }
67
68
69 void TransliteratorAPITest::TestgetID() {
70 UnicodeString trans="Latin-Greek";
71 UnicodeString ID;
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));
77 return;
78 }else{
79 ID= t->getID();
80 if(ID != trans)
81 errln("FAIL: getID returned " + ID + " instead of Latin-Greek");
82 delete t;
83 }
84 int i;
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){
89 continue;
90 }
91 t = Transliterator::createInstance(ID, UTRANS_FORWARD, parseError, status);
92 if(t == 0){
93 errln("FAIL: " + ID);
94 continue;
95 }
96 if(ID != t->getID())
97 errln("FAIL: getID() returned " + t->getID() + " instead of " + ID);
98 delete t;
99 }
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");
103 }
104
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");
109 return;
110 }
111 Transliterator* t3=t1->clone();
112 Transliterator* t4=t2->clone();
113
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");
118
119
120 Transliterator* t5=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
121 if(t5 == 0)
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");
125
126
127 delete t1;
128 delete t2;
129 delete t3;
130 delete t4;
131 delete t5;
132 }
133
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));
143 return;
144 }
145
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() ");
151
152 UnicodeString TransID[]={
153 "Halfwidth-Fullwidth",
154 "Fullwidth-Halfwidth",
155 "Greek-Latin" ,
156 "Latin-Greek",
157 //"Arabic-Latin", // removed in 2.0
158 //"Latin-Arabic",
159 "Katakana-Latin",
160 "Latin-Katakana",
161 //"Hebrew-Latin", // removed in 2.0
162 //"Latin-Hebrew",
163 "Cyrillic-Latin",
164 "Latin-Cyrillic",
165 "Devanagari-Latin",
166 "Latin-Devanagari",
167 "Any-Hex",
168 "Hex-Any"
169 };
170 for(uint32_t i=0; i<UPRV_LENGTHOF(TransID); i=i+2){
171 Transliterator *trans1=Transliterator::createInstance(TransID[i], UTRANS_FORWARD, parseError, status);
172 if(t1 == 0){
173 errln("FAIL: in instantiation for" + TransID[i]);
174 continue;
175 }
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]);
179 delete inver1;
180 delete trans1;
181 }
182 delete t1;
183 delete t2;
184 delete invt1;
185 delete invt2;
186 delete inverse1;
187 delete inverse2;
188
189 }
190
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));
199 return;
200 }
201 t3=t1->clone();
202 t4=t2->clone();
203
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");
208
209 delete t1;
210 delete t2;
211 delete t3;
212 delete t4;
213
214 }
215
216 void TransliteratorAPITest::TestGetDisplayName() {
217 UnicodeString dispNames[]= {
218 //ID, displayName
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"
232 };
233 UnicodeString name="";
234 Transliterator* t;
235 UnicodeString message;
236 UErrorCode status = U_ZERO_ERROR;
237 UParseError parseError;
238
239 #if UCONFIG_NO_FORMATTING
240 logln("Skipping, UCONFIG_NO_FORMATTING is set\n");
241 return;
242 #else
243
244 for (uint32_t i=0; i<UPRV_LENGTHOF(dispNames); i=i+2 ) {
245 t = Transliterator::createInstance(dispNames[i+0], UTRANS_FORWARD, parseError, status);
246 if(t==0){
247 dataerrln("FAIL: construction: " + dispNames[i+0] + " - " + u_errorName(status));
248 status = U_ZERO_ERROR;
249 continue;
250 }
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!!!
254 name="";
255 t->getDisplayName(t->getID(), Locale::getUS(), name);
256 message.remove();
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]);
261 name="";
262
263 delete t;
264 }
265 #endif
266
267 }
268
269 void TransliteratorAPITest::TestTransliterate1(){
270
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") ,
277
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"
283 };
284
285 UnicodeString gotResult;
286 UnicodeString temp;
287 UnicodeString message;
288 Transliterator* t;
289 logln("Testing transliterate");
290 UErrorCode status = U_ZERO_ERROR;
291 UParseError parseError;
292
293 for(uint32_t i=0;i<UPRV_LENGTHOF(Data); i=i+3){
294 t=Transliterator::createInstance(Data[i+0], UTRANS_FORWARD, parseError, status);
295 if(t==0){
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;
299 continue;
300 }
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]);
305
306 //doubt here
307 temp=Data[i+1];
308 t->transliterate(temp);
309 message.remove();
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]);
314
315 callEverything(t, __LINE__);
316 delete t;
317 }
318 }
319
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")
330
331 };
332 logln("\n Testing transliterate(String, int, int, StringBuffer)");
333 Transliterator* t;
334 UnicodeString gotResBuf;
335 UnicodeString temp;
336 int32_t start, limit;
337 UErrorCode status = U_ZERO_ERROR;
338 UParseError parseError;
339
340 for(uint32_t i=0; i<UPRV_LENGTHOF(Data2); i=i+6){
341 t=Transliterator::createInstance(Data2[i+0], UTRANS_FORWARD, parseError, status);
342 if(t==0){
343 dataerrln("FAIL: construction: " + prettify(Data2[i+0]) + " - " + u_errorName(status));
344 continue;
345 }
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]);
352
353 temp=Data2[i+1];
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__);
358 delete t;
359 t = NULL;
360 }
361
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));
367 delete t;
368 return;
369 }
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");
374 } else {
375 errln("FAIL: start greater than limit value returned" + gotResBuf);
376 }
377
378 callEverything(t, __LINE__);
379 delete t;
380
381 }
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", ""),
389 };
390 int start, limit;
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));
397 delete t;
398 return;
399 }
400
401 if(t == 0)
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]);
409 }
410 delete t;
411 }
412
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);
418 if(t == 0) {
419 UnicodeString context;
420
421 if (parseError.preContext[0]) {
422 context += (UnicodeString)" at " + parseError.preContext;
423 }
424 if (parseError.postContext[0]) {
425 context += (UnicodeString)" | " + parseError.postContext;
426 }
427 errln((UnicodeString)"FAIL: can't create Any-Hex, " +
428 (UnicodeString)u_errorName(status) + context);
429 return;
430 }
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);
441
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()
449 };
450 for(uint32_t i=0; i<UPRV_LENGTHOF(index1); i++){
451 status=U_ZERO_ERROR;
452 t->transliterate(rs, index1[i], insertion, status);
453 if(status == U_ILLEGAL_ARGUMENT_ERROR)
454 logln("OK: invalid index values handled correctly");
455 else
456 errln("FAIL: invalid index values didn't throw U_ILLEGAL_ARGUMENT_ERROR throw" + (UnicodeString)u_errorName(status));
457 }
458
459 delete t;
460 }
461 void TransliteratorAPITest::TestKeyboardTransliterator1(){
462 UnicodeString Data[]={
463 //insertion, buffer
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", "") ,
469
470 "a", UnicodeString("\\u0061", "") ,
471 "b", UnicodeString("\\u0061\\u0062", "") ,
472 "z", UnicodeString("\\u0061\\u0062\\u007A", "") ,
473 "", UnicodeString("\\u0061\\u0062\\u007A", "")
474
475 };
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));
481 delete t;
482 return;
483 }
484 //keyboardAux(t, Data);
485 UTransPosition index={0, 0, 0, 0};
486 UnicodeString s;
487 uint32_t i;
488 logln("Testing transliterate(Replaceable, int32_t, UnicodeString, UErrorCode)");
489 for (i=0; i<10; i=i+2) {
490 UnicodeString log;
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));
496 continue;
497 }
498 }else {
499 log = s + " => ";
500 t->finishTransliteration(s, index);
501 }
502 // Show the start index '{' and the cursor '|'
503 displayOutput(s, Data[i+1], log, index);
504
505 }
506
507 s="";
508 status=U_ZERO_ERROR;
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){
512 UnicodeString log;
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));
519 continue;
520 }else {
521 log = s + " => ";
522 t->finishTransliteration(s, index);
523 }
524 // Show the start index '{' and the cursor '|'
525 displayOutput(s, Data[i+1], log, index);
526 }
527
528 delete t;
529 }
530
531 void TransliteratorAPITest::TestKeyboardTransliterator2(){
532 UnicodeString Data[]={
533 //insertion, buffer, index[START], index[LIMIT], index[CURSOR]
534 //data for Any-Hex
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",
548 };
549 Transliterator *t;
550 UnicodeString rs;
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);
557 if(t == 0)
558 dataerrln("FAIL : construction - %s", u_errorName(status));
559 else {
560 keyboardAux(t, Data, rs, 0, 20);
561 delete t;
562 }
563
564 rs="Hindi --";
565 t=Transliterator::createInstance("Latin-Devanagari", UTRANS_FORWARD, parseError, status);
566 if(t == 0)
567 dataerrln("FAIL : construction - %s", u_errorName(status));
568 else
569 keyboardAux(t, Data, rs, 20, 40);
570
571
572 // rs="Add here:";
573 // t=Transliterator::createInstance("Contracted-Expanded");
574 // keyboardAux(t, Data, rs, 35, 55);
575
576
577 delete t;
578 }
579
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", "")
586 };
587
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));
595 delete t;
596 return;
597 }
598 for(uint32_t i=0; i<UPRV_LENGTHOF(Data); i=i+4){
599 UnicodeString log;
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));
606 continue;
607 }
608 t->finishTransliteration(s, index);
609 log = s + " => ";
610 // Show the start index '{' and the cursor '|'
611 displayOutput(s, Data[i+3], log, index);
612 }
613
614 delete t;
615 }
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);
620 int32_t transLimit;
621 int32_t start=0;
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");
627 }
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;
634 index.start = 0;
635 index.limit = limit;
636 nullTrans->finishTransliteration(replaceable, index);
637 if(index.start != limit){
638 errln("ERROR: NullTransliterator->handleTransliterate() failed");
639 }
640 doTest((UnicodeString)"NullTransliterator->handleTransliterate", replaceable, s);
641 callEverything(nullTrans, __LINE__);
642 delete nullTrans;
643
644
645 }
646 void TransliteratorAPITest::TestRegisterUnregister(){
647
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");
652 return;
653 }
654 /* Check inverse too
655 if (Transliterator::createInstance("TestA-TestB",
656 (UTransDirection)UTRANS_REVERSE) != NULL) {
657 errln("FAIL: TestA-TestB inverse already registered\n");
658 return;
659 }
660 */
661 status =U_ZERO_ERROR;
662
663 /* Create it */
664 UParseError parseError;
665 Transliterator *t = Transliterator::createFromRules("TestA-TestB",
666 "a<>b",
667 UTRANS_FORWARD, parseError,
668 status);
669 /* Register it */
670 Transliterator::registerInstance(t);
671
672 /* Now check again -- should exist now*/
673 Transliterator *s = Transliterator::createInstance("TestA-TestB", UTRANS_FORWARD, status);
674 if (s == NULL) {
675 errln("FAIL: TestA-TestB not registered\n");
676 return;
677 }
678 callEverything(s, __LINE__);
679 callEverything(t, __LINE__);
680 delete s;
681
682 /* Check inverse too
683 s = Transliterator::createInstance("TestA-TestB",
684 (UTransDirection)UTRANS_REVERSE);
685 if (s == NULL) {
686 errln("FAIL: TestA-TestB inverse not registered\n");
687 return;
688 }
689 delete s;
690 */
691
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");
697 return;
698 }
699
700 }
701
702
703 int gTestFilter1ClassID = 0;
704 int gTestFilter2ClassID = 0;
705 int gTestFilter3ClassID = 0;
706
707 /**
708 * Used by TestFiltering().
709 */
710 class TestFilter1 : public UnicodeFilter {
711 UClassID getDynamicClassID()const { return &gTestFilter1ClassID; }
712 virtual UnicodeFunctor* clone() const {
713 return new TestFilter1(*this);
714 }
715 virtual UBool contains(UChar32 c) const {
716 if(c==0x63 || c==0x61 || c==0x43 || c==0x41)
717 return FALSE;
718 else
719 return TRUE;
720 }
721 // Stubs
722 virtual UnicodeString& toPattern(UnicodeString& result,
723 UBool /*escapeUnprintable*/) const {
724 return result;
725 }
726 virtual UBool matchesIndexValue(uint8_t /*v*/) const {
727 return FALSE;
728 }
729 virtual void addMatchSetTo(UnicodeSet& /*toUnionTo*/) const {}
730 };
731 class TestFilter2 : public UnicodeFilter {
732 UClassID getDynamicClassID()const { return &gTestFilter2ClassID; }
733 virtual UnicodeFunctor* clone() const {
734 return new TestFilter2(*this);
735 }
736 virtual UBool contains(UChar32 c) const {
737 if(c==0x65 || c==0x6c)
738 return FALSE;
739 else
740 return TRUE;
741 }
742 // Stubs
743 virtual UnicodeString& toPattern(UnicodeString& result,
744 UBool /*escapeUnprintable*/) const {
745 return result;
746 }
747 virtual UBool matchesIndexValue(uint8_t /*v*/) const {
748 return FALSE;
749 }
750 virtual void addMatchSetTo(UnicodeSet& /*toUnionTo*/) const {}
751 };
752 class TestFilter3 : public UnicodeFilter {
753 UClassID getDynamicClassID()const { return &gTestFilter3ClassID; }
754 virtual UnicodeFunctor* clone() const {
755 return new TestFilter3(*this);
756 }
757 virtual UBool contains(UChar32 c) const {
758 if(c==0x6f || c==0x77)
759 return FALSE;
760 else
761 return TRUE;
762 }
763 // Stubs
764 virtual UnicodeString& toPattern(UnicodeString& result,
765 UBool /*escapeUnprintable*/) const {
766 return result;
767 }
768 virtual UBool matchesIndexValue(uint8_t /*v*/) const {
769 return FALSE;
770 }
771 virtual void addMatchSetTo(UnicodeSet& /*toUnionTo*/) const {}
772 };
773
774
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));
781 delete t;
782 return;
783 }
784 const UnicodeFilter *u=t->getFilter();
785 if(u != NULL){
786 errln("FAIL: getFilter failed. Didn't return null when the transliterator used no filtering");
787 delete t;
788 return;
789 }
790
791 UnicodeString got, temp, message;
792 UnicodeString data="ABCabcbbCBa";
793 temp = data;
794 t->transliterate(temp);
795 t->adoptFilter(new TestFilter1);
796
797 got = data;
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);
802
803 logln("Testing round trip");
804 t->adoptFilter((UnicodeFilter*)u);
805 if(t->getFilter() == NULL)
806 logln("OK: adoptFilter and getFilter round trip worked");
807 else
808 errln("FAIL: adoptFilter or getFilter round trip failed");
809
810 got = data;
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);
817
818 t->adoptFilter(new TestFilter2);
819 callEverything(t, __LINE__);
820 data="heelloe";
821 exp=UnicodeString("\\u0068eell\\u006Fe", "");
822 got = data;
823 t->transliterate(got);
824 message="transliteration using (e,l) filter";
825 doTest("transliteration using (e,l) filter", got, exp);
826
827 data="are well";
828 exp=UnicodeString("\\u0061\\u0072e\\u0020\\u0077ell", "");
829 got = data;
830 t->transliterate(got);
831 doTest(message, got, exp);
832
833 t->adoptFilter(new TestFilter3);
834 data="ho, wow!";
835 exp=UnicodeString("\\u0068o\\u002C\\u0020wow\\u0021", "");
836 got = data;
837 t->transliterate(got);
838 message="transliteration using (o,w) filter";
839 doTest("transliteration using (o,w) filter", got, exp);
840
841 data="owl";
842 exp=UnicodeString("ow\\u006C", "");
843 got = data;
844 t->transliterate(got);
845 doTest("transliteration using (o,w) filter", got, exp);
846
847 delete t;
848
849 }
850
851
852
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) {
857 UnicodeString log;
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));
866 continue;
867 }
868 log = s + " => ";
869 t->finishTransliteration(s, index);
870 }
871 // Show the start index '{' and the cursor '|'
872 displayOutput(s, DATA[i+1], log, index);
873
874 }
875 }
876
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);
885 log.append(a).
886 append((UChar)0x7b/*{*/).
887 append(b).
888 append((UChar)0x7c/*|*/).
889 append(c).
890 append((UChar)0x7c).
891 append(d).
892 append((UChar)0x7d/*}*/).
893 append(e);
894 if (got == expected)
895 logln("OK:" + prettify(log));
896 else
897 errln("FAIL: " + prettify(log) + ", expected " + prettify(expected));
898 }
899
900
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) + "\"");
905 else
906 dataerrln((UnicodeString)"FAIL:" + message + " failed Got-->" + prettify(result)+ ", Expected--> " + prettify(expected) );
907 }
908
909
910 //
911 // callEverything call all of the const (non-destructive) methods on a
912 // transliterator, just to verify that they don't fail in some
913 // destructive way.
914 //
915 #define CEASSERT(a) {if (!(a)) { \
916 errln("FAIL at line %d from line %d: %s", __LINE__, line, #a); return; }}
917
918 void TransliteratorAPITest::callEverything(const Transliterator *tr, int line) {
919 Transliterator *clonedTR = tr->clone();
920 CEASSERT(clonedTR != NULL);
921
922 int32_t maxcl = tr->getMaximumContextLength();
923 CEASSERT(clonedTR->getMaximumContextLength() == maxcl);
924
925 UnicodeString id;
926 UnicodeString clonedId;
927 id = tr->getID();
928 clonedId = clonedTR->getID();
929 CEASSERT(id == clonedId);
930
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);
936 } else {
937 CEASSERT(filter != clonedFilter);
938 }
939
940 UnicodeString rules;
941 UnicodeString clonedRules;
942 rules = tr->toRules(rules, FALSE);
943 clonedRules = clonedTR->toRules(clonedRules, FALSE);
944 CEASSERT(rules == clonedRules);
945
946 UnicodeSet sourceSet;
947 UnicodeSet clonedSourceSet;
948 tr->getSourceSet(sourceSet);
949 clonedTR->getSourceSet(clonedSourceSet);
950 CEASSERT(clonedSourceSet == sourceSet);
951
952 UnicodeSet targetSet;
953 UnicodeSet clonedTargetSet;
954 tr->getTargetSet(targetSet);
955 clonedTR->getTargetSet(clonedTargetSet);
956 CEASSERT(targetSet == clonedTargetSet);
957
958 UClassID classID = tr->getDynamicClassID();
959 CEASSERT(classID == clonedTR->getDynamicClassID());
960 CEASSERT(classID != 0);
961
962 delete clonedTR;
963 }
964
965 static const int MyUnicodeFunctorTestClassID = 0;
966 class MyUnicodeFunctorTestClass : public UnicodeFunctor {
967 public:
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*) {}
972 };
973
974 void TransliteratorAPITest::TestUnicodeFunctor() {
975 MyUnicodeFunctorTestClass myClass;
976 if (myClass.toMatcher() != NULL) {
977 errln("FAIL: UnicodeFunctor::toMatcher did not return NULL");
978 }
979 if (myClass.toReplacer() != NULL) {
980 errln("FAIL: UnicodeFunctor::toReplacer did not return NULL");
981 }
982 }
983
984
985 #endif /* #if !UCONFIG_NO_TRANSLITERATION */