2 *******************************************************************************
4 * Copyright (C) 2003-2008, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: convtest.cpp
10 * tab size: 8 (not used)
13 * created on: 2003jul15
14 * created by: Markus W. Scherer
16 * Test file for data-driven conversion tests.
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_LEGACY_CONVERSION
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.
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"
39 #include "unicode/tstdtmod.h"
43 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
46 // characters used in test data for callbacks
53 ConversionTest::ConversionTest() {
54 UErrorCode errorCode
=U_ZERO_ERROR
;
55 utf8Cnv
=ucnv_open("UTF-8", &errorCode
);
56 ucnv_setToUCallBack(utf8Cnv
, UCNV_TO_U_CALLBACK_STOP
, NULL
, NULL
, NULL
, &errorCode
);
57 if(U_FAILURE(errorCode
)) {
58 errln("unable to open UTF-8 converter");
62 ConversionTest::~ConversionTest() {
67 ConversionTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
68 if (exec
) logln("TestSuite ConversionTest: ");
70 case 0: name
="TestToUnicode"; if (exec
) TestToUnicode(); break;
71 case 1: name
="TestFromUnicode"; if (exec
) TestFromUnicode(); break;
72 case 2: name
="TestGetUnicodeSet"; if (exec
) TestGetUnicodeSet(); break;
73 case 3: name
="TestGetUnicodeSet2"; if (exec
) TestGetUnicodeSet2(); break;
74 default: name
=""; break; //needed to end loop
78 // test data interface ----------------------------------------------------- ***
81 ConversionTest::TestToUnicode() {
83 char charset
[100], cbopt
[4];
85 UnicodeString s
, unicode
;
86 int32_t offsetsLength
;
87 UConverterToUCallback callback
;
89 TestDataModule
*dataModule
;
91 const DataMap
*testCase
;
95 errorCode
=U_ZERO_ERROR
;
96 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
97 if(U_SUCCESS(errorCode
)) {
98 testData
=dataModule
->createTestData("toUnicode", errorCode
);
99 if(U_SUCCESS(errorCode
)) {
100 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
101 if(U_FAILURE(errorCode
)) {
102 errln("error retrieving conversion/toUnicode test case %d - %s",
103 i
, u_errorName(errorCode
));
104 errorCode
=U_ZERO_ERROR
;
110 s
=testCase
->getString("charset", errorCode
);
111 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
114 cc
.bytes
=testCase
->getBinary(cc
.bytesLength
, "bytes", errorCode
);
115 unicode
=testCase
->getString("unicode", errorCode
);
116 cc
.unicode
=unicode
.getBuffer();
117 cc
.unicodeLength
=unicode
.length();
120 cc
.offsets
=testCase
->getIntVector(offsetsLength
, "offsets", errorCode
);
121 if(offsetsLength
==0) {
123 } else if(offsetsLength
!=unicode
.length()) {
124 errln("toUnicode[%d] unicode[%d] and offsets[%d] must have the same length",
125 i
, unicode
.length(), offsetsLength
);
126 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
129 cc
.finalFlush
= 0!=testCase
->getInt28("flush", errorCode
);
130 cc
.fallbacks
= 0!=testCase
->getInt28("fallbacks", errorCode
);
132 s
=testCase
->getString("errorCode", errorCode
);
133 if(s
==UNICODE_STRING("invalid", 7)) {
134 cc
.outErrorCode
=U_INVALID_CHAR_FOUND
;
135 } else if(s
==UNICODE_STRING("illegal", 7)) {
136 cc
.outErrorCode
=U_ILLEGAL_CHAR_FOUND
;
137 } else if(s
==UNICODE_STRING("truncated", 9)) {
138 cc
.outErrorCode
=U_TRUNCATED_CHAR_FOUND
;
139 } else if(s
==UNICODE_STRING("illesc", 6)) {
140 cc
.outErrorCode
=U_ILLEGAL_ESCAPE_SEQUENCE
;
141 } else if(s
==UNICODE_STRING("unsuppesc", 9)) {
142 cc
.outErrorCode
=U_UNSUPPORTED_ESCAPE_SEQUENCE
;
144 cc
.outErrorCode
=U_ZERO_ERROR
;
147 s
=testCase
->getString("callback", errorCode
);
148 s
.extract(0, 0x7fffffff, cbopt
, sizeof(cbopt
), "");
152 callback
=UCNV_TO_U_CALLBACK_SUBSTITUTE
;
155 callback
=UCNV_TO_U_CALLBACK_SKIP
;
158 callback
=UCNV_TO_U_CALLBACK_STOP
;
161 callback
=UCNV_TO_U_CALLBACK_ESCAPE
;
167 option
=callback
==NULL
? cbopt
: cbopt
+1;
172 cc
.invalidChars
=testCase
->getBinary(cc
.invalidLength
, "invalidChars", errorCode
);
174 if(U_FAILURE(errorCode
)) {
175 errln("error parsing conversion/toUnicode test case %d - %s",
176 i
, u_errorName(errorCode
));
177 errorCode
=U_ZERO_ERROR
;
179 logln("TestToUnicode[%d] %s", i
, charset
);
180 ToUnicodeCase(cc
, callback
, option
);
188 dataerrln("[DATA] Could not load test conversion data");
193 ConversionTest::TestFromUnicode() {
195 char charset
[100], cbopt
[4];
197 UnicodeString s
, unicode
, invalidUChars
;
198 int32_t offsetsLength
, index
;
199 UConverterFromUCallback callback
;
201 TestDataModule
*dataModule
;
203 const DataMap
*testCase
;
205 UErrorCode errorCode
;
208 errorCode
=U_ZERO_ERROR
;
209 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
210 if(U_SUCCESS(errorCode
)) {
211 testData
=dataModule
->createTestData("fromUnicode", errorCode
);
212 if(U_SUCCESS(errorCode
)) {
213 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
214 if(U_FAILURE(errorCode
)) {
215 errln("error retrieving conversion/fromUnicode test case %d - %s",
216 i
, u_errorName(errorCode
));
217 errorCode
=U_ZERO_ERROR
;
223 s
=testCase
->getString("charset", errorCode
);
224 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
227 unicode
=testCase
->getString("unicode", errorCode
);
228 cc
.unicode
=unicode
.getBuffer();
229 cc
.unicodeLength
=unicode
.length();
230 cc
.bytes
=testCase
->getBinary(cc
.bytesLength
, "bytes", errorCode
);
233 cc
.offsets
=testCase
->getIntVector(offsetsLength
, "offsets", errorCode
);
234 if(offsetsLength
==0) {
236 } else if(offsetsLength
!=cc
.bytesLength
) {
237 errln("fromUnicode[%d] bytes[%d] and offsets[%d] must have the same length",
238 i
, cc
.bytesLength
, offsetsLength
);
239 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
242 cc
.finalFlush
= 0!=testCase
->getInt28("flush", errorCode
);
243 cc
.fallbacks
= 0!=testCase
->getInt28("fallbacks", errorCode
);
245 s
=testCase
->getString("errorCode", errorCode
);
246 if(s
==UNICODE_STRING("invalid", 7)) {
247 cc
.outErrorCode
=U_INVALID_CHAR_FOUND
;
248 } else if(s
==UNICODE_STRING("illegal", 7)) {
249 cc
.outErrorCode
=U_ILLEGAL_CHAR_FOUND
;
250 } else if(s
==UNICODE_STRING("truncated", 9)) {
251 cc
.outErrorCode
=U_TRUNCATED_CHAR_FOUND
;
253 cc
.outErrorCode
=U_ZERO_ERROR
;
256 s
=testCase
->getString("callback", errorCode
);
257 cc
.setSub
=0; // default: no subchar
259 if((index
=s
.indexOf((UChar
)0))>0) {
260 // read NUL-separated subchar first, if any
261 // copy the subchar from Latin-1 characters
262 // start after the NUL
263 p
=s
.getTerminatedBuffer();
266 length
=s
.length()-length
;
267 if(length
<=0 || length
>=(int32_t)sizeof(cc
.subchar
)) {
268 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
272 for(j
=0; j
<length
; ++j
) {
273 cc
.subchar
[j
]=(char)p
[j
];
275 // NUL-terminate the subchar
280 // remove the NUL and subchar from s
282 } else if((index
=s
.indexOf((UChar
)0x3d))>0) /* '=' */ {
283 // read a substitution string, separated by an equal sign
284 p
=s
.getBuffer()+index
+1;
285 length
=s
.length()-(index
+1);
286 if(length
<0 || length
>=LENGTHOF(cc
.subString
)) {
287 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
289 u_memcpy(cc
.subString
, p
, length
);
290 // NUL-terminate the subString
291 cc
.subString
[length
]=0;
295 // remove the equal sign and subString from s
299 s
.extract(0, 0x7fffffff, cbopt
, sizeof(cbopt
), "");
303 callback
=UCNV_FROM_U_CALLBACK_SUBSTITUTE
;
306 callback
=UCNV_FROM_U_CALLBACK_SKIP
;
309 callback
=UCNV_FROM_U_CALLBACK_STOP
;
312 callback
=UCNV_FROM_U_CALLBACK_ESCAPE
;
318 option
=callback
==NULL
? cbopt
: cbopt
+1;
323 invalidUChars
=testCase
->getString("invalidUChars", errorCode
);
324 cc
.invalidUChars
=invalidUChars
.getBuffer();
325 cc
.invalidLength
=invalidUChars
.length();
327 if(U_FAILURE(errorCode
)) {
328 errln("error parsing conversion/fromUnicode test case %d - %s",
329 i
, u_errorName(errorCode
));
330 errorCode
=U_ZERO_ERROR
;
332 logln("TestFromUnicode[%d] %s", i
, charset
);
333 FromUnicodeCase(cc
, callback
, option
);
341 dataerrln("[DATA] Could not load test conversion data");
345 static const UChar ellipsis
[]={ 0x2e, 0x2e, 0x2e };
348 ConversionTest::TestGetUnicodeSet() {
350 UnicodeString s
, map
, mapnot
;
354 UnicodeSet cnvSet
, mapSet
, mapnotSet
, diffSet
;
355 UnicodeSet
*cnvSetPtr
= &cnvSet
;
358 TestDataModule
*dataModule
;
360 const DataMap
*testCase
;
361 UErrorCode errorCode
;
364 errorCode
=U_ZERO_ERROR
;
365 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
366 if(U_SUCCESS(errorCode
)) {
367 testData
=dataModule
->createTestData("getUnicodeSet", errorCode
);
368 if(U_SUCCESS(errorCode
)) {
369 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
370 if(U_FAILURE(errorCode
)) {
371 errln("error retrieving conversion/getUnicodeSet test case %d - %s",
372 i
, u_errorName(errorCode
));
373 errorCode
=U_ZERO_ERROR
;
377 s
=testCase
->getString("charset", errorCode
);
378 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
380 map
=testCase
->getString("map", errorCode
);
381 mapnot
=testCase
->getString("mapnot", errorCode
);
383 which
=testCase
->getInt28("which", errorCode
);
385 if(U_FAILURE(errorCode
)) {
386 errln("error parsing conversion/getUnicodeSet test case %d - %s",
387 i
, u_errorName(errorCode
));
388 errorCode
=U_ZERO_ERROR
;
392 // test this test case
397 mapSet
.applyPattern(map
, pos
, 0, NULL
, errorCode
);
398 if(U_FAILURE(errorCode
) || pos
.getIndex()!=map
.length()) {
399 errln("error creating the map set for conversion/getUnicodeSet test case %d - %s\n"
400 " error index %d index %d U+%04x",
401 i
, u_errorName(errorCode
), pos
.getErrorIndex(), pos
.getIndex(), map
.char32At(pos
.getIndex()));
402 errorCode
=U_ZERO_ERROR
;
407 mapnotSet
.applyPattern(mapnot
, pos
, 0, NULL
, errorCode
);
408 if(U_FAILURE(errorCode
) || pos
.getIndex()!=mapnot
.length()) {
409 errln("error creating the mapnot set for conversion/getUnicodeSet test case %d - %s\n"
410 " error index %d index %d U+%04x",
411 i
, u_errorName(errorCode
), pos
.getErrorIndex(), pos
.getIndex(), mapnot
.char32At(pos
.getIndex()));
412 errorCode
=U_ZERO_ERROR
;
416 logln("TestGetUnicodeSet[%d] %s", i
, charset
);
418 cnv
=cnv_open(charset
, errorCode
);
419 if(U_FAILURE(errorCode
)) {
420 errln("error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
421 charset
, i
, u_errorName(errorCode
));
422 errorCode
=U_ZERO_ERROR
;
426 ucnv_getUnicodeSet(cnv
, (USet
*)cnvSetPtr
, (UConverterUnicodeSet
)which
, &errorCode
);
429 if(U_FAILURE(errorCode
)) {
430 errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
431 charset
, i
, u_errorName(errorCode
));
432 errorCode
=U_ZERO_ERROR
;
436 // are there items that must be in cnvSet but are not?
437 (diffSet
=mapSet
).removeAll(cnvSet
);
438 if(!diffSet
.isEmpty()) {
439 diffSet
.toPattern(s
, TRUE
);
441 s
.replace(100, 0x7fffffff, ellipsis
, LENGTHOF(ellipsis
));
443 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - conversion/getUnicodeSet test case %d",
448 // are there items that must not be in cnvSet but are?
449 (diffSet
=mapnotSet
).retainAll(cnvSet
);
450 if(!diffSet
.isEmpty()) {
451 diffSet
.toPattern(s
, TRUE
);
453 s
.replace(100, 0x7fffffff, ellipsis
, LENGTHOF(ellipsis
));
455 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - conversion/getUnicodeSet test case %d",
465 dataerrln("[DATA] Could not load test conversion data");
470 static void U_CALLCONV
471 getUnicodeSetCallback(const void *context
,
472 UConverterFromUnicodeArgs
* /*fromUArgs*/,
473 const UChar
* /*codeUnits*/,
476 UConverterCallbackReason reason
,
477 UErrorCode
*pErrorCode
) {
478 if(reason
<=UCNV_IRREGULAR
) {
479 ((UnicodeSet
*)context
)->remove(codePoint
); // the converter cannot convert this code point
480 *pErrorCode
=U_ZERO_ERROR
; // skip
481 } // else ignore the reset, close and clone calls.
485 // Compare ucnv_getUnicodeSet() with the set of characters that can be converted.
487 ConversionTest::TestGetUnicodeSet2() {
488 // Build a string with all code points.
492 cpLimit
=s0Length
=0x10000; // BMP only
495 s0Length
=0x10000+0x200000; // BMP + surrogate pairs
497 UChar
*s0
=new UChar
[s0Length
];
505 for(c
=0; c
<=0xd7ff; ++c
) {
509 for(c
=0xdc00; c
<=0xdfff; ++c
) {
513 // (after trails so that there is not even one surrogate pair in between)
514 for(c
=0xd800; c
<=0xdbff; ++c
) {
518 for(c
=0xe000; c
<=0xffff; ++c
) {
521 // supplementary code points = surrogate pairs
522 if(cpLimit
==0x110000) {
523 for(c
=0xd800; c
<=0xdbff; ++c
) {
524 for(c2
=0xdc00; c2
<=0xdfff; ++c2
) {
531 static const char *const cnvNames
[]={
539 "ibm-1390", // EBCDIC_STATEFUL table
540 "ibm-16684", // DBCS-only extension table based on EBCDIC_STATEFUL table
550 for(i
=0; i
<LENGTHOF(cnvNames
); ++i
) {
551 UErrorCode errorCode
=U_ZERO_ERROR
;
552 UConverter
*cnv
=cnv_open(cnvNames
[i
], errorCode
);
553 if(U_FAILURE(errorCode
)) {
554 errln("failed to open converter %s - %s", cnvNames
[i
], u_errorName(errorCode
));
558 ucnv_setFromUCallBack(cnv
, getUnicodeSetCallback
, &expected
, NULL
, NULL
, &errorCode
);
559 if(U_FAILURE(errorCode
)) {
560 errln("failed to set the callback on converter %s - %s", cnvNames
[i
], u_errorName(errorCode
));
564 UConverterUnicodeSet which
;
565 for(which
=UCNV_ROUNDTRIP_SET
; which
<UCNV_SET_COUNT
; which
=(UConverterUnicodeSet
)((int)which
+1)) {
566 if(which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
) {
567 ucnv_setFallback(cnv
, TRUE
);
569 expected
.add(0, cpLimit
-1);
574 flush
=(UBool
)(s
==s0
+s0Length
);
575 ucnv_fromUnicode(cnv
, &t
, buffer
+sizeof(buffer
), (const UChar
**)&s
, s0
+s0Length
, NULL
, flush
, &errorCode
);
576 if(U_FAILURE(errorCode
)) {
577 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
578 errorCode
=U_ZERO_ERROR
;
581 break; // unexpected error, should not occur
586 ucnv_getUnicodeSet(cnv
, (USet
*)&set
, which
, &errorCode
);
587 if(cpLimit
<0x110000) {
588 set
.remove(cpLimit
, 0x10ffff);
590 if(which
==UCNV_ROUNDTRIP_SET
) {
591 // ignore PUA code points because they will be converted even if they
592 // are fallbacks and when other fallbacks are turned off,
593 // but ucnv_getUnicodeSet(UCNV_ROUNDTRIP_SET) delivers true roundtrips
594 expected
.remove(0xe000, 0xf8ff);
595 expected
.remove(0xf0000, 0xffffd);
596 expected
.remove(0x100000, 0x10fffd);
597 set
.remove(0xe000, 0xf8ff);
598 set
.remove(0xf0000, 0xffffd);
599 set
.remove(0x100000, 0x10fffd);
602 // First try to see if we have different sets because ucnv_getUnicodeSet()
603 // added strings: The above conversion method does not tell us what strings might be convertible.
604 // Remove strings from the set and compare again.
605 // Unfortunately, there are no good, direct set methods for finding out whether there are strings
606 // in the set, nor for enumerating or removing just them.
607 // Intersect all code points with the set. The intersection will not contain strings.
608 UnicodeSet
temp(0, 0x10ffff);
616 // are there items that must be in the set but are not?
617 (diffSet
=expected
).removeAll(set
);
618 if(!diffSet
.isEmpty()) {
619 diffSet
.toPattern(out
, TRUE
);
620 if(out
.length()>100) {
621 out
.replace(100, 0x7fffffff, ellipsis
, LENGTHOF(ellipsis
));
623 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - which set: %d",
628 // are there items that must not be in the set but are?
629 (diffSet
=set
).removeAll(expected
);
630 if(!diffSet
.isEmpty()) {
631 diffSet
.toPattern(out
, TRUE
);
632 if(out
.length()>100) {
633 out
.replace(100, 0x7fffffff, ellipsis
, LENGTHOF(ellipsis
));
635 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - which set: %d",
647 // open testdata or ICU data converter ------------------------------------- ***
650 ConversionTest::cnv_open(const char *name
, UErrorCode
&errorCode
) {
651 if(name
!=NULL
&& *name
=='*') {
652 /* loadTestData(): set the data directory */
653 return ucnv_openPackage(loadTestData(errorCode
), name
+1, &errorCode
);
655 return ucnv_open(name
, &errorCode
);
659 // output helpers ---------------------------------------------------------- ***
662 hexDigit(uint8_t digit
) {
663 return digit
<=9 ? (char)('0'+digit
) : (char)('a'-10+digit
);
667 printBytes(const uint8_t *bytes
, int32_t length
, char *out
) {
673 *out
++=hexDigit((uint8_t)(b
>>4));
674 *out
++=hexDigit((uint8_t)(b
&0xf));
681 *out
++=hexDigit((uint8_t)(b
>>4));
682 *out
++=hexDigit((uint8_t)(b
&0xf));
689 printUnicode(const UChar
*unicode
, int32_t length
, char *out
) {
693 for(i
=0; i
<length
;) {
697 U16_NEXT(unicode
, i
, length
, c
);
703 *out
++=hexDigit((uint8_t)((c
>>16)&0xf));
705 *out
++=hexDigit((uint8_t)((c
>>12)&0xf));
706 *out
++=hexDigit((uint8_t)((c
>>8)&0xf));
707 *out
++=hexDigit((uint8_t)((c
>>4)&0xf));
708 *out
++=hexDigit((uint8_t)(c
&0xf));
715 printOffsets(const int32_t *offsets
, int32_t length
, char *out
) {
722 for(i
=0; i
<length
; ++i
) {
728 // print all offsets with 2 characters each (-x, -9..99, xx)
734 *out
++=(char)('0'-o
);
736 *out
++=(d
=o
/10)==0 ? ' ' : (char)('0'+d
);
737 *out
++=(char)('0'+o%10
);
747 // toUnicode test worker functions ----------------------------------------- ***
750 stepToUnicode(ConversionCase
&cc
, UConverter
*cnv
,
751 UChar
*result
, int32_t resultCapacity
,
752 int32_t *resultOffsets
, /* also resultCapacity */
754 UErrorCode
*pErrorCode
) {
755 const char *source
, *sourceLimit
, *bytesLimit
;
756 UChar
*target
, *targetLimit
, *resultLimit
;
759 source
=(const char *)cc
.bytes
;
761 bytesLimit
=source
+cc
.bytesLength
;
762 resultLimit
=result
+resultCapacity
;
765 // call ucnv_toUnicode() with in/out buffers no larger than (step) at a time
766 // move only one buffer (in vs. out) at a time to be extra mean
767 // step==0 performs bulk conversion and generates offsets
769 // initialize the partial limits for the loop
771 // use the entire buffers
772 sourceLimit
=bytesLimit
;
773 targetLimit
=resultLimit
;
776 // start with empty partial buffers
781 // output offsets only for bulk conversion
786 // resetting the opposite conversion direction must not affect this one
787 ucnv_resetFromUnicode(cnv
);
791 &target
, targetLimit
,
792 &source
, sourceLimit
,
796 // check pointers and errors
797 if(source
>sourceLimit
|| target
>targetLimit
) {
798 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
800 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
801 if(target
!=targetLimit
) {
802 // buffer overflow must only be set when the target is filled
803 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
805 } else if(targetLimit
==resultLimit
) {
806 // not just a partial overflow
810 // the partial target is filled, set a new limit, reset the error and continue
811 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
812 *pErrorCode
=U_ZERO_ERROR
;
813 } else if(U_FAILURE(*pErrorCode
)) {
814 // some other error occurred, done
817 if(source
!=sourceLimit
) {
818 // when no error occurs, then the input must be consumed
819 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
823 if(sourceLimit
==bytesLimit
) {
828 // the partial conversion succeeded, set a new limit and continue
829 sourceLimit
=(bytesLimit
-source
)>=step
? source
+step
: bytesLimit
;
830 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==bytesLimit
);
833 } else /* step<0 */ {
835 * step==-1: call only ucnv_getNextUChar()
836 * otherwise alternate between ucnv_toUnicode() and ucnv_getNextUChar()
837 * if step==-2 or -3, then give ucnv_toUnicode() the whole remaining input,
838 * else give it at most (-step-2)/2 bytes
842 // end the loop by getting an index out of bounds error
844 // resetting the opposite conversion direction must not affect this one
845 ucnv_resetFromUnicode(cnv
);
848 if((step
&1)!=0 /* odd: -1, -3, -5, ... */) {
849 sourceLimit
=source
; // use sourceLimit not as a real limit
850 // but to remember the pre-getNextUChar source pointer
851 c
=ucnv_getNextUChar(cnv
, &source
, bytesLimit
, pErrorCode
);
853 // check pointers and errors
854 if(*pErrorCode
==U_INDEX_OUTOFBOUNDS_ERROR
) {
855 if(source
!=bytesLimit
) {
856 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
858 *pErrorCode
=U_ZERO_ERROR
;
861 } else if(U_FAILURE(*pErrorCode
)) {
864 // source may not move if c is from previous overflow
866 if(target
==resultLimit
) {
867 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
873 *target
++=U16_LEAD(c
);
874 if(target
==resultLimit
) {
875 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
878 *target
++=U16_TRAIL(c
);
881 // alternate between -n-1 and -n but leave -1 alone
885 } else /* step is even */ {
886 // allow only one UChar output
887 targetLimit
=target
<resultLimit
? target
+1 : resultLimit
;
889 // as with ucnv_getNextUChar(), we always flush (if we go to bytesLimit)
890 // and never output offsets
892 sourceLimit
=bytesLimit
;
894 sourceLimit
=source
+(-step
-2)/2;
895 if(sourceLimit
>bytesLimit
) {
896 sourceLimit
=bytesLimit
;
901 &target
, targetLimit
,
902 &source
, sourceLimit
,
903 NULL
, (UBool
)(sourceLimit
==bytesLimit
), pErrorCode
);
905 // check pointers and errors
906 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
907 if(target
!=targetLimit
) {
908 // buffer overflow must only be set when the target is filled
909 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
911 } else if(targetLimit
==resultLimit
) {
912 // not just a partial overflow
916 // the partial target is filled, set a new limit and continue
917 *pErrorCode
=U_ZERO_ERROR
;
918 } else if(U_FAILURE(*pErrorCode
)) {
919 // some other error occurred, done
922 if(source
!=sourceLimit
) {
923 // when no error occurs, then the input must be consumed
924 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
928 // we are done (flush==TRUE) but we continue, to get the index out of bounds error above
936 return (int32_t)(target
-result
);
940 ConversionTest::ToUnicodeCase(ConversionCase
&cc
, UConverterToUCallback callback
, const char *option
) {
942 UErrorCode errorCode
;
944 // open the converter
945 errorCode
=U_ZERO_ERROR
;
946 cnv
=cnv_open(cc
.charset
, errorCode
);
947 if(U_FAILURE(errorCode
)) {
948 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
949 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
955 ucnv_setToUCallBack(cnv
, callback
, option
, NULL
, NULL
, &errorCode
);
956 if(U_FAILURE(errorCode
)) {
957 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setToUCallBack() failed - %s",
958 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
964 int32_t resultOffsets
[256];
966 int32_t resultLength
;
969 static const struct {
973 { 0, "bulk" }, // must be first for offsets to be checked
978 { -2, "toU(bulk)+getNext" },
979 { -3, "getNext+toU(bulk)" },
980 { -4, "toU(1)+getNext" },
981 { -5, "getNext+toU(1)" },
982 { -12, "toU(5)+getNext" },
983 { -13, "getNext+toU(5)" },
988 for(i
=0; i
<LENGTHOF(steps
) && ok
; ++i
) {
990 if(step
<0 && !cc
.finalFlush
) {
991 // skip ucnv_getNextUChar() if !finalFlush because
992 // ucnv_getNextUChar() always implies flush
996 // bulk test is first, then offsets are not checked any more
1000 memset(resultOffsets
, -1, LENGTHOF(resultOffsets
));
1002 memset(result
, -1, LENGTHOF(result
));
1003 errorCode
=U_ZERO_ERROR
;
1004 resultLength
=stepToUnicode(cc
, cnv
,
1005 result
, LENGTHOF(result
),
1006 step
==0 ? resultOffsets
: NULL
,
1009 cc
, cnv
, steps
[i
].name
,
1010 result
, resultLength
,
1011 cc
.offsets
!=NULL
? resultOffsets
: NULL
,
1013 if(U_FAILURE(errorCode
) || !cc
.finalFlush
) {
1014 // reset if an error occurred or we did not flush
1015 // otherwise do nothing to make sure that flushing resets
1016 ucnv_resetToUnicode(cnv
);
1018 if (resultOffsets
[resultLength
] != -1) {
1019 errln("toUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
1020 cc
.caseNr
, cc
.charset
, resultLength
);
1022 if (result
[resultLength
] != (UChar
)-1) {
1023 errln("toUnicode[%d](%s) Conversion wrote too much to result at index %d",
1024 cc
.caseNr
, cc
.charset
, resultLength
);
1028 // not a real loop, just a convenience for breaking out of the block
1029 while(ok
&& cc
.finalFlush
) {
1030 // test ucnv_toUChars()
1031 memset(result
, 0, sizeof(result
));
1033 errorCode
=U_ZERO_ERROR
;
1034 resultLength
=ucnv_toUChars(cnv
,
1035 result
, LENGTHOF(result
),
1036 (const char *)cc
.bytes
, cc
.bytesLength
,
1039 cc
, cnv
, "toUChars",
1040 result
, resultLength
,
1047 // test preflighting
1048 // keep the correct result for simple checking
1049 errorCode
=U_ZERO_ERROR
;
1050 resultLength
=ucnv_toUChars(cnv
,
1052 (const char *)cc
.bytes
, cc
.bytesLength
,
1054 if(errorCode
==U_STRING_NOT_TERMINATED_WARNING
|| errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1055 errorCode
=U_ZERO_ERROR
;
1058 cc
, cnv
, "preflight toUChars",
1059 result
, resultLength
,
1070 ConversionTest::checkToUnicode(ConversionCase
&cc
, UConverter
*cnv
, const char *name
,
1071 const UChar
*result
, int32_t resultLength
,
1072 const int32_t *resultOffsets
,
1073 UErrorCode resultErrorCode
) {
1074 char resultInvalidChars
[8];
1075 int8_t resultInvalidLength
;
1076 UErrorCode errorCode
;
1080 // reset the message; NULL will mean "ok"
1083 errorCode
=U_ZERO_ERROR
;
1084 resultInvalidLength
=sizeof(resultInvalidChars
);
1085 ucnv_getInvalidChars(cnv
, resultInvalidChars
, &resultInvalidLength
, &errorCode
);
1086 if(U_FAILURE(errorCode
)) {
1087 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidChars() failed - %s",
1088 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, u_errorName(errorCode
));
1092 // check everything that might have gone wrong
1093 if(cc
.unicodeLength
!=resultLength
) {
1094 msg
="wrong result length";
1095 } else if(0!=u_memcmp(cc
.unicode
, result
, cc
.unicodeLength
)) {
1096 msg
="wrong result string";
1097 } else if(cc
.offsets
!=NULL
&& 0!=memcmp(cc
.offsets
, resultOffsets
, cc
.unicodeLength
*sizeof(*cc
.offsets
))) {
1098 msg
="wrong offsets";
1099 } else if(cc
.outErrorCode
!=resultErrorCode
) {
1100 msg
="wrong error code";
1101 } else if(cc
.invalidLength
!=resultInvalidLength
) {
1102 msg
="wrong length of last invalid input";
1103 } else if(0!=memcmp(cc
.invalidChars
, resultInvalidChars
, cc
.invalidLength
)) {
1104 msg
="wrong last invalid input";
1110 char buffer
[2000]; // one buffer for all strings
1111 char *s
, *bytesString
, *unicodeString
, *resultString
,
1112 *offsetsString
, *resultOffsetsString
,
1113 *invalidCharsString
, *resultInvalidCharsString
;
1115 bytesString
=s
=buffer
;
1116 s
=printBytes(cc
.bytes
, cc
.bytesLength
, bytesString
);
1117 s
=printUnicode(cc
.unicode
, cc
.unicodeLength
, unicodeString
=s
);
1118 s
=printUnicode(result
, resultLength
, resultString
=s
);
1119 s
=printOffsets(cc
.offsets
, cc
.unicodeLength
, offsetsString
=s
);
1120 s
=printOffsets(resultOffsets
, resultLength
, resultOffsetsString
=s
);
1121 s
=printBytes(cc
.invalidChars
, cc
.invalidLength
, invalidCharsString
=s
);
1122 s
=printBytes((uint8_t *)resultInvalidChars
, resultInvalidLength
, resultInvalidCharsString
=s
);
1124 if((s
-buffer
)>(int32_t)sizeof(buffer
)) {
1125 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkToUnicode() test output buffer overflow writing %d chars\n",
1126 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, (int)(s
-buffer
));
1130 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
1132 " expected <%s>[%d]\n"
1133 " result <%s>[%d]\n"
1135 " result offsets <%s>\n"
1136 " error code expected %s got %s\n"
1137 " invalidChars expected <%s> got <%s>\n",
1138 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, msg
,
1139 bytesString
, cc
.bytesLength
,
1140 unicodeString
, cc
.unicodeLength
,
1141 resultString
, resultLength
,
1143 resultOffsetsString
,
1144 u_errorName(cc
.outErrorCode
), u_errorName(resultErrorCode
),
1145 invalidCharsString
, resultInvalidCharsString
);
1151 // fromUnicode test worker functions --------------------------------------- ***
1154 stepFromUTF8(ConversionCase
&cc
,
1155 UConverter
*utf8Cnv
, UConverter
*cnv
,
1156 char *result
, int32_t resultCapacity
,
1158 UErrorCode
*pErrorCode
) {
1159 const char *source
, *sourceLimit
, *utf8Limit
;
1160 UChar pivotBuffer
[32];
1161 UChar
*pivotSource
, *pivotTarget
, *pivotLimit
;
1162 char *target
, *targetLimit
, *resultLimit
;
1166 pivotSource
=pivotTarget
=pivotBuffer
;
1168 utf8Limit
=source
+cc
.utf8Length
;
1169 resultLimit
=result
+resultCapacity
;
1171 // call ucnv_convertEx() with in/out buffers no larger than (step) at a time
1172 // move only one buffer (in vs. out) at a time to be extra mean
1173 // step==0 performs bulk conversion
1175 // initialize the partial limits for the loop
1177 // use the entire buffers
1178 sourceLimit
=utf8Limit
;
1179 targetLimit
=resultLimit
;
1180 flush
=cc
.finalFlush
;
1182 pivotLimit
=pivotBuffer
+LENGTHOF(pivotBuffer
);
1184 // start with empty partial buffers
1189 // empty pivot is not allowed, make it of length step
1190 pivotLimit
=pivotBuffer
+step
;
1194 // resetting the opposite conversion direction must not affect this one
1195 ucnv_resetFromUnicode(utf8Cnv
);
1196 ucnv_resetToUnicode(cnv
);
1199 ucnv_convertEx(cnv
, utf8Cnv
,
1200 &target
, targetLimit
,
1201 &source
, sourceLimit
,
1202 pivotBuffer
, &pivotSource
, &pivotTarget
, pivotLimit
,
1203 FALSE
, flush
, pErrorCode
);
1205 // check pointers and errors
1206 if(source
>sourceLimit
|| target
>targetLimit
) {
1207 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1209 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1210 if(target
!=targetLimit
) {
1211 // buffer overflow must only be set when the target is filled
1212 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1214 } else if(targetLimit
==resultLimit
) {
1215 // not just a partial overflow
1219 // the partial target is filled, set a new limit, reset the error and continue
1220 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
1221 *pErrorCode
=U_ZERO_ERROR
;
1222 } else if(U_FAILURE(*pErrorCode
)) {
1223 if(pivotSource
==pivotBuffer
) {
1224 // toUnicode error, should not occur
1225 // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
1228 // fromUnicode error
1229 // some other error occurred, done
1233 if(source
!=sourceLimit
) {
1234 // when no error occurs, then the input must be consumed
1235 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1239 if(sourceLimit
==utf8Limit
) {
1241 if(*pErrorCode
==U_STRING_NOT_TERMINATED_WARNING
) {
1242 // ucnv_convertEx() warns about not terminating the output
1243 // but ucnv_fromUnicode() does not and so
1244 // checkFromUnicode() does not expect it
1245 *pErrorCode
=U_ZERO_ERROR
;
1250 // the partial conversion succeeded, set a new limit and continue
1251 sourceLimit
=(utf8Limit
-source
)>=step
? source
+step
: utf8Limit
;
1252 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==utf8Limit
);
1256 return (int32_t)(target
-result
);
1260 stepFromUnicode(ConversionCase
&cc
, UConverter
*cnv
,
1261 char *result
, int32_t resultCapacity
,
1262 int32_t *resultOffsets
, /* also resultCapacity */
1264 UErrorCode
*pErrorCode
) {
1265 const UChar
*source
, *sourceLimit
, *unicodeLimit
;
1266 char *target
, *targetLimit
, *resultLimit
;
1271 unicodeLimit
=source
+cc
.unicodeLength
;
1272 resultLimit
=result
+resultCapacity
;
1274 // call ucnv_fromUnicode() with in/out buffers no larger than (step) at a time
1275 // move only one buffer (in vs. out) at a time to be extra mean
1276 // step==0 performs bulk conversion and generates offsets
1278 // initialize the partial limits for the loop
1280 // use the entire buffers
1281 sourceLimit
=unicodeLimit
;
1282 targetLimit
=resultLimit
;
1283 flush
=cc
.finalFlush
;
1285 // start with empty partial buffers
1290 // output offsets only for bulk conversion
1295 // resetting the opposite conversion direction must not affect this one
1296 ucnv_resetToUnicode(cnv
);
1299 ucnv_fromUnicode(cnv
,
1300 &target
, targetLimit
,
1301 &source
, sourceLimit
,
1305 // check pointers and errors
1306 if(source
>sourceLimit
|| target
>targetLimit
) {
1307 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1309 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1310 if(target
!=targetLimit
) {
1311 // buffer overflow must only be set when the target is filled
1312 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1314 } else if(targetLimit
==resultLimit
) {
1315 // not just a partial overflow
1319 // the partial target is filled, set a new limit, reset the error and continue
1320 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
1321 *pErrorCode
=U_ZERO_ERROR
;
1322 } else if(U_FAILURE(*pErrorCode
)) {
1323 // some other error occurred, done
1326 if(source
!=sourceLimit
) {
1327 // when no error occurs, then the input must be consumed
1328 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1332 if(sourceLimit
==unicodeLimit
) {
1337 // the partial conversion succeeded, set a new limit and continue
1338 sourceLimit
=(unicodeLimit
-source
)>=step
? source
+step
: unicodeLimit
;
1339 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==unicodeLimit
);
1343 return (int32_t)(target
-result
);
1347 ConversionTest::FromUnicodeCase(ConversionCase
&cc
, UConverterFromUCallback callback
, const char *option
) {
1349 UErrorCode errorCode
;
1351 // open the converter
1352 errorCode
=U_ZERO_ERROR
;
1353 cnv
=cnv_open(cc
.charset
, errorCode
);
1354 if(U_FAILURE(errorCode
)) {
1355 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
1356 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1359 ucnv_resetToUnicode(utf8Cnv
);
1362 if(callback
!=NULL
) {
1363 ucnv_setFromUCallBack(cnv
, callback
, option
, NULL
, NULL
, &errorCode
);
1364 if(U_FAILURE(errorCode
)) {
1365 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setFromUCallBack() failed - %s",
1366 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1372 // set the fallbacks flag
1373 // TODO change with Jitterbug 2401, then add a similar call for toUnicode too
1374 ucnv_setFallback(cnv
, cc
.fallbacks
);
1380 length
=(int32_t)strlen(cc
.subchar
);
1381 ucnv_setSubstChars(cnv
, cc
.subchar
, (int8_t)length
, &errorCode
);
1382 if(U_FAILURE(errorCode
)) {
1383 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstChars() failed - %s",
1384 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1388 } else if(cc
.setSub
<0) {
1389 ucnv_setSubstString(cnv
, cc
.subString
, -1, &errorCode
);
1390 if(U_FAILURE(errorCode
)) {
1391 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstString() failed - %s",
1392 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1398 // convert unicode to utf8
1401 u_strToUTF8(utf8
, LENGTHOF(utf8
), &cc
.utf8Length
,
1402 cc
.unicode
, cc
.unicodeLength
,
1404 if(U_FAILURE(errorCode
)) {
1405 // skip UTF-8 testing of a string with an unpaired surrogate,
1406 // or of one that's too long
1407 // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
1411 int32_t resultOffsets
[256];
1413 int32_t resultLength
;
1416 static const struct {
1418 const char *name
, *utf8Name
;
1420 { 0, "bulk", "utf8" }, // must be first for offsets to be checked
1421 { 1, "step=1", "utf8 step=1" },
1422 { 3, "step=3", "utf8 step=3" },
1423 { 7, "step=7", "utf8 step=7" }
1428 for(i
=0; i
<LENGTHOF(steps
) && ok
; ++i
) {
1430 memset(resultOffsets
, -1, LENGTHOF(resultOffsets
));
1431 memset(result
, -1, LENGTHOF(result
));
1432 errorCode
=U_ZERO_ERROR
;
1433 resultLength
=stepFromUnicode(cc
, cnv
,
1434 result
, LENGTHOF(result
),
1435 step
==0 ? resultOffsets
: NULL
,
1437 ok
=checkFromUnicode(
1438 cc
, cnv
, steps
[i
].name
,
1439 (uint8_t *)result
, resultLength
,
1440 cc
.offsets
!=NULL
? resultOffsets
: NULL
,
1442 if(U_FAILURE(errorCode
) || !cc
.finalFlush
) {
1443 // reset if an error occurred or we did not flush
1444 // otherwise do nothing to make sure that flushing resets
1445 ucnv_resetFromUnicode(cnv
);
1447 if (resultOffsets
[resultLength
] != -1) {
1448 errln("fromUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
1449 cc
.caseNr
, cc
.charset
, resultLength
);
1451 if (result
[resultLength
] != (char)-1) {
1452 errln("fromUnicode[%d](%s) Conversion wrote too much to result at index %d",
1453 cc
.caseNr
, cc
.charset
, resultLength
);
1456 // bulk test is first, then offsets are not checked any more
1459 // test direct conversion from UTF-8
1460 if(cc
.utf8Length
>=0) {
1461 errorCode
=U_ZERO_ERROR
;
1462 resultLength
=stepFromUTF8(cc
, utf8Cnv
, cnv
,
1463 result
, LENGTHOF(result
),
1465 ok
=checkFromUnicode(
1466 cc
, cnv
, steps
[i
].utf8Name
,
1467 (uint8_t *)result
, resultLength
,
1470 if(U_FAILURE(errorCode
) || !cc
.finalFlush
) {
1471 // reset if an error occurred or we did not flush
1472 // otherwise do nothing to make sure that flushing resets
1473 ucnv_resetToUnicode(utf8Cnv
);
1474 ucnv_resetFromUnicode(cnv
);
1479 // not a real loop, just a convenience for breaking out of the block
1480 while(ok
&& cc
.finalFlush
) {
1481 // test ucnv_fromUChars()
1482 memset(result
, 0, sizeof(result
));
1484 errorCode
=U_ZERO_ERROR
;
1485 resultLength
=ucnv_fromUChars(cnv
,
1486 result
, LENGTHOF(result
),
1487 cc
.unicode
, cc
.unicodeLength
,
1489 ok
=checkFromUnicode(
1490 cc
, cnv
, "fromUChars",
1491 (uint8_t *)result
, resultLength
,
1498 // test preflighting
1499 // keep the correct result for simple checking
1500 errorCode
=U_ZERO_ERROR
;
1501 resultLength
=ucnv_fromUChars(cnv
,
1503 cc
.unicode
, cc
.unicodeLength
,
1505 if(errorCode
==U_STRING_NOT_TERMINATED_WARNING
|| errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1506 errorCode
=U_ZERO_ERROR
;
1508 ok
=checkFromUnicode(
1509 cc
, cnv
, "preflight fromUChars",
1510 (uint8_t *)result
, resultLength
,
1521 ConversionTest::checkFromUnicode(ConversionCase
&cc
, UConverter
*cnv
, const char *name
,
1522 const uint8_t *result
, int32_t resultLength
,
1523 const int32_t *resultOffsets
,
1524 UErrorCode resultErrorCode
) {
1525 UChar resultInvalidUChars
[8];
1526 int8_t resultInvalidLength
;
1527 UErrorCode errorCode
;
1531 // reset the message; NULL will mean "ok"
1534 errorCode
=U_ZERO_ERROR
;
1535 resultInvalidLength
=LENGTHOF(resultInvalidUChars
);
1536 ucnv_getInvalidUChars(cnv
, resultInvalidUChars
, &resultInvalidLength
, &errorCode
);
1537 if(U_FAILURE(errorCode
)) {
1538 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUChars() failed - %s",
1539 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, u_errorName(errorCode
));
1543 // check everything that might have gone wrong
1544 if(cc
.bytesLength
!=resultLength
) {
1545 msg
="wrong result length";
1546 } else if(0!=memcmp(cc
.bytes
, result
, cc
.bytesLength
)) {
1547 msg
="wrong result string";
1548 } else if(cc
.offsets
!=NULL
&& 0!=memcmp(cc
.offsets
, resultOffsets
, cc
.bytesLength
*sizeof(*cc
.offsets
))) {
1549 msg
="wrong offsets";
1550 } else if(cc
.outErrorCode
!=resultErrorCode
) {
1551 msg
="wrong error code";
1552 } else if(cc
.invalidLength
!=resultInvalidLength
) {
1553 msg
="wrong length of last invalid input";
1554 } else if(0!=u_memcmp(cc
.invalidUChars
, resultInvalidUChars
, cc
.invalidLength
)) {
1555 msg
="wrong last invalid input";
1561 char buffer
[2000]; // one buffer for all strings
1562 char *s
, *unicodeString
, *bytesString
, *resultString
,
1563 *offsetsString
, *resultOffsetsString
,
1564 *invalidCharsString
, *resultInvalidUCharsString
;
1566 unicodeString
=s
=buffer
;
1567 s
=printUnicode(cc
.unicode
, cc
.unicodeLength
, unicodeString
);
1568 s
=printBytes(cc
.bytes
, cc
.bytesLength
, bytesString
=s
);
1569 s
=printBytes(result
, resultLength
, resultString
=s
);
1570 s
=printOffsets(cc
.offsets
, cc
.bytesLength
, offsetsString
=s
);
1571 s
=printOffsets(resultOffsets
, resultLength
, resultOffsetsString
=s
);
1572 s
=printUnicode(cc
.invalidUChars
, cc
.invalidLength
, invalidCharsString
=s
);
1573 s
=printUnicode(resultInvalidUChars
, resultInvalidLength
, resultInvalidUCharsString
=s
);
1575 if((s
-buffer
)>(int32_t)sizeof(buffer
)) {
1576 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkFromUnicode() test output buffer overflow writing %d chars\n",
1577 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, (int)(s
-buffer
));
1581 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
1582 " unicode <%s>[%d]\n"
1583 " expected <%s>[%d]\n"
1584 " result <%s>[%d]\n"
1586 " result offsets <%s>\n"
1587 " error code expected %s got %s\n"
1588 " invalidChars expected <%s> got <%s>\n",
1589 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, msg
,
1590 unicodeString
, cc
.unicodeLength
,
1591 bytesString
, cc
.bytesLength
,
1592 resultString
, resultLength
,
1594 resultOffsetsString
,
1595 u_errorName(cc
.outErrorCode
), u_errorName(resultErrorCode
),
1596 invalidCharsString
, resultInvalidUCharsString
);
1602 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */