]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/convtest.cpp
ICU-8.11.2.tar.gz
[apple/icu.git] / icuSources / test / intltest / convtest.cpp
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2003-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: convtest.cpp
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2003jul15
14 * created by: Markus W. Scherer
15 *
16 * Test file for data-driven conversion tests.
17 */
18
19 #include "unicode/utypes.h"
20
21 #if !UCONFIG_NO_LEGACY_CONVERSION
22 /*
23 * Note: Turning off all of convtest.cpp if !UCONFIG_NO_LEGACY_CONVERSION
24 * is slightly unnecessary - it removes tests for Unicode charsets
25 * like UTF-8 that should work.
26 * However, there is no easy way for the test to detect whether a test case
27 * is for a Unicode charset, so it would be difficult to only exclude those.
28 * Also, regular testing of ICU is done with all modules on, therefore
29 * not testing conversion for a custom configuration like this should be ok.
30 */
31
32 #include "unicode/ucnv.h"
33 #include "unicode/unistr.h"
34 #include "unicode/parsepos.h"
35 #include "unicode/uniset.h"
36 #include "unicode/ustring.h"
37 #include "unicode/ures.h"
38 #include "convtest.h"
39 #include "unicode/tstdtmod.h"
40 #include <string.h>
41 #include <stdlib.h>
42
43 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
44
45 enum {
46 // characters used in test data for callbacks
47 SUB_CB='?',
48 SKIP_CB='0',
49 STOP_CB='.',
50 ESC_CB='&'
51 };
52
53 ConversionTest::~ConversionTest() {}
54
55 void
56 ConversionTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
57 if (exec) logln("TestSuite ConversionTest: ");
58 switch (index) {
59 case 0: name="TestToUnicode"; if (exec) TestToUnicode(); break;
60 case 1: name="TestFromUnicode"; if (exec) TestFromUnicode(); break;
61 case 2: name="TestGetUnicodeSet"; if (exec) TestGetUnicodeSet(); break;
62 default: name=""; break; //needed to end loop
63 }
64 }
65
66 // test data interface ----------------------------------------------------- ***
67
68 void
69 ConversionTest::TestToUnicode() {
70 ConversionCase cc;
71 char charset[100], cbopt[4];
72 const char *option;
73 UnicodeString s, unicode;
74 int32_t offsetsLength;
75 UConverterToUCallback callback;
76
77 TestDataModule *dataModule;
78 TestData *testData;
79 const DataMap *testCase;
80 UErrorCode errorCode;
81 int32_t i;
82
83 errorCode=U_ZERO_ERROR;
84 dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
85 if(U_SUCCESS(errorCode)) {
86 testData=dataModule->createTestData("toUnicode", errorCode);
87 if(U_SUCCESS(errorCode)) {
88 for(i=0; testData->nextCase(testCase, errorCode); ++i) {
89 if(U_FAILURE(errorCode)) {
90 errln("error retrieving conversion/toUnicode test case %d - %s",
91 i, u_errorName(errorCode));
92 errorCode=U_ZERO_ERROR;
93 continue;
94 }
95
96 cc.caseNr=i;
97
98 s=testCase->getString("charset", errorCode);
99 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
100 cc.charset=charset;
101
102 cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
103 unicode=testCase->getString("unicode", errorCode);
104 cc.unicode=unicode.getBuffer();
105 cc.unicodeLength=unicode.length();
106
107 offsetsLength=0;
108 cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
109 if(offsetsLength==0) {
110 cc.offsets=NULL;
111 } else if(offsetsLength!=unicode.length()) {
112 errln("toUnicode[%d] unicode[%d] and offsets[%d] must have the same length",
113 i, unicode.length(), offsetsLength);
114 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
115 }
116
117 cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
118 cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
119
120 s=testCase->getString("errorCode", errorCode);
121 if(s==UNICODE_STRING("invalid", 7)) {
122 cc.outErrorCode=U_INVALID_CHAR_FOUND;
123 } else if(s==UNICODE_STRING("illegal", 7)) {
124 cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
125 } else if(s==UNICODE_STRING("truncated", 9)) {
126 cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
127 } else if(s==UNICODE_STRING("illesc", 6)) {
128 cc.outErrorCode=U_ILLEGAL_ESCAPE_SEQUENCE;
129 } else if(s==UNICODE_STRING("unsuppesc", 9)) {
130 cc.outErrorCode=U_UNSUPPORTED_ESCAPE_SEQUENCE;
131 } else {
132 cc.outErrorCode=U_ZERO_ERROR;
133 }
134
135 s=testCase->getString("callback", errorCode);
136 s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
137 cc.cbopt=cbopt;
138 switch(cbopt[0]) {
139 case SUB_CB:
140 callback=UCNV_TO_U_CALLBACK_SUBSTITUTE;
141 break;
142 case SKIP_CB:
143 callback=UCNV_TO_U_CALLBACK_SKIP;
144 break;
145 case STOP_CB:
146 callback=UCNV_TO_U_CALLBACK_STOP;
147 break;
148 case ESC_CB:
149 callback=UCNV_TO_U_CALLBACK_ESCAPE;
150 break;
151 default:
152 callback=NULL;
153 break;
154 }
155 option=callback==NULL ? cbopt : cbopt+1;
156 if(*option==0) {
157 option=NULL;
158 }
159
160 cc.invalidChars=testCase->getBinary(cc.invalidLength, "invalidChars", errorCode);
161
162 if(U_FAILURE(errorCode)) {
163 errln("error parsing conversion/toUnicode test case %d - %s",
164 i, u_errorName(errorCode));
165 errorCode=U_ZERO_ERROR;
166 } else {
167 logln("TestToUnicode[%d] %s", i, charset);
168 ToUnicodeCase(cc, callback, option);
169 }
170 }
171 delete testData;
172 }
173 delete dataModule;
174 }
175 else {
176 errln("Failed: could not load test conversion data");
177 }
178 }
179
180 void
181 ConversionTest::TestFromUnicode() {
182 ConversionCase cc;
183 char charset[100], cbopt[4];
184 const char *option;
185 UnicodeString s, unicode, invalidUChars;
186 int32_t offsetsLength, index;
187 UConverterFromUCallback callback;
188
189 TestDataModule *dataModule;
190 TestData *testData;
191 const DataMap *testCase;
192 const UChar *p;
193 UErrorCode errorCode;
194 int32_t i, length;
195
196 errorCode=U_ZERO_ERROR;
197 dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
198 if(U_SUCCESS(errorCode)) {
199 testData=dataModule->createTestData("fromUnicode", errorCode);
200 if(U_SUCCESS(errorCode)) {
201 for(i=0; testData->nextCase(testCase, errorCode); ++i) {
202 if(U_FAILURE(errorCode)) {
203 errln("error retrieving conversion/fromUnicode test case %d - %s",
204 i, u_errorName(errorCode));
205 errorCode=U_ZERO_ERROR;
206 continue;
207 }
208
209 cc.caseNr=i;
210
211 s=testCase->getString("charset", errorCode);
212 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
213 cc.charset=charset;
214
215 unicode=testCase->getString("unicode", errorCode);
216 cc.unicode=unicode.getBuffer();
217 cc.unicodeLength=unicode.length();
218 cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
219
220 offsetsLength=0;
221 cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
222 if(offsetsLength==0) {
223 cc.offsets=NULL;
224 } else if(offsetsLength!=cc.bytesLength) {
225 errln("fromUnicode[%d] bytes[%d] and offsets[%d] must have the same length",
226 i, cc.bytesLength, offsetsLength);
227 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
228 }
229
230 cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
231 cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
232
233 s=testCase->getString("errorCode", errorCode);
234 if(s==UNICODE_STRING("invalid", 7)) {
235 cc.outErrorCode=U_INVALID_CHAR_FOUND;
236 } else if(s==UNICODE_STRING("illegal", 7)) {
237 cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
238 } else if(s==UNICODE_STRING("truncated", 9)) {
239 cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
240 } else {
241 cc.outErrorCode=U_ZERO_ERROR;
242 }
243
244 s=testCase->getString("callback", errorCode);
245 cc.setSub=0; // default: no subchar
246
247 if((index=s.indexOf((UChar)0))>0) {
248 // read NUL-separated subchar first, if any
249 // copy the subchar from Latin-1 characters
250 // start after the NUL
251 p=s.getTerminatedBuffer();
252 length=index+1;
253 p+=length;
254 length=s.length()-length;
255 if(length<=0 || length>=(int32_t)sizeof(cc.subchar)) {
256 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
257 } else {
258 int32_t j;
259
260 for(j=0; j<length; ++j) {
261 cc.subchar[j]=(char)p[j];
262 }
263 // NUL-terminate the subchar
264 cc.subchar[j]=0;
265 cc.setSub=1;
266 }
267
268 // remove the NUL and subchar from s
269 s.truncate(index);
270 } else if((index=s.indexOf((UChar)0x3d))>0) /* '=' */ {
271 // read a substitution string, separated by an equal sign
272 p=s.getBuffer()+index+1;
273 length=s.length()-(index+1);
274 if(length<0 || length>=LENGTHOF(cc.subString)) {
275 errorCode=U_ILLEGAL_ARGUMENT_ERROR;
276 } else {
277 u_memcpy(cc.subString, p, length);
278 // NUL-terminate the subString
279 cc.subString[length]=0;
280 cc.setSub=-1;
281 }
282
283 // remove the equal sign and subString from s
284 s.truncate(index);
285 }
286
287 s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
288 cc.cbopt=cbopt;
289 switch(cbopt[0]) {
290 case SUB_CB:
291 callback=UCNV_FROM_U_CALLBACK_SUBSTITUTE;
292 break;
293 case SKIP_CB:
294 callback=UCNV_FROM_U_CALLBACK_SKIP;
295 break;
296 case STOP_CB:
297 callback=UCNV_FROM_U_CALLBACK_STOP;
298 break;
299 case ESC_CB:
300 callback=UCNV_FROM_U_CALLBACK_ESCAPE;
301 break;
302 default:
303 callback=NULL;
304 break;
305 }
306 option=callback==NULL ? cbopt : cbopt+1;
307 if(*option==0) {
308 option=NULL;
309 }
310
311 invalidUChars=testCase->getString("invalidUChars", errorCode);
312 cc.invalidUChars=invalidUChars.getBuffer();
313 cc.invalidLength=invalidUChars.length();
314
315 if(U_FAILURE(errorCode)) {
316 errln("error parsing conversion/fromUnicode test case %d - %s",
317 i, u_errorName(errorCode));
318 errorCode=U_ZERO_ERROR;
319 } else {
320 logln("TestFromUnicode[%d] %s", i, charset);
321 FromUnicodeCase(cc, callback, option);
322 }
323 }
324 delete testData;
325 }
326 delete dataModule;
327 }
328 else {
329 errln("Failed: could not load test conversion data");
330 }
331 }
332
333 static const UChar ellipsis[]={ 0x2e, 0x2e, 0x2e };
334
335 void
336 ConversionTest::TestGetUnicodeSet() {
337 char charset[100];
338 UnicodeString s, map, mapnot;
339 int32_t which;
340
341 ParsePosition pos;
342 UnicodeSet cnvSet, mapSet, mapnotSet, diffSet;
343 UnicodeSet *cnvSetPtr = &cnvSet;
344 UConverter *cnv;
345
346 TestDataModule *dataModule;
347 TestData *testData;
348 const DataMap *testCase;
349 UErrorCode errorCode;
350 int32_t i;
351
352 errorCode=U_ZERO_ERROR;
353 dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
354 if(U_SUCCESS(errorCode)) {
355 testData=dataModule->createTestData("getUnicodeSet", errorCode);
356 if(U_SUCCESS(errorCode)) {
357 for(i=0; testData->nextCase(testCase, errorCode); ++i) {
358 if(U_FAILURE(errorCode)) {
359 errln("error retrieving conversion/getUnicodeSet test case %d - %s",
360 i, u_errorName(errorCode));
361 errorCode=U_ZERO_ERROR;
362 continue;
363 }
364
365 s=testCase->getString("charset", errorCode);
366 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
367
368 map=testCase->getString("map", errorCode);
369 mapnot=testCase->getString("mapnot", errorCode);
370
371 which=testCase->getInt28("which", errorCode);
372
373 if(U_FAILURE(errorCode)) {
374 errln("error parsing conversion/getUnicodeSet test case %d - %s",
375 i, u_errorName(errorCode));
376 errorCode=U_ZERO_ERROR;
377 continue;
378 }
379
380 // test this test case
381 mapSet.clear();
382 mapnotSet.clear();
383
384 pos.setIndex(0);
385 mapSet.applyPattern(map, pos, 0, NULL, errorCode);
386 if(U_FAILURE(errorCode) || pos.getIndex()!=map.length()) {
387 errln("error creating the map set for conversion/getUnicodeSet test case %d - %s\n"
388 " error index %d index %d U+%04x",
389 i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), map.char32At(pos.getIndex()));
390 errorCode=U_ZERO_ERROR;
391 continue;
392 }
393
394 pos.setIndex(0);
395 mapnotSet.applyPattern(mapnot, pos, 0, NULL, errorCode);
396 if(U_FAILURE(errorCode) || pos.getIndex()!=mapnot.length()) {
397 errln("error creating the mapnot set for conversion/getUnicodeSet test case %d - %s\n"
398 " error index %d index %d U+%04x",
399 i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), mapnot.char32At(pos.getIndex()));
400 errorCode=U_ZERO_ERROR;
401 continue;
402 }
403
404 logln("TestGetUnicodeSet[%d] %s", i, charset);
405
406 cnv=cnv_open(charset, errorCode);
407 if(U_FAILURE(errorCode)) {
408 errln("error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
409 charset, i, u_errorName(errorCode));
410 errorCode=U_ZERO_ERROR;
411 continue;
412 }
413
414 ucnv_getUnicodeSet(cnv, (USet *)cnvSetPtr, (UConverterUnicodeSet)which, &errorCode);
415 ucnv_close(cnv);
416
417 if(U_FAILURE(errorCode)) {
418 errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
419 charset, i, u_errorName(errorCode));
420 errorCode=U_ZERO_ERROR;
421 continue;
422 }
423
424 // are there items that must be in cnvSet but are not?
425 (diffSet=mapSet).removeAll(cnvSet);
426 if(!diffSet.isEmpty()) {
427 diffSet.toPattern(s, TRUE);
428 if(s.length()>100) {
429 s.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis));
430 }
431 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - conversion/getUnicodeSet test case %d",
432 charset, i);
433 errln(s);
434 }
435
436 // are there items that must not be in cnvSet but are?
437 (diffSet=mapnotSet).retainAll(cnvSet);
438 if(!diffSet.isEmpty()) {
439 diffSet.toPattern(s, TRUE);
440 if(s.length()>100) {
441 s.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis));
442 }
443 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - conversion/getUnicodeSet test case %d",
444 charset, i);
445 errln(s);
446 }
447 }
448 delete testData;
449 }
450 delete dataModule;
451 }
452 else {
453 errln("Failed: could not load test conversion data");
454 }
455 }
456
457 // open testdata or ICU data converter ------------------------------------- ***
458
459 UConverter *
460 ConversionTest::cnv_open(const char *name, UErrorCode &errorCode) {
461 if(name!=NULL && *name=='*') {
462 /* loadTestData(): set the data directory */
463 return ucnv_openPackage(loadTestData(errorCode), name+1, &errorCode);
464 } else {
465 return ucnv_open(name, &errorCode);
466 }
467 }
468
469 // output helpers ---------------------------------------------------------- ***
470
471 static inline char
472 hexDigit(uint8_t digit) {
473 return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit);
474 }
475
476 static char *
477 printBytes(const uint8_t *bytes, int32_t length, char *out) {
478 uint8_t b;
479
480 if(length>0) {
481 b=*bytes++;
482 --length;
483 *out++=hexDigit((uint8_t)(b>>4));
484 *out++=hexDigit((uint8_t)(b&0xf));
485 }
486
487 while(length>0) {
488 b=*bytes++;
489 --length;
490 *out++=' ';
491 *out++=hexDigit((uint8_t)(b>>4));
492 *out++=hexDigit((uint8_t)(b&0xf));
493 }
494 *out++=0;
495 return out;
496 }
497
498 static char *
499 printUnicode(const UChar *unicode, int32_t length, char *out) {
500 UChar32 c;
501 int32_t i;
502
503 for(i=0; i<length;) {
504 if(i>0) {
505 *out++=' ';
506 }
507 U16_NEXT(unicode, i, length, c);
508 // write 4..6 digits
509 if(c>=0x100000) {
510 *out++='1';
511 }
512 if(c>=0x10000) {
513 *out++=hexDigit((uint8_t)((c>>16)&0xf));
514 }
515 *out++=hexDigit((uint8_t)((c>>12)&0xf));
516 *out++=hexDigit((uint8_t)((c>>8)&0xf));
517 *out++=hexDigit((uint8_t)((c>>4)&0xf));
518 *out++=hexDigit((uint8_t)(c&0xf));
519 }
520 *out++=0;
521 return out;
522 }
523
524 static char *
525 printOffsets(const int32_t *offsets, int32_t length, char *out) {
526 int32_t i, o, d;
527
528 if(offsets==NULL) {
529 length=0;
530 }
531
532 for(i=0; i<length; ++i) {
533 if(i>0) {
534 *out++=' ';
535 }
536 o=offsets[i];
537
538 // print all offsets with 2 characters each (-x, -9..99, xx)
539 if(o<-9) {
540 *out++='-';
541 *out++='x';
542 } else if(o<0) {
543 *out++='-';
544 *out++=(char)('0'-o);
545 } else if(o<=99) {
546 *out++=(d=o/10)==0 ? ' ' : (char)('0'+d);
547 *out++=(char)('0'+o%10);
548 } else /* o>99 */ {
549 *out++='x';
550 *out++='x';
551 }
552 }
553 *out++=0;
554 return out;
555 }
556
557 // toUnicode test worker functions ----------------------------------------- ***
558
559 static int32_t
560 stepToUnicode(ConversionCase &cc, UConverter *cnv,
561 UChar *result, int32_t resultCapacity,
562 int32_t *resultOffsets, /* also resultCapacity */
563 int32_t step,
564 UErrorCode *pErrorCode) {
565 const char *source, *sourceLimit, *bytesLimit;
566 UChar *target, *targetLimit, *resultLimit;
567 UBool flush;
568
569 source=(const char *)cc.bytes;
570 target=result;
571 bytesLimit=source+cc.bytesLength;
572 resultLimit=result+resultCapacity;
573
574 if(step>=0) {
575 // call ucnv_toUnicode() with in/out buffers no larger than (step) at a time
576 // move only one buffer (in vs. out) at a time to be extra mean
577 // step==0 performs bulk conversion and generates offsets
578
579 // initialize the partial limits for the loop
580 if(step==0) {
581 // use the entire buffers
582 sourceLimit=bytesLimit;
583 targetLimit=resultLimit;
584 flush=cc.finalFlush;
585 } else {
586 // start with empty partial buffers
587 sourceLimit=source;
588 targetLimit=target;
589 flush=FALSE;
590
591 // output offsets only for bulk conversion
592 resultOffsets=NULL;
593 }
594
595 for(;;) {
596 // resetting the opposite conversion direction must not affect this one
597 ucnv_resetFromUnicode(cnv);
598
599 // convert
600 ucnv_toUnicode(cnv,
601 &target, targetLimit,
602 &source, sourceLimit,
603 resultOffsets,
604 flush, pErrorCode);
605
606 // check pointers and errors
607 if(source>sourceLimit || target>targetLimit) {
608 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
609 break;
610 } else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
611 if(target!=targetLimit) {
612 // buffer overflow must only be set when the target is filled
613 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
614 break;
615 } else if(targetLimit==resultLimit) {
616 // not just a partial overflow
617 break;
618 }
619
620 // the partial target is filled, set a new limit, reset the error and continue
621 targetLimit=(resultLimit-target)>=step ? target+step : resultLimit;
622 *pErrorCode=U_ZERO_ERROR;
623 } else if(U_FAILURE(*pErrorCode)) {
624 // some other error occurred, done
625 break;
626 } else {
627 if(source!=sourceLimit) {
628 // when no error occurs, then the input must be consumed
629 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
630 break;
631 }
632
633 if(sourceLimit==bytesLimit) {
634 // we are done
635 break;
636 }
637
638 // the partial conversion succeeded, set a new limit and continue
639 sourceLimit=(bytesLimit-source)>=step ? source+step : bytesLimit;
640 flush=(UBool)(cc.finalFlush && sourceLimit==bytesLimit);
641 }
642 }
643 } else /* step<0 */ {
644 /*
645 * step==-1: call only ucnv_getNextUChar()
646 * otherwise alternate between ucnv_toUnicode() and ucnv_getNextUChar()
647 * if step==-2 or -3, then give ucnv_toUnicode() the whole remaining input,
648 * else give it at most (-step-2)/2 bytes
649 */
650 UChar32 c;
651
652 // end the loop by getting an index out of bounds error
653 for(;;) {
654 // resetting the opposite conversion direction must not affect this one
655 ucnv_resetFromUnicode(cnv);
656
657 // convert
658 if((step&1)!=0 /* odd: -1, -3, -5, ... */) {
659 sourceLimit=source; // use sourceLimit not as a real limit
660 // but to remember the pre-getNextUChar source pointer
661 c=ucnv_getNextUChar(cnv, &source, bytesLimit, pErrorCode);
662
663 // check pointers and errors
664 if(*pErrorCode==U_INDEX_OUTOFBOUNDS_ERROR) {
665 if(source!=bytesLimit) {
666 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
667 } else {
668 *pErrorCode=U_ZERO_ERROR;
669 }
670 break;
671 } else if(U_FAILURE(*pErrorCode)) {
672 break;
673 }
674 // source may not move if c is from previous overflow
675
676 if(target==resultLimit) {
677 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
678 break;
679 }
680 if(c<=0xffff) {
681 *target++=(UChar)c;
682 } else {
683 *target++=U16_LEAD(c);
684 if(target==resultLimit) {
685 *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
686 break;
687 }
688 *target++=U16_TRAIL(c);
689 }
690
691 // alternate between -n-1 and -n but leave -1 alone
692 if(step<-1) {
693 ++step;
694 }
695 } else /* step is even */ {
696 // allow only one UChar output
697 targetLimit=target<resultLimit ? target+1 : resultLimit;
698
699 // as with ucnv_getNextUChar(), we always flush (if we go to bytesLimit)
700 // and never output offsets
701 if(step==-2) {
702 sourceLimit=bytesLimit;
703 } else {
704 sourceLimit=source+(-step-2)/2;
705 if(sourceLimit>bytesLimit) {
706 sourceLimit=bytesLimit;
707 }
708 }
709
710 ucnv_toUnicode(cnv,
711 &target, targetLimit,
712 &source, sourceLimit,
713 NULL, (UBool)(sourceLimit==bytesLimit), pErrorCode);
714
715 // check pointers and errors
716 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
717 if(target!=targetLimit) {
718 // buffer overflow must only be set when the target is filled
719 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
720 break;
721 } else if(targetLimit==resultLimit) {
722 // not just a partial overflow
723 break;
724 }
725
726 // the partial target is filled, set a new limit and continue
727 *pErrorCode=U_ZERO_ERROR;
728 } else if(U_FAILURE(*pErrorCode)) {
729 // some other error occurred, done
730 break;
731 } else {
732 if(source!=sourceLimit) {
733 // when no error occurs, then the input must be consumed
734 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
735 break;
736 }
737
738 // we are done (flush==TRUE) but we continue, to get the index out of bounds error above
739 }
740
741 --step;
742 }
743 }
744 }
745
746 return (int32_t)(target-result);
747 }
748
749 UBool
750 ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback, const char *option) {
751 UConverter *cnv;
752 UErrorCode errorCode;
753
754 // open the converter
755 errorCode=U_ZERO_ERROR;
756 cnv=cnv_open(cc.charset, errorCode);
757 if(U_FAILURE(errorCode)) {
758 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
759 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
760 return FALSE;
761 }
762
763 // set the callback
764 if(callback!=NULL) {
765 ucnv_setToUCallBack(cnv, callback, option, NULL, NULL, &errorCode);
766 if(U_FAILURE(errorCode)) {
767 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setToUCallBack() failed - %s",
768 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
769 ucnv_close(cnv);
770 return FALSE;
771 }
772 }
773
774 int32_t resultOffsets[200];
775 UChar result[200];
776 int32_t resultLength;
777 UBool ok;
778
779 static const struct {
780 int32_t step;
781 const char *name;
782 } steps[]={
783 { 0, "bulk" }, // must be first for offsets to be checked
784 { 1, "step=1" },
785 { 3, "step=3" },
786 { 7, "step=7" },
787 { -1, "getNext" },
788 { -2, "toU(bulk)+getNext" },
789 { -3, "getNext+toU(bulk)" },
790 { -4, "toU(1)+getNext" },
791 { -5, "getNext+toU(1)" },
792 { -12, "toU(5)+getNext" },
793 { -13, "getNext+toU(5)" },
794 };
795 int32_t i, step;
796
797 ok=TRUE;
798 for(i=0; i<LENGTHOF(steps) && ok; ++i) {
799 step=steps[i].step;
800 if(step<0 && !cc.finalFlush) {
801 // skip ucnv_getNextUChar() if !finalFlush because
802 // ucnv_getNextUChar() always implies flush
803 continue;
804 }
805 if(step!=0) {
806 // bulk test is first, then offsets are not checked any more
807 cc.offsets=NULL;
808 }
809 errorCode=U_ZERO_ERROR;
810 resultLength=stepToUnicode(cc, cnv,
811 result, LENGTHOF(result),
812 step==0 ? resultOffsets : NULL,
813 step, &errorCode);
814 ok=checkToUnicode(
815 cc, cnv, steps[i].name,
816 result, resultLength,
817 cc.offsets!=NULL ? resultOffsets : NULL,
818 errorCode);
819 if(U_FAILURE(errorCode) || !cc.finalFlush) {
820 // reset if an error occurred or we did not flush
821 // otherwise do nothing to make sure that flushing resets
822 ucnv_resetToUnicode(cnv);
823 }
824 }
825
826 // not a real loop, just a convenience for breaking out of the block
827 while(ok && cc.finalFlush) {
828 // test ucnv_toUChars()
829 memset(result, 0, sizeof(result));
830
831 errorCode=U_ZERO_ERROR;
832 resultLength=ucnv_toUChars(cnv,
833 result, LENGTHOF(result),
834 (const char *)cc.bytes, cc.bytesLength,
835 &errorCode);
836 ok=checkToUnicode(
837 cc, cnv, "toUChars",
838 result, resultLength,
839 NULL,
840 errorCode);
841 if(!ok) {
842 break;
843 }
844
845 // test preflighting
846 // keep the correct result for simple checking
847 errorCode=U_ZERO_ERROR;
848 resultLength=ucnv_toUChars(cnv,
849 NULL, 0,
850 (const char *)cc.bytes, cc.bytesLength,
851 &errorCode);
852 if(errorCode==U_STRING_NOT_TERMINATED_WARNING || errorCode==U_BUFFER_OVERFLOW_ERROR) {
853 errorCode=U_ZERO_ERROR;
854 }
855 ok=checkToUnicode(
856 cc, cnv, "preflight toUChars",
857 result, resultLength,
858 NULL,
859 errorCode);
860 break;
861 }
862
863 ucnv_close(cnv);
864 return ok;
865 }
866
867 UBool
868 ConversionTest::checkToUnicode(ConversionCase &cc, UConverter *cnv, const char *name,
869 const UChar *result, int32_t resultLength,
870 const int32_t *resultOffsets,
871 UErrorCode resultErrorCode) {
872 char resultInvalidChars[8];
873 int8_t resultInvalidLength;
874 UErrorCode errorCode;
875
876 const char *msg;
877
878 // reset the message; NULL will mean "ok"
879 msg=NULL;
880
881 errorCode=U_ZERO_ERROR;
882 resultInvalidLength=sizeof(resultInvalidChars);
883 ucnv_getInvalidChars(cnv, resultInvalidChars, &resultInvalidLength, &errorCode);
884 if(U_FAILURE(errorCode)) {
885 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidChars() failed - %s",
886 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, u_errorName(errorCode));
887 return FALSE;
888 }
889
890 // check everything that might have gone wrong
891 if(cc.unicodeLength!=resultLength) {
892 msg="wrong result length";
893 } else if(0!=u_memcmp(cc.unicode, result, cc.unicodeLength)) {
894 msg="wrong result string";
895 } else if(cc.offsets!=NULL && 0!=memcmp(cc.offsets, resultOffsets, cc.unicodeLength*sizeof(*cc.offsets))) {
896 msg="wrong offsets";
897 } else if(cc.outErrorCode!=resultErrorCode) {
898 msg="wrong error code";
899 } else if(cc.invalidLength!=resultInvalidLength) {
900 msg="wrong length of last invalid input";
901 } else if(0!=memcmp(cc.invalidChars, resultInvalidChars, cc.invalidLength)) {
902 msg="wrong last invalid input";
903 }
904
905 if(msg==NULL) {
906 return TRUE;
907 } else {
908 char buffer[2000]; // one buffer for all strings
909 char *s, *bytesString, *unicodeString, *resultString,
910 *offsetsString, *resultOffsetsString,
911 *invalidCharsString, *resultInvalidCharsString;
912
913 bytesString=s=buffer;
914 s=printBytes(cc.bytes, cc.bytesLength, bytesString);
915 s=printUnicode(cc.unicode, cc.unicodeLength, unicodeString=s);
916 s=printUnicode(result, resultLength, resultString=s);
917 s=printOffsets(cc.offsets, cc.unicodeLength, offsetsString=s);
918 s=printOffsets(resultOffsets, resultLength, resultOffsetsString=s);
919 s=printBytes(cc.invalidChars, cc.invalidLength, invalidCharsString=s);
920 s=printBytes((uint8_t *)resultInvalidChars, resultInvalidLength, resultInvalidCharsString=s);
921
922 if((s-buffer)>(int32_t)sizeof(buffer)) {
923 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkToUnicode() test output buffer overflow writing %d chars\n",
924 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, (int)(s-buffer));
925 exit(1);
926 }
927
928 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
929 " bytes <%s>[%d]\n"
930 " expected <%s>[%d]\n"
931 " result <%s>[%d]\n"
932 " offsets <%s>\n"
933 " result offsets <%s>\n"
934 " error code expected %s got %s\n"
935 " invalidChars expected <%s> got <%s>\n",
936 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, msg,
937 bytesString, cc.bytesLength,
938 unicodeString, cc.unicodeLength,
939 resultString, resultLength,
940 offsetsString,
941 resultOffsetsString,
942 u_errorName(cc.outErrorCode), u_errorName(resultErrorCode),
943 invalidCharsString, resultInvalidCharsString);
944
945 return FALSE;
946 }
947 }
948
949 // fromUnicode test worker functions --------------------------------------- ***
950
951 static int32_t
952 stepFromUnicode(ConversionCase &cc, UConverter *cnv,
953 char *result, int32_t resultCapacity,
954 int32_t *resultOffsets, /* also resultCapacity */
955 int32_t step,
956 UErrorCode *pErrorCode) {
957 const UChar *source, *sourceLimit, *unicodeLimit;
958 char *target, *targetLimit, *resultLimit;
959 UBool flush;
960
961 source=cc.unicode;
962 target=result;
963 unicodeLimit=source+cc.unicodeLength;
964 resultLimit=result+resultCapacity;
965
966 // call ucnv_fromUnicode() with in/out buffers no larger than (step) at a time
967 // move only one buffer (in vs. out) at a time to be extra mean
968 // step==0 performs bulk conversion and generates offsets
969
970 // initialize the partial limits for the loop
971 if(step==0) {
972 // use the entire buffers
973 sourceLimit=unicodeLimit;
974 targetLimit=resultLimit;
975 flush=cc.finalFlush;
976 } else {
977 // start with empty partial buffers
978 sourceLimit=source;
979 targetLimit=target;
980 flush=FALSE;
981
982 // output offsets only for bulk conversion
983 resultOffsets=NULL;
984 }
985
986 for(;;) {
987 // resetting the opposite conversion direction must not affect this one
988 ucnv_resetToUnicode(cnv);
989
990 // convert
991 ucnv_fromUnicode(cnv,
992 &target, targetLimit,
993 &source, sourceLimit,
994 resultOffsets,
995 flush, pErrorCode);
996
997 // check pointers and errors
998 if(source>sourceLimit || target>targetLimit) {
999 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
1000 break;
1001 } else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
1002 if(target!=targetLimit) {
1003 // buffer overflow must only be set when the target is filled
1004 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
1005 break;
1006 } else if(targetLimit==resultLimit) {
1007 // not just a partial overflow
1008 break;
1009 }
1010
1011 // the partial target is filled, set a new limit, reset the error and continue
1012 targetLimit=(resultLimit-target)>=step ? target+step : resultLimit;
1013 *pErrorCode=U_ZERO_ERROR;
1014 } else if(U_FAILURE(*pErrorCode)) {
1015 // some other error occurred, done
1016 break;
1017 } else {
1018 if(source!=sourceLimit) {
1019 // when no error occurs, then the input must be consumed
1020 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
1021 break;
1022 }
1023
1024 if(sourceLimit==unicodeLimit) {
1025 // we are done
1026 break;
1027 }
1028
1029 // the partial conversion succeeded, set a new limit and continue
1030 sourceLimit=(unicodeLimit-source)>=step ? source+step : unicodeLimit;
1031 flush=(UBool)(cc.finalFlush && sourceLimit==unicodeLimit);
1032 }
1033 }
1034
1035 return (int32_t)(target-result);
1036 }
1037
1038 UBool
1039 ConversionTest::FromUnicodeCase(ConversionCase &cc, UConverterFromUCallback callback, const char *option) {
1040 UConverter *cnv;
1041 UErrorCode errorCode;
1042
1043 // open the converter
1044 errorCode=U_ZERO_ERROR;
1045 cnv=cnv_open(cc.charset, errorCode);
1046 if(U_FAILURE(errorCode)) {
1047 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
1048 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
1049 return FALSE;
1050 }
1051
1052 // set the callback
1053 if(callback!=NULL) {
1054 ucnv_setFromUCallBack(cnv, callback, option, NULL, NULL, &errorCode);
1055 if(U_FAILURE(errorCode)) {
1056 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setFromUCallBack() failed - %s",
1057 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
1058 ucnv_close(cnv);
1059 return FALSE;
1060 }
1061 }
1062
1063 // set the fallbacks flag
1064 // TODO change with Jitterbug 2401, then add a similar call for toUnicode too
1065 ucnv_setFallback(cnv, cc.fallbacks);
1066
1067 // set the subchar
1068 int32_t length;
1069
1070 if(cc.setSub>0) {
1071 length=(int32_t)strlen(cc.subchar);
1072 ucnv_setSubstChars(cnv, cc.subchar, (int8_t)length, &errorCode);
1073 if(U_FAILURE(errorCode)) {
1074 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstChars() failed - %s",
1075 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
1076 ucnv_close(cnv);
1077 return FALSE;
1078 }
1079 } else if(cc.setSub<0) {
1080 ucnv_setSubstString(cnv, cc.subString, -1, &errorCode);
1081 if(U_FAILURE(errorCode)) {
1082 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstString() failed - %s",
1083 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
1084 ucnv_close(cnv);
1085 return FALSE;
1086 }
1087 }
1088
1089 int32_t resultOffsets[200];
1090 char result[200];
1091 int32_t resultLength;
1092 UBool ok;
1093
1094 static const struct {
1095 int32_t step;
1096 const char *name;
1097 } steps[]={
1098 { 0, "bulk" }, // must be first for offsets to be checked
1099 { 1, "step=1" },
1100 { 3, "step=3" },
1101 { 7, "step=7" }
1102 };
1103 int32_t i, step;
1104
1105 ok=TRUE;
1106 for(i=0; i<LENGTHOF(steps) && ok; ++i) {
1107 step=steps[i].step;
1108 if(step!=0) {
1109 // bulk test is first, then offsets are not checked any more
1110 cc.offsets=NULL;
1111 }
1112 errorCode=U_ZERO_ERROR;
1113 resultLength=stepFromUnicode(cc, cnv,
1114 result, LENGTHOF(result),
1115 step==0 ? resultOffsets : NULL,
1116 step, &errorCode);
1117 ok=checkFromUnicode(
1118 cc, cnv, steps[i].name,
1119 (uint8_t *)result, resultLength,
1120 cc.offsets!=NULL ? resultOffsets : NULL,
1121 errorCode);
1122 if(U_FAILURE(errorCode) || !cc.finalFlush) {
1123 // reset if an error occurred or we did not flush
1124 // otherwise do nothing to make sure that flushing resets
1125 ucnv_resetFromUnicode(cnv);
1126 }
1127 }
1128
1129 // not a real loop, just a convenience for breaking out of the block
1130 while(ok && cc.finalFlush) {
1131 // test ucnv_fromUChars()
1132 memset(result, 0, sizeof(result));
1133
1134 errorCode=U_ZERO_ERROR;
1135 resultLength=ucnv_fromUChars(cnv,
1136 result, LENGTHOF(result),
1137 cc.unicode, cc.unicodeLength,
1138 &errorCode);
1139 ok=checkFromUnicode(
1140 cc, cnv, "fromUChars",
1141 (uint8_t *)result, resultLength,
1142 NULL,
1143 errorCode);
1144 if(!ok) {
1145 break;
1146 }
1147
1148 // test preflighting
1149 // keep the correct result for simple checking
1150 errorCode=U_ZERO_ERROR;
1151 resultLength=ucnv_fromUChars(cnv,
1152 NULL, 0,
1153 cc.unicode, cc.unicodeLength,
1154 &errorCode);
1155 if(errorCode==U_STRING_NOT_TERMINATED_WARNING || errorCode==U_BUFFER_OVERFLOW_ERROR) {
1156 errorCode=U_ZERO_ERROR;
1157 }
1158 ok=checkFromUnicode(
1159 cc, cnv, "preflight fromUChars",
1160 (uint8_t *)result, resultLength,
1161 NULL,
1162 errorCode);
1163 break;
1164 }
1165
1166 ucnv_close(cnv);
1167 return ok;
1168 }
1169
1170 UBool
1171 ConversionTest::checkFromUnicode(ConversionCase &cc, UConverter *cnv, const char *name,
1172 const uint8_t *result, int32_t resultLength,
1173 const int32_t *resultOffsets,
1174 UErrorCode resultErrorCode) {
1175 UChar resultInvalidUChars[8];
1176 int8_t resultInvalidLength;
1177 UErrorCode errorCode;
1178
1179 const char *msg;
1180
1181 // reset the message; NULL will mean "ok"
1182 msg=NULL;
1183
1184 errorCode=U_ZERO_ERROR;
1185 resultInvalidLength=LENGTHOF(resultInvalidUChars);
1186 ucnv_getInvalidUChars(cnv, resultInvalidUChars, &resultInvalidLength, &errorCode);
1187 if(U_FAILURE(errorCode)) {
1188 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUChars() failed - %s",
1189 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, u_errorName(errorCode));
1190 return FALSE;
1191 }
1192
1193 // check everything that might have gone wrong
1194 if(cc.bytesLength!=resultLength) {
1195 msg="wrong result length";
1196 } else if(0!=memcmp(cc.bytes, result, cc.bytesLength)) {
1197 msg="wrong result string";
1198 } else if(cc.offsets!=NULL && 0!=memcmp(cc.offsets, resultOffsets, cc.bytesLength*sizeof(*cc.offsets))) {
1199 msg="wrong offsets";
1200 } else if(cc.outErrorCode!=resultErrorCode) {
1201 msg="wrong error code";
1202 } else if(cc.invalidLength!=resultInvalidLength) {
1203 msg="wrong length of last invalid input";
1204 } else if(0!=u_memcmp(cc.invalidUChars, resultInvalidUChars, cc.invalidLength)) {
1205 msg="wrong last invalid input";
1206 }
1207
1208 if(msg==NULL) {
1209 return TRUE;
1210 } else {
1211 char buffer[2000]; // one buffer for all strings
1212 char *s, *unicodeString, *bytesString, *resultString,
1213 *offsetsString, *resultOffsetsString,
1214 *invalidCharsString, *resultInvalidUCharsString;
1215
1216 unicodeString=s=buffer;
1217 s=printUnicode(cc.unicode, cc.unicodeLength, unicodeString);
1218 s=printBytes(cc.bytes, cc.bytesLength, bytesString=s);
1219 s=printBytes(result, resultLength, resultString=s);
1220 s=printOffsets(cc.offsets, cc.bytesLength, offsetsString=s);
1221 s=printOffsets(resultOffsets, resultLength, resultOffsetsString=s);
1222 s=printUnicode(cc.invalidUChars, cc.invalidLength, invalidCharsString=s);
1223 s=printUnicode(resultInvalidUChars, resultInvalidLength, resultInvalidUCharsString=s);
1224
1225 if((s-buffer)>(int32_t)sizeof(buffer)) {
1226 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkFromUnicode() test output buffer overflow writing %d chars\n",
1227 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, (int)(s-buffer));
1228 exit(1);
1229 }
1230
1231 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
1232 " unicode <%s>[%d]\n"
1233 " expected <%s>[%d]\n"
1234 " result <%s>[%d]\n"
1235 " offsets <%s>\n"
1236 " result offsets <%s>\n"
1237 " error code expected %s got %s\n"
1238 " invalidChars expected <%s> got <%s>\n",
1239 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, msg,
1240 unicodeString, cc.unicodeLength,
1241 bytesString, cc.bytesLength,
1242 resultString, resultLength,
1243 offsetsString,
1244 resultOffsetsString,
1245 u_errorName(cc.outErrorCode), u_errorName(resultErrorCode),
1246 invalidCharsString, resultInvalidUCharsString);
1247
1248 return FALSE;
1249 }
1250 }
1251
1252 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */