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