Home | History | Annotate | Download | only in intltest
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 /*
      4 *******************************************************************************
      5 *
      6 *   Copyright (C) 2003-2014, International Business Machines
      7 *   Corporation and others.  All Rights Reserved.
      8 *
      9 *******************************************************************************
     10 *   file name:  convtest.cpp
     11 *   encoding:   UTF-8
     12 *   tab size:   8 (not used)
     13 *   indentation:4
     14 *
     15 *   created on: 2003jul15
     16 *   created by: Markus W. Scherer
     17 *
     18 *   Test file for data-driven conversion tests.
     19 */
     20 
     21 #include "unicode/utypes.h"
     22 
     23 #if !UCONFIG_NO_LEGACY_CONVERSION
     24 /*
     25  * Note: Turning off all of convtest.cpp if !UCONFIG_NO_LEGACY_CONVERSION
     26  * is slightly unnecessary - it removes tests for Unicode charsets
     27  * like UTF-8 that should work.
     28  * However, there is no easy way for the test to detect whether a test case
     29  * is for a Unicode charset, so it would be difficult to only exclude those.
     30  * Also, regular testing of ICU is done with all modules on, therefore
     31  * not testing conversion for a custom configuration like this should be ok.
     32  */
     33 
     34 #include "unicode/ucnv.h"
     35 #include "unicode/unistr.h"
     36 #include "unicode/parsepos.h"
     37 #include "unicode/uniset.h"
     38 #include "unicode/ustring.h"
     39 #include "unicode/ures.h"
     40 #include "unicode/utf16.h"
     41 #include "convtest.h"
     42 #include "cmemory.h"
     43 #include "unicode/tstdtmod.h"
     44 #include <string.h>
     45 #include <stdlib.h>
     46 
     47 enum {
     48     // characters used in test data for callbacks
     49     SUB_CB='?',
     50     SKIP_CB='0',
     51     STOP_CB='.',
     52     ESC_CB='&'
     53 };
     54 
     55 ConversionTest::ConversionTest() {
     56     UErrorCode errorCode=U_ZERO_ERROR;
     57     utf8Cnv=ucnv_open("UTF-8", &errorCode);
     58     ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &errorCode);
     59     if(U_FAILURE(errorCode)) {
     60         errln("unable to open UTF-8 converter");
     61     }
     62 }
     63 
     64 ConversionTest::~ConversionTest() {
     65     ucnv_close(utf8Cnv);
     66 }
     67 
     68 void
     69 ConversionTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
     70     if (exec) logln("TestSuite ConversionTest: ");
     71     TESTCASE_AUTO_BEGIN;
     72 #if !UCONFIG_NO_FILE_IO
     73     TESTCASE_AUTO(TestToUnicode);
     74     TESTCASE_AUTO(TestFromUnicode);
     75     TESTCASE_AUTO(TestGetUnicodeSet);
     76 #endif
     77     TESTCASE_AUTO(TestGetUnicodeSet2);
     78     TESTCASE_AUTO(TestDefaultIgnorableCallback);
     79     TESTCASE_AUTO(TestUTF8ToUTF8Overflow);
     80     TESTCASE_AUTO_END;
     81 }
     82 
     83 // test data interface ----------------------------------------------------- ***
     84 
     85 void
     86 ConversionTest::TestToUnicode() {
     87     ConversionCase cc;
     88     char charset[100], cbopt[4];
     89     const char *option;
     90     UnicodeString s, unicode;
     91     int32_t offsetsLength;
     92     UConverterToUCallback callback;
     93 
     94     TestDataModule *dataModule;
     95     TestData *testData;
     96     const DataMap *testCase;
     97     UErrorCode errorCode;
     98     int32_t i;
     99 
    100     errorCode=U_ZERO_ERROR;
    101     dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
    102     if(U_SUCCESS(errorCode)) {
    103         testData=dataModule->createTestData("toUnicode", errorCode);
    104         if(U_SUCCESS(errorCode)) {
    105             for(i=0; testData->nextCase(testCase, errorCode); ++i) {
    106                 if(U_FAILURE(errorCode)) {
    107                     errln("error retrieving conversion/toUnicode test case %d - %s",
    108                             i, u_errorName(errorCode));
    109                     errorCode=U_ZERO_ERROR;
    110                     continue;
    111                 }
    112 
    113                 cc.caseNr=i;
    114 
    115                 s=testCase->getString("charset", errorCode);
    116                 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
    117                 cc.charset=charset;
    118 
    119                 // BEGIN android-added
    120                 // To save space, Android does not build full ISO-2022-CN tables.
    121                 // We skip the TestGetKeywordValuesForLocale for counting available collations.
    122                 if (strlen(charset) >= 8 &&
    123                     strncmp(charset+4, "2022-CN", 4) == 0) {
    124                     continue;
    125                 }
    126                 // END android-added
    127 
    128                 cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
    129                 unicode=testCase->getString("unicode", errorCode);
    130                 cc.unicode=unicode.getBuffer();
    131                 cc.unicodeLength=unicode.length();
    132 
    133                 offsetsLength=0;
    134                 cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
    135                 if(offsetsLength==0) {
    136                     cc.offsets=NULL;
    137                 } else if(offsetsLength!=unicode.length()) {
    138                     errln("toUnicode[%d] unicode[%d] and offsets[%d] must have the same length",
    139                             i, unicode.length(), offsetsLength);
    140                     errorCode=U_ILLEGAL_ARGUMENT_ERROR;
    141                 }
    142 
    143                 cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
    144                 cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
    145 
    146                 s=testCase->getString("errorCode", errorCode);
    147                 if(s==UNICODE_STRING("invalid", 7)) {
    148                     cc.outErrorCode=U_INVALID_CHAR_FOUND;
    149                 } else if(s==UNICODE_STRING("illegal", 7)) {
    150                     cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
    151                 } else if(s==UNICODE_STRING("truncated", 9)) {
    152                     cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
    153                 } else if(s==UNICODE_STRING("illesc", 6)) {
    154                     cc.outErrorCode=U_ILLEGAL_ESCAPE_SEQUENCE;
    155                 } else if(s==UNICODE_STRING("unsuppesc", 9)) {
    156                     cc.outErrorCode=U_UNSUPPORTED_ESCAPE_SEQUENCE;
    157                 } else {
    158                     cc.outErrorCode=U_ZERO_ERROR;
    159                 }
    160 
    161                 s=testCase->getString("callback", errorCode);
    162                 s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
    163                 cc.cbopt=cbopt;
    164                 switch(cbopt[0]) {
    165                 case SUB_CB:
    166                     callback=UCNV_TO_U_CALLBACK_SUBSTITUTE;
    167                     break;
    168                 case SKIP_CB:
    169                     callback=UCNV_TO_U_CALLBACK_SKIP;
    170                     break;
    171                 case STOP_CB:
    172                     callback=UCNV_TO_U_CALLBACK_STOP;
    173                     break;
    174                 case ESC_CB:
    175                     callback=UCNV_TO_U_CALLBACK_ESCAPE;
    176                     break;
    177                 default:
    178                     callback=NULL;
    179                     break;
    180                 }
    181                 option=callback==NULL ? cbopt : cbopt+1;
    182                 if(*option==0) {
    183                     option=NULL;
    184                 }
    185 
    186                 cc.invalidChars=testCase->getBinary(cc.invalidLength, "invalidChars", errorCode);
    187 
    188                 if(U_FAILURE(errorCode)) {
    189                     errln("error parsing conversion/toUnicode test case %d - %s",
    190                             i, u_errorName(errorCode));
    191                     errorCode=U_ZERO_ERROR;
    192                 } else {
    193                     logln("TestToUnicode[%d] %s", i, charset);
    194                     ToUnicodeCase(cc, callback, option);
    195                 }
    196             }
    197             delete testData;
    198         }
    199         delete dataModule;
    200     }
    201     else {
    202         dataerrln("Could not load test conversion data");
    203     }
    204 }
    205 
    206 void
    207 ConversionTest::TestFromUnicode() {
    208     ConversionCase cc;
    209     char charset[100], cbopt[4];
    210     const char *option;
    211     UnicodeString s, unicode, invalidUChars;
    212     int32_t offsetsLength, index;
    213     UConverterFromUCallback callback;
    214 
    215     TestDataModule *dataModule;
    216     TestData *testData;
    217     const DataMap *testCase;
    218     const UChar *p;
    219     UErrorCode errorCode;
    220     int32_t i, length;
    221 
    222     errorCode=U_ZERO_ERROR;
    223     dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
    224     if(U_SUCCESS(errorCode)) {
    225         testData=dataModule->createTestData("fromUnicode", errorCode);
    226         if(U_SUCCESS(errorCode)) {
    227             for(i=0; testData->nextCase(testCase, errorCode); ++i) {
    228                 if(U_FAILURE(errorCode)) {
    229                     errln("error retrieving conversion/fromUnicode test case %d - %s",
    230                             i, u_errorName(errorCode));
    231                     errorCode=U_ZERO_ERROR;
    232                     continue;
    233                 }
    234 
    235                 cc.caseNr=i;
    236 
    237                 s=testCase->getString("charset", errorCode);
    238                 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
    239                 cc.charset=charset;
    240 
    241                 // BEGIN android-added
    242                 // To save space, Android does not build full ISO-2022-CN tables.
    243                 // We skip the TestGetKeywordValuesForLocale for counting available collations.
    244                 if (strlen(charset) >= 8 &&
    245                     strncmp(charset+4, "2022-CN", 4) == 0) {
    246                     continue;
    247                 }
    248                 // END android-added
    249 
    250                 unicode=testCase->getString("unicode", errorCode);
    251                 cc.unicode=unicode.getBuffer();
    252                 cc.unicodeLength=unicode.length();
    253                 cc.bytes=testCase->getBinary(cc.bytesLength, "bytes", errorCode);
    254 
    255                 offsetsLength=0;
    256                 cc.offsets=testCase->getIntVector(offsetsLength, "offsets", errorCode);
    257                 if(offsetsLength==0) {
    258                     cc.offsets=NULL;
    259                 } else if(offsetsLength!=cc.bytesLength) {
    260                     errln("fromUnicode[%d] bytes[%d] and offsets[%d] must have the same length",
    261                             i, cc.bytesLength, offsetsLength);
    262                     errorCode=U_ILLEGAL_ARGUMENT_ERROR;
    263                 }
    264 
    265                 cc.finalFlush= 0!=testCase->getInt28("flush", errorCode);
    266                 cc.fallbacks= 0!=testCase->getInt28("fallbacks", errorCode);
    267 
    268                 s=testCase->getString("errorCode", errorCode);
    269                 if(s==UNICODE_STRING("invalid", 7)) {
    270                     cc.outErrorCode=U_INVALID_CHAR_FOUND;
    271                 } else if(s==UNICODE_STRING("illegal", 7)) {
    272                     cc.outErrorCode=U_ILLEGAL_CHAR_FOUND;
    273                 } else if(s==UNICODE_STRING("truncated", 9)) {
    274                     cc.outErrorCode=U_TRUNCATED_CHAR_FOUND;
    275                 } else {
    276                     cc.outErrorCode=U_ZERO_ERROR;
    277                 }
    278 
    279                 s=testCase->getString("callback", errorCode);
    280                 cc.setSub=0; // default: no subchar
    281 
    282                 if((index=s.indexOf((UChar)0))>0) {
    283                     // read NUL-separated subchar first, if any
    284                     // copy the subchar from Latin-1 characters
    285                     // start after the NUL
    286                     p=s.getTerminatedBuffer();
    287                     length=index+1;
    288                     p+=length;
    289                     length=s.length()-length;
    290                     if(length<=0 || length>=(int32_t)sizeof(cc.subchar)) {
    291                         errorCode=U_ILLEGAL_ARGUMENT_ERROR;
    292                     } else {
    293                         int32_t j;
    294 
    295                         for(j=0; j<length; ++j) {
    296                             cc.subchar[j]=(char)p[j];
    297                         }
    298                         // NUL-terminate the subchar
    299                         cc.subchar[j]=0;
    300                         cc.setSub=1;
    301                     }
    302 
    303                     // remove the NUL and subchar from s
    304                     s.truncate(index);
    305                 } else if((index=s.indexOf((UChar)0x3d))>0) /* '=' */ {
    306                     // read a substitution string, separated by an equal sign
    307                     p=s.getBuffer()+index+1;
    308                     length=s.length()-(index+1);
    309                     if(length<0 || length>=UPRV_LENGTHOF(cc.subString)) {
    310                         errorCode=U_ILLEGAL_ARGUMENT_ERROR;
    311                     } else {
    312                         u_memcpy(cc.subString, p, length);
    313                         // NUL-terminate the subString
    314                         cc.subString[length]=0;
    315                         cc.setSub=-1;
    316                     }
    317 
    318                     // remove the equal sign and subString from s
    319                     s.truncate(index);
    320                 }
    321 
    322                 s.extract(0, 0x7fffffff, cbopt, sizeof(cbopt), "");
    323                 cc.cbopt=cbopt;
    324                 switch(cbopt[0]) {
    325                 case SUB_CB:
    326                     callback=UCNV_FROM_U_CALLBACK_SUBSTITUTE;
    327                     break;
    328                 case SKIP_CB:
    329                     callback=UCNV_FROM_U_CALLBACK_SKIP;
    330                     break;
    331                 case STOP_CB:
    332                     callback=UCNV_FROM_U_CALLBACK_STOP;
    333                     break;
    334                 case ESC_CB:
    335                     callback=UCNV_FROM_U_CALLBACK_ESCAPE;
    336                     break;
    337                 default:
    338                     callback=NULL;
    339                     break;
    340                 }
    341                 option=callback==NULL ? cbopt : cbopt+1;
    342                 if(*option==0) {
    343                     option=NULL;
    344                 }
    345 
    346                 invalidUChars=testCase->getString("invalidUChars", errorCode);
    347                 cc.invalidUChars=invalidUChars.getBuffer();
    348                 cc.invalidLength=invalidUChars.length();
    349 
    350                 if(U_FAILURE(errorCode)) {
    351                     errln("error parsing conversion/fromUnicode test case %d - %s",
    352                             i, u_errorName(errorCode));
    353                     errorCode=U_ZERO_ERROR;
    354                 } else {
    355                     logln("TestFromUnicode[%d] %s", i, charset);
    356                     FromUnicodeCase(cc, callback, option);
    357                 }
    358             }
    359             delete testData;
    360         }
    361         delete dataModule;
    362     }
    363     else {
    364         dataerrln("Could not load test conversion data");
    365     }
    366 }
    367 
    368 static const UChar ellipsis[]={ 0x2e, 0x2e, 0x2e };
    369 
    370 void
    371 ConversionTest::TestGetUnicodeSet() {
    372     char charset[100];
    373     UnicodeString s, map, mapnot;
    374     int32_t which;
    375 
    376     ParsePosition pos;
    377     UnicodeSet cnvSet, mapSet, mapnotSet, diffSet;
    378     UnicodeSet *cnvSetPtr = &cnvSet;
    379     LocalUConverterPointer cnv;
    380 
    381     TestDataModule *dataModule;
    382     TestData *testData;
    383     const DataMap *testCase;
    384     UErrorCode errorCode;
    385     int32_t i;
    386 
    387     errorCode=U_ZERO_ERROR;
    388     dataModule=TestDataModule::getTestDataModule("conversion", *this, errorCode);
    389     if(U_SUCCESS(errorCode)) {
    390         testData=dataModule->createTestData("getUnicodeSet", errorCode);
    391         if(U_SUCCESS(errorCode)) {
    392             for(i=0; testData->nextCase(testCase, errorCode); ++i) {
    393                 if(U_FAILURE(errorCode)) {
    394                     errln("error retrieving conversion/getUnicodeSet test case %d - %s",
    395                             i, u_errorName(errorCode));
    396                     errorCode=U_ZERO_ERROR;
    397                     continue;
    398                 }
    399 
    400                 s=testCase->getString("charset", errorCode);
    401                 s.extract(0, 0x7fffffff, charset, sizeof(charset), "");
    402 
    403                 // BEGIN android-added
    404                 // To save space, Android does not build full ISO-2022-CN tables.
    405                 // We skip the TestGetKeywordValuesForLocale for counting available collations.
    406                 if (strlen(charset) >= 8 &&
    407                     strncmp(charset+4, "2022-CN", 4) == 0) {
    408                     continue;
    409                 }
    410                 // END android-added
    411 
    412                 map=testCase->getString("map", errorCode);
    413                 mapnot=testCase->getString("mapnot", errorCode);
    414 
    415                 which=testCase->getInt28("which", errorCode);
    416 
    417                 if(U_FAILURE(errorCode)) {
    418                     errln("error parsing conversion/getUnicodeSet test case %d - %s",
    419                             i, u_errorName(errorCode));
    420                     errorCode=U_ZERO_ERROR;
    421                     continue;
    422                 }
    423 
    424                 // test this test case
    425                 mapSet.clear();
    426                 mapnotSet.clear();
    427 
    428                 pos.setIndex(0);
    429                 mapSet.applyPattern(map, pos, 0, NULL, errorCode);
    430                 if(U_FAILURE(errorCode) || pos.getIndex()!=map.length()) {
    431                     errln("error creating the map set for conversion/getUnicodeSet test case %d - %s\n"
    432                           "    error index %d  index %d  U+%04x",
    433                             i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), map.char32At(pos.getIndex()));
    434                     errorCode=U_ZERO_ERROR;
    435                     continue;
    436                 }
    437 
    438                 pos.setIndex(0);
    439                 mapnotSet.applyPattern(mapnot, pos, 0, NULL, errorCode);
    440                 if(U_FAILURE(errorCode) || pos.getIndex()!=mapnot.length()) {
    441                     errln("error creating the mapnot set for conversion/getUnicodeSet test case %d - %s\n"
    442                           "    error index %d  index %d  U+%04x",
    443                             i, u_errorName(errorCode), pos.getErrorIndex(), pos.getIndex(), mapnot.char32At(pos.getIndex()));
    444                     errorCode=U_ZERO_ERROR;
    445                     continue;
    446                 }
    447 
    448                 logln("TestGetUnicodeSet[%d] %s", i, charset);
    449 
    450                 cnv.adoptInstead(cnv_open(charset, errorCode));
    451                 if(U_FAILURE(errorCode)) {
    452                     errcheckln(errorCode, "error opening \"%s\" for conversion/getUnicodeSet test case %d - %s",
    453                             charset, i, u_errorName(errorCode));
    454                     errorCode=U_ZERO_ERROR;
    455                     continue;
    456                 }
    457 
    458                 ucnv_getUnicodeSet(cnv.getAlias(), cnvSetPtr->toUSet(), (UConverterUnicodeSet)which, &errorCode);
    459 
    460                 if(U_FAILURE(errorCode)) {
    461                     errln("error in ucnv_getUnicodeSet(\"%s\") for conversion/getUnicodeSet test case %d - %s",
    462                             charset, i, u_errorName(errorCode));
    463                     errorCode=U_ZERO_ERROR;
    464                     continue;
    465                 }
    466 
    467                 // are there items that must be in cnvSet but are not?
    468                 (diffSet=mapSet).removeAll(cnvSet);
    469                 if(!diffSet.isEmpty()) {
    470                     diffSet.toPattern(s, TRUE);
    471                     if(s.length()>100) {
    472                         s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
    473                     }
    474                     errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - conversion/getUnicodeSet test case %d",
    475                             charset, i);
    476                     errln(s);
    477                 }
    478 
    479                 // are there items that must not be in cnvSet but are?
    480                 (diffSet=mapnotSet).retainAll(cnvSet);
    481                 if(!diffSet.isEmpty()) {
    482                     diffSet.toPattern(s, TRUE);
    483                     if(s.length()>100) {
    484                         s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
    485                     }
    486                     errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - conversion/getUnicodeSet test case %d",
    487                             charset, i);
    488                     errln(s);
    489                 }
    490             }
    491             delete testData;
    492         }
    493         delete dataModule;
    494     }
    495     else {
    496         dataerrln("Could not load test conversion data");
    497     }
    498 }
    499 
    500 U_CDECL_BEGIN
    501 static void U_CALLCONV
    502 getUnicodeSetCallback(const void *context,
    503                       UConverterFromUnicodeArgs * /*fromUArgs*/,
    504                       const UChar* /*codeUnits*/,
    505                       int32_t /*length*/,
    506                       UChar32 codePoint,
    507                       UConverterCallbackReason reason,
    508                       UErrorCode *pErrorCode) {
    509     if(reason<=UCNV_IRREGULAR) {
    510         ((UnicodeSet *)context)->remove(codePoint);  // the converter cannot convert this code point
    511         *pErrorCode=U_ZERO_ERROR;                    // skip
    512     }  // else ignore the reset, close and clone calls.
    513 }
    514 U_CDECL_END
    515 
    516 // Compare ucnv_getUnicodeSet() with the set of characters that can be converted.
    517 void
    518 ConversionTest::TestGetUnicodeSet2() {
    519     // Build a string with all code points.
    520     UChar32 cpLimit;
    521     int32_t s0Length;
    522     if(quick) {
    523         cpLimit=s0Length=0x10000;  // BMP only
    524     } else {
    525         cpLimit=0x110000;
    526         s0Length=0x10000+0x200000;  // BMP + surrogate pairs
    527     }
    528     UChar *s0=new UChar[s0Length];
    529     if(s0==NULL) {
    530         return;
    531     }
    532     UChar *s=s0;
    533     UChar32 c;
    534     UChar c2;
    535     // low BMP
    536     for(c=0; c<=0xd7ff; ++c) {
    537         *s++=(UChar)c;
    538     }
    539     // trail surrogates
    540     for(c=0xdc00; c<=0xdfff; ++c) {
    541         *s++=(UChar)c;
    542     }
    543     // lead surrogates
    544     // (after trails so that there is not even one surrogate pair in between)
    545     for(c=0xd800; c<=0xdbff; ++c) {
    546         *s++=(UChar)c;
    547     }
    548     // high BMP
    549     for(c=0xe000; c<=0xffff; ++c) {
    550         *s++=(UChar)c;
    551     }
    552     // supplementary code points = surrogate pairs
    553     if(cpLimit==0x110000) {
    554         for(c=0xd800; c<=0xdbff; ++c) {
    555             for(c2=0xdc00; c2<=0xdfff; ++c2) {
    556                 *s++=(UChar)c;
    557                 *s++=c2;
    558             }
    559         }
    560     }
    561 
    562     static const char *const cnvNames[]={
    563         "UTF-8",
    564         "UTF-7",
    565         "UTF-16",
    566         "US-ASCII",
    567         "ISO-8859-1",
    568         "windows-1252",
    569         "Shift-JIS",
    570         "ibm-1390",  // EBCDIC_STATEFUL table
    571         "ibm-16684",  // DBCS-only extension table based on EBCDIC_STATEFUL table
    572         "HZ",
    573         "ISO-2022-JP",
    574         "JIS7",
    575         "ISO-2022-CN",
    576         "ISO-2022-CN-EXT",
    577         "LMBCS"
    578     };
    579     LocalUConverterPointer cnv;
    580     char buffer[1024];
    581     int32_t i;
    582     for(i=0; i<UPRV_LENGTHOF(cnvNames); ++i) {
    583         UErrorCode errorCode=U_ZERO_ERROR;
    584         cnv.adoptInstead(cnv_open(cnvNames[i], errorCode));
    585         if(U_FAILURE(errorCode)) {
    586             errcheckln(errorCode, "failed to open converter %s - %s", cnvNames[i], u_errorName(errorCode));
    587             continue;
    588         }
    589         UnicodeSet expected;
    590         ucnv_setFromUCallBack(cnv.getAlias(), getUnicodeSetCallback, &expected, NULL, NULL, &errorCode);
    591         if(U_FAILURE(errorCode)) {
    592             errln("failed to set the callback on converter %s - %s", cnvNames[i], u_errorName(errorCode));
    593             continue;
    594         }
    595         UConverterUnicodeSet which;
    596         for(which=UCNV_ROUNDTRIP_SET; which<UCNV_SET_COUNT; which=(UConverterUnicodeSet)((int)which+1)) {
    597             if(which==UCNV_ROUNDTRIP_AND_FALLBACK_SET) {
    598                 ucnv_setFallback(cnv.getAlias(), TRUE);
    599             }
    600             expected.add(0, cpLimit-1);
    601             s=s0;
    602             UBool flush;
    603             do {
    604                 char *t=buffer;
    605                 flush=(UBool)(s==s0+s0Length);
    606                 ucnv_fromUnicode(cnv.getAlias(), &t, buffer+sizeof(buffer), (const UChar **)&s, s0+s0Length, NULL, flush, &errorCode);
    607                 if(U_FAILURE(errorCode)) {
    608                     if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
    609                         errorCode=U_ZERO_ERROR;
    610                         continue;
    611                     } else {
    612                         break;  // unexpected error, should not occur
    613                     }
    614                 }
    615             } while(!flush);
    616             UnicodeSet set;
    617             ucnv_getUnicodeSet(cnv.getAlias(), set.toUSet(), which, &errorCode);
    618             if(cpLimit<0x110000) {
    619                 set.remove(cpLimit, 0x10ffff);
    620             }
    621             if(which==UCNV_ROUNDTRIP_SET) {
    622                 // ignore PUA code points because they will be converted even if they
    623                 // are fallbacks and when other fallbacks are turned off,
    624                 // but ucnv_getUnicodeSet(UCNV_ROUNDTRIP_SET) delivers true roundtrips
    625                 expected.remove(0xe000, 0xf8ff);
    626                 expected.remove(0xf0000, 0xffffd);
    627                 expected.remove(0x100000, 0x10fffd);
    628                 set.remove(0xe000, 0xf8ff);
    629                 set.remove(0xf0000, 0xffffd);
    630                 set.remove(0x100000, 0x10fffd);
    631             }
    632             if(set!=expected) {
    633                 // First try to see if we have different sets because ucnv_getUnicodeSet()
    634                 // added strings: The above conversion method does not tell us what strings might be convertible.
    635                 // Remove strings from the set and compare again.
    636                 // Unfortunately, there are no good, direct set methods for finding out whether there are strings
    637                 // in the set, nor for enumerating or removing just them.
    638                 // Intersect all code points with the set. The intersection will not contain strings.
    639                 UnicodeSet temp(0, 0x10ffff);
    640                 temp.retainAll(set);
    641                 set=temp;
    642             }
    643             if(set!=expected) {
    644                 UnicodeSet diffSet;
    645                 UnicodeString out;
    646 
    647                 // are there items that must be in the set but are not?
    648                 (diffSet=expected).removeAll(set);
    649                 if(!diffSet.isEmpty()) {
    650                     diffSet.toPattern(out, TRUE);
    651                     if(out.length()>100) {
    652                         out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
    653                     }
    654                     errln("error: ucnv_getUnicodeSet(\"%s\") is missing items - which set: %d",
    655                             cnvNames[i], which);
    656                     errln(out);
    657                 }
    658 
    659                 // are there items that must not be in the set but are?
    660                 (diffSet=set).removeAll(expected);
    661                 if(!diffSet.isEmpty()) {
    662                     diffSet.toPattern(out, TRUE);
    663                     if(out.length()>100) {
    664                         out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellipsis));
    665                     }
    666                     errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected items - which set: %d",
    667                             cnvNames[i], which);
    668                     errln(out);
    669                 }
    670             }
    671         }
    672     }
    673 
    674     delete [] s0;
    675 }
    676 
    677 // Test all codepoints which has the default ignorable Unicode property are ignored if they have no mapping
    678 // If there are any failures, the hard coded list (IS_DEFAULT_IGNORABLE_CODE_POINT) in ucnv_err.c should be updated
    679 void
    680 ConversionTest::TestDefaultIgnorableCallback() {
    681     UErrorCode status = U_ZERO_ERROR;
    682     const char *cnv_name = "euc-jp-2007";
    683     const char *pattern_ignorable = "[:Default_Ignorable_Code_Point:]";
    684     const char *pattern_not_ignorable = "[:^Default_Ignorable_Code_Point:]";
    685 
    686     UnicodeSet *set_ignorable = new UnicodeSet(pattern_ignorable, status);
    687     if (U_FAILURE(status)) {
    688         dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_ignorable, u_errorName(status));
    689         return;
    690     }
    691 
    692     UnicodeSet *set_not_ignorable = new UnicodeSet(pattern_not_ignorable, status);
    693     if (U_FAILURE(status)) {
    694         dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_not_ignorable, u_errorName(status));
    695         return;
    696     }
    697 
    698     UConverter *cnv = cnv_open(cnv_name, status);
    699     if (U_FAILURE(status)) {
    700         dataerrln("Unable to open converter: %s - %s\n", cnv_name, u_errorName(status));
    701         return;
    702     }
    703 
    704     // set callback for the converter
    705     ucnv_setFromUCallBack(cnv, UCNV_FROM_U_CALLBACK_SUBSTITUTE, NULL, NULL, NULL, &status);
    706 
    707     UChar32 input[1];
    708     char output[10];
    709     int32_t outputLength;
    710 
    711     // test default ignorables are ignored
    712     int size = set_ignorable->size();
    713     for (int i = 0; i < size; i++) {
    714         status = U_ZERO_ERROR;
    715         outputLength= 0;
    716 
    717         input[0] = set_ignorable->charAt(i);
    718 
    719         outputLength = ucnv_fromUChars(cnv, output, 10, UnicodeString::fromUTF32(input, 1).getTerminatedBuffer(), -1, &status);
    720         if (U_FAILURE(status) || outputLength != 0) {
    721             errln("Ignorable code point: U+%04X not skipped as expected - %s", input[0], u_errorName(status));
    722         }
    723     }
    724 
    725     // test non-ignorables are not ignored
    726     size = set_not_ignorable->size();
    727     for (int i = 0; i < size; i++) {
    728         status = U_ZERO_ERROR;
    729         outputLength= 0;
    730 
    731         input[0] = set_not_ignorable->charAt(i);
    732 
    733         if (input[0] == 0) {
    734             continue;
    735         }
    736 
    737         outputLength = ucnv_fromUChars(cnv, output, 10, UnicodeString::fromUTF32(input, 1).getTerminatedBuffer(), -1, &status);
    738         if (U_FAILURE(status) || outputLength <= 0) {
    739             errln("Non-ignorable code point: U+%04X skipped unexpectedly - %s", input[0], u_errorName(status));
    740         }
    741     }
    742 
    743     ucnv_close(cnv);
    744     delete set_not_ignorable;
    745     delete set_ignorable;
    746 }
    747 
    748 void
    749 ConversionTest::TestUTF8ToUTF8Overflow() {
    750     IcuTestErrorCode errorCode(*this, "TestUTF8ToUTF8Overflow");
    751     LocalUConverterPointer cnv1(ucnv_open("UTF-8", errorCode));
    752     LocalUConverterPointer cnv2(ucnv_open("UTF-8", errorCode));
    753     static const char *text = "a";  // : 2 bytes
    754     const char *source = text;
    755     const char *sourceLimit = text + strlen(text);
    756     char result[20];
    757     char *target = result;
    758     const char *targetLimit = result + sizeof(result);
    759     UChar buffer16[20];
    760     UChar *pivotSource = buffer16;
    761     UChar *pivotTarget = buffer16;
    762     const UChar *pivotLimit = buffer16 + UPRV_LENGTHOF(buffer16);
    763 
    764     // Convert with insufficient target capacity.
    765     result[2] = 5;
    766     ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
    767                    &target, result + 2, &source, sourceLimit,
    768                    buffer16, &pivotSource, &pivotTarget, pivotLimit,
    769                    FALSE, FALSE, errorCode);
    770     assertEquals("overflow", U_BUFFER_OVERFLOW_ERROR, errorCode.reset());
    771     int32_t length = (int32_t)(target - result);
    772     assertEquals("number of bytes written", 2, length);
    773     assertEquals("next byte not clobbered", 5, result[2]);
    774 
    775     // Convert the rest and flush.
    776     ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
    777                    &target, targetLimit, &source, sourceLimit,
    778                    buffer16, &pivotSource, &pivotTarget, pivotLimit,
    779                    FALSE, TRUE, errorCode);
    780 
    781     assertSuccess("UTF-8->UTF-8", errorCode);
    782     length = (int32_t)(target - result);
    783     assertEquals("3 bytes", 3, length);
    784     if (length == 3) {
    785         assertTrue("result same as input", memcmp(text, result, length) == 0);
    786     }
    787 
    788     ucnv_reset(cnv1.getAlias());
    789     ucnv_reset(cnv2.getAlias());
    790     memset(result, 0, sizeof(result));
    791     static const char *text2 = "a";  // U+1F6B2 bicycle: 4 bytes
    792     source = text2;
    793     sourceLimit = text2 + strlen(text2);
    794     target = result;
    795     pivotSource = pivotTarget = buffer16;
    796 
    797     // Convert with insufficient target capacity.
    798     result[3] = 5;
    799     ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
    800                    &target, result + 3, &source, sourceLimit,
    801                    buffer16, &pivotSource, &pivotTarget, pivotLimit,
    802                    FALSE, FALSE, errorCode);
    803     assertEquals("text2 overflow", U_BUFFER_OVERFLOW_ERROR, errorCode.reset());
    804     length = (int32_t)(target - result);
    805     assertEquals("text2 number of bytes written", 3, length);
    806     assertEquals("text2 next byte not clobbered", 5, result[3]);
    807 
    808     // Convert the rest and flush.
    809     ucnv_convertEx(cnv2.getAlias(), cnv1.getAlias(),
    810                    &target, targetLimit, &source, sourceLimit,
    811                    buffer16, &pivotSource, &pivotTarget, pivotLimit,
    812                    FALSE, TRUE, errorCode);
    813 
    814     assertSuccess("text2 UTF-8->UTF-8", errorCode);
    815     length = (int32_t)(target - result);
    816     assertEquals("text2 5 bytes", 5, length);
    817     if (length == 5) {
    818         assertTrue("text2 result same as input", memcmp(text2, result, length) == 0);
    819     }
    820 }
    821 
    822 // open testdata or ICU data converter ------------------------------------- ***
    823 
    824 UConverter *
    825 ConversionTest::cnv_open(const char *name, UErrorCode &errorCode) {
    826     if(name!=NULL && *name=='+') {
    827         // Converter names that start with '+' are ignored in ICU4J tests.
    828         ++name;
    829     }
    830     if(name!=NULL && *name=='*') {
    831         /* loadTestData(): set the data directory */
    832         return ucnv_openPackage(loadTestData(errorCode), name+1, &errorCode);
    833     } else {
    834         return ucnv_open(name, &errorCode);
    835     }
    836 }
    837 
    838 // output helpers ---------------------------------------------------------- ***
    839 
    840 static inline char
    841 hexDigit(uint8_t digit) {
    842     return digit<=9 ? (char)('0'+digit) : (char)('a'-10+digit);
    843 }
    844 
    845 static char *
    846 printBytes(const uint8_t *bytes, int32_t length, char *out) {
    847     uint8_t b;
    848 
    849     if(length>0) {
    850         b=*bytes++;
    851         --length;
    852         *out++=hexDigit((uint8_t)(b>>4));
    853         *out++=hexDigit((uint8_t)(b&0xf));
    854     }
    855 
    856     while(length>0) {
    857         b=*bytes++;
    858         --length;
    859         *out++=' ';
    860         *out++=hexDigit((uint8_t)(b>>4));
    861         *out++=hexDigit((uint8_t)(b&0xf));
    862     }
    863     *out++=0;
    864     return out;
    865 }
    866 
    867 static char *
    868 printUnicode(const UChar *unicode, int32_t length, char *out) {
    869     UChar32 c;
    870     int32_t i;
    871 
    872     for(i=0; i<length;) {
    873         if(i>0) {
    874             *out++=' ';
    875         }
    876         U16_NEXT(unicode, i, length, c);
    877         // write 4..6 digits
    878         if(c>=0x100000) {
    879             *out++='1';
    880         }
    881         if(c>=0x10000) {
    882             *out++=hexDigit((uint8_t)((c>>16)&0xf));
    883         }
    884         *out++=hexDigit((uint8_t)((c>>12)&0xf));
    885         *out++=hexDigit((uint8_t)((c>>8)&0xf));
    886         *out++=hexDigit((uint8_t)((c>>4)&0xf));
    887         *out++=hexDigit((uint8_t)(c&0xf));
    888     }
    889     *out++=0;
    890     return out;
    891 }
    892 
    893 static char *
    894 printOffsets(const int32_t *offsets, int32_t length, char *out) {
    895     int32_t i, o, d;
    896 
    897     if(offsets==NULL) {
    898         length=0;
    899     }
    900 
    901     for(i=0; i<length; ++i) {
    902         if(i>0) {
    903             *out++=' ';
    904         }
    905         o=offsets[i];
    906 
    907         // print all offsets with 2 characters each (-x, -9..99, xx)
    908         if(o<-9) {
    909             *out++='-';
    910             *out++='x';
    911         } else if(o<0) {
    912             *out++='-';
    913             *out++=(char)('0'-o);
    914         } else if(o<=99) {
    915             *out++=(d=o/10)==0 ? ' ' : (char)('0'+d);
    916             *out++=(char)('0'+o%10);
    917         } else /* o>99 */ {
    918             *out++='x';
    919             *out++='x';
    920         }
    921     }
    922     *out++=0;
    923     return out;
    924 }
    925 
    926 // toUnicode test worker functions ----------------------------------------- ***
    927 
    928 static int32_t
    929 stepToUnicode(ConversionCase &cc, UConverter *cnv,
    930               UChar *result, int32_t resultCapacity,
    931               int32_t *resultOffsets, /* also resultCapacity */
    932               int32_t step,
    933               UErrorCode *pErrorCode) {
    934     const char *source, *sourceLimit, *bytesLimit;
    935     UChar *target, *targetLimit, *resultLimit;
    936     UBool flush;
    937 
    938     source=(const char *)cc.bytes;
    939     target=result;
    940     bytesLimit=source+cc.bytesLength;
    941     resultLimit=result+resultCapacity;
    942 
    943     if(step>=0) {
    944         // call ucnv_toUnicode() with in/out buffers no larger than (step) at a time
    945         // move only one buffer (in vs. out) at a time to be extra mean
    946         // step==0 performs bulk conversion and generates offsets
    947 
    948         // initialize the partial limits for the loop
    949         if(step==0) {
    950             // use the entire buffers
    951             sourceLimit=bytesLimit;
    952             targetLimit=resultLimit;
    953             flush=cc.finalFlush;
    954         } else {
    955             // start with empty partial buffers
    956             sourceLimit=source;
    957             targetLimit=target;
    958             flush=FALSE;
    959 
    960             // output offsets only for bulk conversion
    961             resultOffsets=NULL;
    962         }
    963 
    964         for(;;) {
    965             // resetting the opposite conversion direction must not affect this one
    966             ucnv_resetFromUnicode(cnv);
    967 
    968             // convert
    969             ucnv_toUnicode(cnv,
    970                 &target, targetLimit,
    971                 &source, sourceLimit,
    972                 resultOffsets,
    973                 flush, pErrorCode);
    974 
    975             // check pointers and errors
    976             if(source>sourceLimit || target>targetLimit) {
    977                 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
    978                 break;
    979             } else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
    980                 if(target!=targetLimit) {
    981                     // buffer overflow must only be set when the target is filled
    982                     *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
    983                     break;
    984                 } else if(targetLimit==resultLimit) {
    985                     // not just a partial overflow
    986                     break;
    987                 }
    988 
    989                 // the partial target is filled, set a new limit, reset the error and continue
    990                 targetLimit=(resultLimit-target)>=step ? target+step : resultLimit;
    991                 *pErrorCode=U_ZERO_ERROR;
    992             } else if(U_FAILURE(*pErrorCode)) {
    993                 // some other error occurred, done
    994                 break;
    995             } else {
    996                 if(source!=sourceLimit) {
    997                     // when no error occurs, then the input must be consumed
    998                     *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
    999                     break;
   1000                 }
   1001 
   1002                 if(sourceLimit==bytesLimit) {
   1003                     // we are done
   1004                     break;
   1005                 }
   1006 
   1007                 // the partial conversion succeeded, set a new limit and continue
   1008                 sourceLimit=(bytesLimit-source)>=step ? source+step : bytesLimit;
   1009                 flush=(UBool)(cc.finalFlush && sourceLimit==bytesLimit);
   1010             }
   1011         }
   1012     } else /* step<0 */ {
   1013         /*
   1014          * step==-1: call only ucnv_getNextUChar()
   1015          * otherwise alternate between ucnv_toUnicode() and ucnv_getNextUChar()
   1016          *   if step==-2 or -3, then give ucnv_toUnicode() the whole remaining input,
   1017          *   else give it at most (-step-2)/2 bytes
   1018          */
   1019         UChar32 c;
   1020 
   1021         // end the loop by getting an index out of bounds error
   1022         for(;;) {
   1023             // resetting the opposite conversion direction must not affect this one
   1024             ucnv_resetFromUnicode(cnv);
   1025 
   1026             // convert
   1027             if((step&1)!=0 /* odd: -1, -3, -5, ... */) {
   1028                 sourceLimit=source; // use sourceLimit not as a real limit
   1029                                     // but to remember the pre-getNextUChar source pointer
   1030                 c=ucnv_getNextUChar(cnv, &source, bytesLimit, pErrorCode);
   1031 
   1032                 // check pointers and errors
   1033                 if(*pErrorCode==U_INDEX_OUTOFBOUNDS_ERROR) {
   1034                     if(source!=bytesLimit) {
   1035                         *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1036                     } else {
   1037                         *pErrorCode=U_ZERO_ERROR;
   1038                     }
   1039                     break;
   1040                 } else if(U_FAILURE(*pErrorCode)) {
   1041                     break;
   1042                 }
   1043                 // source may not move if c is from previous overflow
   1044 
   1045                 if(target==resultLimit) {
   1046                     *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
   1047                     break;
   1048                 }
   1049                 if(c<=0xffff) {
   1050                     *target++=(UChar)c;
   1051                 } else {
   1052                     *target++=U16_LEAD(c);
   1053                     if(target==resultLimit) {
   1054                         *pErrorCode=U_BUFFER_OVERFLOW_ERROR;
   1055                         break;
   1056                     }
   1057                     *target++=U16_TRAIL(c);
   1058                 }
   1059 
   1060                 // alternate between -n-1 and -n but leave -1 alone
   1061                 if(step<-1) {
   1062                     ++step;
   1063                 }
   1064             } else /* step is even */ {
   1065                 // allow only one UChar output
   1066                 targetLimit=target<resultLimit ? target+1 : resultLimit;
   1067 
   1068                 // as with ucnv_getNextUChar(), we always flush (if we go to bytesLimit)
   1069                 // and never output offsets
   1070                 if(step==-2) {
   1071                     sourceLimit=bytesLimit;
   1072                 } else {
   1073                     sourceLimit=source+(-step-2)/2;
   1074                     if(sourceLimit>bytesLimit) {
   1075                         sourceLimit=bytesLimit;
   1076                     }
   1077                 }
   1078 
   1079                 ucnv_toUnicode(cnv,
   1080                     &target, targetLimit,
   1081                     &source, sourceLimit,
   1082                     NULL, (UBool)(sourceLimit==bytesLimit), pErrorCode);
   1083 
   1084                 // check pointers and errors
   1085                 if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
   1086                     if(target!=targetLimit) {
   1087                         // buffer overflow must only be set when the target is filled
   1088                         *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1089                         break;
   1090                     } else if(targetLimit==resultLimit) {
   1091                         // not just a partial overflow
   1092                         break;
   1093                     }
   1094 
   1095                     // the partial target is filled, set a new limit and continue
   1096                     *pErrorCode=U_ZERO_ERROR;
   1097                 } else if(U_FAILURE(*pErrorCode)) {
   1098                     // some other error occurred, done
   1099                     break;
   1100                 } else {
   1101                     if(source!=sourceLimit) {
   1102                         // when no error occurs, then the input must be consumed
   1103                         *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1104                         break;
   1105                     }
   1106 
   1107                     // we are done (flush==TRUE) but we continue, to get the index out of bounds error above
   1108                 }
   1109 
   1110                 --step;
   1111             }
   1112         }
   1113     }
   1114 
   1115     return (int32_t)(target-result);
   1116 }
   1117 
   1118 UBool
   1119 ConversionTest::ToUnicodeCase(ConversionCase &cc, UConverterToUCallback callback, const char *option) {
   1120     // open the converter
   1121     IcuTestErrorCode errorCode(*this, "ToUnicodeCase");
   1122     LocalUConverterPointer cnv(cnv_open(cc.charset, errorCode));
   1123     // with no data, the above crashes with "pointer being freed was not allocated" for charset "x11-compound-text", see #13078
   1124     if(errorCode.isFailure()) {
   1125         errcheckln(errorCode, "toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
   1126                 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, errorCode.errorName());
   1127         errorCode.reset();
   1128         return FALSE;
   1129     }
   1130 
   1131     // set the callback
   1132     if(callback!=NULL) {
   1133         ucnv_setToUCallBack(cnv.getAlias(), callback, option, NULL, NULL, errorCode);
   1134         if(U_FAILURE(errorCode)) {
   1135             errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setToUCallBack() failed - %s",
   1136                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
   1137             return FALSE;
   1138         }
   1139     }
   1140 
   1141     int32_t resultOffsets[256];
   1142     UChar result[256];
   1143     int32_t resultLength;
   1144     UBool ok;
   1145 
   1146     static const struct {
   1147         int32_t step;
   1148         const char *name;
   1149     } steps[]={
   1150         { 0, "bulk" }, // must be first for offsets to be checked
   1151         { 1, "step=1" },
   1152         { 3, "step=3" },
   1153         { 7, "step=7" },
   1154         { -1, "getNext" },
   1155         { -2, "toU(bulk)+getNext" },
   1156         { -3, "getNext+toU(bulk)" },
   1157         { -4, "toU(1)+getNext" },
   1158         { -5, "getNext+toU(1)" },
   1159         { -12, "toU(5)+getNext" },
   1160         { -13, "getNext+toU(5)" },
   1161     };
   1162     int32_t i, step;
   1163 
   1164     ok=TRUE;
   1165     for(i=0; i<UPRV_LENGTHOF(steps) && ok; ++i) {
   1166         step=steps[i].step;
   1167         if(step<0 && !cc.finalFlush) {
   1168             // skip ucnv_getNextUChar() if !finalFlush because
   1169             // ucnv_getNextUChar() always implies flush
   1170             continue;
   1171         }
   1172         if(step!=0) {
   1173             // bulk test is first, then offsets are not checked any more
   1174             cc.offsets=NULL;
   1175         }
   1176         else {
   1177             memset(resultOffsets, -1, UPRV_LENGTHOF(resultOffsets));
   1178         }
   1179         memset(result, -1, UPRV_LENGTHOF(result));
   1180         errorCode.reset();
   1181         resultLength=stepToUnicode(cc, cnv.getAlias(),
   1182                                 result, UPRV_LENGTHOF(result),
   1183                                 step==0 ? resultOffsets : NULL,
   1184                                 step, errorCode);
   1185         ok=checkToUnicode(
   1186                 cc, cnv.getAlias(), steps[i].name,
   1187                 result, resultLength,
   1188                 cc.offsets!=NULL ? resultOffsets : NULL,
   1189                 errorCode);
   1190         if(errorCode.isFailure() || !cc.finalFlush) {
   1191             // reset if an error occurred or we did not flush
   1192             // otherwise do nothing to make sure that flushing resets
   1193             ucnv_resetToUnicode(cnv.getAlias());
   1194         }
   1195         if (cc.offsets != NULL && resultOffsets[resultLength] != -1) {
   1196             errln("toUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
   1197                 cc.caseNr, cc.charset, resultLength);
   1198         }
   1199         if (result[resultLength] != (UChar)-1) {
   1200             errln("toUnicode[%d](%s) Conversion wrote too much to result at index %d",
   1201                 cc.caseNr, cc.charset, resultLength);
   1202         }
   1203     }
   1204 
   1205     // not a real loop, just a convenience for breaking out of the block
   1206     while(ok && cc.finalFlush) {
   1207         // test ucnv_toUChars()
   1208         memset(result, 0, sizeof(result));
   1209 
   1210         errorCode.reset();
   1211         resultLength=ucnv_toUChars(cnv.getAlias(),
   1212                         result, UPRV_LENGTHOF(result),
   1213                         (const char *)cc.bytes, cc.bytesLength,
   1214                         errorCode);
   1215         ok=checkToUnicode(
   1216                 cc, cnv.getAlias(), "toUChars",
   1217                 result, resultLength,
   1218                 NULL,
   1219                 errorCode);
   1220         if(!ok) {
   1221             break;
   1222         }
   1223 
   1224         // test preflighting
   1225         // keep the correct result for simple checking
   1226         errorCode.reset();
   1227         resultLength=ucnv_toUChars(cnv.getAlias(),
   1228                         NULL, 0,
   1229                         (const char *)cc.bytes, cc.bytesLength,
   1230                         errorCode);
   1231         if(errorCode.get()==U_STRING_NOT_TERMINATED_WARNING || errorCode.get()==U_BUFFER_OVERFLOW_ERROR) {
   1232             errorCode.reset();
   1233         }
   1234         ok=checkToUnicode(
   1235                 cc, cnv.getAlias(), "preflight toUChars",
   1236                 result, resultLength,
   1237                 NULL,
   1238                 errorCode);
   1239         break;
   1240     }
   1241 
   1242     errorCode.reset();  // all errors have already been reported
   1243     return ok;
   1244 }
   1245 
   1246 UBool
   1247 ConversionTest::checkToUnicode(ConversionCase &cc, UConverter *cnv, const char *name,
   1248                                const UChar *result, int32_t resultLength,
   1249                                const int32_t *resultOffsets,
   1250                                UErrorCode resultErrorCode) {
   1251     char resultInvalidChars[8];
   1252     int8_t resultInvalidLength;
   1253     UErrorCode errorCode;
   1254 
   1255     const char *msg;
   1256 
   1257     // reset the message; NULL will mean "ok"
   1258     msg=NULL;
   1259 
   1260     errorCode=U_ZERO_ERROR;
   1261     resultInvalidLength=sizeof(resultInvalidChars);
   1262     ucnv_getInvalidChars(cnv, resultInvalidChars, &resultInvalidLength, &errorCode);
   1263     if(U_FAILURE(errorCode)) {
   1264         errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidChars() failed - %s",
   1265                 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, u_errorName(errorCode));
   1266         return FALSE;
   1267     }
   1268 
   1269     // check everything that might have gone wrong
   1270     if(cc.unicodeLength!=resultLength) {
   1271         msg="wrong result length";
   1272     } else if(0!=u_memcmp(cc.unicode, result, cc.unicodeLength)) {
   1273         msg="wrong result string";
   1274     } else if(cc.offsets!=NULL && 0!=memcmp(cc.offsets, resultOffsets, cc.unicodeLength*sizeof(*cc.offsets))) {
   1275         msg="wrong offsets";
   1276     } else if(cc.outErrorCode!=resultErrorCode) {
   1277         msg="wrong error code";
   1278     } else if(cc.invalidLength!=resultInvalidLength) {
   1279         msg="wrong length of last invalid input";
   1280     } else if(0!=memcmp(cc.invalidChars, resultInvalidChars, cc.invalidLength)) {
   1281         msg="wrong last invalid input";
   1282     }
   1283 
   1284     if(msg==NULL) {
   1285         return TRUE;
   1286     } else {
   1287         char buffer[2000]; // one buffer for all strings
   1288         char *s, *bytesString, *unicodeString, *resultString,
   1289             *offsetsString, *resultOffsetsString,
   1290             *invalidCharsString, *resultInvalidCharsString;
   1291 
   1292         bytesString=s=buffer;
   1293         s=printBytes(cc.bytes, cc.bytesLength, bytesString);
   1294         s=printUnicode(cc.unicode, cc.unicodeLength, unicodeString=s);
   1295         s=printUnicode(result, resultLength, resultString=s);
   1296         s=printOffsets(cc.offsets, cc.unicodeLength, offsetsString=s);
   1297         s=printOffsets(resultOffsets, resultLength, resultOffsetsString=s);
   1298         s=printBytes(cc.invalidChars, cc.invalidLength, invalidCharsString=s);
   1299         s=printBytes((uint8_t *)resultInvalidChars, resultInvalidLength, resultInvalidCharsString=s);
   1300 
   1301         if((s-buffer)>(int32_t)sizeof(buffer)) {
   1302             errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkToUnicode() test output buffer overflow writing %d chars\n",
   1303                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, (int)(s-buffer));
   1304             exit(1);
   1305         }
   1306 
   1307         errln("toUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
   1308               "  bytes <%s>[%d]\n"
   1309               " expected <%s>[%d]\n"
   1310               "  result  <%s>[%d]\n"
   1311               " offsets         <%s>\n"
   1312               "  result offsets <%s>\n"
   1313               " error code expected %s got %s\n"
   1314               "  invalidChars expected <%s> got <%s>\n",
   1315               cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, msg,
   1316               bytesString, cc.bytesLength,
   1317               unicodeString, cc.unicodeLength,
   1318               resultString, resultLength,
   1319               offsetsString,
   1320               resultOffsetsString,
   1321               u_errorName(cc.outErrorCode), u_errorName(resultErrorCode),
   1322               invalidCharsString, resultInvalidCharsString);
   1323 
   1324         return FALSE;
   1325     }
   1326 }
   1327 
   1328 // fromUnicode test worker functions --------------------------------------- ***
   1329 
   1330 static int32_t
   1331 stepFromUTF8(ConversionCase &cc,
   1332              UConverter *utf8Cnv, UConverter *cnv,
   1333              char *result, int32_t resultCapacity,
   1334              int32_t step,
   1335              UErrorCode *pErrorCode) {
   1336     const char *source, *sourceLimit, *utf8Limit;
   1337     UChar pivotBuffer[32];
   1338     UChar *pivotSource, *pivotTarget, *pivotLimit;
   1339     char *target, *targetLimit, *resultLimit;
   1340     UBool flush;
   1341 
   1342     source=cc.utf8;
   1343     pivotSource=pivotTarget=pivotBuffer;
   1344     target=result;
   1345     utf8Limit=source+cc.utf8Length;
   1346     resultLimit=result+resultCapacity;
   1347 
   1348     // call ucnv_convertEx() with in/out buffers no larger than (step) at a time
   1349     // move only one buffer (in vs. out) at a time to be extra mean
   1350     // step==0 performs bulk conversion
   1351 
   1352     // initialize the partial limits for the loop
   1353     if(step==0) {
   1354         // use the entire buffers
   1355         sourceLimit=utf8Limit;
   1356         targetLimit=resultLimit;
   1357         flush=cc.finalFlush;
   1358 
   1359         pivotLimit=pivotBuffer+UPRV_LENGTHOF(pivotBuffer);
   1360     } else {
   1361         // start with empty partial buffers
   1362         sourceLimit=source;
   1363         targetLimit=target;
   1364         flush=FALSE;
   1365 
   1366         // empty pivot is not allowed, make it of length step
   1367         pivotLimit=pivotBuffer+step;
   1368     }
   1369 
   1370     for(;;) {
   1371         // resetting the opposite conversion direction must not affect this one
   1372         ucnv_resetFromUnicode(utf8Cnv);
   1373         ucnv_resetToUnicode(cnv);
   1374 
   1375         // convert
   1376         ucnv_convertEx(cnv, utf8Cnv,
   1377             &target, targetLimit,
   1378             &source, sourceLimit,
   1379             pivotBuffer, &pivotSource, &pivotTarget, pivotLimit,
   1380             FALSE, flush, pErrorCode);
   1381 
   1382         // check pointers and errors
   1383         if(source>sourceLimit || target>targetLimit) {
   1384             *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1385             break;
   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;
   1390                 break;
   1391             } else if(targetLimit==resultLimit) {
   1392                 // not just a partial overflow
   1393                 break;
   1394             }
   1395 
   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             if(pivotSource==pivotBuffer) {
   1401                 // toUnicode error, should not occur
   1402                 // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
   1403                 break;
   1404             } else {
   1405                 // fromUnicode error
   1406                 // some other error occurred, done
   1407                 break;
   1408             }
   1409         } else {
   1410             if(source!=sourceLimit) {
   1411                 // when no error occurs, then the input must be consumed
   1412                 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1413                 break;
   1414             }
   1415 
   1416             if(sourceLimit==utf8Limit) {
   1417                 // we are done
   1418                 if(*pErrorCode==U_STRING_NOT_TERMINATED_WARNING) {
   1419                     // ucnv_convertEx() warns about not terminating the output
   1420                     // but ucnv_fromUnicode() does not and so
   1421                     // checkFromUnicode() does not expect it
   1422                     *pErrorCode=U_ZERO_ERROR;
   1423                 }
   1424                 break;
   1425             }
   1426 
   1427             // the partial conversion succeeded, set a new limit and continue
   1428             sourceLimit=(utf8Limit-source)>=step ? source+step : utf8Limit;
   1429             flush=(UBool)(cc.finalFlush && sourceLimit==utf8Limit);
   1430         }
   1431     }
   1432 
   1433     return (int32_t)(target-result);
   1434 }
   1435 
   1436 static int32_t
   1437 stepFromUnicode(ConversionCase &cc, UConverter *cnv,
   1438                 char *result, int32_t resultCapacity,
   1439                 int32_t *resultOffsets, /* also resultCapacity */
   1440                 int32_t step,
   1441                 UErrorCode *pErrorCode) {
   1442     const UChar *source, *sourceLimit, *unicodeLimit;
   1443     char *target, *targetLimit, *resultLimit;
   1444     UBool flush;
   1445 
   1446     source=cc.unicode;
   1447     target=result;
   1448     unicodeLimit=source+cc.unicodeLength;
   1449     resultLimit=result+resultCapacity;
   1450 
   1451     // call ucnv_fromUnicode() with in/out buffers no larger than (step) at a time
   1452     // move only one buffer (in vs. out) at a time to be extra mean
   1453     // step==0 performs bulk conversion and generates offsets
   1454 
   1455     // initialize the partial limits for the loop
   1456     if(step==0) {
   1457         // use the entire buffers
   1458         sourceLimit=unicodeLimit;
   1459         targetLimit=resultLimit;
   1460         flush=cc.finalFlush;
   1461     } else {
   1462         // start with empty partial buffers
   1463         sourceLimit=source;
   1464         targetLimit=target;
   1465         flush=FALSE;
   1466 
   1467         // output offsets only for bulk conversion
   1468         resultOffsets=NULL;
   1469     }
   1470 
   1471     for(;;) {
   1472         // resetting the opposite conversion direction must not affect this one
   1473         ucnv_resetToUnicode(cnv);
   1474 
   1475         // convert
   1476         ucnv_fromUnicode(cnv,
   1477             &target, targetLimit,
   1478             &source, sourceLimit,
   1479             resultOffsets,
   1480             flush, pErrorCode);
   1481 
   1482         // check pointers and errors
   1483         if(source>sourceLimit || target>targetLimit) {
   1484             *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1485             break;
   1486         } else if(*pErrorCode==U_BUFFER_OVERFLOW_ERROR) {
   1487             if(target!=targetLimit) {
   1488                 // buffer overflow must only be set when the target is filled
   1489                 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1490                 break;
   1491             } else if(targetLimit==resultLimit) {
   1492                 // not just a partial overflow
   1493                 break;
   1494             }
   1495 
   1496             // the partial target is filled, set a new limit, reset the error and continue
   1497             targetLimit=(resultLimit-target)>=step ? target+step : resultLimit;
   1498             *pErrorCode=U_ZERO_ERROR;
   1499         } else if(U_FAILURE(*pErrorCode)) {
   1500             // some other error occurred, done
   1501             break;
   1502         } else {
   1503             if(source!=sourceLimit) {
   1504                 // when no error occurs, then the input must be consumed
   1505                 *pErrorCode=U_INTERNAL_PROGRAM_ERROR;
   1506                 break;
   1507             }
   1508 
   1509             if(sourceLimit==unicodeLimit) {
   1510                 // we are done
   1511                 break;
   1512             }
   1513 
   1514             // the partial conversion succeeded, set a new limit and continue
   1515             sourceLimit=(unicodeLimit-source)>=step ? source+step : unicodeLimit;
   1516             flush=(UBool)(cc.finalFlush && sourceLimit==unicodeLimit);
   1517         }
   1518     }
   1519 
   1520     return (int32_t)(target-result);
   1521 }
   1522 
   1523 UBool
   1524 ConversionTest::FromUnicodeCase(ConversionCase &cc, UConverterFromUCallback callback, const char *option) {
   1525     UConverter *cnv;
   1526     UErrorCode errorCode;
   1527 
   1528     // open the converter
   1529     errorCode=U_ZERO_ERROR;
   1530     cnv=cnv_open(cc.charset, errorCode);
   1531     if(U_FAILURE(errorCode)) {
   1532         errcheckln(errorCode, "fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_open() failed - %s",
   1533                 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
   1534         return FALSE;
   1535     }
   1536     ucnv_resetToUnicode(utf8Cnv);
   1537 
   1538     // set the callback
   1539     if(callback!=NULL) {
   1540         ucnv_setFromUCallBack(cnv, callback, option, NULL, NULL, &errorCode);
   1541         if(U_FAILURE(errorCode)) {
   1542             errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setFromUCallBack() failed - %s",
   1543                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
   1544             ucnv_close(cnv);
   1545             return FALSE;
   1546         }
   1547     }
   1548 
   1549     // set the fallbacks flag
   1550     // TODO change with Jitterbug 2401, then add a similar call for toUnicode too
   1551     ucnv_setFallback(cnv, cc.fallbacks);
   1552 
   1553     // set the subchar
   1554     int32_t length;
   1555 
   1556     if(cc.setSub>0) {
   1557         length=(int32_t)strlen(cc.subchar);
   1558         ucnv_setSubstChars(cnv, cc.subchar, (int8_t)length, &errorCode);
   1559         if(U_FAILURE(errorCode)) {
   1560             errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstChars() failed - %s",
   1561                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
   1562             ucnv_close(cnv);
   1563             return FALSE;
   1564         }
   1565     } else if(cc.setSub<0) {
   1566         ucnv_setSubstString(cnv, cc.subString, -1, &errorCode);
   1567         if(U_FAILURE(errorCode)) {
   1568             errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstString() failed - %s",
   1569                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, u_errorName(errorCode));
   1570             ucnv_close(cnv);
   1571             return FALSE;
   1572         }
   1573     }
   1574 
   1575     // convert unicode to utf8
   1576     char utf8[256];
   1577     cc.utf8=utf8;
   1578     u_strToUTF8(utf8, UPRV_LENGTHOF(utf8), &cc.utf8Length,
   1579                 cc.unicode, cc.unicodeLength,
   1580                 &errorCode);
   1581     if(U_FAILURE(errorCode)) {
   1582         // skip UTF-8 testing of a string with an unpaired surrogate,
   1583         // or of one that's too long
   1584         // toUnicode errors are tested in cintltst TestConvertExFromUTF8()
   1585         cc.utf8Length=-1;
   1586     }
   1587 
   1588     int32_t resultOffsets[256];
   1589     char result[256];
   1590     int32_t resultLength;
   1591     UBool ok;
   1592 
   1593     static const struct {
   1594         int32_t step;
   1595         const char *name, *utf8Name;
   1596     } steps[]={
   1597         { 0, "bulk",   "utf8" }, // must be first for offsets to be checked
   1598         { 1, "step=1", "utf8 step=1" },
   1599         { 3, "step=3", "utf8 step=3" },
   1600         { 7, "step=7", "utf8 step=7" }
   1601     };
   1602     int32_t i, step;
   1603 
   1604     ok=TRUE;
   1605     for(i=0; i<UPRV_LENGTHOF(steps) && ok; ++i) {
   1606         step=steps[i].step;
   1607         memset(resultOffsets, -1, UPRV_LENGTHOF(resultOffsets));
   1608         memset(result, -1, UPRV_LENGTHOF(result));
   1609         errorCode=U_ZERO_ERROR;
   1610         resultLength=stepFromUnicode(cc, cnv,
   1611                                 result, UPRV_LENGTHOF(result),
   1612                                 step==0 ? resultOffsets : NULL,
   1613                                 step, &errorCode);
   1614         ok=checkFromUnicode(
   1615                 cc, cnv, steps[i].name,
   1616                 (uint8_t *)result, resultLength,
   1617                 cc.offsets!=NULL ? resultOffsets : NULL,
   1618                 errorCode);
   1619         if(U_FAILURE(errorCode) || !cc.finalFlush) {
   1620             // reset if an error occurred or we did not flush
   1621             // otherwise do nothing to make sure that flushing resets
   1622             ucnv_resetFromUnicode(cnv);
   1623         }
   1624         if (resultOffsets[resultLength] != -1) {
   1625             errln("fromUnicode[%d](%s) Conversion wrote too much to offsets at index %d",
   1626                 cc.caseNr, cc.charset, resultLength);
   1627         }
   1628         if (result[resultLength] != (char)-1) {
   1629             errln("fromUnicode[%d](%s) Conversion wrote too much to result at index %d",
   1630                 cc.caseNr, cc.charset, resultLength);
   1631         }
   1632 
   1633         // bulk test is first, then offsets are not checked any more
   1634         cc.offsets=NULL;
   1635 
   1636         // test direct conversion from UTF-8
   1637         if(cc.utf8Length>=0) {
   1638             errorCode=U_ZERO_ERROR;
   1639             resultLength=stepFromUTF8(cc, utf8Cnv, cnv,
   1640                                     result, UPRV_LENGTHOF(result),
   1641                                     step, &errorCode);
   1642             ok=checkFromUnicode(
   1643                     cc, cnv, steps[i].utf8Name,
   1644                     (uint8_t *)result, resultLength,
   1645                     NULL,
   1646                     errorCode);
   1647             if(U_FAILURE(errorCode) || !cc.finalFlush) {
   1648                 // reset if an error occurred or we did not flush
   1649                 // otherwise do nothing to make sure that flushing resets
   1650                 ucnv_resetToUnicode(utf8Cnv);
   1651                 ucnv_resetFromUnicode(cnv);
   1652             }
   1653         }
   1654     }
   1655 
   1656     // not a real loop, just a convenience for breaking out of the block
   1657     while(ok && cc.finalFlush) {
   1658         // test ucnv_fromUChars()
   1659         memset(result, 0, sizeof(result));
   1660 
   1661         errorCode=U_ZERO_ERROR;
   1662         resultLength=ucnv_fromUChars(cnv,
   1663                         result, UPRV_LENGTHOF(result),
   1664                         cc.unicode, cc.unicodeLength,
   1665                         &errorCode);
   1666         ok=checkFromUnicode(
   1667                 cc, cnv, "fromUChars",
   1668                 (uint8_t *)result, resultLength,
   1669                 NULL,
   1670                 errorCode);
   1671         if(!ok) {
   1672             break;
   1673         }
   1674 
   1675         // test preflighting
   1676         // keep the correct result for simple checking
   1677         errorCode=U_ZERO_ERROR;
   1678         resultLength=ucnv_fromUChars(cnv,
   1679                         NULL, 0,
   1680                         cc.unicode, cc.unicodeLength,
   1681                         &errorCode);
   1682         if(errorCode==U_STRING_NOT_TERMINATED_WARNING || errorCode==U_BUFFER_OVERFLOW_ERROR) {
   1683             errorCode=U_ZERO_ERROR;
   1684         }
   1685         ok=checkFromUnicode(
   1686                 cc, cnv, "preflight fromUChars",
   1687                 (uint8_t *)result, resultLength,
   1688                 NULL,
   1689                 errorCode);
   1690         break;
   1691     }
   1692 
   1693     ucnv_close(cnv);
   1694     return ok;
   1695 }
   1696 
   1697 UBool
   1698 ConversionTest::checkFromUnicode(ConversionCase &cc, UConverter *cnv, const char *name,
   1699                                  const uint8_t *result, int32_t resultLength,
   1700                                  const int32_t *resultOffsets,
   1701                                  UErrorCode resultErrorCode) {
   1702     UChar resultInvalidUChars[8];
   1703     int8_t resultInvalidLength;
   1704     UErrorCode errorCode;
   1705 
   1706     const char *msg;
   1707 
   1708     // reset the message; NULL will mean "ok"
   1709     msg=NULL;
   1710 
   1711     errorCode=U_ZERO_ERROR;
   1712     resultInvalidLength=UPRV_LENGTHOF(resultInvalidUChars);
   1713     ucnv_getInvalidUChars(cnv, resultInvalidUChars, &resultInvalidLength, &errorCode);
   1714     if(U_FAILURE(errorCode)) {
   1715         errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUChars() failed - %s",
   1716                 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, u_errorName(errorCode));
   1717         return FALSE;
   1718     }
   1719 
   1720     // check everything that might have gone wrong
   1721     if(cc.bytesLength!=resultLength) {
   1722         msg="wrong result length";
   1723     } else if(0!=memcmp(cc.bytes, result, cc.bytesLength)) {
   1724         msg="wrong result string";
   1725     } else if(cc.offsets!=NULL && 0!=memcmp(cc.offsets, resultOffsets, cc.bytesLength*sizeof(*cc.offsets))) {
   1726         msg="wrong offsets";
   1727     } else if(cc.outErrorCode!=resultErrorCode) {
   1728         msg="wrong error code";
   1729     } else if(cc.invalidLength!=resultInvalidLength) {
   1730         msg="wrong length of last invalid input";
   1731     } else if(0!=u_memcmp(cc.invalidUChars, resultInvalidUChars, cc.invalidLength)) {
   1732         msg="wrong last invalid input";
   1733     }
   1734 
   1735     if(msg==NULL) {
   1736         return TRUE;
   1737     } else {
   1738         char buffer[2000]; // one buffer for all strings
   1739         char *s, *unicodeString, *bytesString, *resultString,
   1740             *offsetsString, *resultOffsetsString,
   1741             *invalidCharsString, *resultInvalidUCharsString;
   1742 
   1743         unicodeString=s=buffer;
   1744         s=printUnicode(cc.unicode, cc.unicodeLength, unicodeString);
   1745         s=printBytes(cc.bytes, cc.bytesLength, bytesString=s);
   1746         s=printBytes(result, resultLength, resultString=s);
   1747         s=printOffsets(cc.offsets, cc.bytesLength, offsetsString=s);
   1748         s=printOffsets(resultOffsets, resultLength, resultOffsetsString=s);
   1749         s=printUnicode(cc.invalidUChars, cc.invalidLength, invalidCharsString=s);
   1750         s=printUnicode(resultInvalidUChars, resultInvalidLength, resultInvalidUCharsString=s);
   1751 
   1752         if((s-buffer)>(int32_t)sizeof(buffer)) {
   1753             errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) fatal error: checkFromUnicode() test output buffer overflow writing %d chars\n",
   1754                     cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, (int)(s-buffer));
   1755             exit(1);
   1756         }
   1757 
   1758         errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) failed: %s\n"
   1759               "  unicode <%s>[%d]\n"
   1760               " expected <%s>[%d]\n"
   1761               "  result  <%s>[%d]\n"
   1762               " offsets         <%s>\n"
   1763               "  result offsets <%s>\n"
   1764               " error code expected %s got %s\n"
   1765               "  invalidChars expected <%s> got <%s>\n",
   1766               cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, name, msg,
   1767               unicodeString, cc.unicodeLength,
   1768               bytesString, cc.bytesLength,
   1769               resultString, resultLength,
   1770               offsetsString,
   1771               resultOffsetsString,
   1772               u_errorName(cc.outErrorCode), u_errorName(resultErrorCode),
   1773               invalidCharsString, resultInvalidUCharsString);
   1774 
   1775         return FALSE;
   1776     }
   1777 }
   1778 
   1779 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */
   1780