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