]>
Commit | Line | Data |
---|---|---|
b75a7d8f | 1 | /* |
374ca955 A |
2 | ******************************************************************************* |
3 | * Copyright (C) 1997-2004, International Business Machines | |
4 | * Corporation and others. All Rights Reserved. | |
5 | ******************************************************************************* | |
6 | * Date Name Description | |
7 | * 06/23/00 aliu Creation. | |
8 | ******************************************************************************* | |
9 | */ | |
b75a7d8f A |
10 | |
11 | #include "unicode/utypes.h" | |
12 | ||
13 | #if !UCONFIG_NO_TRANSLITERATION | |
14 | ||
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include "unicode/utrans.h" | |
18 | #include "unicode/ustring.h" | |
19 | #include "cintltst.h" | |
20 | ||
21 | #define TEST(x) addTest(root, &x, "utrans/" # x) | |
22 | ||
23 | static void TestAPI(void); | |
24 | static void TestSimpleRules(void); | |
25 | static void TestFilter(void); | |
26 | static void TestOpenInverse(void); | |
27 | static void TestClone(void); | |
28 | static void TestRegisterUnregister(void); | |
29 | static void TestExtractBetween(void); | |
374ca955 | 30 | static void TestUnicodeIDs(void); |
b75a7d8f A |
31 | |
32 | static void _expectRules(const char*, const char*, const char*); | |
33 | static void _expect(const UTransliterator* trans, const char* cfrom, const char* cto); | |
34 | ||
35 | void addUTransTest(TestNode** root); | |
36 | ||
37 | ||
38 | void | |
39 | addUTransTest(TestNode** root) { | |
40 | TEST(TestAPI); | |
41 | TEST(TestSimpleRules); | |
42 | TEST(TestFilter); | |
43 | TEST(TestOpenInverse); | |
44 | TEST(TestClone); | |
45 | TEST(TestRegisterUnregister); | |
46 | TEST(TestExtractBetween); | |
374ca955 | 47 | TEST(TestUnicodeIDs); |
b75a7d8f A |
48 | } |
49 | ||
50 | /*------------------------------------------------------------------ | |
51 | * Replaceable glue | |
52 | * | |
53 | * To test the Replaceable glue we have to dummy up a C-based | |
54 | * Replaceable callback. This code is for testing purposes only. | |
55 | *------------------------------------------------------------------*/ | |
56 | ||
57 | typedef struct XReplaceable { | |
58 | UChar* text; /* MUST BE null-terminated */ | |
59 | } XReplaceable; | |
60 | ||
61 | static void InitXReplaceable(XReplaceable* rep, const char* cstring) { | |
62 | rep->text = malloc(sizeof(UChar) * (strlen(cstring)+1)); | |
63 | u_uastrcpy(rep->text, cstring); | |
64 | } | |
65 | ||
66 | static void FreeXReplaceable(XReplaceable* rep) { | |
67 | if (rep->text != NULL) { | |
68 | free(rep->text); | |
69 | rep->text = NULL; | |
70 | } | |
71 | } | |
72 | ||
73 | /* UReplaceableCallbacks callback */ | |
74 | static int32_t Xlength(const UReplaceable* rep) { | |
75 | const XReplaceable* x = (const XReplaceable*)rep; | |
76 | return u_strlen(x->text); | |
77 | } | |
78 | ||
79 | /* UReplaceableCallbacks callback */ | |
80 | static UChar XcharAt(const UReplaceable* rep, int32_t offset) { | |
81 | const XReplaceable* x = (const XReplaceable*)rep; | |
82 | return x->text[offset]; | |
83 | } | |
84 | ||
85 | /* UReplaceableCallbacks callback */ | |
86 | static UChar32 Xchar32At(const UReplaceable* rep, int32_t offset) { | |
87 | const XReplaceable* x = (const XReplaceable*)rep; | |
88 | return x->text[offset]; | |
89 | } | |
90 | ||
91 | /* UReplaceableCallbacks callback */ | |
92 | static void Xreplace(UReplaceable* rep, int32_t start, int32_t limit, | |
93 | const UChar* text, int32_t textLength) { | |
94 | XReplaceable* x = (XReplaceable*)rep; | |
95 | int32_t newLen = Xlength(rep) + limit - start + textLength; | |
96 | UChar* newText = (UChar*) malloc(sizeof(UChar) * (newLen+1)); | |
97 | u_strncpy(newText, x->text, start); | |
98 | u_strncpy(newText + start, text, textLength); | |
99 | u_strcpy(newText + start + textLength, x->text + limit); | |
100 | free(x->text); | |
101 | x->text = newText; | |
102 | } | |
103 | ||
104 | /* UReplaceableCallbacks callback */ | |
105 | static void Xcopy(UReplaceable* rep, int32_t start, int32_t limit, int32_t dest) { | |
106 | XReplaceable* x = (XReplaceable*)rep; | |
107 | int32_t newLen = Xlength(rep) + limit - start; | |
108 | UChar* newText = (UChar*) malloc(sizeof(UChar) * (newLen+1)); | |
109 | u_strncpy(newText, x->text, dest); | |
110 | u_strncpy(newText + dest, x->text + start, limit - start); | |
111 | u_strcpy(newText + dest + limit - start, x->text + dest); | |
112 | free(x->text); | |
113 | x->text = newText; | |
114 | } | |
115 | ||
116 | /* UReplaceableCallbacks callback */ | |
117 | static void Xextract(UReplaceable* rep, int32_t start, int32_t limit, UChar* dst) { | |
118 | XReplaceable* x = (XReplaceable*)rep; | |
119 | int32_t len = limit - start; | |
120 | u_strncpy(dst, x->text, len); | |
121 | } | |
122 | ||
123 | static void InitXReplaceableCallbacks(UReplaceableCallbacks* callbacks) { | |
124 | callbacks->length = Xlength; | |
125 | callbacks->charAt = XcharAt; | |
126 | callbacks->char32At = Xchar32At; | |
127 | callbacks->replace = Xreplace; | |
128 | callbacks->extract = Xextract; | |
129 | callbacks->copy = Xcopy; | |
130 | } | |
131 | ||
132 | /*------------------------------------------------------------------ | |
133 | * Tests | |
134 | *------------------------------------------------------------------*/ | |
135 | ||
136 | static void TestAPI() { | |
137 | enum { BUF_CAP = 128 }; | |
138 | char buf[BUF_CAP], buf2[BUF_CAP]; | |
139 | UErrorCode status = U_ZERO_ERROR; | |
140 | UTransliterator* trans = NULL; | |
141 | int32_t i, n; | |
142 | ||
143 | /* Test getAvailableIDs */ | |
144 | n = utrans_countAvailableIDs(); | |
145 | if (n < 1) { | |
146 | log_err("FAIL: utrans_countAvailableIDs() returned %d\n", n); | |
147 | } else { | |
148 | log_verbose("System ID count: %d\n", n); | |
149 | } | |
150 | for (i=0; i<n; ++i) { | |
151 | utrans_getAvailableID(i, buf, BUF_CAP); | |
152 | if (*buf == 0) { | |
153 | log_err("FAIL: System transliterator %d: \"\"\n", i); | |
154 | } else { | |
155 | log_verbose("System transliterator %d: \"%s\"\n", i, buf); | |
156 | } | |
157 | } | |
158 | ||
159 | /* Test open */ | |
160 | utrans_getAvailableID(0, buf, BUF_CAP); | |
161 | trans = utrans_open(buf, UTRANS_FORWARD,NULL,0,NULL, &status); | |
162 | if (U_FAILURE(status)) { | |
163 | log_err("FAIL: utrans_open(%s) failed, error=%s\n", | |
164 | buf, u_errorName(status)); | |
165 | } | |
166 | ||
167 | else { | |
168 | /* Test getID */ | |
169 | utrans_getID(trans, buf2, BUF_CAP); | |
170 | if (0 != strcmp(buf, buf2)) { | |
171 | log_err("FAIL: utrans_getID(%s) returned %s\n", | |
172 | buf, buf2); | |
173 | } | |
174 | utrans_close(trans); | |
175 | } | |
176 | } | |
177 | ||
374ca955 A |
178 | static void TestUnicodeIDs() { |
179 | UEnumeration *uenum; | |
180 | UTransliterator *utrans; | |
181 | const UChar *id, *id2; | |
182 | int32_t idLength, id2Length, count, count2; | |
183 | ||
184 | UErrorCode errorCode; | |
185 | ||
186 | errorCode=U_ZERO_ERROR; | |
187 | uenum=utrans_openIDs(&errorCode); | |
188 | if(U_FAILURE(errorCode)) { | |
189 | log_err("utrans_openIDs() failed - %s\n", u_errorName(errorCode)); | |
190 | return; | |
191 | } | |
192 | ||
193 | count=uenum_count(uenum, &errorCode); | |
194 | if(U_FAILURE(errorCode) || count<1) { | |
195 | log_err("uenum_count(transliterator IDs)=%d - %s\n", count, u_errorName(errorCode)); | |
196 | } | |
197 | ||
198 | count=0; | |
199 | for(;;) { | |
200 | id=uenum_unext(uenum, &idLength, &errorCode); | |
201 | if(U_FAILURE(errorCode)) { | |
202 | log_err("uenum_unext(transliterator ID %d) failed - %s\n", count, u_errorName(errorCode)); | |
203 | break; | |
204 | } | |
205 | if(id==NULL) { | |
206 | break; | |
207 | } | |
208 | ||
209 | if(++count>10) { | |
210 | /* try to actually open only a few transliterators */ | |
211 | continue; | |
212 | } | |
213 | ||
214 | utrans=utrans_openU(id, idLength, UTRANS_FORWARD, NULL, 0, NULL, &errorCode); | |
215 | if(U_FAILURE(errorCode)) { | |
216 | log_err("utrans_openU(%s) failed - %s\n", aescstrdup(id, idLength), u_errorName(errorCode)); | |
217 | continue; | |
218 | } | |
219 | ||
220 | id2=utrans_getUnicodeID(utrans, &id2Length); | |
221 | if(idLength!=id2Length || 0!=u_memcmp(id, id2, idLength)) { | |
222 | log_err("utrans_getUnicodeID(%s) does not match the original ID\n", aescstrdup(id, idLength)); | |
223 | } | |
224 | ||
225 | utrans_close(utrans); | |
226 | } | |
227 | ||
228 | uenum_reset(uenum, &errorCode); | |
229 | if(U_FAILURE(errorCode) || count<1) { | |
230 | log_err("uenum_reset(transliterator IDs) failed - %s\n", u_errorName(errorCode)); | |
231 | } else { | |
232 | count2=uenum_count(uenum, &errorCode); | |
233 | if(U_FAILURE(errorCode) || count<1) { | |
234 | log_err("2nd uenum_count(transliterator IDs)=%d - %s\n", count2, u_errorName(errorCode)); | |
235 | } else if(count!=count2) { | |
236 | log_err("uenum_unext(transliterator IDs) returned %d IDs but uenum_count() after uenum_reset() claims there are %d\n", count, count2); | |
237 | } | |
238 | } | |
239 | ||
240 | uenum_close(uenum); | |
241 | } | |
242 | ||
b75a7d8f A |
243 | static void TestOpenInverse(){ |
244 | UErrorCode status=U_ZERO_ERROR; | |
245 | UTransliterator* t1=NULL; | |
246 | UTransliterator* inverse1=NULL; | |
247 | enum { BUF_CAP = 128 }; | |
248 | char buf1[BUF_CAP]; | |
249 | int32_t i=0; | |
250 | ||
251 | const char TransID[][25]={ | |
252 | "Halfwidth-Fullwidth", | |
253 | "Fullwidth-Halfwidth", | |
254 | "Greek-Latin" , | |
255 | "Latin-Greek", | |
256 | /*"Arabic-Latin", // Removed in 2.0*/ | |
257 | /*"Latin-Arabic", // Removed in 2.0*/ | |
258 | "Katakana-Latin", | |
259 | "Latin-Katakana", | |
260 | /*"Hebrew-Latin", // Removed in 2.0*/ | |
261 | /*"Latin-Hebrew", // Removed in 2.0*/ | |
262 | "Cyrillic-Latin", | |
263 | "Latin-Cyrillic", | |
264 | "Devanagari-Latin", | |
265 | "Latin-Devanagari", | |
266 | "Any-Hex", | |
267 | "Hex-Any" | |
268 | }; | |
269 | ||
270 | for(i=0; i<sizeof(TransID)/sizeof(TransID[0]); i=i+2){ | |
271 | status = U_ZERO_ERROR; | |
272 | t1=utrans_open(TransID[i], UTRANS_FORWARD,NULL,0,NULL, &status); | |
273 | if(t1 == NULL || U_FAILURE(status)){ | |
274 | log_err("FAIL: in instantiation for id=%s\n", TransID[i]); | |
275 | continue; | |
276 | } | |
277 | inverse1=utrans_openInverse(t1, &status); | |
278 | if(U_FAILURE(status)){ | |
279 | log_err("FAIL: utrans_openInverse() failed for id=%s. Error=%s\n", TransID[i], myErrorName(status)); | |
280 | continue; | |
281 | } | |
282 | utrans_getID(inverse1, buf1, BUF_CAP); | |
283 | if(strcmp(buf1, TransID[i+1]) != 0){ | |
284 | log_err("FAIL :openInverse() for %s returned %s instead of %s\n", TransID[i], buf1, TransID[i+1]); | |
285 | } | |
286 | utrans_close(t1); | |
287 | utrans_close(inverse1); | |
288 | } | |
289 | } | |
290 | ||
291 | static void TestClone(){ | |
292 | UErrorCode status=U_ZERO_ERROR; | |
293 | UTransliterator* t1=NULL; | |
294 | UTransliterator* t2=NULL; | |
295 | UTransliterator* t3=NULL; | |
296 | UTransliterator* t4=NULL; | |
297 | enum { BUF_CAP = 128 }; | |
298 | char buf1[BUF_CAP], buf2[BUF_CAP], buf3[BUF_CAP]; | |
299 | ||
300 | t1=utrans_open("Latin-Devanagari", UTRANS_FORWARD, NULL,0,NULL,&status); | |
301 | if(U_FAILURE(status)){ | |
302 | log_err("FAIL: construction\n"); | |
303 | return; | |
304 | } | |
305 | t2=utrans_open("Latin-Greek", UTRANS_FORWARD, NULL,0,NULL,&status); | |
306 | if(U_FAILURE(status)){ | |
307 | log_err("FAIL: construction\n"); | |
308 | utrans_close(t1); | |
309 | return; | |
310 | } | |
311 | ||
312 | t3=utrans_clone(t1, &status); | |
313 | t4=utrans_clone(t2, &status); | |
314 | ||
315 | utrans_getID(t1, buf1, BUF_CAP); | |
316 | utrans_getID(t2, buf2, BUF_CAP); | |
317 | utrans_getID(t3, buf3, BUF_CAP); | |
318 | ||
319 | if(strcmp(buf1, buf3) != 0 || | |
320 | strcmp(buf1, buf2) == 0) { | |
321 | log_err("FAIL: utrans_clone() failed\n"); | |
322 | } | |
323 | ||
324 | utrans_getID(t4, buf3, BUF_CAP); | |
325 | ||
326 | if(strcmp(buf2, buf3) != 0 || | |
327 | strcmp(buf1, buf3) == 0) { | |
328 | log_err("FAIL: utrans_clone() failed\n"); | |
329 | } | |
330 | ||
331 | utrans_close(t1); | |
332 | utrans_close(t2); | |
333 | utrans_close(t3); | |
334 | utrans_close(t4); | |
335 | ||
336 | } | |
337 | ||
338 | static void TestRegisterUnregister(){ | |
339 | UErrorCode status=U_ZERO_ERROR; | |
340 | UTransliterator* t1=NULL; | |
374ca955 | 341 | UTransliterator* rules=NULL, *rules2; |
b75a7d8f A |
342 | UTransliterator* inverse1=NULL; |
343 | UChar rule[]={ 0x0061, 0x003c, 0x003e, 0x0063}; /*a<>b*/ | |
344 | ||
374ca955 A |
345 | U_STRING_DECL(ID, "TestA-TestB", 11); |
346 | U_STRING_INIT(ID, "TestA-TestB", 11); | |
347 | ||
b75a7d8f A |
348 | /* Make sure it doesn't exist */ |
349 | t1=utrans_open("TestA-TestB", UTRANS_FORWARD,NULL,0,NULL, &status); | |
350 | if(t1 != NULL || U_SUCCESS(status)) { | |
351 | log_err("FAIL: TestA-TestB already registered\n"); | |
352 | return; | |
353 | } | |
354 | status=U_ZERO_ERROR; | |
355 | /* Check inverse too */ | |
356 | inverse1=utrans_open("TestA-TestB", UTRANS_REVERSE, NULL,0,NULL,&status); | |
357 | if(inverse1 != NULL || U_SUCCESS(status)) { | |
358 | log_err("FAIL: TestA-TestB already registered\n"); | |
359 | return; | |
360 | } | |
361 | status=U_ZERO_ERROR; | |
362 | /* Create it */ | |
363 | rules=utrans_open("TestA-TestB",UTRANS_FORWARD, rule, 4, NULL, &status); | |
364 | if(U_FAILURE(status)){ | |
365 | log_err("FAIL: utrans_openRules(a<>B) failed with error=%s\n", myErrorName(status)); | |
366 | return; | |
367 | } | |
374ca955 A |
368 | |
369 | /* clone it so we can register it a second time */ | |
370 | rules2=utrans_clone(rules, &status); | |
371 | if(U_FAILURE(status)) { | |
372 | log_err("FAIL: utrans_clone(a<>B) failed with error=%s\n", myErrorName(status)); | |
373 | return; | |
374 | } | |
375 | ||
b75a7d8f A |
376 | status=U_ZERO_ERROR; |
377 | /* Register it */ | |
378 | utrans_register(rules, &status); | |
379 | if(U_FAILURE(status)){ | |
380 | log_err("FAIL: utrans_register failed with error=%s\n", myErrorName(status)); | |
381 | return; | |
382 | } | |
383 | status=U_ZERO_ERROR; | |
384 | /* Now check again -- should exist now*/ | |
385 | t1= utrans_open("TestA-TestB", UTRANS_FORWARD, NULL,0,NULL,&status); | |
386 | if(U_FAILURE(status) || t1 == NULL){ | |
387 | log_err("FAIL: TestA-TestB not registered\n"); | |
388 | return; | |
389 | } | |
390 | utrans_close(t1); | |
391 | ||
392 | /*unregister the instance*/ | |
393 | status=U_ZERO_ERROR; | |
394 | utrans_unregister("TestA-TestB"); | |
395 | /* now Make sure it doesn't exist */ | |
396 | t1=utrans_open("TestA-TestB", UTRANS_FORWARD,NULL,0,NULL, &status); | |
397 | if(U_SUCCESS(status) || t1 != NULL) { | |
398 | log_err("FAIL: TestA-TestB isn't unregistered\n"); | |
399 | return; | |
400 | } | |
374ca955 A |
401 | utrans_close(t1); |
402 | ||
403 | /* now with utrans_unregisterID(const UChar *) */ | |
404 | status=U_ZERO_ERROR; | |
405 | utrans_register(rules2, &status); | |
406 | if(U_FAILURE(status)){ | |
407 | log_err("FAIL: 2nd utrans_register failed with error=%s\n", myErrorName(status)); | |
408 | return; | |
409 | } | |
410 | status=U_ZERO_ERROR; | |
411 | /* Now check again -- should exist now*/ | |
412 | t1= utrans_open("TestA-TestB", UTRANS_FORWARD, NULL,0,NULL,&status); | |
413 | if(U_FAILURE(status) || t1 == NULL){ | |
414 | log_err("FAIL: 2nd TestA-TestB not registered\n"); | |
415 | return; | |
416 | } | |
417 | utrans_close(t1); | |
418 | ||
419 | /*unregister the instance*/ | |
420 | status=U_ZERO_ERROR; | |
421 | utrans_unregisterID(ID, -1); | |
422 | /* now Make sure it doesn't exist */ | |
423 | t1=utrans_openU(ID, -1, UTRANS_FORWARD,NULL,0,NULL, &status); | |
424 | if(U_SUCCESS(status) || t1 != NULL) { | |
425 | log_err("FAIL: 2nd TestA-TestB isn't unregistered\n"); | |
426 | return; | |
427 | } | |
428 | ||
b75a7d8f A |
429 | utrans_close(t1); |
430 | utrans_close(inverse1); | |
431 | } | |
432 | ||
433 | static void TestSimpleRules() { | |
434 | /* Test rules */ | |
435 | /* Example: rules 1. ab>x|y | |
436 | * 2. yc>z | |
437 | * | |
438 | * []|eabcd start - no match, copy e to tranlated buffer | |
439 | * [e]|abcd match rule 1 - copy output & adjust cursor | |
440 | * [ex|y]cd match rule 2 - copy output & adjust cursor | |
441 | * [exz]|d no match, copy d to transliterated buffer | |
442 | * [exzd]| done | |
443 | */ | |
444 | _expectRules("ab>x|y;" | |
445 | "yc>z", | |
446 | "eabcd", "exzd"); | |
447 | ||
448 | /* Another set of rules: | |
449 | * 1. ab>x|yzacw | |
450 | * 2. za>q | |
451 | * 3. qc>r | |
452 | * 4. cw>n | |
453 | * | |
454 | * []|ab Rule 1 | |
455 | * [x|yzacw] No match | |
456 | * [xy|zacw] Rule 2 | |
457 | * [xyq|cw] Rule 4 | |
458 | * [xyqn]| Done | |
459 | */ | |
460 | _expectRules("ab>x|yzacw;" | |
461 | "za>q;" | |
462 | "qc>r;" | |
463 | "cw>n", | |
464 | "ab", "xyqn"); | |
465 | ||
466 | /* Test categories | |
467 | */ | |
468 | _expectRules("$dummy=" "\\uE100" ";" /* careful here with E100 */ | |
469 | "$vowel=[aeiouAEIOU];" | |
470 | "$lu=[:Lu:];" | |
471 | "$vowel } $lu > '!';" | |
472 | "$vowel > '&';" | |
473 | "'!' { $lu > '^';" | |
474 | "$lu > '*';" | |
475 | "a > ERROR", | |
476 | "abcdefgABCDEFGU", "&bcd&fg!^**!^*&"); | |
477 | } | |
478 | ||
479 | static void TestFilter() { | |
480 | UErrorCode status = U_ZERO_ERROR; | |
481 | UChar filt[128]; | |
482 | UChar buf[128]; | |
483 | UChar exp[128]; | |
374ca955 | 484 | char *cbuf; |
b75a7d8f A |
485 | int32_t limit; |
486 | const char* DATA[] = { | |
487 | "[^c]", /* Filter out 'c' */ | |
488 | "abcde", | |
489 | "\\u0061\\u0062c\\u0064\\u0065", | |
490 | ||
491 | "", /* No filter */ | |
492 | "abcde", | |
493 | "\\u0061\\u0062\\u0063\\u0064\\u0065" | |
494 | }; | |
495 | int32_t DATA_length = sizeof(DATA) / sizeof(DATA[0]); | |
496 | int32_t i; | |
497 | ||
498 | UTransliterator* hex = utrans_open("Any-Hex", UTRANS_FORWARD, NULL,0,NULL,&status); | |
499 | ||
500 | if (hex == 0 || U_FAILURE(status)) { | |
501 | log_err("FAIL: utrans_open(Unicode-Hex) failed, error=%s\n", | |
502 | u_errorName(status)); | |
503 | goto exit; | |
504 | } | |
505 | ||
506 | for (i=0; i<DATA_length; i+=3) { | |
507 | /*u_uastrcpy(filt, DATA[i]);*/ | |
374ca955 | 508 | u_charsToUChars(DATA[i], filt, (int32_t)strlen(DATA[i])+1); |
b75a7d8f A |
509 | utrans_setFilter(hex, filt, -1, &status); |
510 | ||
511 | if (U_FAILURE(status)) { | |
512 | log_err("FAIL: utrans_setFilter() failed, error=%s\n", | |
513 | u_errorName(status)); | |
514 | goto exit; | |
515 | } | |
516 | ||
517 | /*u_uastrcpy(buf, DATA[i+1]);*/ | |
374ca955 | 518 | u_charsToUChars(DATA[i+1], buf, (int32_t)strlen(DATA[i+1])+1); |
b75a7d8f A |
519 | limit = 5; |
520 | utrans_transUChars(hex, buf, NULL, 128, 0, &limit, &status); | |
521 | ||
522 | if (U_FAILURE(status)) { | |
523 | log_err("FAIL: utrans_transUChars() failed, error=%s\n", | |
524 | u_errorName(status)); | |
525 | goto exit; | |
526 | } | |
527 | ||
374ca955 A |
528 | cbuf=aescstrdup(buf, -1); |
529 | u_charsToUChars(DATA[i+2], exp, (int32_t)strlen(DATA[i+2])+1); | |
b75a7d8f A |
530 | if (0 == u_strcmp(buf, exp)) { |
531 | log_verbose("Ok: %s | %s -> %s\n", DATA[i+1], DATA[i], cbuf); | |
532 | } else { | |
533 | log_err("FAIL: %s | %s -> %s, expected %s\n", DATA[i+1], DATA[i], cbuf, DATA[i+2]); | |
534 | } | |
535 | } | |
536 | ||
537 | exit: | |
538 | utrans_close(hex); | |
539 | } | |
540 | ||
541 | /** | |
542 | * Test the UReplaceableCallback extractBetween support. We use a | |
543 | * transliterator known to rely on this call. | |
544 | */ | |
545 | static void TestExtractBetween() { | |
546 | ||
547 | UTransliterator *trans; | |
548 | UErrorCode status = U_ZERO_ERROR; | |
549 | UParseError parseErr; | |
550 | ||
551 | trans = utrans_open("Lower", UTRANS_FORWARD, NULL, -1, | |
552 | &parseErr, &status); | |
553 | ||
554 | if (U_FAILURE(status)) { | |
555 | log_err("FAIL: utrans_open(Lower) failed, error=%s\n", | |
556 | u_errorName(status)); | |
557 | } else { | |
558 | _expect(trans, "ABC", "abc"); | |
559 | ||
560 | utrans_close(trans); | |
561 | } | |
562 | } | |
563 | ||
564 | static void _expectRules(const char* crules, | |
565 | const char* cfrom, | |
566 | const char* cto) { | |
567 | /* u_uastrcpy has no capacity param for the buffer -- so just | |
568 | * make all buffers way too big */ | |
569 | enum { CAP = 256 }; | |
570 | UChar rules[CAP]; | |
571 | UTransliterator *trans; | |
572 | UErrorCode status = U_ZERO_ERROR; | |
573 | UParseError parseErr; | |
574 | ||
575 | u_uastrcpy(rules, crules); | |
576 | ||
577 | trans = utrans_open(crules /*use rules as ID*/, UTRANS_FORWARD, rules, -1, | |
578 | &parseErr, &status); | |
579 | if (U_FAILURE(status)) { | |
580 | utrans_close(trans); | |
581 | log_err("FAIL: utrans_openRules(%s) failed, error=%s\n", | |
582 | crules, u_errorName(status)); | |
583 | return; | |
584 | } | |
585 | ||
586 | _expect(trans, cfrom, cto); | |
587 | ||
588 | utrans_close(trans); | |
589 | } | |
590 | ||
591 | static void _expect(const UTransliterator* trans, | |
592 | const char* cfrom, | |
593 | const char* cto) { | |
594 | /* u_uastrcpy has no capacity param for the buffer -- so just | |
595 | * make all buffers way too big */ | |
596 | enum { CAP = 256 }; | |
597 | UChar from[CAP]; | |
598 | UChar to[CAP]; | |
599 | UChar buf[CAP]; | |
374ca955 A |
600 | const UChar *ID; |
601 | int32_t IDLength; | |
602 | const char *id; | |
603 | ||
b75a7d8f A |
604 | UErrorCode status = U_ZERO_ERROR; |
605 | int32_t limit; | |
606 | UTransPosition pos; | |
607 | XReplaceable xrep; | |
608 | UReplaceableCallbacks xrepVtable; | |
609 | ||
610 | u_uastrcpy(from, cfrom); | |
611 | u_uastrcpy(to, cto); | |
612 | ||
374ca955 A |
613 | ID = utrans_getUnicodeID(trans, &IDLength); |
614 | id = aescstrdup(ID, IDLength); | |
b75a7d8f A |
615 | |
616 | /* utrans_transUChars() */ | |
617 | u_strcpy(buf, from); | |
618 | limit = u_strlen(buf); | |
619 | utrans_transUChars(trans, buf, NULL, CAP, 0, &limit, &status); | |
620 | if (U_FAILURE(status)) { | |
621 | log_err("FAIL: utrans_transUChars() failed, error=%s\n", | |
622 | u_errorName(status)); | |
623 | return; | |
624 | } | |
625 | ||
626 | if (0 == u_strcmp(buf, to)) { | |
627 | log_verbose("Ok: utrans_transUChars(%s) x %s -> %s\n", | |
628 | id, cfrom, cto); | |
629 | } else { | |
630 | char actual[CAP]; | |
631 | u_austrcpy(actual, buf); | |
632 | log_err("FAIL: utrans_transUChars(%s) x %s -> %s, expected %s\n", | |
633 | id, cfrom, actual, cto); | |
634 | } | |
635 | ||
636 | /* utrans_transIncrementalUChars() */ | |
637 | u_strcpy(buf, from); | |
638 | pos.start = pos.contextStart = 0; | |
639 | pos.limit = pos.contextLimit = u_strlen(buf); | |
640 | utrans_transIncrementalUChars(trans, buf, NULL, CAP, &pos, &status); | |
641 | utrans_transUChars(trans, buf, NULL, CAP, pos.start, &pos.limit, &status); | |
642 | if (U_FAILURE(status)) { | |
643 | log_err("FAIL: utrans_transIncrementalUChars() failed, error=%s\n", | |
644 | u_errorName(status)); | |
645 | return; | |
646 | } | |
647 | ||
648 | if (0 == u_strcmp(buf, to)) { | |
649 | log_verbose("Ok: utrans_transIncrementalUChars(%s) x %s -> %s\n", | |
650 | id, cfrom, cto); | |
651 | } else { | |
652 | char actual[CAP]; | |
653 | u_austrcpy(actual, buf); | |
654 | log_err("FAIL: utrans_transIncrementalUChars(%s) x %s -> %s, expected %s\n", | |
655 | id, cfrom, actual, cto); | |
656 | } | |
657 | ||
658 | /* utrans_trans() */ | |
659 | InitXReplaceableCallbacks(&xrepVtable); | |
660 | InitXReplaceable(&xrep, cfrom); | |
661 | limit = u_strlen(from); | |
662 | utrans_trans(trans, (UReplaceable*)&xrep, &xrepVtable, 0, &limit, &status); | |
663 | if (U_FAILURE(status)) { | |
664 | log_err("FAIL: utrans_trans() failed, error=%s\n", | |
665 | u_errorName(status)); | |
666 | FreeXReplaceable(&xrep); | |
667 | return; | |
668 | } | |
669 | ||
670 | if (0 == u_strcmp(xrep.text, to)) { | |
671 | log_verbose("Ok: utrans_trans(%s) x %s -> %s\n", | |
672 | id, cfrom, cto); | |
673 | } else { | |
674 | char actual[CAP]; | |
675 | u_austrcpy(actual, xrep.text); | |
676 | log_err("FAIL: utrans_trans(%s) x %s -> %s, expected %s\n", | |
677 | id, cfrom, actual, cto); | |
678 | } | |
679 | FreeXReplaceable(&xrep); | |
680 | ||
681 | /* utrans_transIncremental() */ | |
682 | InitXReplaceable(&xrep, cfrom); | |
683 | pos.start = pos.contextStart = 0; | |
684 | pos.limit = pos.contextLimit = u_strlen(from); | |
685 | utrans_transIncremental(trans, (UReplaceable*)&xrep, &xrepVtable, &pos, &status); | |
686 | utrans_trans(trans, (UReplaceable*)&xrep, &xrepVtable, pos.start, &pos.limit, &status); | |
687 | if (U_FAILURE(status)) { | |
688 | log_err("FAIL: utrans_transIncremental() failed, error=%s\n", | |
689 | u_errorName(status)); | |
690 | FreeXReplaceable(&xrep); | |
691 | return; | |
692 | } | |
693 | ||
694 | if (0 == u_strcmp(xrep.text, to)) { | |
695 | log_verbose("Ok: utrans_transIncremental(%s) x %s -> %s\n", | |
696 | id, cfrom, cto); | |
697 | } else { | |
698 | char actual[CAP]; | |
699 | u_austrcpy(actual, xrep.text); | |
700 | log_err("FAIL: utrans_transIncremental(%s) x %s -> %s, expected %s\n", | |
701 | id, cfrom, actual, cto); | |
702 | } | |
703 | FreeXReplaceable(&xrep); | |
704 | } | |
705 | ||
706 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |