2 *******************************************************************************
4 * Copyright (C) 2003-2014, 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"
40 #include "unicode/tstdtmod.h"
45 // characters used in test data for callbacks
52 ConversionTest::ConversionTest() {
53 UErrorCode errorCode
=U_ZERO_ERROR
;
54 utf8Cnv
=ucnv_open("UTF-8", &errorCode
);
55 ucnv_setToUCallBack(utf8Cnv
, UCNV_TO_U_CALLBACK_STOP
, NULL
, NULL
, NULL
, &errorCode
);
56 if(U_FAILURE(errorCode
)) {
57 errln("unable to open UTF-8 converter");
61 ConversionTest::~ConversionTest() {
66 ConversionTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
67 if (exec
) logln("TestSuite ConversionTest: ");
69 #if !UCONFIG_NO_FILE_IO
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
="TestDefaultIgnorableCallback"; if (exec
) TestDefaultIgnorableCallback(); break;
78 case 3: name
="skip"; break;
80 case 4: name
="TestGetUnicodeSet2"; if (exec
) TestGetUnicodeSet2(); break;
81 default: name
=""; break; //needed to end loop
85 // test data interface ----------------------------------------------------- ***
88 ConversionTest::TestToUnicode() {
90 char charset
[100], cbopt
[4];
92 UnicodeString s
, unicode
;
93 int32_t offsetsLength
;
94 UConverterToUCallback callback
;
96 TestDataModule
*dataModule
;
98 const DataMap
*testCase
;
102 errorCode
=U_ZERO_ERROR
;
103 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
104 if(U_SUCCESS(errorCode
)) {
105 testData
=dataModule
->createTestData("toUnicode", errorCode
);
106 if(U_SUCCESS(errorCode
)) {
107 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
108 if(U_FAILURE(errorCode
)) {
109 errln("error retrieving conversion/toUnicode test case %d - %s",
110 i
, u_errorName(errorCode
));
111 errorCode
=U_ZERO_ERROR
;
117 s
=testCase
->getString("charset", errorCode
);
118 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
121 cc
.bytes
=testCase
->getBinary(cc
.bytesLength
, "bytes", errorCode
);
122 unicode
=testCase
->getString("unicode", errorCode
);
123 cc
.unicode
=unicode
.getBuffer();
124 cc
.unicodeLength
=unicode
.length();
127 cc
.offsets
=testCase
->getIntVector(offsetsLength
, "offsets", errorCode
);
128 if(offsetsLength
==0) {
130 } else if(offsetsLength
!=unicode
.length()) {
131 errln("toUnicode[%d] unicode[%d] and offsets[%d] must have the same length",
132 i
, unicode
.length(), offsetsLength
);
133 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
136 cc
.finalFlush
= 0!=testCase
->getInt28("flush", errorCode
);
137 cc
.fallbacks
= 0!=testCase
->getInt28("fallbacks", errorCode
);
139 s
=testCase
->getString("errorCode", errorCode
);
140 if(s
==UNICODE_STRING("invalid", 7)) {
141 cc
.outErrorCode
=U_INVALID_CHAR_FOUND
;
142 } else if(s
==UNICODE_STRING("illegal", 7)) {
143 cc
.outErrorCode
=U_ILLEGAL_CHAR_FOUND
;
144 } else if(s
==UNICODE_STRING("truncated", 9)) {
145 cc
.outErrorCode
=U_TRUNCATED_CHAR_FOUND
;
146 } else if(s
==UNICODE_STRING("illesc", 6)) {
147 cc
.outErrorCode
=U_ILLEGAL_ESCAPE_SEQUENCE
;
148 } else if(s
==UNICODE_STRING("unsuppesc", 9)) {
149 cc
.outErrorCode
=U_UNSUPPORTED_ESCAPE_SEQUENCE
;
151 cc
.outErrorCode
=U_ZERO_ERROR
;
154 s
=testCase
->getString("callback", errorCode
);
155 s
.extract(0, 0x7fffffff, cbopt
, sizeof(cbopt
), "");
159 callback
=UCNV_TO_U_CALLBACK_SUBSTITUTE
;
162 callback
=UCNV_TO_U_CALLBACK_SKIP
;
165 callback
=UCNV_TO_U_CALLBACK_STOP
;
168 callback
=UCNV_TO_U_CALLBACK_ESCAPE
;
174 option
=callback
==NULL
? cbopt
: cbopt
+1;
179 cc
.invalidChars
=testCase
->getBinary(cc
.invalidLength
, "invalidChars", errorCode
);
181 if(U_FAILURE(errorCode
)) {
182 errln("error parsing conversion/toUnicode test case %d - %s",
183 i
, u_errorName(errorCode
));
184 errorCode
=U_ZERO_ERROR
;
186 logln("TestToUnicode[%d] %s", i
, charset
);
187 ToUnicodeCase(cc
, callback
, option
);
195 dataerrln("Could not load test conversion data");
200 ConversionTest::TestFromUnicode() {
202 char charset
[100], cbopt
[4];
204 UnicodeString s
, unicode
, invalidUChars
;
205 int32_t offsetsLength
, index
;
206 UConverterFromUCallback callback
;
208 TestDataModule
*dataModule
;
210 const DataMap
*testCase
;
212 UErrorCode errorCode
;
215 errorCode
=U_ZERO_ERROR
;
216 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
217 if(U_SUCCESS(errorCode
)) {
218 testData
=dataModule
->createTestData("fromUnicode", errorCode
);
219 if(U_SUCCESS(errorCode
)) {
220 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
221 if(U_FAILURE(errorCode
)) {
222 errln("error retrieving conversion/fromUnicode test case %d - %s",
223 i
, u_errorName(errorCode
));
224 errorCode
=U_ZERO_ERROR
;
230 s
=testCase
->getString("charset", errorCode
);
231 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
234 unicode
=testCase
->getString("unicode", errorCode
);
235 cc
.unicode
=unicode
.getBuffer();
236 cc
.unicodeLength
=unicode
.length();
237 cc
.bytes
=testCase
->getBinary(cc
.bytesLength
, "bytes", errorCode
);
240 cc
.offsets
=testCase
->getIntVector(offsetsLength
, "offsets", errorCode
);
241 if(offsetsLength
==0) {
243 } else if(offsetsLength
!=cc
.bytesLength
) {
244 errln("fromUnicode[%d] bytes[%d] and offsets[%d] must have the same length",
245 i
, cc
.bytesLength
, offsetsLength
);
246 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
249 cc
.finalFlush
= 0!=testCase
->getInt28("flush", errorCode
);
250 cc
.fallbacks
= 0!=testCase
->getInt28("fallbacks", errorCode
);
252 s
=testCase
->getString("errorCode", errorCode
);
253 if(s
==UNICODE_STRING("invalid", 7)) {
254 cc
.outErrorCode
=U_INVALID_CHAR_FOUND
;
255 } else if(s
==UNICODE_STRING("illegal", 7)) {
256 cc
.outErrorCode
=U_ILLEGAL_CHAR_FOUND
;
257 } else if(s
==UNICODE_STRING("truncated", 9)) {
258 cc
.outErrorCode
=U_TRUNCATED_CHAR_FOUND
;
260 cc
.outErrorCode
=U_ZERO_ERROR
;
263 s
=testCase
->getString("callback", errorCode
);
264 cc
.setSub
=0; // default: no subchar
266 if((index
=s
.indexOf((UChar
)0))>0) {
267 // read NUL-separated subchar first, if any
268 // copy the subchar from Latin-1 characters
269 // start after the NUL
270 p
=s
.getTerminatedBuffer();
273 length
=s
.length()-length
;
274 if(length
<=0 || length
>=(int32_t)sizeof(cc
.subchar
)) {
275 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
279 for(j
=0; j
<length
; ++j
) {
280 cc
.subchar
[j
]=(char)p
[j
];
282 // NUL-terminate the subchar
287 // remove the NUL and subchar from s
289 } else if((index
=s
.indexOf((UChar
)0x3d))>0) /* '=' */ {
290 // read a substitution string, separated by an equal sign
291 p
=s
.getBuffer()+index
+1;
292 length
=s
.length()-(index
+1);
293 if(length
<0 || length
>=UPRV_LENGTHOF(cc
.subString
)) {
294 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
296 u_memcpy(cc
.subString
, p
, length
);
297 // NUL-terminate the subString
298 cc
.subString
[length
]=0;
302 // remove the equal sign and subString from s
306 s
.extract(0, 0x7fffffff, cbopt
, sizeof(cbopt
), "");
310 callback
=UCNV_FROM_U_CALLBACK_SUBSTITUTE
;
313 callback
=UCNV_FROM_U_CALLBACK_SKIP
;
316 callback
=UCNV_FROM_U_CALLBACK_STOP
;
319 callback
=UCNV_FROM_U_CALLBACK_ESCAPE
;
325 option
=callback
==NULL
? cbopt
: cbopt
+1;
330 invalidUChars
=testCase
->getString("invalidUChars", errorCode
);
331 cc
.invalidUChars
=invalidUChars
.getBuffer();
332 cc
.invalidLength
=invalidUChars
.length();
334 if(U_FAILURE(errorCode
)) {
335 errln("error parsing conversion/fromUnicode test case %d - %s",
336 i
, u_errorName(errorCode
));
337 errorCode
=U_ZERO_ERROR
;
339 logln("TestFromUnicode[%d] %s", i
, charset
);
340 FromUnicodeCase(cc
, callback
, option
);
348 dataerrln("Could not load test conversion data");
352 static const UChar ellipsis
[]={ 0x2e, 0x2e, 0x2e };
355 ConversionTest::TestGetUnicodeSet() {
357 UnicodeString s
, map
, mapnot
;
361 UnicodeSet cnvSet
, mapSet
, mapnotSet
, diffSet
;
362 UnicodeSet
*cnvSetPtr
= &cnvSet
;
363 LocalUConverterPointer cnv
;
365 TestDataModule
*dataModule
;
367 const DataMap
*testCase
;
368 UErrorCode errorCode
;
371 errorCode
=U_ZERO_ERROR
;
372 dataModule
=TestDataModule::getTestDataModule("conversion", *this, errorCode
);
373 if(U_SUCCESS(errorCode
)) {
374 testData
=dataModule
->createTestData("getUnicodeSet", errorCode
);
375 if(U_SUCCESS(errorCode
)) {
376 for(i
=0; testData
->nextCase(testCase
, errorCode
); ++i
) {
377 if(U_FAILURE(errorCode
)) {
378 errln("error retrieving conversion/getUnicodeSet test case %d - %s",
379 i
, u_errorName(errorCode
));
380 errorCode
=U_ZERO_ERROR
;
384 s
=testCase
->getString("charset", errorCode
);
385 s
.extract(0, 0x7fffffff, charset
, sizeof(charset
), "");
387 map
=testCase
->getString("map", errorCode
);
388 mapnot
=testCase
->getString("mapnot", errorCode
);
390 which
=testCase
->getInt28("which", errorCode
);
392 if(U_FAILURE(errorCode
)) {
393 errln("error parsing conversion/getUnicodeSet test case %d - %s",
394 i
, u_errorName(errorCode
));
395 errorCode
=U_ZERO_ERROR
;
399 // test this test case
404 mapSet
.applyPattern(map
, pos
, 0, NULL
, errorCode
);
405 if(U_FAILURE(errorCode
) || pos
.getIndex()!=map
.length()) {
406 errln("error creating the map set for conversion/getUnicodeSet test case %d - %s\n"
407 " error index %d index %d U+%04x",
408 i
, u_errorName(errorCode
), pos
.getErrorIndex(), pos
.getIndex(), map
.char32At(pos
.getIndex()));
409 errorCode
=U_ZERO_ERROR
;
414 mapnotSet
.applyPattern(mapnot
, pos
, 0, NULL
, errorCode
);
415 if(U_FAILURE(errorCode
) || pos
.getIndex()!=mapnot
.length()) {
416 errln("error creating the mapnot set for conversion/getUnicodeSet test case %d - %s\n"
417 " error index %d index %d U+%04x",
418 i
, u_errorName(errorCode
), pos
.getErrorIndex(), pos
.getIndex(), mapnot
.char32At(pos
.getIndex()));
419 errorCode
=U_ZERO_ERROR
;
423 logln("TestGetUnicodeSet[%d] %s", i
, charset
);
425 cnv
.adoptInstead(cnv_open(charset
, errorCode
));
426 if(U_FAILURE(errorCode
)) {
427 errcheckln(errorCode
, "error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
428 charset
, i
, u_errorName(errorCode
));
429 errorCode
=U_ZERO_ERROR
;
433 ucnv_getUnicodeSet(cnv
.getAlias(), cnvSetPtr
->toUSet(), (UConverterUnicodeSet
)which
, &errorCode
);
435 if(U_FAILURE(errorCode
)) {
436 errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
437 charset
, i
, u_errorName(errorCode
));
438 errorCode
=U_ZERO_ERROR
;
442 // are there items that must be in cnvSet but are not?
443 (diffSet
=mapSet
).removeAll(cnvSet
);
444 if(!diffSet
.isEmpty()) {
445 diffSet
.toPattern(s
, TRUE
);
447 s
.replace(100, 0x7fffffff, ellipsis
, UPRV_LENGTHOF(ellipsis
));
449 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - conversion/getUnicodeSet test case %d",
454 // are there items that must not be in cnvSet but are?
455 (diffSet
=mapnotSet
).retainAll(cnvSet
);
456 if(!diffSet
.isEmpty()) {
457 diffSet
.toPattern(s
, TRUE
);
459 s
.replace(100, 0x7fffffff, ellipsis
, UPRV_LENGTHOF(ellipsis
));
461 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - conversion/getUnicodeSet test case %d",
471 dataerrln("Could not load test conversion data");
476 static void U_CALLCONV
477 getUnicodeSetCallback(const void *context
,
478 UConverterFromUnicodeArgs
* /*fromUArgs*/,
479 const UChar
* /*codeUnits*/,
482 UConverterCallbackReason reason
,
483 UErrorCode
*pErrorCode
) {
484 if(reason
<=UCNV_IRREGULAR
) {
485 ((UnicodeSet
*)context
)->remove(codePoint
); // the converter cannot convert this code point
486 *pErrorCode
=U_ZERO_ERROR
; // skip
487 } // else ignore the reset, close and clone calls.
491 // Compare ucnv_getUnicodeSet() with the set of characters that can be converted.
493 ConversionTest::TestGetUnicodeSet2() {
494 // Build a string with all code points.
498 cpLimit
=s0Length
=0x10000; // BMP only
501 s0Length
=0x10000+0x200000; // BMP + surrogate pairs
503 UChar
*s0
=new UChar
[s0Length
];
511 for(c
=0; c
<=0xd7ff; ++c
) {
515 for(c
=0xdc00; c
<=0xdfff; ++c
) {
519 // (after trails so that there is not even one surrogate pair in between)
520 for(c
=0xd800; c
<=0xdbff; ++c
) {
524 for(c
=0xe000; c
<=0xffff; ++c
) {
527 // supplementary code points = surrogate pairs
528 if(cpLimit
==0x110000) {
529 for(c
=0xd800; c
<=0xdbff; ++c
) {
530 for(c2
=0xdc00; c2
<=0xdfff; ++c2
) {
537 static const char *const cnvNames
[]={
545 "ibm-1390", // EBCDIC_STATEFUL table
546 "ibm-16684", // DBCS-only extension table based on EBCDIC_STATEFUL table
554 LocalUConverterPointer cnv
;
557 for(i
=0; i
<UPRV_LENGTHOF(cnvNames
); ++i
) {
558 UErrorCode errorCode
=U_ZERO_ERROR
;
559 cnv
.adoptInstead(cnv_open(cnvNames
[i
], errorCode
));
560 if(U_FAILURE(errorCode
)) {
561 errcheckln(errorCode
, "failed to open converter %s - %s", cnvNames
[i
], u_errorName(errorCode
));
565 ucnv_setFromUCallBack(cnv
.getAlias(), getUnicodeSetCallback
, &expected
, NULL
, NULL
, &errorCode
);
566 if(U_FAILURE(errorCode
)) {
567 errln("failed to set the callback on converter %s - %s", cnvNames
[i
], u_errorName(errorCode
));
570 UConverterUnicodeSet which
;
571 for(which
=UCNV_ROUNDTRIP_SET
; which
<UCNV_SET_COUNT
; which
=(UConverterUnicodeSet
)((int)which
+1)) {
572 if(which
==UCNV_ROUNDTRIP_AND_FALLBACK_SET
) {
573 ucnv_setFallback(cnv
.getAlias(), TRUE
);
575 expected
.add(0, cpLimit
-1);
580 flush
=(UBool
)(s
==s0
+s0Length
);
581 ucnv_fromUnicode(cnv
.getAlias(), &t
, buffer
+sizeof(buffer
), (const UChar
**)&s
, s0
+s0Length
, NULL
, flush
, &errorCode
);
582 if(U_FAILURE(errorCode
)) {
583 if(errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
584 errorCode
=U_ZERO_ERROR
;
587 break; // unexpected error, should not occur
592 ucnv_getUnicodeSet(cnv
.getAlias(), set
.toUSet(), which
, &errorCode
);
593 if(cpLimit
<0x110000) {
594 set
.remove(cpLimit
, 0x10ffff);
596 if(which
==UCNV_ROUNDTRIP_SET
) {
597 // ignore PUA code points because they will be converted even if they
598 // are fallbacks and when other fallbacks are turned off,
599 // but ucnv_getUnicodeSet(UCNV_ROUNDTRIP_SET) delivers true roundtrips
600 expected
.remove(0xe000, 0xf8ff);
601 expected
.remove(0xf0000, 0xffffd);
602 expected
.remove(0x100000, 0x10fffd);
603 set
.remove(0xe000, 0xf8ff);
604 set
.remove(0xf0000, 0xffffd);
605 set
.remove(0x100000, 0x10fffd);
608 // First try to see if we have different sets because ucnv_getUnicodeSet()
609 // added strings: The above conversion method does not tell us what strings might be convertible.
610 // Remove strings from the set and compare again.
611 // Unfortunately, there are no good, direct set methods for finding out whether there are strings
612 // in the set, nor for enumerating or removing just them.
613 // Intersect all code points with the set. The intersection will not contain strings.
614 UnicodeSet
temp(0, 0x10ffff);
622 // are there items that must be in the set but are not?
623 (diffSet
=expected
).removeAll(set
);
624 if(!diffSet
.isEmpty()) {
625 diffSet
.toPattern(out
, TRUE
);
626 if(out
.length()>100) {
627 out
.replace(100, 0x7fffffff, ellipsis
, UPRV_LENGTHOF(ellipsis
));
629 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - which set: %d",
634 // are there items that must not be in the set but are?
635 (diffSet
=set
).removeAll(expected
);
636 if(!diffSet
.isEmpty()) {
637 diffSet
.toPattern(out
, TRUE
);
638 if(out
.length()>100) {
639 out
.replace(100, 0x7fffffff, ellipsis
, UPRV_LENGTHOF(ellipsis
));
641 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - which set: %d",
652 // Test all codepoints which has the default ignorable Unicode property are ignored if they have no mapping
653 // If there are any failures, the hard coded list (IS_DEFAULT_IGNORABLE_CODE_POINT) in ucnv_err.c should be updated
655 ConversionTest::TestDefaultIgnorableCallback() {
656 UErrorCode status
= U_ZERO_ERROR
;
657 const char *cnv_name
= "euc-jp-2007";
658 const char *pattern_ignorable
= "[:Default_Ignorable_Code_Point:]";
659 const char *pattern_not_ignorable
= "[:^Default_Ignorable_Code_Point:]";
661 UnicodeSet
*set_ignorable
= new UnicodeSet(pattern_ignorable
, status
);
662 if (U_FAILURE(status
)) {
663 dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_ignorable
, u_errorName(status
));
667 UnicodeSet
*set_not_ignorable
= new UnicodeSet(pattern_not_ignorable
, status
);
668 if (U_FAILURE(status
)) {
669 dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_not_ignorable
, u_errorName(status
));
673 UConverter
*cnv
= cnv_open(cnv_name
, status
);
674 if (U_FAILURE(status
)) {
675 dataerrln("Unable to open converter: %s - %s\n", cnv_name
, u_errorName(status
));
679 // set callback for the converter
680 ucnv_setFromUCallBack(cnv
, UCNV_FROM_U_CALLBACK_SUBSTITUTE
, NULL
, NULL
, NULL
, &status
);
684 int32_t outputLength
;
686 // test default ignorables are ignored
687 int size
= set_ignorable
->size();
688 for (int i
= 0; i
< size
; i
++) {
689 status
= U_ZERO_ERROR
;
692 input
[0] = set_ignorable
->charAt(i
);
694 outputLength
= ucnv_fromUChars(cnv
, output
, 10, UnicodeString::fromUTF32(input
, 1).getTerminatedBuffer(), -1, &status
);
695 if (U_FAILURE(status
) || outputLength
!= 0) {
696 errln("Ignorable code point: U+%04X not skipped as expected - %s", input
[0], u_errorName(status
));
700 // test non-ignorables are not ignored
701 size
= set_not_ignorable
->size();
702 for (int i
= 0; i
< size
; i
++) {
703 status
= U_ZERO_ERROR
;
706 input
[0] = set_not_ignorable
->charAt(i
);
712 outputLength
= ucnv_fromUChars(cnv
, output
, 10, UnicodeString::fromUTF32(input
, 1).getTerminatedBuffer(), -1, &status
);
713 if (U_FAILURE(status
) || outputLength
<= 0) {
714 errln("Non-ignorable code point: U+%04X skipped unexpectedly - %s", input
[0], u_errorName(status
));
719 delete set_not_ignorable
;
720 delete set_ignorable
;
723 // open testdata or ICU data converter ------------------------------------- ***
726 ConversionTest::cnv_open(const char *name
, UErrorCode
&errorCode
) {
727 if(name
!=NULL
&& *name
=='+') {
728 // Converter names that start with '+' are ignored in ICU4J tests.
731 if(name
!=NULL
&& *name
=='*') {
732 /* loadTestData(): set the data directory */
733 return ucnv_openPackage(loadTestData(errorCode
), name
+1, &errorCode
);
735 return ucnv_open(name
, &errorCode
);
739 // output helpers ---------------------------------------------------------- ***
742 hexDigit(uint8_t digit
) {
743 return digit
<=9 ? (char)('0'+digit
) : (char)('a'-10+digit
);
747 printBytes(const uint8_t *bytes
, int32_t length
, char *out
) {
753 *out
++=hexDigit((uint8_t)(b
>>4));
754 *out
++=hexDigit((uint8_t)(b
&0xf));
761 *out
++=hexDigit((uint8_t)(b
>>4));
762 *out
++=hexDigit((uint8_t)(b
&0xf));
769 printUnicode(const UChar
*unicode
, int32_t length
, char *out
) {
773 for(i
=0; i
<length
;) {
777 U16_NEXT(unicode
, i
, length
, c
);
783 *out
++=hexDigit((uint8_t)((c
>>16)&0xf));
785 *out
++=hexDigit((uint8_t)((c
>>12)&0xf));
786 *out
++=hexDigit((uint8_t)((c
>>8)&0xf));
787 *out
++=hexDigit((uint8_t)((c
>>4)&0xf));
788 *out
++=hexDigit((uint8_t)(c
&0xf));
795 printOffsets(const int32_t *offsets
, int32_t length
, char *out
) {
802 for(i
=0; i
<length
; ++i
) {
808 // print all offsets with 2 characters each (-x, -9..99, xx)
814 *out
++=(char)('0'-o
);
816 *out
++=(d
=o
/10)==0 ? ' ' : (char)('0'+d
);
817 *out
++=(char)('0'+o%10
);
827 // toUnicode test worker functions ----------------------------------------- ***
830 stepToUnicode(ConversionCase
&cc
, UConverter
*cnv
,
831 UChar
*result
, int32_t resultCapacity
,
832 int32_t *resultOffsets
, /* also resultCapacity */
834 UErrorCode
*pErrorCode
) {
835 const char *source
, *sourceLimit
, *bytesLimit
;
836 UChar
*target
, *targetLimit
, *resultLimit
;
839 source
=(const char *)cc
.bytes
;
841 bytesLimit
=source
+cc
.bytesLength
;
842 resultLimit
=result
+resultCapacity
;
845 // call ucnv_toUnicode() with in/out buffers no larger than (step) at a time
846 // move only one buffer (in vs. out) at a time to be extra mean
847 // step==0 performs bulk conversion and generates offsets
849 // initialize the partial limits for the loop
851 // use the entire buffers
852 sourceLimit
=bytesLimit
;
853 targetLimit
=resultLimit
;
856 // start with empty partial buffers
861 // output offsets only for bulk conversion
866 // resetting the opposite conversion direction must not affect this one
867 ucnv_resetFromUnicode(cnv
);
871 &target
, targetLimit
,
872 &source
, sourceLimit
,
876 // check pointers and errors
877 if(source
>sourceLimit
|| target
>targetLimit
) {
878 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
880 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
881 if(target
!=targetLimit
) {
882 // buffer overflow must only be set when the target is filled
883 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
885 } else if(targetLimit
==resultLimit
) {
886 // not just a partial overflow
890 // the partial target is filled, set a new limit, reset the error and continue
891 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
892 *pErrorCode
=U_ZERO_ERROR
;
893 } else if(U_FAILURE(*pErrorCode
)) {
894 // some other error occurred, done
897 if(source
!=sourceLimit
) {
898 // when no error occurs, then the input must be consumed
899 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
903 if(sourceLimit
==bytesLimit
) {
908 // the partial conversion succeeded, set a new limit and continue
909 sourceLimit
=(bytesLimit
-source
)>=step
? source
+step
: bytesLimit
;
910 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==bytesLimit
);
913 } else /* step<0 */ {
915 * step==-1: call only ucnv_getNextUChar()
916 * otherwise alternate between ucnv_toUnicode() and ucnv_getNextUChar()
917 * if step==-2 or -3, then give ucnv_toUnicode() the whole remaining input,
918 * else give it at most (-step-2)/2 bytes
922 // end the loop by getting an index out of bounds error
924 // resetting the opposite conversion direction must not affect this one
925 ucnv_resetFromUnicode(cnv
);
928 if((step
&1)!=0 /* odd: -1, -3, -5, ... */) {
929 sourceLimit
=source
; // use sourceLimit not as a real limit
930 // but to remember the pre-getNextUChar source pointer
931 c
=ucnv_getNextUChar(cnv
, &source
, bytesLimit
, pErrorCode
);
933 // check pointers and errors
934 if(*pErrorCode
==U_INDEX_OUTOFBOUNDS_ERROR
) {
935 if(source
!=bytesLimit
) {
936 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
938 *pErrorCode
=U_ZERO_ERROR
;
941 } else if(U_FAILURE(*pErrorCode
)) {
944 // source may not move if c is from previous overflow
946 if(target
==resultLimit
) {
947 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
953 *target
++=U16_LEAD(c
);
954 if(target
==resultLimit
) {
955 *pErrorCode
=U_BUFFER_OVERFLOW_ERROR
;
958 *target
++=U16_TRAIL(c
);
961 // alternate between -n-1 and -n but leave -1 alone
965 } else /* step is even */ {
966 // allow only one UChar output
967 targetLimit
=target
<resultLimit
? target
+1 : resultLimit
;
969 // as with ucnv_getNextUChar(), we always flush (if we go to bytesLimit)
970 // and never output offsets
972 sourceLimit
=bytesLimit
;
974 sourceLimit
=source
+(-step
-2)/2;
975 if(sourceLimit
>bytesLimit
) {
976 sourceLimit
=bytesLimit
;
981 &target
, targetLimit
,
982 &source
, sourceLimit
,
983 NULL
, (UBool
)(sourceLimit
==bytesLimit
), pErrorCode
);
985 // check pointers and errors
986 if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
987 if(target
!=targetLimit
) {
988 // buffer overflow must only be set when the target is filled
989 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
991 } else if(targetLimit
==resultLimit
) {
992 // not just a partial overflow
996 // the partial target is filled, set a new limit and continue
997 *pErrorCode
=U_ZERO_ERROR
;
998 } else if(U_FAILURE(*pErrorCode
)) {
999 // some other error occurred, done
1002 if(source
!=sourceLimit
) {
1003 // when no error occurs, then the input must be consumed
1004 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1008 // we are done (flush==TRUE) but we continue, to get the index out of bounds error above
1016 return (int32_t)(target
-result
);
1020 ConversionTest::ToUnicodeCase(ConversionCase
&cc
, UConverterToUCallback callback
, const char *option
) {
1021 // open the converter
1022 IcuTestErrorCode
errorCode(*this, "ToUnicodeCase");
1023 LocalUConverterPointer
cnv(cnv_open(cc
.charset
, errorCode
));
1024 if(errorCode
.isFailure()) {
1025 errcheckln(errorCode
, "toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
1026 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, errorCode
.errorName());
1032 if(callback
!=NULL
) {
1033 ucnv_setToUCallBack(cnv
.getAlias(), callback
, option
, NULL
, NULL
, errorCode
);
1034 if(U_FAILURE(errorCode
)) {
1035 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setToUCallBack() failed - %s",
1036 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1041 int32_t resultOffsets
[256];
1043 int32_t resultLength
;
1046 static const struct {
1050 { 0, "bulk" }, // must be first for offsets to be checked
1055 { -2, "toU(bulk)+getNext" },
1056 { -3, "getNext+toU(bulk)" },
1057 { -4, "toU(1)+getNext" },
1058 { -5, "getNext+toU(1)" },
1059 { -12, "toU(5)+getNext" },
1060 { -13, "getNext+toU(5)" },
1065 for(i
=0; i
<UPRV_LENGTHOF(steps
) && ok
; ++i
) {
1067 if(step
<0 && !cc
.finalFlush
) {
1068 // skip ucnv_getNextUChar() if !finalFlush because
1069 // ucnv_getNextUChar() always implies flush
1073 // bulk test is first, then offsets are not checked any more
1077 memset(resultOffsets
, -1, UPRV_LENGTHOF(resultOffsets
));
1079 memset(result
, -1, UPRV_LENGTHOF(result
));
1081 resultLength
=stepToUnicode(cc
, cnv
.getAlias(),
1082 result
, UPRV_LENGTHOF(result
),
1083 step
==0 ? resultOffsets
: NULL
,
1086 cc
, cnv
.getAlias(), steps
[i
].name
,
1087 result
, resultLength
,
1088 cc
.offsets
!=NULL
? resultOffsets
: NULL
,
1090 if(errorCode
.isFailure() || !cc
.finalFlush
) {
1091 // reset if an error occurred or we did not flush
1092 // otherwise do nothing to make sure that flushing resets
1093 ucnv_resetToUnicode(cnv
.getAlias());
1095 if (cc
.offsets
!= NULL
&& resultOffsets
[resultLength
] != -1) {
1096 errln("toUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
1097 cc
.caseNr
, cc
.charset
, resultLength
);
1099 if (result
[resultLength
] != (UChar
)-1) {
1100 errln("toUnicode[%d](%s) Conversion wrote too much to result at index %d",
1101 cc
.caseNr
, cc
.charset
, resultLength
);
1105 // not a real loop, just a convenience for breaking out of the block
1106 while(ok
&& cc
.finalFlush
) {
1107 // test ucnv_toUChars()
1108 memset(result
, 0, sizeof(result
));
1111 resultLength
=ucnv_toUChars(cnv
.getAlias(),
1112 result
, UPRV_LENGTHOF(result
),
1113 (const char *)cc
.bytes
, cc
.bytesLength
,
1116 cc
, cnv
.getAlias(), "toUChars",
1117 result
, resultLength
,
1124 // test preflighting
1125 // keep the correct result for simple checking
1127 resultLength
=ucnv_toUChars(cnv
.getAlias(),
1129 (const char *)cc
.bytes
, cc
.bytesLength
,
1131 if(errorCode
.get()==U_STRING_NOT_TERMINATED_WARNING
|| errorCode
.get()==U_BUFFER_OVERFLOW_ERROR
) {
1135 cc
, cnv
.getAlias(), "preflight toUChars",
1136 result
, resultLength
,
1142 errorCode
.reset(); // all errors have already been reported
1147 ConversionTest::checkToUnicode(ConversionCase
&cc
, UConverter
*cnv
, const char *name
,
1148 const UChar
*result
, int32_t resultLength
,
1149 const int32_t *resultOffsets
,
1150 UErrorCode resultErrorCode
) {
1151 char resultInvalidChars
[8];
1152 int8_t resultInvalidLength
;
1153 UErrorCode errorCode
;
1157 // reset the message; NULL will mean "ok"
1160 errorCode
=U_ZERO_ERROR
;
1161 resultInvalidLength
=sizeof(resultInvalidChars
);
1162 ucnv_getInvalidChars(cnv
, resultInvalidChars
, &resultInvalidLength
, &errorCode
);
1163 if(U_FAILURE(errorCode
)) {
1164 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidChars() failed - %s",
1165 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, u_errorName(errorCode
));
1169 // check everything that might have gone wrong
1170 if(cc
.unicodeLength
!=resultLength
) {
1171 msg
="wrong result length";
1172 } else if(0!=u_memcmp(cc
.unicode
, result
, cc
.unicodeLength
)) {
1173 msg
="wrong result string";
1174 } else if(cc
.offsets
!=NULL
&& 0!=memcmp(cc
.offsets
, resultOffsets
, cc
.unicodeLength
*sizeof(*cc
.offsets
))) {
1175 msg
="wrong offsets";
1176 } else if(cc
.outErrorCode
!=resultErrorCode
) {
1177 msg
="wrong error code";
1178 } else if(cc
.invalidLength
!=resultInvalidLength
) {
1179 msg
="wrong length of last invalid input";
1180 } else if(0!=memcmp(cc
.invalidChars
, resultInvalidChars
, cc
.invalidLength
)) {
1181 msg
="wrong last invalid input";
1187 char buffer
[2000]; // one buffer for all strings
1188 char *s
, *bytesString
, *unicodeString
, *resultString
,
1189 *offsetsString
, *resultOffsetsString
,
1190 *invalidCharsString
, *resultInvalidCharsString
;
1192 bytesString
=s
=buffer
;
1193 s
=printBytes(cc
.bytes
, cc
.bytesLength
, bytesString
);
1194 s
=printUnicode(cc
.unicode
, cc
.unicodeLength
, unicodeString
=s
);
1195 s
=printUnicode(result
, resultLength
, resultString
=s
);
1196 s
=printOffsets(cc
.offsets
, cc
.unicodeLength
, offsetsString
=s
);
1197 s
=printOffsets(resultOffsets
, resultLength
, resultOffsetsString
=s
);
1198 s
=printBytes(cc
.invalidChars
, cc
.invalidLength
, invalidCharsString
=s
);
1199 s
=printBytes((uint8_t *)resultInvalidChars
, resultInvalidLength
, resultInvalidCharsString
=s
);
1201 if((s
-buffer
)>(int32_t)sizeof(buffer
)) {
1202 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkToUnicode() test output buffer overflow writing %d chars\n",
1203 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, (int)(s
-buffer
));
1207 errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
1209 " expected <%s>[%d]\n"
1210 " result <%s>[%d]\n"
1212 " result offsets <%s>\n"
1213 " error code expected %s got %s\n"
1214 " invalidChars expected <%s> got <%s>\n",
1215 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, msg
,
1216 bytesString
, cc
.bytesLength
,
1217 unicodeString
, cc
.unicodeLength
,
1218 resultString
, resultLength
,
1220 resultOffsetsString
,
1221 u_errorName(cc
.outErrorCode
), u_errorName(resultErrorCode
),
1222 invalidCharsString
, resultInvalidCharsString
);
1228 // fromUnicode test worker functions --------------------------------------- ***
1231 stepFromUTF8(ConversionCase
&cc
,
1232 UConverter
*utf8Cnv
, UConverter
*cnv
,
1233 char *result
, int32_t resultCapacity
,
1235 UErrorCode
*pErrorCode
) {
1236 const char *source
, *sourceLimit
, *utf8Limit
;
1237 UChar pivotBuffer
[32];
1238 UChar
*pivotSource
, *pivotTarget
, *pivotLimit
;
1239 char *target
, *targetLimit
, *resultLimit
;
1243 pivotSource
=pivotTarget
=pivotBuffer
;
1245 utf8Limit
=source
+cc
.utf8Length
;
1246 resultLimit
=result
+resultCapacity
;
1248 // call ucnv_convertEx() with in/out buffers no larger than (step) at a time
1249 // move only one buffer (in vs. out) at a time to be extra mean
1250 // step==0 performs bulk conversion
1252 // initialize the partial limits for the loop
1254 // use the entire buffers
1255 sourceLimit
=utf8Limit
;
1256 targetLimit
=resultLimit
;
1257 flush
=cc
.finalFlush
;
1259 pivotLimit
=pivotBuffer
+UPRV_LENGTHOF(pivotBuffer
);
1261 // start with empty partial buffers
1266 // empty pivot is not allowed, make it of length step
1267 pivotLimit
=pivotBuffer
+step
;
1271 // resetting the opposite conversion direction must not affect this one
1272 ucnv_resetFromUnicode(utf8Cnv
);
1273 ucnv_resetToUnicode(cnv
);
1276 ucnv_convertEx(cnv
, utf8Cnv
,
1277 &target
, targetLimit
,
1278 &source
, sourceLimit
,
1279 pivotBuffer
, &pivotSource
, &pivotTarget
, pivotLimit
,
1280 FALSE
, flush
, pErrorCode
);
1282 // check pointers and errors
1283 if(source
>sourceLimit
|| target
>targetLimit
) {
1284 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1286 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1287 if(target
!=targetLimit
) {
1288 // buffer overflow must only be set when the target is filled
1289 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1291 } else if(targetLimit
==resultLimit
) {
1292 // not just a partial overflow
1296 // the partial target is filled, set a new limit, reset the error and continue
1297 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
1298 *pErrorCode
=U_ZERO_ERROR
;
1299 } else if(U_FAILURE(*pErrorCode
)) {
1300 if(pivotSource
==pivotBuffer
) {
1301 // toUnicode error, should not occur
1302 // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
1305 // fromUnicode error
1306 // some other error occurred, done
1310 if(source
!=sourceLimit
) {
1311 // when no error occurs, then the input must be consumed
1312 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1316 if(sourceLimit
==utf8Limit
) {
1318 if(*pErrorCode
==U_STRING_NOT_TERMINATED_WARNING
) {
1319 // ucnv_convertEx() warns about not terminating the output
1320 // but ucnv_fromUnicode() does not and so
1321 // checkFromUnicode() does not expect it
1322 *pErrorCode
=U_ZERO_ERROR
;
1327 // the partial conversion succeeded, set a new limit and continue
1328 sourceLimit
=(utf8Limit
-source
)>=step
? source
+step
: utf8Limit
;
1329 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==utf8Limit
);
1333 return (int32_t)(target
-result
);
1337 stepFromUnicode(ConversionCase
&cc
, UConverter
*cnv
,
1338 char *result
, int32_t resultCapacity
,
1339 int32_t *resultOffsets
, /* also resultCapacity */
1341 UErrorCode
*pErrorCode
) {
1342 const UChar
*source
, *sourceLimit
, *unicodeLimit
;
1343 char *target
, *targetLimit
, *resultLimit
;
1348 unicodeLimit
=source
+cc
.unicodeLength
;
1349 resultLimit
=result
+resultCapacity
;
1351 // call ucnv_fromUnicode() with in/out buffers no larger than (step) at a time
1352 // move only one buffer (in vs. out) at a time to be extra mean
1353 // step==0 performs bulk conversion and generates offsets
1355 // initialize the partial limits for the loop
1357 // use the entire buffers
1358 sourceLimit
=unicodeLimit
;
1359 targetLimit
=resultLimit
;
1360 flush
=cc
.finalFlush
;
1362 // start with empty partial buffers
1367 // output offsets only for bulk conversion
1372 // resetting the opposite conversion direction must not affect this one
1373 ucnv_resetToUnicode(cnv
);
1376 ucnv_fromUnicode(cnv
,
1377 &target
, targetLimit
,
1378 &source
, sourceLimit
,
1382 // check pointers and errors
1383 if(source
>sourceLimit
|| target
>targetLimit
) {
1384 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1386 } else if(*pErrorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1387 if(target
!=targetLimit
) {
1388 // buffer overflow must only be set when the target is filled
1389 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1391 } else if(targetLimit
==resultLimit
) {
1392 // not just a partial overflow
1396 // the partial target is filled, set a new limit, reset the error and continue
1397 targetLimit
=(resultLimit
-target
)>=step
? target
+step
: resultLimit
;
1398 *pErrorCode
=U_ZERO_ERROR
;
1399 } else if(U_FAILURE(*pErrorCode
)) {
1400 // some other error occurred, done
1403 if(source
!=sourceLimit
) {
1404 // when no error occurs, then the input must be consumed
1405 *pErrorCode
=U_INTERNAL_PROGRAM_ERROR
;
1409 if(sourceLimit
==unicodeLimit
) {
1414 // the partial conversion succeeded, set a new limit and continue
1415 sourceLimit
=(unicodeLimit
-source
)>=step
? source
+step
: unicodeLimit
;
1416 flush
=(UBool
)(cc
.finalFlush
&& sourceLimit
==unicodeLimit
);
1420 return (int32_t)(target
-result
);
1424 ConversionTest::FromUnicodeCase(ConversionCase
&cc
, UConverterFromUCallback callback
, const char *option
) {
1426 UErrorCode errorCode
;
1428 // open the converter
1429 errorCode
=U_ZERO_ERROR
;
1430 cnv
=cnv_open(cc
.charset
, errorCode
);
1431 if(U_FAILURE(errorCode
)) {
1432 errcheckln(errorCode
, "fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
1433 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1436 ucnv_resetToUnicode(utf8Cnv
);
1439 if(callback
!=NULL
) {
1440 ucnv_setFromUCallBack(cnv
, callback
, option
, NULL
, NULL
, &errorCode
);
1441 if(U_FAILURE(errorCode
)) {
1442 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setFromUCallBack() failed - %s",
1443 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1449 // set the fallbacks flag
1450 // TODO change with Jitterbug 2401, then add a similar call for toUnicode too
1451 ucnv_setFallback(cnv
, cc
.fallbacks
);
1457 length
=(int32_t)strlen(cc
.subchar
);
1458 ucnv_setSubstChars(cnv
, cc
.subchar
, (int8_t)length
, &errorCode
);
1459 if(U_FAILURE(errorCode
)) {
1460 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstChars() failed - %s",
1461 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1465 } else if(cc
.setSub
<0) {
1466 ucnv_setSubstString(cnv
, cc
.subString
, -1, &errorCode
);
1467 if(U_FAILURE(errorCode
)) {
1468 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstString() failed - %s",
1469 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, u_errorName(errorCode
));
1475 // convert unicode to utf8
1478 u_strToUTF8(utf8
, UPRV_LENGTHOF(utf8
), &cc
.utf8Length
,
1479 cc
.unicode
, cc
.unicodeLength
,
1481 if(U_FAILURE(errorCode
)) {
1482 // skip UTF-8 testing of a string with an unpaired surrogate,
1483 // or of one that's too long
1484 // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
1488 int32_t resultOffsets
[256];
1490 int32_t resultLength
;
1493 static const struct {
1495 const char *name
, *utf8Name
;
1497 { 0, "bulk", "utf8" }, // must be first for offsets to be checked
1498 { 1, "step=1", "utf8 step=1" },
1499 { 3, "step=3", "utf8 step=3" },
1500 { 7, "step=7", "utf8 step=7" }
1505 for(i
=0; i
<UPRV_LENGTHOF(steps
) && ok
; ++i
) {
1507 memset(resultOffsets
, -1, UPRV_LENGTHOF(resultOffsets
));
1508 memset(result
, -1, UPRV_LENGTHOF(result
));
1509 errorCode
=U_ZERO_ERROR
;
1510 resultLength
=stepFromUnicode(cc
, cnv
,
1511 result
, UPRV_LENGTHOF(result
),
1512 step
==0 ? resultOffsets
: NULL
,
1514 ok
=checkFromUnicode(
1515 cc
, cnv
, steps
[i
].name
,
1516 (uint8_t *)result
, resultLength
,
1517 cc
.offsets
!=NULL
? resultOffsets
: NULL
,
1519 if(U_FAILURE(errorCode
) || !cc
.finalFlush
) {
1520 // reset if an error occurred or we did not flush
1521 // otherwise do nothing to make sure that flushing resets
1522 ucnv_resetFromUnicode(cnv
);
1524 if (resultOffsets
[resultLength
] != -1) {
1525 errln("fromUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
1526 cc
.caseNr
, cc
.charset
, resultLength
);
1528 if (result
[resultLength
] != (char)-1) {
1529 errln("fromUnicode[%d](%s) Conversion wrote too much to result at index %d",
1530 cc
.caseNr
, cc
.charset
, resultLength
);
1533 // bulk test is first, then offsets are not checked any more
1536 // test direct conversion from UTF-8
1537 if(cc
.utf8Length
>=0) {
1538 errorCode
=U_ZERO_ERROR
;
1539 resultLength
=stepFromUTF8(cc
, utf8Cnv
, cnv
,
1540 result
, UPRV_LENGTHOF(result
),
1542 ok
=checkFromUnicode(
1543 cc
, cnv
, steps
[i
].utf8Name
,
1544 (uint8_t *)result
, resultLength
,
1547 if(U_FAILURE(errorCode
) || !cc
.finalFlush
) {
1548 // reset if an error occurred or we did not flush
1549 // otherwise do nothing to make sure that flushing resets
1550 ucnv_resetToUnicode(utf8Cnv
);
1551 ucnv_resetFromUnicode(cnv
);
1556 // not a real loop, just a convenience for breaking out of the block
1557 while(ok
&& cc
.finalFlush
) {
1558 // test ucnv_fromUChars()
1559 memset(result
, 0, sizeof(result
));
1561 errorCode
=U_ZERO_ERROR
;
1562 resultLength
=ucnv_fromUChars(cnv
,
1563 result
, UPRV_LENGTHOF(result
),
1564 cc
.unicode
, cc
.unicodeLength
,
1566 ok
=checkFromUnicode(
1567 cc
, cnv
, "fromUChars",
1568 (uint8_t *)result
, resultLength
,
1575 // test preflighting
1576 // keep the correct result for simple checking
1577 errorCode
=U_ZERO_ERROR
;
1578 resultLength
=ucnv_fromUChars(cnv
,
1580 cc
.unicode
, cc
.unicodeLength
,
1582 if(errorCode
==U_STRING_NOT_TERMINATED_WARNING
|| errorCode
==U_BUFFER_OVERFLOW_ERROR
) {
1583 errorCode
=U_ZERO_ERROR
;
1585 ok
=checkFromUnicode(
1586 cc
, cnv
, "preflight fromUChars",
1587 (uint8_t *)result
, resultLength
,
1598 ConversionTest::checkFromUnicode(ConversionCase
&cc
, UConverter
*cnv
, const char *name
,
1599 const uint8_t *result
, int32_t resultLength
,
1600 const int32_t *resultOffsets
,
1601 UErrorCode resultErrorCode
) {
1602 UChar resultInvalidUChars
[8];
1603 int8_t resultInvalidLength
;
1604 UErrorCode errorCode
;
1608 // reset the message; NULL will mean "ok"
1611 errorCode
=U_ZERO_ERROR
;
1612 resultInvalidLength
=UPRV_LENGTHOF(resultInvalidUChars
);
1613 ucnv_getInvalidUChars(cnv
, resultInvalidUChars
, &resultInvalidLength
, &errorCode
);
1614 if(U_FAILURE(errorCode
)) {
1615 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUChars() failed - %s",
1616 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, u_errorName(errorCode
));
1620 // check everything that might have gone wrong
1621 if(cc
.bytesLength
!=resultLength
) {
1622 msg
="wrong result length";
1623 } else if(0!=memcmp(cc
.bytes
, result
, cc
.bytesLength
)) {
1624 msg
="wrong result string";
1625 } else if(cc
.offsets
!=NULL
&& 0!=memcmp(cc
.offsets
, resultOffsets
, cc
.bytesLength
*sizeof(*cc
.offsets
))) {
1626 msg
="wrong offsets";
1627 } else if(cc
.outErrorCode
!=resultErrorCode
) {
1628 msg
="wrong error code";
1629 } else if(cc
.invalidLength
!=resultInvalidLength
) {
1630 msg
="wrong length of last invalid input";
1631 } else if(0!=u_memcmp(cc
.invalidUChars
, resultInvalidUChars
, cc
.invalidLength
)) {
1632 msg
="wrong last invalid input";
1638 char buffer
[2000]; // one buffer for all strings
1639 char *s
, *unicodeString
, *bytesString
, *resultString
,
1640 *offsetsString
, *resultOffsetsString
,
1641 *invalidCharsString
, *resultInvalidUCharsString
;
1643 unicodeString
=s
=buffer
;
1644 s
=printUnicode(cc
.unicode
, cc
.unicodeLength
, unicodeString
);
1645 s
=printBytes(cc
.bytes
, cc
.bytesLength
, bytesString
=s
);
1646 s
=printBytes(result
, resultLength
, resultString
=s
);
1647 s
=printOffsets(cc
.offsets
, cc
.bytesLength
, offsetsString
=s
);
1648 s
=printOffsets(resultOffsets
, resultLength
, resultOffsetsString
=s
);
1649 s
=printUnicode(cc
.invalidUChars
, cc
.invalidLength
, invalidCharsString
=s
);
1650 s
=printUnicode(resultInvalidUChars
, resultInvalidLength
, resultInvalidUCharsString
=s
);
1652 if((s
-buffer
)>(int32_t)sizeof(buffer
)) {
1653 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkFromUnicode() test output buffer overflow writing %d chars\n",
1654 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, (int)(s
-buffer
));
1658 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
1659 " unicode <%s>[%d]\n"
1660 " expected <%s>[%d]\n"
1661 " result <%s>[%d]\n"
1663 " result offsets <%s>\n"
1664 " error code expected %s got %s\n"
1665 " invalidChars expected <%s> got <%s>\n",
1666 cc
.caseNr
, cc
.charset
, cc
.cbopt
, cc
.fallbacks
, cc
.finalFlush
, name
, msg
,
1667 unicodeString
, cc
.unicodeLength
,
1668 bytesString
, cc
.bytesLength
,
1669 resultString
, resultLength
,
1671 resultOffsetsString
,
1672 u_errorName(cc
.outErrorCode
), u_errorName(resultErrorCode
),
1673 invalidCharsString
, resultInvalidUCharsString
);
1679 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */