OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * | 3 * |
4 * Copyright (C) 2003-2013, International Business Machines | 4 * Copyright (C) 2003-2014, International Business Machines |
5 * Corporation and others. All Rights Reserved. | 5 * Corporation and others. All Rights Reserved. |
6 * | 6 * |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 * file name: convtest.cpp | 8 * file name: convtest.cpp |
9 * encoding: US-ASCII | 9 * encoding: US-ASCII |
10 * tab size: 8 (not used) | 10 * tab size: 8 (not used) |
11 * indentation:4 | 11 * indentation:4 |
12 * | 12 * |
13 * created on: 2003jul15 | 13 * created on: 2003jul15 |
14 * created by: Markus W. Scherer | 14 * created by: Markus W. Scherer |
(...skipping 14 matching lines...) Expand all Loading... |
29 * not testing conversion for a custom configuration like this should be ok. | 29 * not testing conversion for a custom configuration like this should be ok. |
30 */ | 30 */ |
31 | 31 |
32 #include "unicode/ucnv.h" | 32 #include "unicode/ucnv.h" |
33 #include "unicode/unistr.h" | 33 #include "unicode/unistr.h" |
34 #include "unicode/parsepos.h" | 34 #include "unicode/parsepos.h" |
35 #include "unicode/uniset.h" | 35 #include "unicode/uniset.h" |
36 #include "unicode/ustring.h" | 36 #include "unicode/ustring.h" |
37 #include "unicode/ures.h" | 37 #include "unicode/ures.h" |
38 #include "convtest.h" | 38 #include "convtest.h" |
| 39 #include "cmemory.h" |
39 #include "unicode/tstdtmod.h" | 40 #include "unicode/tstdtmod.h" |
40 #include <string.h> | 41 #include <string.h> |
41 #include <stdlib.h> | 42 #include <stdlib.h> |
42 | 43 |
43 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) | |
44 | |
45 enum { | 44 enum { |
46 // characters used in test data for callbacks | 45 // characters used in test data for callbacks |
47 SUB_CB='?', | 46 SUB_CB='?', |
48 SKIP_CB='0', | 47 SKIP_CB='0', |
49 STOP_CB='.', | 48 STOP_CB='.', |
50 ESC_CB='&' | 49 ESC_CB='&' |
51 }; | 50 }; |
52 | 51 |
53 ConversionTest::ConversionTest() { | 52 ConversionTest::ConversionTest() { |
54 UErrorCode errorCode=U_ZERO_ERROR; | 53 UErrorCode errorCode=U_ZERO_ERROR; |
55 utf8Cnv=ucnv_open("UTF-8", &errorCode); | 54 utf8Cnv=ucnv_open("UTF-8", &errorCode); |
56 ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &err
orCode); | 55 ucnv_setToUCallBack(utf8Cnv, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &err
orCode); |
57 if(U_FAILURE(errorCode)) { | 56 if(U_FAILURE(errorCode)) { |
58 errln("unable to open UTF-8 converter"); | 57 errln("unable to open UTF-8 converter"); |
59 } | 58 } |
60 } | 59 } |
61 | 60 |
62 ConversionTest::~ConversionTest() { | 61 ConversionTest::~ConversionTest() { |
63 ucnv_close(utf8Cnv); | 62 ucnv_close(utf8Cnv); |
64 } | 63 } |
65 | 64 |
66 void | 65 void |
67 ConversionTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
r * /*par*/) { | 66 ConversionTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
r * /*par*/) { |
68 if (exec) logln("TestSuite ConversionTest: "); | 67 if (exec) logln("TestSuite ConversionTest: "); |
69 switch (index) { | 68 switch (index) { |
70 #if !UCONFIG_NO_FILE_IO | 69 #if !UCONFIG_NO_FILE_IO |
71 case 0: name="TestToUnicode"; if (exec) TestToUnicode(); break; | 70 case 0: name="TestToUnicode"; if (exec) TestToUnicode(); break; |
72 case 1: name="TestFromUnicode"; if (exec) TestFromUnicode(); break; | 71 case 1: name="TestFromUnicode"; if (exec) TestFromUnicode(); break; |
73 case 2: name="TestGetUnicodeSet"; if (exec) TestGetUnicodeSet(); break; | 72 case 2: name="TestGetUnicodeSet"; if (exec) TestGetUnicodeSet(); break; |
| 73 case 3: name="TestDefaultIgnorableCallback"; if (exec) TestDefaultIgnora
bleCallback(); break; |
74 #else | 74 #else |
75 case 0: | 75 case 0: |
76 case 1: | 76 case 1: |
77 case 2: name="skip"; break; | 77 case 2: |
| 78 case 3: name="skip"; break; |
78 #endif | 79 #endif |
79 case 3: name="TestGetUnicodeSet2"; if (exec) TestGetUnicodeSet2(); break
; | 80 case 4: name="TestGetUnicodeSet2"; if (exec) TestGetUnicodeSet2(); break
; |
80 default: name=""; break; //needed to end loop | 81 default: name=""; break; //needed to end loop |
81 } | 82 } |
82 } | 83 } |
83 | 84 |
84 // test data interface ----------------------------------------------------- *** | 85 // test data interface ----------------------------------------------------- *** |
85 | 86 |
86 void | 87 void |
87 ConversionTest::TestToUnicode() { | 88 ConversionTest::TestToUnicode() { |
88 ConversionCase cc; | 89 ConversionCase cc; |
89 char charset[100], cbopt[4]; | 90 char charset[100], cbopt[4]; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 cc.subchar[j]=0; | 283 cc.subchar[j]=0; |
283 cc.setSub=1; | 284 cc.setSub=1; |
284 } | 285 } |
285 | 286 |
286 // remove the NUL and subchar from s | 287 // remove the NUL and subchar from s |
287 s.truncate(index); | 288 s.truncate(index); |
288 } else if((index=s.indexOf((UChar)0x3d))>0) /* '=' */ { | 289 } else if((index=s.indexOf((UChar)0x3d))>0) /* '=' */ { |
289 // read a substitution string, separated by an equal sign | 290 // read a substitution string, separated by an equal sign |
290 p=s.getBuffer()+index+1; | 291 p=s.getBuffer()+index+1; |
291 length=s.length()-(index+1); | 292 length=s.length()-(index+1); |
292 if(length<0 || length>=LENGTHOF(cc.subString)) { | 293 if(length<0 || length>=UPRV_LENGTHOF(cc.subString)) { |
293 errorCode=U_ILLEGAL_ARGUMENT_ERROR; | 294 errorCode=U_ILLEGAL_ARGUMENT_ERROR; |
294 } else { | 295 } else { |
295 u_memcpy(cc.subString, p, length); | 296 u_memcpy(cc.subString, p, length); |
296 // NUL-terminate the subString | 297 // NUL-terminate the subString |
297 cc.subString[length]=0; | 298 cc.subString[length]=0; |
298 cc.setSub=-1; | 299 cc.setSub=-1; |
299 } | 300 } |
300 | 301 |
301 // remove the equal sign and subString from s | 302 // remove the equal sign and subString from s |
302 s.truncate(index); | 303 s.truncate(index); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 charset, i, u_errorName(errorCode)); | 437 charset, i, u_errorName(errorCode)); |
437 errorCode=U_ZERO_ERROR; | 438 errorCode=U_ZERO_ERROR; |
438 continue; | 439 continue; |
439 } | 440 } |
440 | 441 |
441 // are there items that must be in cnvSet but are not? | 442 // are there items that must be in cnvSet but are not? |
442 (diffSet=mapSet).removeAll(cnvSet); | 443 (diffSet=mapSet).removeAll(cnvSet); |
443 if(!diffSet.isEmpty()) { | 444 if(!diffSet.isEmpty()) { |
444 diffSet.toPattern(s, TRUE); | 445 diffSet.toPattern(s, TRUE); |
445 if(s.length()>100) { | 446 if(s.length()>100) { |
446 s.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis))
; | 447 s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellip
sis)); |
447 } | 448 } |
448 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items -
conversion/getUnicodeSet test case %d", | 449 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items -
conversion/getUnicodeSet test case %d", |
449 charset, i); | 450 charset, i); |
450 errln(s); | 451 errln(s); |
451 } | 452 } |
452 | 453 |
453 // are there items that must not be in cnvSet but are? | 454 // are there items that must not be in cnvSet but are? |
454 (diffSet=mapnotSet).retainAll(cnvSet); | 455 (diffSet=mapnotSet).retainAll(cnvSet); |
455 if(!diffSet.isEmpty()) { | 456 if(!diffSet.isEmpty()) { |
456 diffSet.toPattern(s, TRUE); | 457 diffSet.toPattern(s, TRUE); |
457 if(s.length()>100) { | 458 if(s.length()>100) { |
458 s.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis))
; | 459 s.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ellip
sis)); |
459 } | 460 } |
460 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected
items - conversion/getUnicodeSet test case %d", | 461 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected
items - conversion/getUnicodeSet test case %d", |
461 charset, i); | 462 charset, i); |
462 errln(s); | 463 errln(s); |
463 } | 464 } |
464 } | 465 } |
465 delete testData; | 466 delete testData; |
466 } | 467 } |
467 delete dataModule; | 468 delete dataModule; |
468 } | 469 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 "HZ", | 547 "HZ", |
547 "ISO-2022-JP", | 548 "ISO-2022-JP", |
548 "JIS7", | 549 "JIS7", |
549 "ISO-2022-CN", | 550 "ISO-2022-CN", |
550 "ISO-2022-CN-EXT", | 551 "ISO-2022-CN-EXT", |
551 "LMBCS" | 552 "LMBCS" |
552 }; | 553 }; |
553 LocalUConverterPointer cnv; | 554 LocalUConverterPointer cnv; |
554 char buffer[1024]; | 555 char buffer[1024]; |
555 int32_t i; | 556 int32_t i; |
556 for(i=0; i<LENGTHOF(cnvNames); ++i) { | 557 for(i=0; i<UPRV_LENGTHOF(cnvNames); ++i) { |
557 UErrorCode errorCode=U_ZERO_ERROR; | 558 UErrorCode errorCode=U_ZERO_ERROR; |
558 cnv.adoptInstead(cnv_open(cnvNames[i], errorCode)); | 559 cnv.adoptInstead(cnv_open(cnvNames[i], errorCode)); |
559 if(U_FAILURE(errorCode)) { | 560 if(U_FAILURE(errorCode)) { |
560 errcheckln(errorCode, "failed to open converter %s - %s", cnvNames[i
], u_errorName(errorCode)); | 561 errcheckln(errorCode, "failed to open converter %s - %s", cnvNames[i
], u_errorName(errorCode)); |
561 continue; | 562 continue; |
562 } | 563 } |
563 UnicodeSet expected; | 564 UnicodeSet expected; |
564 ucnv_setFromUCallBack(cnv.getAlias(), getUnicodeSetCallback, &expected,
NULL, NULL, &errorCode); | 565 ucnv_setFromUCallBack(cnv.getAlias(), getUnicodeSetCallback, &expected,
NULL, NULL, &errorCode); |
565 if(U_FAILURE(errorCode)) { | 566 if(U_FAILURE(errorCode)) { |
566 errln("failed to set the callback on converter %s - %s", cnvNames[i]
, u_errorName(errorCode)); | 567 errln("failed to set the callback on converter %s - %s", cnvNames[i]
, u_errorName(errorCode)); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 } | 617 } |
617 if(set!=expected) { | 618 if(set!=expected) { |
618 UnicodeSet diffSet; | 619 UnicodeSet diffSet; |
619 UnicodeString out; | 620 UnicodeString out; |
620 | 621 |
621 // are there items that must be in the set but are not? | 622 // are there items that must be in the set but are not? |
622 (diffSet=expected).removeAll(set); | 623 (diffSet=expected).removeAll(set); |
623 if(!diffSet.isEmpty()) { | 624 if(!diffSet.isEmpty()) { |
624 diffSet.toPattern(out, TRUE); | 625 diffSet.toPattern(out, TRUE); |
625 if(out.length()>100) { | 626 if(out.length()>100) { |
626 out.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis
)); | 627 out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ell
ipsis)); |
627 } | 628 } |
628 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items -
which set: %d", | 629 errln("error: ucnv_getUnicodeSet(\"%s\") is missing items -
which set: %d", |
629 cnvNames[i], which); | 630 cnvNames[i], which); |
630 errln(out); | 631 errln(out); |
631 } | 632 } |
632 | 633 |
633 // are there items that must not be in the set but are? | 634 // are there items that must not be in the set but are? |
634 (diffSet=set).removeAll(expected); | 635 (diffSet=set).removeAll(expected); |
635 if(!diffSet.isEmpty()) { | 636 if(!diffSet.isEmpty()) { |
636 diffSet.toPattern(out, TRUE); | 637 diffSet.toPattern(out, TRUE); |
637 if(out.length()>100) { | 638 if(out.length()>100) { |
638 out.replace(100, 0x7fffffff, ellipsis, LENGTHOF(ellipsis
)); | 639 out.replace(100, 0x7fffffff, ellipsis, UPRV_LENGTHOF(ell
ipsis)); |
639 } | 640 } |
640 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected
items - which set: %d", | 641 errln("error: ucnv_getUnicodeSet(\"%s\") contains unexpected
items - which set: %d", |
641 cnvNames[i], which); | 642 cnvNames[i], which); |
642 errln(out); | 643 errln(out); |
643 } | 644 } |
644 } | 645 } |
645 } | 646 } |
646 } | 647 } |
647 | 648 |
648 delete [] s0; | 649 delete [] s0; |
649 } | 650 } |
650 | 651 |
| 652 // Test all codepoints which has the default ignorable Unicode property are igno
red if they have no mapping |
| 653 // If there are any failures, the hard coded list (IS_DEFAULT_IGNORABLE_CODE_POI
NT) in ucnv_err.c should be updated |
| 654 void |
| 655 ConversionTest::TestDefaultIgnorableCallback() { |
| 656 UErrorCode status = U_ZERO_ERROR; |
| 657 const char *cnv_name = "euc-jp-2007"; |
| 658 const char *pattern_ignorable = "[:Default_Ignorable_Code_Point:]"; |
| 659 const char *pattern_not_ignorable = "[:^Default_Ignorable_Code_Point:]"; |
| 660 |
| 661 UnicodeSet *set_ignorable = new UnicodeSet(pattern_ignorable, status); |
| 662 if (U_FAILURE(status)) { |
| 663 dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_ignorable, u
_errorName(status)); |
| 664 return; |
| 665 } |
| 666 |
| 667 UnicodeSet *set_not_ignorable = new UnicodeSet(pattern_not_ignorable, status
); |
| 668 if (U_FAILURE(status)) { |
| 669 dataerrln("Unable to create Unicodeset: %s - %s\n", pattern_not_ignorabl
e, u_errorName(status)); |
| 670 return; |
| 671 } |
| 672 |
| 673 UConverter *cnv = cnv_open(cnv_name, status); |
| 674 if (U_FAILURE(status)) { |
| 675 dataerrln("Unable to open converter: %s - %s\n", cnv_name, u_errorName(s
tatus)); |
| 676 return; |
| 677 } |
| 678 |
| 679 // set callback for the converter |
| 680 ucnv_setFromUCallBack(cnv, UCNV_FROM_U_CALLBACK_SUBSTITUTE, NULL, NULL, NULL
, &status); |
| 681 |
| 682 UChar32 input[1]; |
| 683 char output[10]; |
| 684 int32_t outputLength; |
| 685 |
| 686 // test default ignorables are ignored |
| 687 int size = set_ignorable->size(); |
| 688 for (int i = 0; i < size; i++) { |
| 689 status = U_ZERO_ERROR; |
| 690 outputLength= 0; |
| 691 |
| 692 input[0] = set_ignorable->charAt(i); |
| 693 |
| 694 outputLength = ucnv_fromUChars(cnv, output, 10, UnicodeString::fromUTF32
(input, 1).getTerminatedBuffer(), -1, &status); |
| 695 if (U_FAILURE(status) || outputLength != 0) { |
| 696 errln("Ignorable code point: U+%04X not skipped as expected - %s", i
nput[0], u_errorName(status)); |
| 697 } |
| 698 } |
| 699 |
| 700 // test non-ignorables are not ignored |
| 701 size = set_not_ignorable->size(); |
| 702 for (int i = 0; i < size; i++) { |
| 703 status = U_ZERO_ERROR; |
| 704 outputLength= 0; |
| 705 |
| 706 input[0] = set_not_ignorable->charAt(i); |
| 707 |
| 708 if (input[0] == 0) { |
| 709 continue; |
| 710 } |
| 711 |
| 712 outputLength = ucnv_fromUChars(cnv, output, 10, UnicodeString::fromUTF32
(input, 1).getTerminatedBuffer(), -1, &status); |
| 713 if (U_FAILURE(status) || outputLength <= 0) { |
| 714 errln("Non-ignorable code point: U+%04X skipped unexpectedly - %s",
input[0], u_errorName(status)); |
| 715 } |
| 716 } |
| 717 |
| 718 ucnv_close(cnv); |
| 719 delete set_not_ignorable; |
| 720 delete set_ignorable; |
| 721 } |
| 722 |
651 // open testdata or ICU data converter ------------------------------------- *** | 723 // open testdata or ICU data converter ------------------------------------- *** |
652 | 724 |
653 UConverter * | 725 UConverter * |
654 ConversionTest::cnv_open(const char *name, UErrorCode &errorCode) { | 726 ConversionTest::cnv_open(const char *name, UErrorCode &errorCode) { |
655 if(name!=NULL && *name=='+') { | 727 if(name!=NULL && *name=='+') { |
656 // Converter names that start with '+' are ignored in ICU4J tests. | 728 // Converter names that start with '+' are ignored in ICU4J tests. |
657 ++name; | 729 ++name; |
658 } | 730 } |
659 if(name!=NULL && *name=='*') { | 731 if(name!=NULL && *name=='*') { |
660 /* loadTestData(): set the data directory */ | 732 /* loadTestData(): set the data directory */ |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
983 { -2, "toU(bulk)+getNext" }, | 1055 { -2, "toU(bulk)+getNext" }, |
984 { -3, "getNext+toU(bulk)" }, | 1056 { -3, "getNext+toU(bulk)" }, |
985 { -4, "toU(1)+getNext" }, | 1057 { -4, "toU(1)+getNext" }, |
986 { -5, "getNext+toU(1)" }, | 1058 { -5, "getNext+toU(1)" }, |
987 { -12, "toU(5)+getNext" }, | 1059 { -12, "toU(5)+getNext" }, |
988 { -13, "getNext+toU(5)" }, | 1060 { -13, "getNext+toU(5)" }, |
989 }; | 1061 }; |
990 int32_t i, step; | 1062 int32_t i, step; |
991 | 1063 |
992 ok=TRUE; | 1064 ok=TRUE; |
993 for(i=0; i<LENGTHOF(steps) && ok; ++i) { | 1065 for(i=0; i<UPRV_LENGTHOF(steps) && ok; ++i) { |
994 step=steps[i].step; | 1066 step=steps[i].step; |
995 if(step<0 && !cc.finalFlush) { | 1067 if(step<0 && !cc.finalFlush) { |
996 // skip ucnv_getNextUChar() if !finalFlush because | 1068 // skip ucnv_getNextUChar() if !finalFlush because |
997 // ucnv_getNextUChar() always implies flush | 1069 // ucnv_getNextUChar() always implies flush |
998 continue; | 1070 continue; |
999 } | 1071 } |
1000 if(step!=0) { | 1072 if(step!=0) { |
1001 // bulk test is first, then offsets are not checked any more | 1073 // bulk test is first, then offsets are not checked any more |
1002 cc.offsets=NULL; | 1074 cc.offsets=NULL; |
1003 } | 1075 } |
1004 else { | 1076 else { |
1005 memset(resultOffsets, -1, LENGTHOF(resultOffsets)); | 1077 memset(resultOffsets, -1, UPRV_LENGTHOF(resultOffsets)); |
1006 } | 1078 } |
1007 memset(result, -1, LENGTHOF(result)); | 1079 memset(result, -1, UPRV_LENGTHOF(result)); |
1008 errorCode.reset(); | 1080 errorCode.reset(); |
1009 resultLength=stepToUnicode(cc, cnv.getAlias(), | 1081 resultLength=stepToUnicode(cc, cnv.getAlias(), |
1010 result, LENGTHOF(result), | 1082 result, UPRV_LENGTHOF(result), |
1011 step==0 ? resultOffsets : NULL, | 1083 step==0 ? resultOffsets : NULL, |
1012 step, errorCode); | 1084 step, errorCode); |
1013 ok=checkToUnicode( | 1085 ok=checkToUnicode( |
1014 cc, cnv.getAlias(), steps[i].name, | 1086 cc, cnv.getAlias(), steps[i].name, |
1015 result, resultLength, | 1087 result, resultLength, |
1016 cc.offsets!=NULL ? resultOffsets : NULL, | 1088 cc.offsets!=NULL ? resultOffsets : NULL, |
1017 errorCode); | 1089 errorCode); |
1018 if(errorCode.isFailure() || !cc.finalFlush) { | 1090 if(errorCode.isFailure() || !cc.finalFlush) { |
1019 // reset if an error occurred or we did not flush | 1091 // reset if an error occurred or we did not flush |
1020 // otherwise do nothing to make sure that flushing resets | 1092 // otherwise do nothing to make sure that flushing resets |
1021 ucnv_resetToUnicode(cnv.getAlias()); | 1093 ucnv_resetToUnicode(cnv.getAlias()); |
1022 } | 1094 } |
1023 if (cc.offsets != NULL && resultOffsets[resultLength] != -1) { | 1095 if (cc.offsets != NULL && resultOffsets[resultLength] != -1) { |
1024 errln("toUnicode[%d](%s) Conversion wrote too much to offsets at ind
ex %d", | 1096 errln("toUnicode[%d](%s) Conversion wrote too much to offsets at ind
ex %d", |
1025 cc.caseNr, cc.charset, resultLength); | 1097 cc.caseNr, cc.charset, resultLength); |
1026 } | 1098 } |
1027 if (result[resultLength] != (UChar)-1) { | 1099 if (result[resultLength] != (UChar)-1) { |
1028 errln("toUnicode[%d](%s) Conversion wrote too much to result at inde
x %d", | 1100 errln("toUnicode[%d](%s) Conversion wrote too much to result at inde
x %d", |
1029 cc.caseNr, cc.charset, resultLength); | 1101 cc.caseNr, cc.charset, resultLength); |
1030 } | 1102 } |
1031 } | 1103 } |
1032 | 1104 |
1033 // not a real loop, just a convenience for breaking out of the block | 1105 // not a real loop, just a convenience for breaking out of the block |
1034 while(ok && cc.finalFlush) { | 1106 while(ok && cc.finalFlush) { |
1035 // test ucnv_toUChars() | 1107 // test ucnv_toUChars() |
1036 memset(result, 0, sizeof(result)); | 1108 memset(result, 0, sizeof(result)); |
1037 | 1109 |
1038 errorCode.reset(); | 1110 errorCode.reset(); |
1039 resultLength=ucnv_toUChars(cnv.getAlias(), | 1111 resultLength=ucnv_toUChars(cnv.getAlias(), |
1040 result, LENGTHOF(result), | 1112 result, UPRV_LENGTHOF(result), |
1041 (const char *)cc.bytes, cc.bytesLength, | 1113 (const char *)cc.bytes, cc.bytesLength, |
1042 errorCode); | 1114 errorCode); |
1043 ok=checkToUnicode( | 1115 ok=checkToUnicode( |
1044 cc, cnv.getAlias(), "toUChars", | 1116 cc, cnv.getAlias(), "toUChars", |
1045 result, resultLength, | 1117 result, resultLength, |
1046 NULL, | 1118 NULL, |
1047 errorCode); | 1119 errorCode); |
1048 if(!ok) { | 1120 if(!ok) { |
1049 break; | 1121 break; |
1050 } | 1122 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1177 // move only one buffer (in vs. out) at a time to be extra mean | 1249 // move only one buffer (in vs. out) at a time to be extra mean |
1178 // step==0 performs bulk conversion | 1250 // step==0 performs bulk conversion |
1179 | 1251 |
1180 // initialize the partial limits for the loop | 1252 // initialize the partial limits for the loop |
1181 if(step==0) { | 1253 if(step==0) { |
1182 // use the entire buffers | 1254 // use the entire buffers |
1183 sourceLimit=utf8Limit; | 1255 sourceLimit=utf8Limit; |
1184 targetLimit=resultLimit; | 1256 targetLimit=resultLimit; |
1185 flush=cc.finalFlush; | 1257 flush=cc.finalFlush; |
1186 | 1258 |
1187 pivotLimit=pivotBuffer+LENGTHOF(pivotBuffer); | 1259 pivotLimit=pivotBuffer+UPRV_LENGTHOF(pivotBuffer); |
1188 } else { | 1260 } else { |
1189 // start with empty partial buffers | 1261 // start with empty partial buffers |
1190 sourceLimit=source; | 1262 sourceLimit=source; |
1191 targetLimit=target; | 1263 targetLimit=target; |
1192 flush=FALSE; | 1264 flush=FALSE; |
1193 | 1265 |
1194 // empty pivot is not allowed, make it of length step | 1266 // empty pivot is not allowed, make it of length step |
1195 pivotLimit=pivotBuffer+step; | 1267 pivotLimit=pivotBuffer+step; |
1196 } | 1268 } |
1197 | 1269 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1396 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstStr
ing() failed - %s", | 1468 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d) ucnv_setSubstStr
ing() failed - %s", |
1397 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush
, u_errorName(errorCode)); | 1469 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush
, u_errorName(errorCode)); |
1398 ucnv_close(cnv); | 1470 ucnv_close(cnv); |
1399 return FALSE; | 1471 return FALSE; |
1400 } | 1472 } |
1401 } | 1473 } |
1402 | 1474 |
1403 // convert unicode to utf8 | 1475 // convert unicode to utf8 |
1404 char utf8[256]; | 1476 char utf8[256]; |
1405 cc.utf8=utf8; | 1477 cc.utf8=utf8; |
1406 u_strToUTF8(utf8, LENGTHOF(utf8), &cc.utf8Length, | 1478 u_strToUTF8(utf8, UPRV_LENGTHOF(utf8), &cc.utf8Length, |
1407 cc.unicode, cc.unicodeLength, | 1479 cc.unicode, cc.unicodeLength, |
1408 &errorCode); | 1480 &errorCode); |
1409 if(U_FAILURE(errorCode)) { | 1481 if(U_FAILURE(errorCode)) { |
1410 // skip UTF-8 testing of a string with an unpaired surrogate, | 1482 // skip UTF-8 testing of a string with an unpaired surrogate, |
1411 // or of one that's too long | 1483 // or of one that's too long |
1412 // toUnicode errors are tested in cintltst TestConvertExFromUTF8() | 1484 // toUnicode errors are tested in cintltst TestConvertExFromUTF8() |
1413 cc.utf8Length=-1; | 1485 cc.utf8Length=-1; |
1414 } | 1486 } |
1415 | 1487 |
1416 int32_t resultOffsets[256]; | 1488 int32_t resultOffsets[256]; |
1417 char result[256]; | 1489 char result[256]; |
1418 int32_t resultLength; | 1490 int32_t resultLength; |
1419 UBool ok; | 1491 UBool ok; |
1420 | 1492 |
1421 static const struct { | 1493 static const struct { |
1422 int32_t step; | 1494 int32_t step; |
1423 const char *name, *utf8Name; | 1495 const char *name, *utf8Name; |
1424 } steps[]={ | 1496 } steps[]={ |
1425 { 0, "bulk", "utf8" }, // must be first for offsets to be checked | 1497 { 0, "bulk", "utf8" }, // must be first for offsets to be checked |
1426 { 1, "step=1", "utf8 step=1" }, | 1498 { 1, "step=1", "utf8 step=1" }, |
1427 { 3, "step=3", "utf8 step=3" }, | 1499 { 3, "step=3", "utf8 step=3" }, |
1428 { 7, "step=7", "utf8 step=7" } | 1500 { 7, "step=7", "utf8 step=7" } |
1429 }; | 1501 }; |
1430 int32_t i, step; | 1502 int32_t i, step; |
1431 | 1503 |
1432 ok=TRUE; | 1504 ok=TRUE; |
1433 for(i=0; i<LENGTHOF(steps) && ok; ++i) { | 1505 for(i=0; i<UPRV_LENGTHOF(steps) && ok; ++i) { |
1434 step=steps[i].step; | 1506 step=steps[i].step; |
1435 memset(resultOffsets, -1, LENGTHOF(resultOffsets)); | 1507 memset(resultOffsets, -1, UPRV_LENGTHOF(resultOffsets)); |
1436 memset(result, -1, LENGTHOF(result)); | 1508 memset(result, -1, UPRV_LENGTHOF(result)); |
1437 errorCode=U_ZERO_ERROR; | 1509 errorCode=U_ZERO_ERROR; |
1438 resultLength=stepFromUnicode(cc, cnv, | 1510 resultLength=stepFromUnicode(cc, cnv, |
1439 result, LENGTHOF(result), | 1511 result, UPRV_LENGTHOF(result), |
1440 step==0 ? resultOffsets : NULL, | 1512 step==0 ? resultOffsets : NULL, |
1441 step, &errorCode); | 1513 step, &errorCode); |
1442 ok=checkFromUnicode( | 1514 ok=checkFromUnicode( |
1443 cc, cnv, steps[i].name, | 1515 cc, cnv, steps[i].name, |
1444 (uint8_t *)result, resultLength, | 1516 (uint8_t *)result, resultLength, |
1445 cc.offsets!=NULL ? resultOffsets : NULL, | 1517 cc.offsets!=NULL ? resultOffsets : NULL, |
1446 errorCode); | 1518 errorCode); |
1447 if(U_FAILURE(errorCode) || !cc.finalFlush) { | 1519 if(U_FAILURE(errorCode) || !cc.finalFlush) { |
1448 // reset if an error occurred or we did not flush | 1520 // reset if an error occurred or we did not flush |
1449 // otherwise do nothing to make sure that flushing resets | 1521 // otherwise do nothing to make sure that flushing resets |
1450 ucnv_resetFromUnicode(cnv); | 1522 ucnv_resetFromUnicode(cnv); |
1451 } | 1523 } |
1452 if (resultOffsets[resultLength] != -1) { | 1524 if (resultOffsets[resultLength] != -1) { |
1453 errln("fromUnicode[%d](%s) Conversion wrote too much to offsets at i
ndex %d", | 1525 errln("fromUnicode[%d](%s) Conversion wrote too much to offsets at i
ndex %d", |
1454 cc.caseNr, cc.charset, resultLength); | 1526 cc.caseNr, cc.charset, resultLength); |
1455 } | 1527 } |
1456 if (result[resultLength] != (char)-1) { | 1528 if (result[resultLength] != (char)-1) { |
1457 errln("fromUnicode[%d](%s) Conversion wrote too much to result at in
dex %d", | 1529 errln("fromUnicode[%d](%s) Conversion wrote too much to result at in
dex %d", |
1458 cc.caseNr, cc.charset, resultLength); | 1530 cc.caseNr, cc.charset, resultLength); |
1459 } | 1531 } |
1460 | 1532 |
1461 // bulk test is first, then offsets are not checked any more | 1533 // bulk test is first, then offsets are not checked any more |
1462 cc.offsets=NULL; | 1534 cc.offsets=NULL; |
1463 | 1535 |
1464 // test direct conversion from UTF-8 | 1536 // test direct conversion from UTF-8 |
1465 if(cc.utf8Length>=0) { | 1537 if(cc.utf8Length>=0) { |
1466 errorCode=U_ZERO_ERROR; | 1538 errorCode=U_ZERO_ERROR; |
1467 resultLength=stepFromUTF8(cc, utf8Cnv, cnv, | 1539 resultLength=stepFromUTF8(cc, utf8Cnv, cnv, |
1468 result, LENGTHOF(result), | 1540 result, UPRV_LENGTHOF(result), |
1469 step, &errorCode); | 1541 step, &errorCode); |
1470 ok=checkFromUnicode( | 1542 ok=checkFromUnicode( |
1471 cc, cnv, steps[i].utf8Name, | 1543 cc, cnv, steps[i].utf8Name, |
1472 (uint8_t *)result, resultLength, | 1544 (uint8_t *)result, resultLength, |
1473 NULL, | 1545 NULL, |
1474 errorCode); | 1546 errorCode); |
1475 if(U_FAILURE(errorCode) || !cc.finalFlush) { | 1547 if(U_FAILURE(errorCode) || !cc.finalFlush) { |
1476 // reset if an error occurred or we did not flush | 1548 // reset if an error occurred or we did not flush |
1477 // otherwise do nothing to make sure that flushing resets | 1549 // otherwise do nothing to make sure that flushing resets |
1478 ucnv_resetToUnicode(utf8Cnv); | 1550 ucnv_resetToUnicode(utf8Cnv); |
1479 ucnv_resetFromUnicode(cnv); | 1551 ucnv_resetFromUnicode(cnv); |
1480 } | 1552 } |
1481 } | 1553 } |
1482 } | 1554 } |
1483 | 1555 |
1484 // not a real loop, just a convenience for breaking out of the block | 1556 // not a real loop, just a convenience for breaking out of the block |
1485 while(ok && cc.finalFlush) { | 1557 while(ok && cc.finalFlush) { |
1486 // test ucnv_fromUChars() | 1558 // test ucnv_fromUChars() |
1487 memset(result, 0, sizeof(result)); | 1559 memset(result, 0, sizeof(result)); |
1488 | 1560 |
1489 errorCode=U_ZERO_ERROR; | 1561 errorCode=U_ZERO_ERROR; |
1490 resultLength=ucnv_fromUChars(cnv, | 1562 resultLength=ucnv_fromUChars(cnv, |
1491 result, LENGTHOF(result), | 1563 result, UPRV_LENGTHOF(result), |
1492 cc.unicode, cc.unicodeLength, | 1564 cc.unicode, cc.unicodeLength, |
1493 &errorCode); | 1565 &errorCode); |
1494 ok=checkFromUnicode( | 1566 ok=checkFromUnicode( |
1495 cc, cnv, "fromUChars", | 1567 cc, cnv, "fromUChars", |
1496 (uint8_t *)result, resultLength, | 1568 (uint8_t *)result, resultLength, |
1497 NULL, | 1569 NULL, |
1498 errorCode); | 1570 errorCode); |
1499 if(!ok) { | 1571 if(!ok) { |
1500 break; | 1572 break; |
1501 } | 1573 } |
(...skipping 28 matching lines...) Expand all Loading... |
1530 UChar resultInvalidUChars[8]; | 1602 UChar resultInvalidUChars[8]; |
1531 int8_t resultInvalidLength; | 1603 int8_t resultInvalidLength; |
1532 UErrorCode errorCode; | 1604 UErrorCode errorCode; |
1533 | 1605 |
1534 const char *msg; | 1606 const char *msg; |
1535 | 1607 |
1536 // reset the message; NULL will mean "ok" | 1608 // reset the message; NULL will mean "ok" |
1537 msg=NULL; | 1609 msg=NULL; |
1538 | 1610 |
1539 errorCode=U_ZERO_ERROR; | 1611 errorCode=U_ZERO_ERROR; |
1540 resultInvalidLength=LENGTHOF(resultInvalidUChars); | 1612 resultInvalidLength=UPRV_LENGTHOF(resultInvalidUChars); |
1541 ucnv_getInvalidUChars(cnv, resultInvalidUChars, &resultInvalidLength, &error
Code); | 1613 ucnv_getInvalidUChars(cnv, resultInvalidUChars, &resultInvalidLength, &error
Code); |
1542 if(U_FAILURE(errorCode)) { | 1614 if(U_FAILURE(errorCode)) { |
1543 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUC
hars() failed - %s", | 1615 errln("fromUnicode[%d](%s cb=\"%s\" fb=%d flush=%d %s) ucnv_getInvalidUC
hars() failed - %s", |
1544 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, na
me, u_errorName(errorCode)); | 1616 cc.caseNr, cc.charset, cc.cbopt, cc.fallbacks, cc.finalFlush, na
me, u_errorName(errorCode)); |
1545 return FALSE; | 1617 return FALSE; |
1546 } | 1618 } |
1547 | 1619 |
1548 // check everything that might have gone wrong | 1620 // check everything that might have gone wrong |
1549 if(cc.bytesLength!=resultLength) { | 1621 if(cc.bytesLength!=resultLength) { |
1550 msg="wrong result length"; | 1622 msg="wrong result length"; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1598 offsetsString, | 1670 offsetsString, |
1599 resultOffsetsString, | 1671 resultOffsetsString, |
1600 u_errorName(cc.outErrorCode), u_errorName(resultErrorCode), | 1672 u_errorName(cc.outErrorCode), u_errorName(resultErrorCode), |
1601 invalidCharsString, resultInvalidUCharsString); | 1673 invalidCharsString, resultInvalidUCharsString); |
1602 | 1674 |
1603 return FALSE; | 1675 return FALSE; |
1604 } | 1676 } |
1605 } | 1677 } |
1606 | 1678 |
1607 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */ | 1679 #endif /* #if !UCONFIG_NO_LEGACY_CONVERSION */ |
OLD | NEW |