]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/hxuntrts.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / test / intltest / hxuntrts.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/17/2000 Madhu Creation.
9************************************************************************/
10
11#include "unicode/utypes.h"
12
13#if !UCONFIG_NO_TRANSLITERATION
14
15#include "ittrans.h"
16#include "hxuntrts.h"
17#include "unicode/utypes.h"
18#include "unicode/translit.h"
19#include "unicode/unifilt.h"
20#include "unicode/uchar.h"
21#include "hextouni.h"
22#include "intltest.h"
23#include "cmemory.h"
24#include <string.h>
25#include <stdio.h>
26/*converts a Unicodestring to integer*/
27static int32_t getInt(UnicodeString str)
28{
29 int32_t result = 0;
30 int32_t len = str.length();
31 int32_t i = 0;
32 for(i=0; i<len; i++) {
33 result = result*10+u_charDigitValue(str.char32At(i));
34 }
35 return result;
36}
37
38//---------------------------------------------
39// runIndexedTest
40//---------------------------------------------
41
42void HexToUniTransliteratorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
43{
44 if (exec) logln((UnicodeString)"TestSuite HexadecimalToUnicode Transliterator API ");
45 switch (index) {
46
47 case 0: name = "TestConstruction"; if (exec) TestConstruction(); break;
48 case 1: name = "TestCloneEqual"; if (exec) TestCloneEqual(); break;
49 case 2: name = "TestPattern"; if (exec) TestPattern(); break;
50 case 3: name = "TestSimpleTransliterate"; if (exec) TestSimpleTransliterate(); break;
51 case 4: name = "TestTransliterate"; if (exec) TestTransliterate(); break;
52 default: name = ""; break; /*needed to end loop*/
53 }
54}
55/**
56 * Used by TestConstruction() and TestTransliterate.
57 */
58uint32_t gTestHexFilterClassID = 0;
59class TestHexFilter : public UnicodeFilter {
60 virtual UClassID getDynamicClassID() const { return &gTestHexFilterClassID; }
61 virtual UnicodeFunctor* clone() const {
62 return new TestHexFilter(*this);
63 }
64 virtual UBool contains(UChar32 c) const {
65 if(c == 0x0061 || c == 0x0063 )
66 return FALSE;
67 else
68 return TRUE;
69 }
70 // Stubs
71 virtual UnicodeString& toPattern(UnicodeString& result,
72 UBool /*escapeUnprintable*/) const {
73 return result;
74 }
75 virtual UBool matchesIndexValue(uint8_t /*v*/) const {
76 return FALSE;
77 }
78 virtual void addMatchSetTo(UnicodeSet& /*toUnionTo*/) const {}
79};
80void HexToUniTransliteratorTest::TestConstruction(){
81 UErrorCode status=U_ZERO_ERROR;
82 logln("Testing the construction HexToUnicodeTransliterator()");
83 HexToUnicodeTransliterator *trans1=new HexToUnicodeTransliterator();
84 if(trans1==0){
85 errln("HexToUnicodeTransliterator construction failed Error=" + (UnicodeString)u_errorName(status));
86 return;
87 }
88 delete trans1;
89
90 logln("Testing the cosntruction HexToUnicodeTransliterator(pattern, status)");
91 UnicodeString pattern("\\\\U+0000abc");
92 trans1=new HexToUnicodeTransliterator(pattern, status);
93 if(U_FAILURE(status)){
94 errln("HexToUnicodeTransliterator construction failed with pattern =" + pattern + " Error=" + (UnicodeString)u_errorName(status));
95 status=U_ZERO_ERROR;
96 return;
97 }
98 delete trans1;
99
100 logln("Testing the construction HexToUnicodeTransliterator(pattern, status) with illegal pattern");
101 UnicodeString pattern2("\\X+");
102 trans1=new HexToUnicodeTransliterator(pattern2, status);
103 if(U_FAILURE(status)){
104 logln("OK: HexToUnicodeTransliterator construction for illegal pattern failed, as expected");
105 status=U_ZERO_ERROR;
106 } else {
107 errln("Error: calling the HexToUnicodeTransliterator constructor with illegal pattern should fail");
108 }
109 delete trans1;
110
111 logln("Testing the construction HexToUnicodeTransliterator(pattern, adoptedFilter, status)");
112 trans1=new HexToUnicodeTransliterator(pattern, NULL, status);
113 if(U_FAILURE(status)){
114 errln("HexToUnicodeTransliterator construction failed. Error=" + (UnicodeString)u_errorName(status));
115 status=U_ZERO_ERROR;
116 return;
117 }
118 logln("Testing the copy construction");
119 HexToUnicodeTransliterator *trans1copy=new HexToUnicodeTransliterator(*trans1);
120 if(trans1->toPattern() != trans1copy->toPattern() ||
121 trans1->getID() != trans1copy->getID() ){
122 errln("Copy construction failed");
123 }
124 delete trans1copy;
125 delete trans1;
126
127 logln("Testing the construction HexToUnicodeTransliterator(adoptedFilter)");
128
129 trans1=new HexToUnicodeTransliterator(new TestHexFilter);
130 if(trans1 == 0){
131 errln("HexToUnicodeTransliterator construction failed. Error=" + (UnicodeString)u_errorName(status));
132 return;
133 }
134 logln("Testing the copy construction");
135 trans1copy=new HexToUnicodeTransliterator(*trans1);
136 if(trans1->getFilter() == NULL || trans1copy->getFilter() == NULL ||
137 trans1->toPattern() != trans1copy->toPattern() ||
138 trans1->getID() != trans1copy->getID() ){
139 errln("Copy construction failed");
140 }
141
142 delete trans1copy;
143 delete trans1;
144
145}
146
147void HexToUniTransliteratorTest::TestCloneEqual(){
148 UErrorCode status=U_ZERO_ERROR;
149 HexToUnicodeTransliterator *transdefault=new HexToUnicodeTransliterator();
150 UnicodeString pattern1("\\U##00");
151 UnicodeString pattern2("\\\\uni0000");
152 HexToUnicodeTransliterator *trans1=new HexToUnicodeTransliterator(pattern1, status);
153 if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
154 errln("HexToUnicodeTransliterator construction failed");
155 status=U_ZERO_ERROR;
156 return;
157 }
158 HexToUnicodeTransliterator *trans2=new HexToUnicodeTransliterator(pattern2, status);
159 if(U_FAILURE(status) && status==U_ILLEGAL_ARGUMENT_ERROR){
160 errln("HexToUnicodeTransliterator construction failed");
161 status=U_ZERO_ERROR;
162 return;
163 }
164
165 logln("Testing the clone() API of the HexToUnicodeTransliterator");
166 HexToUnicodeTransliterator *transdefaultclone=(HexToUnicodeTransliterator*)transdefault->clone();
167 HexToUnicodeTransliterator *trans1clone=(HexToUnicodeTransliterator*)trans1->clone();
168 HexToUnicodeTransliterator *trans2clone=(HexToUnicodeTransliterator*)trans2->clone();
169 if(transdefault->toPattern() != transdefaultclone->toPattern() ||
170 trans1->toPattern() != trans1clone->toPattern() ||
171 trans2->toPattern() != trans2clone->toPattern() ||
172 transdefault->toPattern() == trans1->toPattern() ||
173 trans1->toPattern() == trans2clone->toPattern() ||
174 trans2->toPattern() == transdefault->toPattern() ) {
175 errln("Error: clone() failed");
176 }
177
178 logln("Testing the =operator of the HexToUnicodeTransliterator");
179 HexToUnicodeTransliterator *transdefaultequal=new HexToUnicodeTransliterator();
180 HexToUnicodeTransliterator *trans1equal=new HexToUnicodeTransliterator();
181 HexToUnicodeTransliterator *trans2equal=new HexToUnicodeTransliterator();
182 *transdefaultequal=*transdefault;
183 *trans1equal=*trans1;
184 *trans2equal=*trans2;
185 if(transdefault->toPattern() != transdefaultequal->toPattern() ||
186 trans1->toPattern() != trans1equal->toPattern() ||
187 trans2->toPattern() != trans2equal->toPattern() ||
188 transdefault->toPattern() == trans1->toPattern() ||
189 trans1->toPattern() == trans2equal->toPattern() ||
190 trans2->toPattern() == transdefault->toPattern() ) {
191 errln("Error: equal() failed");
192 }
193 if(transdefaultclone->toPattern() != transdefaultequal->toPattern() ||
194 trans1equal->toPattern() != trans1clone->toPattern() ||
195 trans2clone->toPattern() != trans2equal->toPattern() ){
196 errln("Error: equal() or clone() failed");
197 }
198 delete transdefaultclone;
199 delete trans1clone;
200 delete trans2clone;
201 delete transdefaultequal;
202 delete trans1equal;
203 delete trans2equal;
204 delete transdefault;
205 delete trans1;
206 delete trans2;
207}
208
209void HexToUniTransliteratorTest::TestPattern(){
210 logln("Testing the applyPattern() and toPattern() API of HexToUnicodeTransliterator");
211 UErrorCode status = U_ZERO_ERROR;
212 /*default transliterator has pattern \\u0000*/
213 HexToUnicodeTransliterator *transdefault=new HexToUnicodeTransliterator();
214 if(transdefault == 0){
215 errln("HexToUnicodeTransliterator construction failed. Error=" + (UnicodeString)u_errorName(status));
216 return;
217 }
218 UnicodeString defaultpattern=transdefault->toPattern();
219
220 UnicodeString pattern1("\\\\U+0000", "");
221 HexToUnicodeTransliterator *trans1=new HexToUnicodeTransliterator(pattern1, NULL, status);
222 if(U_FAILURE(status) ){
223 errln("HexToUnicodeTransliterator construction failed with pattern =" + pattern1);
224 status=U_ZERO_ERROR;
225 return;
226 }
227 /*test toPattern() */
228 if(transdefault->toPattern() == trans1->toPattern() ||
229 transdefault->toPattern() != UnicodeString("\\\\u0000;\\\\U0000;u+0000;U+0000", "") ||
230 trans1->toPattern() != pattern1 ){
231 errln("Error: toPattern() failed "+ transdefault->toPattern());
232 }
233
234 /*apply patterns for transdefault*/
235 UnicodeString str("abKf");
236 expectPattern(*transdefault, pattern1, UnicodeString("\\U+0061\\U+0062\\U+004B\\U+0066", ""), str);
237 expectPattern(*transdefault, UnicodeString("\\U##00,", ""), UnicodeString("U61,U62,U4B,U66,", ""), str);
238 expectPattern(*transdefault, defaultpattern, UnicodeString("\\u0061\\u0062\\u004B\\u0066", ""), str);
239 expectPattern(*trans1, UnicodeString("\\uni0000", ""), UnicodeString("uni0061uni0062uni004Buni0066", ""), str);
240 expectPattern(*trans1, UnicodeString("\\\\S-0000-E", ""), UnicodeString("\\S-0061-E\\S-0062-E\\S-004B-E\\S-0066-E", ""), str);
241 expectPattern(*trans1, UnicodeString("\\u##0000", ""), UnicodeString("\\u##0061\\u##0062", ""), "FAIL");
242 expectPattern(*trans1, UnicodeString("\\*0000", ""), UnicodeString("*0061*0062*004B*0066", ""), str);
243 expectPattern(*trans1, UnicodeString("\\u####", ""), UnicodeString("\\u##0061\\u##0062", ""), "FAIL");
244
245 delete trans1;
246 delete transdefault;
247
248}
249void HexToUniTransliteratorTest::TestSimpleTransliterate(){
250 logln("Testing the handleTransliterate() API of HexToUnicodeTransliterator");
251 UErrorCode status=U_ZERO_ERROR;
252 UnicodeString pattern1("\\\\U+0000", "");
253 HexToUnicodeTransliterator *trans1=new HexToUnicodeTransliterator(pattern1, NULL, status);
254 if(U_FAILURE(status)){
255 errln("HexToUnicodeTransliterator construction failed with pattern =" + pattern1 + "Error: " + (UnicodeString)u_errorName(status));
256 status=U_ZERO_ERROR;
257 return;
258 }
259 UnicodeString source("He\\U+006C\\U+006C\\U+006F", "");
260 UnicodeString rsource(source);
261 UTransPosition index;
262 index.contextStart =1;
263 index.contextLimit = source.length();
264 index.start = 2;
265 index.limit =source.length();
266 UnicodeString expected("Hello");
267 trans1->handleTransliterate(rsource, index, FALSE);
268 expectAux(trans1->getID() + ":handleTransliterator ", source + "-->" + rsource, rsource==expected, expected);
269 expect(*trans1, "", UnicodeString("\\U+0048\\U+0065\\U+006C\\U+006C\\U+006F", ""), expected);
270 delete trans1;
271
272 HexToUnicodeTransliterator *trans2=new HexToUnicodeTransliterator(new TestHexFilter);
273 expect(*trans2, "with Filter(0x0061, 0x0063) ", CharsToUnicodeString("\\u0061\\u0062\\u0063"),
274 CharsToUnicodeString("\\u0061b\\u0063") );
275 delete trans2;
276
277}
278void HexToUniTransliteratorTest::TestTransliterate(){
279 UErrorCode status=U_ZERO_ERROR;
280 UnicodeString Data[]={
281 //pattern, source, index.contextStart, index.contextLimit, index.start, expectedResult,
282 UnicodeString("U+##00", ""), UnicodeString("abU+63", ""), "1", "7", "2", UnicodeString("abc", ""),
283 UnicodeString("\\\\u0000", ""), UnicodeString("a\\u0062c", ""), "1", "7", "1", UnicodeString("abc", ""),
284 UnicodeString("Uni0000", ""), UnicodeString("abUni0063", ""), "1", "9", "2", UnicodeString("abc", ""),
285 UnicodeString("U[0000]", ""), UnicodeString("heU[006C]U[006C]o", ""), "0", "16", "2", UnicodeString("hello", ""),
286 UnicodeString("prefix-0000-suffix", ""), UnicodeString("aprefix-0062-suffixprefix-0063-suffix", ""), "1", "39", "1", UnicodeString("abc", ""),
287 UnicodeString("*##00*", ""), UnicodeString("hell*6F**74**68**65*re", ""), "1", "20", "4", UnicodeString("hellothere", ""),
288
289 };
290 uint32_t i;
291 for(i=0;i<sizeof(Data)/sizeof(Data[0]);i=i+6){
292 HexToUnicodeTransliterator *trans1=new HexToUnicodeTransliterator(Data[i+0], NULL, status);
293 if(U_FAILURE(status)){
294 errln("HexToUnicodeTransliterator construction failed with pattern =" + Data[i+0]);
295 status=U_ZERO_ERROR;
296 continue;
297 }
298 expectTranslit(*trans1, "", Data[i+1], getInt(Data[i+2]), getInt(Data[i+3]), getInt(Data[i+4]), Data[i+5] );
299 delete trans1;
300
301 }
302
303
304}
305
306//======================================================================
307// Support methods
308//======================================================================
309
310void HexToUniTransliteratorTest::expectTranslit(const HexToUnicodeTransliterator& t,
311 const UnicodeString& message,
312 const UnicodeString& source,
313 int32_t start, int32_t limit, int32_t cursor,
314 const UnicodeString& expectedResult){
315
316
317 UTransPosition _index;
318 _index.contextStart =start;
319 _index.contextLimit = limit;
320 _index.start = cursor;
321 _index.limit = limit;
322 UTransPosition index;
323 uprv_memcpy(&index, &_index, sizeof(index));
324 UnicodeString rsource(source);
325 t.handleTransliterate(rsource, index, FALSE);
326 expectAux(t.getID() + ":handleTransliterator(increment=FALSE) "+ message, source + "-->" + rsource, rsource==expectedResult, expectedResult);
327
328 UnicodeString rsource2(source);
329 uprv_memcpy(&index, &_index, sizeof(index));
330 t.handleTransliterate(rsource2, index, TRUE);
331 expectAux(t.getID() + ":handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
332
333 /*ceates a copy constructor and checks the transliteration*/
334 HexToUnicodeTransliterator *copy=new HexToUnicodeTransliterator(t);
335 rsource2.remove();
336 rsource2.append(source);
337 uprv_memcpy(&index, &_index, sizeof(index));
338 copy->handleTransliterate(rsource2, index, FALSE);
339 expectAux(t.getID() + "COPY:handleTransliterator(increment=FALSE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
340
341 rsource2.remove();
342 rsource2.append(source);
343 uprv_memcpy(&index, &_index, sizeof(index));
344 copy->handleTransliterate(rsource2, index, TRUE);
345 expectAux(t.getID() + "COPY:handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
346 delete copy;
347
348 /*creates a clone and tests transliteration*/
349 HexToUnicodeTransliterator *clone=(HexToUnicodeTransliterator*)t.clone();
350 rsource2.remove();
351 rsource2.append(source);
352 uprv_memcpy(&index, &_index, sizeof(index));
353 clone->handleTransliterate(rsource2, index, FALSE);
354 expectAux(t.getID() + "CLONE:handleTransliterator(increment=FALSE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
355
356 rsource2.remove();
357 rsource2.append(source);
358 uprv_memcpy(&index, &_index, sizeof(index));
359 clone->handleTransliterate(rsource2, index, TRUE);
360 expectAux(t.getID() + "CLONE:handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
361 delete clone;
362
363 /*Uses the assignment operator to create a transliterator and tests transliteration*/
364 HexToUnicodeTransliterator equal=t;
365 rsource2.remove();
366 rsource2.append(source);
367 uprv_memcpy(&index, &_index, sizeof(index));
368 equal.handleTransliterate(rsource2, index, FALSE);
369 expectAux(t.getID() + "=OPERATOR:handleTransliterator(increment=FALSE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
370
371 rsource2.remove();
372 rsource2.append(source);
373 uprv_memcpy(&index, &_index, sizeof(index));
374 equal.handleTransliterate(rsource2, index, TRUE);
375 expectAux(t.getID() + "=OPERATOR:handleTransliterator(increment=TRUE) "+ message, source + "-->" + rsource2, rsource2==expectedResult, expectedResult);
376
377}
378
379
380void HexToUniTransliteratorTest::expectPattern(HexToUnicodeTransliterator& t,
381 const UnicodeString& pattern,
382 const UnicodeString& source,
383 const UnicodeString& expectedResult){
384
385 UErrorCode status=U_ZERO_ERROR;
386 t.applyPattern(pattern, status);
387 if(expectedResult == "FAIL"){
388 if(U_FAILURE(status)){
389 logln("OK: calling applyPattern() with illegal pattern failed as expected. Error=" + (UnicodeString)u_errorName(status));
390 status=U_ZERO_ERROR;
391 return;
392 }
393 }
394 else{
395 if(U_FAILURE(status)){
396 errln("Error: applyPattern() failed with pattern =" + pattern + "--->" + (UnicodeString)u_errorName(status));
397 return;
398 }else {
399 if(t.toPattern() != pattern) {
400 errln("Error: applyPattern or toPatten failed. Expected: " + pattern + "Got: " + t.toPattern());
401 }
402 else{
403 logln("OK: applyPattern passed. Testing transliteration");
404 expect(t, " with pattern "+pattern, source, expectedResult);
405 }
406 }
407 }
408
409}
410void HexToUniTransliteratorTest::expect(const HexToUnicodeTransliterator& t,
411 const UnicodeString& message,
412 const UnicodeString& source,
413 const UnicodeString& expectedResult) {
414
415 UnicodeString rsource(source);
416 t.transliterate(rsource);
417 expectAux(t.getID() + ":Replaceable " + message, source + "->" + rsource, rsource==expectedResult, expectedResult);
418
419 // Test handleTransliterate (incremental) transliteration --
420 rsource.remove();
421 rsource.append(source);
422 UTransPosition index;
423 index.contextStart =0;
424 index.contextLimit =source.length();
425 index.start=0;
426 index.limit = source.length();
427 t.handleTransliterate(rsource, index, TRUE);
428 expectAux(t.getID() + ":handleTransliterate " + message, source + "->" + rsource, rsource==expectedResult, expectedResult);
429
430
431}
432void HexToUniTransliteratorTest::expectAux(const UnicodeString& tag,
433 const UnicodeString& summary, UBool pass,
434 const UnicodeString& expectedResult) {
435 if (pass) {
436 logln(UnicodeString("(")+tag+") " + prettify(summary));
437 } else {
438 errln(UnicodeString("FAIL: (")+tag+") "
439 + prettify(summary)
440 + ", expected " + prettify(expectedResult));
441 }
442}
443
444#endif /* #if !UCONFIG_NO_TRANSLITERATION */